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 "components/omnibox/browser/shortcuts_provider.h" | 5 #include "components/omnibox/browser/shortcuts_provider.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <functional> | 11 #include <functional> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | 22 #include "base/test/sequenced_worker_pool_owner.h" |
| 22 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 23 #include "base/threading/thread.h" |
| 23 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" | |
| 24 #include "chrome/test/base/testing_profile.h" | |
| 25 #include "components/history/core/browser/history_service.h" | 24 #include "components/history/core/browser/history_service.h" |
| 26 #include "components/history/core/browser/url_database.h" | 25 #include "components/history/core/browser/url_database.h" |
| 26 #include "components/history/core/test/history_service_test_util.h" | |
| 27 #include "components/metrics/proto/omnibox_event.pb.h" | 27 #include "components/metrics/proto/omnibox_event.pb.h" |
| 28 #include "components/omnibox/browser/autocomplete_input.h" | 28 #include "components/omnibox/browser/autocomplete_input.h" |
| 29 #include "components/omnibox/browser/autocomplete_match.h" | 29 #include "components/omnibox/browser/autocomplete_match.h" |
| 30 #include "components/omnibox/browser/autocomplete_provider.h" | 30 #include "components/omnibox/browser/autocomplete_provider.h" |
| 31 #include "components/omnibox/browser/autocomplete_result.h" | 31 #include "components/omnibox/browser/autocomplete_result.h" |
| 32 #include "components/omnibox/browser/in_memory_url_index.h" | 32 #include "components/omnibox/browser/in_memory_url_index.h" |
| 33 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" | |
| 33 #include "components/omnibox/browser/shortcuts_backend.h" | 34 #include "components/omnibox/browser/shortcuts_backend.h" |
| 34 #include "components/prefs/pref_service.h" | 35 #include "components/omnibox/browser/test_scheme_classifier.h" |
| 35 #include "content/public/browser/notification_service.h" | |
| 36 #include "content/public/test/test_browser_thread.h" | |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 37 |
| 39 #if defined(ENABLE_EXTENSIONS) | |
| 40 #include "extensions/browser/notification_types.h" | |
| 41 #include "extensions/common/extension.h" | |
| 42 #include "extensions/common/extension_builder.h" | |
| 43 #include "extensions/common/value_builder.h" | |
| 44 #endif | |
| 45 | |
| 46 using base::ASCIIToUTF16; | 38 using base::ASCIIToUTF16; |
| 47 | 39 |
| 48 // TestShortcutInfo ----------------------------------------------------------- | 40 // TestShortcutInfo ----------------------------------------------------------- |
| 49 | 41 |
| 50 namespace { | 42 namespace { |
| 51 | 43 |
| 52 struct TestShortcutInfo { | 44 struct TestShortcutInfo { |
| 53 std::string guid; | 45 std::string guid; |
| 54 std::string text; | 46 std::string text; |
| 55 std::string fill_into_edit; | 47 std::string fill_into_edit; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 131 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
| 140 AutocompleteMatchType::HISTORY_URL, "", 2, 1 }, | 132 AutocompleteMatchType::HISTORY_URL, "", 2, 1 }, |
| 141 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html", | 133 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html", |
| 142 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html", | 134 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html", |
| 143 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 135 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
| 144 AutocompleteMatchType::HISTORY_URL, "", 3, 1 }, | 136 AutocompleteMatchType::HISTORY_URL, "", 3, 1 }, |
| 145 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html", | 137 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html", |
| 146 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html", | 138 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html", |
| 147 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, | 139 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED, |
| 148 AutocompleteMatchType::HISTORY_URL, "", 4, 1 }, | 140 AutocompleteMatchType::HISTORY_URL, "", 4, 1 }, |
| 149 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo", | |
| 150 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo", | |
| 151 "Run Echo command: echo", "0,0", "Echo", "0,4", | |
| 152 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::EXTENSION_APP, | |
| 153 "echo", 1, 1 }, | |
| 154 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com", | 141 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com", |
| 155 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1", | 142 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1", |
| 156 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, | 143 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 1, |
| 157 100 }, | 144 100 }, |
| 158 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query", | 145 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query", |
| 159 "https://www.google.com/search?q=query", "query", "0,0", | 146 "https://www.google.com/search?q=query", "query", "0,0", |
| 160 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED, | 147 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED, |
| 161 AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 }, | 148 AutocompleteMatchType::SEARCH_HISTORY, "", 1, 100 }, |
| 162 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word", | 149 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word", |
| 163 "https://www.google.com/search?q=www.word", "www.word", "0,0", | 150 "https://www.google.com/search?q=www.word", "www.word", "0,0", |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 "Trailing1 - Space in Shortcut", "0,1", | 184 "Trailing1 - Space in Shortcut", "0,1", |
| 198 "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, | 185 "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, |
| 199 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, | 186 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, |
| 200 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ", | 187 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ", |
| 201 "chrome://trailing2blah", "chrome://trailing2blah/", | 188 "chrome://trailing2blah", "chrome://trailing2blah/", |
| 202 "Trailing2 - Space in Shortcut", "0,1", | 189 "Trailing2 - Space in Shortcut", "0,1", |
| 203 "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, | 190 "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED, |
| 204 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, | 191 AutocompleteMatchType::HISTORY_URL, "", 1, 100 }, |
| 205 }; | 192 }; |
| 206 | 193 |
| 194 class FakeAutocompleteProviderClient | |
| 195 : public testing::NiceMock<MockAutocompleteProviderClient> { | |
| 196 public: | |
| 197 FakeAutocompleteProviderClient() | |
| 198 : db_thread_("Test DB thread"), | |
| 199 pool_owner_(3, "Background Pool") { | |
| 200 set_template_url_service( | |
| 201 make_scoped_ptr(new TemplateURLService(nullptr, 0))); | |
| 202 if (history_dir_.CreateUniqueTempDir()) { | |
| 203 history_service_ = history::CreateHistoryService( | |
| 204 history_dir_.path(), std::string(), true); | |
| 205 } | |
| 206 | |
| 207 db_thread_.Start(); | |
| 208 shortcuts_backend_ = new ShortcutsBackend( | |
| 209 GetTemplateURLService(), make_scoped_ptr(new SearchTermsData()), | |
| 210 history_service_.get(), db_thread_.task_runner(), | |
| 211 base::FilePath(), true); | |
| 212 shortcuts_backend_->Init(); | |
| 213 } | |
| 214 | |
| 215 ~FakeAutocompleteProviderClient() override { | |
| 216 db_thread_.Stop(); | |
| 217 } | |
| 218 | |
| 219 history::HistoryService* GetHistoryService() override { | |
| 220 return history_service_.get(); | |
| 221 } | |
| 222 | |
| 223 scoped_refptr<ShortcutsBackend> GetShortcutsBackend() override { | |
| 224 return shortcuts_backend_; | |
| 225 } | |
| 226 | |
| 227 scoped_refptr<ShortcutsBackend> GetShortcutsBackendIfExists() override { | |
| 228 return shortcuts_backend_; | |
| 229 } | |
| 230 | |
| 231 private: | |
| 232 base::Thread db_thread_; | |
| 233 base::SequencedWorkerPoolOwner pool_owner_; | |
| 234 base::ScopedTempDir history_dir_; | |
| 235 scoped_ptr<history::HistoryService> history_service_; | |
| 236 scoped_refptr<ShortcutsBackend> shortcuts_backend_; | |
| 237 | |
| 238 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); | |
| 239 }; | |
| 240 | |
| 207 } // namespace | 241 } // namespace |
| 208 | 242 |
| 209 | |
|
blundell
2016/02/02 15:37:34
I believe these blank lines are intended, although
rohitrao (ping after 24h)
2016/02/02 15:42:26
Yeah, I decided that "---------------------" was a
| |
| 210 // ClassifyTest --------------------------------------------------------------- | 243 // ClassifyTest --------------------------------------------------------------- |
| 211 | 244 |
| 212 // Helper class to make running tests of ClassifyAllMatchesInString() more | 245 // Helper class to make running tests of ClassifyAllMatchesInString() more |
| 213 // convenient. | 246 // convenient. |
| 214 class ClassifyTest { | 247 class ClassifyTest { |
| 215 public: | 248 public: |
| 216 ClassifyTest(const base::string16& text, ACMatchClassifications matches); | 249 ClassifyTest(const base::string16& text, ACMatchClassifications matches); |
| 217 ~ClassifyTest(); | 250 ~ClassifyTest(); |
| 218 | 251 |
| 219 ACMatchClassifications RunTest(const base::string16& find_text); | 252 ACMatchClassifications RunTest(const base::string16& find_text); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 230 } | 263 } |
| 231 | 264 |
| 232 ClassifyTest::~ClassifyTest() { | 265 ClassifyTest::~ClassifyTest() { |
| 233 } | 266 } |
| 234 | 267 |
| 235 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { | 268 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { |
| 236 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, | 269 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, |
| 237 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); | 270 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); |
| 238 } | 271 } |
| 239 | 272 |
| 240 | |
| 241 // ShortcutsProviderTest ------------------------------------------------------ | 273 // ShortcutsProviderTest ------------------------------------------------------ |
| 242 | 274 |
| 243 class ShortcutsProviderTest : public testing::Test { | 275 class ShortcutsProviderTest : public testing::Test { |
| 244 public: | 276 public: |
| 245 ShortcutsProviderTest(); | 277 ShortcutsProviderTest(); |
| 246 | 278 |
| 247 protected: | 279 protected: |
| 248 typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault; | 280 typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault; |
| 249 typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs; | 281 typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs; |
| 250 | 282 |
| 251 class SetShouldContain | 283 class SetShouldContain |
| 252 : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&, | 284 : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&, |
| 253 std::set<std::string> > { | 285 std::set<std::string> > { |
| 254 public: | 286 public: |
| 255 explicit SetShouldContain(const ACMatches& matched_urls); | 287 explicit SetShouldContain(const ACMatches& matched_urls); |
| 256 | 288 |
| 257 void operator()(const ExpectedURLAndAllowedToBeDefault& expected); | 289 void operator()(const ExpectedURLAndAllowedToBeDefault& expected); |
| 258 std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const { | 290 std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const { |
| 259 return matches_; | 291 return matches_; |
| 260 } | 292 } |
| 261 | 293 |
| 262 private: | 294 private: |
| 263 std::set<ExpectedURLAndAllowedToBeDefault> matches_; | 295 std::set<ExpectedURLAndAllowedToBeDefault> matches_; |
| 264 }; | 296 }; |
| 265 | 297 |
| 266 void SetUp() override; | 298 void SetUp() override; |
| 267 void TearDown() override; | 299 void TearDown() override; |
| 268 | 300 |
| 269 // Fills test data into the provider. | 301 // Fills test data into the provider. |
| 270 void FillData(TestShortcutInfo* db, size_t db_size); | 302 void FillData(TestShortcutInfo* db, size_t db_size); |
| 271 | 303 |
| 272 // Runs an autocomplete query on |text| with the provided | 304 // Runs an autocomplete query on |text| with the provided |
| 273 // |prevent_inline_autocomplete| setting and checks to see that the returned | 305 // |prevent_inline_autocomplete| setting and checks to see that the returned |
| 274 // results' destination URLs match those provided. |expected_urls| does not | 306 // results' destination URLs match those provided. |expected_urls| does not |
| 275 // need to be in sorted order, but |expected_top_result| should be the top | 307 // need to be in sorted order, but |expected_top_result| should be the top |
| 276 // match, and it should have inline autocompletion | 308 // match, and it should have inline autocompletion |
| 277 // |top_result_inline_autocompletion|. | 309 // |top_result_inline_autocompletion|. |
| 278 void RunTest(const base::string16 text, | 310 void RunTest(const base::string16 text, |
| 279 bool prevent_inline_autocomplete, | 311 bool prevent_inline_autocomplete, |
| 280 const ExpectedURLs& expected_urls, | 312 const ExpectedURLs& expected_urls, |
| 281 std::string expected_top_result, | 313 std::string expected_top_result, |
| 282 base::string16 top_result_inline_autocompletion); | 314 base::string16 top_result_inline_autocompletion); |
| 283 | 315 |
| 284 // Passthrough to the private function in provider_. | 316 // Passthrough to the private function in provider_. |
| 285 int CalculateScore(const std::string& terms, | 317 int CalculateScore(const std::string& terms, |
| 286 const ShortcutsDatabase::Shortcut& shortcut, | 318 const ShortcutsDatabase::Shortcut& shortcut, |
| 287 int max_relevance); | 319 int max_relevance); |
| 288 | 320 |
| 289 base::MessageLoopForUI message_loop_; | 321 base::MessageLoop message_loop_; |
| 290 content::TestBrowserThread ui_thread_; | 322 scoped_ptr<FakeAutocompleteProviderClient> client_; |
| 291 content::TestBrowserThread file_thread_; | 323 scoped_refptr<ShortcutsProvider> provider_; |
| 292 | |
| 293 TestingProfile profile_; | |
| 294 ChromeAutocompleteProviderClient client_; | |
| 295 | 324 |
| 296 ACMatches ac_matches_; // The resulting matches after running RunTest. | 325 ACMatches ac_matches_; // The resulting matches after running RunTest. |
| 297 | |
| 298 scoped_refptr<ShortcutsBackend> backend_; | |
| 299 scoped_refptr<ShortcutsProvider> provider_; | |
| 300 }; | 326 }; |
| 301 | 327 |
| 302 ShortcutsProviderTest::ShortcutsProviderTest() | 328 ShortcutsProviderTest::ShortcutsProviderTest() { |
| 303 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 304 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
| 305 client_(&profile_) { | |
| 306 } | 329 } |
| 307 | 330 |
| 308 void ShortcutsProviderTest::SetUp() { | 331 void ShortcutsProviderTest::SetUp() { |
| 309 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 332 client_.reset(new FakeAutocompleteProviderClient()); |
| 310 &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting); | 333 |
| 311 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 334 ASSERT_TRUE(client_->GetShortcutsBackend()); |
| 312 ASSERT_TRUE(backend_.get()); | 335 provider_ = new ShortcutsProvider(client_.get()); |
| 313 ASSERT_TRUE(profile_.CreateHistoryService(true, false)); | |
| 314 provider_ = new ShortcutsProvider(&client_); | |
| 315 FillData(shortcut_test_db, arraysize(shortcut_test_db)); | 336 FillData(shortcut_test_db, arraysize(shortcut_test_db)); |
| 316 } | 337 } |
| 317 | 338 |
| 318 void ShortcutsProviderTest::TearDown() { | 339 void ShortcutsProviderTest::TearDown() { |
| 319 // Run all pending tasks or else some threads hold on to the message loop | 340 // Run all pending tasks or else some threads hold on to the message loop |
| 320 // and prevent it from being deleted. | 341 // and prevent it from being deleted. |
| 321 message_loop_.RunUntilIdle(); | 342 message_loop_.RunUntilIdle(); |
| 322 profile_.DestroyHistoryService(); | |
| 323 provider_ = NULL; | 343 provider_ = NULL; |
| 324 } | 344 } |
| 325 | 345 |
| 326 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { | 346 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { |
| 327 DCHECK(provider_.get()); | 347 DCHECK(provider_.get()); |
| 328 size_t expected_size = backend_->shortcuts_map().size() + db_size; | 348 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); |
| 349 size_t expected_size = backend->shortcuts_map().size() + db_size; | |
| 329 for (size_t i = 0; i < db_size; ++i) { | 350 for (size_t i = 0; i < db_size; ++i) { |
| 330 const TestShortcutInfo& cur = db[i]; | 351 const TestShortcutInfo& cur = db[i]; |
| 331 ShortcutsDatabase::Shortcut shortcut( | 352 ShortcutsDatabase::Shortcut shortcut( |
| 332 cur.guid, ASCIIToUTF16(cur.text), | 353 cur.guid, ASCIIToUTF16(cur.text), |
| 333 ShortcutsDatabase::Shortcut::MatchCore( | 354 ShortcutsDatabase::Shortcut::MatchCore( |
| 334 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), | 355 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), |
| 335 ASCIIToUTF16(cur.contents), cur.contents_class, | 356 ASCIIToUTF16(cur.contents), cur.contents_class, |
| 336 ASCIIToUTF16(cur.description), cur.description_class, | 357 ASCIIToUTF16(cur.description), cur.description_class, |
| 337 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), | 358 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), |
| 338 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), | 359 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), |
| 339 cur.number_of_hits); | 360 cur.number_of_hits); |
| 340 backend_->AddShortcut(shortcut); | 361 backend->AddShortcut(shortcut); |
| 341 } | 362 } |
| 342 EXPECT_EQ(expected_size, backend_->shortcuts_map().size()); | 363 EXPECT_EQ(expected_size, backend->shortcuts_map().size()); |
| 343 } | 364 } |
| 344 | 365 |
| 345 ShortcutsProviderTest::SetShouldContain::SetShouldContain( | 366 ShortcutsProviderTest::SetShouldContain::SetShouldContain( |
| 346 const ACMatches& matched_urls) { | 367 const ACMatches& matched_urls) { |
| 347 for (ACMatches::const_iterator iter = matched_urls.begin(); | 368 for (ACMatches::const_iterator iter = matched_urls.begin(); |
| 348 iter != matched_urls.end(); ++iter) | 369 iter != matched_urls.end(); ++iter) |
| 349 matches_.insert(ExpectedURLAndAllowedToBeDefault( | 370 matches_.insert(ExpectedURLAndAllowedToBeDefault( |
| 350 iter->destination_url.spec(), iter->allowed_to_be_default_match)); | 371 iter->destination_url.spec(), iter->allowed_to_be_default_match)); |
| 351 } | 372 } |
| 352 | 373 |
| 353 void ShortcutsProviderTest::SetShouldContain::operator()( | 374 void ShortcutsProviderTest::SetShouldContain::operator()( |
| 354 const ExpectedURLAndAllowedToBeDefault& expected) { | 375 const ExpectedURLAndAllowedToBeDefault& expected) { |
| 355 EXPECT_EQ(1U, matches_.erase(expected)); | 376 EXPECT_EQ(1U, matches_.erase(expected)); |
| 356 } | 377 } |
| 357 | 378 |
| 358 void ShortcutsProviderTest::RunTest( | 379 void ShortcutsProviderTest::RunTest( |
| 359 const base::string16 text, | 380 const base::string16 text, |
| 360 bool prevent_inline_autocomplete, | 381 bool prevent_inline_autocomplete, |
| 361 const ExpectedURLs& expected_urls, | 382 const ExpectedURLs& expected_urls, |
| 362 std::string expected_top_result, | 383 std::string expected_top_result, |
| 363 base::string16 top_result_inline_autocompletion) { | 384 base::string16 top_result_inline_autocompletion) { |
| 364 base::MessageLoop::current()->RunUntilIdle(); | 385 base::MessageLoop::current()->RunUntilIdle(); |
| 365 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), | 386 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), |
| 366 metrics::OmniboxEventProto::INVALID_SPEC, | 387 metrics::OmniboxEventProto::INVALID_SPEC, |
| 367 prevent_inline_autocomplete, false, true, true, false, | 388 prevent_inline_autocomplete, false, true, true, false, |
| 368 ChromeAutocompleteSchemeClassifier(&profile_)); | 389 TestSchemeClassifier()); |
| 369 provider_->Start(input, false); | 390 provider_->Start(input, false); |
| 370 EXPECT_TRUE(provider_->done()); | 391 EXPECT_TRUE(provider_->done()); |
| 371 | 392 |
| 372 ac_matches_ = provider_->matches(); | 393 ac_matches_ = provider_->matches(); |
| 373 | 394 |
| 374 // We should have gotten back at most AutocompleteProvider::kMaxMatches. | 395 // We should have gotten back at most AutocompleteProvider::kMaxMatches. |
| 375 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); | 396 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); |
| 376 | 397 |
| 377 // If the number of expected and actual matches aren't equal then we need | 398 // If the number of expected and actual matches aren't equal then we need |
| 378 // test no further, but let's do anyway so that we know which URLs failed. | 399 // test no further, but let's do anyway so that we know which URLs failed. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 396 } | 417 } |
| 397 | 418 |
| 398 int ShortcutsProviderTest::CalculateScore( | 419 int ShortcutsProviderTest::CalculateScore( |
| 399 const std::string& terms, | 420 const std::string& terms, |
| 400 const ShortcutsDatabase::Shortcut& shortcut, | 421 const ShortcutsDatabase::Shortcut& shortcut, |
| 401 int max_relevance) { | 422 int max_relevance) { |
| 402 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, | 423 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, |
| 403 max_relevance); | 424 max_relevance); |
| 404 } | 425 } |
| 405 | 426 |
| 406 | |
| 407 // Actual tests --------------------------------------------------------------- | 427 // Actual tests --------------------------------------------------------------- |
| 408 | 428 |
| 409 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { | 429 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { |
| 410 base::string16 text(ASCIIToUTF16("go")); | 430 base::string16 text(ASCIIToUTF16("go")); |
| 411 std::string expected_url("http://www.google.com/"); | 431 std::string expected_url("http://www.google.com/"); |
| 412 ExpectedURLs expected_urls; | 432 ExpectedURLs expected_urls; |
| 413 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); | 433 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault(expected_url, true)); |
| 414 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ogle.com")); | 434 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16("ogle.com")); |
| 415 | 435 |
| 416 // Same test with prevent inline autocomplete. | 436 // Same test with prevent inline autocomplete. |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2", | 802 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2", |
| 783 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2", | 803 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2", |
| 784 "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 804 "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
| 785 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, | 805 AutocompleteMatchType::HISTORY_TITLE, "", 1, 1}, |
| 786 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2", | 806 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2", |
| 787 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2", | 807 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2", |
| 788 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, | 808 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED, |
| 789 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, | 809 AutocompleteMatchType::HISTORY_URL, "", 1, 1}, |
| 790 }; | 810 }; |
| 791 | 811 |
| 792 size_t original_shortcuts_count = backend_->shortcuts_map().size(); | 812 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); |
| 813 size_t original_shortcuts_count = backend->shortcuts_map().size(); | |
| 793 | 814 |
| 794 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); | 815 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); |
| 795 | 816 |
| 796 EXPECT_EQ(original_shortcuts_count + 4, backend_->shortcuts_map().size()); | 817 EXPECT_EQ(original_shortcuts_count + 4, backend->shortcuts_map().size()); |
| 797 EXPECT_FALSE(backend_->shortcuts_map().end() == | 818 EXPECT_FALSE(backend->shortcuts_map().end() == |
| 798 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 819 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
| 799 EXPECT_FALSE(backend_->shortcuts_map().end() == | 820 EXPECT_FALSE(backend->shortcuts_map().end() == |
| 800 backend_->shortcuts_map().find(ASCIIToUTF16("erase"))); | 821 backend->shortcuts_map().find(ASCIIToUTF16("erase"))); |
| 801 | 822 |
| 802 AutocompleteMatch match( | 823 AutocompleteMatch match( |
| 803 provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE); | 824 provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE); |
| 804 | 825 |
| 805 match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url); | 826 match.destination_url = GURL(shortcuts_to_test_delete[0].destination_url); |
| 806 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents); | 827 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[0].contents); |
| 807 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description); | 828 match.description = ASCIIToUTF16(shortcuts_to_test_delete[0].description); |
| 808 | 829 |
| 809 provider_->DeleteMatch(match); | 830 provider_->DeleteMatch(match); |
| 810 | 831 |
| 811 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be | 832 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be |
| 812 // deleted, but not shortcuts_to_test_delete[2] or | 833 // deleted, but not shortcuts_to_test_delete[2] or |
| 813 // shortcuts_to_test_delete[3], which have different URLs. | 834 // shortcuts_to_test_delete[3], which have different URLs. |
| 814 EXPECT_EQ(original_shortcuts_count + 2, backend_->shortcuts_map().size()); | 835 EXPECT_EQ(original_shortcuts_count + 2, backend->shortcuts_map().size()); |
| 815 EXPECT_FALSE(backend_->shortcuts_map().end() == | 836 EXPECT_FALSE(backend->shortcuts_map().end() == |
| 816 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 837 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
| 817 EXPECT_TRUE(backend_->shortcuts_map().end() == | 838 EXPECT_TRUE(backend->shortcuts_map().end() == |
| 818 backend_->shortcuts_map().find(ASCIIToUTF16("erase"))); | 839 backend->shortcuts_map().find(ASCIIToUTF16("erase"))); |
| 819 | 840 |
| 820 match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url); | 841 match.destination_url = GURL(shortcuts_to_test_delete[3].destination_url); |
| 821 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents); | 842 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[3].contents); |
| 822 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); | 843 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); |
| 823 | 844 |
| 824 provider_->DeleteMatch(match); | 845 provider_->DeleteMatch(match); |
| 825 EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size()); | 846 EXPECT_EQ(original_shortcuts_count + 1, backend->shortcuts_map().size()); |
| 826 EXPECT_TRUE(backend_->shortcuts_map().end() == | 847 EXPECT_TRUE(backend->shortcuts_map().end() == |
| 827 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 848 backend->shortcuts_map().find(ASCIIToUTF16("delete"))); |
| 828 } | 849 } |
| 829 | 850 |
| 830 TEST_F(ShortcutsProviderTest, DoesNotProvideOnFocus) { | 851 TEST_F(ShortcutsProviderTest, DoesNotProvideOnFocus) { |
| 831 AutocompleteInput input( | 852 AutocompleteInput input( |
| 832 ASCIIToUTF16("about:o"), base::string16::npos, std::string(), GURL(), | 853 ASCIIToUTF16("about:o"), base::string16::npos, std::string(), GURL(), |
| 833 metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true, | 854 metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true, |
| 834 ChromeAutocompleteSchemeClassifier(&profile_)); | 855 TestSchemeClassifier()); |
| 835 provider_->Start(input, false); | 856 provider_->Start(input, false); |
| 836 EXPECT_TRUE(provider_->matches().empty()); | 857 EXPECT_TRUE(provider_->matches().empty()); |
| 837 } | 858 } |
| 838 | |
| 839 #if defined(ENABLE_EXTENSIONS) | |
| 840 TEST_F(ShortcutsProviderTest, Extension) { | |
| 841 // Try an input string that matches an extension URL. | |
| 842 base::string16 text(ASCIIToUTF16("echo")); | |
| 843 std::string expected_url( | |
| 844 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo"); | |
| 845 ExpectedURLs expected_urls; | |
| 846 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( | |
| 847 expected_url, true)); | |
| 848 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(" echo")); | |
| 849 | |
| 850 // Claim the extension has been unloaded. | |
| 851 scoped_refptr<const extensions::Extension> extension = | |
| 852 extensions::ExtensionBuilder() | |
| 853 .SetManifest(std::move(extensions::DictionaryBuilder() | |
| 854 .Set("name", "Echo") | |
| 855 .Set("version", "1.0"))) | |
| 856 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee") | |
| 857 .Build(); | |
| 858 extensions::UnloadedExtensionInfo details( | |
| 859 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); | |
| 860 content::NotificationService::current()->Notify( | |
| 861 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 862 content::Source<Profile>(&profile_), | |
| 863 content::Details<extensions::UnloadedExtensionInfo>(&details)); | |
| 864 | |
| 865 // Now the URL should have disappeared. | |
| 866 RunTest(text, false, ExpectedURLs(), std::string(), base::string16()); | |
| 867 } | |
| 868 #endif | |
| OLD | NEW |