OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.webapk.shell_apk; |
| 6 |
| 7 import static org.mockito.ArgumentMatchers.any; |
| 8 import static org.mockito.ArgumentMatchers.anyInt; |
| 9 |
| 10 import android.content.Context; |
| 11 import android.content.Intent; |
| 12 import android.content.SharedPreferences; |
| 13 import android.content.pm.ActivityInfo; |
| 14 import android.content.pm.ResolveInfo; |
| 15 import android.os.Bundle; |
| 16 |
| 17 import org.junit.Assert; |
| 18 import org.junit.Before; |
| 19 import org.junit.Test; |
| 20 import org.junit.runner.RunWith; |
| 21 import org.mockito.Mockito; |
| 22 import org.robolectric.RuntimeEnvironment; |
| 23 import org.robolectric.annotation.Config; |
| 24 import org.robolectric.res.builder.RobolectricPackageManager; |
| 25 |
| 26 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 27 import org.chromium.webapk.lib.common.WebApkConstants; |
| 28 import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
| 29 import org.chromium.webapk.test.WebApkTestHelper; |
| 30 |
| 31 import java.util.ArrayList; |
| 32 import java.util.Arrays; |
| 33 import java.util.List; |
| 34 |
| 35 /** Tests for WebApkUtils. */ |
| 36 @RunWith(LocalRobolectricTestRunner.class) |
| 37 @Config(manifest = Config.NONE, packageName = WebApkUtilsTest.WEBAPK_PACKAGE_NAM
E) |
| 38 public class WebApkUtilsTest { |
| 39 protected static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.tes
t_package"; |
| 40 private static final String BROWSER_INSTALLED_SUPPORTING_WEBAPKS = "com.chro
me.canary"; |
| 41 private static final String BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS = "com.ch
rome.dev"; |
| 42 private static final String BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS = |
| 43 "browser.installed.not.supporting.webapks"; |
| 44 private static final String ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS = "
com.chrome.beta"; |
| 45 |
| 46 private static final List<String> sInstalledBrowsers = new ArrayList<String>
(Arrays.asList( |
| 47 BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS, BROWSER_INSTALLED_SUPPORTI
NG_WEBAPKS, |
| 48 ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS)); |
| 49 |
| 50 private Context mContext; |
| 51 private RobolectricPackageManager mPackageManager; |
| 52 |
| 53 @Before |
| 54 public void setUp() { |
| 55 mContext = RuntimeEnvironment.application; |
| 56 mPackageManager = |
| 57 Mockito.spy((RobolectricPackageManager) RuntimeEnvironment.getPa
ckageManager()); |
| 58 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager); |
| 59 |
| 60 WebApkUtils.resetCachedHostPackageForTesting(); |
| 61 } |
| 62 |
| 63 /** |
| 64 * Tests that null will be returned if there isn't any browser installed on
the device. |
| 65 */ |
| 66 @Test |
| 67 public void testReturnsNullWhenNoBrowserInstalled() { |
| 68 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 69 Assert.assertNull(hostBrowser); |
| 70 } |
| 71 |
| 72 /** |
| 73 * Tests that the package name of the host browser in the SharedPreference w
ill be returned if |
| 74 * it is installed, even if a host browser is specified in the AndroidManife
st.xml. |
| 75 */ |
| 76 @Test |
| 77 public void testReturnsHostBrowserInSharedPreferenceIfInstalled() { |
| 78 String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| 79 mockInstallBrowsers(ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS); |
| 80 setHostBrowserInMetadata(ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS); |
| 81 setHostBrowserInSharedPreferences(expectedHostBrowser); |
| 82 |
| 83 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 84 Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| 85 } |
| 86 |
| 87 /** |
| 88 * This is a test for the WebAPK WITH a runtime host specified in its Androi
dManifest.xml. |
| 89 * Tests that the package name of the host browser specified in the AndroidM
anifest.xml will be |
| 90 * returned if: |
| 91 * 1. there isn't a host browser specified in the SharedPreference or the sp
ecified one is |
| 92 * uninstalled. |
| 93 * And |
| 94 * 2. the host browser stored in the AndroidManifest is still installed. |
| 95 */ |
| 96 @Test |
| 97 public void testReturnsHostBrowserInManifestIfInstalled() { |
| 98 String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| 99 mockInstallBrowsers(ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS); |
| 100 setHostBrowserInMetadata(expectedHostBrowser); |
| 101 // Simulates there isn't any host browser stored in the SharedPreference
. |
| 102 setHostBrowserInSharedPreferences(null); |
| 103 |
| 104 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 105 Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| 106 |
| 107 WebApkUtils.resetCachedHostPackageForTesting(); |
| 108 // Simulates there is a host browser stored in the SharedPreference but
uninstalled. |
| 109 setHostBrowserInSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS
); |
| 110 hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 111 Assert.assertEquals(hostBrowser, expectedHostBrowser); |
| 112 } |
| 113 |
| 114 /** |
| 115 * This is a test for the WebAPK WITH a runtime host specified in its Androi
dManifest.xml. |
| 116 * Tests that null will be returned if: |
| 117 * 1) there isn't a host browser stored in the SharedPreference or it isn't
installed. |
| 118 * And |
| 119 * 2) the host browser specified in the AndroidManifest.xml isn't installed; |
| 120 * In this test, we only simulate the the first part of the condition 1. |
| 121 */ |
| 122 @Test |
| 123 public void testReturnsNullIfHostBrowserSpecifiedInManifestIsUninstalled() { |
| 124 mockInstallBrowsers(BROWSER_INSTALLED_SUPPORTING_WEBAPKS); |
| 125 setHostBrowserInMetadata(BROWSER_UNINSTALLED_SUPPORTING_WEBAPKS); |
| 126 // Simulates that there isn't any host browser stored in the SharedPrefe
rence. |
| 127 setHostBrowserInSharedPreferences(null); |
| 128 |
| 129 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 130 Assert.assertNull(hostBrowser); |
| 131 } |
| 132 |
| 133 /** |
| 134 * This is a test for the WebAPK WITHOUT any runtime host specified in its A
ndroidManifest.xml. |
| 135 * Tests that the default browser package name will be returned if: |
| 136 * 1. there isn't any host browser stored in the SharedPreference, or the sp
ecified one has |
| 137 * been uninstalled. |
| 138 * And |
| 139 * 2. the default browser supports WebAPKs. |
| 140 * In this test, we only simulate the the first part of the condition 1. |
| 141 */ |
| 142 @Test |
| 143 public void testReturnsDefaultBrowser() { |
| 144 String defaultBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS; |
| 145 mockInstallBrowsers(defaultBrowser); |
| 146 setHostBrowserInMetadata(null); |
| 147 // Simulates that there isn't any host browser stored in the SharedPrefe
rence. |
| 148 setHostBrowserInSharedPreferences(null); |
| 149 |
| 150 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 151 Assert.assertEquals(hostBrowser, defaultBrowser); |
| 152 } |
| 153 |
| 154 /** |
| 155 * This is a test for the WebAPK WITHOUT any runtime host specified in its A
ndroidManifest.xml. |
| 156 * Tests that null will be returned if: |
| 157 * 1. there isn't any host browser stored in the SharedPreference, or the sp
ecified one has |
| 158 * been uninstalled. |
| 159 * And |
| 160 * 2. the default browser doesn't support WebAPKs. |
| 161 * In this test, we only simulate the the first part of the condition 1. |
| 162 */ |
| 163 @Test |
| 164 public void testReturnsNullWhenDefaultBrowserDoesNotSupportWebApks() { |
| 165 mockInstallBrowsers(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPKS); |
| 166 setHostBrowserInMetadata(null); |
| 167 setHostBrowserInSharedPreferences(null); |
| 168 |
| 169 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); |
| 170 Assert.assertNull(hostBrowser); |
| 171 } |
| 172 |
| 173 private static ResolveInfo newResolveInfo(String packageName) { |
| 174 ActivityInfo activityInfo = new ActivityInfo(); |
| 175 activityInfo.packageName = packageName; |
| 176 ResolveInfo resolveInfo = new ResolveInfo(); |
| 177 resolveInfo.activityInfo = activityInfo; |
| 178 return resolveInfo; |
| 179 } |
| 180 |
| 181 private void mockInstallBrowsers(String defaultBrowser) { |
| 182 Intent intent = null; |
| 183 try { |
| 184 intent = Intent.parseUri("http://", Intent.URI_INTENT_SCHEME); |
| 185 } catch (Exception e) { |
| 186 Assert.fail(); |
| 187 return; |
| 188 } |
| 189 |
| 190 for (String name : sInstalledBrowsers) { |
| 191 mPackageManager.addResolveInfoForIntent(intent, newResolveInfo(name)
); |
| 192 } |
| 193 |
| 194 ResolveInfo defaultBrowserInfo = newResolveInfo(defaultBrowser); |
| 195 mPackageManager.addResolveInfoForIntent(intent, defaultBrowserInfo); |
| 196 |
| 197 Mockito.when(mPackageManager.resolveActivity(any(Intent.class), anyInt()
)) |
| 198 .thenReturn(defaultBrowserInfo); |
| 199 } |
| 200 |
| 201 private void setHostBrowserInSharedPreferences(String hostBrowserPackage) { |
| 202 SharedPreferences sharedPref = |
| 203 mContext.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Cont
ext.MODE_PRIVATE); |
| 204 SharedPreferences.Editor editor = sharedPref.edit(); |
| 205 editor.putString(WebApkUtils.SHARED_PREF_RUNTIME_HOST, hostBrowserPackag
e); |
| 206 editor.apply(); |
| 207 } |
| 208 |
| 209 private void setHostBrowserInMetadata(String hostBrowserPackage) { |
| 210 Bundle bundle = new Bundle(); |
| 211 bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPackage); |
| 212 WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle)
; |
| 213 } |
| 214 } |
OLD | NEW |