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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 }); | 147 }); |
148 searchEngineKeyword = ThreadUtils.runOnUiThreadBlockingNoException(new C
allable<String>() { | 148 searchEngineKeyword = ThreadUtils.runOnUiThreadBlockingNoException(new C
allable<String>() { |
149 @Override | 149 @Override |
150 public String call() throws Exception { | 150 public String call() throws Exception { |
151 return templateUrlService.getDefaultSearchEngineTemplateUrl().ge
tKeyword(); | 151 return templateUrlService.getDefaultSearchEngineTemplateUrl().ge
tKeyword(); |
152 } | 152 } |
153 }); | 153 }); |
154 assertEquals(searchEngines.get(1).getKeyword(), searchEngineKeyword); | 154 assertEquals(searchEngines.get(1).getKeyword(), searchEngineKeyword); |
155 } | 155 } |
156 | 156 |
| 157 @SmallTest |
| 158 @Feature({"SearchEngines"}) |
| 159 public void testSortandGetCustomSearchEngine() { |
| 160 final TemplateUrlService templateUrlService = waitForTemplateUrlServiceT
oLoad(); |
| 161 |
| 162 // Get the number of prepopulated search engine. |
| 163 final int prepopulatedEngineNum = |
| 164 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Intege
r>() { |
| 165 @Override |
| 166 public Integer call() throws Exception { |
| 167 return templateUrlService.getSearchEngines().size(); |
| 168 } |
| 169 }); |
| 170 |
| 171 // Add custom search engines and verified only engines visited within 2
days are added. |
| 172 // Also verified custom engines are sorted correctly. |
| 173 List<TemplateUrl> customSearchEngines = |
| 174 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T
emplateUrl>>() { |
| 175 @Override |
| 176 public List<TemplateUrl> call() throws Exception { |
| 177 templateUrlService.addSearchEngineForTesting("keyword1",
0); |
| 178 templateUrlService.addSearchEngineForTesting("keyword2",
0); |
| 179 templateUrlService.addSearchEngineForTesting("keyword3",
3); |
| 180 List<TemplateUrl> searchEngines = templateUrlService.get
SearchEngines(); |
| 181 return searchEngines.subList(prepopulatedEngineNum, sear
chEngines.size()); |
| 182 } |
| 183 }); |
| 184 assertEquals(2, customSearchEngines.size()); |
| 185 assertEquals("keyword2", customSearchEngines.get(0).getKeyword()); |
| 186 assertEquals("keyword1", customSearchEngines.get(1).getKeyword()); |
| 187 |
| 188 // Add more custom search engines and verified at most 3 custom engines
are returned. |
| 189 // Also verified custom engines are sorted correctly. |
| 190 customSearchEngines = |
| 191 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T
emplateUrl>>() { |
| 192 @Override |
| 193 public List<TemplateUrl> call() throws Exception { |
| 194 templateUrlService.addSearchEngineForTesting("keyword4",
0); |
| 195 templateUrlService.addSearchEngineForTesting("keyword5",
0); |
| 196 List<TemplateUrl> searchEngines = templateUrlService.get
SearchEngines(); |
| 197 return searchEngines.subList(prepopulatedEngineNum, sear
chEngines.size()); |
| 198 } |
| 199 }); |
| 200 assertEquals(3, customSearchEngines.size()); |
| 201 assertEquals("keyword5", customSearchEngines.get(0).getKeyword()); |
| 202 assertEquals("keyword4", customSearchEngines.get(1).getKeyword()); |
| 203 assertEquals("keyword2", customSearchEngines.get(2).getKeyword()); |
| 204 |
| 205 // Verified last_visited is updated correctly and sorting in descending
order correctly. |
| 206 customSearchEngines = |
| 207 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T
emplateUrl>>() { |
| 208 @Override |
| 209 public List<TemplateUrl> call() throws Exception { |
| 210 templateUrlService.updateLastVisitedForTesting("keyword3
"); |
| 211 List<TemplateUrl> searchEngines = templateUrlService.get
SearchEngines(); |
| 212 return searchEngines.subList(prepopulatedEngineNum, sear
chEngines.size()); |
| 213 } |
| 214 }); |
| 215 assertEquals(3, customSearchEngines.size()); |
| 216 assertEquals("keyword3", customSearchEngines.get(0).getKeyword()); |
| 217 assertEquals("keyword5", customSearchEngines.get(1).getKeyword()); |
| 218 assertEquals("keyword4", customSearchEngines.get(2).getKeyword()); |
| 219 |
| 220 // Set a custom engine as default provider and verified still 3 custom e
ngines are returned. |
| 221 // Also verified custom engines are sorted correctly. |
| 222 customSearchEngines = |
| 223 ThreadUtils.runOnUiThreadBlockingNoException(new Callable<List<T
emplateUrl>>() { |
| 224 @Override |
| 225 public List<TemplateUrl> call() throws Exception { |
| 226 templateUrlService.setSearchEngine("keyword4"); |
| 227 List<TemplateUrl> searchEngines = templateUrlService.get
SearchEngines(); |
| 228 return searchEngines.subList(prepopulatedEngineNum, sear
chEngines.size()); |
| 229 } |
| 230 }); |
| 231 assertEquals(4, customSearchEngines.size()); |
| 232 assertEquals("keyword4", customSearchEngines.get(0).getKeyword()); |
| 233 assertEquals("keyword3", customSearchEngines.get(1).getKeyword()); |
| 234 assertEquals("keyword5", customSearchEngines.get(2).getKeyword()); |
| 235 assertEquals("keyword2", customSearchEngines.get(3).getKeyword()); |
| 236 } |
| 237 |
157 private TemplateUrlService waitForTemplateUrlServiceToLoad() { | 238 private TemplateUrlService waitForTemplateUrlServiceToLoad() { |
158 final AtomicBoolean observerNotified = new AtomicBoolean(false); | 239 final AtomicBoolean observerNotified = new AtomicBoolean(false); |
159 final LoadListener listener = new LoadListener() { | 240 final LoadListener listener = new LoadListener() { |
160 @Override | 241 @Override |
161 public void onTemplateUrlServiceLoaded() { | 242 public void onTemplateUrlServiceLoaded() { |
162 observerNotified.set(true); | 243 observerNotified.set(true); |
163 } | 244 } |
164 }; | 245 }; |
165 final TemplateUrlService templateUrlService = ThreadUtils.runOnUiThreadB
lockingNoException( | 246 final TemplateUrlService templateUrlService = ThreadUtils.runOnUiThreadB
lockingNoException( |
166 new Callable<TemplateUrlService>() { | 247 new Callable<TemplateUrlService>() { |
167 @Override | 248 @Override |
168 public TemplateUrlService call() { | 249 public TemplateUrlService call() { |
169 TemplateUrlService service = TemplateUrlService.getInsta
nce(); | 250 TemplateUrlService service = TemplateUrlService.getInsta
nce(); |
170 service.registerLoadListener(listener); | 251 service.registerLoadListener(listener); |
171 service.load(); | 252 service.load(); |
172 return service; | 253 return service; |
173 } | 254 } |
174 }); | 255 }); |
175 | 256 |
176 CriteriaHelper.pollInstrumentationThread(new Criteria( | 257 CriteriaHelper.pollInstrumentationThread(new Criteria( |
177 "Observer wasn't notified of TemplateUrlService load.") { | 258 "Observer wasn't notified of TemplateUrlService load.") { |
178 @Override | 259 @Override |
179 public boolean isSatisfied() { | 260 public boolean isSatisfied() { |
180 return observerNotified.get(); | 261 return observerNotified.get(); |
181 } | 262 } |
182 }); | 263 }); |
183 return templateUrlService; | 264 return templateUrlService; |
184 } | 265 } |
185 } | 266 } |
OLD | NEW |