Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

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

Issue 10701043: Make ShortcutsBackend a ProfileKeyedService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
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 "chrome/browser/autocomplete/shortcuts_provider.h" 5 #include "chrome/browser/autocomplete/shortcuts_provider.h"
6 6
7 #include <math.h> 7 #include <math.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/memory/scoped_ptr.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/message_loop.h" 16 #include "base/message_loop.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "chrome/browser/autocomplete/autocomplete.h" 19 #include "chrome/browser/autocomplete/autocomplete.h"
20 #include "chrome/browser/autocomplete/autocomplete_match.h" 20 #include "chrome/browser/autocomplete/autocomplete_match.h"
21 #include "chrome/browser/autocomplete/autocomplete_provider.h" 21 #include "chrome/browser/autocomplete/autocomplete_provider.h"
22 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 22 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
23 #include "chrome/browser/history/history.h" 23 #include "chrome/browser/history/history.h"
24 #include "chrome/browser/history/in_memory_url_index.h" 24 #include "chrome/browser/history/in_memory_url_index.h"
25 #include "chrome/browser/history/shortcuts_backend.h" 25 #include "chrome/browser/history/shortcuts_backend.h"
26 #include "chrome/browser/history/shortcuts_backend_factory.h"
26 #include "chrome/browser/history/url_database.h" 27 #include "chrome/browser/history/url_database.h"
27 #include "chrome/browser/prefs/pref_service.h" 28 #include "chrome/browser/prefs/pref_service.h"
28 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
29 #include "chrome/test/base/testing_profile.h" 30 #include "chrome/test/base/testing_profile.h"
30 #include "content/public/test/test_browser_thread.h" 31 #include "content/public/test/test_browser_thread.h"
31 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
32 33
33 using content::BrowserThread; 34 using content::BrowserThread;
35 using history::ShortcutsBackend;
34 36
35 namespace { 37 namespace {
36 38
37 struct TestShortcutInfo { 39 struct TestShortcutInfo {
38 std::string guid; 40 std::string guid;
39 std::string url; 41 std::string url;
40 std::string title; // The text that orginally was searched for. 42 std::string title; // The text that orginally was searched for.
41 std::string contents; 43 std::string contents;
42 std::string contents_class; 44 std::string contents_class;
43 std::string description; 45 std::string description;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // results' destination URLs match those provided. |expected_urls| does not 152 // results' destination URLs match those provided. |expected_urls| does not
151 // need to be in sorted order. 153 // need to be in sorted order.
152 void RunTest(const string16 text, 154 void RunTest(const string16 text,
153 std::vector<std::string> expected_urls, 155 std::vector<std::string> expected_urls,
154 std::string expected_top_result); 156 std::string expected_top_result);
155 157
156 MessageLoopForUI message_loop_; 158 MessageLoopForUI message_loop_;
157 content::TestBrowserThread ui_thread_; 159 content::TestBrowserThread ui_thread_;
158 content::TestBrowserThread file_thread_; 160 content::TestBrowserThread file_thread_;
159 161
160 scoped_ptr<TestingProfile> profile_; 162 TestingProfile profile_;
161 163
162 ACMatches ac_matches_; // The resulting matches after running RunTest. 164 ACMatches ac_matches_; // The resulting matches after running RunTest.
163 165
164 scoped_refptr<history::ShortcutsBackend> mock_backend_; 166 scoped_refptr<ShortcutsBackend> backend_;
165 scoped_refptr<ShortcutsProvider> provider_; 167 scoped_refptr<ShortcutsProvider> provider_;
166 }; 168 };
167 169
168 ShortcutsProviderTest::ShortcutsProviderTest() 170 ShortcutsProviderTest::ShortcutsProviderTest()
169 : ui_thread_(BrowserThread::UI, &message_loop_), 171 : ui_thread_(BrowserThread::UI, &message_loop_),
170 file_thread_(BrowserThread::FILE, &message_loop_) { 172 file_thread_(BrowserThread::FILE, &message_loop_) {
171 } 173 }
172 174
173 void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches) { 175 void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches) {}
174 }
175 176
176 void ShortcutsProviderTest::SetUp() { 177 void ShortcutsProviderTest::SetUp() {
177 profile_.reset(new TestingProfile()); 178 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
178 profile_->CreateHistoryService(true, false); 179 &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting);
179 provider_ = new ShortcutsProvider(this, profile_.get()); 180 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
180 mock_backend_ = new history::ShortcutsBackend(FilePath(), profile_.get()); 181 ASSERT_TRUE(backend_.get());
181 mock_backend_->Init(); 182 profile_.CreateHistoryService(true, false);
182 provider_->set_shortcuts_backend(mock_backend_.get()); 183 provider_ = new ShortcutsProvider(this, &profile_);
183 FillData(shortcut_test_db, arraysize(shortcut_test_db)); 184 FillData(shortcut_test_db, arraysize(shortcut_test_db));
184 } 185 }
185 186
186 void ShortcutsProviderTest::TearDown() { 187 void ShortcutsProviderTest::TearDown() {
187 // Run all pending tasks or else some threads hold on to the message loop 188 // Run all pending tasks or else some threads hold on to the message loop
188 // and prevent it from being deleted. 189 // and prevent it from being deleted.
189 message_loop_.RunAllPending(); 190 message_loop_.RunAllPending();
190 provider_ = NULL; 191 provider_ = NULL;
191 } 192 }
192 193
193 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { 194 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) {
194 DCHECK(provider_.get()); 195 DCHECK(provider_.get());
196 size_t expected_size = backend_->shortcuts_map().size() + db_size;
195 for (size_t i = 0; i < db_size; ++i) { 197 for (size_t i = 0; i < db_size; ++i) {
196 const TestShortcutInfo& cur = db[i]; 198 const TestShortcutInfo& cur = db[i];
197 history::ShortcutsBackend::Shortcut shortcut(cur.guid, 199 ShortcutsBackend::Shortcut shortcut(cur.guid,
198 ASCIIToUTF16(cur.title), GURL(cur.url), ASCIIToUTF16(cur.contents), 200 ASCIIToUTF16(cur.title), GURL(cur.url), ASCIIToUTF16(cur.contents),
199 AutocompleteMatch::ClassificationsFromString(cur.contents_class), 201 AutocompleteMatch::ClassificationsFromString(cur.contents_class),
200 ASCIIToUTF16(cur.description), 202 ASCIIToUTF16(cur.description),
201 AutocompleteMatch::ClassificationsFromString(cur.description_class), 203 AutocompleteMatch::ClassificationsFromString(cur.description_class),
202 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), 204 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
203 cur.typed_count); 205 cur.typed_count);
204 mock_backend_->AddShortcut(shortcut); 206 backend_->AddShortcut(shortcut);
205 } 207 }
208 EXPECT_EQ(expected_size, backend_->shortcuts_map().size());
206 } 209 }
207 210
208 ShortcutsProviderTest::SetShouldContain::SetShouldContain( 211 ShortcutsProviderTest::SetShouldContain::SetShouldContain(
209 const ACMatches& matched_urls) { 212 const ACMatches& matched_urls) {
210 for (ACMatches::const_iterator iter = matched_urls.begin(); 213 for (ACMatches::const_iterator iter = matched_urls.begin();
211 iter != matched_urls.end(); ++iter) 214 iter != matched_urls.end(); ++iter)
212 matches_.insert(iter->destination_url.spec()); 215 matches_.insert(iter->destination_url.spec());
213 } 216 }
214 217
215 void ShortcutsProviderTest::SetShouldContain::operator()( 218 void ShortcutsProviderTest::SetShouldContain::operator()(
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 ACMatchClassifications spans_content; 556 ACMatchClassifications spans_content;
554 spans_content.push_back(ACMatchClassification(0, ACMatchClassification::URL)); 557 spans_content.push_back(ACMatchClassification(0, ACMatchClassification::URL));
555 spans_content.push_back(ACMatchClassification( 558 spans_content.push_back(ACMatchClassification(
556 4, ACMatchClassification::MATCH | ACMatchClassification::URL)); 559 4, ACMatchClassification::MATCH | ACMatchClassification::URL));
557 spans_content.push_back(ACMatchClassification(8, ACMatchClassification::URL)); 560 spans_content.push_back(ACMatchClassification(8, ACMatchClassification::URL));
558 ACMatchClassifications spans_description; 561 ACMatchClassifications spans_description;
559 spans_description.push_back( 562 spans_description.push_back(
560 ACMatchClassification(0, ACMatchClassification::NONE)); 563 ACMatchClassification(0, ACMatchClassification::NONE));
561 spans_description.push_back( 564 spans_description.push_back(
562 ACMatchClassification(2, ACMatchClassification::MATCH)); 565 ACMatchClassification(2, ACMatchClassification::MATCH));
563 history::ShortcutsBackend::Shortcut shortcut(std::string(), 566 ShortcutsBackend::Shortcut shortcut(std::string(),
564 ASCIIToUTF16("test"), GURL("http://www.test.com"), 567 ASCIIToUTF16("test"), GURL("http://www.test.com"),
565 ASCIIToUTF16("www.test.com"), spans_content, ASCIIToUTF16("A test"), 568 ASCIIToUTF16("www.test.com"), spans_content, ASCIIToUTF16("A test"),
566 spans_description, base::Time::Now(), 1); 569 spans_description, base::Time::Now(), 1);
567 570
568 // Maximal score. 571 // Maximal score.
569 const int kMaxScore = ShortcutsProvider::CalculateScore( 572 const int kMaxScore = ShortcutsProvider::CalculateScore(
570 ASCIIToUTF16("test"), shortcut); 573 ASCIIToUTF16("test"), shortcut);
571 574
572 // Score decreases as percent of the match is decreased. 575 // Score decreases as percent of the match is decreased.
573 int score_three_quarters = 576 int score_three_quarters =
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", 624 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2",
622 "http://www.deletetest.com/1.html", "erase", 625 "http://www.deletetest.com/1.html", "erase",
623 "http://www.deletetest.com/1.html", "0,2", 626 "http://www.deletetest.com/1.html", "0,2",
624 "Erase this shortcut!", "0,0", 1, 1}, 627 "Erase this shortcut!", "0,0", 1, 1},
625 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", 628 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3",
626 "http://www.deletetest.com/2.html", "delete", 629 "http://www.deletetest.com/2.html", "delete",
627 "http://www.deletetest.com/2.html", "0,2", 630 "http://www.deletetest.com/2.html", "0,2",
628 "Erase this shortcut!", "0,0", 1, 1}, 631 "Erase this shortcut!", "0,0", 1, 1},
629 }; 632 };
630 633
631 size_t original_shortcuts_count = 634 size_t original_shortcuts_count = backend_->shortcuts_map().size();
632 provider_->shortcuts_backend_->shortcuts_map().size();
633 635
634 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); 636 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete));
635 637
636 EXPECT_EQ(original_shortcuts_count + 3, 638 EXPECT_EQ(original_shortcuts_count + 3, backend_->shortcuts_map().size());
637 provider_->shortcuts_backend_->shortcuts_map().size()); 639 EXPECT_FALSE(backend_->shortcuts_map().end() ==
638 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == 640 backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
639 provider_->shortcuts_backend_->shortcuts_map().find( 641 EXPECT_FALSE(backend_->shortcuts_map().end() ==
640 ASCIIToUTF16("delete"))); 642 backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
641 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() ==
642 provider_->shortcuts_backend_->shortcuts_map().find(
643 ASCIIToUTF16("erase")));
644 643
645 AutocompleteMatch match(provider_, 1200, true, 644 AutocompleteMatch match(provider_, 1200, true,
646 AutocompleteMatch::HISTORY_TITLE); 645 AutocompleteMatch::HISTORY_TITLE);
647 646
648 match.destination_url = GURL(shortcuts_to_test_delete[0].url); 647 match.destination_url = GURL(shortcuts_to_test_delete[0].url);
649 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents); 648 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents);
650 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description); 649 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description);
651 650
652 provider_->DeleteMatch(match); 651 provider_->DeleteMatch(match);
653 652
654 // |shortcuts_to_test_delete[0]| and |shortcuts_to_test_delete[1]| should be 653 // |shortcuts_to_test_delete[0]| and |shortcuts_to_test_delete[1]| should be
655 // deleted, but not |shortcuts_to_test_delete[2]| as it has different url. 654 // deleted, but not |shortcuts_to_test_delete[2]| as it has different url.
656 EXPECT_EQ(original_shortcuts_count + 1, 655 EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size());
657 provider_->shortcuts_backend_->shortcuts_map().size()); 656 EXPECT_FALSE(backend_->shortcuts_map().end() ==
658 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == 657 backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
659 provider_->shortcuts_backend_->shortcuts_map().find( 658 EXPECT_TRUE(backend_->shortcuts_map().end() ==
660 ASCIIToUTF16("delete"))); 659 backend_->shortcuts_map().find(ASCIIToUTF16("erase")));
661 EXPECT_TRUE(provider_->shortcuts_backend_->shortcuts_map().end() ==
662 provider_->shortcuts_backend_->shortcuts_map().find(
663 ASCIIToUTF16("erase")));
664 660
665 match.destination_url = GURL(shortcuts_to_test_delete[2].url); 661 match.destination_url = GURL(shortcuts_to_test_delete[2].url);
666 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[2].contents); 662 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[2].contents);
667 match.description = ASCIIToUTF16(shortcuts_to_test_delete[2].description); 663 match.description = ASCIIToUTF16(shortcuts_to_test_delete[2].description);
668 664
669 provider_->DeleteMatch(match); 665 provider_->DeleteMatch(match);
670 EXPECT_EQ(original_shortcuts_count, 666 EXPECT_EQ(original_shortcuts_count, backend_->shortcuts_map().size());
671 provider_->shortcuts_backend_->shortcuts_map().size()); 667 EXPECT_TRUE(backend_->shortcuts_map().end() ==
672 EXPECT_TRUE(provider_->shortcuts_backend_->shortcuts_map().end() == 668 backend_->shortcuts_map().find(ASCIIToUTF16("delete")));
673 provider_->shortcuts_backend_->shortcuts_map().find(
674 ASCIIToUTF16("delete")));
675 } 669 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698