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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/UrlBarTest.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.omnibox;
6
7 import android.content.Intent;
8 import android.test.suitebuilder.annotation.SmallTest;
9 import android.text.Editable;
10 import android.view.inputmethod.BaseInputConnection;
11
12 import com.google.android.apps.chrome.R;
13
14 import org.chromium.base.ThreadUtils;
15 import org.chromium.base.test.util.Feature;
16 import org.chromium.chrome.browser.ChromeActivity;
17 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
18 import org.chromium.chrome.test.util.OmniboxTestUtils;
19 import org.chromium.chrome.test.util.OmniboxTestUtils.StubAutocompleteController ;
20
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.atomic.AtomicBoolean;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 /**
27 * Tests for the URL bar UI component.
28 */
29 public class UrlBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
30
31 public UrlBarTest() {
32 super(ChromeActivity.class);
33 }
34
35 private UrlBar getUrlBar() {
36 return (UrlBar) getActivity().findViewById(R.id.url_bar);
37 }
38
39 private void stubLocationBarAutocomplete() {
40 ThreadUtils.runOnUiThread(new Runnable() {
41 @Override
42 public void run() {
43 LocationBarLayout locationBar =
44 (LocationBarLayout) getActivity().findViewById(R.id.loca tion_bar);
45 locationBar.setAutocompleteController(new StubAutocompleteContro ller());
46 }
47 });
48 }
49
50 private static class AutocompleteState {
51 public final boolean hasAutocomplete;
52 public final String textWithoutAutocomplete;
53 public final String textWithAutocomplete;
54
55 public AutocompleteState(
56 boolean hasAutocomplete, String textWithoutAutocomplete,
57 String textWithAutocomplete) {
58 this.hasAutocomplete = hasAutocomplete;
59 this.textWithoutAutocomplete = textWithoutAutocomplete;
60 this.textWithAutocomplete = textWithAutocomplete;
61 }
62 }
63
64 private Editable getUrlBarText(final UrlBar urlBar) throws ExecutionExceptio n {
65 return ThreadUtils.runOnUiThreadBlocking(new Callable<Editable>() {
66 @Override
67 public Editable call() throws Exception {
68 return urlBar.getText();
69 }
70 });
71 }
72
73 private AutocompleteState getAutocompleteState(
74 final UrlBar urlBar, final Runnable action) {
75 final AtomicBoolean hasAutocomplete = new AtomicBoolean();
76 final AtomicReference<String> textWithoutAutocomplete = new AtomicRefere nce<String>();
77 final AtomicReference<String> textWithAutocomplete = new AtomicReference <String>();
78
79 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
80 @Override
81 public void run() {
82 if (action != null) action.run();
83 textWithoutAutocomplete.set(urlBar.getTextWithoutAutocomplete()) ;
84 textWithAutocomplete.set(urlBar.getQueryText());
85 hasAutocomplete.set(urlBar.hasAutocomplete());
86 }
87 });
88
89 return new AutocompleteState(
90 hasAutocomplete.get(), textWithoutAutocomplete.get(), textWithAu tocomplete.get());
91 }
92
93 private void setTextAndVerifyNoAutocomplete(final UrlBar urlBar, final Strin g text) {
94 AutocompleteState state = getAutocompleteState(urlBar, new Runnable() {
95 @Override
96 public void run() {
97 urlBar.setText(text);
98 }
99 });
100
101 assertEquals(text, state.textWithoutAutocomplete);
102 assertEquals(text, state.textWithAutocomplete);
103 assertFalse(state.hasAutocomplete);
104 }
105
106 private void setAutocomplete(final UrlBar urlBar,
107 final String userText, final String autocompleteText) {
108 AutocompleteState state = getAutocompleteState(urlBar, new Runnable() {
109 @Override
110 public void run() {
111 urlBar.setAutocompleteText(userText, autocompleteText);
112 }
113 });
114
115 assertEquals(userText, state.textWithoutAutocomplete);
116 assertEquals(userText + autocompleteText, state.textWithAutocomplete);
117 assertTrue(state.hasAutocomplete);
118 }
119
120 private AutocompleteState setSelection(
121 final UrlBar urlBar, final int selectionStart, final int selectionEn d) {
122 return getAutocompleteState(urlBar, new Runnable() {
123 @Override
124 public void run() {
125 urlBar.setSelection(selectionStart, selectionEnd);
126 }
127 });
128 }
129
130 @SmallTest
131 @Feature({"Omnibox"})
132 public void testRefocusing() throws InterruptedException {
133 startMainActivityOnBlankPage();
134 UrlBar urlBar = getUrlBar();
135 assertFalse(OmniboxTestUtils.doesUrlBarHaveFocus(urlBar));
136 OmniboxTestUtils.checkUrlBarRefocus(urlBar, 5);
137 }
138
139 @SmallTest
140 @Feature({"Omnibox"})
141 public void testAutocompleteUpdatedOnSetText() throws InterruptedException {
142 startMainActivityOnBlankPage();
143 stubLocationBarAutocomplete();
144 final UrlBar urlBar = getUrlBar();
145 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
146
147 // Verify that setting a new string will clear the autocomplete.
148 setTextAndVerifyNoAutocomplete(urlBar, "test");
149 setAutocomplete(urlBar, "test", "ing is fun");
150 setTextAndVerifyNoAutocomplete(urlBar, "new string");
151
152 // Replace part of the non-autocomplete text and see that the autocomple te is cleared.
153 setTextAndVerifyNoAutocomplete(urlBar, "test");
154 setAutocomplete(urlBar, "test", "ing is fun");
155 AutocompleteState state = getAutocompleteState(urlBar, new Runnable() {
156 @Override
157 public void run() {
158 urlBar.setText(urlBar.getText().replace(1, 2, "a"));
159 }
160 });
161 assertFalse(state.hasAutocomplete);
162 assertEquals("tasting is fun", state.textWithoutAutocomplete);
163 assertEquals("tasting is fun", state.textWithAutocomplete);
164
165 // Replace part of the autocomplete text and see that the autocomplete i s cleared.
166 setTextAndVerifyNoAutocomplete(urlBar, "test");
167 setAutocomplete(urlBar, "test", "ing is fun");
168 state = getAutocompleteState(urlBar, new Runnable() {
169 @Override
170 public void run() {
171 urlBar.setText(urlBar.getText().replace(8, 10, "no"));
172 }
173 });
174 assertFalse(state.hasAutocomplete);
175 assertEquals("testing no fun", state.textWithoutAutocomplete);
176 assertEquals("testing no fun", state.textWithAutocomplete);
177 }
178
179 @SmallTest
180 @Feature({"Omnibox"})
181 public void testAutocompleteUpdatedOnSelection() throws InterruptedException {
182 startMainActivityOnBlankPage();
183 stubLocationBarAutocomplete();
184 final UrlBar urlBar = getUrlBar();
185 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
186
187 // Verify that setting a selection before the autocomplete clears it.
188 setTextAndVerifyNoAutocomplete(urlBar, "test");
189 setAutocomplete(urlBar, "test", "ing is fun");
190 AutocompleteState state = setSelection(urlBar, 1, 1);
191 assertFalse(state.hasAutocomplete);
192 assertEquals("test", state.textWithoutAutocomplete);
193 assertEquals("test", state.textWithAutocomplete);
194
195 // Verify that setting a selection range before the autocomplete clears it.
196 setTextAndVerifyNoAutocomplete(urlBar, "test");
197 setAutocomplete(urlBar, "test", "ing is fun");
198 state = setSelection(urlBar, 0, 4);
199 assertFalse(state.hasAutocomplete);
200 assertEquals("test", state.textWithoutAutocomplete);
201 assertEquals("test", state.textWithAutocomplete);
202
203 // Verify that setting a selection at the start of the autocomplete clea rs it.
204 setTextAndVerifyNoAutocomplete(urlBar, "test");
205 setAutocomplete(urlBar, "test", "ing is fun");
206 state = setSelection(urlBar, 4, 4);
207 assertFalse(state.hasAutocomplete);
208 assertEquals("test", state.textWithoutAutocomplete);
209 assertEquals("test", state.textWithAutocomplete);
210
211 // Verify that setting a selection range that covers a portion of the no n-autocomplete
212 // and autocomplete text does not delete the autocomplete text.
213 setTextAndVerifyNoAutocomplete(urlBar, "test");
214 setAutocomplete(urlBar, "test", "ing is fun");
215 state = setSelection(urlBar, 2, 5);
216 assertFalse(state.hasAutocomplete);
217 assertEquals("testing is fun", state.textWithoutAutocomplete);
218 assertEquals("testing is fun", state.textWithAutocomplete);
219
220 // Verify that setting a selection range that over the entire string doe s not delete
221 // the autocomplete text.
222 setTextAndVerifyNoAutocomplete(urlBar, "test");
223 setAutocomplete(urlBar, "test", "ing is fun");
224 state = setSelection(urlBar, 0, 14);
225 assertFalse(state.hasAutocomplete);
226 assertEquals("testing is fun", state.textWithoutAutocomplete);
227 assertEquals("testing is fun", state.textWithAutocomplete);
228
229 // Verify that setting a selection at the end of the text does not delet e the autocomplete
230 // text.
231 setTextAndVerifyNoAutocomplete(urlBar, "test");
232 setAutocomplete(urlBar, "test", "ing is fun");
233 state = setSelection(urlBar, 14, 14);
234 assertFalse(state.hasAutocomplete);
235 assertEquals("testing is fun", state.textWithoutAutocomplete);
236 assertEquals("testing is fun", state.textWithAutocomplete);
237
238 // Verify that setting a selection in the middle of the autocomplete tex t does not delete
239 // the autocomplete text.
240 setTextAndVerifyNoAutocomplete(urlBar, "test");
241 setAutocomplete(urlBar, "test", "ing is fun");
242 state = setSelection(urlBar, 9, 9);
243 assertFalse(state.hasAutocomplete);
244 assertEquals("testing is fun", state.textWithoutAutocomplete);
245 assertEquals("testing is fun", state.textWithAutocomplete);
246
247 // Verify that setting a selection range in the middle of the autocomple te text does not
248 // delete the autocomplete text.
249 setTextAndVerifyNoAutocomplete(urlBar, "test");
250 setAutocomplete(urlBar, "test", "ing is fun");
251 state = setSelection(urlBar, 8, 11);
252 assertFalse(state.hasAutocomplete);
253 assertEquals("testing is fun", state.textWithoutAutocomplete);
254 assertEquals("testing is fun", state.textWithAutocomplete);
255
256 // Verify that setting the same selection does not clear the autocomplet e text.
257 setTextAndVerifyNoAutocomplete(urlBar, "test");
258 setAutocomplete(urlBar, "test", "ing is fun");
259 state = setSelection(urlBar, 4, 14);
260 assertTrue(state.hasAutocomplete);
261 assertEquals("test", state.textWithoutAutocomplete);
262 assertEquals("testing is fun", state.textWithAutocomplete);
263 }
264
265 @SmallTest
266 @Feature({"Omnibox"})
267 public void testAutocompleteUpdatedOnDefocus() throws InterruptedException {
268 startMainActivityOnBlankPage();
269 stubLocationBarAutocomplete();
270 final UrlBar urlBar = getUrlBar();
271 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
272
273 // Verify that defocusing the UrlBar clears the autocomplete.
274 setTextAndVerifyNoAutocomplete(urlBar, "test");
275 setAutocomplete(urlBar, "test", "ing is fun");
276 OmniboxTestUtils.toggleUrlBarFocus(urlBar, false);
277 AutocompleteState state = getAutocompleteState(urlBar, null);
278 assertFalse(state.hasAutocomplete);
279 }
280
281 @SmallTest
282 @Feature({"Omnibox"})
283 public void testAutocompleteClearedOnComposition()
284 throws InterruptedException, ExecutionException {
285 startMainActivityOnBlankPage();
286 stubLocationBarAutocomplete();
287 final UrlBar urlBar = getUrlBar();
288 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
289 OmniboxTestUtils.waitForFocusAndKeyboardActive(urlBar, true);
290
291 setTextAndVerifyNoAutocomplete(urlBar, "test");
292 setAutocomplete(urlBar, "test", "ing is fun");
293
294 assertNotNull(urlBar.mInputConnection);
295 AutocompleteState state = getAutocompleteState(urlBar, new Runnable() {
296 @Override
297 public void run() {
298 urlBar.mInputConnection.setComposingText("ing compose", 4);
299 }
300 });
301 assertFalse(state.hasAutocomplete);
302
303 Editable urlText = getUrlBarText(urlBar);
304 assertEquals("testing compose", urlText.toString());
305 // TODO(tedchoc): Investigate why this fails on x86.
306 //assertEquals(4, BaseInputConnection.getComposingSpanStart(urlText));
307 //assertEquals(15, BaseInputConnection.getComposingSpanEnd(urlText));
308 }
309
310 @SmallTest
311 @Feature("Omnibox")
312 public void testDelayedCompositionCorrectedWithAutocomplete()
313 throws InterruptedException, ExecutionException {
314 startMainActivityOnBlankPage();
315 stubLocationBarAutocomplete();
316
317 final UrlBar urlBar = getUrlBar();
318 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
319 OmniboxTestUtils.waitForFocusAndKeyboardActive(urlBar, true);
320
321 assertNotNull(urlBar.mInputConnection);
322
323 // Test with a single autocomplete
324
325 setTextAndVerifyNoAutocomplete(urlBar, "chrome://f");
326 setAutocomplete(urlBar, "chrome://f", "lags");
327
328 AutocompleteState state = getAutocompleteState(urlBar, new Runnable() {
329 @Override
330 public void run() {
331 urlBar.mInputConnection.setComposingRegion(13, 14);
332 urlBar.mInputConnection.setComposingText("f", 1);
333 }
334 });
335 assertFalse(state.hasAutocomplete);
336
337 Editable urlText = getUrlBarText(urlBar);
338 assertEquals("chrome://f", urlText.toString());
339 assertEquals(BaseInputConnection.getComposingSpanStart(urlText), 9);
340 assertEquals(BaseInputConnection.getComposingSpanEnd(urlText), 10);
341
342 // Test with > 1 characters in composition.
343
344 setTextAndVerifyNoAutocomplete(urlBar, "chrome://fl");
345 setAutocomplete(urlBar, "chrome://fl", "ags");
346
347 state = getAutocompleteState(urlBar, new Runnable() {
348 @Override
349 public void run() {
350 urlBar.mInputConnection.setComposingRegion(12, 14);
351 urlBar.mInputConnection.setComposingText("fl", 1);
352 }
353 });
354 assertFalse(state.hasAutocomplete);
355
356 urlText = getUrlBarText(urlBar);
357 assertEquals("chrome://fl", urlText.toString());
358 assertEquals(BaseInputConnection.getComposingSpanStart(urlText), 9);
359 assertEquals(BaseInputConnection.getComposingSpanEnd(urlText), 11);
360
361 // Test with non-matching composition. Should just append to the URL te xt.
362
363 setTextAndVerifyNoAutocomplete(urlBar, "chrome://f");
364 setAutocomplete(urlBar, "chrome://f", "lags");
365
366 state = getAutocompleteState(urlBar, new Runnable() {
367 @Override
368 public void run() {
369 urlBar.mInputConnection.setComposingRegion(13, 14);
370 urlBar.mInputConnection.setComposingText("g", 1);
371 }
372 });
373 assertFalse(state.hasAutocomplete);
374
375 urlText = getUrlBarText(urlBar);
376 assertEquals("chrome://fg", urlText.toString());
377 assertEquals(BaseInputConnection.getComposingSpanStart(urlText), 10);
378 assertEquals(BaseInputConnection.getComposingSpanEnd(urlText), 11);
379
380 // Test with composition text that matches the entire text w/o autocompl ete.
381
382 setTextAndVerifyNoAutocomplete(urlBar, "chrome://f");
383 setAutocomplete(urlBar, "chrome://f", "lags");
384
385 state = getAutocompleteState(urlBar, new Runnable() {
386 @Override
387 public void run() {
388 urlBar.mInputConnection.setComposingRegion(13, 14);
389 urlBar.mInputConnection.setComposingText("chrome://f", 1);
390 }
391 });
392 assertFalse(state.hasAutocomplete);
393
394 urlText = getUrlBarText(urlBar);
395 assertEquals("chrome://f", urlText.toString());
396 assertEquals(BaseInputConnection.getComposingSpanStart(urlText), 0);
397 assertEquals(BaseInputConnection.getComposingSpanEnd(urlText), 10);
398
399 // Test with composition text longer than the URL text. Shouldn't crash and should
400 // just append text.
401
402 setTextAndVerifyNoAutocomplete(urlBar, "chrome://f");
403 setAutocomplete(urlBar, "chrome://f", "lags");
404
405 state = getAutocompleteState(urlBar, new Runnable() {
406 @Override
407 public void run() {
408 urlBar.mInputConnection.setComposingRegion(13, 14);
409 urlBar.mInputConnection.setComposingText("blahblahblah", 1);
410 }
411 });
412 assertFalse(state.hasAutocomplete);
413
414 urlText = getUrlBarText(urlBar);
415 assertEquals("chrome://fblahblahblah", urlText.toString());
416 assertEquals(BaseInputConnection.getComposingSpanStart(urlText), 10);
417 assertEquals(BaseInputConnection.getComposingSpanEnd(urlText), 22);
418 }
419
420 /**
421 * Test to verify the omnibox can take focus during startup before native li braries have
422 * loaded.
423 */
424 @SmallTest
425 @Feature({"Omnibox"})
426 public void testFocusingOnStartup() throws InterruptedException {
427 Intent intent = new Intent(Intent.ACTION_MAIN);
428 intent.addCategory(Intent.CATEGORY_LAUNCHER);
429 prepareUrlIntent(intent, "about:blank");
430 startActivityCompletely(intent);
431
432 UrlBar urlBar = getUrlBar();
433 assertNotNull(urlBar);
434 OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
435 assertTrue(OmniboxTestUtils.waitForFocusAndKeyboardActive(urlBar, true)) ;
436 }
437
438 @Override
439 public void startMainActivity() throws InterruptedException {
440 // Each test will start the activity.
441 }
442 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698