| 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" | |
| 16 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | |
| 18 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 19 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 20 | 17 |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 19 |
| 23 | 20 |
| 24 // ShortcutsBackendTest ------------------------------------------------------- | 21 // ShortcutsBackendTest ------------------------------------------------------- |
| 25 | 22 |
| 26 class ShortcutsBackendTest : public testing::Test, | 23 class ShortcutsBackendTest : public testing::Test, |
| 27 public ShortcutsBackend::ShortcutsBackendObserver { | 24 public ShortcutsBackend::ShortcutsBackendObserver { |
| 28 public: | 25 public: |
| 29 ShortcutsBackendTest(); | 26 ShortcutsBackendTest(); |
| 30 | 27 |
| 31 history::ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting( | 28 history::ShortcutsDatabase::Shortcut::MatchCore MatchCoreForTesting( |
| 32 const std::string& url, | 29 const std::string& url, |
| 33 const std::string& contents_class = std::string(), | 30 const std::string& contents_class = std::string(), |
| 34 const std::string& description_class = std::string(), | 31 const std::string& description_class = std::string(), |
| 35 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED); | 32 AutocompleteMatch::Type type = AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
| 36 void SetSearchProvider(); | |
| 37 | 33 |
| 38 virtual void SetUp(); | 34 virtual void SetUp(); |
| 39 virtual void TearDown(); | 35 virtual void TearDown(); |
| 40 | 36 |
| 41 virtual void OnShortcutsLoaded() OVERRIDE; | 37 virtual void OnShortcutsLoaded() OVERRIDE; |
| 42 virtual void OnShortcutsChanged() OVERRIDE; | 38 virtual void OnShortcutsChanged() OVERRIDE; |
| 43 | 39 |
| 44 const ShortcutsBackend::ShortcutMap& shortcuts_map() const { | 40 const ShortcutsBackend::ShortcutMap& shortcuts_map() const { |
| 45 return backend_->shortcuts_map(); | 41 return backend_->shortcuts_map(); |
| 46 } | 42 } |
| 47 bool changed_notified() const { return changed_notified_; } | 43 bool changed_notified() const { return changed_notified_; } |
| 48 void set_changed_notified(bool changed_notified) { | 44 void set_changed_notified(bool changed_notified) { |
| 49 changed_notified_ = changed_notified; | 45 changed_notified_ = changed_notified; |
| 50 } | 46 } |
| 51 | 47 |
| 52 void InitBackend(); | 48 void InitBackend(); |
| 53 bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); | 49 bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); |
| 54 bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); | 50 bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut); |
| 55 bool DeleteShortcutsWithURL(const GURL& url); | 51 bool DeleteShortcutsWithURL(const GURL& url); |
| 56 bool DeleteShortcutsWithIDs( | 52 bool DeleteShortcutsWithIDs( |
| 57 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids); | 53 const history::ShortcutsDatabase::ShortcutIDs& deleted_ids); |
| 58 | 54 |
| 59 protected: | |
| 60 TestingProfile profile_; | |
| 61 | 55 |
| 62 private: | 56 private: |
| 57 TestingProfile profile_; |
| 63 scoped_refptr<ShortcutsBackend> backend_; | 58 scoped_refptr<ShortcutsBackend> backend_; |
| 64 base::MessageLoopForUI ui_message_loop_; | 59 base::MessageLoopForUI ui_message_loop_; |
| 65 content::TestBrowserThread ui_thread_; | 60 content::TestBrowserThread ui_thread_; |
| 66 content::TestBrowserThread db_thread_; | 61 content::TestBrowserThread db_thread_; |
| 67 | 62 |
| 68 bool load_notified_; | 63 bool load_notified_; |
| 69 bool changed_notified_; | 64 bool changed_notified_; |
| 70 | 65 |
| 71 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest); | 66 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackendTest); |
| 72 }; | 67 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 const std::string& contents_class, | 79 const std::string& contents_class, |
| 85 const std::string& description_class, | 80 const std::string& description_class, |
| 86 AutocompleteMatch::Type type) { | 81 AutocompleteMatch::Type type) { |
| 87 AutocompleteMatch match(NULL, 0, 0, type); | 82 AutocompleteMatch match(NULL, 0, 0, type); |
| 88 match.destination_url = GURL(url); | 83 match.destination_url = GURL(url); |
| 89 match.contents = base::ASCIIToUTF16("test"); | 84 match.contents = base::ASCIIToUTF16("test"); |
| 90 match.contents_class = | 85 match.contents_class = |
| 91 AutocompleteMatch::ClassificationsFromString(contents_class); | 86 AutocompleteMatch::ClassificationsFromString(contents_class); |
| 92 match.description_class = | 87 match.description_class = |
| 93 AutocompleteMatch::ClassificationsFromString(description_class); | 88 AutocompleteMatch::ClassificationsFromString(description_class); |
| 94 match.search_terms_args.reset( | 89 return ShortcutsBackend::MatchToMatchCore(match); |
| 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); | |
| 110 } | 90 } |
| 111 | 91 |
| 112 void ShortcutsBackendTest::SetUp() { | 92 void ShortcutsBackendTest::SetUp() { |
| 113 db_thread_.Start(); | 93 db_thread_.Start(); |
| 114 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 94 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( |
| 115 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); | 95 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); |
| 116 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 96 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); |
| 117 ASSERT_TRUE(backend_.get()); | 97 ASSERT_TRUE(backend_.get()); |
| 118 backend_->AddObserver(this); | 98 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); | |
| 125 } | 99 } |
| 126 | 100 |
| 127 void ShortcutsBackendTest::TearDown() { | 101 void ShortcutsBackendTest::TearDown() { |
| 128 backend_->RemoveObserver(this); | 102 backend_->RemoveObserver(this); |
| 129 db_thread_.Stop(); | 103 db_thread_.Stop(); |
| 130 } | 104 } |
| 131 | 105 |
| 132 void ShortcutsBackendTest::OnShortcutsLoaded() { | 106 void ShortcutsBackendTest::OnShortcutsLoaded() { |
| 133 load_notified_ = true; | 107 load_notified_ = true; |
| 134 base::MessageLoop::current()->Quit(); | 108 base::MessageLoop::current()->Quit(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // types. | 149 // types. |
| 176 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) { | 150 TEST_F(ShortcutsBackendTest, SanitizeMatchCore) { |
| 177 struct { | 151 struct { |
| 178 std::string input_contents_class; | 152 std::string input_contents_class; |
| 179 std::string input_description_class; | 153 std::string input_description_class; |
| 180 AutocompleteMatch::Type input_type; | 154 AutocompleteMatch::Type input_type; |
| 181 std::string output_contents_class; | 155 std::string output_contents_class; |
| 182 std::string output_description_class; | 156 std::string output_description_class; |
| 183 AutocompleteMatch::Type output_type; | 157 AutocompleteMatch::Type output_type; |
| 184 } cases[] = { | 158 } cases[] = { |
| 185 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, | 159 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, |
| 186 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, | 160 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, |
| 187 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, | 161 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, |
| 188 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, | 162 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, |
| 189 { "0,1", "0,0,11,2,15,0", | 163 { "0,1", "0,0,11,2,15,0", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 190 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 164 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
| 191 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, | 165 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, |
| 192 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, | 166 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
| 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 }, | |
| 202 }; | 167 }; |
| 203 | 168 |
| 204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 205 history::ShortcutsDatabase::Shortcut::MatchCore match_core( | 170 history::ShortcutsDatabase::Shortcut::MatchCore match_core( |
| 206 MatchCoreForTesting(std::string(), cases[i].input_contents_class, | 171 MatchCoreForTesting(std::string(), cases[i].input_contents_class, |
| 207 cases[i].input_description_class, | 172 cases[i].input_description_class, |
| 208 cases[i].input_type)); | 173 cases[i].input_type)); |
| 209 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class) | 174 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class); |
| 210 << ":i:" << i << ":type:" << cases[i].input_type; | 175 EXPECT_EQ(cases[i].output_description_class, match_core.description_class); |
| 211 EXPECT_EQ(cases[i].output_description_class, match_core.description_class) | 176 EXPECT_EQ(cases[i].output_type, match_core.type); |
| 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; | |
| 215 } | 177 } |
| 216 } | 178 } |
| 217 | 179 |
| 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 | |
| 245 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { | 180 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { |
| 246 InitBackend(); | 181 InitBackend(); |
| 247 EXPECT_FALSE(changed_notified()); | 182 EXPECT_FALSE(changed_notified()); |
| 248 | 183 |
| 249 history::ShortcutsDatabase::Shortcut shortcut( | 184 history::ShortcutsDatabase::Shortcut shortcut( |
| 250 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), | 185 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), |
| 251 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | 186 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); |
| 252 EXPECT_TRUE(AddShortcut(shortcut)); | 187 EXPECT_TRUE(AddShortcut(shortcut)); |
| 253 EXPECT_TRUE(changed_notified()); | 188 EXPECT_TRUE(changed_notified()); |
| 254 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter( | 189 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); | 246 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); |
| 312 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); | 247 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); |
| 313 | 248 |
| 314 history::ShortcutsDatabase::ShortcutIDs deleted_ids; | 249 history::ShortcutsDatabase::ShortcutIDs deleted_ids; |
| 315 deleted_ids.push_back(shortcut3.id); | 250 deleted_ids.push_back(shortcut3.id); |
| 316 deleted_ids.push_back(shortcut4.id); | 251 deleted_ids.push_back(shortcut4.id); |
| 317 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); | 252 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); |
| 318 | 253 |
| 319 ASSERT_EQ(0U, shortcuts_map().size()); | 254 ASSERT_EQ(0U, shortcuts_map().size()); |
| 320 } | 255 } |
| OLD | NEW |