| 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.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.SharedPreferences; | 10 import android.content.SharedPreferences; |
| 11 import android.os.Build; | |
| 12 import android.test.suitebuilder.annotation.MediumTest; | 11 import android.test.suitebuilder.annotation.MediumTest; |
| 13 import android.view.View; | 12 import android.view.View; |
| 14 | 13 |
| 15 import org.chromium.base.ApplicationStatus; | 14 import org.chromium.base.ApplicationStatus; |
| 16 import org.chromium.base.ContextUtils; | 15 import org.chromium.base.ContextUtils; |
| 17 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
| 18 import org.chromium.base.test.util.CommandLineFlags; | |
| 19 import org.chromium.base.test.util.DisabledTest; | |
| 20 import org.chromium.base.test.util.MinAndroidSdkLevel; | |
| 21 import org.chromium.base.test.util.Restriction; | |
| 22 import org.chromium.base.test.util.UrlUtils; | 17 import org.chromium.base.test.util.UrlUtils; |
| 23 import org.chromium.blink_public.platform.WebDisplayMode; | 18 import org.chromium.blink_public.platform.WebDisplayMode; |
| 24 import org.chromium.chrome.browser.ChromeActivity; | 19 import org.chromium.chrome.browser.ChromeActivity; |
| 25 import org.chromium.chrome.browser.ChromeSwitches; | |
| 26 import org.chromium.chrome.browser.ChromeTabbedActivity; | 20 import org.chromium.chrome.browser.ChromeTabbedActivity; |
| 27 import org.chromium.chrome.browser.ShortcutHelper; | 21 import org.chromium.chrome.browser.ShortcutHelper; |
| 28 import org.chromium.chrome.browser.ShortcutSource; | 22 import org.chromium.chrome.browser.ShortcutSource; |
| 29 import org.chromium.chrome.browser.document.DocumentActivity; | |
| 30 import org.chromium.chrome.browser.preferences.DocumentModeManager; | |
| 31 import org.chromium.chrome.browser.tab.Tab; | 23 import org.chromium.chrome.browser.tab.Tab; |
| 32 import org.chromium.chrome.browser.tab.TabIdManager; | 24 import org.chromium.chrome.browser.tab.TabIdManager; |
| 33 import org.chromium.chrome.test.MultiActivityTestBase; | 25 import org.chromium.chrome.test.MultiActivityTestBase; |
| 34 import org.chromium.chrome.test.util.ActivityUtils; | 26 import org.chromium.chrome.test.util.ActivityUtils; |
| 35 import org.chromium.chrome.test.util.ApplicationTestUtils; | 27 import org.chromium.chrome.test.util.ApplicationTestUtils; |
| 36 import org.chromium.chrome.test.util.ChromeRestriction; | |
| 37 import org.chromium.chrome.test.util.browser.TabLoadObserver; | 28 import org.chromium.chrome.test.util.browser.TabLoadObserver; |
| 38 import org.chromium.content.browser.test.util.Criteria; | 29 import org.chromium.content.browser.test.util.Criteria; |
| 39 import org.chromium.content.browser.test.util.CriteriaHelper; | 30 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 40 import org.chromium.content.browser.test.util.JavaScriptUtils; | 31 import org.chromium.content.browser.test.util.JavaScriptUtils; |
| 41 import org.chromium.content.browser.test.util.TouchCommon; | 32 import org.chromium.content.browser.test.util.TouchCommon; |
| 42 import org.chromium.content_public.common.ScreenOrientationValues; | 33 import org.chromium.content_public.common.ScreenOrientationValues; |
| 43 | 34 |
| 44 /** | 35 /** |
| 45 * Tests that WebappActivities are launched correctly. | 36 * Tests that WebappActivities are launched correctly. |
| 46 * | 37 * |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 * Ensure WebappActivities can't be launched without proper security checks. | 209 * Ensure WebappActivities can't be launched without proper security checks. |
| 219 */ | 210 */ |
| 220 @MediumTest | 211 @MediumTest |
| 221 public void testWebappRequiresValidMac() throws Exception { | 212 public void testWebappRequiresValidMac() throws Exception { |
| 222 // Try to start a WebappActivity. Fail because the Intent is insecure. | 213 // Try to start a WebappActivity. Fail because the Intent is insecure. |
| 223 fireWebappIntent(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON,
false); | 214 fireWebappIntent(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON,
false); |
| 224 CriteriaHelper.pollUiThread(new Criteria() { | 215 CriteriaHelper.pollUiThread(new Criteria() { |
| 225 @Override | 216 @Override |
| 226 public boolean isSatisfied() { | 217 public boolean isSatisfied() { |
| 227 Activity lastActivity = ApplicationStatus.getLastTrackedFocusedA
ctivity(); | 218 Activity lastActivity = ApplicationStatus.getLastTrackedFocusedA
ctivity(); |
| 228 return lastActivity instanceof ChromeTabbedActivity | 219 return lastActivity instanceof ChromeTabbedActivity; |
| 229 || lastActivity instanceof DocumentActivity; | |
| 230 } | 220 } |
| 231 }); | 221 }); |
| 232 ChromeActivity chromeActivity = | 222 ChromeActivity chromeActivity = |
| 233 (ChromeActivity) ApplicationStatus.getLastTrackedFocusedActivity
(); | 223 (ChromeActivity) ApplicationStatus.getLastTrackedFocusedActivity
(); |
| 234 waitForFullLoad(chromeActivity, WEBAPP_1_TITLE); | 224 waitForFullLoad(chromeActivity, WEBAPP_1_TITLE); |
| 235 | 225 |
| 236 // Firing a correct Intent should start a WebappActivity instance instea
d of the browser. | 226 // Firing a correct Intent should start a WebappActivity instance instea
d of the browser. |
| 237 fireWebappIntent(WEBAPP_2_ID, WEBAPP_2_URL, WEBAPP_2_TITLE, WEBAPP_ICON,
true); | 227 fireWebappIntent(WEBAPP_2_ID, WEBAPP_2_URL, WEBAPP_2_TITLE, WEBAPP_ICON,
true); |
| 238 CriteriaHelper.pollUiThread(new Criteria() { | 228 CriteriaHelper.pollUiThread(new Criteria() { |
| 239 @Override | 229 @Override |
| 240 public boolean isSatisfied() { | 230 public boolean isSatisfied() { |
| 241 return isWebappActivityReady(ApplicationStatus.getLastTrackedFoc
usedActivity()); | 231 return isWebappActivityReady(ApplicationStatus.getLastTrackedFoc
usedActivity()); |
| 242 } | 232 } |
| 243 }); | 233 }); |
| 244 } | 234 } |
| 245 | 235 |
| 246 /** | 236 /** |
| 247 * Tests that WebappActivities handle window.open() properly in document mod
e. | |
| 248 */ | |
| 249 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 250 @Restriction(ChromeRestriction.RESTRICTION_TYPE_PHONE) | |
| 251 @DisabledTest | |
| 252 public void testWebappHandlesWindowOpenInDocumentMode() throws Exception { | |
| 253 triggerWindowOpenAndWaitForDocumentLoad(ONCLICK_LINK, true); | |
| 254 } | |
| 255 | |
| 256 /** | |
| 257 * Tests that WebappActivities handle window.open() properly in tabbed mode. | 237 * Tests that WebappActivities handle window.open() properly in tabbed mode. |
| 258 */ | 238 */ |
| 259 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) | |
| 260 @MediumTest | 239 @MediumTest |
| 261 public void testWebappHandlesWindowOpenInTabbedMode() throws Exception { | 240 public void testWebappHandlesWindowOpenInTabbedMode() throws Exception { |
| 262 triggerWindowOpenAndWaitForLoad(ChromeTabbedActivity.class, ONCLICK_LINK
, true); | 241 triggerWindowOpenAndWaitForLoad(ChromeTabbedActivity.class, ONCLICK_LINK
, true); |
| 263 } | 242 } |
| 264 | 243 |
| 265 /** | 244 /** |
| 266 * Tests that WebappActivities handle suppressed window.open() properly in d
ocument mode. | |
| 267 */ | |
| 268 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 269 @Restriction(ChromeRestriction.RESTRICTION_TYPE_PHONE) | |
| 270 @DisabledTest | |
| 271 public void testWebappHandlesSuppressedWindowOpenInDocumentMode() throws Exc
eption { | |
| 272 triggerWindowOpenAndWaitForDocumentLoad(HREF_NO_REFERRER_LINK, false); | |
| 273 } | |
| 274 | |
| 275 /** | |
| 276 * Tests that WebappActivities handle suppressed window.open() properly in t
abbed mode. | 245 * Tests that WebappActivities handle suppressed window.open() properly in t
abbed mode. |
| 277 */ | 246 */ |
| 278 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) | |
| 279 @MediumTest | 247 @MediumTest |
| 280 public void testWebappHandlesSuppressedWindowOpenInTabbedMode() throws Excep
tion { | 248 public void testWebappHandlesSuppressedWindowOpenInTabbedMode() throws Excep
tion { |
| 281 triggerWindowOpenAndWaitForLoad(ChromeTabbedActivity.class, HREF_NO_REFE
RRER_LINK, false); | 249 triggerWindowOpenAndWaitForLoad(ChromeTabbedActivity.class, HREF_NO_REFE
RRER_LINK, false); |
| 282 } | 250 } |
| 283 | 251 |
| 284 private void triggerWindowOpenAndWaitForDocumentLoad( | |
| 285 String linkHtml, boolean checkContents) throws Exception { | |
| 286 // We default to tabbed mode. To have the WebappActivity launch a Docume
ntActivity, | |
| 287 // we have to explicitly override that default here. | |
| 288 DocumentModeManager documentModeManager = DocumentModeManager.getInstanc
e( | |
| 289 getInstrumentation().getTargetContext()); | |
| 290 boolean previouslyOptedOut = documentModeManager.isOptedOutOfDocumentMod
e(); | |
| 291 documentModeManager.setOptedOutState(DocumentModeManager.OPT_OUT_PROMO_D
ISMISSED); | |
| 292 try { | |
| 293 triggerWindowOpenAndWaitForLoad(DocumentActivity.class, linkHtml, ch
eckContents); | |
| 294 } finally { | |
| 295 if (previouslyOptedOut) { | |
| 296 documentModeManager.setOptedOutState( | |
| 297 DocumentModeManager.OPTED_OUT_OF_DOCUMENT_MODE); | |
| 298 } | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 private <T extends ChromeActivity> void triggerWindowOpenAndWaitForLoad( | 252 private <T extends ChromeActivity> void triggerWindowOpenAndWaitForLoad( |
| 303 Class<T> classToWaitFor, String linkHtml, boolean checkContents) thr
ows Exception { | 253 Class<T> classToWaitFor, String linkHtml, boolean checkContents) thr
ows Exception { |
| 304 final WebappActivity firstActivity = | 254 final WebappActivity firstActivity = |
| 305 startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, W
EBAPP_ICON); | 255 startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, W
EBAPP_ICON); |
| 306 final int firstWebappId = firstActivity.getActivityTab().getId(); | 256 final int firstWebappId = firstActivity.getActivityTab().getId(); |
| 307 | 257 |
| 308 // Load up the test page. | 258 // Load up the test page. |
| 309 new TabLoadObserver(firstActivity.getActivityTab()).fullyLoadUrl(linkHtm
l); | 259 new TabLoadObserver(firstActivity.getActivityTab()).fullyLoadUrl(linkHtm
l); |
| 310 | 260 |
| 311 // Do a plain click to make the link open in the main browser via a wind
ow.open(). | 261 // Do a plain click to make the link open in the main browser via a wind
ow.open(). |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 323 |
| 374 WebappActivity webappActivity = (WebappActivity) lastActivity; | 324 WebappActivity webappActivity = (WebappActivity) lastActivity; |
| 375 if (webappActivity.getActivityTab() == null) return false; | 325 if (webappActivity.getActivityTab() == null) return false; |
| 376 | 326 |
| 377 View rootView = webappActivity.findViewById(android.R.id.content); | 327 View rootView = webappActivity.findViewById(android.R.id.content); |
| 378 if (!rootView.hasWindowFocus()) return false; | 328 if (!rootView.hasWindowFocus()) return false; |
| 379 | 329 |
| 380 return true; | 330 return true; |
| 381 } | 331 } |
| 382 } | 332 } |
| OLD | NEW |