Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: components/omnibox/keyword_provider_unittest.cc

Issue 469623004: Move KeywordProvider to components/omnibox (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move tests Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/omnibox/keyword_provider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "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 "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
9 #include "chrome/browser/autocomplete/keyword_provider.h"
10 #include "components/metrics/proto/omnibox_event.pb.h" 8 #include "components/metrics/proto/omnibox_event.pb.h"
11 #include "components/omnibox/autocomplete_match.h" 9 #include "components/omnibox/autocomplete_match.h"
10 #include "components/omnibox/autocomplete_scheme_classifier.h"
11 #include "components/omnibox/keyword_provider.h"
12 #include "components/search_engines/search_engines_switches.h" 12 #include "components/search_engines/search_engines_switches.h"
13 #include "components/search_engines/template_url.h" 13 #include "components/search_engines/template_url.h"
14 #include "components/search_engines/template_url_service.h" 14 #include "components/search_engines/template_url_service.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 16 #include "url/gurl.h"
17 17
18 using base::ASCIIToUTF16; 18 using base::ASCIIToUTF16;
19 19
20 namespace {
21
22 class TestingSchemeClassifier : public AutocompleteSchemeClassifier {
23 public:
24 virtual metrics::OmniboxInputType::Type GetInputTypeForScheme(
25 const std::string& scheme) const OVERRIDE {
26 if (net::URLRequest::IsHandledProtocol(scheme))
27 return metrics::OmniboxInputType::URL;
28 return metrics::OmniboxInputType::INVALID;
29 }
30 };
31
32 } // namespace
33
20 class KeywordProviderTest : public testing::Test { 34 class KeywordProviderTest : public testing::Test {
21 protected: 35 protected:
22 template<class ResultType> 36 template<class ResultType>
23 struct MatchType { 37 struct MatchType {
24 const ResultType member; 38 const ResultType member;
25 bool allowed_to_be_default_match; 39 bool allowed_to_be_default_match;
26 }; 40 };
27 41
28 template<class ResultType> 42 template<class ResultType>
29 struct TestData { 43 struct TestData {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 model_.reset(new TemplateURLService(kTestData, arraysize(kTestData))); 80 model_.reset(new TemplateURLService(kTestData, arraysize(kTestData)));
67 kw_provider_ = new KeywordProvider(NULL, model_.get()); 81 kw_provider_ = new KeywordProvider(NULL, model_.get());
68 } 82 }
69 83
70 void KeywordProviderTest::TearDown() { 84 void KeywordProviderTest::TearDown() {
71 model_.reset(); 85 model_.reset();
72 kw_provider_ = NULL; 86 kw_provider_ = NULL;
73 } 87 }
74 88
75 template<class ResultType> 89 template<class ResultType>
76 void KeywordProviderTest::RunTest( 90 void KeywordProviderTest::RunTest(TestData<ResultType>* keyword_cases,
77 TestData<ResultType>* keyword_cases, 91 int num_cases,
78 int num_cases, 92 ResultType AutocompleteMatch::* member) {
79 ResultType AutocompleteMatch::* member) {
80 ACMatches matches; 93 ACMatches matches;
81 for (int i = 0; i < num_cases; ++i) { 94 for (int i = 0; i < num_cases; ++i) {
82 SCOPED_TRACE(keyword_cases[i].input); 95 SCOPED_TRACE(keyword_cases[i].input);
83 AutocompleteInput input(keyword_cases[i].input, base::string16::npos, 96 AutocompleteInput input(keyword_cases[i].input, base::string16::npos,
84 base::string16(), GURL(), 97 base::string16(), GURL(),
85 metrics::OmniboxEventProto::INVALID_SPEC, true, 98 metrics::OmniboxEventProto::INVALID_SPEC, true,
86 false, true, true, 99 false, true, true, TestingSchemeClassifier());
87 ChromeAutocompleteSchemeClassifier(NULL));
88 kw_provider_->Start(input, false); 100 kw_provider_->Start(input, false);
89 EXPECT_TRUE(kw_provider_->done()); 101 EXPECT_TRUE(kw_provider_->done());
90 matches = kw_provider_->matches(); 102 matches = kw_provider_->matches();
91 ASSERT_EQ(keyword_cases[i].num_results, matches.size()); 103 ASSERT_EQ(keyword_cases[i].num_results, matches.size());
92 for (size_t j = 0; j < matches.size(); ++j) { 104 for (size_t j = 0; j < matches.size(); ++j) {
93 EXPECT_EQ(keyword_cases[i].output[j].member, matches[j].*member); 105 EXPECT_EQ(keyword_cases[i].output[j].member, matches[j].*member);
94 EXPECT_EQ(keyword_cases[i].output[j].allowed_to_be_default_match, 106 EXPECT_EQ(keyword_cases[i].output[j].allowed_to_be_default_match,
95 matches[j].allowed_to_be_default_match); 107 matches[j].allowed_to_be_default_match);
96 } 108 }
97 } 109 }
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 { "aa foo", 5u, true, "aa.com?foo={searchTerms}", "foo", 1u }, 331 { "aa foo", 5u, true, "aa.com?foo={searchTerms}", "foo", 1u },
320 332
321 // Disallow exact keyword match. 333 // Disallow exact keyword match.
322 { "aa foo", base::string16::npos, false, "", "aa foo", 334 { "aa foo", base::string16::npos, false, "", "aa foo",
323 base::string16::npos }, 335 base::string16::npos },
324 }; 336 };
325 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { 337 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
326 AutocompleteInput input( 338 AutocompleteInput input(
327 ASCIIToUTF16(cases[i].text), cases[i].cursor_position, base::string16(), 339 ASCIIToUTF16(cases[i].text), cases[i].cursor_position, base::string16(),
328 GURL(), metrics::OmniboxEventProto::INVALID_SPEC, false, false, 340 GURL(), metrics::OmniboxEventProto::INVALID_SPEC, false, false,
329 cases[i].allow_exact_keyword_match, true, 341 cases[i].allow_exact_keyword_match, true, TestingSchemeClassifier());
330 ChromeAutocompleteSchemeClassifier(NULL));
331 const TemplateURL* url = 342 const TemplateURL* url =
332 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), 343 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(),
333 &input); 344 &input);
334 if (cases[i].expected_url.empty()) 345 if (cases[i].expected_url.empty())
335 EXPECT_FALSE(url); 346 EXPECT_FALSE(url);
336 else 347 else
337 EXPECT_EQ(cases[i].expected_url, url->url()); 348 EXPECT_EQ(cases[i].expected_url, url->url());
338 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); 349 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text());
339 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); 350 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position());
340 } 351 }
341 } 352 }
342 353
343 // If extra query params are specified on the command line, they should be 354 // If extra query params are specified on the command line, they should be
344 // reflected (only) in the default search provider's destination URL. 355 // reflected (only) in the default search provider's destination URL.
345 TEST_F(KeywordProviderTest, ExtraQueryParams) { 356 TEST_F(KeywordProviderTest, ExtraQueryParams) {
346 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 357 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
347 switches::kExtraSearchQueryParams, "a=b"); 358 switches::kExtraSearchQueryParams, "a=b");
348 359
349 TestData<GURL> url_cases[] = { 360 TestData<GURL> url_cases[] = {
350 { ASCIIToUTF16("a 1 2 3"), 3, 361 { ASCIIToUTF16("a 1 2 3"), 3,
351 { { GURL("aa.com?a=b&foo=1+2+3"), false }, 362 { { GURL("aa.com?a=b&foo=1+2+3"), false },
352 { GURL("bogus URL 1+2+3"), false }, 363 { GURL("bogus URL 1+2+3"), false },
353 { GURL("http://aaaa/?aaaa=1&b=1+2+3&c"), false } } }, 364 { GURL("http://aaaa/?aaaa=1&b=1+2+3&c"), false } } },
354 }; 365 };
355 366
356 RunTest<GURL>(url_cases, arraysize(url_cases), 367 RunTest<GURL>(url_cases, arraysize(url_cases),
357 &AutocompleteMatch::destination_url); 368 &AutocompleteMatch::destination_url);
358 } 369 }
OLDNEW
« no previous file with comments | « components/omnibox/keyword_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698