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 | |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 47 | private static final String JSON_NON_STREAMING = "{" |
| 48 | + " \"name\": \"vip update\", \"url\": \"file:///my-builds/a.zip\"," |
| 49 | + " \"ab_install_type\": \"NON_STREAMING\"," |
| 50 | + " \"ab_config\": {" |
| 51 | + " \"force_switch_slot\": false," |
| 52 | + " \"verify_payload_metadata\": false } }"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 53 | |
| 54 | @Rule |
| 55 | public final ExpectedException thrown = ExpectedException.none(); |
| 56 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 57 | private Context mContext; |
| 58 | private Context mTargetContext; |
| 59 | private String mJsonStreaming001; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
| 63 | mContext = InstrumentationRegistry.getContext(); |
| 64 | mTargetContext = InstrumentationRegistry.getTargetContext(); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 65 | mJsonStreaming001 = readResource(R.raw.update_config_001_stream); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 68 | @Test |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 69 | public void fromJson_parsesNonStreaming() throws Exception { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 70 | UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING); |
| 71 | assertEquals("name is parsed", "vip update", config.getName()); |
| 72 | assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson()); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 73 | assertSame("type is parsed", |
| 74 | UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING, |
| 75 | config.getInstallType()); |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 76 | assertEquals("url is parsed", "file:///my-builds/a.zip", config.getUrl()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 80 | public void fromJson_parsesStreaming() throws Exception { |
| 81 | UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001); |
| 82 | assertEquals("streaming-001", config.getName()); |
| 83 | assertEquals("http://foo.bar/update.zip", config.getUrl()); |
| 84 | assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType()); |
| 85 | assertEquals("payload.bin", |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 86 | config.getAbConfig().getPropertyFiles()[0].getFilename()); |
| 87 | assertEquals(195, config.getAbConfig().getPropertyFiles()[0].getOffset()); |
| 88 | assertEquals(8, config.getAbConfig().getPropertyFiles()[0].getSize()); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 89 | assertTrue(config.getAbConfig().getForceSwitchSlot()); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 93 | public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 94 | UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 95 | thrown.expect(RuntimeException.class); |
| 96 | config.getUpdatePackageFile(); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception { |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 101 | String json = "{" |
| 102 | + " \"name\": \"upd\", \"url\": \"http://foo.bar\"," |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 103 | + " \"ab_install_type\": \"NON_STREAMING\"," |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 104 | + " \"ab_config\": {" |
| 105 | + " \"force_switch_slot\": false," |
| 106 | + " \"verify_payload_metadata\": false } }"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 107 | UpdateConfig config = UpdateConfig.fromJson(json); |
| 108 | thrown.expect(RuntimeException.class); |
| 109 | config.getUpdatePackageFile(); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void getUpdatePackageFile_works() throws Exception { |
| 114 | UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING); |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 115 | assertEquals("/my-builds/a.zip", c.getUpdatePackageFile().getAbsolutePath()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 118 | private String readResource(int id) throws IOException { |
| 119 | return CharStreams.toString(new InputStreamReader( |
| 120 | mContext.getResources().openRawResource(id))); |
| 121 | } |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 122 | } |