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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/QueryInOmniboxTest.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 static org.chromium.chrome.test.util.OmniboxTestUtils.buildSuggestionMap;
8
9 import android.os.SystemClock;
10 import android.test.suitebuilder.annotation.MediumTest;
11 import android.test.suitebuilder.annotation.SmallTest;
12 import android.text.Selection;
13 import android.text.TextUtils;
14 import android.view.KeyEvent;
15 import android.view.View;
16 import android.widget.ImageButton;
17
18 import com.google.android.apps.chrome.R;
19
20 import org.chromium.base.ThreadUtils;
21 import org.chromium.base.test.util.Feature;
22 import org.chromium.chrome.browser.ChromeActivity;
23 import org.chromium.chrome.browser.Tab;
24 import org.chromium.chrome.browser.ntp.NewTabPage;
25 import org.chromium.chrome.browser.omnibox.AutocompleteController.OnSuggestionsR eceivedListener;
26 import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxResultIt em;
27 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion.Type;
28 import org.chromium.chrome.browser.toolbar.ToolbarDataProvider;
29 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
30 import org.chromium.chrome.test.util.OmniboxTestUtils;
31 import org.chromium.chrome.test.util.OmniboxTestUtils.SuggestionsResult;
32 import org.chromium.chrome.test.util.OmniboxTestUtils.SuggestionsResultBuilder;
33 import org.chromium.chrome.test.util.OmniboxTestUtils.TestAutocompleteController ;
34 import org.chromium.chrome.test.util.OmniboxTestUtils.TestSuggestionResultsBuild er;
35 import org.chromium.chrome.test.util.TestHttpServerClient;
36 import org.chromium.content.browser.test.util.KeyUtils;
37
38 import java.util.List;
39 import java.util.Map;
40 import java.util.concurrent.Callable;
41 import java.util.concurrent.atomic.AtomicInteger;
42
43 /**
44 * Tests for Query in Omnibox.
45 */
46 public class QueryInOmniboxTest extends ChromeActivityTestCaseBase<ChromeActivit y> {
47
48 private static final String QUERY_EXTRACTION_PARAM = "espv=1";
49 private static final String SEARCH_TERM = "Puppies";
50
51 private UrlBar mUrlBar;
52 private LocationBarLayout mLocationBar;
53 private TestToolbarDataProvider mTestToolbarDataProvider;
54 private ImageButton mDeleteButton;
55
56 public QueryInOmniboxTest() {
57 super(ChromeActivity.class);
58 }
59
60 /**
61 * Loading a google https url which is searching for queryText.
62 * @throws InterruptedException
63 */
64 private void loadUrlFromQueryText()
65 throws InterruptedException {
66 String url = TestHttpServerClient.getUrl("chrome/test/data/android/googl e.html?q="
67 + SEARCH_TERM + "&hl=en&client=chrome-mobile&"
68 + QUERY_EXTRACTION_PARAM + "&tbm=isch");
69 loadUrl(url);
70 }
71
72 /**
73 * @return The current UrlBar querytext.
74 */
75 private String getUrlBarQueryText() {
76 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<String> () {
77 @Override
78 public String call() throws Exception {
79 return TextUtils.isEmpty(mUrlBar.getQueryText()) ? "" :
80 mUrlBar.getQueryText();
81 }
82 });
83 }
84
85 /**
86 * @return The current UrlBar text.
87 */
88 private CharSequence getUrlBarText() {
89 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<CharSeq uence>() {
90 @Override
91 public CharSequence call() throws Exception {
92 return mUrlBar.getText();
93 }
94 });
95 }
96
97 /**
98 * Assert the urlbar is displaying the correct text
99 */
100 private void checkUrlBarText() {
101 assertEquals(SEARCH_TERM, getUrlBarQueryText());
102 }
103
104 @SmallTest
105 @Feature({"QueryinOmnibox", "Omnibox"})
106 public void testQueryInOmnibox() throws InterruptedException {
107 loadUrlFromQueryText();
108 checkUrlBarText();
109 }
110
111 @SmallTest
112 @Feature({"QueryinOmnibox", "Omnibox"})
113 public void testQueryInOmniboxOnFocus() throws InterruptedException {
114 loadUrlFromQueryText();
115 OmniboxTestUtils.toggleUrlBarFocus(mUrlBar, true);
116 checkUrlBarText();
117 }
118
119 @SmallTest
120 @Feature({"QueryinOmnibox", "Omnibox"})
121 public void testRefocusWithQueryInOmnibox() throws InterruptedException {
122 loadUrlFromQueryText();
123 assertEquals(SEARCH_TERM, getUrlBarQueryText());
124 assertFalse(OmniboxTestUtils.doesUrlBarHaveFocus(mUrlBar));
125 OmniboxTestUtils.checkUrlBarRefocus(mUrlBar, 5);
126 }
127
128 @MediumTest
129 @Feature({"QueryinOmnibox", "Omnibox"})
130 public void testDeleteButton() throws InterruptedException {
131 loadUrlFromQueryText();
132 OmniboxTestUtils.toggleUrlBarFocus(mUrlBar, true);
133 checkUrlBarText();
134 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
135 @Override
136 public void run() {
137 mDeleteButton.callOnClick();
138 }
139 });
140 assertTrue(getUrlBarQueryText().isEmpty());
141 }
142
143 @MediumTest
144 @Feature({"QueryinOmnibox", "Omnibox"})
145 public void testBackspaceDelete() throws InterruptedException {
146 loadUrlFromQueryText();
147 OmniboxTestUtils.toggleUrlBarFocus(mUrlBar, true);
148 checkUrlBarText();
149 // Backspace delete the entire querytext
150 for (int i = 0; i < SEARCH_TERM.length(); i++) {
151 final View v = mUrlBar;
152 KeyUtils.singleKeyEventView(getInstrumentation(), v, KeyEvent.KEYCOD E_DEL);
153 }
154 // Query text should be gone
155 assertTrue(getUrlBarQueryText().isEmpty());
156 }
157
158 @MediumTest
159 @Feature({"QueryinOmnibox", "Omnibox"})
160 public void testShrinkingAutocompleteTextResults()
161 throws InterruptedException {
162 loadUrlFromQueryText();
163 OmniboxTestUtils.toggleUrlBarFocus(mUrlBar, true);
164 final String term = SEARCH_TERM + "i";
165 setupAutocompleteSuggestions(term);
166 CharSequence urlText = getUrlBarText();
167 assertEquals("URL Bar text not autocompleted as expected.",
168 SEARCH_TERM + "ingz", getUrlBarQueryText());
169 assertEquals(8, Selection.getSelectionStart(urlText));
170 assertEquals(11, Selection.getSelectionEnd(urlText));
171 }
172
173 @MediumTest
174 @Feature({"QueryinOmnibox", "Omnibox"})
175 public void testRefineSuggestion() throws InterruptedException {
176 loadUrlFromQueryText();
177 OmniboxTestUtils.toggleUrlBarFocus(mUrlBar, true);
178 final String term = SEARCH_TERM + "i";
179 setupAutocompleteSuggestions(term);
180 assertEquals(SEARCH_TERM + "ingz", getUrlBarQueryText());
181 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
182 @Override
183 public void run() {
184 OmniboxSuggestion refineView =
185 ((OmniboxResultItem) mLocationBar.getSuggestionList()
186 .getAdapter().getItem(1)).getSuggestion();
187 ((OmniboxResultsAdapter) mLocationBar.getSuggestionList()
188 .getAdapter()).getSuggestionDelegate()
189 .onRefineSuggestion(refineView);
190 }
191 });
192 CharSequence urlText = getUrlBarText();
193 assertEquals("Refine suggestion did not work as expected.",
194 SEARCH_TERM + "iblarg", getUrlBarQueryText());
195 assertEquals(urlText.length(), Selection.getSelectionStart(urlText));
196 assertEquals(urlText.length(), Selection.getSelectionEnd(urlText));
197 }
198
199 /**
200 * Setup Autocomplete suggestion map, add suggestionListener and autocomplet e controller
201 * to the location bar.
202 * @param term The search term.
203 * @throws InterruptedException
204 */
205 private void setupAutocompleteSuggestions(final String term) throws Interrup tedException {
206 final String suggestionFor = term.toLowerCase();
207 Map<String, List<SuggestionsResult>> suggestionsMap = buildSuggestionMap (
208 new TestSuggestionResultsBuilder()
209 .setTextShownFor(suggestionFor)
210 .addSuggestions(new SuggestionsResultBuilder()
211 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m , null)
212 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m + "ng", null)
213 .setAutocompleteText("ng is awesome"))
214 .addSuggestions(new SuggestionsResultBuilder()
215 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m, null)
216 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m + "z", null)
217 .setAutocompleteText("ng is hard"))
218 .addSuggestions(new SuggestionsResultBuilder()
219 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m, null)
220 .addGeneratedSuggestion(Type.SEARCH_HISTORY, ter m + "blarg", null)
221 .setAutocompleteText("ngz")));
222
223 final Object suggestionsProcessedSignal = new Object();
224 final AtomicInteger suggestionsLeft = new AtomicInteger(
225 suggestionsMap.get(suggestionFor).size());
226 OnSuggestionsReceivedListener suggestionsListener = new OnSuggestionsRec eivedListener() {
227 @Override
228 public void onSuggestionsReceived(
229 List<OmniboxSuggestion> suggestions, String inlineAutocomple teText) {
230 mLocationBar.onSuggestionsReceived(suggestions, inlineAutocomple teText);
231 synchronized (suggestionsProcessedSignal) {
232 int remaining = suggestionsLeft.decrementAndGet();
233 if (remaining == 0) {
234 suggestionsProcessedSignal.notifyAll();
235 } else if (remaining < 0) {
236 fail("Unexpected suggestions received");
237 }
238 }
239 }
240 };
241 final TestAutocompleteController controller = new TestAutocompleteContro ller(
242 mLocationBar, suggestionsListener, suggestionsMap);
243
244 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
245 @Override
246 public void run() {
247 mLocationBar.setAutocompleteController(controller);
248 mUrlBar.append("i");
249 }
250 });
251 synchronized (suggestionsProcessedSignal) {
252 long endTime = SystemClock.uptimeMillis() + 3000;
253 while (suggestionsLeft.get() != 0) {
254 long waitTime = endTime - SystemClock.uptimeMillis();
255 if (waitTime <= 0) break;
256 suggestionsProcessedSignal.wait(waitTime);
257 }
258 }
259 }
260
261 @Override
262 public void startMainActivity() throws InterruptedException {
263 startMainActivityOnBlankPage();
264 mUrlBar = (UrlBar) getActivity().findViewById(R.id.url_bar);
265 mLocationBar = (LocationBarLayout) getActivity().findViewById(R.id.locat ion_bar);
266 mTestToolbarDataProvider =
267 new TestToolbarDataProvider(mLocationBar.getToolbarDataProvider( ));
268 mLocationBar.setToolbarDataProvider(mTestToolbarDataProvider);
269 mDeleteButton = (ImageButton) getActivity().findViewById(R.id.delete_but ton);
270 }
271
272 private static class TestToolbarDataProvider implements ToolbarDataProvider {
273 private final ToolbarDataProvider mBaseProvider;
274
275 public TestToolbarDataProvider(ToolbarDataProvider baseProvider) {
276 mBaseProvider = baseProvider;
277 }
278
279 @Override
280 public Tab getTab() {
281 return mBaseProvider.getTab();
282 }
283
284 @Override
285 public NewTabPage getNewTabPageForCurrentTab() {
286 return mBaseProvider.getNewTabPageForCurrentTab();
287 }
288
289 @Override
290 public boolean isIncognito() {
291 return mBaseProvider.isIncognito();
292 }
293
294 @Override
295 public int getLoadProgress() {
296 return mBaseProvider.getLoadProgress();
297 }
298
299 @Override
300 public String getText() {
301 return SEARCH_TERM;
302 }
303
304 @Override
305 public boolean wouldReplaceURL() {
306 return true;
307 }
308
309 @Override
310 public int getPrimaryColor() {
311 return mBaseProvider.getPrimaryColor();
312 }
313
314 @Override
315 public boolean isUsingBrandColor() {
316 return mBaseProvider.isUsingBrandColor();
317 }
318
319 @Override
320 public String getCorpusChipText() {
321 return null;
322 }
323 }
324 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698