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..f8eb9d7130f1fa957682222242c2786b5d4e2802 |
| --- /dev/null |
| +++ b/chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java |
| @@ -0,0 +1,224 @@ |
| +// 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.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.Mockito; |
| +import org.robolectric.RuntimeEnvironment; |
| +import org.robolectric.annotation.Config; |
| +import org.robolectric.res.builder.RobolectricPackageManager; |
| + |
| +import org.chromium.testing.local.LocalRobolectricTestRunner; |
| +import org.chromium.webapk.lib.common.WebApkConstants; |
| +import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
| +import org.chromium.webapk.test.WebApkTestHelper; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Arrays; |
| +import java.util.List; |
| + |
| +/** |
| + * Tests for WebApkUtils. |
| + */ |
| + |
|
pkotwicz
2017/05/31 17:05:57
Nit: Remove new line. You can make the comment a s
Xi Han
2017/05/31 19:11:39
Done.
|
| +@RunWith(LocalRobolectricTestRunner.class) |
| +@Config(manifest = Config.NONE, packageName = WebApkUtilsTest.WEBAPK_PACKAGE_NAME) |
| +public class WebApkUtilsTest { |
| + protected static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.test_package"; |
| + private static final String BROWSER_INSTALLED_SUPPORTING_WEBAPKS = "com.chrome.canary"; |
| + private static final String BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS = "com.chrome.dev"; |
| + private static final String BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS = |
| + "browser.installed.not.supporting.webapks"; |
| + private static final String ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS = "com.chrome.beta"; |
| + |
| + private static final List<String> sInstalledBrowsers = new ArrayList<String>(Arrays.asList( |
| + BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS, BROWSER_INSTALLED_SUPPORTING_WEBAPKS, |
| + ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS)); |
| + |
| + private Context mContext; |
| + private RobolectricPackageManager mPackageManager; |
| + |
| + @Before |
| + public void setUp() { |
| + mContext = RuntimeEnvironment.application; |
| + mPackageManager = |
| + Mockito.spy((RobolectricPackageManager) RuntimeEnvironment.getPackageManager()); |
| + RuntimeEnvironment.setRobolectricPackageManager(mPackageManager); |
| + |
| + WebApkUtils.resetCachedHostPackageForTesting(); |
| + } |
| + |
| + /** |
| + * 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 in the SharedPreference will be returned if |
| + * it is installed, even if a host browser is specified in the AndroidManifest.xml. |
| + */ |
| + @Test |
| + public void testReturnsHostBrowserInSharedPreferenceIfInstalled() { |
| + String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS); |
|
pkotwicz
2017/05/31 17:05:57
Might as well make the default browser: ANOTHER_BR
Xi Han
2017/05/31 19:11:39
Done.
|
| + setHostBrowserInMetadata(ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS); |
| + setHostBrowserInSharedPreferences(expectedHostBrowser); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + } |
| + |
| + /** |
| + * This is a test for the WebAPK WITH a runtime host specified in its AndroidManifest.xml. |
| + * Tests that the package name of the host browser specified in the AndroidManifest.xml will be |
| + * returned if: |
| + * 1. there isn't a host browser specified in the SharedPreference or the specified one is |
| + * uninstalled. |
| + * And |
| + * 2. the host browser stored in the AndroidManifest and it is still installed. |
| + */ |
| + @Test |
| + public void testReturnsHostBrowserInManifestIfInstalled() { |
| + String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS); |
| + setHostBrowserInMetadata(expectedHostBrowser); |
| + // Simulates there isn't any host browser stored in the SharedPreference. |
| + setHostBrowserInSharedPreferences(null); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + |
| + WebApkUtils.resetCachedHostPackageForTesting(); |
| + // Simulates there is a host browser stored in the SharedPreference but uninstalled. |
| + setHostBrowserInSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS); |
| + hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| + } |
| + |
| + /** |
| + * This is a test for the WebAPK WITH a runtime host specified in its AndroidManifest.xml. |
| + * Tests that null will be returned if: |
| + * 1) there isn't a host browser stored in the SharedPreference or it isn't installed. |
| + * And |
| + * 2) the host browser specified in the AndroidManifest.xml isn't installed; |
| + * In this test, we only simulate the the first part of the condition 1. |
| + */ |
| + @Test |
| + public void testReturnsNullIfHostBrowserSpecifiedInManifestIsUninstalled() { |
| + mockInstallBrowsers(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS); |
|
pkotwicz
2017/05/31 17:05:57
I think that ANOTHER_BROWSER_INSTALLED_SUPPORTING_
Xi Han
2017/05/31 19:11:39
Good idea, thanks!
|
| + setHostBrowserInMetadata(BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS); |
| + // Simulates that there isn't any host browser stored in the SharedPreference. |
| + setHostBrowserInSharedPreferences(null); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertNull(hostBrowser); |
| + } |
| + |
| + /** |
| + * This is a test for the WebAPK WITHOUT any runtime host specified in its AndroidManifest.xml. |
| + * Tests that the default browser package name will be returned if: |
| + * 1. there isn't any host browser stored in the SharedPreference, or the specified one has |
| + * been uninstalled. |
| + * And |
| + * 2. the default browser supports WebAPKs. |
| + * In this test, we only simulate the the first part of the condition 1. |
| + */ |
| + @Test |
| + public void testReturnsDefaultBrowser() { |
| + String defaultBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| + mockInstallBrowsers(defaultBrowser); |
| + setHostBrowserInMetadata(null); |
| + // Simulates that there isn't any host browser stored in the SharedPreference. |
| + setHostBrowserInSharedPreferences(null); |
| + |
| + String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| + Assert.assertEquals(hostBrowser, defaultBrowser); |
| + } |
| + |
| + /** |
| + * This is a test for the WebAPK WITHOUT any runtime host specified in its AndroidManifest.xml. |
| + * Tests that null will be returned if: |
| + * 1. there isn't any host browser stored in the SharedPreference, or the specified one has |
| + * been uninstalled. |
| + * And |
| + * 2. there isn't a host browser specified in the AndroidManifest.xml. |
|
pkotwicz
2017/05/31 17:05:57
Isn't this condition redundant? You mention it in
Xi Han
2017/05/31 19:11:39
Removed.
|
| + * And |
| + * 3. the default browser doesn't support WebAPKs. |
| + * In this test, we only simulate the the first part of the condition 1. |
| + */ |
| + @Test |
| + public void testReturnsNullWhenDefaultBrowserDoesNotSupportWebApks() { |
| + mockInstallBrowsers(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS); |
| + setHostBrowserInMetadata(null); |
| + setHostBrowserInSharedPreferences(null); |
| + |
| + 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(String defaultBrowser) { |
| + Intent intent = null; |
| + try { |
| + intent = Intent.parseUri("http://", Intent.URI_INTENT_SCHEME); |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
|
pkotwicz
2017/05/31 17:05:57
Calling fail() would probably be more appropriate
Xi Han
2017/05/31 19:11:39
Done.
|
| + return; |
| + } |
| + |
| + ResolveInfo defaultBrowserInfo = null; |
|
pkotwicz
2017/05/31 17:05:57
Can you move declaration of |defaultBrowserInfo| t
Xi Han
2017/05/31 19:11:39
Thanks. Also remove line 199 - 201 since it isn't
|
| + for (String name : sInstalledBrowsers) { |
| + ResolveInfo info = newResolveInfo(name); |
| + mPackageManager.addResolveInfoForIntent(intent, info); |
| + if (name.equals(defaultBrowser)) { |
| + defaultBrowserInfo = info; |
| + } |
| + } |
| + |
| + defaultBrowserInfo = newResolveInfo(defaultBrowser); |
| + mPackageManager.addResolveInfoForIntent(intent, defaultBrowserInfo); |
| + |
| + Mockito.when(mPackageManager.resolveActivity(any(Intent.class), anyInt())) |
| + .thenReturn(defaultBrowserInfo); |
| + } |
| + |
| + private void setHostBrowserInSharedPreferences(String hostBrowserPackage) { |
| + SharedPreferences sharedPref = |
| + mContext.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE); |
| + SharedPreferences.Editor editor = sharedPref.edit(); |
| + editor.putString(WebApkUtils.SHARED_PREF_RUNTIME_HOST, hostBrowserPackage); |
| + editor.apply(); |
| + } |
| + |
| + private void setHostBrowserInMetadata(String hostBrowserPackage) { |
| + Bundle bundle = new Bundle(); |
| + bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPackage); |
| + WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle); |
| + } |
| +} |