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

Unified Diff: chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java

Issue 2858563004: Add support for webapk without runtimeHost (Closed)
Patch Set: Fix test failure. Created 3 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/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java
diff --git a/chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java b/chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..966a9e77910e6a3e34fff2fcfc0dec01f0ddd2aa
--- /dev/null
+++ b/chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java
@@ -0,0 +1,205 @@
+// Copyright 2017 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.shell_apk;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.robolectric.annotation.Config;
+
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+import org.chromium.webapk.lib.common.WebApkConstants;
+import org.chromium.webapk.lib.common.WebApkMetaDataKeys;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Tests for WebApkUtils.
+ */
+
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE)
+public class WebApkUtilsTest {
+ private static final String HOST_BROWSER_PACKAGE_NAME = "browser.supporting.webapks";
+ private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.test_package";
+
+ private static final List<String> sInstalledBrowsers =
+ new ArrayList<String>(Arrays.asList("browser.installed", HOST_BROWSER_PACKAGE_NAME));
+
+ @Mock
+ private Context mContext;
+ @Mock
+ private PackageManager mPackageManager;
+
+ @Before
+ public void setUp() {
+ mContext = Mockito.mock(Context.class);
+ Mockito.when(mContext.getPackageName()).thenReturn(WEBAPK_PACKAGE_NAME);
+ mPackageManager = Mockito.mock(PackageManager.class);
+ Mockito.when(mContext.getPackageManager()).thenReturn(mPackageManager);
+
+ WebApkUtils.resetCachedHostPackageForTesting();
+ WebApkUtils.setBrowsersSupportingWebApkForTesting(sInstalledBrowsers);
+ }
+
+ /**
+ * Tests that null will be returned if there isn't any browser installed on the device.
+ */
+ @Test
+ public void testReturnsEmptyStringWhenNoBrowserInstalled() {
Yaron 2017/05/15 18:41:37 testReturnsNull
Xi Han 2017/05/16 13:50:39 Done.
+ String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertNull(hostBrowser);
+ }
+
+ /**
+ * Tests that the package name of the host browser specified in the AndroidManifest.xml will be
+ * returned as the WebAPK's host browser if it is installed.
+ */
+ @Test
+ public void testReturnsHostBrowserInManifestIfInstalled() {
+ String expectedHostBrowser = HOST_BROWSER_PACKAGE_NAME;
+ mockInstallBrowsers();
+ mockWebApkPackageMetadata(HOST_BROWSER_PACKAGE_NAME);
+ mockSharedPreferences(null);
+
+ String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertTrue(hostBrowser.equals(expectedHostBrowser));
Yaron 2017/05/15 18:41:37 use assertEquals (throughout) for better error mes
Xi Han 2017/05/16 13:50:39 It is deprecated, that is why I try to void using
Yaron 2017/05/16 15:20:47 Pretty sure it's just a bug in studio (or at least
Xi Han 2017/05/23 16:51:09 Only the one with (object, object) is deprecated,
+ }
+
+ /**
+ * Tests that the package name of the host browser in the SharedPreference will be returned if:
+ * 1. there isn't a host browser specified in the AndroidManifest.xml, or the specified one has
+ * been uninstalled;
+ * And
+ * 2. there is a host browser stored in the SharedPreference and it is still installed.
+ */
+ @Test
+ public void testReturnsHostBrowserInSharedPreferenceIfInstalled() {
+ String expectedHostBrowser = HOST_BROWSER_PACKAGE_NAME;
+ mockInstallBrowsers();
+ mockWebApkPackageMetadata(null);
+ mockSharedPreferences(expectedHostBrowser);
+
+ String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertTrue(hostBrowser.equals(expectedHostBrowser));
+
+ WebApkUtils.resetCachedHostPackageForTesting();
+ mockWebApkPackageMetadata("browser.uninstalled");
Yaron 2017/05/15 18:41:37 so the idea is that this is referring to a webapk
Xi Han 2017/05/16 13:50:39 Added, thanks!
+ hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertTrue(hostBrowser.equals(expectedHostBrowser));
+ }
+
+ /**
+ * Tests that the default browser package name will be returned if:
+ * 1. there isn't any host browser specified in the AndroidManifest.xml or the SharedPreference,
+ * or the specified one has uninstalled;
+ * And
+ * 2. the default browser supports WebAPKs.
+ */
+ @Test
+ public void testReturnsDefaultBrowser() {
+ String defaultBrowser = HOST_BROWSER_PACKAGE_NAME;
+ mockInstallBrowsers();
+ mockWebApkPackageMetadata(null);
+ mockSharedPreferences(null);
+ mockDefaultBrowser(defaultBrowser);
+
+ String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertTrue(hostBrowser.equals(defaultBrowser));
+
+ WebApkUtils.resetCachedHostPackageForTesting();
+ mockSharedPreferences("browser.uninstalled");
+ hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertTrue(hostBrowser.equals(defaultBrowser));
+ }
+
+ /**
+ * Tests that the package name of one of installed browsers that support WebAPKs will be
+ * returned if:
+ * 1. there is neither a host browser specified in the AndroidManifest.xml nor in the
+ * SharedPreference, or the specified one has uninstalled;
+ * And
+ * 2. the default browser doesn't support WebAPKs.
+ * And
+ * 3. there is another installed browser that supports WebAPKs.
+ */
+ @Test
+ public void testReturnsInstalledBrowserSupportingWebApks() {
+ mockInstallBrowsers();
+ mockWebApkPackageMetadata(null);
+ mockSharedPreferences(null);
+ mockDefaultBrowser("browser.not.supporting.webapks");
+
+ String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
+ Assert.assertNotNull(hostBrowser);
+ Assert.assertTrue(sInstalledBrowsers.contains(hostBrowser));
+ }
+
+ private static ResolveInfo newResolveInfo(String packageName) {
+ ActivityInfo activityInfo = new ActivityInfo();
+ activityInfo.packageName = packageName;
+ ResolveInfo resolveInfo = new ResolveInfo();
+ resolveInfo.activityInfo = activityInfo;
+ return resolveInfo;
+ }
+
+ private void mockInstallBrowsers() {
+ List<ResolveInfo> browsers = new ArrayList<ResolveInfo>();
+ for (String name : sInstalledBrowsers) {
+ browsers.add(newResolveInfo(name));
+ }
+ Mockito.when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
+ .thenReturn(browsers);
+ }
+
+ private void mockSharedPreferences(String hostBrowserPackage) {
+ SharedPreferences sharedPreferences = Mockito.mock(SharedPreferences.class);
+ Mockito.when(mContext.getSharedPreferences(
+ WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE))
+ .thenReturn(sharedPreferences);
+
+ Mockito.when(sharedPreferences.getString(WebApkUtils.SHARED_PREF_RUNTIME_HOST, null))
+ .thenReturn(hostBrowserPackage);
+ SharedPreferences.Editor editor = Mockito.mock(SharedPreferences.Editor.class);
+ Mockito.when(sharedPreferences.edit()).thenReturn(editor);
+ }
+
+ private void mockWebApkPackageMetadata(String hostBrowserPackage) {
+ ApplicationInfo ai = Mockito.mock(ApplicationInfo.class);
+ ai.metaData = new Bundle();
+ if (hostBrowserPackage != null) {
+ ai.metaData.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPackage);
+ }
+ try {
+ Mockito.when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
Yaron 2017/05/15 18:41:37 instead of anyString() you should be able to use h
Xi Han 2017/05/16 13:50:39 Done.
+ } catch (PackageManager.NameNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void mockDefaultBrowser(String defaultBrowser) {
+ ResolveInfo defaultBrowserInfo = newResolveInfo(defaultBrowser);
+ Mockito.when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
+ .thenReturn(defaultBrowserInfo);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698