Index: chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
index 25c476d3e882f4f5b40a3162092de4336ed36f44..6d234b88004f0f0f985fb0cad01612564b120652 100644 |
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
@@ -4,8 +4,12 @@ |
package org.chromium.chrome.browser.webapps; |
+import android.app.ActivityManager; |
+import android.app.ActivityManager.AppTask; |
+import android.content.Context; |
import android.content.Intent; |
import android.graphics.Color; |
+import android.os.Build; |
import android.support.test.InstrumentationRegistry; |
import android.support.test.filters.SmallTest; |
@@ -17,7 +21,6 @@ import org.junit.Test; |
import org.junit.runner.RunWith; |
import org.chromium.base.ApiCompatibilityUtils; |
-import org.chromium.base.ThreadUtils; |
import org.chromium.base.test.util.CommandLineFlags; |
import org.chromium.base.test.util.Feature; |
import org.chromium.chrome.R; |
@@ -28,7 +31,7 @@ import org.chromium.chrome.browser.customtabs.CustomTabActivity; |
import org.chromium.chrome.browser.firstrun.FirstRunStatus; |
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; |
-import org.chromium.content_public.browser.LoadUrlParams; |
+import org.chromium.content.browser.test.util.DOMUtils; |
import org.chromium.net.test.EmbeddedTestServer; |
import org.chromium.ui.base.PageTransition; |
@@ -64,18 +67,12 @@ public class WebappNavigationTest { |
@Test |
@SmallTest |
@Feature({"Webapps"}) |
- public void testOffOriginNavigationUsingLinkAndNoWebappThemeColor() throws Exception { |
+ public void testRegularLinkOffOriginInCctNoWebappThemeColor() throws Exception { |
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
- |
- // Not using #loadUrl, as it expects the URL to load in the activity under test, |
- // which is not happening here. |
- ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
- @Override |
- public void run() { |
- mActivityTestRule.getActivity().getActivityTab().loadUrl( |
- new LoadUrlParams(OFF_ORIGIN_URL, PageTransition.LINK)); |
- } |
- }); |
+ assertNumberOfTasksInRecents("We should start with one Android task", 1); |
+ addAnchor("testId", OFF_ORIGIN_URL, "_self"); |
+ DOMUtils.clickNode( |
+ mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); |
CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl(); |
@@ -84,14 +81,17 @@ public class WebappNavigationTest { |
ApiCompatibilityUtils.getColor( |
customTab.getResources(), R.color.default_primary_color), |
customTab.getToolbarManager().getPrimaryColor()); |
+ assertNumberOfTasksInRecents( |
+ "Link with target=\"_self\" should stay in the same Android task.", 1); |
} |
@Test |
@SmallTest |
@Feature({"Webapps"}) |
- public void testOffOriginNavigationUsingJavaScriptAndWebappThemeColor() throws Exception { |
+ public void testWindowTopLocationOffOriginInCctAndWebappThemeColor() throws Exception { |
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra( |
ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); |
+ assertNumberOfTasksInRecents("We should start with one Android task", 1); |
mActivityTestRule.runJavaScriptCodeInCurrentTab( |
String.format("window.top.location = '%s'", OFF_ORIGIN_URL)); |
@@ -100,16 +100,58 @@ public class WebappNavigationTest { |
Assert.assertEquals("CCT Toolbar should use the theme color of a webapp", Color.CYAN, |
customTab.getToolbarManager().getPrimaryColor()); |
+ assertNumberOfTasksInRecents( |
+ "Executing window.top.location = url; should stay in the same Android task.", 1); |
} |
- private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { |
- CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity.class); |
+ @Test |
+ @SmallTest |
+ @Feature({"Webapps"}) |
+ public void testNewTabLinkOpensInCct() throws Exception { |
+ runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtra( |
+ ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); |
+ assertNumberOfTasksInRecents("We should start with one Android task", 1); |
+ addAnchor("testId", OFF_ORIGIN_URL, "_blank"); |
+ DOMUtils.clickNode( |
+ mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); |
+ CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl(); |
+ Assert.assertEquals( |
+ "CCT Toolbar should use default primary color even if webapp has theme color", |
+ ApiCompatibilityUtils.getColor( |
+ customTab.getResources(), R.color.default_primary_color), |
+ customTab.getToolbarManager().getPrimaryColor()); |
+ assertNumberOfTasksInRecents( |
+ "Link with target=\"_blank\" should open in a new Android task.", 2); |
+ } |
- mActivityTestRule.waitUntilIdle(customTab); |
- // Dropping the TLD as Google can redirect to a local site, so this could fail outside US. |
- Assert.assertTrue(customTab.getActivityTab().getUrl().startsWith("https://www.google.")); |
+ @Test |
+ @SmallTest |
+ @Feature({"Webapps"}) |
+ public void testWindowOpenInCct() throws Exception { |
+ runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
+ assertNumberOfTasksInRecents("We should start with one Android task", 1); |
+ // Executing window.open() through a click on a link, |
+ // as it needs user gesture to avoid Chrome blocking it as a popup. |
+ mActivityTestRule.runJavaScriptCodeInCurrentTab( |
+ String.format("var aTag = document.createElement('testId');" |
+ + "aTag.id = 'testId';" |
+ + "aTag.innerHTML = 'Click Me!';" |
+ + "aTag.onclick = function() {" |
+ + " window.open('%s');" |
+ + " return false;" |
+ + "};" |
+ + "document.body.appendChild(aTag);", |
+ OFF_ORIGIN_URL)); |
+ DOMUtils.clickNode( |
+ mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); |
- return customTab; |
+ CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl(); |
+ Assert.assertEquals("CCT Toolbar should use default primary color", |
+ ApiCompatibilityUtils.getColor( |
+ customTab.getResources(), R.color.default_primary_color), |
+ customTab.getToolbarManager().getPrimaryColor()); |
+ assertNumberOfTasksInRecents( |
+ "Executing window.open(url) should open a new Android task.", 2); |
} |
@Test |
@@ -138,11 +180,7 @@ public class WebappNavigationTest { |
FirstRunStatus.setFirstRunFlowComplete(true); |
runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
- mActivityTestRule.runJavaScriptCodeInCurrentTab("var aTag = document.createElement('a');" |
- + "aTag.id = 'myTestAnchorId';" |
- + "aTag.setAttribute('href','https://www.google.com/');" |
- + "aTag.innerHTML = 'Click Me!';" |
- + "document.body.appendChild(aTag);"); |
+ addAnchor("myTestAnchorId", OFF_ORIGIN_URL, "_self"); |
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), |
null /* activity to check for focus after click */, |
@@ -163,4 +201,45 @@ public class WebappNavigationTest { |
mActivityTestRule.waitUntilSplashscreenHides(); |
mActivityTestRule.waitUntilIdle(); |
} |
+ |
+ private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { |
+ CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity.class); |
+ |
+ mActivityTestRule.waitUntilIdle(customTab); |
+ // Dropping the TLD as Google can redirect to a local site, so this could fail outside US. |
+ Assert.assertTrue(customTab.getActivityTab().getUrl().contains("https://www.google.")); |
+ |
+ return customTab; |
+ } |
+ |
+ private void addAnchor(String id, String url, String target) throws Exception { |
+ mActivityTestRule.runJavaScriptCodeInCurrentTab( |
+ String.format("var aTag = document.createElement('a');" |
+ + "aTag.id = '%s';" |
+ + "aTag.setAttribute('href','%s');" |
+ + "aTag.setAttribute('target','%s');" |
+ + "aTag.innerHTML = 'Click Me!';" |
+ + "document.body.appendChild(aTag);", |
+ id, url, target)); |
+ } |
+ |
+ private void assertNumberOfTasksInRecents(String message, int expectedNumberOfTasks) { |
+ // We only have API to check this since Lollipop. |
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; |
+ |
+ ActivityManager activityManager = |
+ (ActivityManager) mActivityTestRule.getActivity().getSystemService( |
+ Context.ACTIVITY_SERVICE); |
+ |
+ int count = 0; |
+ for (AppTask task : activityManager.getAppTasks()) { |
+ if ((task.getTaskInfo().baseIntent.getFlags() |
+ & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
+ == 0) { |
+ count++; |
+ } |
+ } |
+ |
+ Assert.assertEquals(message, expectedNumberOfTasks, count); |
+ } |
} |