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 import static org.mockito.ArgumentMatchers.anyString; | |
| 10 | |
| 11 import android.content.Context; | |
| 12 import android.content.Intent; | |
| 13 import android.content.SharedPreferences; | |
| 14 import android.content.pm.ActivityInfo; | |
| 15 import android.content.pm.ApplicationInfo; | |
| 16 import android.content.pm.PackageManager; | |
| 17 import android.content.pm.ResolveInfo; | |
| 18 import android.os.Bundle; | |
| 19 | |
| 20 import org.junit.Assert; | |
| 21 import org.junit.Before; | |
| 22 import org.junit.Test; | |
| 23 import org.junit.runner.RunWith; | |
| 24 import org.mockito.Mock; | |
| 25 import org.mockito.Mockito; | |
| 26 import org.robolectric.annotation.Config; | |
| 27 | |
| 28 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 29 import org.chromium.webapk.lib.common.WebApkConstants; | |
| 30 import org.chromium.webapk.lib.common.WebApkMetaDataKeys; | |
| 31 | |
| 32 import java.util.ArrayList; | |
| 33 import java.util.Arrays; | |
| 34 import java.util.List; | |
| 35 | |
| 36 /** | |
| 37 * Tests for WebApkUtils. | |
| 38 */ | |
| 39 | |
| 40 @RunWith(LocalRobolectricTestRunner.class) | |
| 41 @Config(manifest = Config.NONE) | |
| 42 public class WebApkUtilsTest { | |
| 43 private static final String HOST_BROWSER_PACKAGE_NAME = "browser.supporting. webapks"; | |
| 44 private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.test_ package"; | |
| 45 | |
| 46 private static final List<String> sInstalledBrowsers = | |
| 47 new ArrayList<String>(Arrays.asList("browser.installed", HOST_BROWSE R_PACKAGE_NAME)); | |
| 48 | |
| 49 @Mock | |
| 50 private Context mContext; | |
| 51 @Mock | |
| 52 private PackageManager mPackageManager; | |
| 53 | |
| 54 @Before | |
| 55 public void setUp() { | |
| 56 mContext = Mockito.mock(Context.class); | |
| 57 Mockito.when(mContext.getPackageName()).thenReturn(WEBAPK_PACKAGE_NAME); | |
| 58 mPackageManager = Mockito.mock(PackageManager.class); | |
| 59 Mockito.when(mContext.getPackageManager()).thenReturn(mPackageManager); | |
| 60 | |
| 61 WebApkUtils.resetCachedHostPackageForTesting(); | |
| 62 WebApkUtils.setBrowsersSupportingWebApkForTesting(sInstalledBrowsers); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Tests that null will be returned if there isn't any browser installed on the device. | |
| 67 */ | |
| 68 @Test | |
| 69 public void testReturnsEmptyStringWhenNoBrowserInstalled() { | |
|
Yaron
2017/05/15 18:41:37
testReturnsNull
Xi Han
2017/05/16 13:50:39
Done.
| |
| 70 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 71 Assert.assertNull(hostBrowser); | |
| 72 } | |
| 73 | |
| 74 /** | |
| 75 * Tests that the package name of the host browser specified in the AndroidM anifest.xml will be | |
| 76 * returned as the WebAPK's host browser if it is installed. | |
| 77 */ | |
| 78 @Test | |
| 79 public void testReturnsHostBrowserInManifestIfInstalled() { | |
| 80 String expectedHostBrowser = HOST_BROWSER_PACKAGE_NAME; | |
| 81 mockInstallBrowsers(); | |
| 82 mockWebApkPackageMetadata(HOST_BROWSER_PACKAGE_NAME); | |
| 83 mockSharedPreferences(null); | |
| 84 | |
| 85 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 86 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,
| |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * Tests that the package name of the host browser in the SharedPreference w ill be returned if: | |
| 91 * 1. there isn't a host browser specified in the AndroidManifest.xml, or th e specified one has | |
| 92 * been uninstalled; | |
| 93 * And | |
| 94 * 2. there is a host browser stored in the SharedPreference and it is still installed. | |
| 95 */ | |
| 96 @Test | |
| 97 public void testReturnsHostBrowserInSharedPreferenceIfInstalled() { | |
| 98 String expectedHostBrowser = HOST_BROWSER_PACKAGE_NAME; | |
| 99 mockInstallBrowsers(); | |
| 100 mockWebApkPackageMetadata(null); | |
| 101 mockSharedPreferences(expectedHostBrowser); | |
| 102 | |
| 103 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 104 Assert.assertTrue(hostBrowser.equals(expectedHostBrowser)); | |
| 105 | |
| 106 WebApkUtils.resetCachedHostPackageForTesting(); | |
| 107 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!
| |
| 108 hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 109 Assert.assertTrue(hostBrowser.equals(expectedHostBrowser)); | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Tests that the default browser package name will be returned if: | |
| 114 * 1. there isn't any host browser specified in the AndroidManifest.xml or t he SharedPreference, | |
| 115 * or the specified one has uninstalled; | |
| 116 * And | |
| 117 * 2. the default browser supports WebAPKs. | |
| 118 */ | |
| 119 @Test | |
| 120 public void testReturnsDefaultBrowser() { | |
| 121 String defaultBrowser = HOST_BROWSER_PACKAGE_NAME; | |
| 122 mockInstallBrowsers(); | |
| 123 mockWebApkPackageMetadata(null); | |
| 124 mockSharedPreferences(null); | |
| 125 mockDefaultBrowser(defaultBrowser); | |
| 126 | |
| 127 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 128 Assert.assertTrue(hostBrowser.equals(defaultBrowser)); | |
| 129 | |
| 130 WebApkUtils.resetCachedHostPackageForTesting(); | |
| 131 mockSharedPreferences("browser.uninstalled"); | |
| 132 hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 133 Assert.assertTrue(hostBrowser.equals(defaultBrowser)); | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Tests that the package name of one of installed browsers that support Web APKs will be | |
| 138 * returned if: | |
| 139 * 1. there is neither a host browser specified in the AndroidManifest.xml n or in the | |
| 140 * SharedPreference, or the specified one has uninstalled; | |
| 141 * And | |
| 142 * 2. the default browser doesn't support WebAPKs. | |
| 143 * And | |
| 144 * 3. there is another installed browser that supports WebAPKs. | |
| 145 */ | |
| 146 @Test | |
| 147 public void testReturnsInstalledBrowserSupportingWebApks() { | |
| 148 mockInstallBrowsers(); | |
| 149 mockWebApkPackageMetadata(null); | |
| 150 mockSharedPreferences(null); | |
| 151 mockDefaultBrowser("browser.not.supporting.webapks"); | |
| 152 | |
| 153 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext); | |
| 154 Assert.assertNotNull(hostBrowser); | |
| 155 Assert.assertTrue(sInstalledBrowsers.contains(hostBrowser)); | |
| 156 } | |
| 157 | |
| 158 private static ResolveInfo newResolveInfo(String packageName) { | |
| 159 ActivityInfo activityInfo = new ActivityInfo(); | |
| 160 activityInfo.packageName = packageName; | |
| 161 ResolveInfo resolveInfo = new ResolveInfo(); | |
| 162 resolveInfo.activityInfo = activityInfo; | |
| 163 return resolveInfo; | |
| 164 } | |
| 165 | |
| 166 private void mockInstallBrowsers() { | |
| 167 List<ResolveInfo> browsers = new ArrayList<ResolveInfo>(); | |
| 168 for (String name : sInstalledBrowsers) { | |
| 169 browsers.add(newResolveInfo(name)); | |
| 170 } | |
| 171 Mockito.when(mPackageManager.queryIntentActivities(any(Intent.class), an yInt())) | |
| 172 .thenReturn(browsers); | |
| 173 } | |
| 174 | |
| 175 private void mockSharedPreferences(String hostBrowserPackage) { | |
| 176 SharedPreferences sharedPreferences = Mockito.mock(SharedPreferences.cla ss); | |
| 177 Mockito.when(mContext.getSharedPreferences( | |
| 178 WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE) ) | |
| 179 .thenReturn(sharedPreferences); | |
| 180 | |
| 181 Mockito.when(sharedPreferences.getString(WebApkUtils.SHARED_PREF_RUNTIME _HOST, null)) | |
| 182 .thenReturn(hostBrowserPackage); | |
| 183 SharedPreferences.Editor editor = Mockito.mock(SharedPreferences.Editor. class); | |
| 184 Mockito.when(sharedPreferences.edit()).thenReturn(editor); | |
| 185 } | |
| 186 | |
| 187 private void mockWebApkPackageMetadata(String hostBrowserPackage) { | |
| 188 ApplicationInfo ai = Mockito.mock(ApplicationInfo.class); | |
| 189 ai.metaData = new Bundle(); | |
| 190 if (hostBrowserPackage != null) { | |
| 191 ai.metaData.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPa ckage); | |
| 192 } | |
| 193 try { | |
| 194 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.
| |
| 195 } catch (PackageManager.NameNotFoundException e) { | |
| 196 e.printStackTrace(); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 private void mockDefaultBrowser(String defaultBrowser) { | |
| 201 ResolveInfo defaultBrowserInfo = newResolveInfo(defaultBrowser); | |
| 202 Mockito.when(mPackageManager.resolveActivity(any(Intent.class), anyInt() )) | |
| 203 .thenReturn(defaultBrowserInfo); | |
| 204 } | |
| 205 } | |
| OLD | NEW |