OLD | NEW |
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.app.Activity; | 7 import android.app.Activity; |
8 import android.app.Instrumentation; | 8 import android.app.Instrumentation; |
9 import android.app.Instrumentation.ActivityMonitor; | 9 import android.app.Instrumentation.ActivityMonitor; |
10 import android.content.Context; | 10 import android.content.Context; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 private static final String NAVIGATION_WITH_FALLBACK_URL_PARENT_FRAME_PAGE = | 64 private static final String NAVIGATION_WITH_FALLBACK_URL_PARENT_FRAME_PAGE = |
65 BASE_PATH + "navigation_with_fallback_url_parent_frame.html"; | 65 BASE_PATH + "navigation_with_fallback_url_parent_frame.html"; |
66 private static final String FALLBACK_LANDING_PATH = BASE_PATH + "hello.html"
; | 66 private static final String FALLBACK_LANDING_PATH = BASE_PATH + "hello.html"
; |
67 private static final String OPEN_WINDOW_FROM_USER_GESTURE_PAGE = | 67 private static final String OPEN_WINDOW_FROM_USER_GESTURE_PAGE = |
68 BASE_PATH + "open_window_from_user_gesture.html"; | 68 BASE_PATH + "open_window_from_user_gesture.html"; |
69 private static final String NAVIGATION_FROM_JAVA_REDIRECTION_PAGE = | 69 private static final String NAVIGATION_FROM_JAVA_REDIRECTION_PAGE = |
70 BASE_PATH + "navigation_from_java_redirection.html"; | 70 BASE_PATH + "navigation_from_java_redirection.html"; |
71 | 71 |
72 private static class TestTabObserver extends EmptyTabObserver { | 72 private static class TestTabObserver extends EmptyTabObserver { |
73 private final CallbackHelper mFinishCallback; | 73 private final CallbackHelper mFinishCallback; |
74 private final CallbackHelper mPageFailCallback; | 74 private final CallbackHelper mFailCallback; |
75 private final CallbackHelper mLoadFailCallback; | |
76 | 75 |
77 TestTabObserver(final CallbackHelper finishCallback, final CallbackHelpe
r pageFailCallback, | 76 TestTabObserver(final CallbackHelper finishCallback, final CallbackHelpe
r failCallback) { |
78 final CallbackHelper loadFailCallback) { | |
79 mFinishCallback = finishCallback; | 77 mFinishCallback = finishCallback; |
80 mPageFailCallback = pageFailCallback; | 78 mFailCallback = failCallback; |
81 mLoadFailCallback = loadFailCallback; | |
82 } | 79 } |
83 | 80 |
84 @Override | 81 @Override |
85 public void onDidFinishNavigation(Tab tab, String url, boolean isInMainF
rame, | 82 public void onDidFinishNavigation(Tab tab, String url, boolean isInMainF
rame, |
86 boolean isErrorPage, boolean hasCommitted, boolean isSameDocumen
t, | 83 boolean isErrorPage, boolean hasCommitted, boolean isSameDocumen
t, |
87 boolean isFragmentNavigation, Integer pageTransition, int errorC
ode, | 84 boolean isFragmentNavigation, Integer pageTransition, int errorC
ode, |
88 int httpStatusCode) { | 85 int httpStatusCode) { |
89 if (errorCode != 0) { | 86 if (errorCode == 0) return; |
90 mLoadFailCallback.notifyCalled(); | 87 mFailCallback.notifyCalled(); |
91 } | |
92 } | 88 } |
93 | 89 |
94 @Override | 90 @Override |
95 public void onPageLoadFinished(Tab tab) { | 91 public void onPageLoadFinished(Tab tab) { |
96 mFinishCallback.notifyCalled(); | 92 mFinishCallback.notifyCalled(); |
97 } | 93 } |
98 | 94 |
99 @Override | 95 @Override |
100 public void onPageLoadFailed(Tab tab, int errorCode) { | |
101 mPageFailCallback.notifyCalled(); | |
102 } | |
103 | |
104 @Override | |
105 public void onDidFailLoad(Tab tab, boolean isMainFrame, int errorCode, S
tring description, | |
106 String failingUrl) { | |
107 mLoadFailCallback.notifyCalled(); | |
108 } | |
109 | |
110 @Override | |
111 public void onDestroyed(Tab tab) { | 96 public void onDestroyed(Tab tab) { |
112 // A new tab is destroyed when loading is overridden while opening i
t. | 97 // A new tab is destroyed when loading is overridden while opening i
t. |
113 mPageFailCallback.notifyCalled(); | 98 mFailCallback.notifyCalled(); |
114 } | 99 } |
115 } | 100 } |
116 | 101 |
117 private ActivityMonitor mActivityMonitor; | 102 private ActivityMonitor mActivityMonitor; |
118 private EmbeddedTestServer mTestServer; | 103 private EmbeddedTestServer mTestServer; |
119 | 104 |
120 public UrlOverridingTest() { | 105 public UrlOverridingTest() { |
121 super(ChromeActivity.class); | 106 super(ChromeActivity.class); |
122 } | 107 } |
123 | 108 |
(...skipping 16 matching lines...) Expand all Loading... |
140 | 125 |
141 private void loadUrlAndWaitForIntentUrl(final String url, boolean needClick, | 126 private void loadUrlAndWaitForIntentUrl(final String url, boolean needClick, |
142 boolean shouldLaunchExternalIntent, boolean isMainFrame) throws Inte
rruptedException { | 127 boolean shouldLaunchExternalIntent, boolean isMainFrame) throws Inte
rruptedException { |
143 loadUrlAndWaitForIntentUrl(url, needClick, 0, shouldLaunchExternalIntent
, url, isMainFrame); | 128 loadUrlAndWaitForIntentUrl(url, needClick, 0, shouldLaunchExternalIntent
, url, isMainFrame); |
144 } | 129 } |
145 | 130 |
146 private void loadUrlAndWaitForIntentUrl(final String url, boolean needClick, | 131 private void loadUrlAndWaitForIntentUrl(final String url, boolean needClick, |
147 int expectedNewTabCount, final boolean shouldLaunchExternalIntent, | 132 int expectedNewTabCount, final boolean shouldLaunchExternalIntent, |
148 final String expectedFinalUrl, boolean isMainFrame) throws Interrupt
edException { | 133 final String expectedFinalUrl, boolean isMainFrame) throws Interrupt
edException { |
149 final CallbackHelper finishCallback = new CallbackHelper(); | 134 final CallbackHelper finishCallback = new CallbackHelper(); |
150 final CallbackHelper pageFailCallback = new CallbackHelper(); | 135 final CallbackHelper failCallback = new CallbackHelper(); |
151 final CallbackHelper loadFailCallback = new CallbackHelper(); | |
152 final CallbackHelper newTabCallback = new CallbackHelper(); | 136 final CallbackHelper newTabCallback = new CallbackHelper(); |
153 | 137 |
154 final Tab tab = getActivity().getActivityTab(); | 138 final Tab tab = getActivity().getActivityTab(); |
155 final Tab[] latestTabHolder = new Tab[1]; | 139 final Tab[] latestTabHolder = new Tab[1]; |
156 latestTabHolder[0] = tab; | 140 latestTabHolder[0] = tab; |
157 tab.addObserver(new TestTabObserver(finishCallback, pageFailCallback, lo
adFailCallback)); | 141 tab.addObserver(new TestTabObserver(finishCallback, failCallback)); |
158 if (expectedNewTabCount > 0) { | 142 if (expectedNewTabCount > 0) { |
159 getActivity().getTabModelSelector().addObserver(new EmptyTabModelSel
ectorObserver() { | 143 getActivity().getTabModelSelector().addObserver(new EmptyTabModelSel
ectorObserver() { |
160 @Override | 144 @Override |
161 public void onNewTabCreated(Tab newTab) { | 145 public void onNewTabCreated(Tab newTab) { |
162 newTabCallback.notifyCalled(); | 146 newTabCallback.notifyCalled(); |
163 newTab.addObserver(new TestTabObserver( | 147 newTab.addObserver(new TestTabObserver(finishCallback, failC
allback)); |
164 finishCallback, pageFailCallback, loadFailCallback))
; | |
165 latestTabHolder[0] = newTab; | 148 latestTabHolder[0] = newTab; |
166 } | 149 } |
167 }); | 150 }); |
168 } | 151 } |
169 | 152 |
170 getActivity().onUserInteraction(); | 153 getActivity().onUserInteraction(); |
171 getInstrumentation().runOnMainSync(new Runnable() { | 154 getInstrumentation().runOnMainSync(new Runnable() { |
172 @Override | 155 @Override |
173 public void run() { | 156 public void run() { |
174 tab.loadUrl(new LoadUrlParams(url, PageTransition.LINK)); | 157 tab.loadUrl(new LoadUrlParams(url, PageTransition.LINK)); |
175 } | 158 } |
176 }); | 159 }); |
177 | 160 |
178 if (finishCallback.getCallCount() == 0) { | 161 if (finishCallback.getCallCount() == 0) { |
179 try { | 162 try { |
180 finishCallback.waitForCallback(0, 1, 20, TimeUnit.SECONDS); | 163 finishCallback.waitForCallback(0, 1, 20, TimeUnit.SECONDS); |
181 } catch (TimeoutException ex) { | 164 } catch (TimeoutException ex) { |
182 fail(); | 165 fail(); |
183 return; | 166 return; |
184 } | 167 } |
185 } | 168 } |
186 | 169 |
187 SystemClock.sleep(1); | 170 SystemClock.sleep(1); |
188 getActivity().onUserInteraction(); | 171 getActivity().onUserInteraction(); |
189 if (needClick) { | 172 if (needClick) { |
190 singleClickView(tab.getView()); | 173 singleClickView(tab.getView()); |
191 } | 174 } |
192 | 175 |
193 CallbackHelper helper = isMainFrame ? pageFailCallback : loadFailCallbac
k; | 176 if (failCallback.getCallCount() == 0) { |
194 if (helper.getCallCount() == 0) { | |
195 try { | 177 try { |
196 helper.waitForCallback(0, 1, 20, TimeUnit.SECONDS); | 178 failCallback.waitForCallback(0, 1, 20, TimeUnit.SECONDS); |
197 } catch (TimeoutException ex) { | 179 } catch (TimeoutException ex) { |
198 fail(); | 180 fail("Haven't received navigation failure of intents."); |
199 return; | 181 return; |
200 } | 182 } |
201 } | 183 } |
202 | 184 |
| 185 boolean hasFallbackUrl = |
| 186 expectedFinalUrl != null && !TextUtils.equals(url, expectedFinal
Url); |
| 187 |
| 188 if (hasFallbackUrl) { |
| 189 if (finishCallback.getCallCount() == 1) { |
| 190 try { |
| 191 finishCallback.waitForCallback(1, 1, 20, TimeUnit.SECONDS); |
| 192 } catch (TimeoutException ex) { |
| 193 fail("Fallback URL is not loaded"); |
| 194 return; |
| 195 } |
| 196 } |
| 197 } |
| 198 |
203 assertEquals(expectedNewTabCount, newTabCallback.getCallCount()); | 199 assertEquals(expectedNewTabCount, newTabCallback.getCallCount()); |
204 // For sub frames, the |loadFailCallback| run through different threads | 200 // For sub frames, the |loadFailCallback| run through different threads |
205 // from the ExternalNavigationHandler. As a result, there is no guarante
e | 201 // from the ExternalNavigationHandler. As a result, there is no guarante
e |
206 // when url override result would come. | 202 // when url override result would come. |
207 CriteriaHelper.pollUiThread( | 203 CriteriaHelper.pollUiThread( |
208 new Criteria() { | 204 new Criteria() { |
209 @Override | 205 @Override |
210 public boolean isSatisfied() { | 206 public boolean isSatisfied() { |
211 // Note that we do not distinguish between OVERRIDE_WITH
_CLOBBERING_TAB | 207 // Note that we do not distinguish between OVERRIDE_WITH
_CLOBBERING_TAB |
212 // and NO_OVERRIDE since tab clobbering will eventually
lead to NO_OVERRIDE. | 208 // and NO_OVERRIDE since tab clobbering will eventually
lead to NO_OVERRIDE. |
213 // in the tab. Rather, we check the final URL to disting
uish between | 209 // in the tab. Rather, we check the final URL to disting
uish between |
214 // fallback and normal navigation. See crbug.com/487364
for more. | 210 // fallback and normal navigation. See crbug.com/487364
for more. |
215 Tab tab = latestTabHolder[0]; | 211 Tab tab = latestTabHolder[0]; |
216 if (shouldLaunchExternalIntent | 212 if (shouldLaunchExternalIntent |
217 != (OverrideUrlLoadingResult.OVERRIDE_WITH_EXTER
NAL_INTENT | 213 != (OverrideUrlLoadingResult.OVERRIDE_WITH_EXTER
NAL_INTENT |
218 == tab.getInterceptNavigationDelegate() | 214 == tab.getInterceptNavigationDelegate() |
219 .getLastOverrideUrlLoadingResult
ForTests())) { | 215 .getLastOverrideUrlLoadingResult
ForTests())) { |
220 return false; | 216 return false; |
221 } | 217 } |
222 updateFailureReason("Expected: " + expectedFinalUrl + "
actual: " | 218 updateFailureReason("Expected: " + expectedFinalUrl + "
actual: " |
223 + tab.getUrl()); | 219 + tab.getUrl()); |
224 return expectedFinalUrl == null | 220 return expectedFinalUrl == null |
225 || TextUtils.equals(expectedFinalUrl, tab.getUrl
()); | 221 || TextUtils.equals(expectedFinalUrl, tab.getUrl
()); |
226 } | 222 } |
227 }); | 223 }); |
| 224 |
| 225 CriteriaHelper.pollUiThread( |
| 226 Criteria.equals(shouldLaunchExternalIntent ? 1 : 0, new Callable
<Integer>() { |
| 227 @Override |
| 228 public Integer call() { |
| 229 return mActivityMonitor.getHits(); |
| 230 } |
| 231 })); |
| 232 assertEquals(1 + (hasFallbackUrl ? 1 : 0), finishCallback.getCallCount()
); |
| 233 assertEquals(1, failCallback.getCallCount()); |
228 } | 234 } |
229 | 235 |
230 @SmallTest | 236 @SmallTest |
231 @RetryOnFailure | 237 @RetryOnFailure |
232 public void testNavigationFromTimer() throws InterruptedException { | 238 public void testNavigationFromTimer() throws InterruptedException { |
233 loadUrlAndWaitForIntentUrl( | 239 loadUrlAndWaitForIntentUrl( |
234 mTestServer.getURL(NAVIGATION_FROM_TIMEOUT_PAGE), false, false,
true); | 240 mTestServer.getURL(NAVIGATION_FROM_TIMEOUT_PAGE), false, false,
true); |
235 } | 241 } |
236 | 242 |
237 @SmallTest | 243 @SmallTest |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 return mActivityMonitor.getHits(); | 355 return mActivityMonitor.getHits(); |
350 } | 356 } |
351 })); | 357 })); |
352 } | 358 } |
353 | 359 |
354 @Override | 360 @Override |
355 public void startMainActivity() throws InterruptedException { | 361 public void startMainActivity() throws InterruptedException { |
356 startMainActivityOnBlankPage(); | 362 startMainActivityOnBlankPage(); |
357 } | 363 } |
358 } | 364 } |
OLD | NEW |