Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / autocomplete / shortcuts_backend_unittest.cc
blobf74f39a102eaef9677c0d13dca6cf66c8cc5e387
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.
5 #include "chrome/browser/autocomplete/shortcuts_backend.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
12 #include "chrome/browser/autocomplete/shortcuts_database.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "sql/statement.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 // ShortcutsBackendTest -------------------------------------------------------
25 class ShortcutsBackendTest : public testing::Test,
26 public ShortcutsBackend::ShortcutsBackendObserver {
27 public:
28 ShortcutsBackendTest();
30 ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting(
31 const std::string& url,
32 const std::string& contents_class = std::string(),
33 const std::string& description_class = std::string(),
34 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED);
35 void SetSearchProvider();
37 void SetUp() override;
38 void TearDown() override;
40 void OnShortcutsLoaded() override;
41 void OnShortcutsChanged() override;
43 const ShortcutsBackend::ShortcutMap& shortcuts_map() const {
44 return backend_->shortcuts_map();
46 bool changed_notified() const { return changed_notified_; }
47 void set_changed_notified(bool changed_notified) {
48 changed_notified_ = changed_notified;
51 void InitBackend();
52 bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut);
53 bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut);
54 bool DeleteShortcutsWithURL(const GURL& url);
55 bool DeleteShortcutsWithIDs(
56 const ShortcutsDatabase::ShortcutIDs& deleted_ids);
58 protected:
59 TestingProfile profile_;
61 private:
62 scoped_refptr<ShortcutsBackend> backend_;
63 base::MessageLoopForUI ui_message_loop_;
64 content::TestBrowserThread ui_thread_;
65 content::TestBrowserThread db_thread_;
67 bool load_notified_;
68 bool changed_notified_;
70 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest);
73 ShortcutsBackendTest::ShortcutsBackendTest()
74 : ui_thread_(content::BrowserThread::UI, &ui_message_loop_),
75 db_thread_(content::BrowserThread::DB),
76 load_notified_(false),
77 changed_notified_(false) {
80 ShortcutsDatabase::Shortcut::MatchCore
81 ShortcutsBackendTest::MatchCoreForTesting(const std::string& url,
82 const std::string& contents_class,
83 const std::string& description_class,
84 AutocompleteMatch::Type type) {
85 AutocompleteMatch match(NULL, 0, 0, type);
86 match.destination_url = GURL(url);
87 match.contents = base::ASCIIToUTF16("test");
88 match.contents_class =
89 AutocompleteMatch::ClassificationsFromString(contents_class);
90 match.description_class =
91 AutocompleteMatch::ClassificationsFromString(description_class);
92 match.search_terms_args.reset(
93 new TemplateURLRef::SearchTermsArgs(match.contents));
94 return ShortcutsBackend::MatchToMatchCore(match, &profile_);
97 void ShortcutsBackendTest::SetSearchProvider() {
98 TemplateURLService* template_url_service =
99 TemplateURLServiceFactory::GetForProfile(&profile_);
100 TemplateURLData data;
101 data.SetURL("http://foo.com/search?bar={searchTerms}");
102 data.SetKeyword(base::UTF8ToUTF16("foo"));
104 TemplateURL* template_url = new TemplateURL(data);
105 // Takes ownership of |template_url|.
106 template_url_service->Add(template_url);
107 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
110 void ShortcutsBackendTest::SetUp() {
111 db_thread_.Start();
112 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
113 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting);
114 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
115 ASSERT_TRUE(backend_.get());
116 backend_->AddObserver(this);
118 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
119 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
120 TemplateURLService* template_url_service =
121 TemplateURLServiceFactory::GetForProfile(&profile_);
122 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
125 void ShortcutsBackendTest::TearDown() {
126 backend_->RemoveObserver(this);
127 db_thread_.Stop();
130 void ShortcutsBackendTest::OnShortcutsLoaded() {
131 load_notified_ = true;
132 base::MessageLoop::current()->Quit();
135 void ShortcutsBackendTest::OnShortcutsChanged() {
136 changed_notified_ = true;
139 void ShortcutsBackendTest::InitBackend() {
140 ShortcutsBackend* backend =
141 ShortcutsBackendFactory::GetForProfile(&profile_).get();
142 ASSERT_TRUE(backend);
143 ASSERT_FALSE(load_notified_);
144 ASSERT_FALSE(backend_->initialized());
145 base::MessageLoop::current()->Run();
146 EXPECT_TRUE(load_notified_);
147 EXPECT_TRUE(backend_->initialized());
150 bool ShortcutsBackendTest::AddShortcut(
151 const ShortcutsDatabase::Shortcut& shortcut) {
152 return backend_->AddShortcut(shortcut);
155 bool ShortcutsBackendTest::UpdateShortcut(
156 const ShortcutsDatabase::Shortcut& shortcut) {
157 return backend_->UpdateShortcut(shortcut);
160 bool ShortcutsBackendTest::DeleteShortcutsWithURL(const GURL& url) {
161 return backend_->DeleteShortcutsWithURL(url);
164 bool ShortcutsBackendTest::DeleteShortcutsWithIDs(
165 const ShortcutsDatabase::ShortcutIDs& deleted_ids) {
166 return backend_->DeleteShortcutsWithIDs(deleted_ids);
170 // Actual tests ---------------------------------------------------------------
172 // Verifies that creating MatchCores strips classifications and sanitizes match
173 // types.
174 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) {
175 struct {
176 std::string input_contents_class;
177 std::string input_description_class;
178 AutocompleteMatch::Type input_type;
179 std::string output_contents_class;
180 std::string output_description_class;
181 AutocompleteMatch::Type output_type;
182 } cases[] = {
183 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED,
184 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL },
185 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST,
186 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL },
187 { "0,1", "0,0,11,2,15,0",
188 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
189 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
190 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST,
191 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY },
192 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_ENTITY,
193 "", "", AutocompleteMatchType::SEARCH_HISTORY },
194 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_TAIL,
195 "", "", AutocompleteMatchType::SEARCH_HISTORY },
196 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED,
197 "", "", AutocompleteMatchType::SEARCH_HISTORY },
198 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PROFILE,
199 "", "", AutocompleteMatchType::SEARCH_HISTORY },
202 for (size_t i = 0; i < arraysize(cases); ++i) {
203 ShortcutsDatabase::Shortcut::MatchCore match_core(MatchCoreForTesting(
204 std::string(), cases[i].input_contents_class,
205 cases[i].input_description_class, cases[i].input_type));
206 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class)
207 << ":i:" << i << ":type:" << cases[i].input_type;
208 EXPECT_EQ(cases[i].output_description_class, match_core.description_class)
209 << ":i:" << i << ":type:" << cases[i].input_type;
210 EXPECT_EQ(cases[i].output_type, match_core.type)
211 << ":i:" << i << ":type:" << cases[i].input_type;
215 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) {
216 SetSearchProvider();
217 AutocompleteMatch match;
218 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt");
219 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY;
220 match.contents = base::UTF8ToUTF16("roosevelt");
221 match.contents_class =
222 AutocompleteMatch::ClassificationsFromString("0,0,5,2");
223 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt");
224 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4");
225 match.destination_url = GURL(
226 "http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234");
227 match.keyword = base::UTF8ToUTF16("foo");
228 match.search_terms_args.reset(
229 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit));
231 ShortcutsDatabase::Shortcut::MatchCore match_core =
232 ShortcutsBackend::MatchToMatchCore(match, &profile_);
234 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt",
235 match_core.destination_url.spec());
236 EXPECT_EQ(match.fill_into_edit, match_core.contents);
237 EXPECT_EQ("0,0", match_core.contents_class);
238 EXPECT_EQ(base::string16(), match_core.description);
239 EXPECT_TRUE(match_core.description_class.empty());
242 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
243 InitBackend();
244 EXPECT_FALSE(changed_notified());
246 ShortcutsDatabase::Shortcut shortcut(
247 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
248 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
249 EXPECT_TRUE(AddShortcut(shortcut));
250 EXPECT_TRUE(changed_notified());
251 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter(
252 shortcuts_map().find(shortcut.text));
253 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
254 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
255 EXPECT_EQ(shortcut.match_core.contents,
256 shortcut_iter->second.match_core.contents);
258 set_changed_notified(false);
259 shortcut.match_core.contents = base::ASCIIToUTF16("Google Web Search");
260 EXPECT_TRUE(UpdateShortcut(shortcut));
261 EXPECT_TRUE(changed_notified());
262 shortcut_iter = shortcuts_map().find(shortcut.text);
263 ASSERT_TRUE(shortcut_iter != shortcuts_map().end());
264 EXPECT_EQ(shortcut.id, shortcut_iter->second.id);
265 EXPECT_EQ(shortcut.match_core.contents,
266 shortcut_iter->second.match_core.contents);
269 TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
270 InitBackend();
271 ShortcutsDatabase::Shortcut shortcut1(
272 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"),
273 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
274 EXPECT_TRUE(AddShortcut(shortcut1));
276 ShortcutsDatabase::Shortcut shortcut2(
277 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", base::ASCIIToUTF16("gle"),
278 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100);
279 EXPECT_TRUE(AddShortcut(shortcut2));
281 ShortcutsDatabase::Shortcut shortcut3(
282 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", base::ASCIIToUTF16("sp"),
283 MatchCoreForTesting("http://www.sport.com"), base::Time::Now(), 10);
284 EXPECT_TRUE(AddShortcut(shortcut3));
286 ShortcutsDatabase::Shortcut shortcut4(
287 "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", base::ASCIIToUTF16("mov"),
288 MatchCoreForTesting("http://www.film.com"), base::Time::Now(), 10);
289 EXPECT_TRUE(AddShortcut(shortcut4));
291 ASSERT_EQ(4U, shortcuts_map().size());
292 EXPECT_EQ(shortcut1.id, shortcuts_map().find(shortcut1.text)->second.id);
293 EXPECT_EQ(shortcut2.id, shortcuts_map().find(shortcut2.text)->second.id);
294 EXPECT_EQ(shortcut3.id, shortcuts_map().find(shortcut3.text)->second.id);
295 EXPECT_EQ(shortcut4.id, shortcuts_map().find(shortcut4.text)->second.id);
297 EXPECT_TRUE(DeleteShortcutsWithURL(shortcut1.match_core.destination_url));
299 ASSERT_EQ(2U, shortcuts_map().size());
300 EXPECT_EQ(0U, shortcuts_map().count(shortcut1.text));
301 EXPECT_EQ(0U, shortcuts_map().count(shortcut2.text));
302 const ShortcutsBackend::ShortcutMap::const_iterator shortcut3_iter(
303 shortcuts_map().find(shortcut3.text));
304 ASSERT_TRUE(shortcut3_iter != shortcuts_map().end());
305 EXPECT_EQ(shortcut3.id, shortcut3_iter->second.id);
306 const ShortcutsBackend::ShortcutMap::const_iterator shortcut4_iter(
307 shortcuts_map().find(shortcut4.text));
308 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end());
309 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id);
311 ShortcutsDatabase::ShortcutIDs deleted_ids;
312 deleted_ids.push_back(shortcut3.id);
313 deleted_ids.push_back(shortcut4.id);
314 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids));
316 ASSERT_EQ(0U, shortcuts_map().size());