OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "chrome/browser/search_engines/template_url_service_factory.h" | 27 #include "chrome/browser/search_engines/template_url_service_factory.h" |
28 #include "chrome/common/metrics/entropy_provider.h" | 28 #include "chrome/common/metrics/entropy_provider.h" |
29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
30 #include "chrome/test/base/testing_browser_process.h" | 30 #include "chrome/test/base/testing_browser_process.h" |
31 #include "chrome/test/base/testing_profile.h" | 31 #include "chrome/test/base/testing_profile.h" |
32 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
33 #include "net/url_request/test_url_fetcher_factory.h" | 33 #include "net/url_request/test_url_fetcher_factory.h" |
34 #include "net/url_request/url_request_status.h" | 34 #include "net/url_request/url_request_status.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
36 | 36 |
37 using content::BrowserThread; | 37 |
| 38 // SearchProviderTest --------------------------------------------------------- |
38 | 39 |
39 // The following environment is configured for these tests: | 40 // The following environment is configured for these tests: |
40 // . The TemplateURL default_t_url_ is set as the default provider. | 41 // . The TemplateURL default_t_url_ is set as the default provider. |
41 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This | 42 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This |
42 // TemplateURL has a valid suggest and search URL. | 43 // TemplateURL has a valid suggest and search URL. |
43 // . The URL created by using the search term term1_ with default_t_url_ is | 44 // . The URL created by using the search term term1_ with default_t_url_ is |
44 // added to history. | 45 // added to history. |
45 // . The URL created by using the search term keyword_term_ with keyword_t_url_ | 46 // . The URL created by using the search term keyword_term_ with keyword_t_url_ |
46 // is added to history. | 47 // is added to history. |
47 // . test_factory_ is set as the URLFetcherFactory. | 48 // . test_factory_ is set as the URLFetcherFactory. |
48 class SearchProviderTest : public testing::Test, | 49 class SearchProviderTest : public testing::Test, |
49 public AutocompleteProviderListener { | 50 public AutocompleteProviderListener { |
50 public: | 51 public: |
51 SearchProviderTest() | |
52 : default_t_url_(NULL), | |
53 term1_(ASCIIToUTF16("term1")), | |
54 keyword_t_url_(NULL), | |
55 keyword_term_(ASCIIToUTF16("keyword")), | |
56 ui_thread_(BrowserThread::UI, &message_loop_), | |
57 io_thread_(BrowserThread::IO), | |
58 quit_when_done_(false) { | |
59 io_thread_.Start(); | |
60 } | |
61 | |
62 static void SetUpTestCase(); | |
63 | |
64 static void TearDownTestCase(); | |
65 | |
66 // See description above class for what this registers. | |
67 virtual void SetUp(); | |
68 | |
69 virtual void TearDown(); | |
70 | |
71 struct ResultInfo { | 52 struct ResultInfo { |
72 ResultInfo() : result_type(AutocompleteMatchType::NUM_TYPES) { | 53 ResultInfo() : result_type(AutocompleteMatchType::NUM_TYPES) { |
73 } | 54 } |
74 ResultInfo(GURL gurl, | 55 ResultInfo(GURL gurl, |
75 AutocompleteMatch::Type result_type, | 56 AutocompleteMatch::Type result_type, |
76 string16 fill_into_edit) | 57 string16 fill_into_edit) |
77 : gurl(gurl), | 58 : gurl(gurl), |
78 result_type(result_type), | 59 result_type(result_type), |
79 fill_into_edit(fill_into_edit) { | 60 fill_into_edit(fill_into_edit) { |
80 } | 61 } |
| 62 |
81 const GURL gurl; | 63 const GURL gurl; |
82 const AutocompleteMatch::Type result_type; | 64 const AutocompleteMatch::Type result_type; |
83 const string16 fill_into_edit; | 65 const string16 fill_into_edit; |
84 }; | 66 }; |
| 67 |
85 struct TestData { | 68 struct TestData { |
86 const string16 input; | 69 const string16 input; |
87 const size_t num_results; | 70 const size_t num_results; |
88 const ResultInfo output[3]; | 71 const ResultInfo output[3]; |
89 }; | 72 }; |
90 | 73 |
| 74 SearchProviderTest() |
| 75 : default_t_url_(NULL), |
| 76 term1_(ASCIIToUTF16("term1")), |
| 77 keyword_t_url_(NULL), |
| 78 keyword_term_(ASCIIToUTF16("keyword")), |
| 79 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 80 io_thread_(content::BrowserThread::IO), |
| 81 quit_when_done_(false) { |
| 82 io_thread_.Start(); |
| 83 } |
| 84 |
| 85 static void SetUpTestCase(); |
| 86 static void TearDownTestCase(); |
| 87 |
| 88 // See description above class for what this registers. |
| 89 virtual void SetUp() OVERRIDE; |
| 90 virtual void TearDown() OVERRIDE; |
| 91 |
91 void RunTest(TestData* cases, int num_cases, bool prefer_keyword); | 92 void RunTest(TestData* cases, int num_cases, bool prefer_keyword); |
92 | 93 |
93 protected: | 94 protected: |
94 // Needed for AutocompleteFieldTrial::ActivateStaticTrials(); | 95 // Needed for AutocompleteFieldTrial::ActivateStaticTrials(); |
95 static base::FieldTrialList* field_trial_list_; | 96 static base::FieldTrialList* field_trial_list_; |
96 | 97 |
97 // Default value used for testing. | 98 // Default value used for testing. |
98 static const std::string kNotApplicable; | 99 static const std::string kNotApplicable; |
99 | 100 |
100 // Adds a search for |term|, using the engine |t_url| to the history, and | 101 // Adds a search for |term|, using the engine |t_url| to the history, and |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 scoped_refptr<SearchProvider> provider_; | 159 scoped_refptr<SearchProvider> provider_; |
159 | 160 |
160 // If true, OnProviderUpdate exits out of the current message loop. | 161 // If true, OnProviderUpdate exits out of the current message loop. |
161 bool quit_when_done_; | 162 bool quit_when_done_; |
162 | 163 |
163 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); | 164 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); |
164 }; | 165 }; |
165 | 166 |
166 // static | 167 // static |
167 base::FieldTrialList* SearchProviderTest::field_trial_list_ = NULL; | 168 base::FieldTrialList* SearchProviderTest::field_trial_list_ = NULL; |
168 | |
169 // static | |
170 const std::string SearchProviderTest::kNotApplicable = "Not Applicable"; | 169 const std::string SearchProviderTest::kNotApplicable = "Not Applicable"; |
171 | 170 |
172 // static | 171 // static |
173 void SearchProviderTest::SetUpTestCase() { | 172 void SearchProviderTest::SetUpTestCase() { |
174 // Set up Suggest experiments. | 173 // Set up Suggest experiments. |
175 field_trial_list_ = new base::FieldTrialList( | 174 field_trial_list_ = new base::FieldTrialList( |
176 new metrics::SHA1EntropyProvider("foo")); | 175 new metrics::SHA1EntropyProvider("foo")); |
177 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( | 176 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
178 "AutocompleteDynamicTrial_0", "DefaultGroup"); | 177 "AutocompleteDynamicTrial_0", "DefaultGroup"); |
179 trial->group(); | 178 trial->group(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 228 |
230 // Keywords are updated by the InMemoryHistoryBackend only after the message | 229 // Keywords are updated by the InMemoryHistoryBackend only after the message |
231 // has been processed on the history thread. Block until history processes all | 230 // has been processed on the history thread. Block until history processes all |
232 // requests to ensure the InMemoryDatabase is the state we expect it. | 231 // requests to ensure the InMemoryDatabase is the state we expect it. |
233 profile_.BlockUntilHistoryProcessesPendingRequests(); | 232 profile_.BlockUntilHistoryProcessesPendingRequests(); |
234 | 233 |
235 provider_ = new SearchProvider(this, &profile_); | 234 provider_ = new SearchProvider(this, &profile_); |
236 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; | 235 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; |
237 } | 236 } |
238 | 237 |
| 238 void SearchProviderTest::TearDown() { |
| 239 message_loop_.RunUntilIdle(); |
| 240 |
| 241 // Shutdown the provider before the profile. |
| 242 provider_ = NULL; |
| 243 } |
| 244 |
| 245 void SearchProviderTest::RunTest(TestData* cases, |
| 246 int num_cases, |
| 247 bool prefer_keyword) { |
| 248 ACMatches matches; |
| 249 for (int i = 0; i < num_cases; ++i) { |
| 250 AutocompleteInput input(cases[i].input, string16::npos, string16(), GURL(), |
| 251 false, prefer_keyword, true, |
| 252 AutocompleteInput::ALL_MATCHES); |
| 253 provider_->Start(input, false); |
| 254 matches = provider_->matches(); |
| 255 string16 diagnostic_details = ASCIIToUTF16("Input was: ") + cases[i].input + |
| 256 ASCIIToUTF16("; prefer_keyword was: ") + |
| 257 (prefer_keyword ? ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 258 EXPECT_EQ(cases[i].num_results, matches.size()) << diagnostic_details; |
| 259 if (matches.size() == cases[i].num_results) { |
| 260 for (size_t j = 0; j < cases[i].num_results; ++j) { |
| 261 EXPECT_EQ(cases[i].output[j].gurl, matches[j].destination_url) << |
| 262 diagnostic_details; |
| 263 EXPECT_EQ(cases[i].output[j].result_type, matches[j].type) << |
| 264 diagnostic_details; |
| 265 EXPECT_EQ(cases[i].output[j].fill_into_edit, |
| 266 matches[j].fill_into_edit) << |
| 267 diagnostic_details; |
| 268 } |
| 269 } |
| 270 } |
| 271 } |
| 272 |
239 void SearchProviderTest::OnProviderUpdate(bool updated_matches) { | 273 void SearchProviderTest::OnProviderUpdate(bool updated_matches) { |
240 if (quit_when_done_ && provider_->done()) { | 274 if (quit_when_done_ && provider_->done()) { |
241 quit_when_done_ = false; | 275 quit_when_done_ = false; |
242 message_loop_.Quit(); | 276 message_loop_.Quit(); |
243 } | 277 } |
244 } | 278 } |
245 | 279 |
246 net::TestURLFetcher* SearchProviderTest::WaitUntilURLFetcherIsReady( | 280 net::TestURLFetcher* SearchProviderTest::WaitUntilURLFetcherIsReady( |
247 int fetcher_id) { | 281 int fetcher_id) { |
248 net::TestURLFetcher* url_fetcher = test_factory_.GetFetcherByID(fetcher_id); | 282 net::TestURLFetcher* url_fetcher = test_factory_.GetFetcherByID(fetcher_id); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 321 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
288 if (!wyt_match) | 322 if (!wyt_match) |
289 return; | 323 return; |
290 ASSERT_GE(provider_->matches().size(), 1u); | 324 ASSERT_GE(provider_->matches().size(), 1u); |
291 EXPECT_TRUE(FindMatchWithDestination(GURL( | 325 EXPECT_TRUE(FindMatchWithDestination(GURL( |
292 default_t_url_->url_ref().ReplaceSearchTerms( | 326 default_t_url_->url_ref().ReplaceSearchTerms( |
293 TemplateURLRef::SearchTermsArgs(text))), | 327 TemplateURLRef::SearchTermsArgs(text))), |
294 wyt_match)); | 328 wyt_match)); |
295 } | 329 } |
296 | 330 |
297 void SearchProviderTest::TearDown() { | |
298 message_loop_.RunUntilIdle(); | |
299 | |
300 // Shutdown the provider before the profile. | |
301 provider_ = NULL; | |
302 } | |
303 | |
304 void SearchProviderTest::RunTest(TestData* cases, | |
305 int num_cases, | |
306 bool prefer_keyword) { | |
307 ACMatches matches; | |
308 for (int i = 0; i < num_cases; ++i) { | |
309 AutocompleteInput input(cases[i].input, string16::npos, string16(), GURL(), | |
310 false, prefer_keyword, true, | |
311 AutocompleteInput::ALL_MATCHES); | |
312 provider_->Start(input, false); | |
313 matches = provider_->matches(); | |
314 string16 diagnostic_details = ASCIIToUTF16("Input was: ") + cases[i].input + | |
315 ASCIIToUTF16("; prefer_keyword was: ") + | |
316 (prefer_keyword ? ASCIIToUTF16("true") : ASCIIToUTF16("false")); | |
317 EXPECT_EQ(cases[i].num_results, matches.size()) << diagnostic_details; | |
318 if (matches.size() == cases[i].num_results) { | |
319 for (size_t j = 0; j < cases[i].num_results; ++j) { | |
320 EXPECT_EQ(cases[i].output[j].gurl, matches[j].destination_url) << | |
321 diagnostic_details; | |
322 EXPECT_EQ(cases[i].output[j].result_type, matches[j].type) << | |
323 diagnostic_details; | |
324 EXPECT_EQ(cases[i].output[j].fill_into_edit, | |
325 matches[j].fill_into_edit) << | |
326 diagnostic_details; | |
327 } | |
328 } | |
329 } | |
330 } | |
331 | |
332 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, | 331 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, |
333 string16 term, | 332 string16 term, |
334 int visit_count) { | 333 int visit_count) { |
335 HistoryService* history = | 334 HistoryService* history = |
336 HistoryServiceFactory::GetForProfile(&profile_, | 335 HistoryServiceFactory::GetForProfile(&profile_, |
337 Profile::EXPLICIT_ACCESS); | 336 Profile::EXPLICIT_ACCESS); |
338 GURL search(t_url->url_ref().ReplaceSearchTerms( | 337 GURL search(t_url->url_ref().ReplaceSearchTerms( |
339 TemplateURLRef::SearchTermsArgs(term))); | 338 TemplateURLRef::SearchTermsArgs(term))); |
340 static base::Time last_added_time; | 339 static base::Time last_added_time; |
341 last_added_time = std::max(base::Time::Now(), | 340 last_added_time = std::max(base::Time::Now(), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 void SearchProviderTest::FinishDefaultSuggestQuery() { | 372 void SearchProviderTest::FinishDefaultSuggestQuery() { |
374 net::TestURLFetcher* default_fetcher = WaitUntilURLFetcherIsReady( | 373 net::TestURLFetcher* default_fetcher = WaitUntilURLFetcherIsReady( |
375 SearchProvider::kDefaultProviderURLFetcherID); | 374 SearchProvider::kDefaultProviderURLFetcherID); |
376 ASSERT_TRUE(default_fetcher); | 375 ASSERT_TRUE(default_fetcher); |
377 | 376 |
378 // Tell the SearchProvider the default suggest query is done. | 377 // Tell the SearchProvider the default suggest query is done. |
379 default_fetcher->set_response_code(200); | 378 default_fetcher->set_response_code(200); |
380 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); | 379 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); |
381 } | 380 } |
382 | 381 |
383 // Tests ----------------------------------------------------------------------- | 382 |
| 383 // Actual Tests --------------------------------------------------------------- |
384 | 384 |
385 // Make sure we query history for the default provider and a URLFetcher is | 385 // Make sure we query history for the default provider and a URLFetcher is |
386 // created for the default provider suggest results. | 386 // created for the default provider suggest results. |
387 TEST_F(SearchProviderTest, QueryDefaultProvider) { | 387 TEST_F(SearchProviderTest, QueryDefaultProvider) { |
388 string16 term = term1_.substr(0, term1_.length() - 1); | 388 string16 term = term1_.substr(0, term1_.length() - 1); |
389 QueryForInput(term, false, false); | 389 QueryForInput(term, false, false); |
390 | 390 |
391 // Make sure the default providers suggest service was queried. | 391 // Make sure the default providers suggest service was queried. |
392 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 392 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
393 SearchProvider::kDefaultProviderURLFetcherID); | 393 SearchProvider::kDefaultProviderURLFetcherID); |
(...skipping 1689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2083 } else { | 2083 } else { |
2084 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion; | 2084 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion; |
2085 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion()); | 2085 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion()); |
2086 ++sug_it; | 2086 ++sug_it; |
2087 } | 2087 } |
2088 } | 2088 } |
2089 EXPECT_EQ(sug_end, sug_it); | 2089 EXPECT_EQ(sug_end, sug_it); |
2090 EXPECT_EQ(nav_end, nav_it); | 2090 EXPECT_EQ(nav_end, nav_it); |
2091 } | 2091 } |
2092 } | 2092 } |
OLD | NEW |