OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.contextualsearch; | 5 package org.chromium.chrome.browser.contextualsearch; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.net.Uri; | 8 import android.net.Uri; |
9 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
10 import android.widget.LinearLayout; | 10 import android.widget.LinearLayout; |
11 | 11 |
12 import org.junit.Assert; | 12 import org.junit.Assert; |
13 import org.junit.Before; | 13 import org.junit.Before; |
14 import org.junit.Rule; | 14 import org.junit.Rule; |
15 import org.junit.Test; | 15 import org.junit.Test; |
16 import org.junit.runner.RunWith; | 16 import org.junit.runner.RunWith; |
17 | 17 |
18 import org.chromium.base.ThreadUtils; | 18 import org.chromium.base.ThreadUtils; |
19 import org.chromium.base.test.util.CommandLineFlags; | 19 import org.chromium.base.test.util.CommandLineFlags; |
20 import org.chromium.base.test.util.Feature; | 20 import org.chromium.base.test.util.Feature; |
21 import org.chromium.base.test.util.Restriction; | 21 import org.chromium.base.test.util.Restriction; |
22 import org.chromium.chrome.browser.ChromeActivity; | 22 import org.chromium.chrome.browser.ChromeActivity; |
23 import org.chromium.chrome.browser.ChromeSwitches; | 23 import org.chromium.chrome.browser.ChromeSwitches; |
24 import org.chromium.chrome.browser.WebContentsFactory; | 24 import org.chromium.chrome.browser.WebContentsFactory; |
25 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManager; | 25 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManager; |
26 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManagerWrapp
er; | 26 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManagerWrapp
er; |
27 import org.chromium.chrome.browser.compositor.bottombar.contextualsearch.Context
ualSearchPanel; | 27 import org.chromium.chrome.browser.compositor.bottombar.contextualsearch.Context
ualSearchPanel; |
28 import org.chromium.chrome.browser.compositor.layouts.LayoutUpdateHost; | 28 import org.chromium.chrome.browser.compositor.layouts.LayoutUpdateHost; |
29 import org.chromium.chrome.browser.contextualsearch.ContextualSearchInternalStat
eController.InternalState; | |
30 import org.chromium.chrome.browser.contextualsearch.ContextualSearchSelectionCon
troller.SelectionType; | |
31 import org.chromium.chrome.test.ChromeActivityTestRule; | 29 import org.chromium.chrome.test.ChromeActivityTestRule; |
32 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | 30 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
33 import org.chromium.content.browser.ContentViewCore; | 31 import org.chromium.content.browser.ContentViewCore; |
34 import org.chromium.content.browser.SelectionClient; | 32 import org.chromium.content.browser.SelectionClient; |
35 import org.chromium.content.browser.SelectionPopupController; | 33 import org.chromium.content.browser.SelectionPopupController; |
36 import org.chromium.content_public.browser.WebContents; | 34 import org.chromium.content_public.browser.WebContents; |
37 import org.chromium.ui.resources.dynamics.DynamicResourceLoader; | 35 import org.chromium.ui.resources.dynamics.DynamicResourceLoader; |
38 import org.chromium.ui.touch_selection.SelectionEventType; | 36 import org.chromium.ui.touch_selection.SelectionEventType; |
39 | 37 |
40 import javax.annotation.Nullable; | 38 import javax.annotation.Nullable; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 119 } |
122 | 120 |
123 @Override | 121 @Override |
124 protected ContextualSearchRequest createContextualSearchRequest( | 122 protected ContextualSearchRequest createContextualSearchRequest( |
125 String query, String altTerm, String mid, boolean shouldPrefetch
) { | 123 String query, String altTerm, String mid, boolean shouldPrefetch
) { |
126 return new MockContextualSearchRequest(query, altTerm, shouldPrefetc
h); | 124 return new MockContextualSearchRequest(query, altTerm, shouldPrefetc
h); |
127 } | 125 } |
128 | 126 |
129 @Override | 127 @Override |
130 protected void nativeGatherSurroundingText(long nativeContextualSearchMa
nager, | 128 protected void nativeGatherSurroundingText(long nativeContextualSearchMa
nager, |
131 ContextualSearchContext contextualSearchContext, WebContents bas
eWebContents) { | 129 ContextualSearchContext contextualSearchContext, WebContents bas
eWebContents) {} |
132 getContextualSearchInternalStateController().notifyFinishedWorkOn( | |
133 InternalState.GATHERING_SURROUNDINGS); | |
134 } | |
135 | 130 |
136 /** | 131 /** |
137 * @return A stubbed ContentViewCore for mocking text selection. | 132 * @return A stubbed ContentViewCore for mocking text selection. |
138 */ | 133 */ |
139 public StubbedContentViewCore getBaseContentView() { | 134 public StubbedContentViewCore getBaseContentView() { |
140 return (StubbedContentViewCore) getSelectionController().getBaseCont
entView(); | 135 return (StubbedContentViewCore) getSelectionController().getBaseCont
entView(); |
141 } | 136 } |
142 } | 137 } |
143 | 138 |
144 // -------------------------------------------------------------------------
------------------- | 139 // -------------------------------------------------------------------------
------------------- |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 239 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
245 @Override | 240 @Override |
246 public void run() { | 241 public void run() { |
247 mContextualSearchClient.showUnhandledTapUIIfNeeded(0, 0); | 242 mContextualSearchClient.showUnhandledTapUIIfNeeded(0, 0); |
248 mContextualSearchClient.onSelectionEvent( | 243 mContextualSearchClient.onSelectionEvent( |
249 SelectionEventType.SELECTION_HANDLES_CLEARED, 0, 0); | 244 SelectionEventType.SELECTION_HANDLES_CLEARED, 0, 0); |
250 } | 245 } |
251 }); | 246 }); |
252 } | 247 } |
253 | 248 |
254 private void mockSelectWordAroundCaretAck(final String text) { | 249 /** |
| 250 * Generates a call indicating that surrounding text and selection range are
available. |
| 251 */ |
| 252 private void generateTextSurroundingSelectionAvailable() { |
255 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 253 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
256 @Override | 254 @Override |
257 public void run() { | 255 public void run() { |
258 mContextualSearchManager.handleSelection(text, true, SelectionTy
pe.TAP, 0, 0); | 256 // It only makes sense to send dummy data here because we can't
easily control |
| 257 // what's in the native context. |
| 258 mContextualSearchManager.onTextSurroundingSelectionAvailable( |
| 259 "UTF-8", "unused", 0, 0); |
259 } | 260 } |
260 }); | 261 }); |
261 } | 262 } |
| 263 |
| 264 /** |
| 265 * Generates an ACK for the SelectWordAroundCaret native call, which indicat
es that the select |
| 266 * action has completed with the given result. |
| 267 */ |
| 268 private void generateSelectWordAroundCaretAck() { |
| 269 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 270 @Override |
| 271 public void run() { |
| 272 // It only makes sense to send dummy data here because we can't
easily control |
| 273 // what's in the native context. |
| 274 mContextualSearchManager.selectWordAroundCaretAck(true, 0, 0); |
| 275 } |
| 276 }); |
| 277 } |
262 | 278 |
263 // -------------------------------------------------------------------------
------------------- | 279 // -------------------------------------------------------------------------
------------------- |
264 | 280 |
265 @Before | 281 @Before |
266 public void setUp() throws Exception { | 282 public void setUp() throws Exception { |
267 mActivityTestRule.startMainActivityOnBlankPage(); | 283 mActivityTestRule.startMainActivityOnBlankPage(); |
268 final ChromeActivity activity = mActivityTestRule.getActivity(); | 284 final ChromeActivity activity = mActivityTestRule.getActivity(); |
269 | 285 |
270 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 286 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
271 @Override | 287 @Override |
(...skipping 17 matching lines...) Expand all Loading... |
289 */ | 305 */ |
290 @Test | 306 @Test |
291 @SmallTest | 307 @SmallTest |
292 @Feature({"ContextualSearch"}) | 308 @Feature({"ContextualSearch"}) |
293 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) | 309 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) |
294 public void testLongpressFollowedByNonTextTap() { | 310 public void testLongpressFollowedByNonTextTap() { |
295 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 0); | 311 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 0); |
296 | 312 |
297 // Fake a selection event. | 313 // Fake a selection event. |
298 mockLongpressText("text"); | 314 mockLongpressText("text"); |
| 315 // Generate the surrounding-text-available callback. |
| 316 // Surrounding text is gathered for longpress due to icing integration. |
| 317 generateTextSurroundingSelectionAvailable(); |
299 | 318 |
300 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); | 319 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); |
301 Assert.assertEquals(mPanelManager.getPanelHideCount(), 0); | 320 Assert.assertEquals(mPanelManager.getPanelHideCount(), 0); |
302 Assert.assertEquals(mContextualSearchManager.getSelectionController().ge
tSelectedText(), | 321 Assert.assertEquals(mContextualSearchManager.getSelectionController().ge
tSelectedText(), |
303 "text"); | 322 "text"); |
304 | 323 |
305 // Fake tap on non-text. | 324 // Fake tap on non-text. |
306 mockTapEmptySpace(); | 325 mockTapEmptySpace(); |
307 | 326 |
308 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); | 327 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); |
309 Assert.assertEquals(mPanelManager.getPanelHideCount(), 1); | 328 Assert.assertEquals(mPanelManager.getPanelHideCount(), 1); |
310 Assert.assertNull(mContextualSearchManager.getSelectionController().getS
electedText()); | 329 Assert.assertNull(mContextualSearchManager.getSelectionController().getS
electedText()); |
311 } | 330 } |
312 | 331 |
313 /** | 332 /** |
314 * Tests that a Tap gesture followed by tapping empty space closes the panel
. | 333 * Tests that a Tap gesture followed by tapping empty space closes the panel
. |
315 */ | 334 */ |
316 @Test | 335 @Test |
317 @SmallTest | 336 @SmallTest |
318 @Feature({"ContextualSearch"}) | 337 @Feature({"ContextualSearch"}) |
319 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) | 338 @Restriction(Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE) |
320 public void testTextTapFollowedByNonTextTap() { | 339 public void testTextTapFollowedByNonTextTap() { |
321 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 0); | 340 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 0); |
322 | 341 |
323 // Fake a Tap event. | 342 // Fake a Tap event. |
324 mockTapText("text"); | 343 mockTapText("text"); |
| 344 // Generate the surrounding-text-available callback. |
| 345 generateTextSurroundingSelectionAvailable(); |
325 // Right now the tap-processing sequence will stall at selectWordAroundC
aret, so we need | 346 // Right now the tap-processing sequence will stall at selectWordAroundC
aret, so we need |
326 // to prod it forward with a manual hack: | 347 // to prod it forward by generating an ACK: |
327 mockSelectWordAroundCaretAck("text"); | 348 generateSelectWordAroundCaretAck(); |
328 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); | 349 Assert.assertEquals(mPanelManager.getRequestPanelShowCount(), 1); |
329 Assert.assertEquals(mPanelManager.getPanelHideCount(), 0); | 350 Assert.assertEquals(mPanelManager.getPanelHideCount(), 0); |
330 } | 351 } |
331 } | 352 } |
OLD | NEW |