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

Clarify WebappNavigationTests

This CL:
- Clarifies which tests test that a CCT is launched and which do not
- Adds testing that the toolbar is hidden/shown in test cases where the toolbar
  should be shown instead of launching a CCT.
- Removes deceptively named assertOffOrigin() which does not assert but instead
  waits for a condition to be met. Instead:
- Changes WebappNavigationTest#waitFor() to wait till the tab for an activity has
  been created
- Uses ChromeTabUtils#waitForTabPageLoaded()

BUG=None

Change-Id: If588dcc4ae2a9341221eb64ac7184bb5b75efd33
Reviewed-on: https://chromium-review.googlesource.com/777903
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517823}
parent a3c44079
...@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.customtabs.CustomTabActivity; ...@@ -35,6 +35,7 @@ import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.OverrideUrlLoadingResult; import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.OverrideUrlLoadingResult;
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.browser.tab.Tab;
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.ChromeTabUtils;
import org.chromium.chrome.test.util.MenuUtils; import org.chromium.chrome.test.util.MenuUtils;
...@@ -66,43 +67,79 @@ public class WebappNavigationTest { ...@@ -66,43 +67,79 @@ public class WebappNavigationTest {
@Rule @Rule
public final NativeLibraryTestRule mNativeLibraryTestRule = new NativeLibraryTestRule(); public final NativeLibraryTestRule mNativeLibraryTestRule = new NativeLibraryTestRule();
/**
* Test that navigating a webapp whose launch intent does not specify a theme colour outside of
* the webapp scope by tapping a regular link:
* - Shows a CCT-like webapp toolbar.
* - Uses the default theme colour as the toolbar colour.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
@RetryOnFailure @RetryOnFailure
public void testRegularLinkOffOriginInCctNoWebappThemeColor() throws Exception { public void testRegularLinkOffOriginNoWebappThemeColor() throws Exception {
WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
assertToolbarShowState(activity, false);
addAnchorAndClick(offOriginUrl(), "_self");
ChromeTabUtils.waitForTabPageLoaded(activity.getActivityTab(), offOriginUrl());
assertToolbarShowState(activity, true);
Assert.assertEquals( Assert.assertEquals(
"Toolbar should use default primary color if theme color is not specified", getDefaultPrimaryColor(), activity.getToolbarManager().getPrimaryColor());
ApiCompatibilityUtils.getColor( }
activity.getResources(), R.color.default_primary_color),
activity.getToolbarManager().getPrimaryColor());
addAnchor("testId", offOriginUrl(), "_self"); /**
DOMUtils.clickNode(activity.getActivityTab().getContentViewCore(), "testId"); * Test that navigating a webapp whose launch intent specifies a theme colour outside of the
mActivityTestRule.waitUntilIdle(); * webapp scope by tapping a regular link:
* - Shows a CCT-like webapp toolbar.
* - Uses the webapp theme colour as the toolbar colour.
*/
@Test
@SmallTest
@Feature({"Webapps"})
@RetryOnFailure
public void testRegularLinkOffOriginThemeColor() throws Exception {
WebappActivity activity =
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
assertToolbarShowState(activity, false);
addAnchorAndClick(offOriginUrl(), "_self");
assertOffOrigin(activity); ChromeTabUtils.waitForTabPageLoaded(activity.getActivityTab(), offOriginUrl());
assertToolbarShowState(activity, true);
Assert.assertEquals(Color.CYAN, activity.getToolbarManager().getPrimaryColor());
} }
/**
* Test that navigating outside of the webapp scope by changing the top location via JavaScript:
* - Shows a CCT-like webapp toolbar.
* - Preserves the theme color specified in the launch intent.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
@RetryOnFailure @RetryOnFailure
public void testWindowTopLocationOffOriginInCctAndWebappThemeColor() throws Exception { public void testWindowTopLocationOffOrigin() throws Exception {
WebappActivity activity = WebappActivity activity =
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra( runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
Assert.assertEquals("Toolbar should use the theme color of a webapp", Color.CYAN, assertToolbarShowState(activity, false);
activity.getToolbarManager().getPrimaryColor());
mActivityTestRule.runJavaScriptCodeInCurrentTab( mActivityTestRule.runJavaScriptCodeInCurrentTab(
String.format("window.top.location = '%s'", offOriginUrl())); String.format("window.top.location = '%s'", offOriginUrl()));
mActivityTestRule.waitUntilIdle();
assertOffOrigin(activity); ChromeTabUtils.waitForTabPageLoaded(activity.getActivityTab(), offOriginUrl());
assertToolbarShowState(activity, true);
Assert.assertEquals(Color.CYAN, activity.getToolbarManager().getPrimaryColor());
} }
/**
* Test that navigating outside of the webapp scope by tapping a link with target="_blank":
* - Launches a CCT.
* - The CCT toolbar does not use the webapp theme colour.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
...@@ -110,40 +147,46 @@ public class WebappNavigationTest { ...@@ -110,40 +147,46 @@ 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", offOriginUrl(), "_blank"); addAnchorAndClick(offOriginUrl(), "_blank");
DOMUtils.clickNode( CustomTabActivity customTab = waitFor(CustomTabActivity.class);
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); ChromeTabUtils.waitForTabPageLoaded(customTab.getActivityTab(), offOriginUrl());
CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl();
Assert.assertEquals( Assert.assertEquals(
"CCT Toolbar should use default primary color even if webapp has theme color", getDefaultPrimaryColor(), customTab.getToolbarManager().getPrimaryColor());
ApiCompatibilityUtils.getColor(
customTab.getResources(), R.color.default_primary_color),
customTab.getToolbarManager().getPrimaryColor());
} }
/**
* Test that navigating within the webapp scope by tapping a link with target="_blank" launches
* a CCT.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
@RetryOnFailure @RetryOnFailure
public void testInScopeNewTabLinkOpensInCct() throws Exception { public void testInScopeNewTabLinkOpensInCct() throws Exception {
String inScopeUrl = mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH);
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.getTestServer().getURL(IN_SCOPE_PAGE_PATH), "_blank"); addAnchorAndClick(inScopeUrl, "_blank");
DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
CustomTabActivity customTab = waitFor(CustomTabActivity.class); CustomTabActivity customTab = waitFor(CustomTabActivity.class);
mActivityTestRule.waitUntilIdle(customTab); ChromeTabUtils.waitForTabPageLoaded(customTab.getActivityTab(), inScopeUrl);
Assert.assertTrue( Assert.assertTrue(
mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.textContent") mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.textContent")
.contains("Do-nothing page with a service worker")); .contains("Do-nothing page with a service worker"));
} }
/**
* Test that navigating outside of the webapp via window.open():
* - Launches a CCT.
* - The CCT toolbar does not use the webapp theme colour.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
@RetryOnFailure @RetryOnFailure
public void testWindowOpenInCct() throws Exception { public void testWindowOpenInCct() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra(
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
// Executing window.open() through a click on a link, // Executing window.open() through a click on a link,
// as it needs user gesture to avoid Chrome blocking it as a popup. // as it needs user gesture to avoid Chrome blocking it as a popup.
mActivityTestRule.runJavaScriptCodeInCurrentTab( mActivityTestRule.runJavaScriptCodeInCurrentTab(
...@@ -159,30 +202,28 @@ public class WebappNavigationTest { ...@@ -159,30 +202,28 @@ public class WebappNavigationTest {
DOMUtils.clickNode( DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl(); CustomTabActivity customTab = waitFor(CustomTabActivity.class);
Assert.assertEquals("CCT Toolbar should use default primary color", ChromeTabUtils.waitForTabPageLoaded(customTab.getActivityTab(), offOriginUrl());
ApiCompatibilityUtils.getColor( Assert.assertEquals(
customTab.getResources(), R.color.default_primary_color), getDefaultPrimaryColor(), customTab.getToolbarManager().getPrimaryColor());
customTab.getToolbarManager().getPrimaryColor());
} }
/**
* Test that navigating a webapp within the webapp scope by tapping a regular link:
* - Does not show a CCT-like webapp toolbar.
* - Does not launch a CCT.
*/
@Test @Test
@SmallTest @SmallTest
@Feature({"Webapps"}) @Feature({"Webapps"})
@RetryOnFailure @RetryOnFailure
public void testInScopeNavigationStaysInWebapp() throws Exception { public void testInScopeNavigationStaysInWebapp() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
String otherPageUrl = mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH); String otherPageUrl = mActivityTestRule.getTestServer().getURL(IN_SCOPE_PAGE_PATH);
mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, addAnchorAndClick(otherPageUrl, "_self");
mActivityTestRule.getActivity().getActivityTab()); ChromeTabUtils.waitForTabPageLoaded(activity.getActivityTab(), otherPageUrl);
mActivityTestRule.waitUntilIdle(); assertToolbarShowState(activity, false);
Assert.assertEquals(
otherPageUrl, mActivityTestRule.getActivity().getActivityTab().getUrl());
Assert.assertSame(
mActivityTestRule.getActivity(), ApplicationStatus.getLastTrackedFocusedActivity());
} }
@Test @Test
...@@ -202,9 +243,7 @@ public class WebappNavigationTest { ...@@ -202,9 +243,7 @@ public class WebappNavigationTest {
R.id.contextmenu_open_in_chrome); R.id.contextmenu_open_in_chrome);
ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class); ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class);
ChromeTabUtils.waitForTabPageLoaded(tabbedChrome.getActivityTab(), offOriginUrl());
mActivityTestRule.waitUntilIdle(tabbedChrome);
assertOffOrigin(tabbedChrome);
} }
@Test @Test
...@@ -220,11 +259,8 @@ public class WebappNavigationTest { ...@@ -220,11 +259,8 @@ public class WebappNavigationTest {
InstrumentationRegistry.getInstrumentation(), activity, R.id.open_in_browser_id); InstrumentationRegistry.getInstrumentation(), activity, R.id.open_in_browser_id);
ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class); ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class);
mActivityTestRule.waitUntilIdle(tabbedChrome); ChromeTabUtils.waitForTabPageLoaded(tabbedChrome.getActivityTab(),
mActivityTestRule.getTestServer().getURL(WEB_APP_PATH));
Assert.assertEquals("Tab in tabbed activity should show the Web App page",
mActivityTestRule.getTestServer().getURL(WEB_APP_PATH),
tabbedChrome.getActivityTab().getUrl());
} }
@Test @Test
...@@ -237,9 +273,7 @@ public class WebappNavigationTest { ...@@ -237,9 +273,7 @@ public class WebappNavigationTest {
InterceptNavigationDelegateImpl navigationDelegate = InterceptNavigationDelegateImpl navigationDelegate =
mActivityTestRule.getActivity().getActivityTab().getInterceptNavigationDelegate(); mActivityTestRule.getActivity().getActivityTab().getInterceptNavigationDelegate();
addAnchor("testLink", YOUTUBE_URL, "_self"); addAnchorAndClick(YOUTUBE_URL, "_self");
DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testLink");
waitForExternalAppOrIntentPicker(); waitForExternalAppOrIntentPicker();
Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT, Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT,
...@@ -253,9 +287,7 @@ public class WebappNavigationTest { ...@@ -253,9 +287,7 @@ public class WebappNavigationTest {
public void testNewTabLinkToExternalApp() throws Exception { public void testNewTabLinkToExternalApp() throws Exception {
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
addAnchor("testLink", YOUTUBE_URL, "_blank"); addAnchorAndClick(YOUTUBE_URL, "_blank");
DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testLink");
waitForExternalAppOrIntentPicker(); waitForExternalAppOrIntentPicker();
} }
...@@ -275,13 +307,9 @@ public class WebappNavigationTest { ...@@ -275,13 +307,9 @@ public class WebappNavigationTest {
mActivityTestRule.getActivity(), R.id.open_in_browser_id); mActivityTestRule.getActivity(), R.id.open_in_browser_id);
ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class); ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class);
mActivityTestRule.waitUntilIdle(tabbedChrome);
ThreadUtils.runOnUiThreadBlocking( ThreadUtils.runOnUiThreadBlocking(
() -> tabbedChrome.getActivityTab().loadUrl(new LoadUrlParams(offOriginUrl()))); () -> tabbedChrome.getActivityTab().loadUrl(new LoadUrlParams(offOriginUrl())));
ChromeTabUtils.waitForTabPageLoaded(tabbedChrome.getActivityTab(), offOriginUrl());
mActivityTestRule.waitUntilIdle(tabbedChrome);
assertOffOrigin(tabbedChrome);
} }
@Test @Test
...@@ -290,30 +318,27 @@ public class WebappNavigationTest { ...@@ -290,30 +318,27 @@ public class WebappNavigationTest {
@RetryOnFailure @RetryOnFailure
public void testCloseButtonReturnsToMostRecentInScopeUrl() throws Exception { public void testCloseButtonReturnsToMostRecentInScopeUrl() throws Exception {
WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); WebappActivity activity = runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent());
Tab tab = activity.getActivityTab();
String otherInScopeUrl = mActivityTestRule.getTestServer().getURL(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, tab.getUrl());
ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl( mActivityTestRule.loadUrlInTab(offOriginUrl(), PageTransition.LINK, tab);
new LoadUrlParams(offOriginUrl(), PageTransition.LINK))); String mozillaUrl = mActivityTestRule.getTestServer().getURLWithHostName(
assertOffOrigin(activity); "mozilla.org", "/defaultresponse");
ThreadUtils.runOnUiThreadBlocking(() -> activity.getActivityTab().loadUrl( mActivityTestRule.loadUrlInTab(mozillaUrl, PageTransition.LINK, tab);
new LoadUrlParams("https://www.mozilla.org/", PageTransition.LINK)));
mActivityTestRule.waitUntilIdle(); // Toolbar with the close button should be visible.
assertToolbarShowState(activity, true);
// Navigate back to in-scope through a close button. // Navigate back to in-scope through a close button.
ThreadUtils.runOnUiThreadBlocking(() -> activity.getToolbarManager() ThreadUtils.runOnUiThreadBlocking(() -> activity.getToolbarManager()
.getToolbarLayout().findViewById(R.id.close_button).callOnClick()); .getToolbarLayout().findViewById(R.id.close_button).callOnClick());
// We should end up on most recent in-scope URL. // We should end up on most recent in-scope URL.
CriteriaHelper.pollUiThread(new Criteria() { ChromeTabUtils.waitForTabPageLoaded(tab, otherInScopeUrl);
@Override
public boolean isSatisfied() {
return otherInScopeUrl.equals(activity.getActivityTab().getUrl());
}
});
} }
@Test @Test
...@@ -353,24 +378,24 @@ public class WebappNavigationTest { ...@@ -353,24 +378,24 @@ public class WebappNavigationTest {
return mActivityTestRule.getActivity(); return mActivityTestRule.getActivity();
} }
private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() private void assertToolbarShowState(final ChromeActivity activity, final boolean showState) {
throws InterruptedException { ThreadUtils.runOnUiThreadBlocking(new Runnable() {
CustomTabActivity customTab = waitFor(CustomTabActivity.class); @Override
public void run() {
mActivityTestRule.waitUntilIdle(customTab); Assert.assertEquals(showState, activity.getActivityTab().canShowBrowserControls());
assertOffOrigin(customTab); }
});
}
return customTab; private long getDefaultPrimaryColor() {
return ApiCompatibilityUtils.getColor(
mActivityTestRule.getActivity().getResources(), R.color.default_primary_color);
} }
private String offOriginUrl() { private String offOriginUrl() {
return mActivityTestRule.getTestServer().getURLWithHostName("foo.com", "/defaultresponse"); return mActivityTestRule.getTestServer().getURLWithHostName("foo.com", "/defaultresponse");
} }
private void assertOffOrigin(ChromeActivity activity) throws InterruptedException {
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 {
mActivityTestRule.runJavaScriptCodeInCurrentTab( mActivityTestRule.runJavaScriptCodeInCurrentTab(
String.format("var aTag = document.createElement('a');" String.format("var aTag = document.createElement('a');"
...@@ -382,14 +407,21 @@ public class WebappNavigationTest { ...@@ -382,14 +407,21 @@ public class WebappNavigationTest {
id, url, target)); id, url, target));
} }
private void addAnchorAndClick(String url, String target) throws Exception {
addAnchor("testId", url, target);
DOMUtils.clickNode(
mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId");
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T extends Activity> T waitFor(final Class<T> expectedClass) { private <T extends ChromeActivity> T waitFor(final Class<T> expectedClass) {
final Activity[] holder = new Activity[1]; final Activity[] holder = new Activity[1];
CriteriaHelper.pollUiThread(new Criteria() { CriteriaHelper.pollUiThread(new Criteria() {
@Override @Override
public boolean isSatisfied() { public boolean isSatisfied() {
holder[0] = ApplicationStatus.getLastTrackedFocusedActivity(); holder[0] = ApplicationStatus.getLastTrackedFocusedActivity();
return holder[0] != null && expectedClass.isAssignableFrom(holder[0].getClass()); return holder[0] != null && expectedClass.isAssignableFrom(holder[0].getClass())
&& ((ChromeActivity) holder[0]).getActivityTab() != null;
} }
}); });
return (T) holder[0]; return (T) holder[0];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment