Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java

Issue 1971773002: Upstream: Add WebAPK's client library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add WebApkValidatorTest. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java
diff --git a/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java b/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..15e03d7a67bb5cf4e2044ec7be9837c2e19c70af
--- /dev/null
+++ b/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkValidatorTest.java
@@ -0,0 +1,168 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.webapk.lib.client;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.ResolveInfo;
+import android.content.pm.Signature;
+
+import junit.framework.Assert;
+
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.annotation.Config;
+import org.robolectric.res.builder.RobolectricPackageManager;
+
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Unit tests for {@link org.chromium.webapk.lib.client.WebApkValidator}.
+ */
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE)
+public class WebApkValidatorTest {
+ private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.foo";
+ private static final String INVALID_WEBAPK_PACKAGE_NAME = "invalid.org.chromium.webapk.foo";
+ private static final String URL_OF_WEBAPK = "https://www.foo.com";
+ private static final String URL_WITHOUT_WEBAPK = "https://www.other.com";
+ private static final byte[] EXPECTED_SIGNATURE = new byte[] {
+ 48, -126, 3, -121, 48, -126, 2, 111, -96, 3, 2, 1, 2, 2, 4, 20, -104, -66, -126, 48, 13,
+ 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 11, 5, 0, 48, 116, 49, 11, 48, 9, 6, 3, 85, 4,
+ 6, 19, 2, 67, 65, 49, 16, 48, 14, 6, 3, 85, 4, 8, 19, 7, 79, 110, 116, 97, 114, 105,
+ 111, 49, 17, 48, 15, 6, 3, 85, 4, 7, 19, 8, 87, 97, 116, 101, 114, 108, 111, 111, 49,
+ 17, 48, 15, 6, 3, 85, 4, 10, 19, 8, 67, 104, 114, 111, 109, 105, 117, 109, 49, 17, 48};
+
+ private Context mContext;
+ private PackageManager mPackageManager;
+ private PackageInfo mWebApkPackageInfo;
+ private PackageInfo mInvalidWebApkPackageInfo;
+
+ @Before
+ public void setUp() {
+ mContext = mock(Context.class);
+ mPackageManager = mock(PackageManager.class);
+ when(mContext.getPackageManager()).thenReturn(mPackageManager);
+ WebApkValidator.initWithBrowserHostSignature(EXPECTED_SIGNATURE);
+ }
+
+ @Test
Yaron 2016/05/12 22:11:39 nit: move @Test annotation to after comments
Xi Han 2016/05/13 15:42:55 Done.
+ /**
+ * Tests {@link WebApkValidator.queryWebApkPackage()} returns a WebAPK's package name if it can
+ * handle the given URL.
+ */
+ public void testQueryWebAPKPackageReturnWebApkPackageNameWhichCanHandleTheURL() {
Yaron 2016/05/12 22:11:39 please add test for non-browsable (should not retu
Yaron 2016/05/12 22:11:40 WebApk (throughout)
Xi Han 2016/05/13 15:42:55 Done.
+ Intent intent;
Yaron 2016/05/12 22:11:40 nit: move to L73 (no need to be separate declarati
Xi Han 2016/05/13 15:42:55 Done.
+ try {
+ intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCHEME);
+ intent.addCategory(Intent.CATEGORY_BROWSABLE);
+
+ ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
+ RobolectricPackageManager packageManager =
+ (RobolectricPackageManager) Robolectric.application.getPackageManager();
+ packageManager.addResolveInfoForIntent(intent, info);
+
+ assertTrue(WebApkValidator.queryWebAPKPackage(
Yaron 2016/05/12 22:11:40 assertEquals (throughout)
Xi Han 2016/05/13 15:42:55 Done.
+ Robolectric.application, URL_OF_WEBAPK).equals(WEBAPK_PACKAGE_NAME));
+ } catch (URISyntaxException e) {
+ Assert.fail("URI is invalid.");
+ }
+ }
+
+ @Test
+ /**
+ * Tests {@link WebApkValidator.queryWebApkPackage()} returns null if no WebAPK can handle
+ * the given URL.
+ */
+ public void testQueryWebApkPackageReturnNullWhenNoWebApkCanHandleTheURL() {
+ Intent intent;
+ try {
+ intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCHEME);
+ intent.addCategory(Intent.CATEGORY_BROWSABLE);
+
+ ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
+ RobolectricPackageManager packageManager =
+ (RobolectricPackageManager) Robolectric.application.getPackageManager();
+ packageManager.addResolveInfoForIntent(intent, info);
+
+ assertTrue(WebApkValidator.queryWebAPKPackage(
Yaron 2016/05/12 22:11:40 assertNull (throughout)
Xi Han 2016/05/13 15:42:54 Done.
+ Robolectric.application, URL_WITHOUT_WEBAPK) == null);
+ } catch (URISyntaxException e) {
+ Assert.fail("URI is invalid.");
+ }
+ }
+
+ @Test
+ /**
+ * Tests {@link WebApkValidator.findWebAPKPackage()} returns a WebAPK's package name when there
+ * are ResolveInfos corresponds to a WebAPK.
+ */
+ public void testFindWebAPKPackageReturnValidWebApkPackageName() {
+ List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
+ infos.add(newResolveInfo(WEBAPK_PACKAGE_NAME));
+ assertTrue(WebApkValidator.findWebAPKPackage(infos).equals(WEBAPK_PACKAGE_NAME));
+ }
+
+ @Test
+ /**
+ * Tests {@link WebApkValidator.findWebAPKPackage()} returns null when there isn't any
+ * ResolveInfos corresponds to a WebAPK.
+ */
+ public void testFindWebAPKPackageReturnNullWhenNoResolvesInfosCorrespondsToWebApk() {
Yaron 2016/05/12 22:11:40 s/ResolvesInfosCorresponds/ResolveInfosCorrespondi
Xi Han 2016/05/13 15:42:55 Done.
+ List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
+ infos.add(newResolveInfo("com.google.android"));
+ assertTrue(WebApkValidator.findWebAPKPackage(infos) == null);
+ }
+
+ @Test
+ /**
+ * Tests {@link WebApkValidator.IsValidWebApk} returns true if the WebAPK has the expected
+ * signature.
+ */
+ public void testIsValidWebApkReturnsTrueForValidWebApk() throws NameNotFoundException {
Yaron 2016/05/12 22:11:39 please add test for multiple signatures with one m
Xi Han 2016/05/13 15:42:55 It is possible to update these two tests to cover
+ mWebApkPackageInfo = mock(PackageInfo.class);
+ when(mPackageManager.getPackageInfo(WEBAPK_PACKAGE_NAME, PackageManager.GET_SIGNATURES))
+ .thenReturn(mWebApkPackageInfo);
+ mWebApkPackageInfo.signatures = new Signature[] {new Signature(EXPECTED_SIGNATURE)};
+
+ assertTrue(WebApkValidator.isValidWebApk(mContext, WEBAPK_PACKAGE_NAME));
+ }
+
+ @Test
+ /**
+ * Tests {@link WebApkValidator.IsValidWebApk} returns true if the WebAPK doesn't have the
+ * expected signature.
+ */
+ public void testIsValidWebApkReturnsFalseForInValidWebApk() throws NameNotFoundException {
Yaron 2016/05/12 22:11:39 s/InValid/Invalid/
Xi Han 2016/05/13 15:42:55 Done.
+ mInvalidWebApkPackageInfo = mock(PackageInfo.class);
+ when(mPackageManager.getPackageInfo(INVALID_WEBAPK_PACKAGE_NAME,
+ PackageManager.GET_SIGNATURES)).thenReturn(mInvalidWebApkPackageInfo);
+ mInvalidWebApkPackageInfo.signatures = new Signature[] {new Signature(new byte[] {})};
+
+ assertFalse(WebApkValidator.isValidWebApk(mContext, INVALID_WEBAPK_PACKAGE_NAME));
+ }
+
+ private static ResolveInfo newResolveInfo(String packageName) {
+ ActivityInfo activityInfo = new ActivityInfo();
+ activityInfo.packageName = packageName;
+ ResolveInfo resolveInfo = new ResolveInfo();
+ resolveInfo.activityInfo = activityInfo;
+ return resolveInfo;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698