blob: 1cbd8601e7b8caf4e39be3161d877f1a2f42729b [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;
24import android.support.test.InstrumentationRegistry;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070025import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070028import com.example.android.systemupdatersample.tests.R;
29import com.google.common.io.CharStreams;
30
31import org.junit.Before;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070032import org.junit.Rule;
33import org.junit.Test;
34import org.junit.rules.ExpectedException;
35import org.junit.runner.RunWith;
36
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070037import java.io.IOException;
38import java.io.InputStreamReader;
39
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070040/**
41 * Tests for {@link UpdateConfig}
42 */
43@RunWith(AndroidJUnit4.class)
44@SmallTest
45public class UpdateConfigTest {
46
47 private static final String JSON_NON_STREAMING =
48 "{\"name\": \"vip update\", \"url\": \"file:///builds/a.zip\", "
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070049 + " \"ab_install_type\": \"NON_STREAMING\","
50 + " \"ab_config\": { \"force_switch_slot\": false } }";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070051
52 @Rule
53 public final ExpectedException thrown = ExpectedException.none();
54
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070055 private Context mContext;
56 private Context mTargetContext;
57 private String mJsonStreaming001;
58
59 @Before
60 public void setUp() throws Exception {
61 mContext = InstrumentationRegistry.getContext();
62 mTargetContext = InstrumentationRegistry.getTargetContext();
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070063 mJsonStreaming001 = readResource(R.raw.update_config_001_stream);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070064 }
65
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070066 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070067 public void fromJson_parsesNonStreaming() throws Exception {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070068 UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING);
69 assertEquals("name is parsed", "vip update", config.getName());
70 assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson());
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070071 assertSame("type is parsed",
72 UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
73 config.getInstallType());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070074 assertEquals("url is parsed", "file:///builds/a.zip", config.getUrl());
75 }
76
77 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070078 public void fromJson_parsesStreaming() throws Exception {
79 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
80 assertEquals("streaming-001", config.getName());
81 assertEquals("http://foo.bar/update.zip", config.getUrl());
82 assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType());
83 assertEquals("payload.bin",
84 config.getStreamingMetadata().getPropertyFiles()[0].getFilename());
85 assertEquals(195, config.getStreamingMetadata().getPropertyFiles()[0].getOffset());
86 assertEquals(8, config.getStreamingMetadata().getPropertyFiles()[0].getSize());
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070087 assertTrue(config.getAbConfig().getForceSwitchSlot());
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070088 }
89
90 @Test
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070091 public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070092 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070093 thrown.expect(RuntimeException.class);
94 config.getUpdatePackageFile();
95 }
96
97 @Test
98 public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
99 String json = "{\"name\": \"upd\", \"url\": \"http://foo.bar\","
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -0700100 + " \"ab_install_type\": \"NON_STREAMING\","
101 + " \"ab_config\": { \"force_switch_slot\": false } }";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700102 UpdateConfig config = UpdateConfig.fromJson(json);
103 thrown.expect(RuntimeException.class);
104 config.getUpdatePackageFile();
105 }
106
107 @Test
108 public void getUpdatePackageFile_works() throws Exception {
109 UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700110 assertEquals("/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700111 }
112
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700113 private String readResource(int id) throws IOException {
114 return CharStreams.toString(new InputStreamReader(
115 mContext.getResources().openRawResource(id)));
116 }
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700117}