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

Side by Side Diff: chrome/browser/autocomplete/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: Remove more dependency 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 | « no previous file | 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/logging.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.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" 18 #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" 19 #include "components/metrics/proto/omnibox_event.pb.h"
24 #include "components/omnibox/browser/autocomplete_controller.h" 20 #include "components/omnibox/browser/autocomplete_controller.h"
25 #include "components/omnibox/browser/autocomplete_input.h" 21 #include "components/omnibox/browser/autocomplete_input.h"
26 #include "components/omnibox/browser/autocomplete_match.h" 22 #include "components/omnibox/browser/autocomplete_match.h"
27 #include "components/omnibox/browser/autocomplete_provider_listener.h" 23 #include "components/omnibox/browser/autocomplete_provider_listener.h"
28 #include "components/omnibox/browser/keyword_provider.h" 24 #include "components/omnibox/browser/keyword_provider.h"
25 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
29 #include "components/omnibox/browser/search_provider.h" 26 #include "components/omnibox/browser/search_provider.h"
30 #include "components/search_engines/search_engines_switches.h" 27 #include "components/search_engines/search_engines_switches.h"
31 #include "components/search_engines/template_url.h" 28 #include "components/search_engines/template_url.h"
32 #include "components/search_engines/template_url_service.h" 29 #include "components/search_engines/template_url_service.h"
30 #include "components/search_engines/template_url_service_client.h"
33 #include "content/public/browser/notification_observer.h" 31 #include "content/public/browser/notification_observer.h"
34 #include "content/public/browser/notification_registrar.h" 32 #include "content/public/browser/notification_registrar.h"
35 #include "content/public/browser/notification_source.h" 33 #include "content/public/browser/notification_source.h"
36 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
37 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
38 36
37 using testing::NiceMock;
38
39 static std::ostream& operator<<(std::ostream& os, 39 static std::ostream& operator<<(std::ostream& os,
40 const AutocompleteResult::const_iterator& it) { 40 const AutocompleteResult::const_iterator& it) {
41 return os << static_cast<const AutocompleteMatch*>(&(*it)); 41 return os << static_cast<const AutocompleteMatch*>(&(*it));
42 } 42 }
43 43
44 namespace { 44 namespace {
45 const size_t kResultsPerProvider = 3; 45 const size_t kResultsPerProvider = 3;
46 const char kTestTemplateURLKeyword[] = "t"; 46 const char kTestTemplateURLKeyword[] = "t";
47 } 47
48 class TestingSchemeClassifier : public AutocompleteSchemeClassifier {
49 public:
50 metrics::OmniboxInputType::Type GetInputTypeForScheme(
51 const std::string& scheme) const override {
52 if (net::URLRequest::IsHandledProtocol(scheme))
53 return metrics::OmniboxInputType::URL;
54 return metrics::OmniboxInputType::INVALID;
55 }
56 };
57
58 } // namespace
48 59
49 // Autocomplete provider that provides known results. Note that this is 60 // Autocomplete provider that provides known results. Note that this is
50 // refcounted so that it can also be a task on the message loop. 61 // refcounted so that it can also be a task on the message loop.
51 class TestProvider : public AutocompleteProvider { 62 class TestProvider : public AutocompleteProvider {
52 public: 63 public:
53 TestProvider(int relevance, const base::string16& prefix, 64 TestProvider(int relevance, const base::string16& prefix,
54 Profile* profile, 65 const base::string16 match_keyword,
55 const base::string16 match_keyword) 66 AutocompleteProviderClient* client)
56 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH), 67 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH),
57 listener_(NULL), 68 listener_(NULL),
58 profile_(profile),
59 relevance_(relevance), 69 relevance_(relevance),
60 prefix_(prefix), 70 prefix_(prefix),
61 match_keyword_(match_keyword) { 71 match_keyword_(match_keyword),
72 client_(client) {
62 } 73 }
63 74
64 void Start(const AutocompleteInput& input, bool minimal_changes) override; 75 void Start(const AutocompleteInput& input, bool minimal_changes) override;
65 76
66 void set_listener(AutocompleteProviderListener* listener) { 77 void set_listener(AutocompleteProviderListener* listener) {
67 listener_ = listener; 78 listener_ = listener;
68 } 79 }
69 80
70 private: 81 private:
71 ~TestProvider() override {} 82 ~TestProvider() override {}
72 83
73 void Run(); 84 void Run();
74 85
75 void AddResults(int start_at, int num); 86 void AddResults(int start_at, int num);
76 void AddResultsWithSearchTermsArgs( 87 void AddResultsWithSearchTermsArgs(
77 int start_at, 88 int start_at,
78 int num, 89 int num,
79 AutocompleteMatch::Type type, 90 AutocompleteMatch::Type type,
80 const TemplateURLRef::SearchTermsArgs& search_terms_args); 91 const TemplateURLRef::SearchTermsArgs& search_terms_args);
81 92
82 AutocompleteProviderListener* listener_; 93 AutocompleteProviderListener* listener_;
83 Profile* profile_;
84 int relevance_; 94 int relevance_;
85 const base::string16 prefix_; 95 const base::string16 prefix_;
86 const base::string16 match_keyword_; 96 const base::string16 match_keyword_;
97 AutocompleteProviderClient* client_;
87 }; 98 };
88 99
89 void TestProvider::Start(const AutocompleteInput& input, bool minimal_changes) { 100 void TestProvider::Start(const AutocompleteInput& input, bool minimal_changes) {
90 if (minimal_changes) 101 if (minimal_changes)
91 return; 102 return;
92 103
93 matches_.clear(); 104 matches_.clear();
94 105
95 if (input.from_omnibox_focus()) 106 if (input.from_omnibox_focus())
96 return; 107 return;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 match.contents = match.fill_into_edit; 156 match.contents = match.fill_into_edit;
146 match.contents_class.push_back( 157 match.contents_class.push_back(
147 ACMatchClassification(0, ACMatchClassification::NONE)); 158 ACMatchClassification(0, ACMatchClassification::NONE));
148 match.description = match.fill_into_edit; 159 match.description = match.fill_into_edit;
149 match.description_class.push_back( 160 match.description_class.push_back(
150 ACMatchClassification(0, ACMatchClassification::NONE)); 161 ACMatchClassification(0, ACMatchClassification::NONE));
151 match.search_terms_args.reset( 162 match.search_terms_args.reset(
152 new TemplateURLRef::SearchTermsArgs(search_terms_args)); 163 new TemplateURLRef::SearchTermsArgs(search_terms_args));
153 if (!match_keyword_.empty()) { 164 if (!match_keyword_.empty()) {
154 match.keyword = match_keyword_; 165 match.keyword = match_keyword_;
155 TemplateURLService* service = 166 TemplateURLService* service = client_->GetTemplateURLService();
156 TemplateURLServiceFactory::GetForProfile(profile_);
157 ASSERT_TRUE(match.GetTemplateURL(service, false) != NULL); 167 ASSERT_TRUE(match.GetTemplateURL(service, false) != NULL);
158 } 168 }
159 169
160 matches_.push_back(match); 170 matches_.push_back(match);
161 } 171 }
162 } 172 }
163 173
164 class AutocompleteProviderTest : public testing::Test, 174 class AutocompleteProviderTest : public testing::Test,
165 public content::NotificationObserver { 175 public content::NotificationObserver {
176 public:
177 ~AutocompleteProviderTest() override {}
178
166 protected: 179 protected:
167 struct KeywordTestData { 180 struct KeywordTestData {
168 const base::string16 fill_into_edit; 181 const base::string16 fill_into_edit;
169 const base::string16 keyword; 182 const base::string16 keyword;
170 const base::string16 expected_associated_keyword; 183 const base::string16 expected_associated_keyword;
171 }; 184 };
172 185
173 struct AssistedQueryStatsTestData { 186 struct AssistedQueryStatsTestData {
174 const AutocompleteMatch::Type match_type; 187 const AutocompleteMatch::Type match_type;
175 const std::string expected_aqs; 188 const std::string expected_aqs;
176 }; 189 };
177 190
178 protected: 191 protected:
179 // Registers a test TemplateURL under the given keyword. 192 // Registers a test TemplateURL under the given keyword.
180 void RegisterTemplateURL(const base::string16 keyword, 193 void RegisterTemplateURL(const base::string16 keyword,
181 const std::string& template_url); 194 const std::string& template_url);
182 195
183 // Resets |controller_| with two TestProviders. |provider1_ptr| and 196 // Resets |controller_| with two TestProviders. |provider1_ptr| and
184 // |provider2_ptr| are updated to point to the new providers if non-NULL. 197 // |provider2_ptr| are updated to point to the new providers if non-NULL.
185 void ResetControllerWithTestProviders(bool same_destinations, 198 void ResetControllerWithTestProviders(bool same_destinations,
186 TestProvider** provider1_ptr, 199 TestProvider** provider1_ptr,
187 TestProvider** provider2_ptr); 200 TestProvider** provider2_ptr);
188 201
202 void SetUp() override;
203
189 // Runs a query on the input "a", and makes sure both providers' input is 204 // Runs a query on the input "a", and makes sure both providers' input is
190 // properly collected. 205 // properly collected.
191 void RunTest(); 206 void RunTest();
192 207
193 // Constructs an AutocompleteResult from |match_data|, sets the |controller_| 208 // Constructs an AutocompleteResult from |match_data|, sets the |controller_|
194 // to pretend it was running against input |input|, calls the |controller_|'s 209 // to pretend it was running against input |input|, calls the |controller_|'s
195 // UpdateAssociatedKeywords, and checks that the matches have associated 210 // UpdateAssociatedKeywords, and checks that the matches have associated
196 // keywords as expected. 211 // keywords as expected.
197 void RunKeywordTest(const base::string16& input, 212 void RunKeywordTest(const base::string16& input,
198 const KeywordTestData* match_data, 213 const KeywordTestData* match_data,
(...skipping 30 matching lines...) Expand all
229 AutocompleteResult result_; 244 AutocompleteResult result_;
230 245
231 private: 246 private:
232 // content::NotificationObserver: 247 // content::NotificationObserver:
233 void Observe(int type, 248 void Observe(int type,
234 const content::NotificationSource& source, 249 const content::NotificationSource& source,
235 const content::NotificationDetails& details) override; 250 const content::NotificationDetails& details) override;
236 251
237 content::TestBrowserThreadBundle thread_bundle_; 252 content::TestBrowserThreadBundle thread_bundle_;
238 content::NotificationRegistrar registrar_; 253 content::NotificationRegistrar registrar_;
239 TestingProfile profile_;
240 scoped_ptr<AutocompleteController> controller_; 254 scoped_ptr<AutocompleteController> controller_;
255 scoped_ptr<NiceMock<MockAutocompleteProviderClient>> client_;
241 }; 256 };
droger 2015/11/02 10:20:33 add blank line here
Abhishek 2015/11/03 13:34:33 Done.
257 void AutocompleteProviderTest::SetUp() {
droger 2015/11/02 10:20:33 Nit: in general, using the constructor is preferre
Abhishek 2015/11/03 13:34:33 Done.
258 scoped_ptr<TemplateURLService> template_url_service(
259 new TemplateURLService(nullptr, 0));
260 client_.reset(new NiceMock<MockAutocompleteProviderClient>());
261 client_->set_template_url_service(template_url_service.Pass());
262 }
242 263
243 void AutocompleteProviderTest::RegisterTemplateURL( 264 void AutocompleteProviderTest::RegisterTemplateURL(
244 const base::string16 keyword, 265 const base::string16 keyword,
245 const std::string& template_url) { 266 const std::string& template_url) {
246 if (TemplateURLServiceFactory::GetForProfile(&profile_) == NULL) {
247 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
248 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
249 }
250 TemplateURLData data; 267 TemplateURLData data;
251 data.SetURL(template_url); 268 data.SetURL(template_url);
252 data.SetShortName(keyword); 269 data.SetShortName(keyword);
253 data.SetKeyword(keyword); 270 data.SetKeyword(keyword);
254 TemplateURL* default_t_url = new TemplateURL(data); 271 TemplateURL* default_t_url = new TemplateURL(data);
255 TemplateURLService* turl_model = 272 TemplateURLService* turl_model = client_->GetTemplateURLService();
256 TemplateURLServiceFactory::GetForProfile(&profile_);
257 turl_model->Add(default_t_url); 273 turl_model->Add(default_t_url);
258 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); 274 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url);
259 turl_model->Load(); 275 turl_model->Load();
260 TemplateURLID default_provider_id = default_t_url->id(); 276 TemplateURLID default_provider_id = default_t_url->id();
261 ASSERT_NE(0, default_provider_id); 277 ASSERT_NE(0, default_provider_id);
262 } 278 }
263 279
264 void AutocompleteProviderTest::ResetControllerWithTestProviders( 280 void AutocompleteProviderTest::ResetControllerWithTestProviders(
265 bool same_destinations, 281 bool same_destinations,
266 TestProvider** provider1_ptr, 282 TestProvider** provider1_ptr,
(...skipping 10 matching lines...) Expand all
277 // (B > A). 293 // (B > A).
278 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword), 294 RegisterTemplateURL(base::ASCIIToUTF16(kTestTemplateURLKeyword),
279 "http://aqs/{searchTerms}/{google:assistedQueryStats}"); 295 "http://aqs/{searchTerms}/{google:assistedQueryStats}");
280 296
281 AutocompleteController::Providers providers; 297 AutocompleteController::Providers providers;
282 298
283 // Construct two new providers, with either the same or different prefixes. 299 // Construct two new providers, with either the same or different prefixes.
284 TestProvider* provider1 = new TestProvider( 300 TestProvider* provider1 = new TestProvider(
285 kResultsPerProvider, 301 kResultsPerProvider,
286 base::ASCIIToUTF16("http://a"), 302 base::ASCIIToUTF16("http://a"),
287 &profile_, 303 base::ASCIIToUTF16(kTestTemplateURLKeyword), client_.get());
288 base::ASCIIToUTF16(kTestTemplateURLKeyword));
289 providers.push_back(provider1); 304 providers.push_back(provider1);
290 305
291 TestProvider* provider2 = new TestProvider( 306 TestProvider* provider2 = new TestProvider(
292 kResultsPerProvider * 2, 307 kResultsPerProvider * 2,
293 same_destinations ? base::ASCIIToUTF16("http://a") 308 same_destinations ? base::ASCIIToUTF16("http://a")
294 : base::ASCIIToUTF16("http://b"), 309 : base::ASCIIToUTF16("http://b"),
295 &profile_, 310 base::string16(), client_.get());
296 base::string16());
297 providers.push_back(provider2); 311 providers.push_back(provider2);
298 312
299 // Reset the controller to contain our new providers. 313 // Reset the controller to contain our new providers.
300 controller_.reset(new AutocompleteController( 314 controller_.reset(new AutocompleteController(make_scoped_ptr(client_.get()),
301 315 NULL, 0));
302 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL,
303 0));
304 // We're going to swap the providers vector, but the old vector should be 316 // 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. 317 // empty so no elements need to be freed at this point.
306 EXPECT_TRUE(controller_->providers_.empty()); 318 EXPECT_TRUE(controller_->providers_.empty());
307 controller_->providers_.swap(providers); 319 controller_->providers_.swap(providers);
308 provider1->set_listener(controller_.get()); 320 provider1->set_listener(controller_.get());
309 provider2->set_listener(controller_.get()); 321 provider2->set_listener(controller_.get());
310 322
311 // The providers don't complete synchronously, so listen for "result updated" 323 // The providers don't complete synchronously, so listen for "result updated"
312 // notifications. 324 // notifications.
313 registrar_.Add(this, 325 registrar_.Add(this,
314 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY, 326 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
315 content::Source<AutocompleteController>(controller_.get())); 327 content::Source<AutocompleteController>(controller_.get()));
316 328
317 if (provider1_ptr) 329 if (provider1_ptr)
318 *provider1_ptr = provider1; 330 *provider1_ptr = provider1;
319 if (provider2_ptr) 331 if (provider2_ptr)
320 *provider2_ptr = provider2; 332 *provider2_ptr = provider2;
321 } 333 }
322 334
323 void AutocompleteProviderTest:: 335 void AutocompleteProviderTest::
324 ResetControllerWithKeywordAndSearchProviders() { 336 ResetControllerWithKeywordAndSearchProviders() {
325 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
326 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
327
328 // Reset the default TemplateURL. 337 // Reset the default TemplateURL.
329 TemplateURLData data; 338 TemplateURLData data;
330 data.SetShortName(base::ASCIIToUTF16("default")); 339 data.SetShortName(base::ASCIIToUTF16("default"));
331 data.SetKeyword(base::ASCIIToUTF16("default")); 340 data.SetKeyword(base::ASCIIToUTF16("default"));
332 data.SetURL("http://defaultturl/{searchTerms}"); 341 data.SetURL("http://defaultturl/{searchTerms}");
333 TemplateURL* default_t_url = new TemplateURL(data); 342 TemplateURL* default_t_url = new TemplateURL(data);
334 TemplateURLService* turl_model = 343 TemplateURLService* turl_model = client_->GetTemplateURLService();
335 TemplateURLServiceFactory::GetForProfile(&profile_);
336 turl_model->Add(default_t_url); 344 turl_model->Add(default_t_url);
337 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); 345 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url);
338 TemplateURLID default_provider_id = default_t_url->id(); 346 TemplateURLID default_provider_id = default_t_url->id();
339 ASSERT_NE(0, default_provider_id); 347 ASSERT_NE(0, default_provider_id);
340 348
341 // Create another TemplateURL for KeywordProvider. 349 // Create another TemplateURL for KeywordProvider.
342 TemplateURLData data2; 350 TemplateURLData data2;
343 data2.SetShortName(base::ASCIIToUTF16("k")); 351 data2.SetShortName(base::ASCIIToUTF16("k"));
344 data2.SetKeyword(base::ASCIIToUTF16("k")); 352 data2.SetKeyword(base::ASCIIToUTF16("k"));
345 data2.SetURL("http://keyword/{searchTerms}"); 353 data2.SetURL("http://keyword/{searchTerms}");
346 TemplateURL* keyword_t_url = new TemplateURL(data2); 354 TemplateURL* keyword_t_url = new TemplateURL(data2);
347 turl_model->Add(keyword_t_url); 355 turl_model->Add(keyword_t_url);
348 ASSERT_NE(0, keyword_t_url->id()); 356 ASSERT_NE(0, keyword_t_url->id());
349 357
350 controller_.reset(new AutocompleteController( 358 controller_.reset(new AutocompleteController(
351 359 make_scoped_ptr(client_.get()), NULL,
droger 2015/11/02 10:20:33 Here and everywhere you call make_scoped_ptr(clien
Abhishek 2015/11/03 13:34:33 Thanks for letting me know about it!
droger 2015/11/03 14:27:48 Just to make sure there is no misunderstanding: Yo
Abhishek 2015/11/04 11:57:11 Thanks! I've updated it to raw pointer.
352 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL,
353 AutocompleteProvider::TYPE_KEYWORD | AutocompleteProvider::TYPE_SEARCH)); 360 AutocompleteProvider::TYPE_KEYWORD | AutocompleteProvider::TYPE_SEARCH));
354 } 361 }
355 362
356 void AutocompleteProviderTest::ResetControllerWithKeywordProvider() { 363 void AutocompleteProviderTest::ResetControllerWithKeywordProvider() {
357 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 364 TemplateURLService* turl_model = client_->GetTemplateURLService();
358 &profile_, &TemplateURLServiceFactory::BuildInstanceFor);
359
360 TemplateURLService* turl_model =
361 TemplateURLServiceFactory::GetForProfile(&profile_);
362 365
363 // Create a TemplateURL for KeywordProvider. 366 // Create a TemplateURL for KeywordProvider.
364 TemplateURLData data; 367 TemplateURLData data;
365 data.SetShortName(base::ASCIIToUTF16("foo.com")); 368 data.SetShortName(base::ASCIIToUTF16("foo.com"));
366 data.SetKeyword(base::ASCIIToUTF16("foo.com")); 369 data.SetKeyword(base::ASCIIToUTF16("foo.com"));
367 data.SetURL("http://foo.com/{searchTerms}"); 370 data.SetURL("http://foo.com/{searchTerms}");
368 TemplateURL* keyword_t_url = new TemplateURL(data); 371 TemplateURL* keyword_t_url = new TemplateURL(data);
369 turl_model->Add(keyword_t_url); 372 turl_model->Add(keyword_t_url);
370 ASSERT_NE(0, keyword_t_url->id()); 373 ASSERT_NE(0, keyword_t_url->id());
371 374
372 // Make a TemplateURL for KeywordProvider that a shorter version of the 375 // Make a TemplateURL for KeywordProvider that a shorter version of the
373 // first. 376 // first.
374 data.SetShortName(base::ASCIIToUTF16("f")); 377 data.SetShortName(base::ASCIIToUTF16("f"));
375 data.SetKeyword(base::ASCIIToUTF16("f")); 378 data.SetKeyword(base::ASCIIToUTF16("f"));
376 data.SetURL("http://f.com/{searchTerms}"); 379 data.SetURL("http://f.com/{searchTerms}");
377 keyword_t_url = new TemplateURL(data); 380 keyword_t_url = new TemplateURL(data);
378 turl_model->Add(keyword_t_url); 381 turl_model->Add(keyword_t_url);
379 ASSERT_NE(0, keyword_t_url->id()); 382 ASSERT_NE(0, keyword_t_url->id());
380 383
381 // Create another TemplateURL for KeywordProvider. 384 // Create another TemplateURL for KeywordProvider.
382 data.SetShortName(base::ASCIIToUTF16("bar.com")); 385 data.SetShortName(base::ASCIIToUTF16("bar.com"));
383 data.SetKeyword(base::ASCIIToUTF16("bar.com")); 386 data.SetKeyword(base::ASCIIToUTF16("bar.com"));
384 data.SetURL("http://bar.com/{searchTerms}"); 387 data.SetURL("http://bar.com/{searchTerms}");
385 keyword_t_url = new TemplateURL(data); 388 keyword_t_url = new TemplateURL(data);
386 turl_model->Add(keyword_t_url); 389 turl_model->Add(keyword_t_url);
387 ASSERT_NE(0, keyword_t_url->id()); 390 ASSERT_NE(0, keyword_t_url->id());
388 391
389 controller_.reset(new AutocompleteController( 392 controller_.reset(new AutocompleteController(
390 make_scoped_ptr(new ChromeAutocompleteProviderClient(&profile_)), NULL, 393 make_scoped_ptr(client_.get()), NULL,
391 AutocompleteProvider::TYPE_KEYWORD)); 394 AutocompleteProvider::TYPE_KEYWORD));
392 } 395 }
393 396
394 void AutocompleteProviderTest::RunTest() { 397 void AutocompleteProviderTest::RunTest() {
395 RunQuery(base::ASCIIToUTF16("a")); 398 RunQuery(base::ASCIIToUTF16("a"));
396 } 399 }
397 400
398 void AutocompleteProviderTest::RunKeywordTest(const base::string16& input, 401 void AutocompleteProviderTest::RunKeywordTest(const base::string16& input,
399 const KeywordTestData* match_data, 402 const KeywordTestData* match_data,
400 size_t size) { 403 size_t size) {
401 ACMatches matches; 404 ACMatches matches;
402 for (size_t i = 0; i < size; ++i) { 405 for (size_t i = 0; i < size; ++i) {
403 AutocompleteMatch match; 406 AutocompleteMatch match;
404 match.relevance = 1000; // Arbitrary non-zero value. 407 match.relevance = 1000; // Arbitrary non-zero value.
405 match.allowed_to_be_default_match = true; 408 match.allowed_to_be_default_match = true;
406 match.fill_into_edit = match_data[i].fill_into_edit; 409 match.fill_into_edit = match_data[i].fill_into_edit;
407 match.transition = ui::PAGE_TRANSITION_KEYWORD; 410 match.transition = ui::PAGE_TRANSITION_KEYWORD;
408 match.keyword = match_data[i].keyword; 411 match.keyword = match_data[i].keyword;
409 matches.push_back(match); 412 matches.push_back(match);
410 } 413 }
411 414
412 controller_->input_ = AutocompleteInput( 415 controller_->input_ = AutocompleteInput(
413 input, base::string16::npos, std::string(), GURL(), 416 input, base::string16::npos, std::string(), GURL(),
414 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, 417 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
415 false, true, true, true, false, 418 false, true, true, true, false,
416 ChromeAutocompleteSchemeClassifier(&profile_)); 419 TestingSchemeClassifier());
417 AutocompleteResult result; 420 AutocompleteResult result;
418 result.AppendMatches(controller_->input_, matches); 421 result.AppendMatches(controller_->input_, matches);
419 controller_->UpdateAssociatedKeywords(&result); 422 controller_->UpdateAssociatedKeywords(&result);
420
421 for (size_t j = 0; j < result.size(); ++j) { 423 for (size_t j = 0; j < result.size(); ++j) {
422 EXPECT_EQ(match_data[j].expected_associated_keyword, 424 EXPECT_EQ(match_data[j].expected_associated_keyword,
423 result.match_at(j)->associated_keyword.get() ? 425 result.match_at(j)->associated_keyword.get() ?
424 result.match_at(j)->associated_keyword->keyword : 426 result.match_at(j)->associated_keyword->keyword :
425 base::string16()); 427 base::string16());
426 } 428 }
427 } 429 }
428 430
429 void AutocompleteProviderTest::RunAssistedQueryStatsTest( 431 void AutocompleteProviderTest::RunAssistedQueryStatsTest(
430 const AssistedQueryStatsTestData* aqs_test_data, 432 const AssistedQueryStatsTestData* aqs_test_data,
(...skipping 21 matching lines...) Expand all
452 EXPECT_EQ(aqs_test_data[i].expected_aqs, 454 EXPECT_EQ(aqs_test_data[i].expected_aqs,
453 result_.match_at(i)->search_terms_args->assisted_query_stats); 455 result_.match_at(i)->search_terms_args->assisted_query_stats);
454 } 456 }
455 } 457 }
456 458
457 void AutocompleteProviderTest::RunQuery(const base::string16 query) { 459 void AutocompleteProviderTest::RunQuery(const base::string16 query) {
458 result_.Reset(); 460 result_.Reset();
459 controller_->Start(AutocompleteInput( 461 controller_->Start(AutocompleteInput(
460 query, base::string16::npos, std::string(), GURL(), 462 query, base::string16::npos, std::string(), GURL(),
461 metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, true, false, 463 metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, true, false,
462 ChromeAutocompleteSchemeClassifier(&profile_))); 464 TestingSchemeClassifier()));
463 465
464 if (!controller_->done()) 466 if (!controller_->done())
465 // The message loop will terminate when all autocomplete input has been 467 // The message loop will terminate when all autocomplete input has been
466 // collected. 468 // collected.
467 base::MessageLoop::current()->Run(); 469 base::MessageLoop::current()->Run();
468 } 470 }
469 471
470 void AutocompleteProviderTest::RunExactKeymatchTest( 472 void AutocompleteProviderTest::RunExactKeymatchTest(
471 bool allow_exact_keyword_match) { 473 bool allow_exact_keyword_match) {
472 // Send the controller input which exactly matches the keyword provider we 474 // Send the controller input which exactly matches the keyword provider we
473 // created in ResetControllerWithKeywordAndSearchProviders(). The default 475 // created in ResetControllerWithKeywordAndSearchProviders(). The default
474 // match should thus be a search-other-engine match iff 476 // match should thus be a search-other-engine match iff
475 // |allow_exact_keyword_match| is true. Regardless, the match should 477 // |allow_exact_keyword_match| is true. Regardless, the match should
476 // be from SearchProvider. (It provides all verbatim search matches, 478 // be from SearchProvider. (It provides all verbatim search matches,
477 // keyword or not.) 479 // keyword or not.)
478 controller_->Start(AutocompleteInput( 480 controller_->Start(AutocompleteInput(
479 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(), 481 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(),
480 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 482 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
481 allow_exact_keyword_match, false, false, 483 allow_exact_keyword_match, false, false,
482 ChromeAutocompleteSchemeClassifier(&profile_))); 484 TestingSchemeClassifier()));
483 EXPECT_TRUE(controller_->done()); 485 EXPECT_TRUE(controller_->done());
484 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, 486 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH,
485 controller_->result().default_match()->provider->type()); 487 controller_->result().default_match()->provider->type());
486 EXPECT_EQ(allow_exact_keyword_match ? 488 EXPECT_EQ(allow_exact_keyword_match ?
487 AutocompleteMatchType::SEARCH_OTHER_ENGINE : 489 AutocompleteMatchType::SEARCH_OTHER_ENGINE :
488 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 490 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
489 controller_->result().default_match()->type); 491 controller_->result().default_match()->type);
490 } 492 }
491 493
492 void AutocompleteProviderTest::CopyResults() { 494 void AutocompleteProviderTest::CopyResults() {
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 // Test that exact match keywords trump keywords associated with 630 // Test that exact match keywords trump keywords associated with
629 // the match. 631 // the match.
630 TEST_F(AutocompleteProviderTest, ExactMatchKeywords) { 632 TEST_F(AutocompleteProviderTest, ExactMatchKeywords) {
631 ResetControllerWithKeywordProvider(); 633 ResetControllerWithKeywordProvider();
632 634
633 { 635 {
634 KeywordTestData keyword_match[] = { 636 KeywordTestData keyword_match[] = {
635 { base::ASCIIToUTF16("foo.com"), base::string16(), 637 { base::ASCIIToUTF16("foo.com"), base::string16(),
636 base::ASCIIToUTF16("foo.com") } 638 base::ASCIIToUTF16("foo.com") }
637 }; 639 };
638
639 SCOPED_TRACE("keyword match as usual"); 640 SCOPED_TRACE("keyword match as usual");
640 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match, 641 RunKeywordTest(base::ASCIIToUTF16("fo"), keyword_match,
641 arraysize(keyword_match)); 642 arraysize(keyword_match));
642 } 643 }
643 644
644 // The same result set with an input of "f" (versus "fo") should get 645 // 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 646 // a different associated keyword because "f" is an exact match for
646 // a keyword and that should trump the keyword normally associated with 647 // a keyword and that should trump the keyword normally associated with
647 // this match. 648 // this match.
648 { 649 {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 EXPECT_FALSE(search_provider_field_trial_triggered_in_session()); 748 EXPECT_FALSE(search_provider_field_trial_triggered_in_session());
748 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); 749 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456));
749 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); 750 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path());
750 751
751 // Test page classification and field trial triggered set. 752 // Test page classification and field trial triggered set.
752 set_search_provider_field_trial_triggered_in_session(true); 753 set_search_provider_field_trial_triggered_in_session(true);
753 EXPECT_TRUE(search_provider_field_trial_triggered_in_session()); 754 EXPECT_TRUE(search_provider_field_trial_triggered_in_session());
754 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); 755 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456));
755 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); 756 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path());
756 } 757 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698