1 /*
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 
17 package com.example.android.systemupdatersample;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertSame;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.content.Context;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.example.android.systemupdatersample.tests.R;
30 import com.google.common.io.CharStreams;
31 
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 
41 /**
42  * Tests for {@link UpdateConfig}
43  */
44 @RunWith(AndroidJUnit4.class)
45 @SmallTest
46 public class UpdateConfigTest {
47 
48     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 } }";
54 
55     @Rule
56     public final ExpectedException thrown = ExpectedException.none();
57 
58     private Context mContext;
59     private Context mTargetContext;
60     private String mJsonStreaming001;
61 
62     @Before
setUp()63     public void setUp() throws Exception {
64         mContext = InstrumentationRegistry.getContext();
65         mTargetContext = InstrumentationRegistry.getTargetContext();
66         mJsonStreaming001 = readResource(R.raw.update_config_001_stream);
67     }
68 
69     @Test
fromJson_parsesNonStreaming()70     public void fromJson_parsesNonStreaming() throws Exception {
71         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());
74         assertSame("type is parsed",
75                 UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
76                 config.getInstallType());
77         assertEquals("url is parsed", "file:///my-builds/a.zip", config.getUrl());
78     }
79 
80     @Test
fromJson_parsesStreaming()81     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",
87                 config.getAbConfig().getPropertyFiles()[0].getFilename());
88         assertEquals(195, config.getAbConfig().getPropertyFiles()[0].getOffset());
89         assertEquals(8, config.getAbConfig().getPropertyFiles()[0].getSize());
90         assertTrue(config.getAbConfig().getForceSwitchSlot());
91     }
92 
93     @Test
getUpdatePackageFile_throwsErrorIfStreaming()94     public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
95         UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
96         thrown.expect(RuntimeException.class);
97         config.getUpdatePackageFile();
98     }
99 
100     @Test
getUpdatePackageFile_throwsErrorIfNotAFile()101     public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
102         String json = "{"
103                 + " \"name\": \"upd\", \"url\": \"http://foo.bar\","
104                 + " \"ab_install_type\": \"NON_STREAMING\","
105                 + " \"ab_config\": {"
106                 + "     \"force_switch_slot\": false,"
107                 + "     \"verify_payload_metadata\": false } }";
108         UpdateConfig config = UpdateConfig.fromJson(json);
109         thrown.expect(RuntimeException.class);
110         config.getUpdatePackageFile();
111     }
112 
113     @Test
getUpdatePackageFile_works()114     public void getUpdatePackageFile_works() throws Exception {
115         UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
116         assertEquals("/my-builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
117     }
118 
readResource(int id)119     private String readResource(int id) throws IOException {
120         return CharStreams.toString(new InputStreamReader(
121             mContext.getResources().openRawResource(id)));
122     }
123 }
124