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