Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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; |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertTrue; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 22 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.support.test.InstrumentationRegistry; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
| 27 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 28 | import com.example.android.systemupdatersample.tests.R; |
| 29 | import com.google.common.io.CharStreams; |
| 30 | |
| 31 | import org.junit.Before; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 32 | import org.junit.Rule; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.rules.ExpectedException; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 37 | import java.io.IOException; |
| 38 | import java.io.InputStreamReader; |
| 39 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Tests for {@link UpdateConfig} |
| 42 | */ |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | @SmallTest |
| 45 | public class UpdateConfigTest { |
| 46 | |
| 47 | private static final String JSON_NON_STREAMING = |
| 48 | "{\"name\": \"vip update\", \"url\": \"file:///builds/a.zip\", " |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 49 | + " \"ab_install_type\": \"NON_STREAMING\"," |
| 50 | + " \"ab_config\": { \"force_switch_slot\": false } }"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 51 | |
| 52 | @Rule |
| 53 | public final ExpectedException thrown = ExpectedException.none(); |
| 54 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 55 | 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(); |
| 63 | mJsonStreaming001 = readResource(R.raw.update_config_stream_001); |
| 64 | } |
| 65 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 66 | @Test |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 67 | public void fromJson_parsesNonStreaming() throws Exception { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 68 | 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 71 | assertSame("type is parsed", |
| 72 | UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING, |
| 73 | config.getInstallType()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 74 | assertEquals("url is parsed", "file:///builds/a.zip", config.getUrl()); |
| 75 | } |
| 76 | |
| 77 | @Test |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 78 | 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 Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 87 | assertTrue(config.getAbConfig().getForceSwitchSlot()); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 91 | public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 92 | UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 93 | 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 Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 100 | + " \"ab_install_type\": \"NON_STREAMING\"," |
| 101 | + " \"ab_config\": { \"force_switch_slot\": false } }"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 102 | 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 110 | assertEquals("/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 113 | private String readResource(int id) throws IOException { |
| 114 | return CharStreams.toString(new InputStreamReader( |
| 115 | mContext.getResources().openRawResource(id))); |
| 116 | } |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 117 | } |