blob: ae666830be464b1d61e71e7251dbdc3f8985beec [file] [log] [blame]
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.systemupdatersample;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertSame;
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070021import static org.junit.Assert.assertTrue;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070022
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070023import android.content.Context;
koushik panugantid5c7fb52018-12-12 10:39:44 -080024
25import androidx.test.InstrumentationRegistry;
26import androidx.test.filters.SmallTest;
27import androidx.test.runner.AndroidJUnit4;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070028
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070029import com.example.android.systemupdatersample.tests.R;
30import com.google.common.io.CharStreams;
31
32import org.junit.Before;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070033import org.junit.Rule;
34import org.junit.Test;
35import org.junit.rules.ExpectedException;
36import org.junit.runner.RunWith;
37
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070038import java.io.IOException;
39import java.io.InputStreamReader;
40
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070041/**
42 * Tests for {@link UpdateConfig}
43 */
44@RunWith(AndroidJUnit4.class)
45@SmallTest
46public class UpdateConfigTest {
47
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -070048 private static final String JSON_NON_STREAMING = "{"
49 + " \"name\": \"vip update\", \"url\": \"file:///my-builds/a.zip\","
50 + " \"ab_install_type\": \"NON_STREAMING\","
51 + " \"ab_config\": {"
52 + " \"force_switch_slot\": false,"
53 + " \"verify_payload_metadata\": false } }";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070054
55 @Rule
56 public final ExpectedException thrown = ExpectedException.none();
57
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070058 private Context mContext;
59 private Context mTargetContext;
60 private String mJsonStreaming001;
61
62 @Before
63 public void setUp() throws Exception {
64 mContext = InstrumentationRegistry.getContext();
65 mTargetContext = InstrumentationRegistry.getTargetContext();
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070066 mJsonStreaming001 = readResource(R.raw.update_config_001_stream);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070067 }
68
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070069 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070070 public void fromJson_parsesNonStreaming() throws Exception {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070071 UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING);
72 assertEquals("name is parsed", "vip update", config.getName());
73 assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson());
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070074 assertSame("type is parsed",
75 UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
76 config.getInstallType());
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -070077 assertEquals("url is parsed", "file:///my-builds/a.zip", config.getUrl());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070078 }
79
80 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070081 public void fromJson_parsesStreaming() throws Exception {
82 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
83 assertEquals("streaming-001", config.getName());
84 assertEquals("http://foo.bar/update.zip", config.getUrl());
85 assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType());
86 assertEquals("payload.bin",
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -070087 config.getAbConfig().getPropertyFiles()[0].getFilename());
88 assertEquals(195, config.getAbConfig().getPropertyFiles()[0].getOffset());
89 assertEquals(8, config.getAbConfig().getPropertyFiles()[0].getSize());
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070090 assertTrue(config.getAbConfig().getForceSwitchSlot());
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070091 }
92
93 @Test
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070094 public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070095 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070096 thrown.expect(RuntimeException.class);
97 config.getUpdatePackageFile();
98 }
99
100 @Test
101 public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -0700102 String json = "{"
103 + " \"name\": \"upd\", \"url\": \"http://foo.bar\","
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -0700104 + " \"ab_install_type\": \"NON_STREAMING\","
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -0700105 + " \"ab_config\": {"
106 + " \"force_switch_slot\": false,"
107 + " \"verify_payload_metadata\": false } }";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700108 UpdateConfig config = UpdateConfig.fromJson(json);
109 thrown.expect(RuntimeException.class);
110 config.getUpdatePackageFile();
111 }
112
113 @Test
114 public void getUpdatePackageFile_works() throws Exception {
115 UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -0700116 assertEquals("/my-builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700117 }
118
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700119 private String readResource(int id) throws IOException {
120 return CharStreams.toString(new InputStreamReader(
121 mContext.getResources().openRawResource(id)));
122 }
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700123}