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