Chromium Code Reviews| 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 return ShortcutsBackend::MatchToMatchCore(match, &profile_); |
| 95 } | |
| 96 | |
| 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")); | |
| 103 | |
| 104 TemplateURL* template_url = new TemplateURL(&profile_, data); | |
| 105 // Takes ownership of |template_url|. | |
| 106 template_url_service->Add(template_url); | |
| 107 template_url_service->SetDefaultSearchProvider(template_url); | |
| 90 } | 108 } |
| 91 | 109 |
| 92 void ShortcutsBackendTest::SetUp() { | 110 void ShortcutsBackendTest::SetUp() { |
| 93 db_thread_.Start(); | 111 db_thread_.Start(); |
| 94 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 112 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( |
| 95 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); | 113 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); |
| 96 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 114 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); |
| 97 ASSERT_TRUE(backend_.get()); | 115 ASSERT_TRUE(backend_.get()); |
| 98 backend_->AddObserver(this); | 116 backend_->AddObserver(this); |
| 117 | |
| 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); | |
| 99 } | 123 } |
| 100 | 124 |
| 101 void ShortcutsBackendTest::TearDown() { | 125 void ShortcutsBackendTest::TearDown() { |
| 102 backend_->RemoveObserver(this); | 126 backend_->RemoveObserver(this); |
| 103 db_thread_.Stop(); | 127 db_thread_.Stop(); |
| 104 } | 128 } |
| 105 | 129 |
| 106 void ShortcutsBackendTest::OnShortcutsLoaded() { | 130 void ShortcutsBackendTest::OnShortcutsLoaded() { |
| 107 load_notified_ = true; | 131 load_notified_ = true; |
| 108 base::MessageLoop::current()->Quit(); | 132 base::MessageLoop::current()->Quit(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 AutocompleteMatch::Type output_type; | 181 AutocompleteMatch::Type output_type; |
| 158 } cases[] = { | 182 } cases[] = { |
| 159 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, | 183 { "0,1,4,0", "0,3,4,1", AutocompleteMatchType::URL_WHAT_YOU_TYPED, |
| 160 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, | 184 "0,1,4,0", "0,1", AutocompleteMatchType::HISTORY_URL }, |
| 161 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, | 185 { "0,3,5,1", "0,2,5,0", AutocompleteMatchType::NAVSUGGEST, |
| 162 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, | 186 "0,1", "0,0", AutocompleteMatchType::HISTORY_URL }, |
| 163 { "0,1", "0,0,11,2,15,0", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 187 { "0,1", "0,0,11,2,15,0", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 164 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, | 188 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
| 165 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, | 189 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST, |
| 166 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, | 190 "0,1", "0,0", AutocompleteMatchType::SEARCH_HISTORY }, |
| 191 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_ENTITY, | |
|
Peter Kasting
2014/03/21 18:36:26
Nit: If you're going to indent these less, then al
Anuj
2014/03/24 07:16:16
Done.
| |
| 192 "", "", AutocompleteMatchType::SEARCH_HISTORY }, | |
| 193 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_INFINITE, | |
| 194 "", "", AutocompleteMatchType::SEARCH_HISTORY }, | |
| 195 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED, | |
| 196 "", "", AutocompleteMatchType::SEARCH_HISTORY }, | |
| 197 { "0,1", "0,0", AutocompleteMatchType::SEARCH_SUGGEST_PROFILE, | |
| 198 "", "", AutocompleteMatchType::SEARCH_HISTORY }, | |
| 167 }; | 199 }; |
| 168 | 200 |
| 169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 201 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 170 history::ShortcutsDatabase::Shortcut::MatchCore match_core( | 202 history::ShortcutsDatabase::Shortcut::MatchCore match_core( |
| 171 MatchCoreForTesting(std::string(), cases[i].input_contents_class, | 203 MatchCoreForTesting(std::string(), cases[i].input_contents_class, |
| 172 cases[i].input_description_class, | 204 cases[i].input_description_class, |
| 173 cases[i].input_type)); | 205 cases[i].input_type)); |
| 174 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class); | 206 EXPECT_EQ(cases[i].output_contents_class, match_core.contents_class); |
| 175 EXPECT_EQ(cases[i].output_description_class, match_core.description_class); | 207 EXPECT_EQ(cases[i].output_description_class, match_core.description_class); |
| 176 EXPECT_EQ(cases[i].output_type, match_core.type); | 208 EXPECT_EQ(cases[i].output_type, match_core.type); |
| 177 } | 209 } |
| 178 } | 210 } |
| 179 | 211 |
| 212 TEST_F(ShortcutsBackendTest, EntitySuggestionTest) { | |
| 213 SetSearchProvider(); | |
| 214 AutocompleteMatch match; | |
| 215 match.fill_into_edit = base::UTF8ToUTF16("franklin d roosevelt"); | |
| 216 match.type = AutocompleteMatchType::SEARCH_SUGGEST_ENTITY; | |
| 217 match.contents = base::UTF8ToUTF16("roosevelt"); | |
| 218 match.contents_class = | |
| 219 AutocompleteMatch::ClassificationsFromString("0,0,5,2"); | |
| 220 match.description = base::UTF8ToUTF16("Franklin D. Roosevelt"); | |
| 221 match.description_class = AutocompleteMatch::ClassificationsFromString("0,4"); | |
| 222 match.destination_url = GURL( | |
| 223 "http://www.foo.com/search?bar=franklin+d+roosevelt&gs_ssp=1234"); | |
| 224 match.keyword = base::UTF8ToUTF16("foo"); | |
| 225 | |
| 226 history::ShortcutsDatabase::Shortcut::MatchCore match_core = | |
| 227 ShortcutsBackend::MatchToMatchCore(match, &profile_); | |
| 228 | |
| 229 EXPECT_EQ("http://foo.com/search?bar=franklin+d+roosevelt", | |
| 230 match_core.destination_url.spec()); | |
| 231 EXPECT_EQ(match.fill_into_edit, match_core.contents); | |
| 232 EXPECT_TRUE(match_core.contents_class.empty()); | |
| 233 EXPECT_EQ(base::string16(), match_core.description); | |
| 234 EXPECT_TRUE(match_core.description_class.empty()); | |
| 235 } | |
| 236 | |
| 180 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { | 237 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { |
| 181 InitBackend(); | 238 InitBackend(); |
| 182 EXPECT_FALSE(changed_notified()); | 239 EXPECT_FALSE(changed_notified()); |
| 183 | 240 |
| 184 history::ShortcutsDatabase::Shortcut shortcut( | 241 history::ShortcutsDatabase::Shortcut shortcut( |
| 185 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), | 242 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", base::ASCIIToUTF16("goog"), |
| 186 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); | 243 MatchCoreForTesting("http://www.google.com"), base::Time::Now(), 100); |
| 187 EXPECT_TRUE(AddShortcut(shortcut)); | 244 EXPECT_TRUE(AddShortcut(shortcut)); |
| 188 EXPECT_TRUE(changed_notified()); | 245 EXPECT_TRUE(changed_notified()); |
| 189 ShortcutsBackend::ShortcutMap::const_iterator shortcut_iter( | 246 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()); | 303 ASSERT_TRUE(shortcut4_iter != shortcuts_map().end()); |
| 247 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); | 304 EXPECT_EQ(shortcut4.id, shortcut4_iter->second.id); |
| 248 | 305 |
| 249 history::ShortcutsDatabase::ShortcutIDs deleted_ids; | 306 history::ShortcutsDatabase::ShortcutIDs deleted_ids; |
| 250 deleted_ids.push_back(shortcut3.id); | 307 deleted_ids.push_back(shortcut3.id); |
| 251 deleted_ids.push_back(shortcut4.id); | 308 deleted_ids.push_back(shortcut4.id); |
| 252 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); | 309 EXPECT_TRUE(DeleteShortcutsWithIDs(deleted_ids)); |
| 253 | 310 |
| 254 ASSERT_EQ(0U, shortcuts_map().size()); | 311 ASSERT_EQ(0U, shortcuts_map().size()); |
| 255 } | 312 } |
| OLD | NEW |