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