OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
| 10 import android.support.test.InstrumentationRegistry; |
10 import android.support.test.filters.SmallTest; | 11 import android.support.test.filters.SmallTest; |
11 | 12 |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Rule; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 |
12 import org.chromium.base.ThreadUtils; | 19 import org.chromium.base.ThreadUtils; |
13 import org.chromium.base.test.util.CommandLineFlags; | 20 import org.chromium.base.test.util.CommandLineFlags; |
14 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
15 import org.chromium.base.test.util.Restriction; | 22 import org.chromium.base.test.util.Restriction; |
16 import org.chromium.base.test.util.RetryOnFailure; | 23 import org.chromium.base.test.util.RetryOnFailure; |
17 import org.chromium.base.test.util.UrlUtils; | 24 import org.chromium.base.test.util.UrlUtils; |
18 import org.chromium.chrome.R; | 25 import org.chromium.chrome.R; |
19 import org.chromium.chrome.browser.ChromeActivity; | 26 import org.chromium.chrome.browser.ChromeActivity; |
| 27 import org.chromium.chrome.browser.ChromeSwitches; |
20 import org.chromium.chrome.browser.ShortcutHelper; | 28 import org.chromium.chrome.browser.ShortcutHelper; |
21 import org.chromium.chrome.browser.tab.Tab; | 29 import org.chromium.chrome.browser.tab.Tab; |
22 import org.chromium.chrome.browser.tabmodel.TabModel; | 30 import org.chromium.chrome.browser.tabmodel.TabModel; |
23 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 31 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 32 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
24 import org.chromium.chrome.test.util.browser.TabLoadObserver; | 33 import org.chromium.chrome.test.util.browser.TabLoadObserver; |
25 import org.chromium.chrome.test.util.browser.TabTitleObserver; | 34 import org.chromium.chrome.test.util.browser.TabTitleObserver; |
26 import org.chromium.content.browser.test.util.Criteria; | 35 import org.chromium.content.browser.test.util.Criteria; |
27 import org.chromium.content.browser.test.util.CriteriaHelper; | 36 import org.chromium.content.browser.test.util.CriteriaHelper; |
28 import org.chromium.content.common.ContentSwitches; | 37 import org.chromium.content.common.ContentSwitches; |
29 import org.chromium.net.test.EmbeddedTestServer; | 38 import org.chromium.net.test.EmbeddedTestServer; |
30 | 39 |
31 import java.util.concurrent.Callable; | 40 import java.util.concurrent.Callable; |
32 | 41 |
33 /** | 42 /** |
34 * Tests org.chromium.chrome.browser.webapps.AddToHomescreenManager and its C++
counterpart. | 43 * Tests org.chromium.chrome.browser.webapps.AddToHomescreenManager and its C++
counterpart. |
35 */ | 44 */ |
| 45 @RunWith(ChromeJUnit4ClassRunner.class) |
36 @RetryOnFailure | 46 @RetryOnFailure |
37 public class AddToHomescreenManagerTest extends ChromeActivityTestCaseBase<Chrom
eActivity> { | 47 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 48 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) |
| 49 public class AddToHomescreenManagerTest { |
| 50 @Rule |
| 51 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = |
| 52 new ChromeActivityTestRule<>(ChromeActivity.class); |
| 53 |
38 private static final String WEBAPP_ACTION_NAME = "WEBAPP_ACTION"; | 54 private static final String WEBAPP_ACTION_NAME = "WEBAPP_ACTION"; |
39 | 55 |
40 private static final String WEBAPP_TITLE = "Webapp shortcut"; | 56 private static final String WEBAPP_TITLE = "Webapp shortcut"; |
41 private static final String WEBAPP_HTML = UrlUtils.encodeHtmlDataUri( | 57 private static final String WEBAPP_HTML = UrlUtils.encodeHtmlDataUri( |
42 "<html><head>" | 58 "<html><head>" |
43 + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" | 59 + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" |
44 + "<title>" + WEBAPP_TITLE + "</title>" | 60 + "<title>" + WEBAPP_TITLE + "</title>" |
45 + "</head><body>Webapp capable</body></html>"); | 61 + "</head><body>Webapp capable</body></html>"); |
46 private static final String EDITED_WEBAPP_TITLE = "Webapp shortcut edited"; | 62 private static final String EDITED_WEBAPP_TITLE = "Webapp shortcut edited"; |
47 | 63 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 120 } |
105 | 121 |
106 private class WebappDataStorageWrapper extends WebappDataStorage { | 122 private class WebappDataStorageWrapper extends WebappDataStorage { |
107 | 123 |
108 public WebappDataStorageWrapper(String webappId) { | 124 public WebappDataStorageWrapper(String webappId) { |
109 super(webappId); | 125 super(webappId); |
110 } | 126 } |
111 | 127 |
112 @Override | 128 @Override |
113 public void updateSplashScreenImage(String splashScreenImage) { | 129 public void updateSplashScreenImage(String splashScreenImage) { |
114 assertNull(mSplashImage); | 130 Assert.assertNull(mSplashImage); |
115 mSplashImage = splashScreenImage; | 131 mSplashImage = splashScreenImage; |
116 } | 132 } |
117 } | 133 } |
118 } | 134 } |
119 /** | 135 /** |
120 * Test AddToHomescreenManager subclass which mocks showing the add-to-homes
creen dialog and | 136 * Test AddToHomescreenManager subclass which mocks showing the add-to-homes
creen dialog and |
121 * adds the shortcut to the home screen once it is ready. | 137 * adds the shortcut to the home screen once it is ready. |
122 */ | 138 */ |
123 private static class TestAddToHomescreenManager extends AddToHomescreenManag
er { | 139 private static class TestAddToHomescreenManager extends AddToHomescreenManag
er { |
124 private String mTitle; | 140 private String mTitle; |
(...skipping 21 matching lines...) Expand all Loading... |
146 | 162 |
147 setObserver(observer); | 163 setObserver(observer); |
148 } | 164 } |
149 } | 165 } |
150 | 166 |
151 private EmbeddedTestServer mTestServer; | 167 private EmbeddedTestServer mTestServer; |
152 private ChromeActivity mActivity; | 168 private ChromeActivity mActivity; |
153 private Tab mTab; | 169 private Tab mTab; |
154 private TestShortcutHelperDelegate mShortcutHelperDelegate; | 170 private TestShortcutHelperDelegate mShortcutHelperDelegate; |
155 | 171 |
156 public AddToHomescreenManagerTest() { | 172 @Before |
157 super(ChromeActivity.class); | |
158 } | |
159 | |
160 @Override | |
161 public void startMainActivity() throws InterruptedException { | |
162 startMainActivityOnBlankPage(); | |
163 } | |
164 | |
165 @Override | |
166 public void setUp() throws Exception { | 173 public void setUp() throws Exception { |
167 super.setUp(); | 174 mActivityTestRule.startMainActivityOnBlankPage(); |
168 ChromeWebApkHost.initForTesting(false); | 175 ChromeWebApkHost.initForTesting(false); |
169 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); | 176 mTestServer = EmbeddedTestServer.createAndStartServer( |
| 177 InstrumentationRegistry.getInstrumentation().getContext()); |
170 mShortcutHelperDelegate = new TestShortcutHelperDelegate(); | 178 mShortcutHelperDelegate = new TestShortcutHelperDelegate(); |
171 ShortcutHelper.setDelegateForTests(mShortcutHelperDelegate); | 179 ShortcutHelper.setDelegateForTests(mShortcutHelperDelegate); |
172 mActivity = getActivity(); | 180 mActivity = mActivityTestRule.getActivity(); |
173 mTab = mActivity.getActivityTab(); | 181 mTab = mActivity.getActivityTab(); |
174 } | 182 } |
175 | 183 |
| 184 @Test |
176 @SmallTest | 185 @SmallTest |
177 @Feature("{Webapp}") | 186 @Feature("{Webapp}") |
178 public void testAddWebappShortcuts() throws Exception { | 187 public void testAddWebappShortcuts() throws Exception { |
179 // Add a webapp shortcut and make sure the intent's parameters make sens
e. | 188 // Add a webapp shortcut and make sure the intent's parameters make sens
e. |
180 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); | 189 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); |
181 addShortcutToTab(mTab, ""); | 190 addShortcutToTab(mTab, ""); |
182 assertEquals(WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShortcutTit
le); | 191 Assert.assertEquals(WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); |
183 | 192 |
184 Intent launchIntent = mShortcutHelperDelegate.mRequestedShortcutIntent; | 193 Intent launchIntent = mShortcutHelperDelegate.mRequestedShortcutIntent; |
185 assertEquals(WEBAPP_HTML, launchIntent.getStringExtra(ShortcutHelper.EXT
RA_URL)); | 194 Assert.assertEquals(WEBAPP_HTML, launchIntent.getStringExtra(ShortcutHel
per.EXTRA_URL)); |
186 assertEquals(WEBAPP_ACTION_NAME, launchIntent.getAction()); | 195 Assert.assertEquals(WEBAPP_ACTION_NAME, launchIntent.getAction()); |
187 assertEquals(mActivity.getPackageName(), launchIntent.getPackage()); | 196 Assert.assertEquals(mActivity.getPackageName(), launchIntent.getPackage(
)); |
188 | 197 |
189 // Add a second shortcut and make sure it matches the second webapp's pa
rameters. | 198 // Add a second shortcut and make sure it matches the second webapp's pa
rameters. |
190 mShortcutHelperDelegate.clearRequestedShortcutData(); | 199 mShortcutHelperDelegate.clearRequestedShortcutData(); |
191 loadUrl(SECOND_WEBAPP_HTML, SECOND_WEBAPP_TITLE); | 200 loadUrl(SECOND_WEBAPP_HTML, SECOND_WEBAPP_TITLE); |
192 addShortcutToTab(mTab, ""); | 201 addShortcutToTab(mTab, ""); |
193 assertEquals(SECOND_WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); | 202 Assert.assertEquals(SECOND_WEBAPP_TITLE, mShortcutHelperDelegate.mReques
tedShortcutTitle); |
194 | 203 |
195 Intent newLaunchIntent = mShortcutHelperDelegate.mRequestedShortcutInten
t; | 204 Intent newLaunchIntent = mShortcutHelperDelegate.mRequestedShortcutInten
t; |
196 assertEquals(SECOND_WEBAPP_HTML, newLaunchIntent.getStringExtra(Shortcut
Helper.EXTRA_URL)); | 205 Assert.assertEquals( |
197 assertEquals(WEBAPP_ACTION_NAME, newLaunchIntent.getAction()); | 206 SECOND_WEBAPP_HTML, newLaunchIntent.getStringExtra(ShortcutHelpe
r.EXTRA_URL)); |
198 assertEquals(mActivity.getPackageName(), newLaunchIntent.getPackage()); | 207 Assert.assertEquals(WEBAPP_ACTION_NAME, newLaunchIntent.getAction()); |
| 208 Assert.assertEquals(mActivity.getPackageName(), newLaunchIntent.getPacka
ge()); |
199 } | 209 } |
200 | 210 |
| 211 @Test |
201 @SmallTest | 212 @SmallTest |
202 @Feature("{Webapp}") | 213 @Feature("{Webapp}") |
203 public void testAddBookmarkShortcut() throws Exception { | 214 public void testAddBookmarkShortcut() throws Exception { |
204 loadUrl(NORMAL_HTML, NORMAL_TITLE); | 215 loadUrl(NORMAL_HTML, NORMAL_TITLE); |
205 addShortcutToTab(mTab, ""); | 216 addShortcutToTab(mTab, ""); |
206 | 217 |
207 // Make sure the intent's parameters make sense. | 218 // Make sure the intent's parameters make sense. |
208 assertEquals(NORMAL_TITLE, mShortcutHelperDelegate.mRequestedShortcutTit
le); | 219 Assert.assertEquals(NORMAL_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); |
209 | 220 |
210 Intent launchIntent = mShortcutHelperDelegate.mRequestedShortcutIntent; | 221 Intent launchIntent = mShortcutHelperDelegate.mRequestedShortcutIntent; |
211 assertEquals(mActivity.getPackageName(), launchIntent.getPackage()); | 222 Assert.assertEquals(mActivity.getPackageName(), launchIntent.getPackage(
)); |
212 assertEquals(Intent.ACTION_VIEW, launchIntent.getAction()); | 223 Assert.assertEquals(Intent.ACTION_VIEW, launchIntent.getAction()); |
213 assertEquals(NORMAL_HTML, launchIntent.getDataString()); | 224 Assert.assertEquals(NORMAL_HTML, launchIntent.getDataString()); |
214 } | 225 } |
215 | 226 |
| 227 @Test |
216 @SmallTest | 228 @SmallTest |
217 @Feature("{Webapp}") | 229 @Feature("{Webapp}") |
218 public void testAddWebappShortcutsWithoutTitleEdit() throws Exception { | 230 public void testAddWebappShortcutsWithoutTitleEdit() throws Exception { |
219 // Add a webapp shortcut using the page's title. | 231 // Add a webapp shortcut using the page's title. |
220 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); | 232 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); |
221 addShortcutToTab(mTab, ""); | 233 addShortcutToTab(mTab, ""); |
222 assertEquals(WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShortcutTit
le); | 234 Assert.assertEquals(WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); |
223 } | 235 } |
224 | 236 |
| 237 @Test |
225 @SmallTest | 238 @SmallTest |
226 @Feature("{Webapp}") | 239 @Feature("{Webapp}") |
227 public void testAddWebappShortcutsWithTitleEdit() throws Exception { | 240 public void testAddWebappShortcutsWithTitleEdit() throws Exception { |
228 // Add a webapp shortcut with a custom title. | 241 // Add a webapp shortcut with a custom title. |
229 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); | 242 loadUrl(WEBAPP_HTML, WEBAPP_TITLE); |
230 addShortcutToTab(mTab, EDITED_WEBAPP_TITLE); | 243 addShortcutToTab(mTab, EDITED_WEBAPP_TITLE); |
231 assertEquals(EDITED_WEBAPP_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); | 244 Assert.assertEquals(EDITED_WEBAPP_TITLE, mShortcutHelperDelegate.mReques
tedShortcutTitle); |
232 } | 245 } |
233 | 246 |
| 247 @Test |
234 @SmallTest | 248 @SmallTest |
235 @Feature("{Webapp}") | 249 @Feature("{Webapp}") |
236 public void testAddWebappShortcutsWithApplicationName() throws Exception { | 250 public void testAddWebappShortcutsWithApplicationName() throws Exception { |
237 loadUrl(META_APP_NAME_HTML, META_APP_NAME_PAGE_TITLE); | 251 loadUrl(META_APP_NAME_HTML, META_APP_NAME_PAGE_TITLE); |
238 addShortcutToTab(mTab, ""); | 252 addShortcutToTab(mTab, ""); |
239 assertEquals(META_APP_NAME_TITLE, mShortcutHelperDelegate.mRequestedShor
tcutTitle); | 253 Assert.assertEquals(META_APP_NAME_TITLE, mShortcutHelperDelegate.mReques
tedShortcutTitle); |
240 } | 254 } |
241 | 255 |
| 256 @Test |
242 @SmallTest | 257 @SmallTest |
243 @Feature("{Webapp}") | 258 @Feature("{Webapp}") |
244 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) | 259 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) |
245 @CommandLineFlags.Add(ContentSwitches.DISABLE_POPUP_BLOCKING) | 260 @CommandLineFlags.Add(ContentSwitches.DISABLE_POPUP_BLOCKING) |
246 public void testAddWebappShortcutWithEmptyPage() throws Exception { | 261 public void testAddWebappShortcutWithEmptyPage() throws Exception { |
247 Tab spawnedPopup = spawnPopupInBackground(""); | 262 Tab spawnedPopup = spawnPopupInBackground(""); |
248 addShortcutToTab(spawnedPopup, ""); | 263 addShortcutToTab(spawnedPopup, ""); |
249 } | 264 } |
250 | 265 |
| 266 @Test |
251 @SmallTest | 267 @SmallTest |
252 @Feature("{Webapp}") | 268 @Feature("{Webapp}") |
253 public void testAddWebappShortcutSplashScreenIcon() throws Exception { | 269 public void testAddWebappShortcutSplashScreenIcon() throws Exception { |
254 try { | 270 try { |
255 // Sets the overriden factory to observer splash screen update. | 271 // Sets the overriden factory to observer splash screen update. |
256 final TestDataStorageFactory dataStorageFactory = new TestDataStorag
eFactory(); | 272 final TestDataStorageFactory dataStorageFactory = new TestDataStorag
eFactory(); |
257 WebappDataStorage.setFactoryForTests(dataStorageFactory); | 273 WebappDataStorage.setFactoryForTests(dataStorageFactory); |
258 | 274 |
259 loadUrl(mTestServer.getURL(MANIFEST_PATH), MANIFEST_TITLE); | 275 loadUrl(mTestServer.getURL(MANIFEST_PATH), MANIFEST_TITLE); |
260 addShortcutToTab(mTab, ""); | 276 addShortcutToTab(mTab, ""); |
261 | 277 |
262 // Make sure that the splash screen image was downloaded. | 278 // Make sure that the splash screen image was downloaded. |
263 CriteriaHelper.pollUiThread(new Criteria() { | 279 CriteriaHelper.pollUiThread(new Criteria() { |
264 @Override | 280 @Override |
265 public boolean isSatisfied() { | 281 public boolean isSatisfied() { |
266 return dataStorageFactory.mSplashImage != null; | 282 return dataStorageFactory.mSplashImage != null; |
267 } | 283 } |
268 }); | 284 }); |
269 | 285 |
270 // Test that bitmap sizes match expectations. | 286 // Test that bitmap sizes match expectations. |
271 int idealSize = mActivity.getResources().getDimensionPixelSize( | 287 int idealSize = mActivity.getResources().getDimensionPixelSize( |
272 R.dimen.webapp_splash_image_size_ideal); | 288 R.dimen.webapp_splash_image_size_ideal); |
273 Bitmap splashImage = | 289 Bitmap splashImage = |
274 ShortcutHelper.decodeBitmapFromString(dataStorageFactory.mSp
lashImage); | 290 ShortcutHelper.decodeBitmapFromString(dataStorageFactory.mSp
lashImage); |
275 assertEquals(idealSize, splashImage.getWidth()); | 291 Assert.assertEquals(idealSize, splashImage.getWidth()); |
276 assertEquals(idealSize, splashImage.getHeight()); | 292 Assert.assertEquals(idealSize, splashImage.getHeight()); |
277 } finally { | 293 } finally { |
278 mTestServer.stopAndDestroyServer(); | 294 mTestServer.stopAndDestroyServer(); |
279 } | 295 } |
280 } | 296 } |
281 | 297 |
282 /** Tests that the appinstalled event is fired when an app is installed. | 298 /** Tests that the appinstalled event is fired when an app is installed. |
283 */ | 299 */ |
| 300 @Test |
284 @SmallTest | 301 @SmallTest |
285 @Feature("{Webapp}") | 302 @Feature("{Webapp}") |
286 public void testAddWebappShortcutAppInstalledEvent() throws Exception { | 303 public void testAddWebappShortcutAppInstalledEvent() throws Exception { |
287 try { | 304 try { |
288 loadUrl(mTestServer.getURL(EVENT_WEBAPP_PATH), EVENT_WEBAPP_TITLE); | 305 loadUrl(mTestServer.getURL(EVENT_WEBAPP_PATH), EVENT_WEBAPP_TITLE); |
289 addShortcutToTab(mTab, ""); | 306 addShortcutToTab(mTab, ""); |
290 | 307 |
291 // Wait for the tab title to change. This will happen (due to the Ja
vaScript that runs | 308 // Wait for the tab title to change. This will happen (due to the Ja
vaScript that runs |
292 // in the page) once the appinstalled event has been fired. | 309 // in the page) once the appinstalled event has been fired. |
293 new TabTitleObserver(mTab, "Got appinstalled").waitForTitleUpdate(3)
; | 310 new TabTitleObserver(mTab, "Got appinstalled").waitForTitleUpdate(3)
; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 "(function() {" | 362 "(function() {" |
346 + "window.open('" + url + "');" | 363 + "window.open('" + url + "');" |
347 + "})()", | 364 + "})()", |
348 null); | 365 null); |
349 } | 366 } |
350 }); | 367 }); |
351 | 368 |
352 CriteriaHelper.pollUiThread(Criteria.equals(2, new Callable<Integer>() { | 369 CriteriaHelper.pollUiThread(Criteria.equals(2, new Callable<Integer>() { |
353 @Override | 370 @Override |
354 public Integer call() { | 371 public Integer call() { |
355 return getActivity().getTabModelSelector().getModel(false).getCo
unt(); | 372 return mActivityTestRule.getActivity() |
| 373 .getTabModelSelector() |
| 374 .getModel(false) |
| 375 .getCount(); |
356 } | 376 } |
357 })); | 377 })); |
358 | 378 |
359 TabModel tabModel = getActivity().getTabModelSelector().getModel(false); | 379 TabModel tabModel = mActivityTestRule.getActivity().getTabModelSelector(
).getModel(false); |
360 assertEquals(0, tabModel.indexOf(mTab)); | 380 Assert.assertEquals(0, tabModel.indexOf(mTab)); |
361 return getActivity().getTabModelSelector().getModel(false).getTabAt(1); | 381 return mActivityTestRule.getActivity().getTabModelSelector().getModel(fa
lse).getTabAt(1); |
362 } | 382 } |
363 } | 383 } |
OLD | NEW |