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