OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "components/omnibox/browser/history_quick_provider.h" | 5 #include "components/omnibox/browser/history_quick_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
| 15 #include "base/files/scoped_temp_dir.h" |
15 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
19 #include "base/prefs/pref_service.h" | 20 #include "base/prefs/pref_service.h" |
20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | 23 #include "base/test/sequenced_worker_pool_owner.h" |
23 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 24 #include "components/bookmarks/browser/bookmark_model.h" |
24 #include "chrome/browser/autocomplete/in_memory_url_index_factory.h" | |
25 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
26 #include "chrome/browser/history/history_service_factory.h" | |
27 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" | |
28 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
29 #include "chrome/test/base/testing_browser_process.h" | |
30 #include "chrome/test/base/testing_profile.h" | |
31 #include "components/bookmarks/test/bookmark_test_helpers.h" | 25 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 26 #include "components/bookmarks/test/test_bookmark_client.h" |
32 #include "components/history/core/browser/history_backend.h" | 27 #include "components/history/core/browser/history_backend.h" |
33 #include "components/history/core/browser/history_database.h" | 28 #include "components/history/core/browser/history_database.h" |
| 29 #include "components/history/core/browser/history_database_params.h" |
34 #include "components/history/core/browser/history_service.h" | 30 #include "components/history/core/browser/history_service.h" |
35 #include "components/history/core/browser/history_service_observer.h" | 31 #include "components/history/core/browser/history_service_observer.h" |
36 #include "components/history/core/browser/url_database.h" | 32 #include "components/history/core/browser/url_database.h" |
| 33 #include "components/history/core/test/test_history_database.h" |
37 #include "components/metrics/proto/omnibox_event.pb.h" | 34 #include "components/metrics/proto/omnibox_event.pb.h" |
38 #include "components/omnibox/browser/autocomplete_match.h" | 35 #include "components/omnibox/browser/autocomplete_match.h" |
39 #include "components/omnibox/browser/autocomplete_result.h" | 36 #include "components/omnibox/browser/autocomplete_result.h" |
| 37 #include "components/omnibox/browser/history_index_restore_observer.h" |
40 #include "components/omnibox/browser/history_url_provider.h" | 38 #include "components/omnibox/browser/history_url_provider.h" |
41 #include "components/omnibox/browser/in_memory_url_index.h" | 39 #include "components/omnibox/browser/in_memory_url_index.h" |
| 40 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" |
| 41 #include "components/omnibox/browser/test_scheme_classifier.h" |
42 #include "components/omnibox/browser/url_index_private_data.h" | 42 #include "components/omnibox/browser/url_index_private_data.h" |
43 #include "components/search_engines/search_terms_data.h" | 43 #include "components/search_engines/search_terms_data.h" |
44 #include "components/search_engines/template_url.h" | 44 #include "components/search_engines/template_url.h" |
45 #include "components/search_engines/template_url_service.h" | 45 #include "components/search_engines/template_url_service.h" |
46 #include "content/public/test/test_browser_thread.h" | |
47 #include "content/public/test/test_utils.h" | |
48 #include "sql/transaction.h" | 46 #include "sql/transaction.h" |
49 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
50 | 48 |
51 using base::ASCIIToUTF16; | 49 using base::ASCIIToUTF16; |
52 using base::Time; | 50 using base::Time; |
53 using base::TimeDelta; | 51 using base::TimeDelta; |
54 | 52 |
55 using content::BrowserThread; | |
56 | |
57 namespace { | 53 namespace { |
58 | 54 |
59 struct TestURLInfo { | 55 struct TestURLInfo { |
60 std::string url; | 56 std::string url; |
61 std::string title; | 57 std::string title; |
62 int visit_count; | 58 int visit_count; |
63 int typed_count; | 59 int typed_count; |
64 int days_from_now; | 60 int days_from_now; |
65 } quick_test_db[] = { | 61 } quick_test_db[] = { |
66 {"http://www.google.com/", "Google", 3, 3, 0}, | 62 {"http://www.google.com/", "Google", 3, 3, 0}, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 | 172 |
177 private: | 173 private: |
178 ~GetURLTask() override {} | 174 ~GetURLTask() override {} |
179 | 175 |
180 bool* result_storage_; | 176 bool* result_storage_; |
181 const GURL url_; | 177 const GURL url_; |
182 | 178 |
183 DISALLOW_COPY_AND_ASSIGN(GetURLTask); | 179 DISALLOW_COPY_AND_ASSIGN(GetURLTask); |
184 }; | 180 }; |
185 | 181 |
| 182 class QuitTask : public history::HistoryDBTask { |
| 183 public: |
| 184 QuitTask() {} |
| 185 |
| 186 bool RunOnDBThread(history::HistoryBackend* backend, |
| 187 history::HistoryDatabase* db) override { |
| 188 return true; |
| 189 } |
| 190 |
| 191 void DoneRunOnMainThread() override { |
| 192 base::MessageLoop::current()->QuitWhenIdle(); |
| 193 } |
| 194 |
| 195 private: |
| 196 ~QuitTask() override {} |
| 197 |
| 198 DISALLOW_COPY_AND_ASSIGN(QuitTask); |
| 199 }; |
| 200 |
| 201 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient { |
| 202 public: |
| 203 FakeAutocompleteProviderClient() { |
| 204 bookmark_model_ = bookmark_client_.CreateModel(); |
| 205 set_template_url_service( |
| 206 make_scoped_ptr(new TemplateURLService(nullptr, 0))); |
| 207 } |
| 208 |
| 209 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override { |
| 210 return scheme_classifier_; |
| 211 } |
| 212 |
| 213 const SearchTermsData& GetSearchTermsData() const override { |
| 214 return search_terms_data_; |
| 215 } |
| 216 |
| 217 history::HistoryService* GetHistoryService() override { |
| 218 return &history_service_; |
| 219 } |
| 220 |
| 221 bookmarks::BookmarkModel* GetBookmarkModel() override { |
| 222 return bookmark_model_.get(); |
| 223 } |
| 224 |
| 225 InMemoryURLIndex* GetInMemoryURLIndex() override { |
| 226 return in_memory_url_index_.get(); |
| 227 } |
| 228 |
| 229 std::string GetAcceptLanguages() const override { return "en,en-US,ko"; } |
| 230 |
| 231 void set_in_memory_url_index(scoped_ptr<InMemoryURLIndex> index) { |
| 232 in_memory_url_index_ = std::move(index); |
| 233 } |
| 234 |
| 235 private: |
| 236 bookmarks::TestBookmarkClient bookmark_client_; |
| 237 scoped_ptr<bookmarks::BookmarkModel> bookmark_model_; |
| 238 TestSchemeClassifier scheme_classifier_; |
| 239 SearchTermsData search_terms_data_; |
| 240 scoped_ptr<InMemoryURLIndex> in_memory_url_index_; |
| 241 history::HistoryService history_service_; |
| 242 |
| 243 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); |
| 244 }; |
| 245 |
186 } // namespace | 246 } // namespace |
187 | 247 |
188 class HistoryQuickProviderTest : public testing::Test { | 248 class HistoryQuickProviderTest : public testing::Test { |
189 public: | 249 public: |
190 HistoryQuickProviderTest() | 250 HistoryQuickProviderTest() : pool_owner_(3, "Background Pool") {} |
191 : ui_thread_(BrowserThread::UI, &message_loop_), | |
192 file_thread_(BrowserThread::FILE, &message_loop_) {} | |
193 | 251 |
194 protected: | 252 protected: |
195 class SetShouldContain : public std::unary_function<const std::string&, | 253 class SetShouldContain : public std::unary_function<const std::string&, |
196 std::set<std::string> > { | 254 std::set<std::string> > { |
197 public: | 255 public: |
198 explicit SetShouldContain(const ACMatches& matched_urls); | 256 explicit SetShouldContain(const ACMatches& matched_urls); |
199 | 257 |
200 void operator()(const std::string& expected); | 258 void operator()(const std::string& expected); |
201 | 259 |
202 std::set<std::string> LeftOvers() const { return matches_; } | 260 std::set<std::string> LeftOvers() const { return matches_; } |
203 | 261 |
204 private: | 262 private: |
205 std::set<std::string> matches_; | 263 std::set<std::string> matches_; |
206 }; | 264 }; |
207 | 265 |
208 static scoped_ptr<KeyedService> CreateTemplateURLService( | |
209 content::BrowserContext* context) { | |
210 Profile* profile = static_cast<Profile*>(context); | |
211 return make_scoped_ptr(new TemplateURLService( | |
212 profile->GetPrefs(), make_scoped_ptr(new SearchTermsData), NULL, | |
213 scoped_ptr<TemplateURLServiceClient>(new ChromeTemplateURLServiceClient( | |
214 HistoryServiceFactory::GetForProfile( | |
215 profile, ServiceAccessType::EXPLICIT_ACCESS))), | |
216 NULL, NULL, base::Closure())); | |
217 } | |
218 | |
219 void SetUp() override; | 266 void SetUp() override; |
220 void TearDown() override; | 267 void TearDown() override; |
221 | 268 |
222 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); | 269 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); |
223 | 270 |
224 // Fills test data into the history system. | 271 // Fills test data into the history system. |
225 void FillData(); | 272 void FillData(); |
226 | 273 |
227 // Runs an autocomplete query on |text| and checks to see that the returned | 274 // Runs an autocomplete query on |text| and checks to see that the returned |
228 // results' destination URLs match those provided. |expected_urls| does not | 275 // results' destination URLs match those provided. |expected_urls| does not |
(...skipping 13 matching lines...) Expand all Loading... |
242 bool can_inline_top_result, | 289 bool can_inline_top_result, |
243 base::string16 expected_fill_into_edit, | 290 base::string16 expected_fill_into_edit, |
244 base::string16 autocompletion); | 291 base::string16 autocompletion); |
245 | 292 |
246 // TODO(shess): From history_service.h in reference to history_backend: | 293 // TODO(shess): From history_service.h in reference to history_backend: |
247 // > This class has most of the implementation and runs on the 'thread_'. | 294 // > This class has most of the implementation and runs on the 'thread_'. |
248 // > You MUST communicate with this class ONLY through the thread_'s | 295 // > You MUST communicate with this class ONLY through the thread_'s |
249 // > message_loop(). | 296 // > message_loop(). |
250 // Direct use of this object in tests is almost certainly not thread-safe. | 297 // Direct use of this object in tests is almost certainly not thread-safe. |
251 history::HistoryBackend* history_backend() { | 298 history::HistoryBackend* history_backend() { |
252 return history_service_->history_backend_.get(); | 299 return client_->GetHistoryService()->history_backend_.get(); |
253 } | 300 } |
254 | 301 |
255 // Call history_backend()->GetURL(url, NULL) on the history thread, returning | 302 // Call history_backend()->GetURL(url, NULL) on the history thread, returning |
256 // the result. | 303 // the result. |
257 bool GetURLProxy(const GURL& url); | 304 bool GetURLProxy(const GURL& url); |
258 | 305 |
259 base::MessageLoopForUI message_loop_; | 306 // Helper functions to initialize the HistoryService. |
260 content::TestBrowserThread ui_thread_; | 307 bool InitializeHistoryService(); |
261 content::TestBrowserThread file_thread_; | 308 void CreateInMemoryURLIndex(); |
| 309 void BlockUntilHistoryProcessesPendingRequests(); |
| 310 void BlockUntilHistoryIndexIsRefreshed(); |
262 | 311 |
263 scoped_ptr<TestingProfile> profile_; | 312 base::MessageLoop message_loop_; |
264 scoped_ptr<ChromeAutocompleteProviderClient> client_; | 313 base::SequencedWorkerPoolOwner pool_owner_; |
265 history::HistoryService* history_service_; | 314 base::ScopedTempDir history_dir_; |
| 315 scoped_ptr<FakeAutocompleteProviderClient> client_; |
266 | 316 |
267 ACMatches ac_matches_; // The resulting matches after running RunTest. | 317 ACMatches ac_matches_; // The resulting matches after running RunTest. |
268 | 318 |
269 scoped_refptr<HistoryQuickProvider> provider_; | 319 scoped_refptr<HistoryQuickProvider> provider_; |
270 }; | 320 }; |
271 | 321 |
272 void HistoryQuickProviderTest::SetUp() { | 322 void HistoryQuickProviderTest::SetUp() { |
273 profile_.reset(new TestingProfile()); | 323 client_.reset(new FakeAutocompleteProviderClient()); |
274 client_.reset(new ChromeAutocompleteProviderClient(profile_.get())); | 324 ASSERT_TRUE(InitializeHistoryService()); |
275 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); | 325 FillData(); |
276 profile_->CreateBookmarkModel(true); | 326 |
277 bookmarks::test::WaitForBookmarkModelToLoad( | 327 // |FillData()| must be called before |CreateInMemoryURLIndex()|. This will |
278 BookmarkModelFactory::GetForProfile(profile_.get())); | 328 // ensure that the index is properly populated with data from the database. |
279 profile_->BlockUntilHistoryIndexIsRefreshed(); | 329 CreateInMemoryURLIndex(); |
| 330 BlockUntilHistoryIndexIsRefreshed(); |
280 // History index refresh creates rebuilt tasks to run on history thread. | 331 // History index refresh creates rebuilt tasks to run on history thread. |
281 // Block here to make sure that all of them are complete. | 332 // Block here to make sure that all of them are complete. |
282 profile_->BlockUntilHistoryProcessesPendingRequests(); | 333 BlockUntilHistoryProcessesPendingRequests(); |
283 history_service_ = HistoryServiceFactory::GetForProfile( | 334 |
284 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS); | |
285 EXPECT_TRUE(history_service_); | |
286 provider_ = new HistoryQuickProvider(client_.get()); | 335 provider_ = new HistoryQuickProvider(client_.get()); |
287 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
288 profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService); | |
289 FillData(); | |
290 InMemoryURLIndex* index = | |
291 InMemoryURLIndexFactory::GetForProfile(profile_.get()); | |
292 EXPECT_TRUE(index); | |
293 index->RebuildFromHistory(history_backend()->db()); | |
294 } | 336 } |
295 | 337 |
296 void HistoryQuickProviderTest::TearDown() { | 338 void HistoryQuickProviderTest::TearDown() { |
297 provider_ = NULL; | 339 provider_ = NULL; |
| 340 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in |
| 341 // its destructor. |
| 342 client_->GetInMemoryURLIndex()->Shutdown(); |
| 343 client_->set_in_memory_url_index(nullptr); |
298 // History index rebuild task is created from main thread during SetUp, | 344 // History index rebuild task is created from main thread during SetUp, |
299 // performed on DB thread and must be deleted on main thread. | 345 // performed on DB thread and must be deleted on main thread. |
300 // Run main loop to process delete task, to prevent leaks. | 346 // Run main loop to process delete task, to prevent leaks. |
301 base::MessageLoop::current()->RunUntilIdle(); | 347 base::MessageLoop::current()->RunUntilIdle(); |
302 } | 348 } |
303 | 349 |
304 void HistoryQuickProviderTest::GetTestData(size_t* data_count, | 350 void HistoryQuickProviderTest::GetTestData(size_t* data_count, |
305 TestURLInfo** test_data) { | 351 TestURLInfo** test_data) { |
306 DCHECK(data_count); | 352 DCHECK(data_count); |
307 DCHECK(test_data); | 353 DCHECK(test_data); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 bool prevent_inline_autocomplete, | 430 bool prevent_inline_autocomplete, |
385 std::vector<std::string> expected_urls, | 431 std::vector<std::string> expected_urls, |
386 bool can_inline_top_result, | 432 bool can_inline_top_result, |
387 base::string16 expected_fill_into_edit, | 433 base::string16 expected_fill_into_edit, |
388 base::string16 expected_autocompletion) { | 434 base::string16 expected_autocompletion) { |
389 SCOPED_TRACE(text); // Minimal hint to query being run. | 435 SCOPED_TRACE(text); // Minimal hint to query being run. |
390 base::MessageLoop::current()->RunUntilIdle(); | 436 base::MessageLoop::current()->RunUntilIdle(); |
391 AutocompleteInput input(text, cursor_position, std::string(), GURL(), | 437 AutocompleteInput input(text, cursor_position, std::string(), GURL(), |
392 metrics::OmniboxEventProto::INVALID_SPEC, | 438 metrics::OmniboxEventProto::INVALID_SPEC, |
393 prevent_inline_autocomplete, false, true, true, false, | 439 prevent_inline_autocomplete, false, true, true, false, |
394 ChromeAutocompleteSchemeClassifier(profile_.get())); | 440 TestSchemeClassifier()); |
395 provider_->Start(input, false); | 441 provider_->Start(input, false); |
396 EXPECT_TRUE(provider_->done()); | 442 EXPECT_TRUE(provider_->done()); |
397 | 443 |
398 ac_matches_ = provider_->matches(); | 444 ac_matches_ = provider_->matches(); |
399 | 445 |
400 // We should have gotten back at most AutocompleteProvider::kMaxMatches. | 446 // We should have gotten back at most AutocompleteProvider::kMaxMatches. |
401 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); | 447 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); |
402 | 448 |
403 // If the number of expected and actual matches aren't equal then we need | 449 // If the number of expected and actual matches aren't equal then we need |
404 // test no further, but let's do anyway so that we know which URLs failed. | 450 // test no further, but let's do anyway so that we know which URLs failed. |
405 EXPECT_EQ(expected_urls.size(), ac_matches_.size()); | 451 EXPECT_EQ(expected_urls.size(), ac_matches_.size()); |
406 | 452 |
407 // Verify that all expected URLs were found and that all found URLs | 453 // Verify that all expected URLs were found and that all found URLs |
408 // were expected. | 454 // were expected. |
409 std::set<std::string> leftovers = | 455 std::set<std::string> leftovers = |
410 for_each(expected_urls.begin(), expected_urls.end(), | 456 for_each(expected_urls.begin(), expected_urls.end(), |
411 SetShouldContain(ac_matches_)).LeftOvers(); | 457 SetShouldContain(ac_matches_)).LeftOvers(); |
412 EXPECT_EQ(0U, leftovers.size()) << "There were " << leftovers.size() | 458 EXPECT_EQ(0U, leftovers.size()) << "There were " << leftovers.size() |
413 << " unexpected results, one of which was: '" | 459 << " unexpected results, one of which was: '" |
414 << *(leftovers.begin()) << "'."; | 460 << *(leftovers.begin()) << "'."; |
415 | 461 |
416 if (expected_urls.empty()) | 462 if (expected_urls.empty()) |
417 return; | 463 return; |
418 | 464 |
| 465 ASSERT_FALSE(ac_matches_.empty()); |
419 // Verify that we got the results in the order expected. | 466 // Verify that we got the results in the order expected. |
420 int best_score = ac_matches_.begin()->relevance + 1; | 467 int best_score = ac_matches_.begin()->relevance + 1; |
421 int i = 0; | 468 int i = 0; |
422 std::vector<std::string>::const_iterator expected = expected_urls.begin(); | 469 std::vector<std::string>::const_iterator expected = expected_urls.begin(); |
423 for (ACMatches::const_iterator actual = ac_matches_.begin(); | 470 for (ACMatches::const_iterator actual = ac_matches_.begin(); |
424 actual != ac_matches_.end() && expected != expected_urls.end(); | 471 actual != ac_matches_.end() && expected != expected_urls.end(); |
425 ++actual, ++expected, ++i) { | 472 ++actual, ++expected, ++i) { |
426 EXPECT_EQ(*expected, actual->destination_url.spec()) | 473 EXPECT_EQ(*expected, actual->destination_url.spec()) |
427 << "For result #" << i << " we got '" << actual->destination_url.spec() | 474 << "For result #" << i << " we got '" << actual->destination_url.spec() |
428 << "' but expected '" << *expected << "'."; | 475 << "' but expected '" << *expected << "'."; |
429 EXPECT_LT(actual->relevance, best_score) | 476 EXPECT_LT(actual->relevance, best_score) |
430 << "At result #" << i << " (url=" << actual->destination_url.spec() | 477 << "At result #" << i << " (url=" << actual->destination_url.spec() |
431 << "), we noticed scores are not monotonically decreasing."; | 478 << "), we noticed scores are not monotonically decreasing."; |
432 best_score = actual->relevance; | 479 best_score = actual->relevance; |
433 } | 480 } |
434 | 481 |
435 EXPECT_EQ(can_inline_top_result, ac_matches_[0].allowed_to_be_default_match); | 482 EXPECT_EQ(can_inline_top_result, ac_matches_[0].allowed_to_be_default_match); |
436 if (can_inline_top_result) | 483 if (can_inline_top_result) |
437 EXPECT_EQ(expected_autocompletion, ac_matches_[0].inline_autocompletion); | 484 EXPECT_EQ(expected_autocompletion, ac_matches_[0].inline_autocompletion); |
438 EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit); | 485 EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit); |
439 } | 486 } |
440 | 487 |
441 bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) { | 488 bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) { |
442 base::CancelableTaskTracker task_tracker; | 489 base::CancelableTaskTracker task_tracker; |
443 bool result = false; | 490 bool result = false; |
444 history_service_->ScheduleDBTask( | 491 client_->GetHistoryService()->ScheduleDBTask( |
445 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)), | 492 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)), |
446 &task_tracker); | 493 &task_tracker); |
447 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If | 494 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If |
448 // the test hangs, DoneRunOnMainThread isn't being invoked correctly. | 495 // the test hangs, DoneRunOnMainThread isn't being invoked correctly. |
449 base::MessageLoop::current()->Run(); | 496 base::MessageLoop::current()->Run(); |
450 return result; | 497 return result; |
451 } | 498 } |
452 | 499 |
| 500 bool HistoryQuickProviderTest::InitializeHistoryService() { |
| 501 if (!history_dir_.CreateUniqueTempDir() || |
| 502 !client_->GetHistoryService()->Init( |
| 503 false, client_->GetAcceptLanguages(), |
| 504 history::TestHistoryDatabaseParamsForPath(history_dir_.path()))) |
| 505 return false; |
| 506 |
| 507 BlockUntilHistoryProcessesPendingRequests(); |
| 508 return true; |
| 509 } |
| 510 |
| 511 void HistoryQuickProviderTest::CreateInMemoryURLIndex() { |
| 512 scoped_ptr<InMemoryURLIndex> in_memory_url_index(new InMemoryURLIndex( |
| 513 client_->GetBookmarkModel(), client_->GetHistoryService(), |
| 514 pool_owner_.pool().get(), history_dir_.path(), |
| 515 client_->GetAcceptLanguages(), SchemeSet())); |
| 516 in_memory_url_index->Init(); |
| 517 in_memory_url_index->RebuildFromHistory(history_backend()->db()); |
| 518 client_->set_in_memory_url_index(std::move(in_memory_url_index)); |
| 519 } |
| 520 |
| 521 void HistoryQuickProviderTest::BlockUntilHistoryProcessesPendingRequests() { |
| 522 base::CancelableTaskTracker tracker; |
| 523 client_->GetHistoryService()->ScheduleDBTask( |
| 524 scoped_ptr<history::HistoryDBTask>(new QuitTask()), &tracker); |
| 525 base::MessageLoop::current()->Run(); |
| 526 } |
| 527 |
| 528 void HistoryQuickProviderTest::BlockUntilHistoryIndexIsRefreshed() { |
| 529 InMemoryURLIndex* index = client_->GetInMemoryURLIndex(); |
| 530 if (!index || index->restored()) |
| 531 return; |
| 532 base::RunLoop run_loop; |
| 533 HistoryIndexRestoreObserver observer(run_loop.QuitClosure()); |
| 534 index->set_restore_cache_observer(&observer); |
| 535 run_loop.Run(); |
| 536 index->set_restore_cache_observer(nullptr); |
| 537 DCHECK(index->restored()); |
| 538 } |
| 539 |
453 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { | 540 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { |
454 std::vector<std::string> expected_urls; | 541 std::vector<std::string> expected_urls; |
455 expected_urls.push_back("http://slashdot.org/favorite_page.html"); | 542 expected_urls.push_back("http://slashdot.org/favorite_page.html"); |
456 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, | 543 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, |
457 ASCIIToUTF16("slashdot.org/favorite_page.html"), | 544 ASCIIToUTF16("slashdot.org/favorite_page.html"), |
458 ASCIIToUTF16(".org/favorite_page.html")); | 545 ASCIIToUTF16(".org/favorite_page.html")); |
459 } | 546 } |
460 | 547 |
461 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) { | 548 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) { |
462 std::vector<std::string> expected_urls; | 549 std::vector<std::string> expected_urls; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 EXPECT_EQ(1U, ac_matches_.size()); | 761 EXPECT_EQ(1U, ac_matches_.size()); |
675 EXPECT_TRUE(GetURLProxy(test_url)); | 762 EXPECT_TRUE(GetURLProxy(test_url)); |
676 provider_->DeleteMatch(ac_matches_[0]); | 763 provider_->DeleteMatch(ac_matches_[0]); |
677 | 764 |
678 // Check that the underlying URL is deleted from the history DB (this implies | 765 // Check that the underlying URL is deleted from the history DB (this implies |
679 // that all visits are gone as well). Also verify that a deletion notification | 766 // that all visits are gone as well). Also verify that a deletion notification |
680 // is sent, in response to which the secondary data stores (InMemoryDatabase, | 767 // is sent, in response to which the secondary data stores (InMemoryDatabase, |
681 // InMemoryURLIndex) will drop any data they might have pertaining to the URL. | 768 // InMemoryURLIndex) will drop any data they might have pertaining to the URL. |
682 // To ensure that the deletion has been propagated everywhere before we start | 769 // To ensure that the deletion has been propagated everywhere before we start |
683 // verifying post-deletion states, first wait until we see the notification. | 770 // verifying post-deletion states, first wait until we see the notification. |
684 WaitForURLsDeletedNotification(history_service_); | 771 WaitForURLsDeletedNotification(client_->GetHistoryService()); |
685 EXPECT_FALSE(GetURLProxy(test_url)); | 772 EXPECT_FALSE(GetURLProxy(test_url)); |
686 | 773 |
687 // Just to be on the safe side, explicitly verify that we have deleted enough | 774 // Just to be on the safe side, explicitly verify that we have deleted enough |
688 // data so that we will not be serving the same result again. | 775 // data so that we will not be serving the same result again. |
689 expected_urls.clear(); | 776 expected_urls.clear(); |
690 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, | 777 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, |
691 ASCIIToUTF16("NONE EXPECTED"), base::string16()); | 778 ASCIIToUTF16("NONE EXPECTED"), base::string16()); |
692 } | 779 } |
693 | 780 |
694 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { | 781 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 RunTest(ASCIIToUTF16("popularsitewithroot.com/"), true, expected_urls, true, | 876 RunTest(ASCIIToUTF16("popularsitewithroot.com/"), true, expected_urls, true, |
790 ASCIIToUTF16("popularsitewithroot.com"), base::string16()); | 877 ASCIIToUTF16("popularsitewithroot.com"), base::string16()); |
791 } | 878 } |
792 | 879 |
793 TEST_F(HistoryQuickProviderTest, CullSearchResults) { | 880 TEST_F(HistoryQuickProviderTest, CullSearchResults) { |
794 // Set up a default search engine. | 881 // Set up a default search engine. |
795 TemplateURLData data; | 882 TemplateURLData data; |
796 data.SetShortName(ASCIIToUTF16("TestEngine")); | 883 data.SetShortName(ASCIIToUTF16("TestEngine")); |
797 data.SetKeyword(ASCIIToUTF16("TestEngine")); | 884 data.SetKeyword(ASCIIToUTF16("TestEngine")); |
798 data.SetURL("http://testsearch.com/?q={searchTerms}"); | 885 data.SetURL("http://testsearch.com/?q={searchTerms}"); |
799 TemplateURLService* template_url_service = | 886 TemplateURLService* template_url_service = client_->GetTemplateURLService(); |
800 TemplateURLServiceFactory::GetForProfile(profile_.get()); | |
801 TemplateURL* template_url = new TemplateURL(data); | 887 TemplateURL* template_url = new TemplateURL(data); |
802 template_url_service->Add(template_url); | 888 template_url_service->Add(template_url); |
803 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); | 889 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); |
804 template_url_service->Load(); | 890 template_url_service->Load(); |
805 | 891 |
806 // A search results page should not be returned when typing a query. | 892 // A search results page should not be returned when typing a query. |
807 std::vector<std::string> expected_urls; | 893 std::vector<std::string> expected_urls; |
808 RunTest(ASCIIToUTF16("thequery"), false, expected_urls, false, | 894 RunTest(ASCIIToUTF16("thequery"), false, expected_urls, false, |
809 ASCIIToUTF16("anotherengine.com/?q=thequery"), base::string16()); | 895 ASCIIToUTF16("anotherengine.com/?q=thequery"), base::string16()); |
810 | 896 |
811 // A search results page should not be returned when typing the engine URL. | 897 // A search results page should not be returned when typing the engine URL. |
812 expected_urls.clear(); | 898 expected_urls.clear(); |
813 expected_urls.push_back("http://testsearch.com/"); | 899 expected_urls.push_back("http://testsearch.com/"); |
814 RunTest(ASCIIToUTF16("testsearch"), false, expected_urls, true, | 900 RunTest(ASCIIToUTF16("testsearch"), false, expected_urls, true, |
815 ASCIIToUTF16("testsearch.com"), | 901 ASCIIToUTF16("testsearch.com"), |
816 ASCIIToUTF16(".com")); | 902 ASCIIToUTF16(".com")); |
817 } | 903 } |
818 | 904 |
819 TEST_F(HistoryQuickProviderTest, DoesNotProvideMatchesOnFocus) { | 905 TEST_F(HistoryQuickProviderTest, DoesNotProvideMatchesOnFocus) { |
820 AutocompleteInput input( | 906 AutocompleteInput input(ASCIIToUTF16("popularsite"), base::string16::npos, |
821 ASCIIToUTF16("popularsite"), base::string16::npos, std::string(), GURL(), | 907 std::string(), GURL(), |
822 metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true, | 908 metrics::OmniboxEventProto::INVALID_SPEC, false, |
823 ChromeAutocompleteSchemeClassifier(profile_.get())); | 909 false, true, true, true, TestSchemeClassifier()); |
824 provider_->Start(input, false); | 910 provider_->Start(input, false); |
825 EXPECT_TRUE(provider_->matches().empty()); | 911 EXPECT_TRUE(provider_->matches().empty()); |
826 } | 912 } |
827 | 913 |
828 // HQPOrderingTest ------------------------------------------------------------- | 914 // HQPOrderingTest ------------------------------------------------------------- |
829 | 915 |
830 TestURLInfo ordering_test_db[] = { | 916 TestURLInfo ordering_test_db[] = { |
831 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, | 917 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, |
832 256}, | 918 256}, |
833 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, " | 919 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, " |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 | 972 |
887 TEST_F(HQPOrderingTest, TEAMatch) { | 973 TEST_F(HQPOrderingTest, TEAMatch) { |
888 std::vector<std::string> expected_urls; | 974 std::vector<std::string> expected_urls; |
889 expected_urls.push_back("http://www.teamliquid.net/"); | 975 expected_urls.push_back("http://www.teamliquid.net/"); |
890 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 976 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
891 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 977 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
892 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, | 978 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, |
893 ASCIIToUTF16("www.teamliquid.net"), | 979 ASCIIToUTF16("www.teamliquid.net"), |
894 ASCIIToUTF16("mliquid.net")); | 980 ASCIIToUTF16("mliquid.net")); |
895 } | 981 } |
OLD | NEW |