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 "chrome/browser/autocomplete/shortcuts_backend.h" | 5 #include "chrome/browser/autocomplete/shortcuts_backend.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" | 12 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" |
13 #include "chrome/browser/history/shortcuts_database.h" | 13 #include "chrome/browser/history/shortcuts_database.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
14 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
16 #include "sql/statement.h" | 19 #include "sql/statement.h" |
17 | 20 |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
20 | 23 |
21 // ShortcutsBackendTest ------------------------------------------------------- | 24 // ShortcutsBackendTest ------------------------------------------------------- |
22 | 25 |
23 class ShortcutsBackendTest : public testing::Test, | 26 class ShortcutsBackendTest : public testing::Test, |
24 public ShortcutsBackend::ShortcutsBackendObserver { | 27 public ShortcutsBackend::ShortcutsBackendObserver { |
25 public: | 28 public: |
26 ShortcutsBackendTest(); | 29 ShortcutsBackendTest(); |
27 | 30 |
28 history::ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting( | 31 history::ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting( |
29 const std::string& url, | 32 const std::string& url, |
30 const std::string& contents_class = std::string(), | 33 const std::string& contents_class = std::string(), |
31 const std::string& description_class = std::string(), | 34 const std::string& description_class = std::string(), |
32 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 35 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
| 36 void SetSearchProvider(); |
33 | 37 |
34 virtual void SetUp(); | 38 virtual void SetUp(); |
35 virtual void TearDown(); | 39 virtual void TearDown(); |
36 | 40 |
37 virtual void OnShortcutsLoaded() OVERRIDE; | 41 virtual void OnShortcutsLoaded() OVERRIDE; |
38 virtual void OnShortcutsChanged() OVERRIDE; | 42 virtual void OnShortcutsChanged() OVERRIDE; |
39 | 43 |
40 const ShortcutsBackend::ShortcutMap& shortcuts_map() const { | 44 const ShortcutsBackend::ShortcutMap& shortcuts_map() const { |
41 return backend_->shortcuts_map(); | 45 return backend_->shortcuts_map(); |
42 } | 46 } |
43 bool changed_notified() const { return changed_notified_; } | 47 bool changed_notified() const { return changed_notified_; } |
44 void set_changed_notified(bool changed_notified) { | 48 void set_changed_notified(bool changed_notified) { |
45 changed_notified_ = changed_notified; | 49 changed_notified_ = changed_notified; |
46 } | 50 } |
47 | 51 |
48 void InitBackend(); | 52 void InitBackend(); |
49 bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); | 53 bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); |
50 bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); | 54 bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); |
51 bool DeleteShortcutsWithURL(const GURL& url); | 55 bool DeleteShortcutsWithURL(const GURL& url); |
52 bool DeleteShortcutsWithIDs( | 56 bool DeleteShortcutsWithIDs( |
53 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids); | 57 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids); |
54 | 58 |
| 59 protected: |
| 60 TestingProfile profile_; |
55 | 61 |
56 private: | 62 private: |
57 TestingProfile profile_; | |
58 scoped_refptr<ShortcutsBackend> backend_; | 63 scoped_refptr<ShortcutsBackend> backend_; |
59 base::MessageLoopForUI ui_message_loop_; | 64 base::MessageLoopForUI ui_message_loop_; |
60 content::TestBrowserThread ui_thread_; | 65 content::TestBrowserThread ui_thread_; |
61 content::TestBrowserThread db_thread_; | 66 content::TestBrowserThread db_thread_; |
62 | 67 |
63 bool load_notified_; | 68 bool load_notified_; |
64 bool changed_notified_; | 69 bool changed_notified_; |
65 | 70 |
66 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest); | 71 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest); |
67 }; | 72 }; |
(...skipping 11 matching lines...) Expand all Loading... |
79 const std::string& contents_class, | 84 const std::string& contents_class, |
80 const std::string& description_class, | 85 const std::string& description_class, |
81 AutocompleteMatch::Type type) { | 86 AutocompleteMatch::Type type) { |
82 AutocompleteMatch match(NULL, 0, 0, type); | 87 AutocompleteMatch match(NULL, 0, 0, type); |
83 match.destination_url = GURL(url); | 88 match.destination_url = GURL(url); |
84 match.contents = base::ASCIIToUTF16("test"); | 89 match.contents = base::ASCIIToUTF16("test"); |
85 match.contents_class = | 90 match.contents_class = |
86 AutocompleteMatch::ClassificationsFromString(contents_class); | 91 AutocompleteMatch::ClassificationsFromString(contents_class); |
87 match.description_class = | 92 match.description_class = |
88 AutocompleteMatch::ClassificationsFromString(description_class); | 93 AutocompleteMatch::ClassificationsFromString(description_class); |
89 return ShortcutsBackend::MatchToMatchCore(match); | 94 match.search_terms_args.reset( |
| 95 new TemplateURLRef::SearchTermsArgs(match.contents)); |
| 96 return ShortcutsBackend::MatchToMatchCore(match, &profile_); |
| 97 } |
| 98 |
| 99 void ShortcutsBackendTest::SetSearchProvider() { |
| 100 TemplateURLService* template_url_service = |
| 101 TemplateURLServiceFactory::GetForProfile(&profile_); |
| 102 TemplateURLData data; |
| 103 data.SetURL("http://foo.com/search?bar={searchTerms}"); |
| 104 data.SetKeyword(base::UTF8ToUTF16("foo")); |
| 105 |
| 106 TemplateURL* template_url = new TemplateURL(&profile_, data); |
| 107 // Takes ownership of |template_url|. |
| 108 template_url_service->Add(template_url); |
| 109 template_url_service->SetDefaultSearchProvider(template_url); |
90 } | 110 } |
91 | 111 |
92 void ShortcutsBackendTest::SetUp() { | 112 void ShortcutsBackendTest::SetUp() { |
93 db_thread_.Start(); | 113 db_thread_.Start(); |
94 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 114 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( |
95 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); | 115 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); |
96 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 116 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); |
97 ASSERT_TRUE(backend_.get()); | 117 ASSERT_TRUE(backend_.get()); |
98 backend_->AddObserver(this); | 118 backend_->AddObserver(this); |
| 119 |
| 120 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 121 &profile_, &TemplateURLServiceFactory::BuildInstanceFor); |
| 122 TemplateURLService* template_url_service = |
| 123 TemplateURLServiceFactory::GetForProfile(&profile_); |
| 124 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
99 } | 125 } |
100 | 126 |
101 void ShortcutsBackendTest::TearDown() { | 127 void ShortcutsBackendTest::TearDown() { |
102 backend_->RemoveObserver(this); | 128 backend_->RemoveObserver(this); |
103 db_thread_.Stop(); | 129 db_thread_.Stop(); |
104 } | 130 } |
105 | 131 |
106 void ShortcutsBackendTest::OnShortcutsLoaded() { | 132 void ShortcutsBackendTest::OnShortcutsLoaded() { |
107 load_notified_ = true; | 133 load_notified_ = true; |
108 base::MessageLoop::current()->Quit(); | 134 base::MessageLoop::current()->Quit(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // types. | 175 // types. |
150 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) { | 176 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) { |
151 struct { | 177 struct { |
152 std::string input_contents_class; | 178 std::string input_contents_class; |
153 std::string input_description_class; | 179 std::string input_description_class; |
154 AutocompleteMatch::Type input_type; | 180 AutocompleteMatch::Type input_type; |
155 std::string output_contents_class; | 181 std::string output_contents_class; |
156 std::string output_description_class; | 182 std::string output_description_class; |
157 AutocompleteMatch::Type output_type; | 183 AutocompleteMatch::Type output_type; |
158 } cases[] = { | 184 } cases[] = { |
159 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, | 185 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, |
160 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, | 186 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, |
161 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, | 187 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, |
162 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, | 188 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, |
163 { "0,1", "0,0,11,2,15,0", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 189 { "0,1", "0,0,11,2,15,0", |
164 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, | 190 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
165 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, | 191 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
166 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, | 192 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, |
| 193 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
| 194 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_ENTITY, |
| 195 "", "", AutocompleteMatchType::SEARCH_HISTORY }, |
| 196 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_INFINITE, |
| 197 "", "", AutocompleteMatchType::SEARCH_HISTORY }, |
| 198 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED, |
| 199 "", "", AutocompleteMatchType::SEARCH_HISTORY }, |
| 200 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PROFILE, |
| 201 "", "", AutocompleteMatchType::SEARCH_HISTORY }, |
167 }; | 202 }; |
168 | 203 |
169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
170 history::ShortcutsDatabase::Shortcut::MatchCore match_core( | 205 history::ShortcutsDatabase::Shortcut::MatchCore match_core( |
171 MatchCoreForTesting(std::string(), cases[i].input_contents_class, | 206 MatchCoreForTesting(std::string(), cases[i].input_contents_class, |
172 cases[i].input_description_class, | 207 cases[i].input_description_class, |
173 cases[i].input_type)); | 208 cases[i].input_type)); |
174 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class); | 209 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class) |
175 EXPECT_EQ(cases[i].output_description_class, match_core.description_class); | 210 << ":i:" << i << ":type:" << cases[i].input_type; |
176 EXPECT_EQ(cases[i].output_type, match_core.type); | 211 EXPECT_EQ(cases[i].output_description_class, match_core.description_class) |
| 212 << ":i:" << i << ":type:" << cases[i].input_type; |
| 213 EXPECT_EQ(cases[i].output_type, match_core.type) |
| 214 << ":i:" << i << ":type:" << cases[i].input_type; |
177 } | 215 } |
178 } | 216 } |
179 | 217 |
| 218 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) { |
| 219 SetSearchProvider(); |
| 220 AutocompleteMatch match; |
| 221 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt"); |
| 222 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY; |
| 223 match.contents = base::UTF8ToUTF16("roosevelt"); |
| 224 match.contents_class = |
| 225 AutocompleteMatch::ClassificationsFromString("0,0,5,2"); |
| 226 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt"); |
| 227 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4"); |
| 228 match.destination_url = GURL( |
| 229 "http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234"); |
| 230 match.keyword = base::UTF8ToUTF16("foo"); |
| 231 match.search_terms_args.reset( |
| 232 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit)); |
| 233 |
| 234 history::ShortcutsDatabase::Shortcut::MatchCore match_core = |
| 235 ShortcutsBackend::MatchToMatchCore(match, &profile_); |
| 236 |
| 237 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt", |
| 238 match_core.destination_url.spec()); |
| 239 EXPECT_EQ(match.fill_into_edit, match_core.contents); |
| 240 EXPECT_EQ("0,0", match_core.contents_class); |
| 241 EXPECT_EQ(base::string16(), match_core.description); |
| 242 EXPECT_TRUE(match_core.description_class.empty()); |
| 243 } |
| 244 |
180 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { | 245 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { |
181 InitBackend(); | 246 InitBackend(); |
182 EXPECT_FALSE(changed_notified()); | 247 EXPECT_FALSE(changed_notified()); |
183 | 248 |
184 history::ShortcutsDatabase::Shortcut shortcut( | 249 history::ShortcutsDatabase::Shortcut shortcut( |
185 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), | 250 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), |
186 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | 251 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); |
187 EXPECT_TRUE(AddShortcut(shortcut)); | 252 EXPECT_TRUE(AddShortcut(shortcut)); |
188 EXPECT_TRUE(changed_notified()); | 253 EXPECT_TRUE(changed_notified()); |
189 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter( | 254 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); | 311 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); |
247 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); | 312 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); |
248 | 313 |
249 history::ShortcutsDatabase::ShortcutIDs deleted_ids; | 314 history::ShortcutsDatabase::ShortcutIDs deleted_ids; |
250 deleted_ids.push_back(shortcut3.id); | 315 deleted_ids.push_back(shortcut3.id); |
251 deleted_ids.push_back(shortcut4.id); | 316 deleted_ids.push_back(shortcut4.id); |
252 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); | 317 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); |
253 | 318 |
254 ASSERT_EQ(0U, shortcuts_map().size()); | 319 ASSERT_EQ(0U, shortcuts_map().size()); |
255 } | 320 } |
OLD | NEW |