summaryrefslogtreecommitdiffstats
path: root/updater_sample/tests/src/com/example/android/systemupdatersample
diff options
context:
space:
mode:
Diffstat (limited to 'updater_sample/tests/src/com/example/android/systemupdatersample')
-rw-r--r--updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java54
-rw-r--r--updater_sample/tests/src/com/example/android/systemupdatersample/util/FileDownloaderTest.java80
-rw-r--r--updater_sample/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java34
-rw-r--r--updater_sample/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java14
4 files changed, 155 insertions, 27 deletions
diff --git a/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java b/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
index 87153715e..0975e76be 100644
--- a/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
+++ b/updater_sample/tests/src/com/example/android/systemupdatersample/UpdateConfigTest.java
@@ -19,14 +19,23 @@ package com.example.android.systemupdatersample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
+import com.example.android.systemupdatersample.tests.R;
+import com.google.common.io.CharStreams;
+
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
/**
* Tests for {@link UpdateConfig}
*/
@@ -36,27 +45,48 @@ public class UpdateConfigTest {
private static final String JSON_NON_STREAMING =
"{\"name\": \"vip update\", \"url\": \"file:///builds/a.zip\", "
- + " \"type\": \"NON_STREAMING\"}";
-
- private static final String JSON_STREAMING =
- "{\"name\": \"vip update 2\", \"url\": \"http://foo.bar/a.zip\", "
- + "\"type\": \"STREAMING\"}";
+ + " \"ab_install_type\": \"NON_STREAMING\"}";
@Rule
public final ExpectedException thrown = ExpectedException.none();
+ private Context mContext;
+ private Context mTargetContext;
+ private String mJsonStreaming001;
+
+ @Before
+ public void setUp() throws Exception {
+ mContext = InstrumentationRegistry.getContext();
+ mTargetContext = InstrumentationRegistry.getTargetContext();
+ mJsonStreaming001 = readResource(R.raw.update_config_stream_001);
+ }
+
@Test
- public void fromJson_parsesJsonConfigWithoutMetadata() throws Exception {
+ public void fromJson_parsesNonStreaming() throws Exception {
UpdateConfig config = UpdateConfig.fromJson(JSON_NON_STREAMING);
assertEquals("name is parsed", "vip update", config.getName());
assertEquals("stores raw json", JSON_NON_STREAMING, config.getRawJson());
- assertSame("type is parsed", UpdateConfig.TYPE_NON_STREAMING, config.getInstallType());
+ assertSame("type is parsed",
+ UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
+ config.getInstallType());
assertEquals("url is parsed", "file:///builds/a.zip", config.getUrl());
}
@Test
+ public void fromJson_parsesStreaming() throws Exception {
+ UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
+ assertEquals("streaming-001", config.getName());
+ assertEquals("http://foo.bar/update.zip", config.getUrl());
+ assertSame(UpdateConfig.AB_INSTALL_TYPE_STREAMING, config.getInstallType());
+ assertEquals("payload.bin",
+ config.getStreamingMetadata().getPropertyFiles()[0].getFilename());
+ assertEquals(195, config.getStreamingMetadata().getPropertyFiles()[0].getOffset());
+ assertEquals(8, config.getStreamingMetadata().getPropertyFiles()[0].getSize());
+ }
+
+ @Test
public void getUpdatePackageFile_throwsErrorIfStreaming() throws Exception {
- UpdateConfig config = UpdateConfig.fromJson(JSON_STREAMING);
+ UpdateConfig config = UpdateConfig.fromJson(mJsonStreaming001);
thrown.expect(RuntimeException.class);
config.getUpdatePackageFile();
}
@@ -64,7 +94,7 @@ public class UpdateConfigTest {
@Test
public void getUpdatePackageFile_throwsErrorIfNotAFile() throws Exception {
String json = "{\"name\": \"upd\", \"url\": \"http://foo.bar\","
- + " \"type\": \"NON_STREAMING\"}";
+ + " \"ab_install_type\": \"NON_STREAMING\"}";
UpdateConfig config = UpdateConfig.fromJson(json);
thrown.expect(RuntimeException.class);
config.getUpdatePackageFile();
@@ -73,7 +103,11 @@ public class UpdateConfigTest {
@Test
public void getUpdatePackageFile_works() throws Exception {
UpdateConfig c = UpdateConfig.fromJson(JSON_NON_STREAMING);
- assertEquals("correct path", "/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
+ assertEquals("/builds/a.zip", c.getUpdatePackageFile().getAbsolutePath());
}
+ private String readResource(int id) throws IOException {
+ return CharStreams.toString(new InputStreamReader(
+ mContext.getResources().openRawResource(id)));
+ }
}
diff --git a/updater_sample/tests/src/com/example/android/systemupdatersample/util/FileDownloaderTest.java b/updater_sample/tests/src/com/example/android/systemupdatersample/util/FileDownloaderTest.java
new file mode 100644
index 000000000..80506ee6d
--- /dev/null
+++ b/updater_sample/tests/src/com/example/android/systemupdatersample/util/FileDownloaderTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.systemupdatersample.util;
+
+import static junit.framework.Assert.assertEquals;
+
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import com.example.android.systemupdatersample.tests.R;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+/**
+ * Tests for {@link FileDownloader}
+ */
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class FileDownloaderTest {
+
+ @Rule
+ public final ExpectedException thrown = ExpectedException.none();
+
+ private Context mTestContext;
+ private Context mTargetContext;
+
+ @Before
+ public void setUp() {
+ mTestContext = InstrumentationRegistry.getContext();
+ mTargetContext = InstrumentationRegistry.getTargetContext();
+ }
+
+ @Test
+ public void download_downloadsChunkOfZip() throws Exception {
+ // Prepare the target file
+ File packageFile = Paths
+ .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip")
+ .toFile();
+ Files.deleteIfExists(packageFile.toPath());
+ Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
+ packageFile.toPath());
+ String url = "file://" + packageFile.getAbsolutePath();
+ // prepare where to download
+ File outFile = Paths
+ .get(mTargetContext.getCacheDir().getAbsolutePath(), "care_map.txt")
+ .toFile();
+ Files.deleteIfExists(outFile.toPath());
+ // download a chunk of ota.zip
+ FileDownloader downloader = new FileDownloader(url, 160, 8, outFile);
+ downloader.download();
+ String downloadedContent = String.join("\n", Files.readAllLines(outFile.toPath()));
+ // archive contains text files with uppercase filenames
+ assertEquals("CARE_MAP", downloadedContent);
+ }
+
+}
diff --git a/updater_sample/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java b/updater_sample/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java
index 6f06ca3e1..2912e209e 100644
--- a/updater_sample/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java
+++ b/updater_sample/tests/src/com/example/android/systemupdatersample/util/PayloadSpecsTest.java
@@ -16,8 +16,9 @@
package com.example.android.systemupdatersample.util;
-import static com.example.android.systemupdatersample.util.PackagePropertyFiles.PAYLOAD_BINARY_FILE_NAME;
-import static com.example.android.systemupdatersample.util.PackagePropertyFiles.PAYLOAD_PROPERTIES_FILE_NAME;
+import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME;
+import static com.example.android.systemupdatersample.util.PackageFiles
+ .PAYLOAD_PROPERTIES_FILE_NAME;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -28,6 +29,8 @@ import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.example.android.systemupdatersample.PayloadSpec;
+import com.google.common.base.Charsets;
+import com.google.common.io.Files;
import org.junit.Before;
import org.junit.Rule;
@@ -56,16 +59,16 @@ public class PayloadSpecsTest {
private File mTestDir;
- private Context mContext;
+ private Context mTargetContext;
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
- mContext = InstrumentationRegistry.getTargetContext();
+ mTargetContext = InstrumentationRegistry.getTargetContext();
- mTestDir = mContext.getFilesDir();
+ mTestDir = mTargetContext.getFilesDir();
}
@Test
@@ -87,6 +90,21 @@ public class PayloadSpecsTest {
PayloadSpecs.forNonStreaming(new File("/fake/news.zip"));
}
+ @Test
+ public void forStreaming_works() throws Exception {
+ String url = "http://a.com/b.zip";
+ long offset = 45;
+ long size = 200;
+ File propertiesFile = createMockPropertiesFile();
+
+ PayloadSpec spec = PayloadSpecs.forStreaming(url, offset, size, propertiesFile);
+ assertEquals("same url", url, spec.getUrl());
+ assertEquals("same offset", offset, spec.getOffset());
+ assertEquals("same size", size, spec.getSize());
+ assertArrayEquals("correct properties",
+ new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0]));
+ }
+
/**
* Creates package zip file that contains payload.bin and payload_properties.txt
*/
@@ -114,4 +132,10 @@ public class PayloadSpecsTest {
return testFile;
}
+ private File createMockPropertiesFile() throws IOException {
+ File propertiesFile = new File(mTestDir, PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME);
+ Files.asCharSink(propertiesFile, Charsets.UTF_8).write(PROPERTIES_CONTENTS);
+ return propertiesFile;
+ }
+
}
diff --git a/updater_sample/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java b/updater_sample/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java
index 4aa8c6453..4ccae9380 100644
--- a/updater_sample/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java
+++ b/updater_sample/tests/src/com/example/android/systemupdatersample/util/UpdateConfigsTest.java
@@ -18,14 +18,11 @@ package com.example.android.systemupdatersample.util;
import static org.junit.Assert.assertArrayEquals;
-import android.content.Context;
-import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.example.android.systemupdatersample.UpdateConfig;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -41,21 +38,14 @@ import java.util.List;
@SmallTest
public class UpdateConfigsTest {
- private Context mContext;
-
@Rule
public final ExpectedException thrown = ExpectedException.none();
- @Before
- public void setUp() {
- mContext = InstrumentationRegistry.getTargetContext();
- }
-
@Test
public void configsToNames_extractsNames() {
List<UpdateConfig> configs = Arrays.asList(
- new UpdateConfig("blah", "http://", UpdateConfig.TYPE_NON_STREAMING),
- new UpdateConfig("blah 2", "http://", UpdateConfig.TYPE_STREAMING)
+ new UpdateConfig("blah", "http://", UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING),
+ new UpdateConfig("blah 2", "http://", UpdateConfig.AB_INSTALL_TYPE_STREAMING)
);
String[] names = UpdateConfigs.configsToNames(configs);
assertArrayEquals(new String[] {"blah", "blah 2"}, names);