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

Side by Side Diff: components/omnibox/browser/autocomplete_provider_unittest.cc

Issue 1425013002: Removing bad dependencies from autocomplete_provider_unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add comment for client_owned_ Created 5 years, 1 month 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
« no previous file with comments | « components/omnibox/browser/BUILD.gn ('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 "components/omnibox/browser/autocomplete_provider.h" 5 #include "components/omnibox/browser/autocomplete_provider.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
11 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
17 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
18 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "components/metrics/proto/omnibox_event.pb.h" 18 #include "components/metrics/proto/omnibox_event.pb.h"
24 #include "components/omnibox/browser/autocomplete_controller.h" 19 #include "components/omnibox/browser/autocomplete_controller.h"
25 #include "components/omnibox/browser/autocomplete_input.h" 20 #include "components/omnibox/browser/autocomplete_input.h"
26 #include "components/omnibox/browser/autocomplete_match.h" 21 #include "components/omnibox/browser/autocomplete_match.h"
27 #include "components/omnibox/browser/autocomplete_provider_listener.h" 22 #include "components/omnibox/browser/autocomplete_provider_listener.h"
28 #include "components/omnibox/browser/keyword_provider.h" 23 #include "components/omnibox/browser/keyword_provider.h"
24 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
29 #include "components/omnibox/browser/search_provider.h" 25 #include "components/omnibox/browser/search_provider.h"
30 #include "components/search_engines/search_engines_switches.h" 26 #include "components/search_engines/search_engines_switches.h"
31 #include "components/search_engines/template_url.h" 27 #include "components/search_engines/template_url.h"
32 #include "components/search_engines/template_url_service.h" 28 #include "components/search_engines/template_url_service.h"
33 #include "content/public/browser/notification_observer.h" 29 #include "components/search_engines/template_url_service_client.h"
34 #include "content/public/browser/notification_registrar.h"
35 #include "content/public/browser/notification_source.h"
36 #include "content/public/test/test_browser_thread_bundle.h"
37 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
38 31
39 static std::ostream& operator<<(std::ostream& os, 32 static std::ostream& operator<<(std::ostream& os,
40 const AutocompleteResult::const_iterator& it) { 33 const AutocompleteResult::const_iterator& it) {
41 return os << static_cast<const AutocompleteMatch*>(&(*it)); 34 return os << static_cast<const AutocompleteMatch*>(&(*it));
42 } 35 }
43 36
44 namespace { 37 namespace {
Peter Kasting 2015/11/19 19:04:41 Nit: Blank line below
Abhishek 2015/11/20 09:37:20 Done.
45 const size_t kResultsPerProvider = 3; 38 const size_t kResultsPerProvider = 3;
46 const char kTestTemplateURLKeyword[] = "t"; 39 const char kTestTemplateURLKeyword[] = "t";
47 } 40
41 class TestingSchemeClassifier : public AutocompleteSchemeClassifier {
42 public:
43 metrics::OmniboxInputType::Type GetInputTypeForScheme(
44 const std::string& scheme) const override {
45 if (net::URLRequest::IsHandledProtocol(scheme))
46 return metrics::OmniboxInputType::URL;
47 return metrics::OmniboxInputType::INVALID;
Peter Kasting 2015/11/19 19:04:41 Nit: Simpler: return net::URLRequest::IsHandl
Abhishek 2015/11/20 09:37:21 Done.
48 }
49 };
Peter Kasting 2015/11/19 19:04:41 Nit: DISALLOW_COPY_AND_ASSIGN
Abhishek 2015/11/20 09:37:20 Done.
50
51 // AutocompleteProviderClient implementation that calls the specified closure
52 // when the result is ready.
53 class AutocompleteProviderClientWithClosure
54 : public MockAutocompleteProviderClient {
55 public:
56 void set_closure(const base::Closure& closure) { closure_ = closure; }
57
58 private:
59 void OnAutocompleteControllerResultReady(
60 AutocompleteController* controller) override {
61 if (closure_.is_null())
62 return;
Peter Kasting 2015/11/19 19:04:41 Don't we still need to call QuitWhenIdle() if the
Abhishek 2015/11/20 09:37:20 I think we can't do that, because tests are gettin
Peter Kasting 2015/11/20 19:52:18 Can you investigate to the point of understanding
Abhishek 2015/11/21 12:11:20 AutocompleteProviderTest.AllowExactKeywordMatch Au
Peter Kasting 2015/11/21 21:08:42 Crashes how? DCHECK failure? I think you should
Abhishek 2015/11/23 15:22:07 Actually its crashing in both the cases(either clo
droger 2015/11/23 16:11:15 I don't know much about the specifics of this test
63
64 if (controller->done()) {
65 closure_.Run();
66 base::MessageLoop::current()->QuitWhenIdle();
67 }
68 }
69
70 base::Closure closure_;
71 };
Peter Kasting 2015/11/19 19:04:41 Nit: DISALLOW_COPY_AND_ASSIGN
Abhishek 2015/11/20 09:37:21 Done.
72
73 } // namespace
48 74
49 // Autocomplete provider that provides known results. Note that this is 75 // Autocomplete provider that provides known results. Note that this is
50 // refcounted so that it can also be a task on the message loop. 76 // refcounted so that it can also be a task on the message loop.
51 class TestProvider : public AutocompleteProvider { 77 class TestProvider : public AutocompleteProvider {
52 public: 78 public:
53 TestProvider(int relevance, const base::string16& prefix, 79 TestProvider(int relevance,
54 Profile* profile, 80 const base::string16& prefix,
55 const base::string16 match_keyword) 81 const base::string16 match_keyword,
82 AutocompleteProviderClient* client)
56 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH), 83 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH),
57 listener_(NULL), 84 listener_(NULL),
58 profile_(profile),
59 relevance_(relevance), 85 relevance_(relevance),
60 prefix_(prefix), 86 prefix_(prefix),
61 match_keyword_(match_keyword) { 87 match_keyword_(match_keyword),
62 } 88 client_(client) {}
63 89
64 void Start(const AutocompleteInput& input, bool minimal_changes) override; 90 void Start(const AutocompleteInput& input, bool minimal_changes) override;
65 91
66 void set_listener(AutocompleteProviderListener* listener) { 92 void set_listener(AutocompleteProviderListener* listener) {
67 listener_ = listener; 93 listener_ = listener;
68 } 94 }
69 95
70 private: 96 private:
71 ~TestProvider() override {} 97 ~TestProvider() override {}
72 98
73 void Run(); 99 void Run();
74 100
75 void AddResults(int start_at, int num); 101 void AddResults(int start_at, int num);
76 void AddResultsWithSearchTermsArgs( 102 void AddResultsWithSearchTermsArgs(
77 int start_at, 103 int start_at,
78 int num, 104 int num,
79 AutocompleteMatch::Type type, 105 AutocompleteMatch::Type type,
80 const TemplateURLRef::SearchTermsArgs& search_terms_args); 106 const TemplateURLRef::SearchTermsArgs& search_terms_args);
81 107
82 AutocompleteProviderListener* listener_; 108 AutocompleteProviderListener* listener_;
83 Profile* profile_;
84 int relevance_; 109 int relevance_;
85 const base::string16 prefix_; 110 const base::string16 prefix_;
86 const base::string16 match_keyword_; 111 const base::string16 match_keyword_;
112 AutocompleteProviderClient* client_;
87 }; 113 };
Peter Kasting 2015/11/19 19:04:41 Nit: DISALLOW_COPY_AND_ASSIGN
Abhishek 2015/11/20 09:37:21 Done.
88 114
89 void TestProvider::Start(const AutocompleteInput& input, bool minimal_changes) { 115 void TestProvider::Start(const AutocompleteInput& input, bool minimal_changes) {
90 if (minimal_changes) 116 if (minimal_changes)
91 return; 117 return;
92 118
93 matches_.clear(); 119 matches_.clear();
94 120
95 if (input.from_omnibox_focus()) 121 if (input.from_omnibox_focus())
96 return; 122 return;
97 123
(...skipping 18 matching lines...) Expand all
116 142
117 void TestProvider::Run() { 143 void TestProvider::Run() {
118 DCHECK_GT(kResultsPerProvider, 0U); 144 DCHECK_GT(kResultsPerProvider, 0U);
119 AddResults(1, kResultsPerProvider); 145 AddResults(1, kResultsPerProvider);
120 done_ = true; 146 done_ = true;
121 DCHECK(listener_); 147 DCHECK(listener_);
122 listener_->OnProviderUpdate(true); 148 listener_->OnProviderUpdate(true);
123 } 149 }
124 150
125 void TestProvider::AddResults(int start_at, int num) { 151 void TestProvider::AddResults(int start_at, int num) {
126 AddResultsWithSearchTermsArgs(start_at, 152 AddResultsWithSearchTermsArgs(
127 num, 153 start_at, num, AutocompleteMatchType::URL_WHAT_YOU_TYPED,
128 AutocompleteMatchType::URL_WHAT_YOU_TYPED, 154 TemplateURLRef::SearchTermsArgs(base::string16()));
129 TemplateURLRef::SearchTermsArgs(
130 base::string16()));
131 } 155 }
132 156
133 void TestProvider::AddResultsWithSearchTermsArgs( 157 void TestProvider::AddResultsWithSearchTermsArgs(
134 int start_at, 158 int start_at,
135 int num, 159 int num,
136 AutocompleteMatch::Type type, 160 AutocompleteMatch::Type type,
137 const TemplateURLRef::SearchTermsArgs& search_terms_args) { 161 const TemplateURLRef::SearchTermsArgs& search_terms_args) {
138 for (int i = start_at; i < num; i++) { 162 for (int i = start_at; i < num; i++) {
139 AutocompleteMatch match(this, relevance_ - i, false, type); 163 AutocompleteMatch match(this, relevance_ - i, false, type);
140 164
141 match.fill_into_edit = prefix_ + base::UTF8ToUTF16(base::IntToString(i)); 165 match.fill_into_edit = prefix_ + base::UTF8ToUTF16(base::IntToString(i));
142 match.destination_url = GURL(base::UTF16ToUTF8(match.fill_into_edit)); 166 match.destination_url = GURL(base::UTF16ToUTF8(match.fill_into_edit));
143 match.allowed_to_be_default_match = true; 167 match.allowed_to_be_default_match = true;
144 168
145 match.contents = match.fill_into_edit; 169 match.contents = match.fill_into_edit;
146 match.contents_class.push_back( 170 match.contents_class.push_back(
147 ACMatchClassification(0, ACMatchClassification::NONE)); 171 ACMatchClassification(0, ACMatchClassification::NONE));
148 match.description = match.fill_into_edit; 172 match.description = match.fill_into_edit;
149 match.description_class.push_back( 173 match.description_class.push_back(
150 ACMatchClassification(0, ACMatchClassification::NONE)); 174 ACMatchClassification(0, ACMatchClassification::NONE));
151 match.search_terms_args.reset( 175 match.search_terms_args.reset(
152 new TemplateURLRef::SearchTermsArgs(search_terms_args)); 176 new TemplateURLRef::SearchTermsArgs(search_terms_args));
153 if (!match_keyword_.empty()) { 177 if (!match_keyword_.empty()) {
154 match.keyword = match_keyword_; 178 match.keyword = match_keyword_;
155 TemplateURLService* service = 179 TemplateURLService* service = client_->GetTemplateURLService();
156 TemplateURLServiceFactory::GetForProfile(profile_);
157 ASSERT_TRUE(match.GetTemplateURL(service, false) != NULL); 180 ASSERT_TRUE(match.GetTemplateURL(service, false) != NULL);
158 } 181 }
159 182
160 matches_.push_back(match); 183 matches_.push_back(match);
161 } 184 }
162 } 185 }
163 186
164 class AutocompleteProviderTest : public testing::Test, 187 class AutocompleteProviderTest : public testing::Test {
165 public content::NotificationObserver { 188 public:
189 ~AutocompleteProviderTest() override;
190 AutocompleteProviderTest();
Peter Kasting 2015/11/19 19:04:41 Nit: Constructor before destructor
Abhishek 2015/11/20 09:37:20 Done.
191
166 protected: 192 protected:
167 struct KeywordTestData { 193 struct KeywordTestData {
168 const base::string16 fill_into_edit; 194 const base::string16 fill_into_edit;
169 const base::string16 keyword; 195 const base::string16 keyword;
170 const base::string16 expected_associated_keyword; 196 const base::string16 expected_associated_keyword;
171 }; 197 };
172 198
173 struct AssistedQueryStatsTestData { 199 struct AssistedQueryStatsTestData {
174 const AutocompleteMatch::Type match_type; 200 const AutocompleteMatch::Type match_type;
175 const std::string expected_aqs; 201 const std::string expected_aqs;
176 }; 202 };
177 203
178 protected: 204 protected:
Peter Kasting 2015/11/19 19:04:41 Nit: Remove this duplicate line
Abhishek 2015/11/20 09:37:21 Done.
179 // Registers a test TemplateURL under the given keyword. 205 // Registers a test TemplateURL under the given keyword.
180 void RegisterTemplateURL(const base::string16 keyword, 206 void RegisterTemplateURL(const base::string16 keyword,
181 const std::string& template_url); 207 const std::string& template_url);
182 208
183 // Resets |controller_| with two TestProviders. |provider1_ptr| and 209 // Resets |controller_| with two TestProviders. |provider1_ptr| and
184 // |provider2_ptr| are updated to point to the new providers if non-NULL. 210 // |provider2_ptr| are updated to point to the new providers if non-NULL.
185 void ResetControllerWithTestProviders(bool same_destinations, 211 void ResetControllerWithTestProviders(bool same_destinations,
186 TestProvider** provider1_ptr, 212 TestProvider** provider1_ptr,
187 TestProvider** provider2_ptr); 213 TestProvider** provider2_ptr);
188 214
189 // Runs a query on the input "a", and makes sure both providers' input is 215 // Runs a query on the input "a", and makes sure both providers' input is
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 return controller_->search_provider_->field_trial_triggered_in_session(); 248 return controller_->search_provider_->field_trial_triggered_in_session();
223 } 249 }
224 void set_current_page_classification( 250 void set_current_page_classification(
225 metrics::OmniboxEventProto::PageClassification classification) { 251 metrics::OmniboxEventProto::PageClassification classification) {
226 controller_->input_.current_page_classification_ = classification; 252 controller_->input_.current_page_classification_ = classification;
227 } 253 }
228 254
229 AutocompleteResult result_; 255 AutocompleteResult result_;
230 256
231 private: 257 private:
232 // content::NotificationObserver: 258 base::MessageLoop message_loop_;
233 void Observe(int type, 259 scoped_ptr<AutocompleteController> controller_;
234 const content::NotificationSource& source, 260 // Owned by |controller_|.
235 const content::NotificationDetails& details) override; 261 AutocompleteProviderClientWithClosure* client_;
262 // Sanity check to ensure that |client_| ownership has been passed to
Peter Kasting 2015/11/19 19:04:40 Nit: Sanity check -> Used
Abhishek 2015/11/20 09:37:20 Done.
263 // |controller_| exactly once.
264 bool client_owned_;
265 };
Peter Kasting 2015/11/19 19:04:41 Nit: DISALLOW_COPY_AND_ASSIGN
Abhishek 2015/11/20 09:37:20 Done.
236 266
237 content::TestBrowserThreadBundle thread_bundle_; 267 AutocompleteProviderTest::AutocompleteProviderTest()
238 content::NotificationRegistrar registrar_; 268 : client_(new AutocompleteProviderClientWithClosure), client_owned_(false) {
239 TestingProfile profile_; 269 scoped_ptr<TemplateURLService> template_url_service(
240 scoped_ptr<AutocompleteController> controller_; 270 new TemplateURLService(nullptr, 0));
241 }; 271 client_->set_template_url_service(template_url_service.Pass());
272 }
273
274 AutocompleteProviderTest::~AutocompleteProviderTest() {
275 DCHECK(client_owned_);
Peter Kasting 2015/11/19 19:04:41 Avoid using DCHECK in tests; failed DCHECKs crash
Abhishek 2015/11/20 09:37:20 As per the gtest.h we can't use ASSERT_* directly
Peter Kasting 2015/11/20 19:52:18 Avoiding ASSERT in destructors is a good move. I
Abhishek 2015/11/21 12:11:20 Done.
276 }
242 277
243 void AutocompleteProviderTest::RegisterTemplateURL( 278 void AutocompleteProviderTest::RegisterTemplateURL(
244 const base::string16 keyword, 279 const base::string16 keyword,
245 const std::string& template_url) { 280 const std::string& template_url) {
246 if (TemplateURLServiceFactory::GetForProfile(&profile_) == NULL) {
247 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
248 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
249 }
250 TemplateURLData data; 281 TemplateURLData data;
251 data.SetURL(template_url); 282 data.SetURL(template_url);
252 data.SetShortName(keyword); 283 data.SetShortName(keyword);
253 data.SetKeyword(keyword); 284 data.SetKeyword(keyword);
254 TemplateURL* default_t_url = new TemplateURL(data); 285 TemplateURL* default_t_url = new TemplateURL(data);
255 TemplateURLService* turl_model = 286 TemplateURLService* turl_model = client_->GetTemplateURLService();
256 TemplateURLServiceFactory::GetForProfile(&profile_);
257 turl_model->Add(default_t_url); 287 turl_model->Add(default_t_url);
258 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); 288 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url);
259 turl_model->Load(); 289 turl_model->Load();
260 TemplateURLID default_provider_id = default_t_url->id(); 290 TemplateURLID default_provider_id = default_t_url->id();
261 ASSERT_NE(0, default_provider_id); 291 ASSERT_NE(0, default_provider_id);
262 } 292 }
263 293
264 void AutocompleteProviderTest::ResetControllerWithTestProviders( 294 void AutocompleteProviderTest::ResetControllerWithTestProviders(
265 bool same_destinations, 295 bool same_destinations,
266 TestProvider** provider1_ptr, 296 TestProvider** provider1_ptr,
267 TestProvider** provider2_ptr) { 297 TestProvider** provider2_ptr) {
268 // TODO: Move it outside this method, after refactoring the existing 298 // TODO: Move it outside this method, after refactoring the existing
269 // unit tests. Specifically: 299 // unit tests. Specifically:
270 // (1) Make sure that AutocompleteMatch.keyword is set iff there is 300 // (1) Make sure that AutocompleteMatch.keyword is set iff there is
271 // a corresponding call to RegisterTemplateURL; otherwise the 301 // a corresponding call to RegisterTemplateURL; otherwise the
272 // controller flow will crash; this practically means that 302 // controller flow will crash; this practically means that
273 // RunTests/ResetControllerXXX/RegisterTemplateURL should 303 // RunTests/ResetControllerXXX/RegisterTemplateURL should
274 // be coordinated with each other. 304 // be coordinated with each other.
275 // (2) Inject test arguments rather than rely on the hardcoded values, e.g. 305 // (2) Inject test arguments rather than rely on the hardcoded values, e.g.
276 // don't rely on kResultsPerProvided and default relevance ordering 306 // don't rely on kResultsPerProvided and default relevance ordering
277 // (B > A). 307 // (B > A).
278 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword), 308 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword),
279 "http://aqs/{searchTerms}/{google:assistedQueryStats}"); 309 "http://aqs/{searchTerms}/{google:assistedQueryStats}");
280 310
281 AutocompleteController::Providers providers; 311 AutocompleteController::Providers providers;
282 312
283 // Construct two new providers, with either the same or different prefixes. 313 // Construct two new providers, with either the same or different prefixes.
284 TestProvider* provider1 = new TestProvider( 314 TestProvider* provider1 =
285 kResultsPerProvider, 315 new TestProvider(kResultsPerProvider, base::ASCIIToUTF16("http://a"),
286 base::ASCIIToUTF16("http://a"), 316 base::ASCIIToUTF16(kTestTemplateURLKeyword), client_);
287 &profile_,
288 base::ASCIIToUTF16(kTestTemplateURLKeyword));
289 providers.push_back(provider1); 317 providers.push_back(provider1);
290 318
291 TestProvider* provider2 = new TestProvider( 319 TestProvider* provider2 =
292 kResultsPerProvider * 2, 320 new TestProvider(kResultsPerProvider * 2,
293 same_destinations ? base::ASCIIToUTF16("http://a") 321 same_destinations ? base::ASCIIToUTF16("http://a")
294 : base::ASCIIToUTF16("http://b"), 322 : base::ASCIIToUTF16("http://b"),
295 &profile_, 323 base::string16(), client_);
Peter Kasting 2015/11/19 19:04:41 Nit: Shorter: TestProvider* provider2 = new Tes
Abhishek 2015/11/20 09:37:21 Done.
296 base::string16());
297 providers.push_back(provider2); 324 providers.push_back(provider2);
298 325
326 DCHECK(!client_owned_);
299 // Reset the controller to contain our new providers. 327 // Reset the controller to contain our new providers.
300 controller_.reset(new AutocompleteController( 328 controller_.reset(
301 329 new AutocompleteController(make_scoped_ptr(client_), NULL, 0));
302 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL, 330 client_owned_ = true;
Peter Kasting 2015/11/19 19:04:41 Nit: Three times you write a block of code to do:
Abhishek 2015/11/20 09:37:21 Done.
303 0));
304 // We're going to swap the providers vector, but the old vector should be 331 // We're going to swap the providers vector, but the old vector should be
305 // empty so no elements need to be freed at this point. 332 // empty so no elements need to be freed at this point.
306 EXPECT_TRUE(controller_->providers_.empty()); 333 EXPECT_TRUE(controller_->providers_.empty());
307 controller_->providers_.swap(providers); 334 controller_->providers_.swap(providers);
308 provider1->set_listener(controller_.get()); 335 provider1->set_listener(controller_.get());
309 provider2->set_listener(controller_.get()); 336 provider2->set_listener(controller_.get());
310 337
311 // The providers don't complete synchronously, so listen for "result updated" 338 client_->set_closure(base::Bind(&AutocompleteProviderTest::CopyResults,
312 // notifications. 339 base::Unretained(this)));
313 registrar_.Add(this,
314 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
315 content::Source<AutocompleteController>(controller_.get()));
316 340
317 if (provider1_ptr) 341 if (provider1_ptr)
318 *provider1_ptr = provider1; 342 *provider1_ptr = provider1;
319 if (provider2_ptr) 343 if (provider2_ptr)
320 *provider2_ptr = provider2; 344 *provider2_ptr = provider2;
321 } 345 }
322 346
323 void AutocompleteProviderTest:: 347 void AutocompleteProviderTest::ResetControllerWithKeywordAndSearchProviders() {
324 ResetControllerWithKeywordAndSearchProviders() {
325 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
326 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
327
328 // Reset the default TemplateURL. 348 // Reset the default TemplateURL.
329 TemplateURLData data; 349 TemplateURLData data;
330 data.SetShortName(base::ASCIIToUTF16("default")); 350 data.SetShortName(base::ASCIIToUTF16("default"));
331 data.SetKeyword(base::ASCIIToUTF16("default")); 351 data.SetKeyword(base::ASCIIToUTF16("default"));
332 data.SetURL("http://defaultturl/{searchTerms}"); 352 data.SetURL("http://defaultturl/{searchTerms}");
333 TemplateURL* default_t_url = new TemplateURL(data); 353 TemplateURL* default_t_url = new TemplateURL(data);
334 TemplateURLService* turl_model = 354 TemplateURLService* turl_model = client_->GetTemplateURLService();
335 TemplateURLServiceFactory::GetForProfile(&profile_);
336 turl_model->Add(default_t_url); 355 turl_model->Add(default_t_url);
337 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); 356 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url);
338 TemplateURLID default_provider_id = default_t_url->id(); 357 TemplateURLID default_provider_id = default_t_url->id();
339 ASSERT_NE(0, default_provider_id); 358 ASSERT_NE(0, default_provider_id);
340 359
341 // Create another TemplateURL for KeywordProvider. 360 // Create another TemplateURL for KeywordProvider.
342 TemplateURLData data2; 361 TemplateURLData data2;
343 data2.SetShortName(base::ASCIIToUTF16("k")); 362 data2.SetShortName(base::ASCIIToUTF16("k"));
344 data2.SetKeyword(base::ASCIIToUTF16("k")); 363 data2.SetKeyword(base::ASCIIToUTF16("k"));
345 data2.SetURL("http://keyword/{searchTerms}"); 364 data2.SetURL("http://keyword/{searchTerms}");
346 TemplateURL* keyword_t_url = new TemplateURL(data2); 365 TemplateURL* keyword_t_url = new TemplateURL(data2);
347 turl_model->Add(keyword_t_url); 366 turl_model->Add(keyword_t_url);
348 ASSERT_NE(0, keyword_t_url->id()); 367 ASSERT_NE(0, keyword_t_url->id());
349 368
369 DCHECK(!client_owned_);
350 controller_.reset(new AutocompleteController( 370 controller_.reset(new AutocompleteController(
351 371 make_scoped_ptr(client_), NULL,
352 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL,
353 AutocompleteProvider::TYPE_KEYWORD | AutocompleteProvider::TYPE_SEARCH)); 372 AutocompleteProvider::TYPE_KEYWORD | AutocompleteProvider::TYPE_SEARCH));
373 client_owned_ = true;
354 } 374 }
355 375
356 void AutocompleteProviderTest::ResetControllerWithKeywordProvider() { 376 void AutocompleteProviderTest::ResetControllerWithKeywordProvider() {
357 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 377 TemplateURLService* turl_model = client_->GetTemplateURLService();
358 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
359
360 TemplateURLService* turl_model =
361 TemplateURLServiceFactory::GetForProfile(&profile_);
362 378
363 // Create a TemplateURL for KeywordProvider. 379 // Create a TemplateURL for KeywordProvider.
364 TemplateURLData data; 380 TemplateURLData data;
365 data.SetShortName(base::ASCIIToUTF16("foo.com")); 381 data.SetShortName(base::ASCIIToUTF16("foo.com"));
366 data.SetKeyword(base::ASCIIToUTF16("foo.com")); 382 data.SetKeyword(base::ASCIIToUTF16("foo.com"));
367 data.SetURL("http://foo.com/{searchTerms}"); 383 data.SetURL("http://foo.com/{searchTerms}");
368 TemplateURL* keyword_t_url = new TemplateURL(data); 384 TemplateURL* keyword_t_url = new TemplateURL(data);
369 turl_model->Add(keyword_t_url); 385 turl_model->Add(keyword_t_url);
370 ASSERT_NE(0, keyword_t_url->id()); 386 ASSERT_NE(0, keyword_t_url->id());
371 387
372 // Make a TemplateURL for KeywordProvider that a shorter version of the 388 // Make a TemplateURL for KeywordProvider that a shorter version of the
373 // first. 389 // first.
374 data.SetShortName(base::ASCIIToUTF16("f")); 390 data.SetShortName(base::ASCIIToUTF16("f"));
375 data.SetKeyword(base::ASCIIToUTF16("f")); 391 data.SetKeyword(base::ASCIIToUTF16("f"));
376 data.SetURL("http://f.com/{searchTerms}"); 392 data.SetURL("http://f.com/{searchTerms}");
377 keyword_t_url = new TemplateURL(data); 393 keyword_t_url = new TemplateURL(data);
378 turl_model->Add(keyword_t_url); 394 turl_model->Add(keyword_t_url);
379 ASSERT_NE(0, keyword_t_url->id()); 395 ASSERT_NE(0, keyword_t_url->id());
380 396
381 // Create another TemplateURL for KeywordProvider. 397 // Create another TemplateURL for KeywordProvider.
382 data.SetShortName(base::ASCIIToUTF16("bar.com")); 398 data.SetShortName(base::ASCIIToUTF16("bar.com"));
383 data.SetKeyword(base::ASCIIToUTF16("bar.com")); 399 data.SetKeyword(base::ASCIIToUTF16("bar.com"));
384 data.SetURL("http://bar.com/{searchTerms}"); 400 data.SetURL("http://bar.com/{searchTerms}");
385 keyword_t_url = new TemplateURL(data); 401 keyword_t_url = new TemplateURL(data);
386 turl_model->Add(keyword_t_url); 402 turl_model->Add(keyword_t_url);
387 ASSERT_NE(0, keyword_t_url->id()); 403 ASSERT_NE(0, keyword_t_url->id());
388 404
405 DCHECK(!client_owned_);
389 controller_.reset(new AutocompleteController( 406 controller_.reset(new AutocompleteController(
390 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL, 407 make_scoped_ptr(client_), NULL, AutocompleteProvider::TYPE_KEYWORD));
391 AutocompleteProvider::TYPE_KEYWORD)); 408 client_owned_ = true;
392 } 409 }
393 410
394 void AutocompleteProviderTest::RunTest() { 411 void AutocompleteProviderTest::RunTest() {
395 RunQuery(base::ASCIIToUTF16("a")); 412 RunQuery(base::ASCIIToUTF16("a"));
396 } 413 }
397 414
398 void AutocompleteProviderTest::RunKeywordTest(const base::string16& input, 415 void AutocompleteProviderTest::RunKeywordTest(const base::string16& input,
399 const KeywordTestData* match_data, 416 const KeywordTestData* match_data,
400 size_t size) { 417 size_t size) {
401 ACMatches matches; 418 ACMatches matches;
402 for (size_t i = 0; i < size; ++i) { 419 for (size_t i = 0; i < size; ++i) {
403 AutocompleteMatch match; 420 AutocompleteMatch match;
404 match.relevance = 1000; // Arbitrary non-zero value. 421 match.relevance = 1000; // Arbitrary non-zero value.
405 match.allowed_to_be_default_match = true; 422 match.allowed_to_be_default_match = true;
406 match.fill_into_edit = match_data[i].fill_into_edit; 423 match.fill_into_edit = match_data[i].fill_into_edit;
407 match.transition = ui::PAGE_TRANSITION_KEYWORD; 424 match.transition = ui::PAGE_TRANSITION_KEYWORD;
408 match.keyword = match_data[i].keyword; 425 match.keyword = match_data[i].keyword;
409 matches.push_back(match); 426 matches.push_back(match);
410 } 427 }
411 428
412 controller_->input_ = AutocompleteInput( 429 controller_->input_ = AutocompleteInput(
413 input, base::string16::npos, std::string(), GURL(), 430 input, base::string16::npos, std::string(), GURL(),
414 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, 431 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
415 false, true, true, true, false, 432 false, true, true, true, false, TestingSchemeClassifier());
416 ChromeAutocompleteSchemeClassifier(&profile_));
417 AutocompleteResult result; 433 AutocompleteResult result;
418 result.AppendMatches(controller_->input_, matches); 434 result.AppendMatches(controller_->input_, matches);
419 controller_->UpdateAssociatedKeywords(&result); 435 controller_->UpdateAssociatedKeywords(&result);
420
421 for (size_t j = 0; j < result.size(); ++j) { 436 for (size_t j = 0; j < result.size(); ++j) {
422 EXPECT_EQ(match_data[j].expected_associated_keyword, 437 EXPECT_EQ(match_data[j].expected_associated_keyword,
423 result.match_at(j)->associated_keyword.get() ? 438 result.match_at(j)->associated_keyword.get() ?
424 result.match_at(j)->associated_keyword->keyword : 439 result.match_at(j)->associated_keyword->keyword :
425 base::string16()); 440 base::string16());
426 } 441 }
427 } 442 }
428 443
429 void AutocompleteProviderTest::RunAssistedQueryStatsTest( 444 void AutocompleteProviderTest::RunAssistedQueryStatsTest(
430 const AssistedQueryStatsTestData* aqs_test_data, 445 const AssistedQueryStatsTestData* aqs_test_data,
(...skipping 18 matching lines...) Expand all
449 464
450 // Verify data. 465 // Verify data.
451 for (size_t i = 0; i < size; ++i) { 466 for (size_t i = 0; i < size; ++i) {
452 EXPECT_EQ(aqs_test_data[i].expected_aqs, 467 EXPECT_EQ(aqs_test_data[i].expected_aqs,
453 result_.match_at(i)->search_terms_args->assisted_query_stats); 468 result_.match_at(i)->search_terms_args->assisted_query_stats);
454 } 469 }
455 } 470 }
456 471
457 void AutocompleteProviderTest::RunQuery(const base::string16 query) { 472 void AutocompleteProviderTest::RunQuery(const base::string16 query) {
458 result_.Reset(); 473 result_.Reset();
459 controller_->Start(AutocompleteInput( 474 controller_->Start(
460 query, base::string16::npos, std::string(), GURL(), 475 AutocompleteInput(query, base::string16::npos, std::string(), GURL(),
461 metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, true, false, 476 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
462 ChromeAutocompleteSchemeClassifier(&profile_))); 477 true, true, false, TestingSchemeClassifier()));
463 478
464 if (!controller_->done()) 479 if (!controller_->done())
465 // The message loop will terminate when all autocomplete input has been 480 // The message loop will terminate when all autocomplete input has been
466 // collected. 481 // collected.
467 base::MessageLoop::current()->Run(); 482 base::MessageLoop::current()->Run();
468 } 483 }
469 484
470 void AutocompleteProviderTest::RunExactKeymatchTest( 485 void AutocompleteProviderTest::RunExactKeymatchTest(
471 bool allow_exact_keyword_match) { 486 bool allow_exact_keyword_match) {
472 // Send the controller input which exactly matches the keyword provider we 487 // Send the controller input which exactly matches the keyword provider we
473 // created in ResetControllerWithKeywordAndSearchProviders(). The default 488 // created in ResetControllerWithKeywordAndSearchProviders(). The default
474 // match should thus be a search-other-engine match iff 489 // match should thus be a search-other-engine match iff
475 // |allow_exact_keyword_match| is true. Regardless, the match should 490 // |allow_exact_keyword_match| is true. Regardless, the match should
476 // be from SearchProvider. (It provides all verbatim search matches, 491 // be from SearchProvider. (It provides all verbatim search matches,
477 // keyword or not.) 492 // keyword or not.)
478 controller_->Start(AutocompleteInput( 493 controller_->Start(AutocompleteInput(
479 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(), 494 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(),
480 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 495 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
481 allow_exact_keyword_match, false, false, 496 allow_exact_keyword_match, false, false, TestingSchemeClassifier()));
482 ChromeAutocompleteSchemeClassifier(&profile_)));
483 EXPECT_TRUE(controller_->done()); 497 EXPECT_TRUE(controller_->done());
484 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, 498 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH,
485 controller_->result().default_match()->provider->type()); 499 controller_->result().default_match()->provider->type());
486 EXPECT_EQ(allow_exact_keyword_match ? 500 EXPECT_EQ(allow_exact_keyword_match ?
487 AutocompleteMatchType::SEARCH_OTHER_ENGINE : 501 AutocompleteMatchType::SEARCH_OTHER_ENGINE :
488 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 502 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
489 controller_->result().default_match()->type); 503 controller_->result().default_match()->type);
490 } 504 }
491 505
492 void AutocompleteProviderTest::CopyResults() { 506 void AutocompleteProviderTest::CopyResults() {
493 result_.CopyFrom(controller_->result()); 507 result_.CopyFrom(controller_->result());
494 } 508 }
495 509
496 GURL AutocompleteProviderTest::GetDestinationURL( 510 GURL AutocompleteProviderTest::GetDestinationURL(
497 AutocompleteMatch match, 511 AutocompleteMatch match,
498 base::TimeDelta query_formulation_time) const { 512 base::TimeDelta query_formulation_time) const {
499 controller_->UpdateMatchDestinationURLWithQueryFormulationTime( 513 controller_->UpdateMatchDestinationURLWithQueryFormulationTime(
500 query_formulation_time, &match); 514 query_formulation_time, &match);
501 return match.destination_url; 515 return match.destination_url;
502 } 516 }
503 517
504 void AutocompleteProviderTest::Observe(
505 int type,
506 const content::NotificationSource& source,
507 const content::NotificationDetails& details) {
508 if (controller_->done()) {
509 CopyResults();
510 base::MessageLoop::current()->QuitWhenIdle();
511 }
512 }
513
514 // Tests that the default selection is set properly when updating results. 518 // Tests that the default selection is set properly when updating results.
515 TEST_F(AutocompleteProviderTest, Query) { 519 TEST_F(AutocompleteProviderTest, Query) {
516 TestProvider* provider1 = NULL; 520 TestProvider* provider1 = NULL;
517 TestProvider* provider2 = NULL; 521 TestProvider* provider2 = NULL;
518 ResetControllerWithTestProviders(false, &provider1, &provider2); 522 ResetControllerWithTestProviders(false, &provider1, &provider2);
519 RunTest(); 523 RunTest();
520 524
521 // Make sure the default match gets set to the highest relevance match. The 525 // Make sure the default match gets set to the highest relevance match. The
522 // highest relevance matches should come from the second provider. 526 // highest relevance matches should come from the second provider.
523 EXPECT_EQ(kResultsPerProvider * 2, result_.size()); 527 EXPECT_EQ(kResultsPerProvider * 2, result_.size());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 // Test that exact match keywords trump keywords associated with 632 // Test that exact match keywords trump keywords associated with
629 // the match. 633 // the match.
630 TEST_F(AutocompleteProviderTest, ExactMatchKeywords) { 634 TEST_F(AutocompleteProviderTest, ExactMatchKeywords) {
631 ResetControllerWithKeywordProvider(); 635 ResetControllerWithKeywordProvider();
632 636
633 { 637 {
634 KeywordTestData keyword_match[] = { 638 KeywordTestData keyword_match[] = {
635 { base::ASCIIToUTF16("foo.com"), base::string16(), 639 { base::ASCIIToUTF16("foo.com"), base::string16(),
636 base::ASCIIToUTF16("foo.com") } 640 base::ASCIIToUTF16("foo.com") }
637 }; 641 };
638
Peter Kasting 2015/11/19 19:04:41 Nit: Don't remove this
Abhishek 2015/11/20 09:37:21 Done.
639 SCOPED_TRACE("keyword match as usual"); 642 SCOPED_TRACE("keyword match as usual");
640 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match, 643 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match,
641 arraysize(keyword_match)); 644 arraysize(keyword_match));
642 } 645 }
643 646
644 // The same result set with an input of "f" (versus "fo") should get 647 // The same result set with an input of "f" (versus "fo") should get
645 // a different associated keyword because "f" is an exact match for 648 // a different associated keyword because "f" is an exact match for
646 // a keyword and that should trump the keyword normally associated with 649 // a keyword and that should trump the keyword normally associated with
647 // this match. 650 // this match.
648 { 651 {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 EXPECT_FALSE(search_provider_field_trial_triggered_in_session()); 750 EXPECT_FALSE(search_provider_field_trial_triggered_in_session());
748 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); 751 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456));
749 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); 752 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path());
750 753
751 // Test page classification and field trial triggered set. 754 // Test page classification and field trial triggered set.
752 set_search_provider_field_trial_triggered_in_session(true); 755 set_search_provider_field_trial_triggered_in_session(true);
753 EXPECT_TRUE(search_provider_field_trial_triggered_in_session()); 756 EXPECT_TRUE(search_provider_field_trial_triggered_in_session());
754 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); 757 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456));
755 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); 758 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path());
756 } 759 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698