Commit efb32a06 authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android Webapp] Make WebappNavigationTest not use live web page

This CL:
- Plumbs the version of EmbeddedTestServer::GetURL() which takes a host name to
  Java
- Switches WebappNavigationTests from navigating to live www.google.com to
  navigating to a page hosted on the test server.

Cq-Include-Trybots: master.tryserver.chromium.android:android_cronet_tester;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I28bf48914d77acd7033ee858fc7794da4875bc6d
Reviewed-on: https://chromium-review.googlesource.com/771539Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarEmily Stark <estark@chromium.org>
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517744}
parent 3d3468d1
...@@ -36,3 +36,9 @@ include_rules = [ ...@@ -36,3 +36,9 @@ include_rules = [
"+services/service_manager/public/java", "+services/service_manager/public/java",
] ]
specific_include_rules = {
"WebappNavigationTest\.java": [
"+content/public/android/java/src/org/chromium/content/common/ContentSwitches.java",
]
}
...@@ -52,7 +52,7 @@ public class WebappActionsNotificationTest { ...@@ -52,7 +52,7 @@ public class WebappActionsNotificationTest {
@Before @Before
public void startWebapp() throws Exception { public void startWebapp() throws Exception {
mActivityTestRule.startWebappActivity(mActivityTestRule.createIntent().putExtra( mActivityTestRule.startWebappActivity(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_URL, mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH))); ShortcutHelper.EXTRA_URL, mActivityTestRule.getTestServer().getURL(WEB_APP_PATH)));
mActivityTestRule.waitUntilSplashscreenHides(); mActivityTestRule.waitUntilSplashscreenHides();
} }
...@@ -105,7 +105,7 @@ public class WebappActionsNotificationTest { ...@@ -105,7 +105,7 @@ public class WebappActionsNotificationTest {
ClipboardManager clipboard = ClipboardManager clipboard =
(ClipboardManager) mActivityTestRule.getActivity().getSystemService( (ClipboardManager) mActivityTestRule.getActivity().getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
Assert.assertEquals(mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH), Assert.assertEquals(mActivityTestRule.getTestServer().getURL(WEB_APP_PATH),
clipboard.getPrimaryClip().getItemAt(0).getText().toString()); clipboard.getPrimaryClip().getItemAt(0).getText().toString());
}); });
} }
......
...@@ -22,6 +22,7 @@ import org.chromium.chrome.browser.ShortcutHelper; ...@@ -22,6 +22,7 @@ import org.chromium.chrome.browser.ShortcutHelper;
import org.chromium.chrome.test.ChromeActivityTestRule; import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.EmbeddedTestServerRule; import org.chromium.net.test.EmbeddedTestServerRule;
/** /**
...@@ -74,8 +75,8 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit ...@@ -74,8 +75,8 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
super(WebappActivity0.class); super(WebappActivity0.class);
} }
public String getUrlFromTestServer(String relativeUrl) { public EmbeddedTestServer getTestServer() {
return mTestServerRule.getServer().getURL(relativeUrl); return mTestServerRule.getServer();
} }
/** /**
...@@ -169,7 +170,7 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit ...@@ -169,7 +170,7 @@ public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
throws Exception { throws Exception {
// Reset the url to one that takes more time to load. // Reset the url to one that takes more time to load.
// This is to make sure splash screen won't disappear during test. // This is to make sure splash screen won't disappear during test.
intent.putExtra(ShortcutHelper.EXTRA_URL, getUrlFromTestServer("/slow?2")); intent.putExtra(ShortcutHelper.EXTRA_URL, getTestServer().getURL("/slow?2"));
launchActivity(intent); launchActivity(intent);
InstrumentationRegistry.getInstrumentation().waitForIdleSync(); InstrumentationRegistry.getInstrumentation().waitForIdleSync();
CriteriaHelper.pollInstrumentationThread(new Criteria() { CriteriaHelper.pollInstrumentationThread(new Criteria() {
......
...@@ -70,7 +70,7 @@ public class WebappDisplayModeTest { ...@@ -70,7 +70,7 @@ public class WebappDisplayModeTest {
Assert.assertEquals("Web App title should be displayed on the title bar", Assert.assertEquals("Web App title should be displayed on the title bar",
WEB_APP_PAGE_TITLE, ((TextView) activity.findViewById(R.id.title_bar)).getText()); WEB_APP_PAGE_TITLE, ((TextView) activity.findViewById(R.id.title_bar)).getText());
Assert.assertEquals("URL Bar should display URL authority", Assert.assertEquals("URL Bar should display URL authority",
Uri.parse(mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH)).getAuthority(), Uri.parse(mActivityTestRule.getTestServer().getURL(WEB_APP_PATH)).getAuthority(),
((UrlBar) activity.findViewById(R.id.url_bar)).getText().toString()); ((UrlBar) activity.findViewById(R.id.url_bar)).getText().toString());
Assert.assertEquals("CCT Close button should not be visible", View.GONE, Assert.assertEquals("CCT Close button should not be visible", View.GONE,
activity.findViewById(R.id.close_button).getVisibility()); activity.findViewById(R.id.close_button).getVisibility());
...@@ -80,7 +80,7 @@ public class WebappDisplayModeTest { ...@@ -80,7 +80,7 @@ public class WebappDisplayModeTest {
mActivityTestRule.startWebappActivity( mActivityTestRule.startWebappActivity(
mActivityTestRule.createIntent() mActivityTestRule.createIntent()
.putExtra(ShortcutHelper.EXTRA_URL, .putExtra(ShortcutHelper.EXTRA_URL,
mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH)) mActivityTestRule.getTestServer().getURL(WEB_APP_PATH))
.putExtra(ShortcutHelper.EXTRA_DISPLAY_MODE, displayMode) .putExtra(ShortcutHelper.EXTRA_DISPLAY_MODE, displayMode)
.putExtra(ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); .putExtra(ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
......
...@@ -36,6 +36,7 @@ import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid ...@@ -36,6 +36,7 @@ import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
import org.chromium.chrome.browser.firstrun.FirstRunStatus; import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl; import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.MenuUtils; import org.chromium.chrome.test.util.MenuUtils;
import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils;
import org.chromium.components.navigation_interception.NavigationParams; import org.chromium.components.navigation_interception.NavigationParams;
...@@ -43,6 +44,7 @@ import org.chromium.content.browser.test.NativeLibraryTestRule; ...@@ -43,6 +44,7 @@ import org.chromium.content.browser.test.NativeLibraryTestRule;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.DOMUtils; import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content.common.ContentSwitches;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.ui.base.PageTransition; import org.chromium.ui.base.PageTransition;
...@@ -50,11 +52,10 @@ import org.chromium.ui.base.PageTransition; ...@@ -50,11 +52,10 @@ import org.chromium.ui.base.PageTransition;
* Tests web navigations originating from a WebappActivity. * Tests web navigations originating from a WebappActivity.
*/ */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
ContentSwitches.HOST_RESOLVER_RULES + "=MAP * 127.0.0.1"})
public class WebappNavigationTest { public class WebappNavigationTest {
private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=EYmjoW4vIX8"; private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=EYmjoW4vIX8";
private static final String OFF_ORIGIN_URL = "https://www.google.com/";
private static final String OFF_ORIGIN_URL_NO_TLD = "https://www.google.";
private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html"; private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html";
private static final String IN_SCOPE_PAGE_PATH = private static final String IN_SCOPE_PAGE_PATH =
"/chrome/test/data/banners/manifest_no_service_worker.html"; "/chrome/test/data/banners/manifest_no_service_worker.html";
...@@ -77,7 +78,7 @@ public class WebappNavigationTest { ...@@ -77,7 +78,7 @@ public class WebappNavigationTest {
activity.getResources(), R.color.default_primary_color), activity.getResources(), R.color.default_primary_color),
activity.getToolbarManager().getPrimaryColor()); activity.getToolbarManager().getPrimaryColor());
addAnchor("testId", OFF_ORIGIN_URL, "_self"); addAnchor("testId", offOriginUrl(), "_self");
DOMUtils.clickNode(activity.getActivityTab().getContentViewCore(), "testId"); DOMUtils.clickNode(activity.getActivityTab().getContentViewCore(), "testId");
mActivityTestRule.waitUntilIdle(); mActivityTestRule.waitUntilIdle();
...@@ -96,7 +97,7 @@ public class WebappNavigationTest { ...@@ -96,7 +97,7 @@ public class WebappNavigationTest {
activity.getToolbarManager().getPrimaryColor()); activity.getToolbarManager().getPrimaryColor());
mActivityTestRule.runJavaScriptCodeInCurrentTab( mActivityTestRule.runJavaScriptCodeInCurrentTab(
String.format("window.top.location = '%s'", OFF_ORIGIN_URL)); String.format("window.top.location = '%s'", offOriginUrl()));
mActivityTestRule.waitUntilIdle(); mActivityTestRule.waitUntilIdle();
assertOffOrigin(activity); assertOffOrigin(activity);
...@@ -109,7 +110,7 @@ public class WebappNavigationTest { ...@@ -109,7 +110,7 @@ public class WebappNavigationTest {
public void testOffScopeNewTabLinkOpensInCct() throws Exception { public void testOffScopeNewTabLinkOpensInCct() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra( runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
addAnchor("testId", OFF_ORIGIN_URL, "_blank"); addAnchor("testId", offOriginUrl(), "_blank");
DOMUtils.clickNode( DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl(); CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl();
...@@ -127,7 +128,7 @@ public class WebappNavigationTest { ...@@ -127,7 +128,7 @@ public class WebappNavigationTest {
public void testInScopeNewTabLinkOpensInCct() throws Exception { public void testInScopeNewTabLinkOpensInCct() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra( runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
addAnchor("testId", mActivityTestRule.getUrlFromTestServer(IN_SCOPE_PAGE_PATH), "_blank"); addAnchor("testId", mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH), "_blank");
DOMUtils.clickNode( DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
CustomTabActivity customTab = waitFor(CustomTabActivity.class); CustomTabActivity customTab = waitFor(CustomTabActivity.class);
...@@ -154,7 +155,7 @@ public class WebappNavigationTest { ...@@ -154,7 +155,7 @@ public class WebappNavigationTest {
+ " return false;" + " return false;"
+ "};" + "};"
+ "document.body.appendChild(aTag);", + "document.body.appendChild(aTag);",
OFF_ORIGIN_URL)); offOriginUrl()));
DOMUtils.clickNode( DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
...@@ -172,7 +173,7 @@ public class WebappNavigationTest { ...@@ -172,7 +173,7 @@ public class WebappNavigationTest {
public void testInScopeNavigationStaysInWebapp() throws Exception { public void testInScopeNavigationStaysInWebapp() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
String otherPageUrl = mActivityTestRule.getUrlFromTestServer(IN_SCOPE_PAGE_PATH); String otherPageUrl = mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH);
mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK,
mActivityTestRule.getActivity().getActivityTab()); mActivityTestRule.getActivity().getActivityTab());
...@@ -193,7 +194,7 @@ public class WebappNavigationTest { ...@@ -193,7 +194,7 @@ public class WebappNavigationTest {
FirstRunStatus.setFirstRunFlowComplete(true); FirstRunStatus.setFirstRunFlowComplete(true);
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
addAnchor("myTestAnchorId", OFF_ORIGIN_URL, "_self"); addAnchor("myTestAnchorId", offOriginUrl(), "_self");
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
null /* activity to check for focus after click */, null /* activity to check for focus after click */,
...@@ -222,7 +223,7 @@ public class WebappNavigationTest { ...@@ -222,7 +223,7 @@ public class WebappNavigationTest {
mActivityTestRule.waitUntilIdle(tabbedChrome); mActivityTestRule.waitUntilIdle(tabbedChrome);
Assert.assertEquals("Tab in tabbed activity should show the Web App page", Assert.assertEquals("Tab in tabbed activity should show the Web App page",
mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH), mActivityTestRule.getTestServer().getURL(WEB_APP_PATH),
tabbedChrome.getActivityTab().getUrl()); tabbedChrome.getActivityTab().getUrl());
} }
...@@ -277,7 +278,7 @@ public class WebappNavigationTest { ...@@ -277,7 +278,7 @@ public class WebappNavigationTest {
mActivityTestRule.waitUntilIdle(tabbedChrome); mActivityTestRule.waitUntilIdle(tabbedChrome);
ThreadUtils.runOnUiThreadBlocking( ThreadUtils.runOnUiThreadBlocking(
() -> tabbedChrome.getActivityTab().loadUrl(new LoadUrlParams(OFF_ORIGIN_URL))); () -> tabbedChrome.getActivityTab().loadUrl(new LoadUrlParams(offOriginUrl())));
mActivityTestRule.waitUntilIdle(tabbedChrome); mActivityTestRule.waitUntilIdle(tabbedChrome);
assertOffOrigin(tabbedChrome); assertOffOrigin(tabbedChrome);
...@@ -290,13 +291,13 @@ public class WebappNavigationTest { ...@@ -290,13 +291,13 @@ public class WebappNavigationTest {
public void testCloseButtonReturnsToMostRecentInScopeUrl() throws Exception { public void testCloseButtonReturnsToMostRecentInScopeUrl() throws Exception {
WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
String otherInScopeUrl = mActivityTestRule.getUrlFromTestServer(IN_SCOPE_PAGE_PATH); String otherInScopeUrl = mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH);
mActivityTestRule.loadUrlInTab( mActivityTestRule.loadUrlInTab(
otherInScopeUrl, PageTransition.LINK, activity.getActivityTab()); otherInScopeUrl, PageTransition.LINK, activity.getActivityTab());
Assert.assertEquals(otherInScopeUrl, activity.getActivityTab().getUrl()); Assert.assertEquals(otherInScopeUrl, activity.getActivityTab().getUrl());
ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl( ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl(
new LoadUrlParams(OFF_ORIGIN_URL, PageTransition.LINK))); new LoadUrlParams(offOriginUrl(), PageTransition.LINK)));
assertOffOrigin(activity); assertOffOrigin(activity);
ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl( ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl(
new LoadUrlParams("https://www.mozilla.org/", PageTransition.LINK))); new LoadUrlParams("https://www.mozilla.org/", PageTransition.LINK)));
...@@ -346,13 +347,14 @@ public class WebappNavigationTest { ...@@ -346,13 +347,14 @@ public class WebappNavigationTest {
private WebappActivity runWebappActivityAndWaitForIdle(Intent intent) throws Exception { private WebappActivity runWebappActivityAndWaitForIdle(Intent intent) throws Exception {
mActivityTestRule.startWebappActivity(intent.putExtra( mActivityTestRule.startWebappActivity(intent.putExtra(
ShortcutHelper.EXTRA_URL, mActivityTestRule.getUrlFromTestServer(WEB_APP_PATH))); ShortcutHelper.EXTRA_URL, mActivityTestRule.getTestServer().getURL(WEB_APP_PATH)));
mActivityTestRule.waitUntilSplashscreenHides(); mActivityTestRule.waitUntilSplashscreenHides();
mActivityTestRule.waitUntilIdle(); mActivityTestRule.waitUntilIdle();
return mActivityTestRule.getActivity(); return mActivityTestRule.getActivity();
} }
private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl()
throws InterruptedException {
CustomTabActivity customTab = waitFor(CustomTabActivity.class); CustomTabActivity customTab = waitFor(CustomTabActivity.class);
mActivityTestRule.waitUntilIdle(customTab); mActivityTestRule.waitUntilIdle(customTab);
...@@ -361,14 +363,12 @@ public class WebappNavigationTest { ...@@ -361,14 +363,12 @@ public class WebappNavigationTest {
return customTab; return customTab;
} }
private void assertOffOrigin(ChromeActivity activity) { private String offOriginUrl() {
// Dropping the TLD as Google can redirect to a local site, so this could fail outside US. return mActivityTestRule.getTestServer().getURLWithHostName("foo.com", "/defaultresponse");
CriteriaHelper.pollUiThread(new Criteria() { }
@Override
public boolean isSatisfied() { private void assertOffOrigin(ChromeActivity activity) throws InterruptedException {
return activity.getActivityTab().getUrl().contains(OFF_ORIGIN_URL_NO_TLD); ChromeTabUtils.waitForTabPageLoaded(activity.getActivityTab(), offOriginUrl());
}
});
} }
private void addAnchor(String id, String url, String target) throws Exception { private void addAnchor(String id, String url, String target) throws Exception {
......
...@@ -421,6 +421,25 @@ public class EmbeddedTestServer { ...@@ -421,6 +421,25 @@ public class EmbeddedTestServer {
} }
} }
/** Get the full URL for the given relative URL. Similar to the above method but uses the given
* hostname instead of 127.0.0.1. The hostname should be resolved to 127.0.0.1.
*
* @param hostName The host name which should be used.
* @param relativeUrl The relative URL for which a full URL should be returned.
* @return The URL as a String.
*/
public String getURLWithHostName(String hostname, String relativeUrl) {
try {
synchronized (mImplMonitor) {
checkServiceLocked();
return mImpl.getURLWithHostName(hostname, relativeUrl);
}
} catch (RemoteException e) {
throw new EmbeddedTestServerFailure(
"Failed to get URL for " + hostname + " and " + relativeUrl, e);
}
}
/** Get the full URLs for the given relative URLs. /** Get the full URLs for the given relative URLs.