OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.content.Intent; | 7 import android.content.Intent; |
8 import android.graphics.Color; | 8 import android.graphics.Color; |
9 import android.support.test.InstrumentationRegistry; | 9 import android.support.test.InstrumentationRegistry; |
10 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
11 | 11 |
12 import org.junit.After; | 12 import org.junit.After; |
13 import org.junit.Assert; | 13 import org.junit.Assert; |
14 import org.junit.Before; | 14 import org.junit.Before; |
15 import org.junit.Rule; | 15 import org.junit.Rule; |
16 import org.junit.Test; | 16 import org.junit.Test; |
17 import org.junit.runner.RunWith; | 17 import org.junit.runner.RunWith; |
18 | 18 |
19 import org.chromium.base.ApiCompatibilityUtils; | 19 import org.chromium.base.ApiCompatibilityUtils; |
20 import org.chromium.base.ThreadUtils; | |
21 import org.chromium.base.test.util.CommandLineFlags; | 20 import org.chromium.base.test.util.CommandLineFlags; |
22 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
23 import org.chromium.chrome.R; | 22 import org.chromium.chrome.R; |
24 import org.chromium.chrome.browser.ChromeSwitches; | 23 import org.chromium.chrome.browser.ChromeSwitches; |
25 import org.chromium.chrome.browser.ChromeTabbedActivity; | 24 import org.chromium.chrome.browser.ChromeTabbedActivity; |
26 import org.chromium.chrome.browser.ShortcutHelper; | 25 import org.chromium.chrome.browser.ShortcutHelper; |
27 import org.chromium.chrome.browser.customtabs.CustomTabActivity; | 26 import org.chromium.chrome.browser.customtabs.CustomTabActivity; |
28 import org.chromium.chrome.browser.firstrun.FirstRunStatus; | 27 import org.chromium.chrome.browser.firstrun.FirstRunStatus; |
29 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | 28 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
30 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; | 29 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; |
31 import org.chromium.content_public.browser.LoadUrlParams; | 30 import org.chromium.content.browser.test.util.DOMUtils; |
32 import org.chromium.net.test.EmbeddedTestServer; | 31 import org.chromium.net.test.EmbeddedTestServer; |
33 import org.chromium.ui.base.PageTransition; | 32 import org.chromium.ui.base.PageTransition; |
34 | 33 |
35 /** | 34 /** |
36 * Tests web navigations originating from a WebappActivity. | 35 * Tests web navigations originating from a WebappActivity. |
37 */ | 36 */ |
38 @RunWith(ChromeJUnit4ClassRunner.class) | 37 @RunWith(ChromeJUnit4ClassRunner.class) |
39 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) | 38 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) |
40 public class WebappNavigationTest { | 39 public class WebappNavigationTest { |
41 private static final String OFF_ORIGIN_URL = "https://www.google.com/"; | 40 private static final String OFF_ORIGIN_URL = "https://www.google.com/"; |
(...skipping 15 matching lines...) Expand all Loading... |
57 } | 56 } |
58 | 57 |
59 @After | 58 @After |
60 public void tearDown() throws Exception { | 59 public void tearDown() throws Exception { |
61 mTestServer.stopAndDestroyServer(); | 60 mTestServer.stopAndDestroyServer(); |
62 } | 61 } |
63 | 62 |
64 @Test | 63 @Test |
65 @SmallTest | 64 @SmallTest |
66 @Feature({"Webapps"}) | 65 @Feature({"Webapps"}) |
67 public void testOffOriginNavigationUsingLinkAndNoWebappThemeColor() throws E
xception { | 66 public void testRegularLinkOffOriginInCctNoWebappThemeColor() throws Excepti
on { |
68 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | 67 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
69 | 68 addAnchor("testId", OFF_ORIGIN_URL, "_self"); |
70 // Not using #loadUrl, as it expects the URL to load in the activity und
er test, | 69 DOMUtils.clickNode( |
71 // which is not happening here. | 70 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testId"); |
72 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
73 @Override | |
74 public void run() { | |
75 mActivityTestRule.getActivity().getActivityTab().loadUrl( | |
76 new LoadUrlParams(OFF_ORIGIN_URL, PageTransition.LINK)); | |
77 } | |
78 }); | |
79 | 71 |
80 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); | 72 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); |
81 | 73 |
82 Assert.assertEquals( | 74 Assert.assertEquals( |
83 "CCT Toolbar should use default primary color if theme color is
not specified", | 75 "CCT Toolbar should use default primary color if theme color is
not specified", |
84 ApiCompatibilityUtils.getColor( | 76 ApiCompatibilityUtils.getColor( |
85 customTab.getResources(), R.color.default_primary_color)
, | 77 customTab.getResources(), R.color.default_primary_color)
, |
86 customTab.getToolbarManager().getPrimaryColor()); | 78 customTab.getToolbarManager().getPrimaryColor()); |
87 } | 79 } |
88 | 80 |
89 @Test | 81 @Test |
90 @SmallTest | 82 @SmallTest |
91 @Feature({"Webapps"}) | 83 @Feature({"Webapps"}) |
92 public void testOffOriginNavigationUsingJavaScriptAndWebappThemeColor() thro
ws Exception { | 84 public void testWindowTopLocationOffOriginInCctAndWebappThemeColor() throws
Exception { |
93 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( | 85 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( |
94 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); | 86 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); |
95 | 87 |
96 mActivityTestRule.runJavaScriptCodeInCurrentTab( | 88 mActivityTestRule.runJavaScriptCodeInCurrentTab( |
97 String.format("window.top.location = '%s'", OFF_ORIGIN_URL)); | 89 String.format("window.top.location = '%s'", OFF_ORIGIN_URL)); |
98 | 90 |
99 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); | 91 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); |
100 | 92 |
101 Assert.assertEquals("CCT Toolbar should use the theme color of a webapp"
, Color.CYAN, | 93 Assert.assertEquals("CCT Toolbar should use the theme color of a webapp"
, Color.CYAN, |
102 customTab.getToolbarManager().getPrimaryColor()); | 94 customTab.getToolbarManager().getPrimaryColor()); |
103 } | 95 } |
104 | 96 |
105 private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { | 97 @Test |
106 CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity
.class); | 98 @SmallTest |
107 | 99 @Feature({"Webapps"}) |
108 mActivityTestRule.waitUntilIdle(customTab); | 100 public void testNewTabLinkOpensInCct() throws Exception { |
109 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | 101 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( |
110 Assert.assertTrue(customTab.getActivityTab().getUrl().startsWith("https:
//www.google.")); | 102 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); |
111 | 103 addAnchor("testId", OFF_ORIGIN_URL, "_blank"); |
112 return customTab; | 104 DOMUtils.clickNode( |
| 105 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testId"); |
| 106 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); |
| 107 Assert.assertEquals( |
| 108 "CCT Toolbar should use default primary color even if webapp has
theme color", |
| 109 ApiCompatibilityUtils.getColor( |
| 110 customTab.getResources(), R.color.default_primary_color)
, |
| 111 customTab.getToolbarManager().getPrimaryColor()); |
113 } | 112 } |
114 | 113 |
115 @Test | 114 @Test |
| 115 @SmallTest |
| 116 @Feature({"Webapps"}) |
| 117 public void testWindowOpenInCct() throws Exception { |
| 118 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| 119 // Executing window.open() through a click on a link, |
| 120 // as it needs user gesture to avoid Chrome blocking it as a popup. |
| 121 mActivityTestRule.runJavaScriptCodeInCurrentTab( |
| 122 String.format("var aTag = document.createElement('testId');" |
| 123 + "aTag.id = 'testId';" |
| 124 + "aTag.innerHTML = 'Click Me!';" |
| 125 + "aTag.onclick = function() {" |
| 126 + " window.open('%s');" |
| 127 + " return false;" |
| 128 + "};" |
| 129 + "document.body.appendChild(aTag);", |
| 130 OFF_ORIGIN_URL)); |
| 131 DOMUtils.clickNode( |
| 132 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testId"); |
| 133 |
| 134 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); |
| 135 Assert.assertEquals("CCT Toolbar should use default primary color", |
| 136 ApiCompatibilityUtils.getColor( |
| 137 customTab.getResources(), R.color.default_primary_color)
, |
| 138 customTab.getToolbarManager().getPrimaryColor()); |
| 139 } |
| 140 |
| 141 @Test |
116 @SmallTest | 142 @SmallTest |
117 @Feature({"Webapps"}) | 143 @Feature({"Webapps"}) |
118 public void testInOriginNavigationStaysInWebapp() throws Exception { | 144 public void testInOriginNavigationStaysInWebapp() throws Exception { |
119 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | 145 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
120 | 146 |
121 String otherPageUrl = mTestServer.getURL(OTHER_PAGE_PATH); | 147 String otherPageUrl = mTestServer.getURL(OTHER_PAGE_PATH); |
122 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, | 148 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, |
123 mActivityTestRule.getActivity().getActivityTab()); | 149 mActivityTestRule.getActivity().getActivityTab()); |
124 | 150 |
125 mActivityTestRule.waitUntilIdle(); | 151 mActivityTestRule.waitUntilIdle(); |
126 Assert.assertEquals( | 152 Assert.assertEquals( |
127 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); | 153 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); |
128 | 154 |
129 Assert.assertSame( | 155 Assert.assertSame( |
130 mActivityTestRule.getActivity(), activityListener.getMostRecentA
ctivity()); | 156 mActivityTestRule.getActivity(), activityListener.getMostRecentA
ctivity()); |
131 } | 157 } |
132 | 158 |
133 @Test | 159 @Test |
134 @SmallTest | 160 @SmallTest |
135 @Feature({"Webapps"}) | 161 @Feature({"Webapps"}) |
136 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { | 162 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { |
137 // Needed to get full context menu. | 163 // Needed to get full context menu. |
138 FirstRunStatus.setFirstRunFlowComplete(true); | 164 FirstRunStatus.setFirstRunFlowComplete(true); |
139 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | 165 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
140 | 166 |
141 mActivityTestRule.runJavaScriptCodeInCurrentTab("var aTag = document.cre
ateElement('a');" | 167 addAnchor("myTestAnchorId", OFF_ORIGIN_URL, "_self"); |
142 + "aTag.id = 'myTestAnchorId';" | |
143 + "aTag.setAttribute('href','https://www.google.com/');" | |
144 + "aTag.innerHTML = 'Click Me!';" | |
145 + "document.body.appendChild(aTag);"); | |
146 | 168 |
147 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), | 169 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), |
148 null /* activity to check for focus after click */, | 170 null /* activity to check for focus after click */, |
149 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", | 171 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", |
150 R.id.menu_id_open_in_chrome); | 172 R.id.menu_id_open_in_chrome); |
151 | 173 |
152 ChromeTabbedActivity tabbedChrome = activityListener.waitFor(ChromeTabbe
dActivity.class); | 174 ChromeTabbedActivity tabbedChrome = activityListener.waitFor(ChromeTabbe
dActivity.class); |
153 | 175 |
154 mActivityTestRule.waitUntilIdle(tabbedChrome); | 176 mActivityTestRule.waitUntilIdle(tabbedChrome); |
155 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | 177 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
156 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); | 178 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); |
157 } | 179 } |
158 | 180 |
159 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ | 181 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ |
160 mActivityTestRule.startWebappActivity( | 182 mActivityTestRule.startWebappActivity( |
161 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); | 183 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); |
162 | 184 |
163 mActivityTestRule.waitUntilSplashscreenHides(); | 185 mActivityTestRule.waitUntilSplashscreenHides(); |
164 mActivityTestRule.waitUntilIdle(); | 186 mActivityTestRule.waitUntilIdle(); |
165 } | 187 } |
| 188 |
| 189 private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { |
| 190 CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity
.class); |
| 191 |
| 192 mActivityTestRule.waitUntilIdle(customTab); |
| 193 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
| 194 Assert.assertTrue(customTab.getActivityTab().getUrl().contains("https://
www.google.")); |
| 195 |
| 196 return customTab; |
| 197 } |
| 198 |
| 199 private void addAnchor(String id, String url, String target) throws Exceptio
n { |
| 200 mActivityTestRule.runJavaScriptCodeInCurrentTab( |
| 201 String.format("var aTag = document.createElement('a');" |
| 202 + "aTag.id = '%s';" |
| 203 + "aTag.setAttribute('href','%s');" |
| 204 + "aTag.setAttribute('target','%s');" |
| 205 + "aTag.innerHTML = 'Click Me!';" |
| 206 + "document.body.appendChild(aTag);", |
| 207 id, url, target)); |
| 208 } |
166 } | 209 } |
OLD | NEW |