Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchManagerTest.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.contextualsearch;
6
7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E ND_DEVICE;
8 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE;
9 import static org.chromium.content.browser.test.util.CriteriaHelper.DEFAULT_POLL ING_INTERVAL;
10
11 import android.content.Context;
12 import android.content.pm.ActivityInfo;
13 import android.graphics.Point;
14 import android.os.SystemClock;
15 import android.test.FlakyTest;
16 import android.test.suitebuilder.annotation.SmallTest;
17 import android.text.TextUtils;
18 import android.view.KeyEvent;
19
20 import com.google.android.apps.chrome.R;
21
22 import org.chromium.base.ThreadUtils;
23 import org.chromium.base.test.util.CommandLineFlags;
24 import org.chromium.base.test.util.Feature;
25 import org.chromium.base.test.util.Restriction;
26 import org.chromium.chrome.browser.ChromeActivity;
27 import org.chromium.chrome.browser.ChromeSwitches;
28 import org.chromium.chrome.browser.ChromeTabbedActivity;
29 import org.chromium.chrome.browser.CompositorChromeActivity;
30 import org.chromium.chrome.browser.Tab;
31 import org.chromium.chrome.browser.compositor.bottombar.contextualsearch.Context ualSearchPanel.PanelState;
32 import org.chromium.chrome.browser.compositor.bottombar.contextualsearch.Context ualSearchPanelDelegate;
33 import org.chromium.chrome.browser.omnibox.UrlBar;
34 import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
35 import org.chromium.chrome.browser.tabmodel.EmptyTabModelSelectorObserver;
36 import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
37 import org.chromium.chrome.browser.tabmodel.TabModelUtils;
38 import org.chromium.chrome.browser.util.FeatureUtilities;
39 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
40 import org.chromium.chrome.test.util.ChromeTabUtils;
41 import org.chromium.chrome.test.util.OmniboxTestUtils;
42 import org.chromium.chrome.test.util.TestHttpServerClient;
43 import org.chromium.content.browser.ContentViewCore;
44 import org.chromium.content.browser.test.util.CallbackHelper;
45 import org.chromium.content.browser.test.util.Criteria;
46 import org.chromium.content.browser.test.util.CriteriaHelper;
47 import org.chromium.content.browser.test.util.DOMUtils;
48
49 import java.util.concurrent.TimeoutException;
50
51 /**
52 * Tests the Contextual Search Manager using instrumentation tests.
53 */
54 @CommandLineFlags.Add({
55 ChromeSwitches.ENABLE_CONTEXTUAL_SEARCH_FOR_TESTING
56 })
57 public class ContextualSearchManagerTest extends ChromeActivityTestCaseBase<Chro meActivity> {
58
59 private static final String TEST_PAGE =
60 TestHttpServerClient.getUrl("chrome/test/data/android/contextualsear ch/tap_test.html");
61 private static final int TEST_TIMEOUT = 15000;
62
63 // TODO(donnd): get these from TemplateURL once the low-priority or Contextu al Search API
64 // is fully supported.
65 private static final String NORMAL_PRIORITY_SEARCH_ENDPOINT = "/search?";
66 private static final String LOW_PRIORITY_SEARCH_ENDPOINT = "/s?";
67 private static final String CONTEXTUAL_SEARCH_PREFETCH_PARAM = "&pf=c";
68
69 private ContextualSearchManager mManager;
70 private ContextualSearchFakeServer mFakeServer;
71 private ContextualSearchPanelDelegate mPanelDelegate;
72 private ContextualSearchSelectionController mSelectionController;
73 private ContextualSearchPolicy mPolicy;
74 private ChromePreferenceManager mPreferenceManager;
75
76 public ContextualSearchManagerTest() {
77 super(ChromeActivity.class);
78 }
79
80 @Override
81 protected void setUp() throws Exception {
82 super.setUp();
83
84 ChromeActivity activity = getActivity();
85 if (activity instanceof CompositorChromeActivity) {
86 mManager = ((CompositorChromeActivity) activity).getContextualSearch Manager();
87 }
88
89 if (mManager != null) {
90 mFakeServer = new ContextualSearchFakeServer(mManager);
91 mManager.setNetworkCommunicator(mFakeServer);
92 mPanelDelegate = mManager.getContextualSearchPanelDelegate();
93 mSelectionController = mManager.getSelectionController();
94 mPolicy = ContextualSearchPolicy.getInstance(getActivity());
95 mPreferenceManager = ChromePreferenceManager.getInstance(getActivity ());
96
97 mPolicy.overrideDecidedStateForTesting(true);
98 resetTapCounters();
99 }
100 }
101
102 @Override
103 public void startMainActivity() throws InterruptedException {
104 startMainActivityWithURL(TEST_PAGE);
105 }
106
107 /**
108 * Simulates a click on the given node.
109 * @param nodeId A string containing the node ID.
110 */
111 private void clickNode(String nodeId) throws InterruptedException, TimeoutEx ception {
112 Tab tab = getActivity().getActivityTab();
113 DOMUtils.clickNode(this, tab.getContentViewCore(), nodeId);
114 }
115
116 /**
117 * Simulates a click on the given word node.
118 * Waits for the bar to peek.
119 * @param nodeId A string containing the node ID.
120 */
121 private void clickWordNode(String nodeId) throws InterruptedException, Timeo utException {
122 clickNode(nodeId);
123 waitForPanelToPeekAndAssert();
124 }
125
126 /**
127 * Simulates a key press.
128 * @param keycode The key's code.
129 */
130 private void pressKey(int keycode) {
131 getInstrumentation().sendKeyDownUpSync(keycode);
132 getInstrumentation().waitForIdleSync();
133 }
134
135 /**
136 * Simulates pressing back button.
137 */
138 private void pressBackButton() {
139 pressKey(KeyEvent.KEYCODE_BACK);
140 }
141
142 /**
143 * @return The selected text.
144 */
145 private String getSelectedText() {
146 return mSelectionController.getSelectedText();
147 }
148
149 /**
150 * Simulates a long-press on the given node.
151 * @param nodeId A string containing the node ID.
152 */
153 private void longPressNode(String nodeId) throws InterruptedException, Timeo utException {
154 Tab tab = getActivity().getActivityTab();
155 DOMUtils.longPressNode(this, tab.getContentViewCore(), nodeId);
156 waitForPanelToPeekAndAssert();
157 }
158
159 /**
160 * Posts a fake response on the Main thread.
161 */
162 private final class FakeResponseOnMainThread implements Runnable {
163
164 private final boolean mIsNetworkUnavailable;
165 private final int mResponseCode;
166 private final String mSearchTerm;
167 private final String mDisplayText;
168 private final String mAlternateTerm;
169 private final boolean mDoPreventPreload;
170
171 public FakeResponseOnMainThread(boolean isNetworkUnavailable, int respon seCode,
172 String searchTerm, String displayText, String alternateTerm,
173 boolean doPreventPreload) {
174 mIsNetworkUnavailable = isNetworkUnavailable;
175 mResponseCode = responseCode;
176 mSearchTerm = searchTerm;
177 mDisplayText = displayText;
178 mAlternateTerm = alternateTerm;
179 mDoPreventPreload = doPreventPreload;
180 }
181
182 @Override
183 public void run() {
184 mFakeServer.handleSearchTermResolutionResponse(
185 mIsNetworkUnavailable, mResponseCode, mSearchTerm, mDisplayT ext,
186 mAlternateTerm, mDoPreventPreload);
187 }
188 }
189
190 /**
191 * Fakes a server response with the parameters given.
192 * {@See ContextualSearchManager#handleSearchTermResolutionResponse}.
193 */
194 private void fakeResponse(boolean isNetworkUnavailable, int responseCode,
195 String searchTerm, String displayText, String alternateTerm, boolean doPreventPreload) {
196 if (mFakeServer.getSearchTermRequested() != null) {
197 getInstrumentation().runOnMainSync(
198 new FakeResponseOnMainThread(isNetworkUnavailable, responseC ode, searchTerm,
199 displayText, alternateTerm, doPreventPreload));
200 }
201 }
202
203 private void assertContainsParameters(String searchTerm, String alternateTer m) {
204 assertTrue(mFakeServer == null || mFakeServer.getSearchTermRequested() = = null
205 || mFakeServer.getLoadedUrl().contains(searchTerm)
206 && mFakeServer.getLoadedUrl().contains(alternateTerm));
207 }
208
209 private void assertContainsNoParameters() {
210 assertTrue(mFakeServer == null || mFakeServer.getLoadedUrl() == null);
211 }
212
213 private void assertSearchTermRequested() {
214 assertNotNull(mFakeServer.getSearchTermRequested());
215 }
216
217 private void assertSearchTermNotRequested() {
218 assertNull(mFakeServer.getSearchTermRequested());
219 }
220
221 private void assertPanelClosedOrUndefined() {
222 boolean success = false;
223 if (mPanelDelegate == null) {
224 success = true;
225 } else {
226 PanelState panelState = mPanelDelegate.getPanelState();
227 success = panelState == PanelState.CLOSED || panelState == PanelStat e.UNDEFINED;
228 }
229 assertTrue(success);
230 }
231
232 private void assertLoadedNoUrl() {
233 assertTrue("Requested a search or preload when none was expected!",
234 (mFakeServer == null || mFakeServer.getLoadedUrl() == null));
235 }
236
237 private void assertLoadedLowPriorityUrl() {
238 if (mFakeServer == null) return;
239 String message = "Expected a low priority search request URL, but got "
240 + (mFakeServer.getLoadedUrl() != null ? mFakeServer.getLoadedUrl () : "null");
241 assertTrue(message, mFakeServer.getLoadedUrl() != null
242 && mFakeServer.getLoadedUrl().contains(LOW_PRIORITY_SEARCH_ENDPO INT));
243 assertTrue("Low priority request does not have the required prefetch par ameter!",
244 mFakeServer.getLoadedUrl() != null
245 && mFakeServer.getLoadedUrl().contains(CONTEXTUAL_SEARCH_PREFETC H_PARAM));
246 }
247
248 private void assertLoadedNormalPriorityUrl() {
249 if (mFakeServer == null) return;
250 String message = "Expected a normal priority search request URL, but got "
251 + (mFakeServer.getLoadedUrl() != null ? mFakeServer.getLoadedUrl () : "null");
252 assertTrue(message, mFakeServer.getLoadedUrl() != null
253 && mFakeServer.getLoadedUrl().contains(NORMAL_PRIORITY_SEARCH_EN DPOINT));
254 assertTrue("Normal priority request should not have the prefetch paramet er, but did!",
255 mFakeServer.getLoadedUrl() != null
256 && !mFakeServer.getLoadedUrl().contains(CONTEXTUAL_SEARCH_PREFET CH_PARAM));
257 }
258
259 private void assertNoSearchesLoaded() {
260 assertEquals(0, mFakeServer.loadedUrlCount());
261 assertLoadedNoUrl();
262 }
263
264 private void assertContentViewCoreCreated() {
265 assertTrue(mFakeServer.isSearchContentViewCreated());
266 }
267
268 private void assertNoContentViewCore() {
269 assertFalse(mFakeServer.isSearchContentViewCreated());
270 }
271
272 /**
273 * Fakes navigation of the Content View with the given httpResult code.
274 * The URL of the navigation is the one requested previously.
275 * @param httpResultCode The result to fake.
276 */
277 private void fakeContentViewDidNavigate(int httpResultCode) {
278 String url = mFakeServer.getLoadedUrl();
279 mFakeServer.handleDidNavigateMainFrame(url, httpResultCode);
280 }
281
282 /**
283 * Waits for the Search Panel (the Search Bar) to peek up from the bottom, a nd asserts that it
284 * did peek.
285 * @throws InterruptedException
286 */
287 private void waitForPanelToPeekAndAssert() throws InterruptedException {
288 assertTrue("Search Bar did not peek.", waitForPanelToEnterState(PanelSta te.PEEKED));
289 }
290
291 /**
292 * Waits for the Search Panel to expand, and asserts that it did expand.
293 * @throws InterruptedException
294 */
295 private void waitForPanelToExpandAndAssert() throws InterruptedException {
296 assertTrue("Search Bar did not expand.", waitForPanelToEnterState(PanelS tate.EXPANDED));
297 }
298
299 /**
300 * Waits for the Search Panel to maximize, and asserts that it did maximize.
301 * @throws InterruptedException
302 */
303 private void waitForPanelToMaximizeAndAssert() throws InterruptedException {
304 assertTrue("Search Bar did not maximize.", waitForPanelToEnterState(Pane lState.MAXIMIZED));
305 }
306
307 /**
308 * Waits for the Search Panel to close, and asserts that it did close.
309 * @throws InterruptedException
310 */
311 private void waitForPanelToCloseAndAssert() throws InterruptedException {
312 // TODO(donnd): figure out why using waitForPanelToEnterState here doesn 't work.
313 assertTrue("Search Bar did not close.",
314 CriteriaHelper.pollForCriteria(new Criteria() {
315 @Override
316 public boolean isSatisfied() {
317 return !mManager.isSearchPanelShowing();
318 }
319 }, TEST_TIMEOUT, DEFAULT_POLLING_INTERVAL));
320 }
321
322 /**
323 * Waits for the Search Panel to enter the given {@code PanelState}.
324 * @throws InterruptedException
325 */
326 private boolean waitForPanelToEnterState(final PanelState state) throws Inte rruptedException {
327 return CriteriaHelper.pollForCriteria(new Criteria() {
328 @Override
329 public boolean isSatisfied() {
330 return mPanelDelegate != null
331 && mPanelDelegate.getPanelState() == state;
332 }
333 }, TEST_TIMEOUT, DEFAULT_POLLING_INTERVAL);
334 }
335
336 /**
337 * Waits for the manager to finish processing a gesture.
338 * Tells the manager that a gesture has started, and then waits for it to co mplete.
339 * @throws InterruptedException
340 */
341 private void waitForGestureProcessingAndAssert() throws InterruptedException {
342 assertTrue("Gesture processing did not complete.",
343 CriteriaHelper.pollForCriteria(new Criteria() {
344 @Override
345 public boolean isSatisfied() {
346 return !mSelectionController.wasAnyTapGestureDetected();
347 }
348 }, TEST_TIMEOUT, DEFAULT_POLLING_INTERVAL));
349 }
350
351 /**
352 * Shorthand for a common sequence:
353 * 1) Waits for gesture processing,
354 * 2) Waits for the panel to close,
355 * 3) Asserts that there is no selection and that the panel closed.
356 * @throws InterruptedException
357 */
358 private void waitForGestureToClosePanelAndAssertNoSelection() throws Interru ptedException {
359 waitForGestureProcessingAndAssert();
360 waitForPanelToCloseAndAssert();
361 assertPanelClosedOrUndefined();
362 assertNull(getSelectedText());
363 }
364
365 /**
366 * Waits for the selected text string to be the given string, and asserts.
367 * @param text The string to wait for the selection to become.
368 */
369 private void waitForSelectionToBe(final String text) throws InterruptedExcep tion {
370 assertTrue("Bar never showed desired text.",
371 CriteriaHelper.pollForCriteria(new Criteria() {
372 @Override
373 public boolean isSatisfied() {
374 return TextUtils.equals(text, getSelectedText());
375 }
376 }, TEST_TIMEOUT, DEFAULT_POLLING_INTERVAL));
377 }
378
379 /**
380 * A ContentViewCore that has some methods stubbed out for testing.
381 */
382 private static final class StubbedContentViewCore extends ContentViewCore {
383 private boolean mIsFocusedNodeEditable;
384
385 public StubbedContentViewCore(Context context) {
386 super(context);
387 }
388
389 public void setIsFocusedNodeEditableForTest(boolean isFocusedNodeEditabl e) {
390 mIsFocusedNodeEditable = isFocusedNodeEditable;
391 }
392
393 @Override
394 public boolean isFocusedNodeEditable() {
395 return mIsFocusedNodeEditable;
396 }
397 }
398
399 /**
400 * Generate a swipe sequence from the give start/end X,Y percentages, for th e given steps.
401 * Works in either landscape or portrait orientation.
402 */
403 private void swipe(float startX, float startY, float endX, float endY, int s tepCount) {
404 Point size = new Point();
405 getActivity().getWindowManager().getDefaultDisplay().getSize(size);
406 float dragStartX = size.x * startX;
407 float dragEndX = size.x * endX;
408 float dragStartY = size.y * startY;
409 float dragEndY = size.y * endY;
410 long downTime = SystemClock.uptimeMillis();
411 dragStart(dragStartX, dragStartY, downTime);
412 dragTo(dragStartX, dragEndX, dragStartY, dragEndY, stepCount, downTime);
413 dragEnd(dragEndX, dragEndY, downTime);
414 }
415
416 /**
417 * Swipes the panel up to it's expanded size.
418 */
419 private void swipePanelUp() {
420 swipe(0.5f, 0.95f, 0.5f, 0.55f, 1000);
421 }
422
423 /**
424 * Swipes the panel up to it's maximized size.
425 */
426 private void swipePanelUpToTop() {
427 swipe(0.5f, 0.95f, 0.5f, 0.05f, 1000);
428 }
429
430 /**
431 * Scrolls the base page.
432 */
433 private void scrollBasePage() {
434 swipe(0.f, 0.75f, 0.f, 0.7f, 100);
435 }
436
437 /**
438 * Taps the base page near the top.
439 */
440 private void tapBasePage() throws InterruptedException {
441 tapBasePage(0.1f, 0.1f);
442 }
443
444 /**
445 * Taps the base page at the given x, y position.
446 */
447 private void tapBasePage(float x, float y) throws InterruptedException {
448 Point size = new Point();
449 getActivity().getWindowManager().getDefaultDisplay().getSize(size);
450 x *= size.x;
451 y *= size.y;
452 singleClick(x, y);
453 waitForPanelToCloseAndAssert();
454 }
455
456 /**
457 * Click various places to cause the panel to show, expand, then close.
458 */
459 private void clickToExpandAndClosePanel() throws InterruptedException, Timeo utException {
460 clickWordNode("states");
461 tapPeekingBarToExpandAndAssert();
462 tapBasePage();
463 waitForPanelToCloseAndAssert();
464 }
465
466 /**
467 * Generate a click in the panel's bar.
468 * @barHeight The vertical position where the click should take place as a p ercentage
469 * of the screen size.
470 */
471 private void clickPanelBar(float barPositionVertical) {
472 Point size = new Point();
473 getActivity().getWindowManager().getDefaultDisplay().getSize(size);
474 float w = size.x;
475 float h = size.y;
476 boolean landscape = w > h;
477 float tapX = landscape ? w * barPositionVertical : w / 2f;
478 float tapY = landscape ? h / 2f : h * barPositionVertical;
479
480 singleClick(tapX, tapY);
481 }
482
483 /**
484 * Taps the peeking bar to expand the panel
485 */
486 private void tapPeekingBarToExpandAndAssert() throws InterruptedException {
487 clickPanelBar(0.95f);
488 waitForPanelToExpandAndAssert();
489 }
490
491 /**
492 * Simple sequence useful for checking if a Search Term Resolution request i s sent.
493 * Resets the fake server and clicks near to cause a search, then clicks far to let the panel
494 * drop down (taking us back to the same state).
495 */
496 private void clickToTriggerSearchTermResolution()
497 throws InterruptedException, TimeoutException {
498 mFakeServer.reset();
499 clickWordNode("states");
500 clickNode("states-far");
501 waitForPanelToCloseAndAssert();
502 }
503
504 /**
505 * Simple sequence useful for checking if a Search Request is prefetched.
506 * Resets the fake server and clicks near to cause a search, fakes a server response to
507 * trigger a prefetch, then clicks far to let the panel drop down
508 * which takes us back to the starting state except the the fake server know s
509 * if a prefetch occurred.
510 */
511 private void clickToTriggerPrefetch() throws InterruptedException, TimeoutEx ception {
512 mFakeServer.reset();
513 clickWordNode("states");
514 assertSearchTermRequested();
515 fakeResponse(false, 200, "States", "display-text", "alternate-term", fal se);
516 waitForPanelToPeekAndAssert();
517 clickNode("states-far");
518 }
519
520 /**
521 * Simple sequence to click, resolve, and prefetch. Verifies a prefetch occ urred.
522 */
523 private void clickToResolveAndAssertPrefetch() throws InterruptedException, TimeoutException {
524 clickWordNode("states");
525 assertLoadedNoUrl();
526 assertSearchTermRequested();
527
528 fakeResponse(false, 200, "states", "United States Intelligence", "altern ate-term", false);
529 waitForPanelToPeekAndAssert();
530 assertLoadedLowPriorityUrl();
531 assertContainsParameters("states", "alternate-term");
532 }
533
534 /**
535 * Resets the tap counters on the UI thread.
536 */
537 private void resetTapCounters() throws InterruptedException {
538 ThreadUtils.runOnUiThread(new Runnable() {
539 @Override
540 public void run() {
541 mPolicy.resetTapCounters();
542 // The "Promo" tap counter is never reset outside of testing bec ause it
543 // is used to persistently count the number of peeks *ever* seen by the user
544 // before the first open, and is then frozen in a disabled state to record that
545 // value rather than being reset.
546 // We reset it here to simulate a new user for our feature.
547 mPreferenceManager.setContextualSearchTapTriggeredPromoCount(0);
548 }
549 });
550 CriteriaHelper.pollForCriteria(new Criteria() {
551 @Override
552 public boolean isSatisfied() {
553 return mPolicy.didResetTapCounters();
554 }
555 }, TEST_TIMEOUT, DEFAULT_POLLING_INTERVAL);
556 }
557
558 /**
559 * Tests whether the contextual search panel hides when omnibox is clicked.
560 */
561 @SmallTest
562 @Feature({"ContextualSearch"})
563 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
564 public void testHidesWhenOmniboxFocused() throws InterruptedException, Timeo utException {
565 clickWordNode("intelligence");
566
567 assertEquals("Intelligence", mFakeServer.getSearchTermRequested());
568 fakeResponse(false, 200, "Intelligence", "display-text", "alternate-term ", false);
569 assertContainsParameters("Intelligence", "alternate-term");
570 waitForPanelToPeekAndAssert();
571
572 OmniboxTestUtils.toggleUrlBarFocus((UrlBar) getActivity().findViewById(R .id.url_bar), true);
573
574 assertPanelClosedOrUndefined();
575 }
576
577 /**
578 * Tests the doesContainAWord method.
579 */
580 @SmallTest
581 @Feature({"ContextualSearch"})
582 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
583 public void testDoesContainAWord() {
584 assertTrue(mManager.doesContainAWord("word"));
585 assertTrue(mManager.doesContainAWord("word "));
586 assertFalse("Emtpy string should not be considered a word!",
587 mManager.doesContainAWord(""));
588 assertFalse("Special symbols should not be considered a word!",
589 mManager.doesContainAWord("@"));
590 assertFalse("White space should not be considered a word",
591 mManager.doesContainAWord(" "));
592 assertTrue(mManager.doesContainAWord("Q2"));
593 assertTrue(mManager.doesContainAWord("123"));
594 }
595
596 /**
597 * Tests the isValidSelection method.
598 */
599 @SmallTest
600 @Feature({"ContextualSearch"})
601 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
602 public void testIsValidSelection() {
603 StubbedContentViewCore stubbedCvc = new StubbedContentViewCore(
604 getActivity().getBaseContext());
605 assertTrue(mManager.isValidSelection("valid", stubbedCvc));
606 assertFalse(mManager.isValidSelection(" ", stubbedCvc));
607 stubbedCvc.setIsFocusedNodeEditableForTest(true);
608 assertFalse(mManager.isValidSelection("editable", stubbedCvc));
609 stubbedCvc.setIsFocusedNodeEditableForTest(false);
610 String numberString = "0123456789";
611 StringBuilder longStringBuilder = new StringBuilder();
612 for (int i = 0; i < 11; i++) {
613 longStringBuilder.append(numberString);
614 }
615 assertTrue(mManager.isValidSelection(numberString, stubbedCvc));
616 assertFalse(mManager.isValidSelection(longStringBuilder.toString(),
617 stubbedCvc));
618 }
619
620 /**
621 * Tests a simple Tap.
622 */
623 @SmallTest
624 @Feature({"ContextualSearch"})
625 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
626 public void testTap() throws InterruptedException, TimeoutException {
627 clickWordNode("intelligence");
628
629 assertEquals("Intelligence", mFakeServer.getSearchTermRequested());
630 fakeResponse(false, 200, "Intelligence", "display-text", "alternate-term ", false);
631 assertContainsParameters("Intelligence", "alternate-term");
632 waitForPanelToPeekAndAssert();
633 assertLoadedLowPriorityUrl();
634 }
635
636 /**
637 * Tests a simple Long-Press gesture, without opening the panel.
638 */
639 @SmallTest
640 @Feature({"ContextualSearch"})
641 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
642 public void testLongPress() throws InterruptedException, TimeoutException {
643 longPressNode("states");
644
645 assertNull(mFakeServer.getSearchTermRequested());
646 waitForPanelToPeekAndAssert();
647 assertLoadedNoUrl();
648 assertNoContentViewCore();
649 }
650
651 /**
652 * Tests swiping the overlay open, after an initial tap that activates the p eeking card.
653 */
654 @SmallTest
655 @Feature({"ContextualSearch"})
656 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
657 public void testSwipeExpand() throws InterruptedException, TimeoutException {
658 assertNoSearchesLoaded();
659 clickWordNode("intelligence");
660 assertNoSearchesLoaded();
661
662 // Fake a search term resolution response.
663 fakeResponse(false, 200, "Intelligence", "United States Intelligence", " alternate-term",
664 false);
665 assertContainsParameters("Intelligence", "alternate-term");
666 assertEquals(1, mFakeServer.loadedUrlCount());
667 assertLoadedLowPriorityUrl();
668
669 waitForPanelToPeekAndAssert();
670 swipePanelUp();
671 waitForPanelToExpandAndAssert();
672 assertEquals(1, mFakeServer.loadedUrlCount());
673 assertLoadedLowPriorityUrl();
674 }
675
676 /**
677 * Tests swiping the overlay open, after an initial long-press that activate s the peeking card,
678 * followed by closing the panel.
679 */
680 @SmallTest
681 @Feature({"ContextualSearch"})
682 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
683 public void testLongPressSwipeExpand() throws InterruptedException, TimeoutE xception {
684 longPressNode("intelligence");
685 assertNoContentViewCore();
686
687 // Fake a search term resolution response.
688 fakeResponse(false, 200, "Intelligence", "United States Intelligence", " alternate-term",
689 false);
690 assertContainsParameters("Intelligence", "alternate-term");
691
692 waitForPanelToPeekAndAssert();
693 assertLoadedNoUrl();
694 assertNoContentViewCore();
695 swipePanelUp();
696 waitForPanelToExpandAndAssert();
697 assertContentViewCoreCreated();
698 assertLoadedNormalPriorityUrl();
699 assertEquals(1, mFakeServer.loadedUrlCount());
700
701 // tap the base page to close.
702 tapBasePage();
703 waitForPanelToCloseAndAssert();
704 assertEquals(1, mFakeServer.loadedUrlCount());
705 assertNoContentViewCore();
706 }
707
708 /**
709 * Tests a sequence in landscape orientation: swiping the overlay open, afte r an
710 * initial tap that activates the peeking card.
711 */
712 @SmallTest
713 @Feature({"ContextualSearch"})
714 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
715 public void testSwipeExpandLandscape() throws InterruptedException, TimeoutE xception {
716 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LA NDSCAPE);
717 testSwipeExpand();
718 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PO RTRAIT);
719 }
720
721 /**
722 * Tests tap to expand, after an initial tap to activate the peeking card.
723 */
724 @SmallTest
725 @Feature({"ContextualSearch"})
726 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
727 public void testTapExpand() throws InterruptedException, TimeoutException {
728 assertNoSearchesLoaded();
729 clickWordNode("states");
730 assertNoContentViewCore();
731 assertNoSearchesLoaded();
732
733 // Fake a search term resolution response.
734 fakeResponse(false, 200, "states", "United States Intelligence", "altern ate-term", false);
735 assertContainsParameters("states", "alternate-term");
736 assertEquals(1, mFakeServer.loadedUrlCount());
737 assertLoadedLowPriorityUrl();
738 assertContentViewCoreCreated();
739 tapPeekingBarToExpandAndAssert();
740 assertLoadedLowPriorityUrl();
741 assertEquals(1, mFakeServer.loadedUrlCount());
742
743 // tap the base page to close.
744 tapBasePage();
745 waitForPanelToCloseAndAssert();
746 assertEquals(1, mFakeServer.loadedUrlCount());
747 assertNoContentViewCore();
748 }
749
750 /**
751 * Tests that only a single low-priority request is issued for a Tap/Open se quence.
752 */
753 @SmallTest
754 @Feature({"ContextualSearch"})
755 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
756 public void testTapCausesOneLowPriorityRequest() throws InterruptedException , TimeoutException {
757 mFakeServer.reset();
758 clickWordNode("states");
759
760 // We should not make a second-request until we get a good response from the first-request.
761 assertLoadedNoUrl();
762 assertEquals(0, mFakeServer.loadedUrlCount());
763 fakeResponse(false, 200, "states", "United States Intelligence", "altern ate-term", false);
764 assertLoadedLowPriorityUrl();
765 assertEquals(1, mFakeServer.loadedUrlCount());
766
767 // When the second request succeeds, we should not issue a new request.
768 fakeContentViewDidNavigate(200);
769 assertLoadedLowPriorityUrl();
770 assertEquals(1, mFakeServer.loadedUrlCount());
771
772 // When the bar opens, we should not make any additional request.
773 tapPeekingBarToExpandAndAssert();
774 assertLoadedLowPriorityUrl();
775 assertEquals(1, mFakeServer.loadedUrlCount());
776 assertLoadedLowPriorityUrl();
777 }
778
779 /**
780 * Tests that a failover for a prefetch request is issued after the panel is opened.
781 */
782 @SmallTest
783 @Feature({"ContextualSearch"})
784 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
785 public void testPrefetchFailoverRequestMadeAfterOpen()
786 throws InterruptedException, TimeoutException {
787 mFakeServer.reset();
788 clickWordNode("states");
789
790 // We should not make a SERP request until we get a good response from t he resolve request.
791 assertLoadedNoUrl();
792 assertEquals(0, mFakeServer.loadedUrlCount());
793 fakeResponse(false, 200, "states", "United States Intelligence", "altern ate-term", false);
794 assertLoadedLowPriorityUrl();
795 assertEquals(1, mFakeServer.loadedUrlCount());
796
797 // When the second request fails, we should not issue a new request.
798 fakeContentViewDidNavigate(500);
799 assertLoadedLowPriorityUrl();
800 assertEquals(1, mFakeServer.loadedUrlCount());
801
802 // Once the bar opens, we make a new request at normal priority.
803 tapPeekingBarToExpandAndAssert();
804 assertLoadedNormalPriorityUrl();
805 assertEquals(2, mFakeServer.loadedUrlCount());
806 }
807
808 /**
809 * Tests a simple Tap with disable-preload set.
810 */
811 @SmallTest
812 @Feature({"ContextualSearch"})
813 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
814 public void testTapDisablePreload() throws InterruptedException, TimeoutExce ption {
815 clickWordNode("intelligence");
816
817 assertSearchTermRequested();
818 fakeResponse(false, 200, "Intelligence", "display-text", "alternate-term ", true);
819 assertContainsNoParameters();
820 waitForPanelToPeekAndAssert();
821 assertLoadedNoUrl();
822 }
823
824 /**
825 * Tests that long-press selects text, and a subsequent tap will unselect te xt.
826 * @SmallTest
827 * @Feature({"ContextualSearch"})
828 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
829 */
830 @SmallTest
831 @Feature({"ContextualSearch"})
832 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
833 public void testLongPressGestureSelects() throws InterruptedException, Timeo utException {
834 longPressNode("intelligence");
835 assertEquals("Intelligence", getSelectedText());
836 fakeResponse(false, 200, "Intelligence", "Intelligence", "alternate-term ", false);
837 assertContainsParameters("Intelligence", "alternate-term");
838 waitForPanelToPeekAndAssert();
839 assertLoadedNoUrl(); // No load after long-press until opening panel.
840 clickNode("question-mark");
841 waitForGestureProcessingAndAssert();
842 assertNull(getSelectedText());
843 assertPanelClosedOrUndefined();
844 assertLoadedNoUrl();
845 }
846
847 /**
848 * Tests that a Tap gesture selects the expected text.
849 */
850 @SmallTest
851 @Feature({"ContextualSearch"})
852 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
853 public void testTapGestureSelects() throws InterruptedException, TimeoutExce ption {
854 clickWordNode("intelligence");
855 assertEquals("Intelligence", getSelectedText());
856 fakeResponse(false, 200, "Intelligence", "Intelligence", "alternate-term ", false);
857 assertContainsParameters("Intelligence", "alternate-term");
858 waitForPanelToPeekAndAssert();
859 assertLoadedLowPriorityUrl();
860 clickNode("question-mark");
861 waitForGestureProcessingAndAssert();
862 assertNull(getSelectedText());
863 }
864
865 /**
866 * Tests that a Tap gesture on a special character does not select or show t he panel.
867 */
868 @SmallTest
869 @Feature({"ContextualSearch"})
870 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
871 public void testTapGestureOnSpecialCharacterDoesntSelect()
872 throws InterruptedException, TimeoutException {
873 clickNode("question-mark");
874 waitForGestureProcessingAndAssert();
875 assertNull(getSelectedText());
876 assertPanelClosedOrUndefined();
877 assertLoadedNoUrl();
878 }
879
880 /**
881 * Tests that a Tap gesture followed by scrolling clears the selection.
882 */
883 @SmallTest
884 @Feature({"ContextualSearch"})
885 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
886 public void testTapGestureFollowedByScrollClearsSelection()
887 throws InterruptedException, TimeoutException {
888 clickWordNode("intelligence");
889 fakeResponse(false, 200, "Intelligence", "Intelligence", "alternate-term ", false);
890 assertContainsParameters("Intelligence", "alternate-term");
891 waitForPanelToPeekAndAssert();
892 assertLoadedLowPriorityUrl();
893 scrollBasePage();
894 assertPanelClosedOrUndefined();
895 assertNull(mSelectionController.getSelectedText());
896 }
897
898 /**
899 * Tests that a Tap gesture followed by tapping an invalid character doesn't select.
900 */
901 @SmallTest
902 @Feature({"ContextualSearch"})
903 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
904 public void testTapGestureFollowedByInvalidTextTapCloses()
905 throws InterruptedException, TimeoutException {
906 clickWordNode("states-far");
907 waitForPanelToPeekAndAssert();
908 clickNode("question-mark");
909 waitForGestureProcessingAndAssert();
910 assertPanelClosedOrUndefined();
911 assertNull(mSelectionController.getSelectedText());
912 }
913
914 /**
915 * Tests that a Tap gesture followed by tapping a non-text character doesn't select.
916 */
917 @SmallTest
918 @Feature({"ContextualSearch"})
919 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
920 public void testTapGestureFollowedByNonTextTap()
921 throws InterruptedException, TimeoutException {
922 clickWordNode("states-far");
923 waitForPanelToPeekAndAssert();
924 clickNode("button");
925 waitForGestureProcessingAndAssert();
926 assertPanelClosedOrUndefined();
927 assertNull(mSelectionController.getSelectedText());
928 }
929
930 /**
931 * Tests that a Tap gesture far away toggles selecting text.
932 */
933 @SmallTest
934 @Feature({"ContextualSearch"})
935 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
936 public void testTapGestureFarAwayTogglesSelecting()
937 throws InterruptedException, TimeoutException {
938 clickWordNode("states");
939 assertEquals("States", getSelectedText());
940 waitForPanelToPeekAndAssert();
941 clickNode("states-far");
942 waitForGestureProcessingAndAssert();
943 assertNull(getSelectedText());
944 assertPanelClosedOrUndefined();
945 clickNode("states-far");
946 waitForGestureProcessingAndAssert();
947 waitForPanelToPeekAndAssert();
948 assertEquals("States", getSelectedText());
949 }
950
951 /**
952 * Tests that sequential Tap gestures nearby keep selecting.
953 */
954 @SmallTest
955 @Feature({"ContextualSearch"})
956 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
957 public void testTapGesturesNearbyKeepSelecting()
958 throws InterruptedException, TimeoutException {
959 clickWordNode("states");
960 assertEquals("States", getSelectedText());
961 waitForPanelToPeekAndAssert();
962 // Because sequential taps never hide the bar, we we can't wait for it t o peek.
963 // Instead we use clickNode (which doesn't wait) instead of clickWordNod e and wait
964 // for the selection to change.
965 clickNode("states-near");
966 waitForSelectionToBe("StatesNear");
967 clickNode("states");
968 waitForSelectionToBe("States");
969 }
970
971 /**
972 * Tests that a long-press gesture followed by scrolling does not clear the selection.
973 * @SmallTest
974 * @Feature({"ContextualSearch"})
975 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
976 */
977 @SmallTest
978 @Feature({"ContextualSearch"})
979 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
980 public void testLongPressGestureFollowedByScrollMaintainsSelection()
981 throws InterruptedException, TimeoutException {
982 longPressNode("intelligence");
983 waitForPanelToPeekAndAssert();
984 scrollBasePage();
985 assertPanelClosedOrUndefined();
986 assertEquals("Intelligence", getSelectedText());
987 assertLoadedNoUrl();
988 }
989
990 /**
991 * Tests that a long-press gesture followed by a tap does not select.
992 * @SmallTest
993 * @Feature({"ContextualSearch"})
994 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
995 */
996 @SmallTest
997 @Feature({"ContextualSearch"})
998 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
999 public void testLongPressGestureFollowedByTapDoesntSelect()
1000 throws InterruptedException, TimeoutException {
1001 longPressNode("intelligence");
1002 waitForPanelToPeekAndAssert();
1003 clickWordNode("states-far");
1004 waitForGestureToClosePanelAndAssertNoSelection();
1005 assertLoadedNoUrl();
1006 }
1007
1008 /**
1009 * Tests that the panel closes when its base page crashes.
1010 */
1011 @SmallTest
1012 @Feature({"ContextualSearch"})
1013 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1014 public void testContextualSearchDismissedOnForegroundTabCrash()
1015 throws InterruptedException, TimeoutException {
1016 clickWordNode("states");
1017 assertEquals("States", getSelectedText());
1018 waitForPanelToPeekAndAssert();
1019
1020 ThreadUtils.runOnUiThread(new Runnable() {
1021 @Override
1022 public void run() {
1023 getActivity().getActivityTab().simulateRendererKilledForTesting( true);
1024 }
1025 });
1026
1027 // Give the panelState time to change
1028 CriteriaHelper.pollForCriteria(new Criteria(){
1029 @Override
1030 public boolean isSatisfied() {
1031 PanelState panelState = mPanelDelegate.getPanelState();
1032 return panelState != PanelState.PEEKED;
1033 }
1034 });
1035
1036 assertPanelClosedOrUndefined();
1037 }
1038
1039 /**
1040 * Test the the panel does not close when some background tab crashes.
1041 */
1042 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE)
1043 @SmallTest
1044 @Feature({"ContextualSearch"})
1045 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1046 public void testContextualSearchNotDismissedOnBackgroundTabCrash()
1047 throws InterruptedException, TimeoutException {
1048 ChromeTabUtils.newTabFromMenu(getInstrumentation(),
1049 (ChromeTabbedActivity) getActivity());
1050 final Tab tab2 = TabModelUtils.getCurrentTab(getActivity().getCurrentTab Model());
1051
1052 ThreadUtils.runOnUiThread(new Runnable() {
1053 @Override
1054 public void run() {
1055 TabModelUtils.setIndex(getActivity().getCurrentTabModel(), 0);
1056 }
1057 });
1058 getInstrumentation().waitForIdleSync();
1059
1060 clickWordNode("states");
1061 assertEquals("States", getSelectedText());
1062 waitForPanelToPeekAndAssert();
1063
1064 ThreadUtils.runOnUiThread(new Runnable() {
1065 @Override
1066 public void run() {
1067 tab2.simulateRendererKilledForTesting(false);
1068 }
1069 });
1070
1071 // Give the panelState time to change
1072 CriteriaHelper.pollForCriteria(new Criteria(){
1073 @Override
1074 public boolean isSatisfied() {
1075 PanelState panelState = mPanelDelegate.getPanelState();
1076 return panelState != PanelState.PEEKED;
1077 }
1078 });
1079
1080 waitForPanelToPeekAndAssert();
1081 }
1082
1083 /*
1084 * Test that tapping on the Search Bar before having a resolved search term does not
1085 * promote to a tab, and that after the resolution it does promote to a tab.
1086 *
1087 * @SmallTest
1088 * @Feature({"ContextualSearch"})
1089 */
1090 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1091 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE)
1092 @FlakyTest
1093 public void testTapSearchBarPromotesToTab() throws InterruptedException, Tim eoutException {
1094 // Tap on a word.
1095 clickWordNode("intelligence");
1096 assertSearchTermRequested();
1097
1098 // Wait for the panel to peek.
1099 waitForPanelToPeekAndAssert();
1100
1101 // Swipe Panel up and wait for it to maximize.
1102 swipePanelUpToTop();
1103 waitForPanelToMaximizeAndAssert();
1104
1105 // Create an observer to track that a new tab is created.
1106 final CallbackHelper tabCreatedHelper = new CallbackHelper();
1107 int tabCreatedHelperCallCount = tabCreatedHelper.getCallCount();
1108 TabModelSelectorObserver observer = new EmptyTabModelSelectorObserver() {
1109 @Override
1110 public void onNewTabCreated(Tab tab) {
1111 tabCreatedHelper.notifyCalled();
1112 }
1113 };
1114 getActivity().getTabModelSelector().addObserver(observer);
1115
1116 // Tap the Search Bar.
1117 clickPanelBar(0.05f);
1118
1119 // The Search Term Resolution response hasn't arrived yet, so the Panel should not
1120 // be promoted. Therefore, we are asserting that the Panel is still maxi mized.
1121 waitForPanelToMaximizeAndAssert();
1122
1123 // Get a Search Term Resolution response.
1124 fakeResponse(false, 200, "Intelligence", "display-text", "alternate-term ", false);
1125 assertContainsParameters("Intelligence", "alternate-term");
1126
1127 // Tap the Search Bar again.
1128 clickPanelBar(0.05f);
1129
1130 // Now that the response has arrived, tapping on the Search Panel should promote it
1131 // to a Tab. Therefore, we are asserting that the Panel got closed.
1132 waitForPanelToCloseAndAssert();
1133
1134 // Finally, make sure a tab was created.
1135 if (!FeatureUtilities.isDocumentMode(getInstrumentation().getContext())) {
1136 // TODO(donnd): figure out how to check for tab creation in Document mode.
1137 tabCreatedHelper.waitForCallback(tabCreatedHelperCallCount);
1138 }
1139 getActivity().getTabModelSelector().removeObserver(observer);
1140 }
1141
1142 /**
1143 * Tests that a Tap gesture on an element with an ARIA role does not trigger .
1144 */
1145 @SmallTest
1146 @Feature({"ContextualSearch"})
1147 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1148 public void testTapOnRoleIgnored() throws InterruptedException, TimeoutExcep tion {
1149 clickNode("role");
1150 waitForGestureToClosePanelAndAssertNoSelection();
1151 }
1152
1153 /**
1154 * Tests that a Tap gesture on an element with an ARIA attribute does not tr igger.
1155 */
1156 @SmallTest
1157 @Feature({"ContextualSearch"})
1158 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1159 public void testTapOnARIAIgnored() throws InterruptedException, TimeoutExcep tion {
1160 clickNode("aria");
1161 waitForGestureToClosePanelAndAssertNoSelection();
1162 }
1163
1164 /**
1165 * Tests that a Tap gesture on an element that is focusable does not trigger .
1166 */
1167 @SmallTest
1168 @Feature({"ContextualSearch"})
1169 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1170 public void testTapOnFocusableIgnored() throws InterruptedException, Timeout Exception {
1171 clickNode("focusable");
1172 waitForGestureToClosePanelAndAssertNoSelection();
1173 }
1174
1175 /**
1176 * Tests that taps can be resolve-limited for decided users.
1177 * @CommandLineFlags.Add(
1178 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_RESOLVE_LIMIT_FO R_DECIDED + "=2")
1179 * @SmallTest
1180 * @Feature({"ContextualSearch"})
1181 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
1182 * crbug.com/487759
1183 */
1184 @FlakyTest
1185 public void testTapResolveLimitForDecided() throws InterruptedException, Tim eoutException {
1186 clickToTriggerSearchTermResolution();
1187 assertSearchTermRequested();
1188 clickToTriggerSearchTermResolution();
1189 assertSearchTermRequested();
1190 // 3rd click should not resolve.
1191 clickToTriggerSearchTermResolution();
1192 assertSearchTermNotRequested();
1193
1194 // Expanding the panel should reset the limit.
1195 clickToExpandAndClosePanel();
1196
1197 // Click should resolve again.
1198 clickToTriggerSearchTermResolution();
1199 assertSearchTermRequested();
1200 }
1201
1202 /**
1203 * Tests that taps can be resolve-limited for undecided users.
1204 * @CommandLineFlags.Add({
1205 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_RESOLVE_LIMIT_FO R_UNDECIDED + "=2"})
1206 * @SmallTest
1207 * @Feature({"ContextualSearch"})
1208 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
1209 * crbug.com/487759
1210 */
1211 @FlakyTest
1212 public void testTapResolveLimitForUndecided() throws InterruptedException, T imeoutException {
1213 mPolicy.overrideDecidedStateForTesting(false);
1214
1215 clickToTriggerSearchTermResolution();
1216 assertSearchTermRequested();
1217 clickToTriggerSearchTermResolution();
1218 assertSearchTermRequested();
1219 // 3rd click should not resolve.
1220 clickToTriggerSearchTermResolution();
1221 assertSearchTermNotRequested();
1222
1223 // Expanding the panel should reset the limit.
1224 clickToExpandAndClosePanel();
1225
1226 // Click should resolve again.
1227 clickToTriggerSearchTermResolution();
1228 assertSearchTermRequested();
1229 }
1230
1231 /**
1232 * Tests that taps can be preload-limited for decided users.
1233 * @CommandLineFlags.Add(
1234 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_PREFETCH_LIMIT_F OR_DECIDED + "=2")
1235 * @SmallTest
1236 * @Feature({"ContextualSearch"})
1237 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
1238 * crbug.com/487759
1239 */
1240 @FlakyTest
1241 public void testTapPrefetchLimitForDecided() throws InterruptedException, Ti meoutException {
1242 clickToTriggerPrefetch();
1243 assertLoadedLowPriorityUrl();
1244 clickToTriggerPrefetch();
1245 assertLoadedLowPriorityUrl();
1246 // 3rd click should not preload.
1247 clickToTriggerPrefetch();
1248 assertLoadedNoUrl();
1249
1250 // Expanding the panel should reset the limit.
1251 clickToExpandAndClosePanel();
1252
1253 // Click should preload again.
1254 clickToTriggerPrefetch();
1255 assertLoadedLowPriorityUrl();
1256 }
1257
1258 /**
1259 * Tests that taps can be preload-limited for undecided users.
1260 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
1261 * @CommandLineFlags.Add(
1262 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_PREFETCH_LIMIT_F OR_UNDECIDED + "=2")
1263 * @SmallTest
1264 * @Feature({"ContextualSearch"})
1265 * crbug.com/487759
1266 */
1267 @FlakyTest
1268 public void testTapPrefetchLimitForUndecided() throws InterruptedException, TimeoutException {
1269 mPolicy.overrideDecidedStateForTesting(false);
1270
1271 clickToTriggerPrefetch();
1272 assertLoadedLowPriorityUrl();
1273 clickToTriggerPrefetch();
1274 assertLoadedLowPriorityUrl();
1275 // 3rd click should not preload.
1276 clickToTriggerPrefetch();
1277 assertLoadedNoUrl();
1278
1279 // Expanding the panel should reset the limit.
1280 clickToExpandAndClosePanel();
1281
1282 // Click should preload again.
1283 clickToTriggerPrefetch();
1284 assertLoadedLowPriorityUrl();
1285 }
1286
1287 /**
1288 * Tests the tap triggered promo limit for opt-out.
1289 * @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE })
1290 * @CommandLineFlags.Add({
1291 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_PROMO_ON_LIMITED_TAP S + "=true",
1292 * ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_TRIGGERED_PROMO_ LIMIT + "=2"})
1293 * @SmallTest
1294 * @Feature({"ContextualSearch"})
1295 * crbug.com/487759
1296 */
1297 @FlakyTest
1298 public void testTapTriggeredPromoLimitForOptOut()
1299 throws InterruptedException, TimeoutException {
1300 mPolicy.overrideDecidedStateForTesting(false);
1301
1302 clickWordNode("states");
1303 clickNode("states-far");
1304 waitForPanelToCloseAndAssert();
1305 clickWordNode("states");
1306 clickNode("states-far");
1307 waitForPanelToCloseAndAssert();
1308
1309 // 3rd click won't peek the panel.
1310 clickNode("states");
1311 assertPanelClosedOrUndefined();
1312 // The Tap should not select any text either!
1313 assertNull(getSelectedText());
1314
1315 // A long-press should still show the promo bar.
1316 longPressNode("states");
1317 waitForPanelToPeekAndAssert();
1318
1319 // Expanding the panel should deactivate the limit.
1320 clickToExpandAndClosePanel();
1321
1322 // Three taps should work now.
1323 clickWordNode("states");
1324 clickNode("states-far");
1325 waitForPanelToCloseAndAssert();
1326 clickWordNode("states");
1327 clickNode("states-far");
1328 waitForPanelToCloseAndAssert();
1329 clickWordNode("states");
1330 clickNode("states-far");
1331 waitForPanelToCloseAndAssert();
1332 }
1333
1334 /**
1335 * This is a test that happens to create a separate bar without any content area!
1336 *
1337 * @SmallTest
1338 * @Feature({"ContextualSearch"})
1339 */
1340 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1341 @FlakyTest
1342 @CommandLineFlags.Add(
1343 ContextualSearchFieldTrial.CONTEXTUAL_SEARCH_TAP_PREFETCH_LIMIT_FOR_ DECIDED + "=2")
1344 public void testDisembodiedBar() throws InterruptedException, TimeoutExcepti on {
1345 clickToTriggerPrefetch();
1346 assertLoadedLowPriorityUrl();
1347 clickToTriggerPrefetch();
1348 assertLoadedLowPriorityUrl();
1349 // 3rd click should not preload.
1350 clickToTriggerPrefetch();
1351 assertLoadedNoUrl();
1352
1353 // Expanding the panel should reset the limit.
1354 swipePanelUp();
1355 singleClick(0.5f, 0.5f);
1356 waitForPanelToCloseAndAssert();
1357
1358 // Click should preload again.
1359 clickToTriggerPrefetch();
1360 assertLoadedLowPriorityUrl();
1361 }
1362
1363 /**
1364 * Tests expanding the panel before the search term has resolved, verifies t hat nothing
1365 * loads until the resolve completes and that it's now a normal priority URL .
1366 */
1367 @SmallTest
1368 @Feature({"ContextualSearch"})
1369 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1370 public void testExpandBeforeSearchTermResolution()
1371 throws InterruptedException, TimeoutException {
1372 clickWordNode("states");
1373 assertNoContentViewCore();
1374
1375 // Expanding before the search term resolves should not load anything.
1376 tapPeekingBarToExpandAndAssert();
1377 assertLoadedNoUrl();
1378
1379 // Once the response comes in, it should load.
1380 fakeResponse(false, 200, "states", "United States Intelligence", "altern ate-term", false);
1381 assertContainsParameters("states", "alternate-term");
1382 assertLoadedNormalPriorityUrl();
1383 assertContentViewCoreCreated();
1384 }
1385
1386 /**
1387 * Tests that an error from the Search Term Resolution request causes a fall back to a
1388 * search request for the literal selection.
1389 */
1390 @SmallTest
1391 @Feature({"ContextualSearch"})
1392 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1393 public void testSearchTermResolutionError() throws InterruptedException, Tim eoutException {
1394 clickWordNode("states");
1395 assertSearchTermRequested();
1396 fakeResponse(false, 403, "", "", "", false);
1397 assertLoadedNoUrl();
1398 tapPeekingBarToExpandAndAssert();
1399 assertLoadedNormalPriorityUrl();
1400 }
1401
1402 // ------------------------------------------------------------------------- -------------------
1403 // HTTP/HTTPS for Undecided/Decided users.
1404 // ------------------------------------------------------------------------- -------------------
1405
1406 /**
1407 * Tests that HTTPS does not resolve in the opt-out model before the user ac cepts.
1408 */
1409 @SmallTest
1410 @Feature({"ContextualSearch"})
1411 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1412 public void testHttpsBeforeAcceptForOptOut() throws InterruptedException, Ti meoutException {
1413 mPolicy.overrideDecidedStateForTesting(false);
1414 mFakeServer.setShouldUseHttps(true);
1415
1416 clickWordNode("states");
1417 assertLoadedLowPriorityUrl();
1418 assertSearchTermNotRequested();
1419 }
1420
1421 /**
1422 * Tests that HTTPS does resolve in the opt-out model after the user accepts .
1423 */
1424 @SmallTest
1425 @Feature({"ContextualSearch"})
1426 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1427 public void testHttpsAfterAcceptForOptOut() throws InterruptedException, Tim eoutException {
1428 mPolicy.overrideDecidedStateForTesting(true);
1429 mFakeServer.setShouldUseHttps(true);
1430
1431 clickToResolveAndAssertPrefetch();
1432 }
1433
1434 /**
1435 * Tests that HTTP does resolve in the opt-out model before the user accepts .
1436 */
1437 @SmallTest
1438 @Feature({"ContextualSearch"})
1439 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1440 public void testHttpBeforeAcceptForOptOut() throws InterruptedException, Tim eoutException {
1441 mPolicy.overrideDecidedStateForTesting(false);
1442
1443 clickToResolveAndAssertPrefetch();
1444 }
1445
1446 /**
1447 * Tests that HTTP does resolve in the opt-out model after the user accepts.
1448 */
1449 @SmallTest
1450 @Feature({"ContextualSearch"})
1451 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1452 public void testHttpAfterAcceptForOptOut() throws InterruptedException, Time outException {
1453 mPolicy.overrideDecidedStateForTesting(true);
1454
1455 clickToResolveAndAssertPrefetch();
1456 }
1457
1458 // ------------------------------------------------------------------------- -------------------
1459 // App Menu Suppression
1460 // ------------------------------------------------------------------------- -------------------
1461
1462 /**
1463 * Simulates pressing the App Menu button.
1464 */
1465 private void pressAppMenuKey() {
1466 pressKey(KeyEvent.KEYCODE_MENU);
1467 }
1468
1469 /**
1470 * Asserts whether the App Menu is visible.
1471 */
1472 private void assertAppMenuVisibility(final boolean isVisible) throws Interru ptedException {
1473 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
1474 @Override
1475 public boolean isSatisfied() {
1476 if (((CompositorChromeActivity) getActivity())
1477 .getAppMenuHandler().isAppMenuShowing() == isVisible) re turn true;
1478 return false;
1479 }
1480 }));
1481 }
1482
1483 /**
1484 * Tests that the App Menu gets suppressed when Search Panel is expanded.
1485 */
1486 @SmallTest
1487 @Feature({"ContextualSearch"})
1488 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1489 public void testAppMenuSuppressedWhenExpanded() throws InterruptedException, TimeoutException {
1490 clickWordNode("states");
1491 tapPeekingBarToExpandAndAssert();
1492
1493 pressAppMenuKey();
1494 assertAppMenuVisibility(false);
1495
1496 tapBasePage();
1497 waitForPanelToCloseAndAssert();
1498
1499 pressAppMenuKey();
1500 assertAppMenuVisibility(true);
1501 }
1502
1503 /**
1504 * Tests that the App Menu gets suppressed when Search Panel is maximized.
1505 */
1506 @SmallTest
1507 @Feature({"ContextualSearch"})
1508 @Restriction({RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
1509 public void testAppMenuSuppressedWhenMaximized() throws InterruptedException , TimeoutException {
1510 clickWordNode("states");
1511 swipePanelUpToTop();
1512 waitForPanelToMaximizeAndAssert();
1513
1514 pressAppMenuKey();
1515 assertAppMenuVisibility(false);
1516
1517 pressBackButton();
1518 waitForPanelToCloseAndAssert();
1519
1520 pressAppMenuKey();
1521 assertAppMenuVisibility(true);
1522 }
1523 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698