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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/externalnav/ExternalNavigationHandlerTest.java

Issue 2550413007: Instant Apps: Add instrumentation tests. (Closed)
Patch Set: Fix test expectation. Created 4 years 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
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.externalnav; 5 package org.chromium.chrome.browser.externalnav;
6 6
7 import android.annotation.SuppressLint; 7 import android.annotation.SuppressLint;
8 import android.content.Intent; 8 import android.content.Intent;
9 import android.content.pm.ActivityInfo; 9 import android.content.pm.ActivityInfo;
10 import android.content.pm.PackageManager; 10 import android.content.pm.PackageManager;
11 import android.content.pm.ResolveInfo; 11 import android.content.pm.ResolveInfo;
12 import android.net.Uri; 12 import android.net.Uri;
13 import android.os.SystemClock; 13 import android.os.SystemClock;
14 import android.provider.Browser; 14 import android.provider.Browser;
15 import android.support.customtabs.CustomTabsIntent; 15 import android.support.customtabs.CustomTabsIntent;
16 import android.test.mock.MockContext; 16 import android.test.mock.MockContext;
17 import android.test.mock.MockPackageManager; 17 import android.test.mock.MockPackageManager;
18 import android.test.suitebuilder.annotation.SmallTest; 18 import android.test.suitebuilder.annotation.SmallTest;
19 19
20 import org.chromium.base.metrics.RecordHistogram; 20 import org.chromium.base.metrics.RecordHistogram;
21 import org.chromium.chrome.browser.IntentHandler; 21 import org.chromium.chrome.browser.IntentHandler;
22 import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider; 22 import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
23 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid eUrlLoadingResult; 23 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid eUrlLoadingResult;
24 import org.chromium.chrome.browser.instantapps.InstantAppsHandler;
24 import org.chromium.chrome.browser.tab.Tab; 25 import org.chromium.chrome.browser.tab.Tab;
25 import org.chromium.chrome.browser.tab.TabRedirectHandler; 26 import org.chromium.chrome.browser.tab.TabRedirectHandler;
26 import org.chromium.chrome.browser.webapps.ChromeWebApkHost; 27 import org.chromium.chrome.browser.webapps.ChromeWebApkHost;
27 import org.chromium.content.browser.test.NativeLibraryTestBase; 28 import org.chromium.content.browser.test.NativeLibraryTestBase;
28 import org.chromium.ui.base.PageTransition; 29 import org.chromium.ui.base.PageTransition;
29 import org.chromium.webapk.lib.common.WebApkConstants; 30 import org.chromium.webapk.lib.common.WebApkConstants;
30 31
31 import java.net.URISyntaxException; 32 import java.net.URISyntaxException;
32 import java.util.ArrayList; 33 import java.util.ArrayList;
33 import java.util.List; 34 import java.util.List;
34 35
35 /** 36 /**
36 * Instrumentation tests for {@link ExternalNavigationHandler}. 37 * Instrumentation tests for {@link ExternalNavigationHandler}.
37 */ 38 */
38 public class ExternalNavigationHandlerTest extends NativeLibraryTestBase { 39 public class ExternalNavigationHandlerTest extends NativeLibraryTestBase {
39 40
40 // Expectations 41 // Expectations
41 private static final int IGNORE = 0x0; 42 private static final int IGNORE = 0x0;
42 private static final int START_INCOGNITO = 0x1; 43 private static final int START_INCOGNITO = 0x1;
43 private static final int START_CHROME = 0x2; 44 private static final int START_CHROME = 0x2;
44 private static final int START_WEBAPK = 0x4; 45 private static final int START_WEBAPK = 0x4;
45 private static final int START_FILE = 0x8; 46 private static final int START_FILE = 0x8;
46 private static final int START_OTHER_ACTIVITY = 0x10; 47 private static final int START_OTHER_ACTIVITY = 0x10;
47 private static final int INTENT_SANITIZATION_EXCEPTION = 0x20; 48 private static final int INTENT_SANITIZATION_EXCEPTION = 0x20;
49 private static final int PROXY_FOR_INSTANT_APPS = 0x40;
48 50
49 private static final String SEARCH_RESULT_URL_FOR_TOM_HANKS = 51 private static final String SEARCH_RESULT_URL_FOR_TOM_HANKS =
50 "https://www.google.com/search?q=tom+hanks"; 52 "https://www.google.com/search?q=tom+hanks";
51 private static final String IMDB_WEBPAGE_FOR_TOM_HANKS = "http://m.imdb.com/ name/nm0000158"; 53 private static final String IMDB_WEBPAGE_FOR_TOM_HANKS = "http://m.imdb.com/ name/nm0000158";
52 private static final String INTENT_URL_WITH_FALLBACK_URL = 54 private static final String INTENT_URL_WITH_FALLBACK_URL =
53 "intent:///name/nm0000158#Intent;scheme=imdb;package=com.imdb.mobile ;" 55 "intent:///name/nm0000158#Intent;scheme=imdb;package=com.imdb.mobile ;"
54 + "S." + ExternalNavigationHandler.EXTRA_BROWSER_FALLBACK_URL + "=" 56 + "S." + ExternalNavigationHandler.EXTRA_BROWSER_FALLBACK_URL + "="
55 + Uri.encode(IMDB_WEBPAGE_FOR_TOM_HANKS) + ";end"; 57 + Uri.encode(IMDB_WEBPAGE_FOR_TOM_HANKS) + ";end";
56 private static final String INTENT_URL_WITH_FALLBACK_URL_WITHOUT_PACKAGE_NAM E = 58 private static final String INTENT_URL_WITH_FALLBACK_URL_WITHOUT_PACKAGE_NAM E =
57 "intent:///name/nm0000158#Intent;scheme=imdb;" 59 "intent:///name/nm0000158#Intent;scheme=imdb;"
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 535
534 mDelegate.setCanHandleWithInstantApp(true); 536 mDelegate.setCanHandleWithInstantApp(true);
535 checkUrl("http://instantappenabled.com") 537 checkUrl("http://instantappenabled.com")
536 .withPageTransition(transTypeLinkFromIntent) 538 .withPageTransition(transTypeLinkFromIntent)
537 .withIsRedirect(true) 539 .withIsRedirect(true)
538 .withRedirectHandler(redirectHandler) 540 .withRedirectHandler(redirectHandler)
539 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T, IGNORE); 541 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T, IGNORE);
540 } 542 }
541 543
542 @SmallTest 544 @SmallTest
545 public void testInstantAppsIntent_incomingIntentRedirect() throws Exception {
546 TestContext context = new TestContext();
547 int transTypeLinkFromIntent = PageTransition.LINK
548 | PageTransition.FROM_API;
549 TabRedirectHandler redirectHandler = new TabRedirectHandler(context);
550 Intent fooIntent = Intent.parseUri("http://instantappenabled.com",
551 Intent.URI_INTENT_SCHEME);
552 redirectHandler.updateIntent(fooIntent);
553 redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, false, fals e, 0, 0);
554 redirectHandler.updateNewUrlLoading(transTypeLinkFromIntent, true, false , 0, 0);
555
556 mDelegate.setCanHandleWithInstantApp(true);
557 checkUrl("http://goo.gl/1234")
558 .withPageTransition(transTypeLinkFromIntent)
559 .withIsRedirect(true)
560 .withRedirectHandler(redirectHandler)
561 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T, IGNORE);
562
563 // URL that cannot be handled with instant apps should stay in Chrome.
564 mDelegate.setCanHandleWithInstantApp(false);
565 checkUrl("http://goo.gl/1234")
566 .withPageTransition(transTypeLinkFromIntent)
567 .withIsRedirect(true)
568 .withRedirectHandler(redirectHandler)
569 .expecting(OverrideUrlLoadingResult.NO_OVERRIDE, IGNORE);
570 }
571
572 @SmallTest
573 public void testInstantAppsIntent_handleNavigation() {
574 mDelegate.setCanHandleWithInstantApp(false);
575 checkUrl("http://maybeinstantapp.com")
576 .withPageTransition(PageTransition.LINK)
577 .expecting(OverrideUrlLoadingResult.NO_OVERRIDE, IGNORE);
578
579 mDelegate.setCanHandleWithInstantApp(true);
580 checkUrl("http://maybeinstantapp.com")
581 .withPageTransition(PageTransition.LINK)
582 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T, IGNORE);
583 }
584
585 @SmallTest
586 public void testInstantAppsIntent_serpReferrer() {
587 String intentUrl = "intent://buzzfeed.com/tasty#Intent;scheme=http;"
588 + "package=com.google.android.instantapps.supervisor;"
589 + "action=com.google.android.instantapps.START;"
590 + "S.com.google.android.instantapps.FALLBACK_PACKAGE="
591 + "com.android.chrome;S.com.google.android.instantapps.INSTANT_A PP_PACKAGE="
592 + "com.yelp.android;S.android.intent.extra.REFERRER_NAME="
593 + "https%3A%2F%2Fwww.google.com;end";
594 mDelegate.setIsSerpReferrer(true);
595 checkUrl(intentUrl)
596 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T,
597 START_OTHER_ACTIVITY | PROXY_FOR_INSTANT_APPS);
598 assertTrue(mDelegate.startActivityIntent.hasExtra(
599 InstantAppsHandler.IS_GOOGLE_SEARCH_REFERRER));
600
601 // Check that we block all instant app intent:// URLs not from SERP
602 mDelegate.setIsSerpReferrer(false);
603 checkUrl(intentUrl)
604 .expecting(OverrideUrlLoadingResult.NO_OVERRIDE, IGNORE);
605
606 // Check that IS_GOOGLE_SEARCH_REFERRER param is stripped on non-supervi sor intents.
607 mDelegate.setIsSerpReferrer(true);
608 String nonSupervisor = "intent://buzzfeed.com/tasty#Intent;scheme=http;"
609 + "package=com.imdb;action=com.google.VIEW;"
610 + "S.com.google.android.gms.instantapps.IS_GOOGLE_SEARCH_REFERRE R="
611 + "true;S.android.intent.extra.REFERRER_NAME="
612 + "https%3A%2F%2Fwww.google.com;end";
613 checkUrl(nonSupervisor)
614 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T,
615 START_OTHER_ACTIVITY);
616 assertFalse(mDelegate.startActivityIntent.hasExtra(
617 InstantAppsHandler.IS_GOOGLE_SEARCH_REFERRER));
618 }
619
620 @SmallTest
543 public void testFallbackUrl_IntentResolutionSucceeds() { 621 public void testFallbackUrl_IntentResolutionSucceeds() {
544 // IMDB app is installed. 622 // IMDB app is installed.
545 mDelegate.setCanResolveActivity(true); 623 mDelegate.setCanResolveActivity(true);
546 624
547 checkUrl(INTENT_URL_WITH_FALLBACK_URL) 625 checkUrl(INTENT_URL_WITH_FALLBACK_URL)
548 .withReferrer(SEARCH_RESULT_URL_FOR_TOM_HANKS) 626 .withReferrer(SEARCH_RESULT_URL_FOR_TOM_HANKS)
549 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T, 627 .expecting(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTEN T,
550 START_OTHER_ACTIVITY); 628 START_OTHER_ACTIVITY);
551 629
552 Intent invokedIntent = mDelegate.startActivityIntent; 630 Intent invokedIntent = mDelegate.startActivityIntent;
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 @Override 1223 @Override
1146 public void startActivity(Intent intent, boolean proxy) { 1224 public void startActivity(Intent intent, boolean proxy) {
1147 startActivityIntent = intent; 1225 startActivityIntent = intent;
1148 } 1226 }
1149 1227
1150 @Override 1228 @Override
1151 public boolean startActivityIfNeeded(Intent intent, boolean proxy) { 1229 public boolean startActivityIfNeeded(Intent intent, boolean proxy) {
1152 // For simplicity, don't distinguish between startActivityIfNeeded a nd startActivity 1230 // For simplicity, don't distinguish between startActivityIfNeeded a nd startActivity
1153 // until a test requires this distinction. 1231 // until a test requires this distinction.
1154 startActivityIntent = intent; 1232 startActivityIntent = intent;
1233 mCalledWithProxy = proxy;
1155 return true; 1234 return true;
1156 } 1235 }
1157 1236
1158 @Override 1237 @Override
1159 public void startIncognitoIntent(Intent intent, String referrerUrl, Stri ng fallbackUrl, 1238 public void startIncognitoIntent(Intent intent, String referrerUrl, Stri ng fallbackUrl,
1160 Tab tab, boolean needsToCloseTab, boolean proxy) { 1239 Tab tab, boolean needsToCloseTab, boolean proxy) {
1161 startIncognitoIntentCalled = true; 1240 startIncognitoIntentCalled = true;
1162 } 1241 }
1163 1242
1164 @Override 1243 @Override
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1204 } 1283 }
1205 1284
1206 @Override 1285 @Override
1207 public boolean maybeLaunchInstantApp(Tab tab, String url, String referre rUrl, 1286 public boolean maybeLaunchInstantApp(Tab tab, String url, String referre rUrl,
1208 boolean isIncomingRedirect) { 1287 boolean isIncomingRedirect) {
1209 return mCanHandleWithInstantApp; 1288 return mCanHandleWithInstantApp;
1210 } 1289 }
1211 1290
1212 @Override 1291 @Override
1213 public boolean isSerpReferrer(Tab tab) { 1292 public boolean isSerpReferrer(Tab tab) {
1214 return false; 1293 return mIsSerpReferrer;
1215 } 1294 }
1216 1295
1217 public void reset() { 1296 public void reset() {
1218 startActivityIntent = null; 1297 startActivityIntent = null;
1219 startIncognitoIntentCalled = false; 1298 startIncognitoIntentCalled = false;
1220 startFileIntentCalled = false; 1299 startFileIntentCalled = false;
1300 mCalledWithProxy = false;
1221 } 1301 }
1222 1302
1223 public void setCanResolveActivity(boolean value) { 1303 public void setCanResolveActivity(boolean value) {
1224 mQueryIntentOverride = value; 1304 mQueryIntentOverride = value;
1225 } 1305 }
1226 1306
1227 public String getNewUrlAfterClobbering() { 1307 public String getNewUrlAfterClobbering() {
1228 return mNewUrlAfterClobbering; 1308 return mNewUrlAfterClobbering;
1229 } 1309 }
1230 1310
1231 public String getReferrerUrlForClobbering() { 1311 public String getReferrerUrlForClobbering() {
1232 return mReferrerUrlForClobbering; 1312 return mReferrerUrlForClobbering;
1233 } 1313 }
1234 1314
1235 public void setIsChromeAppInForeground(boolean value) { 1315 public void setIsChromeAppInForeground(boolean value) {
1236 mIsChromeAppInForeground = value; 1316 mIsChromeAppInForeground = value;
1237 } 1317 }
1238 1318
1239 public void setIsWithinCurrentWebappScope(boolean value) { 1319 public void setIsWithinCurrentWebappScope(boolean value) {
1240 mIsWithinCurrentWebappScope = value; 1320 mIsWithinCurrentWebappScope = value;
1241 } 1321 }
1242 1322
1243 public void setCanHandleWithInstantApp(boolean value) { 1323 public void setCanHandleWithInstantApp(boolean value) {
1244 mCanHandleWithInstantApp = value; 1324 mCanHandleWithInstantApp = value;
1245 } 1325 }
1246 1326
1327 public void setIsSerpReferrer(boolean value) {
1328 mIsSerpReferrer = value;
1329 }
1330
1247 public Intent startActivityIntent; 1331 public Intent startActivityIntent;
1248 public boolean startIncognitoIntentCalled; 1332 public boolean startIncognitoIntentCalled;
1249 1333
1250 // This should not be reset for every run of check(). 1334 // This should not be reset for every run of check().
1251 private Boolean mQueryIntentOverride; 1335 private Boolean mQueryIntentOverride;
1252 1336
1253 private String mNewUrlAfterClobbering; 1337 private String mNewUrlAfterClobbering;
1254 private String mReferrerUrlForClobbering; 1338 private String mReferrerUrlForClobbering;
1255 private boolean mCanHandleWithInstantApp; 1339 private boolean mCanHandleWithInstantApp;
1340 private boolean mIsSerpReferrer;
1341 public boolean mCalledWithProxy;
1256 public boolean mIsChromeAppInForeground = true; 1342 public boolean mIsChromeAppInForeground = true;
1257 public boolean mIsWithinCurrentWebappScope; 1343 public boolean mIsWithinCurrentWebappScope;
1258 1344
1259 public boolean shouldRequestFileAccess; 1345 public boolean shouldRequestFileAccess;
1260 public boolean startFileIntentCalled; 1346 public boolean startFileIntentCalled;
1261 public String defaultSmsPackageName; 1347 public String defaultSmsPackageName;
1262 } 1348 }
1263 1349
1264 private void checkIntentSanity(Intent intent, String name) { 1350 private void checkIntentSanity(Intent intent, String name) {
1265 assertTrue("The invoked " + name + " doesn't have the BROWSABLE category set\n", 1351 assertTrue("The invoked " + name + " doesn't have the BROWSABLE category set\n",
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1335 int otherExpectation) { 1421 int otherExpectation) {
1336 boolean expectStartIncognito = (otherExpectation & START_INCOGNITO) != 0; 1422 boolean expectStartIncognito = (otherExpectation & START_INCOGNITO) != 0;
1337 boolean expectStartActivity = 1423 boolean expectStartActivity =
1338 (otherExpectation & (START_CHROME | START_WEBAPK | START_OTH ER_ACTIVITY)) != 0; 1424 (otherExpectation & (START_CHROME | START_WEBAPK | START_OTH ER_ACTIVITY)) != 0;
1339 boolean expectStartChrome = (otherExpectation & START_CHROME) != 0; 1425 boolean expectStartChrome = (otherExpectation & START_CHROME) != 0;
1340 boolean expectStartWebApk = (otherExpectation & START_WEBAPK) != 0; 1426 boolean expectStartWebApk = (otherExpectation & START_WEBAPK) != 0;
1341 boolean expectStartOtherActivity = (otherExpectation & START_OTHER_A CTIVITY) != 0; 1427 boolean expectStartOtherActivity = (otherExpectation & START_OTHER_A CTIVITY) != 0;
1342 boolean expectStartFile = (otherExpectation & START_FILE) != 0; 1428 boolean expectStartFile = (otherExpectation & START_FILE) != 0;
1343 boolean expectSaneIntent = expectStartOtherActivity 1429 boolean expectSaneIntent = expectStartOtherActivity
1344 && (otherExpectation & INTENT_SANITIZATION_EXCEPTION) == 0; 1430 && (otherExpectation & INTENT_SANITIZATION_EXCEPTION) == 0;
1431 boolean expectProxyForIA = (otherExpectation & PROXY_FOR_INSTANT_APP S) != 0;
1345 1432
1346 mDelegate.reset(); 1433 mDelegate.reset();
1347 1434
1348 ExternalNavigationParams params = new ExternalNavigationParams.Build er( 1435 ExternalNavigationParams params = new ExternalNavigationParams.Build er(
1349 mUrl, mIsIncognito, mReferrerUrl, 1436 mUrl, mIsIncognito, mReferrerUrl,
1350 mPageTransition, mIsRedirect) 1437 mPageTransition, mIsRedirect)
1351 .setApplicationMustBeInForeground(mChromeAppInForegroundRequ ired) 1438 .setApplicationMustBeInForeground(mChromeAppInForegroundRequ ired)
1352 .setRedirectHandler(mRedirectHandler) 1439 .setRedirectHandler(mRedirectHandler)
1353 .setIsBackgroundTabNavigation(mIsBackgroundTabNavigation) 1440 .setIsBackgroundTabNavigation(mIsBackgroundTabNavigation)
1354 .setIsMainFrame(true) 1441 .setIsMainFrame(true)
(...skipping 13 matching lines...) Expand all
1368 packageName.startsWith(WebApkConstants.WEBAPK_PACKAG E_PREFIX); 1455 packageName.startsWith(WebApkConstants.WEBAPK_PACKAG E_PREFIX);
1369 } 1456 }
1370 } 1457 }
1371 1458
1372 assertEquals(expectedOverrideResult, result); 1459 assertEquals(expectedOverrideResult, result);
1373 assertEquals(expectStartIncognito, mDelegate.startIncognitoIntentCal led); 1460 assertEquals(expectStartIncognito, mDelegate.startIncognitoIntentCal led);
1374 assertEquals(expectStartActivity, startActivityCalled); 1461 assertEquals(expectStartActivity, startActivityCalled);
1375 assertEquals(expectStartChrome, startChromeCalled); 1462 assertEquals(expectStartChrome, startChromeCalled);
1376 assertEquals(expectStartWebApk, startWebApkCalled); 1463 assertEquals(expectStartWebApk, startWebApkCalled);
1377 assertEquals(expectStartFile, mDelegate.startFileIntentCalled); 1464 assertEquals(expectStartFile, mDelegate.startFileIntentCalled);
1465 assertEquals(expectProxyForIA, mDelegate.mCalledWithProxy);
1378 1466
1379 if (startActivityCalled && expectSaneIntent) { 1467 if (startActivityCalled && expectSaneIntent) {
1380 checkIntentSanity(mDelegate.startActivityIntent, "Intent"); 1468 checkIntentSanity(mDelegate.startActivityIntent, "Intent");
1381 if (mDelegate.startActivityIntent.getSelector() != null) { 1469 if (mDelegate.startActivityIntent.getSelector() != null) {
1382 checkIntentSanity(mDelegate.startActivityIntent.getSelector( ), 1470 checkIntentSanity(mDelegate.startActivityIntent.getSelector( ),
1383 "Intent's selector"); 1471 "Intent's selector");
1384 } 1472 }
1385 } 1473 }
1386 } 1474 }
1387 } 1475 }
(...skipping 20 matching lines...) Expand all
1408 return new TestPackageManager(); 1496 return new TestPackageManager();
1409 } 1497 }
1410 1498
1411 @Override 1499 @Override
1412 public String getPackageName() { 1500 public String getPackageName() {
1413 return "test.app.name"; 1501 return "test.app.name";
1414 } 1502 }
1415 1503
1416 } 1504 }
1417 } 1505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698