OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.search_engines; | 5 package org.chromium.chrome.browser.search_engines; |
6 | 6 |
7 import android.net.Uri; | 7 import android.net.Uri; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 }); | 148 }); |
149 searchEngineKeyword = ThreadUtils.runOnUiThreadBlockingNoException(new C allable<String>() { | 149 searchEngineKeyword = ThreadUtils.runOnUiThreadBlockingNoException(new C allable<String>() { |
150 @Override | 150 @Override |
151 public String call() throws Exception { | 151 public String call() throws Exception { |
152 return templateUrlService.getDefaultSearchEngineTemplateUrl().ge tKeyword(); | 152 return templateUrlService.getDefaultSearchEngineTemplateUrl().ge tKeyword(); |
153 } | 153 } |
154 }); | 154 }); |
155 assertEquals(searchEngines.get(1).getKeyword(), searchEngineKeyword); | 155 assertEquals(searchEngines.get(1).getKeyword(), searchEngineKeyword); |
156 } | 156 } |
157 | 157 |
158 @SmallTest | |
159 @Feature({"SearchEngines"}) | |
160 public void testSortandGetCustomSearchEngine() throws InterruptedException { | |
161 final TemplateUrlService templateUrlService = waitForTemplateUrlServiceT oLoad(); | |
162 | |
163 // Add custom search engines and verified only engines visited within 2 days are added. | |
164 // Also verified custom engines are sorted correctly. | |
165 List<TemplateUrl> searchEngines = | |
166 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T emplateUrl>>() { | |
167 @Override | |
168 public List<TemplateUrl> call() throws Exception { | |
169 templateUrlService.addSearchEngineForTesting("keyword1", 0); | |
170 templateUrlService.addSearchEngineForTesting("keyword2", 0); | |
171 templateUrlService.addSearchEngineForTesting("keyword3", 3); | |
172 return templateUrlService.getSearchEngines(); | |
173 } | |
174 }); | |
175 assertEquals(7, searchEngines.size()); | |
Ted C
2016/12/15 21:38:06
I assume the 7 comes from the fact that there are
ltian
2016/12/15 22:56:54
Return the sublist after pre-populated ones is a g
| |
176 assertEquals("keyword2", searchEngines.get(5).getKeyword()); | |
177 assertEquals("keyword1", searchEngines.get(6).getKeyword()); | |
178 | |
179 // Add more custom search engines and verified at most 3 custom engines are returned. | |
180 // Also verified custom engines are sorted correctly. | |
181 searchEngines = | |
182 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T emplateUrl>>() { | |
183 @Override | |
184 public List<TemplateUrl> call() throws Exception { | |
185 templateUrlService.addSearchEngineForTesting("keyword4", 0); | |
186 templateUrlService.addSearchEngineForTesting("keyword5", 0); | |
187 return templateUrlService.getSearchEngines(); | |
188 } | |
189 }); | |
190 assertEquals(8, searchEngines.size()); | |
191 assertEquals("keyword5", searchEngines.get(5).getKeyword()); | |
192 assertEquals("keyword4", searchEngines.get(6).getKeyword()); | |
193 assertEquals("keyword2", searchEngines.get(7).getKeyword()); | |
194 | |
195 // Verified last_visited is updated correctly and sorting in descending order correctly. | |
196 searchEngines = | |
197 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T emplateUrl>>() { | |
198 @Override | |
199 public List<TemplateUrl> call() throws Exception { | |
200 templateUrlService.updateLastVisitedForTesting("keyword3 "); | |
201 return templateUrlService.getSearchEngines(); | |
202 } | |
203 }); | |
204 assertEquals(8, searchEngines.size()); | |
205 assertEquals("keyword3", searchEngines.get(5).getKeyword()); | |
206 assertEquals("keyword5", searchEngines.get(6).getKeyword()); | |
207 assertEquals("keyword4", searchEngines.get(7).getKeyword()); | |
208 | |
209 // Set a custom engine as default provider and verified still 3 custom e ngines are returned. | |
210 // Also verified custom engines are sorted correctly. | |
211 searchEngines = | |
212 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T emplateUrl>>() { | |
213 @Override | |
214 public List<TemplateUrl> call() throws Exception { | |
215 templateUrlService.setSearchEngine("keyword4"); | |
216 return templateUrlService.getSearchEngines(); | |
217 } | |
218 }); | |
219 assertEquals(9, searchEngines.size()); | |
220 assertEquals("keyword4", searchEngines.get(5).getKeyword()); | |
221 assertEquals("keyword3", searchEngines.get(6).getKeyword()); | |
222 assertEquals("keyword5", searchEngines.get(7).getKeyword()); | |
223 assertEquals("keyword2", searchEngines.get(8).getKeyword()); | |
224 } | |
225 | |
158 private TemplateUrlService waitForTemplateUrlServiceToLoad() throws Interrup tedException { | 226 private TemplateUrlService waitForTemplateUrlServiceToLoad() throws Interrup tedException { |
159 final AtomicBoolean observerNotified = new AtomicBoolean(false); | 227 final AtomicBoolean observerNotified = new AtomicBoolean(false); |
160 final LoadListener listener = new LoadListener() { | 228 final LoadListener listener = new LoadListener() { |
161 @Override | 229 @Override |
162 public void onTemplateUrlServiceLoaded() { | 230 public void onTemplateUrlServiceLoaded() { |
163 observerNotified.set(true); | 231 observerNotified.set(true); |
164 } | 232 } |
165 }; | 233 }; |
166 final TemplateUrlService templateUrlService = ThreadUtils.runOnUiThreadB lockingNoException( | 234 final TemplateUrlService templateUrlService = ThreadUtils.runOnUiThreadB lockingNoException( |
167 new Callable<TemplateUrlService>() { | 235 new Callable<TemplateUrlService>() { |
168 @Override | 236 @Override |
169 public TemplateUrlService call() { | 237 public TemplateUrlService call() { |
170 TemplateUrlService service = TemplateUrlService.getInsta nce(); | 238 TemplateUrlService service = TemplateUrlService.getInsta nce(); |
171 service.registerLoadListener(listener); | 239 service.registerLoadListener(listener); |
172 service.load(); | 240 service.load(); |
173 return service; | 241 return service; |
174 } | 242 } |
175 }); | 243 }); |
176 | 244 |
177 CriteriaHelper.pollInstrumentationThread(new Criteria( | 245 CriteriaHelper.pollInstrumentationThread(new Criteria( |
178 "Observer wasn't notified of TemplateUrlService load.") { | 246 "Observer wasn't notified of TemplateUrlService load.") { |
179 @Override | 247 @Override |
180 public boolean isSatisfied() { | 248 public boolean isSatisfied() { |
181 return observerNotified.get(); | 249 return observerNotified.get(); |
182 } | 250 } |
183 }); | 251 }); |
184 return templateUrlService; | 252 return templateUrlService; |
185 } | 253 } |
186 } | 254 } |
OLD | NEW |