| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "components/metrics/proto/omnibox_event.pb.h" | 8 #include "components/metrics/proto/omnibox_event.pb.h" |
| 9 #include "components/omnibox/autocomplete_match.h" | 9 #include "components/omnibox/autocomplete_match.h" |
| 10 #include "components/omnibox/autocomplete_scheme_classifier.h" | 10 #include "components/omnibox/autocomplete_scheme_classifier.h" |
| 11 #include "components/omnibox/keyword_provider.h" | 11 #include "components/omnibox/keyword_provider.h" |
| 12 #include "components/omnibox/mock_autocomplete_provider_client.h" |
| 12 #include "components/search_engines/search_engines_switches.h" | 13 #include "components/search_engines/search_engines_switches.h" |
| 13 #include "components/search_engines/template_url.h" | 14 #include "components/search_engines/template_url.h" |
| 14 #include "components/search_engines/template_url_service.h" | 15 #include "components/search_engines/template_url_service.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 using base::ASCIIToUTF16; | 20 using base::ASCIIToUTF16; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 class TestingSchemeClassifier : public AutocompleteSchemeClassifier { | 24 class TestingSchemeClassifier : public AutocompleteSchemeClassifier { |
| 23 public: | 25 public: |
| 24 metrics::OmniboxInputType::Type GetInputTypeForScheme( | 26 metrics::OmniboxInputType::Type GetInputTypeForScheme( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 54 | 56 |
| 55 template<class ResultType> | 57 template<class ResultType> |
| 56 void RunTest(TestData<ResultType>* keyword_cases, | 58 void RunTest(TestData<ResultType>* keyword_cases, |
| 57 int num_cases, | 59 int num_cases, |
| 58 ResultType AutocompleteMatch::* member); | 60 ResultType AutocompleteMatch::* member); |
| 59 | 61 |
| 60 protected: | 62 protected: |
| 61 static const TemplateURLService::Initializer kTestData[]; | 63 static const TemplateURLService::Initializer kTestData[]; |
| 62 | 64 |
| 63 scoped_refptr<KeywordProvider> kw_provider_; | 65 scoped_refptr<KeywordProvider> kw_provider_; |
| 64 scoped_ptr<TemplateURLService> model_; | 66 scoped_ptr<MockAutocompleteProviderClient> client_; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 // static | 69 // static |
| 68 const TemplateURLService::Initializer KeywordProviderTest::kTestData[] = { | 70 const TemplateURLService::Initializer KeywordProviderTest::kTestData[] = { |
| 69 { "aa", "aa.com?foo={searchTerms}", "aa" }, | 71 { "aa", "aa.com?foo={searchTerms}", "aa" }, |
| 70 { "aaaa", "http://aaaa/?aaaa=1&b={searchTerms}&c", "aaaa" }, | 72 { "aaaa", "http://aaaa/?aaaa=1&b={searchTerms}&c", "aaaa" }, |
| 71 { "aaaaa", "{searchTerms}", "aaaaa" }, | 73 { "aaaaa", "{searchTerms}", "aaaaa" }, |
| 72 { "ab", "bogus URL {searchTerms}", "ab" }, | 74 { "ab", "bogus URL {searchTerms}", "ab" }, |
| 73 { "weasel", "weasel{searchTerms}weasel", "weasel" }, | 75 { "weasel", "weasel{searchTerms}weasel", "weasel" }, |
| 74 { "www", " +%2B?={searchTerms}foo ", "www" }, | 76 { "www", " +%2B?={searchTerms}foo ", "www" }, |
| 75 { "nonsub", "http://nonsubstituting-keyword.com/", "nonsub" }, | 77 { "nonsub", "http://nonsubstituting-keyword.com/", "nonsub" }, |
| 76 { "z", "{searchTerms}=z", "z" }, | 78 { "z", "{searchTerms}=z", "z" }, |
| 77 }; | 79 }; |
| 78 | 80 |
| 79 void KeywordProviderTest::SetUp() { | 81 void KeywordProviderTest::SetUp() { |
| 80 model_.reset(new TemplateURLService(kTestData, arraysize(kTestData))); | 82 scoped_ptr<TemplateURLService> template_url_service( |
| 81 kw_provider_ = new KeywordProvider(NULL, model_.get()); | 83 new TemplateURLService(kTestData, arraysize(kTestData))); |
| 84 client_.reset(new MockAutocompleteProviderClient()); |
| 85 client_->set_template_url_service(template_url_service.Pass()); |
| 86 kw_provider_ = new KeywordProvider(client_.get(), nullptr); |
| 82 } | 87 } |
| 83 | 88 |
| 84 void KeywordProviderTest::TearDown() { | 89 void KeywordProviderTest::TearDown() { |
| 85 model_.reset(); | 90 client_.reset(); |
| 86 kw_provider_ = NULL; | 91 kw_provider_ = NULL; |
| 87 } | 92 } |
| 88 | 93 |
| 89 template<class ResultType> | 94 template<class ResultType> |
| 90 void KeywordProviderTest::RunTest(TestData<ResultType>* keyword_cases, | 95 void KeywordProviderTest::RunTest(TestData<ResultType>* keyword_cases, |
| 91 int num_cases, | 96 int num_cases, |
| 92 ResultType AutocompleteMatch::* member) { | 97 ResultType AutocompleteMatch::* member) { |
| 93 ACMatches matches; | 98 ACMatches matches; |
| 94 for (int i = 0; i < num_cases; ++i) { | 99 for (int i = 0; i < num_cases; ++i) { |
| 95 SCOPED_TRACE(keyword_cases[i].input); | 100 SCOPED_TRACE(keyword_cases[i].input); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 &AutocompleteMatch::contents); | 272 &AutocompleteMatch::contents); |
| 268 } | 273 } |
| 269 | 274 |
| 270 TEST_F(KeywordProviderTest, AddKeyword) { | 275 TEST_F(KeywordProviderTest, AddKeyword) { |
| 271 TemplateURLData data; | 276 TemplateURLData data; |
| 272 data.SetShortName(ASCIIToUTF16("Test")); | 277 data.SetShortName(ASCIIToUTF16("Test")); |
| 273 base::string16 keyword(ASCIIToUTF16("foo")); | 278 base::string16 keyword(ASCIIToUTF16("foo")); |
| 274 data.SetKeyword(keyword); | 279 data.SetKeyword(keyword); |
| 275 data.SetURL("http://www.google.com/foo?q={searchTerms}"); | 280 data.SetURL("http://www.google.com/foo?q={searchTerms}"); |
| 276 TemplateURL* template_url = new TemplateURL(data); | 281 TemplateURL* template_url = new TemplateURL(data); |
| 277 model_->Add(template_url); | 282 client_->GetTemplateURLService()->Add(template_url); |
| 278 ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword)); | 283 ASSERT_TRUE( |
| 284 template_url == |
| 285 client_->GetTemplateURLService()->GetTemplateURLForKeyword(keyword)); |
| 279 } | 286 } |
| 280 | 287 |
| 281 TEST_F(KeywordProviderTest, RemoveKeyword) { | 288 TEST_F(KeywordProviderTest, RemoveKeyword) { |
| 289 TemplateURLService* template_url_service = client_->GetTemplateURLService(); |
| 282 base::string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); | 290 base::string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); |
| 283 model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); | 291 template_url_service->Remove( |
| 284 ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL); | 292 template_url_service->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); |
| 293 ASSERT_TRUE(template_url_service->GetTemplateURLForKeyword( |
| 294 ASCIIToUTF16("aaaa")) == NULL); |
| 285 } | 295 } |
| 286 | 296 |
| 287 TEST_F(KeywordProviderTest, GetKeywordForInput) { | 297 TEST_F(KeywordProviderTest, GetKeywordForInput) { |
| 288 EXPECT_EQ(ASCIIToUTF16("aa"), | 298 EXPECT_EQ(ASCIIToUTF16("aa"), |
| 289 kw_provider_->GetKeywordForText(ASCIIToUTF16("aa"))); | 299 kw_provider_->GetKeywordForText(ASCIIToUTF16("aa"))); |
| 290 EXPECT_EQ(base::string16(), | 300 EXPECT_EQ(base::string16(), |
| 291 kw_provider_->GetKeywordForText(ASCIIToUTF16("aafoo"))); | 301 kw_provider_->GetKeywordForText(ASCIIToUTF16("aafoo"))); |
| 292 EXPECT_EQ(base::string16(), | 302 EXPECT_EQ(base::string16(), |
| 293 kw_provider_->GetKeywordForText(ASCIIToUTF16("aa foo"))); | 303 kw_provider_->GetKeywordForText(ASCIIToUTF16("aa foo"))); |
| 294 } | 304 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 { "aa foo", base::string16::npos, false, "", "aa foo", | 344 { "aa foo", base::string16::npos, false, "", "aa foo", |
| 335 base::string16::npos }, | 345 base::string16::npos }, |
| 336 }; | 346 }; |
| 337 for (size_t i = 0; i < arraysize(cases); i++) { | 347 for (size_t i = 0; i < arraysize(cases); i++) { |
| 338 AutocompleteInput input(ASCIIToUTF16(cases[i].text), | 348 AutocompleteInput input(ASCIIToUTF16(cases[i].text), |
| 339 cases[i].cursor_position, std::string(), GURL(), | 349 cases[i].cursor_position, std::string(), GURL(), |
| 340 metrics::OmniboxEventProto::INVALID_SPEC, false, | 350 metrics::OmniboxEventProto::INVALID_SPEC, false, |
| 341 false, cases[i].allow_exact_keyword_match, true, | 351 false, cases[i].allow_exact_keyword_match, true, |
| 342 false, TestingSchemeClassifier()); | 352 false, TestingSchemeClassifier()); |
| 343 const TemplateURL* url = | 353 const TemplateURL* url = |
| 344 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), | 354 KeywordProvider::GetSubstitutingTemplateURLForInput( |
| 345 &input); | 355 client_->GetTemplateURLService(), &input); |
| 346 if (cases[i].expected_url.empty()) | 356 if (cases[i].expected_url.empty()) |
| 347 EXPECT_FALSE(url); | 357 EXPECT_FALSE(url); |
| 348 else | 358 else |
| 349 EXPECT_EQ(cases[i].expected_url, url->url()); | 359 EXPECT_EQ(cases[i].expected_url, url->url()); |
| 350 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); | 360 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); |
| 351 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); | 361 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); |
| 352 } | 362 } |
| 353 } | 363 } |
| 354 | 364 |
| 355 // If extra query params are specified on the command line, they should be | 365 // If extra query params are specified on the command line, they should be |
| (...skipping 14 matching lines...) Expand all Loading... |
| 370 } | 380 } |
| 371 | 381 |
| 372 TEST_F(KeywordProviderTest, DoesNotProvideMatchesOnFocus) { | 382 TEST_F(KeywordProviderTest, DoesNotProvideMatchesOnFocus) { |
| 373 AutocompleteInput input(ASCIIToUTF16("aaa"), base::string16::npos, | 383 AutocompleteInput input(ASCIIToUTF16("aaa"), base::string16::npos, |
| 374 std::string(), GURL(), | 384 std::string(), GURL(), |
| 375 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 385 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
| 376 true, true, true, TestingSchemeClassifier()); | 386 true, true, true, TestingSchemeClassifier()); |
| 377 kw_provider_->Start(input, false); | 387 kw_provider_->Start(input, false); |
| 378 ASSERT_TRUE(kw_provider_->matches().empty()); | 388 ASSERT_TRUE(kw_provider_->matches().empty()); |
| 379 } | 389 } |
| OLD | NEW |