Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: chrome/android/webapk/shell_apk/junit/src/org/chromium/webapk/shell_apk/WebApkUtilsTest.java

Issue 2858563004: Add support for webapk without runtimeHost (Closed)
Patch Set: Use spy to partically mock. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
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 =
44 "browser.installed.supporting.webapks";
pkotwicz 2017/05/26 22:38:38 Can this be com.chrome.canary?
Xi Han 2017/05/29 21:18:55 I would prefer this naming, since it reflects what
pkotwicz 2017/05/31 01:06:21 I am suggesting this naming because it enables get
Xi Han 2017/05/31 15:50:15 Got it. Updated the naming.
45 private static final String BROWSER_UNINSTALLED_SUPPORTING_WEBAPK =
46 "browser.uninstalled.supporting.webapks";
pkotwicz 2017/05/26 22:38:38 Can this be com.chrome.dev?
47 private static final String BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK =
48 "browser.installed.not.supporting.webapks";
49 private static final String ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS =
50 "another.browser.installed.supporting.webapks";
pkotwicz 2017/05/26 22:38:38 Can this be com.chrome.beta?
51
52 private static final List<String> sInstalledBrowsers = new ArrayList<String> (Arrays.asList(
53 BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK, BROWSER_INSTALLED_SUPPORTIN G_WEBAPKS,
54 ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS));
55
56 private static final List<String> sBrowsersSupportingWebApks =
57 new ArrayList<String>(Arrays.asList(BROWSER_UNINSTALLED_SUPPORTING_W EBAPK,
58 BROWSER_INSTALLED_SUPPORTING_WEBAPKS,
59 ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS));
60
61 private Context mContext;
62 private RobolectricPackageManager mPackageManager;
63
64 @Before
65 public void setUp() {
66 mContext = RuntimeEnvironment.application;
67 mPackageManager =
68 Mockito.spy((RobolectricPackageManager) RuntimeEnvironment.getPa ckageManager());
69 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager);
70
71 WebApkUtils.resetCachedHostPackageForTesting();
72 WebApkUtils.setBrowsersSupportingWebApkForTesting(sBrowsersSupportingWeb Apks);
73 }
74
75 /**
76 * Tests that null will be returned if there isn't any browser installed on the device.
77 */
78 @Test
79 public void testReturnsNullWhenNoBrowserInstalled() {
80 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
81 Assert.assertNull(hostBrowser);
82 }
83
84 /**
85 * Tests that the package name of the host browser in the SharedPreference w ill be returned if
86 * it is installed, no matter whether a host browser is specified in the And roidManifest.xml or
87 * not.
pkotwicz 2017/05/26 22:38:38 How about: "no matter whether a host browser is sp
Xi Han 2017/05/29 21:18:55 Sound good, thanks!
88 */
89 @Test
90 public void testReturnsHostBrowserInSharedPreferenceIfInstalled() {
91 String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS;
92 mockInstallBrowsers();
pkotwicz 2017/05/26 22:38:38 The default browser should be a parameter to mockI
Xi Han 2017/05/29 21:18:55 Yes, adding the default browser as a parameter is
93 mockWebApkPackageMetadata(ANOTHER_BROWSER_INSTALLED_SUPPORTING_WEBAPKS);
94 mockSharedPreferences(expectedHostBrowser);
95
96 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
97 Assert.assertEquals(hostBrowser, expectedHostBrowser);
98 }
99
100 /**
101 * Tests that the package name of the host browser specified in the AndroidM anifest.xml will be
102 * returned if:
103 * 1. there isn't a host browser specified in the SharedPreference or the sp ecified one is
104 * uninstalled;
pkotwicz 2017/05/26 22:38:38 Nit: Make the comment reflect the test. You don't
Xi Han 2017/05/29 21:18:55 Good catch! I add the missing part to the test to
105 * And
106 * 2. there is a host browser stored in the AndroidManifest and it is still installed.
107 */
108 @Test
109 public void testReturnsHostBrowserInManifestIfInstalled() {
110 String expectedHostBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS;
111 mockInstallBrowsers();
112 mockWebApkPackageMetadata(expectedHostBrowser);
113 mockSharedPreferences(null);
114
115 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
116 Assert.assertEquals(hostBrowser, expectedHostBrowser);
117 }
118
119 /**
120 * Tests that null will be returned if:
121 * 1) there isn't a host browser stored in the SharedPreference or it isn't installed.
122 * And
123 * 2) the host browser specified in the AndroidManifest.xml isn't installed;
124 */
125 @Test
126 public void testReturnsNullIfHostBrowserSpecifiedInManifestIsUninstalled() {
pkotwicz 2017/05/26 22:38:38 This test is identical to testReturnsNullWhenDefau
Xi Han 2017/05/29 21:18:55 Sorry for the confusion. They are different tests,
127 mockInstallBrowsers();
128 mockWebApkPackageMetadata(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK);
129 // Simulates that there isn't any host browser stored in the SharedPrefe rence.
130 mockSharedPreferences(null);
131
132 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
133 Assert.assertNull(hostBrowser);
134
135 // Simulates that the host browser stored in the SharedPreference has be en uninstalled.
pkotwicz 2017/05/26 22:38:38 Perhaps this should be its own test. We can simpli
Xi Han 2017/05/29 21:18:55 This is a test for browser-WebAPK, which has a hos
136 mockSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK);
137 hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
138 Assert.assertNull(hostBrowser);
139 }
140
141 /**
142 * Tests that the default browser package name will be returned if:
143 * 1. there isn't any host browser stored in the SharedPreference, or the sp ecified one has
144 * been uninstalled;
145 * And
146 * 2. there isn't a host browser specified in the AndroidManifest.xml;
147 * And
148 * 3. the default browser supports WebAPKs.
149 */
150 @Test
151 public void testReturnsDefaultBrowser() {
152 String defaultBrowser = BROWSER_INSTALLED_SUPPORTING_WEBAPKS;
153 mockInstallBrowsers();
154 mockWebApkPackageMetadata(null);
155 // Simulates that there isn't any host browser stored in the SharedPrefe rence.
156 mockSharedPreferences(null);
157 mockDefaultBrowser(defaultBrowser);
158
159 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
160 Assert.assertEquals(hostBrowser, defaultBrowser);
161
162 // Simulates that the host browser stored in the SharedPreference isn't installed.
pkotwicz 2017/05/26 22:38:38 I don't think that you need to test this case. If
Xi Han 2017/05/29 21:18:55 That test is for browser-webapk, while this is for
pkotwicz 2017/05/31 01:06:21 I think of WebApkUtils#getHostBrowserPackageName()
163 WebApkUtils.resetCachedHostPackageForTesting();
164 mockSharedPreferences(BROWSER_UNINSTALLED_SUPPORTING_WEBAPK);
165 hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
166 Assert.assertEquals(hostBrowser, defaultBrowser);
167 }
168
169 /**
170 * Tests that null will be returned if:
171 * 1. there isn't any host browser stored in the SharedPreference, or the sp ecified one has
172 * been uninstalled;
173 * And
174 * 2. there isn't a host browser specified in the AndroidManifest.xml;
175 * And
176 * 3. the default browser doesn't support WebAPKs.
177 */
178 @Test
179 public void testReturnsNullWhenDefaultBrowserDoesNotSupportWebApks() {
180 mockInstallBrowsers();
181 mockWebApkPackageMetadata(null);
182 mockSharedPreferences(null);
183 mockDefaultBrowser(BROWSER_INSTALLED_NOT_SUPPORTING_WEBAPK);
184
185 String hostBrowser = WebApkUtils.getHostBrowserPackageName(mContext);
186 Assert.assertNull(hostBrowser);
187 }
188
189 private static ResolveInfo newResolveInfo(String packageName) {
190 ActivityInfo activityInfo = new ActivityInfo();
191 activityInfo.packageName = packageName;
192 ResolveInfo resolveInfo = new ResolveInfo();
193 resolveInfo.activityInfo = activityInfo;
194 return resolveInfo;
195 }
196
197 private void mockInstallBrowsers() {
198 Intent intent = null;
199 try {
200 intent = Intent.parseUri("http://", Intent.URI_INTENT_SCHEME);
201 } catch (Exception e) {
202 e.printStackTrace();
203 return;
204 }
205
206 for (String name : sInstalledBrowsers) {
207 mPackageManager.addResolveInfoForIntent(intent, newResolveInfo(name) );
208 }
209 }
210
211 private void mockSharedPreferences(String hostBrowserPackage) {
pkotwicz 2017/05/26 22:38:38 Maybe rename this function to setHostBrowserInShar
Xi Han 2017/05/29 21:18:55 SG
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 mockWebApkPackageMetadata(String hostBrowserPackage) {
pkotwicz 2017/05/26 22:38:38 Maybe rename this function to setHostBrowserInMeta
Xi Han 2017/05/29 21:18:55 Done.
220 Bundle bundle = new Bundle();
221 bundle.putString(WebApkMetaDataKeys.RUNTIME_HOST, hostBrowserPackage);
222 WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle) ;
223 }
224
225 private void mockDefaultBrowser(String defaultBrowser) {
pkotwicz 2017/05/26 22:38:38 Maybe rename this function to setDefaultBrowser()
Xi Han 2017/05/29 21:18:55 Move the logic to |mockInstalledBrowsers()|.
226 ResolveInfo defaultBrowserInfo = newResolveInfo(defaultBrowser);
227 Mockito.when(mPackageManager.resolveActivity(any(Intent.class), anyInt() ))
228 .thenReturn(defaultBrowserInfo);
229 }
230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698