Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / base_search_provider_unittest.cc
blob0800f03b47a923f1a494557e9304ae1f527a7326
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
3 // found in the LICENSE file.
5 #include "components/omnibox/browser/base_search_provider.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/browser/autocomplete_match.h"
10 #include "components/omnibox/browser/autocomplete_match_type.h"
11 #include "components/omnibox/browser/autocomplete_scheme_classifier.h"
12 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
13 #include "components/omnibox/browser/search_suggestion_parser.h"
14 #include "components/omnibox/browser/suggestion_answer.h"
15 #include "components/search_engines/search_terms_data.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "components/search_engines/template_url_service_client.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using testing::NiceMock;
22 using testing::Return;
23 using testing::_;
25 class TestBaseSearchProvider : public BaseSearchProvider {
26 public:
27 typedef BaseSearchProvider::MatchMap MatchMap;
29 TestBaseSearchProvider(AutocompleteProvider::Type type,
30 AutocompleteProviderClient* client)
31 : BaseSearchProvider(type, client) {}
32 MOCK_METHOD1(DeleteMatch, void(const AutocompleteMatch& match));
33 MOCK_CONST_METHOD1(AddProviderInfo, void(ProvidersInfo* provider_info));
34 MOCK_CONST_METHOD1(GetTemplateURL, const TemplateURL*(bool is_keyword));
35 MOCK_CONST_METHOD1(GetInput, const AutocompleteInput(bool is_keyword));
36 MOCK_CONST_METHOD1(ShouldAppendExtraParams,
37 bool(const SearchSuggestionParser::SuggestResult& result));
38 MOCK_METHOD1(RecordDeletionResult, void(bool success));
40 MOCK_METHOD2(Start,
41 void(const AutocompleteInput& input, bool minimal_changes));
42 void AddMatchToMap(const SearchSuggestionParser::SuggestResult& result,
43 const std::string& metadata,
44 int accepted_suggestion,
45 bool mark_as_deletable,
46 bool in_keyword_mode,
47 MatchMap* map) {
48 BaseSearchProvider::AddMatchToMap(result,
49 metadata,
50 accepted_suggestion,
51 mark_as_deletable,
52 in_keyword_mode,
53 map);
56 protected:
57 virtual ~TestBaseSearchProvider() {}
59 private:
60 DISALLOW_COPY_AND_ASSIGN(TestBaseSearchProvider);
63 class BaseSearchProviderTest : public testing::Test {
64 public:
65 ~BaseSearchProviderTest() override {}
67 protected:
68 void SetUp() override {
69 scoped_ptr<TemplateURLService> template_url_service(new TemplateURLService(
70 nullptr, scoped_ptr<SearchTermsData>(new SearchTermsData), nullptr,
71 scoped_ptr<TemplateURLServiceClient>(), nullptr, nullptr,
72 base::Closure()));
73 client_.reset(new NiceMock<MockAutocompleteProviderClient>());
74 client_->set_template_url_service(template_url_service.Pass());
75 provider_ = new NiceMock<TestBaseSearchProvider>(
76 AutocompleteProvider::TYPE_SEARCH, client_.get());
79 scoped_refptr<NiceMock<TestBaseSearchProvider> > provider_;
80 scoped_ptr<NiceMock<MockAutocompleteProviderClient>> client_;
83 TEST_F(BaseSearchProviderTest, PreserveAnswersWhenDeduplicating) {
84 TemplateURLData data;
85 data.SetURL("http://foo.com/url?bar={searchTerms}");
86 scoped_ptr<TemplateURL> template_url(new TemplateURL(data));
88 TestBaseSearchProvider::MatchMap map;
89 base::string16 query = base::ASCIIToUTF16("weather los angeles");
90 base::string16 answer_contents = base::ASCIIToUTF16("some answer content");
91 base::string16 answer_type = base::ASCIIToUTF16("2334");
92 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
93 answer->set_type(2334);
95 EXPECT_CALL(*provider_, GetInput(_))
96 .WillRepeatedly(Return(AutocompleteInput()));
97 EXPECT_CALL(*provider_, GetTemplateURL(_))
98 .WillRepeatedly(Return(template_url.get()));
100 SearchSuggestionParser::SuggestResult more_relevant(
101 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(),
102 base::string16(), base::string16(), base::string16(), nullptr,
103 std::string(), std::string(), false, 1300, true, false, query);
104 provider_->AddMatchToMap(
105 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
106 false, false, &map);
108 SearchSuggestionParser::SuggestResult less_relevant(
109 query, AutocompleteMatchType::SEARCH_SUGGEST, query, base::string16(),
110 base::string16(), answer_contents, answer_type,
111 SuggestionAnswer::copy(answer.get()), std::string(), std::string(),
112 false, 850, true, false, query);
113 provider_->AddMatchToMap(
114 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
115 false, false, &map);
117 ASSERT_EQ(1U, map.size());
118 AutocompleteMatch match = map.begin()->second;
119 ASSERT_EQ(1U, match.duplicate_matches.size());
120 AutocompleteMatch duplicate = match.duplicate_matches[0];
122 EXPECT_EQ(answer_contents, match.answer_contents);
123 EXPECT_EQ(answer_type, match.answer_type);
124 EXPECT_TRUE(answer->Equals(*match.answer));
125 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type);
126 EXPECT_EQ(1300, match.relevance);
128 EXPECT_EQ(answer_contents, duplicate.answer_contents);
129 EXPECT_EQ(answer_type, duplicate.answer_type);
130 EXPECT_TRUE(answer->Equals(*duplicate.answer));
131 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type);
132 EXPECT_EQ(850, duplicate.relevance);
134 // Ensure answers are not copied over existing answers.
135 map.clear();
136 base::string16 answer_contents2 = base::ASCIIToUTF16("different answer");
137 base::string16 answer_type2 = base::ASCIIToUTF16("8242");
138 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
139 answer2->set_type(8242);
140 more_relevant = SearchSuggestionParser::SuggestResult(
141 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(),
142 base::string16(), answer_contents2, answer_type2,
143 SuggestionAnswer::copy(answer2.get()), std::string(), std::string(),
144 false, 1300, true, false, query);
145 provider_->AddMatchToMap(
146 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
147 false, false, &map);
148 provider_->AddMatchToMap(
149 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
150 false, false, &map);
151 ASSERT_EQ(1U, map.size());
152 match = map.begin()->second;
153 ASSERT_EQ(1U, match.duplicate_matches.size());
154 duplicate = match.duplicate_matches[0];
156 EXPECT_EQ(answer_contents2, match.answer_contents);
157 EXPECT_EQ(answer_type2, match.answer_type);
158 EXPECT_TRUE(answer2->Equals(*match.answer));
159 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type);
160 EXPECT_EQ(1300, match.relevance);
162 EXPECT_EQ(answer_contents, duplicate.answer_contents);
163 EXPECT_EQ(answer_type, duplicate.answer_type);
164 EXPECT_TRUE(answer->Equals(*duplicate.answer));
165 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type);
166 EXPECT_EQ(850, duplicate.relevance);