Chromium Code Reviews| 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..e0e62bc57c591561a4dcf532d83c2ba08c00f36c |
| --- /dev/null |
| +++ b/chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java |
| @@ -0,0 +1,240 @@ |
| +// 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 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 WEBAPK_PACKAGE_NAME = "org.chromium.webapk.test_package"; |
| + private static final String BROWSER_INSTALLED_SUPPORTING_WEBAPKS = |
| + "browser.installed.supporting.webapks"; |
| + private static final String BROWSER_UNINSTALLED_SUPPORTING_WEBAPK = |
| + "browser.uninstalled.supporting.webapks"; |
| + private static final String BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK = |
| + "browser.installed.not.supporting.webapks"; |
| + |
| + private static final List<String> sInstalledBrowsers = new ArrayList<String>(Arrays.asList( |
| + BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK, BROWSER_INSTALLED_SUPPORTING_WEBAPKS)); |
| + |
| + private static final List<String> sBrowsersSupportingWebApks = |
| + new ArrayList<String>(Arrays.asList( |
| + BROWSER_UNINSTALLED_SUPPORTING_WEBAPK, BROWSER_INSTALLED_SUPPORTING_WEBAPKS)); |
| + |
| + @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(sBrowsersSupportingWebApks); |
|
pkotwicz
2017/05/25 02:59:09
Is calling setBrowsersSupportingWebApkForTesting()
Xi Han
2017/05/25 19:29:33
Yes, it is necessary. The |sBrowsersSupportingWebA
pkotwicz
2017/05/26 05:29:49
I was thinking of using the hard coded values in W
|
| + } |
| + |
| + /** |
| + * Tests that null will be returned if there isn't any browser installed on the device. |
| + */ |
| + @Test |
| + public void testReturnsNullWhenNoBrowserInstalled() { |
| + 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 = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(); |
| + mockWebApkPackageMetadata(expectedHostBrowser); |
| + mockSharedPreferences(null); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + } |
| + |
| + /** |
| + * Tests that null will be returned if: |
| + * 1) a host browser specified in the AndroidManifest.xml but isn't installed; |
|
pkotwicz
2017/05/25 02:59:09
"browser specified" -> "browser is specified"
Xi Han
2017/05/25 19:29:33
Done.
|
| + * And |
| + * 2) there isn't a host browser stored in the SharedPreference or it isn't installed. |
| + */ |
| + @Test |
| + public void testReturnsNullIfHostBrowserSpecifiedInManifestIsUninstalled() { |
| + mockInstallBrowsers(); |
| + mockWebApkPackageMetadata(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK); |
| + // Simulates that there isn't any host browser stored in the SharedPreference. |
| + mockSharedPreferences(null); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertNull(hostBrowser); |
| + |
| + // Simulates that the host browser stored in the SharedPreference has been uninstalled. |
| + mockSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK); |
| + hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertNull(hostBrowser); |
| + } |
| + |
| + /** |
| + * 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 is |
| + * uninstalled; |
| + * And |
| + * 2. there is a host browser stored in the SharedPreference and it is still installed. |
| + */ |
| + @Test |
| + public void testReturnsHostBrowserInSharedPreferenceIfInstalled() { |
| + String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(); |
| + // Simulates that the WebAPK doesn't specify any host browser in the AndroidManifest.xml. |
| + mockWebApkPackageMetadata(null); |
| + mockSharedPreferences(expectedHostBrowser); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + |
| + // Now simulates the WebApk specifying a host browser that isn't installed. |
| + mockWebApkPackageMetadata(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK); |
| + hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + } |
| + |
| + /** |
| + * Tests that the default browser package name will be returned if: |
| + * 1. there isn't a host browser specified in the AndroidManifest.xml; |
| + * And |
| + * 2. there isn't any host browser stored in the SharedPreference, or the specified one has |
| + * been uninstalled; |
| + * And |
| + * 3. the default browser supports WebAPKs. |
| + */ |
| + @Test |
| + public void testReturnsDefaultBrowser() { |
| + String defaultBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(); |
| + mockWebApkPackageMetadata(null); |
| + // Simulates that there isn't any host browser stored in the SharedPreference. |
| + mockSharedPreferences(null); |
| + mockDefaultBrowser(defaultBrowser); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, defaultBrowser); |
| + |
| + // Simulates that the host browser stored in the SharedPreference isn't installed. |
| + WebApkUtils.resetCachedHostPackageForTesting(); |
| + mockSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK); |
| + hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, defaultBrowser); |
| + } |
| + |
| + /** |
| + * Tests that null will be returned if: |
| + * 1. there isn't a host browser specified in the AndroidManifest.xml; |
| + * And |
| + * 2. there isn't any host browser stored in the SharedPreference, or the specified one has |
| + * been uninstalled; |
| + * And |
| + * 3. the default browser doesn't support WebAPKs. |
| + */ |
| + @Test |
| + public void testReturnsNullWhenDefaultBrowserDoesNotSupportWebApks() { |
| + mockInstallBrowsers(); |
| + mockWebApkPackageMetadata(null); |
| + mockSharedPreferences(null); |
| + mockDefaultBrowser(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertNull(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() { |
|
pkotwicz
2017/05/25 02:59:09
- Can you use RobolectricPackageManager#addResolve
Xi Han
2017/05/25 19:29:33
I didn't find a way to set the default browser thr
pkotwicz
2017/05/26 05:29:49
This seems to work (Partial mocks for the win!)
|
| + 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); |
|
pkotwicz
2017/05/25 02:59:09
Is this code necessary? Can you instead do:
Share
Xi Han
2017/05/25 19:29:33
The context isn't a Robolectric RuntimeEnvironment
|
| + } |
| + |
| + private void mockWebApkPackageMetadata(String hostBrowserPackage) { |
|
pkotwicz
2017/05/25 02:59:09
Can you use WebApkTestHelper#registerWebApkWithMet
Xi Han
2017/05/25 19:29:33
Same reason as above.
|
| + 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( |
| + WEBAPK_PACKAGE_NAME, PackageManager.GET_META_DATA)) |
| + .thenReturn(ai); |
| + } 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); |
| + } |
| +} |