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

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

Issue 1654833005: Componentizes shortcuts unittests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 20% Created 4 years, 10 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/omnibox/browser/shortcuts_backend.h"
6
7 #include <stddef.h>
8
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
17 #include "chrome/test/base/search_test_utils.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/omnibox/browser/shortcuts_database.h"
20 #include "components/search_engines/template_url_service.h"
21 #include "content/public/test/test_browser_thread.h"
22
23 #include "testing/gtest/include/gtest/gtest.h"
24
25
26 // ShortcutsBackendTest -------------------------------------------------------
27
28 class ShortcutsBackendTest : public testing::Test,
29 public ShortcutsBackend::ShortcutsBackendObserver {
30 public:
31 ShortcutsBackendTest();
32
33 ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting(
34 const std::string& url,
35 const std::string& contents_class = std::string(),
36 const std::string& description_class = std::string(),
37 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED);
38 void SetSearchProvider();
39
40 void SetUp() override;
41 void TearDown() override;
42
43 void OnShortcutsLoaded() override;
44 void OnShortcutsChanged() override;
45
46 const ShortcutsBackend::ShortcutMap& shortcuts_map() const {
47 return backend_->shortcuts_map();
48 }
49 bool changed_notified() const { return changed_notified_; }
50 void set_changed_notified(bool changed_notified) {
51 changed_notified_ = changed_notified;
52 }
53
54 void InitBackend();
55 bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut);
56 bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut);
57 bool DeleteShortcutsWithURL(const GURL& url);
58 bool DeleteShortcutsWithIDs(
59 const ShortcutsDatabase::ShortcutIDs& deleted_ids);
60
61 private:
62 base::MessageLoopForUI ui_message_loop_;
63 content::TestBrowserThread ui_thread_;
64 content::TestBrowserThread db_thread_;
65
66 protected:
67 TestingProfile profile_;
68 UIThreadSearchTermsData search_terms_data_;
69
70 private:
71 scoped_refptr<ShortcutsBackend> backend_;
72
73 bool load_notified_;
74 bool changed_notified_;
75
76 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
77 };
78
79 ShortcutsBackendTest::ShortcutsBackendTest()
80 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_),
81 db_thread_(content::BrowserThread::DB),
82 search_terms_data_(&profile_),
83 load_notified_(false),
84 changed_notified_(false) {
85 }
86
87 ShortcutsDatabase::Shortcut::MatchCore
88 ShortcutsBackendTest::MatchCoreForTesting(const std::string& url,
89 const std::string& contents_class,
90 const std::string& description_class,
91 AutocompleteMatch::Type type) {
92 AutocompleteMatch match(NULL, 0, 0, type);
93 match.destination_url = GURL(url);
94 match.contents = base::ASCIIToUTF16("test");
95 match.contents_class =
96 AutocompleteMatch::ClassificationsFromString(contents_class);
97 match.description_class =
98 AutocompleteMatch::ClassificationsFromString(description_class);
99 match.search_terms_args.reset(
100 new TemplateURLRef::SearchTermsArgs(match.contents));
101 return ShortcutsBackend::MatchToMatchCore(
102 match, TemplateURLServiceFactory::GetForProfile(&profile_),
103 &search_terms_data_);
104 }
105
106 void ShortcutsBackendTest::SetSearchProvider() {
107 TemplateURLService* template_url_service =
108 TemplateURLServiceFactory::GetForProfile(&profile_);
109 TemplateURLData data;
110 data.SetURL("http://foo.com/search?bar={searchTerms}");
111 data.SetShortName(base::UTF8ToUTF16("foo"));
112 data.SetKeyword(base::UTF8ToUTF16("foo"));
113
114 TemplateURL* template_url = new TemplateURL(data);
115 // Takes ownership of |template_url|.
116 template_url_service->Add(template_url);
117 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
118 }
119
120 void ShortcutsBackendTest::SetUp() {
121 db_thread_.Start();
122 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
123 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting);
124 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
125 ASSERT_TRUE(backend_.get());
126 backend_->AddObserver(this);
127
128 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
129 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
130 TemplateURLService* template_url_service =
131 TemplateURLServiceFactory::GetForProfile(&profile_);
132 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
133 }
134
135 void ShortcutsBackendTest::TearDown() {
136 backend_->RemoveObserver(this);
137 db_thread_.Stop();
138 }
139
140 void ShortcutsBackendTest::OnShortcutsLoaded() {
141 load_notified_ = true;
142 base::MessageLoop::current()->QuitWhenIdle();
143 }
144
145 void ShortcutsBackendTest::OnShortcutsChanged() {
146 changed_notified_ = true;
147 }
148
149 void ShortcutsBackendTest::InitBackend() {
150 ShortcutsBackend* backend =
151 ShortcutsBackendFactory::GetForProfile(&profile_).get();
152 ASSERT_TRUE(backend);
153 ASSERT_FALSE(load_notified_);
154 ASSERT_FALSE(backend_->initialized());
155 base::MessageLoop::current()->Run();
156 EXPECT_TRUE(load_notified_);
157 EXPECT_TRUE(backend_->initialized());
158 }
159
160 bool ShortcutsBackendTest::AddShortcut(
161 const ShortcutsDatabase::Shortcut& shortcut) {
162 return backend_->AddShortcut(shortcut);
163 }
164
165 bool ShortcutsBackendTest::UpdateShortcut(
166 const ShortcutsDatabase::Shortcut& shortcut) {
167 return backend_->UpdateShortcut(shortcut);
168 }
169
170 bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
171 return backend_->DeleteShortcutsWithURL(url);
172 }
173
174 bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
175 const ShortcutsDatabase::ShortcutIDs& deleted_ids) {
176 return backend_->DeleteShortcutsWithIDs(deleted_ids);
177 }
178
179
180 // Actual tests ---------------------------------------------------------------
181
182 // Verifies that creating MatchCores strips classifications and sanitizes match
183 // types.
184 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) {
185 struct {
186 std::string input_contents_class;
187 std::string input_description_class;
188 AutocompleteMatch::Type input_type;
189 std::string output_contents_class;
190 std::string output_description_class;
191 AutocompleteMatch::Type output_type;
192 } cases[] = {
193 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED,
194 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL },
195 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST,
196 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL },
197 { "0,1", "0,0,11,2,15,0",
198 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
199 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
200 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST,
201 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
202 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_ENTITY,
203 "", "", AutocompleteMatchType::SEARCH_HISTORY },
204 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_TAIL,
205 "", "", AutocompleteMatchType::SEARCH_HISTORY },
206 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED,
207 "", "", AutocompleteMatchType::SEARCH_HISTORY },
208 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PROFILE,
209 "", "", AutocompleteMatchType::SEARCH_HISTORY },
210 };
211
212 for (size_t i = 0; i < arraysize(cases); ++i) {
213 ShortcutsDatabase::Shortcut::MatchCore match_core(MatchCoreForTesting(
214 std::string(), cases[i].input_contents_class,
215 cases[i].input_description_class, cases[i].input_type));
216 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class)
217 << ":i:" << i << ":type:" << cases[i].input_type;
218 EXPECT_EQ(cases[i].output_description_class, match_core.description_class)
219 << ":i:" << i << ":type:" << cases[i].input_type;
220 EXPECT_EQ(cases[i].output_type, match_core.type)
221 << ":i:" << i << ":type:" << cases[i].input_type;
222 }
223 }
224
225 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) {
226 SetSearchProvider();
227 AutocompleteMatch match;
228 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt");
229 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY;
230 match.contents = base::UTF8ToUTF16("roosevelt");
231 match.contents_class =
232 AutocompleteMatch::ClassificationsFromString("0,0,5,2");
233 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt");
234 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4");
235 match.destination_url = GURL(
236 "http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234");
237 match.keyword = base::UTF8ToUTF16("foo");
238 match.search_terms_args.reset(
239 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit));
240
241 ShortcutsDatabase::Shortcut::MatchCore match_core =
242 ShortcutsBackend::MatchToMatchCore(
243 match, TemplateURLServiceFactory::GetForProfile(&profile_),
244 &search_terms_data_);
245 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt",
246 match_core.destination_url.spec());
247 EXPECT_EQ(match.fill_into_edit, match_core.contents);
248 EXPECT_EQ("0,0", match_core.contents_class);
249 EXPECT_EQ(base::string16(), match_core.description);
250 EXPECT_TRUE(match_core.description_class.empty());
251 }
252
253 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
254 InitBackend();
255 EXPECT_FALSE(changed_notified());
256
257 ShortcutsDatabase::Shortcut shortcut(
258 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
259 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
260 EXPECT_TRUE(AddShortcut(shortcut));
261 EXPECT_TRUE(changed_notified());
262 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter(
263 shortcuts_map().find(shortcut.text));
264 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
265 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
266 EXPECT_EQ(shortcut.match_core.contents,
267 shortcut_iter->second.match_core.contents);
268
269 set_changed_notified(false);
270 shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
271 EXPECT_TRUE(UpdateShortcut(shortcut));
272 EXPECT_TRUE(changed_notified());
273 shortcut_iter = shortcuts_map().find(shortcut.text);
274 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
275 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
276 EXPECT_EQ(shortcut.match_core.contents,
277 shortcut_iter->second.match_core.contents);
278 }
279
280 TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
281 InitBackend();
282 ShortcutsDatabase::Shortcut shortcut1(
283 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
284 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
285 EXPECT_TRUE(AddShortcut(shortcut1));
286
287 ShortcutsDatabase::Shortcut shortcut2(
288 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"),
289 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
290 EXPECT_TRUE(AddShortcut(shortcut2));
291
292 ShortcutsDatabase::Shortcut shortcut3(
293 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"),
294 MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10);
295 EXPECT_TRUE(AddShortcut(shortcut3));
296
297 ShortcutsDatabase::Shortcut shortcut4(
298 "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"),
299 MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10);
300 EXPECT_TRUE(AddShortcut(shortcut4));
301
302 ASSERT_EQ(4U, shortcuts_map().size());
303 EXPECT_EQ(shortcut1.id, shortcuts_map().find(shortcut1.text)->second.id);
304 EXPECT_EQ(shortcut2.id, shortcuts_map().find(shortcut2.text)->second.id);
305 EXPECT_EQ(shortcut3.id, shortcuts_map().find(shortcut3.text)->second.id);
306 EXPECT_EQ(shortcut4.id, shortcuts_map().find(shortcut4.text)->second.id);
307
308 EXPECT_TRUE(DeleteShortcutsWithURL(shortcut1.match_core.destination_url));
309
310 ASSERT_EQ(2U, shortcuts_map().size());
311 EXPECT_EQ(0U, shortcuts_map().count(shortcut1.text));
312 EXPECT_EQ(0U, shortcuts_map().count(shortcut2.text));
313 const ShortcutsBackend::ShortcutMap::const_iterator shortcut3_iter(
314 shortcuts_map().find(shortcut3.text));
315 ASSERT_TRUE(shortcut3_iter != shortcuts_map().end());
316 EXPECT_EQ(shortcut3.id, shortcut3_iter->second.id);
317 const ShortcutsBackend::ShortcutMap::const_iterator shortcut4_iter(
318 shortcuts_map().find(shortcut4.text));
319 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end());
320 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id);
321
322 ShortcutsDatabase::ShortcutIDs deleted_ids;
323 deleted_ids.push_back(shortcut3.id);
324 deleted_ids.push_back(shortcut4.id);
325 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
326
327 ASSERT_EQ(0U, shortcuts_map().size());
328 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698