Chromium Code Reviews

Side by Side Diff: chrome/browser/autocomplete/history_quick_provider_unittest.cc

Issue 1368293012: HistoryQuickProviderTest.DeleteMatch thread safety fix. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to HistoryDBTask. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 33 matching lines...)
44 #include "content/public/test/test_utils.h" 44 #include "content/public/test/test_utils.h"
45 #include "sql/transaction.h" 45 #include "sql/transaction.h"
46 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
47 47
48 using base::ASCIIToUTF16; 48 using base::ASCIIToUTF16;
49 using base::Time; 49 using base::Time;
50 using base::TimeDelta; 50 using base::TimeDelta;
51 51
52 using content::BrowserThread; 52 using content::BrowserThread;
53 53
54 namespace {
55
54 struct TestURLInfo { 56 struct TestURLInfo {
55 std::string url; 57 std::string url;
56 std::string title; 58 std::string title;
57 int visit_count; 59 int visit_count;
58 int typed_count; 60 int typed_count;
59 int days_from_now; 61 int days_from_now;
60 } quick_test_db[] = { 62 } quick_test_db[] = {
61 {"http://www.google.com/", "Google", 3, 3, 0}, 63 {"http://www.google.com/", "Google", 3, 3, 0},
62 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0}, 64 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0},
63 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0}, 65 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0},
(...skipping 79 matching lines...)
143 145
144 void WaitForURLsDeletedNotification(history::HistoryService* history_service) { 146 void WaitForURLsDeletedNotification(history::HistoryService* history_service) {
145 base::RunLoop runner; 147 base::RunLoop runner;
146 WaitForURLsDeletedObserver observer(&runner); 148 WaitForURLsDeletedObserver observer(&runner);
147 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 149 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
148 scoped_observer(&observer); 150 scoped_observer(&observer);
149 scoped_observer.Add(history_service); 151 scoped_observer.Add(history_service);
150 runner.Run(); 152 runner.Run();
151 } 153 }
152 154
155 // Post history_backend->GetURL() to the history thread and stop the originating
156 // thread's message loop when done.
157 class GetURLTask : public history::HistoryDBTask {
158 public:
159 GetURLTask(const GURL& url, bool* result_storage)
160 : result_storage_(result_storage),
161 url_(url) {
162 }
163
164 bool RunOnDBThread(history::HistoryBackend* backend,
165 history::HistoryDatabase* db) override {
166 *result_storage_ = backend->GetURL(url_, NULL);
167 return true;
168 }
169
170 void DoneRunOnMainThread() override {
171 base::MessageLoop::current()->Quit();
172 }
173
174 private:
175 ~GetURLTask() override {}
176
177 bool* result_storage_;
178 const GURL url_;
179
180 DISALLOW_COPY_AND_ASSIGN(GetURLTask);
181 };
182
183 } // namespace
184
153 class HistoryQuickProviderTest : public testing::Test { 185 class HistoryQuickProviderTest : public testing::Test {
154 public: 186 public:
155 HistoryQuickProviderTest() 187 HistoryQuickProviderTest()
156 : ui_thread_(BrowserThread::UI, &message_loop_), 188 : ui_thread_(BrowserThread::UI, &message_loop_),
157 file_thread_(BrowserThread::FILE, &message_loop_) {} 189 file_thread_(BrowserThread::FILE, &message_loop_) {}
158 190
159 protected: 191 protected:
160 class SetShouldContain : public std::unary_function<const std::string&, 192 class SetShouldContain : public std::unary_function<const std::string&,
161 std::set<std::string> > { 193 std::set<std::string> > {
162 public: 194 public:
(...skipping 38 matching lines...)
201 233
202 // As above, simply with a cursor position specified. 234 // As above, simply with a cursor position specified.
203 void RunTestWithCursor(const base::string16 text, 235 void RunTestWithCursor(const base::string16 text,
204 const size_t cursor_position, 236 const size_t cursor_position,
205 bool prevent_inline_autocomplete, 237 bool prevent_inline_autocomplete,
206 std::vector<std::string> expected_urls, 238 std::vector<std::string> expected_urls,
207 bool can_inline_top_result, 239 bool can_inline_top_result,
208 base::string16 expected_fill_into_edit, 240 base::string16 expected_fill_into_edit,
209 base::string16 autocompletion); 241 base::string16 autocompletion);
210 242
243 // TODO(shess): From history_service.h in reference to history_backend:
244 // > This class has most of the implementation and runs on the 'thread_'.
245 // > You MUST communicate with this class ONLY through the thread_'s
246 // > message_loop().
247 // Direct use of this object in tests is almost certainly not thread-safe.
211 history::HistoryBackend* history_backend() { 248 history::HistoryBackend* history_backend() {
212 return history_service_->history_backend_.get(); 249 return history_service_->history_backend_.get();
213 } 250 }
214 251
252 // Call history_backend()->GetURL(url, NULL) on the history thread, returning
253 // the result.
254 bool GetURLProxy(const GURL& url);
255
215 base::MessageLoopForUI message_loop_; 256 base::MessageLoopForUI message_loop_;
216 content::TestBrowserThread ui_thread_; 257 content::TestBrowserThread ui_thread_;
217 content::TestBrowserThread file_thread_; 258 content::TestBrowserThread file_thread_;
218 259
219 scoped_ptr<TestingProfile> profile_; 260 scoped_ptr<TestingProfile> profile_;
220 scoped_ptr<ChromeAutocompleteProviderClient> client_; 261 scoped_ptr<ChromeAutocompleteProviderClient> client_;
221 history::HistoryService* history_service_; 262 history::HistoryService* history_service_;
222 263
223 ACMatches ac_matches_; // The resulting matches after running RunTest. 264 ACMatches ac_matches_; // The resulting matches after running RunTest.
224 265
(...skipping 162 matching lines...)
387 << "), we noticed scores are not monotonically decreasing."; 428 << "), we noticed scores are not monotonically decreasing.";
388 best_score = actual->relevance; 429 best_score = actual->relevance;
389 } 430 }
390 431
391 EXPECT_EQ(can_inline_top_result, ac_matches_[0].allowed_to_be_default_match); 432 EXPECT_EQ(can_inline_top_result, ac_matches_[0].allowed_to_be_default_match);
392 if (can_inline_top_result) 433 if (can_inline_top_result)
393 EXPECT_EQ(expected_autocompletion, ac_matches_[0].inline_autocompletion); 434 EXPECT_EQ(expected_autocompletion, ac_matches_[0].inline_autocompletion);
394 EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit); 435 EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit);
395 } 436 }
396 437
438 bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) {
439 base::CancelableTaskTracker task_tracker;
440 bool result = false;
441 history_service_->ScheduleDBTask(
442 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)),
443 &task_tracker);
444 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If
445 // the test hangs, DoneRunOnMainThread isn't being invoked correctly.
446 base::MessageLoop::current()->Run();
447 return result;
448 }
449
397 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { 450 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
398 std::vector<std::string> expected_urls; 451 std::vector<std::string> expected_urls;
399 expected_urls.push_back("http://slashdot.org/favorite_page.html"); 452 expected_urls.push_back("http://slashdot.org/favorite_page.html");
400 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 453 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
401 ASCIIToUTF16("slashdot.org/favorite_page.html"), 454 ASCIIToUTF16("slashdot.org/favorite_page.html"),
402 ASCIIToUTF16(".org/favorite_page.html")); 455 ASCIIToUTF16(".org/favorite_page.html"));
403 } 456 }
404 457
405 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) { 458 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) {
406 std::vector<std::string> expected_urls; 459 std::vector<std::string> expected_urls;
(...skipping 202 matching lines...)
609 662
610 TEST_F(HistoryQuickProviderTest, DeleteMatch) { 663 TEST_F(HistoryQuickProviderTest, DeleteMatch) {
611 GURL test_url("http://slashdot.org/favorite_page.html"); 664 GURL test_url("http://slashdot.org/favorite_page.html");
612 std::vector<std::string> expected_urls; 665 std::vector<std::string> expected_urls;
613 expected_urls.push_back(test_url.spec()); 666 expected_urls.push_back(test_url.spec());
614 // Fill up ac_matches_; we don't really care about the test yet. 667 // Fill up ac_matches_; we don't really care about the test yet.
615 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 668 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
616 ASCIIToUTF16("slashdot.org/favorite_page.html"), 669 ASCIIToUTF16("slashdot.org/favorite_page.html"),
617 ASCIIToUTF16(".org/favorite_page.html")); 670 ASCIIToUTF16(".org/favorite_page.html"));
618 EXPECT_EQ(1U, ac_matches_.size()); 671 EXPECT_EQ(1U, ac_matches_.size());
619 EXPECT_TRUE(history_backend()->GetURL(test_url, NULL)); 672 EXPECT_TRUE(GetURLProxy(test_url));
620 provider_->DeleteMatch(ac_matches_[0]); 673 provider_->DeleteMatch(ac_matches_[0]);
621 674
622 // Check that the underlying URL is deleted from the history DB (this implies 675 // Check that the underlying URL is deleted from the history DB (this implies
623 // that all visits are gone as well). Also verify that a deletion notification 676 // that all visits are gone as well). Also verify that a deletion notification
624 // is sent, in response to which the secondary data stores (InMemoryDatabase, 677 // is sent, in response to which the secondary data stores (InMemoryDatabase,
625 // InMemoryURLIndex) will drop any data they might have pertaining to the URL. 678 // InMemoryURLIndex) will drop any data they might have pertaining to the URL.
626 // To ensure that the deletion has been propagated everywhere before we start 679 // To ensure that the deletion has been propagated everywhere before we start
627 // verifying post-deletion states, first wait until we see the notification. 680 // verifying post-deletion states, first wait until we see the notification.
628 WaitForURLsDeletedNotification(history_service_); 681 WaitForURLsDeletedNotification(history_service_);
629 EXPECT_FALSE(history_backend()->GetURL(test_url, NULL)); 682 EXPECT_FALSE(GetURLProxy(test_url));
630 683
631 // Just to be on the safe side, explicitly verify that we have deleted enough 684 // Just to be on the safe side, explicitly verify that we have deleted enough
632 // data so that we will not be serving the same result again. 685 // data so that we will not be serving the same result again.
633 expected_urls.clear(); 686 expected_urls.clear();
634 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 687 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
635 ASCIIToUTF16("NONE EXPECTED"), base::string16()); 688 ASCIIToUTF16("NONE EXPECTED"), base::string16());
636 } 689 }
637 690
638 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { 691 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) {
639 std::vector<std::string> expected_urls; 692 std::vector<std::string> expected_urls;
(...skipping 190 matching lines...)
830 883
831 TEST_F(HQPOrderingTest, TEAMatch) { 884 TEST_F(HQPOrderingTest, TEAMatch) {
832 std::vector<std::string> expected_urls; 885 std::vector<std::string> expected_urls;
833 expected_urls.push_back("http://www.teamliquid.net/"); 886 expected_urls.push_back("http://www.teamliquid.net/");
834 expected_urls.push_back("http://www.teamliquid.net/tlpd"); 887 expected_urls.push_back("http://www.teamliquid.net/tlpd");
835 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); 888 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players");
836 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, 889 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true,
837 ASCIIToUTF16("www.teamliquid.net"), 890 ASCIIToUTF16("www.teamliquid.net"),
838 ASCIIToUTF16("mliquid.net")); 891 ASCIIToUTF16("mliquid.net"));
839 } 892 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine