blob: 0975e76be7b44dfdadef65cac5063982392a44f5 [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;
21
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070022import android.content.Context;
23import android.support.test.InstrumentationRegistry;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070024import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070027import com.example.android.systemupdatersample.tests.R;
28import com.google.common.io.CharStreams;
29
30import org.junit.Before;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070031import org.junit.Rule;
32import org.junit.Test;
33import org.junit.rules.ExpectedException;
34import org.junit.runner.RunWith;
35
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070036import java.io.IOException;
37import java.io.InputStreamReader;
38
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070039/**
40 * Tests for {@link UpdateConfig}
41 */
42@RunWith(AndroidJUnit4.class)
43@SmallTest
44public class UpdateConfigTest {
45
46 private static final String JSON_NON_STREAMING =
47 "{\"name\": \"vip update\", \"url\": \"file:///builds/a.zip\", "
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070048 + " \"ab_install_type\": \"NON_STREAMING\"}";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070049
50 @Rule
51 public final ExpectedException thrown = ExpectedException.none();
52
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070053 private Context mContext;
54 private Context mTargetContext;
55 private String mJsonStreaming001;
56
57 @Before
58 public void setUp() throws Exception {
59 mContext = InstrumentationRegistry.getContext();
60 mTargetContext = InstrumentationRegistry.getTargetContext();
61 mJsonStreaming001 = readResource(R.raw.update_config_stream_001);
62 }
63
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070064 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070065 public void fromJson_parsesNonStreaming() throws Exception {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070066 UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING);
67 assertEquals("name is parsed", "vip update", config.getName());
68 assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson());
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070069 assertSame("type is parsed",
70 UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
71 config.getInstallType());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070072 assertEquals("url is parsed", "file:///builds/a.zip", config.getUrl());
73 }
74
75 @Test
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070076 public void fromJson_parsesStreaming() throws Exception {
77 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
78 assertEquals("streaming-001", config.getName());
79 assertEquals("http://foo.bar/update.zip", config.getUrl());
80 assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType());
81 assertEquals("payload.bin",
82 config.getStreamingMetadata().getPropertyFiles()[0].getFilename());
83 assertEquals(195, config.getStreamingMetadata().getPropertyFiles()[0].getOffset());
84 assertEquals(8, config.getStreamingMetadata().getPropertyFiles()[0].getSize());
85 }
86
87 @Test
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070088 public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070089 UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070090 thrown.expect(RuntimeException.class);
91 config.getUpdatePackageFile();
92 }
93
94 @Test
95 public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
96 String json = "{\"name\": \"upd\", \"url\": \"http://foo.bar\","
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070097 + " \"ab_install_type\": \"NON_STREAMING\"}";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070098 UpdateConfig config = UpdateConfig.fromJson(json);
99 thrown.expect(RuntimeException.class);
100 config.getUpdatePackageFile();
101 }
102
103 @Test
104 public void getUpdatePackageFile_works() throws Exception {
105 UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700106 assertEquals("/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700107 }
108
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700109 private String readResource(int id) throws IOException {
110 return CharStreams.toString(new InputStreamReader(
111 mContext.getResources().openRawResource(id)));
112 }
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700113}