OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/zero_suggest_provider.h" | 5 #include "components/omnibox/browser/zero_suggest_provider.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); | 235 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); |
236 base::FieldTrialList::CreateFieldTrial( | 236 base::FieldTrialList::CreateFieldTrial( |
237 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); | 237 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
238 } | 238 } |
239 | 239 |
240 TEST_F(ZeroSuggestProviderTest, TestDoesNotReturnMatchesForPrefix) { | 240 TEST_F(ZeroSuggestProviderTest, TestDoesNotReturnMatchesForPrefix) { |
241 CreatePersonalizedFieldTrial(); | 241 CreatePersonalizedFieldTrial(); |
242 | 242 |
243 std::string url("http://www.cnn.com/"); | 243 std::string url("http://www.cnn.com/"); |
244 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, | 244 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
245 std::string(), GURL(url), | 245 std::string(), GURL(url), base::string16(), |
246 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 246 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
247 true, true, false, TestSchemeClassifier()); | 247 true, true, false, TestSchemeClassifier()); |
248 | 248 |
249 // Set up the pref to cache the response from the previous run. | 249 // Set up the pref to cache the response from the previous run. |
250 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 250 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
251 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 251 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
252 "\"google:verbatimrelevance\":1300}]"); | 252 "\"google:verbatimrelevance\":1300}]"); |
253 PrefService* prefs = client_->GetPrefs(); | 253 PrefService* prefs = client_->GetPrefs(); |
254 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); | 254 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); |
255 | 255 |
256 provider_->Start(input, false); | 256 provider_->Start(input, false); |
257 | 257 |
258 // Expect that matches don't get populated out of cache because we are not | 258 // Expect that matches don't get populated out of cache because we are not |
259 // in zero suggest mode. | 259 // in zero suggest mode. |
260 EXPECT_TRUE(provider_->matches().empty()); | 260 EXPECT_TRUE(provider_->matches().empty()); |
261 | 261 |
262 // Expect that fetcher did not get created. | 262 // Expect that fetcher did not get created. |
263 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); | 263 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); |
264 EXPECT_FALSE(fetcher); | 264 EXPECT_FALSE(fetcher); |
265 } | 265 } |
266 | 266 |
267 TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) { | 267 TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) { |
268 CreateMostVisitedFieldTrial(); | 268 CreateMostVisitedFieldTrial(); |
269 | 269 |
270 std::string current_url("http://www.foxnews.com/"); | 270 std::string current_url("http://www.foxnews.com/"); |
271 std::string input_url("http://www.cnn.com/"); | 271 std::string input_url("http://www.cnn.com/"); |
272 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, | 272 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, |
273 std::string(), GURL(current_url), | 273 std::string(), GURL(current_url), base::string16(), |
274 metrics::OmniboxEventProto::OTHER, false, false, true, | 274 metrics::OmniboxEventProto::OTHER, false, false, true, |
275 true, true, TestSchemeClassifier()); | 275 true, true, TestSchemeClassifier()); |
276 history::MostVisitedURLList urls; | 276 history::MostVisitedURLList urls; |
277 history::MostVisitedURL url(GURL("http://foo.com/"), | 277 history::MostVisitedURL url(GURL("http://foo.com/"), |
278 base::ASCIIToUTF16("Foo")); | 278 base::ASCIIToUTF16("Foo")); |
279 urls.push_back(url); | 279 urls.push_back(url); |
280 | 280 |
281 provider_->Start(input, false); | 281 provider_->Start(input, false); |
282 EXPECT_TRUE(provider_->matches().empty()); | 282 EXPECT_TRUE(provider_->matches().empty()); |
283 scoped_refptr<history::TopSites> top_sites = client_->GetTopSites(); | 283 scoped_refptr<history::TopSites> top_sites = client_->GetTopSites(); |
(...skipping 10 matching lines...) Expand all Loading... |
294 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); | 294 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
295 EXPECT_TRUE(provider_->matches().empty()); | 295 EXPECT_TRUE(provider_->matches().empty()); |
296 } | 296 } |
297 | 297 |
298 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { | 298 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { |
299 CreateMostVisitedFieldTrial(); | 299 CreateMostVisitedFieldTrial(); |
300 | 300 |
301 std::string current_url("http://www.foxnews.com/"); | 301 std::string current_url("http://www.foxnews.com/"); |
302 std::string input_url("http://www.cnn.com/"); | 302 std::string input_url("http://www.cnn.com/"); |
303 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, | 303 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, |
304 std::string(), GURL(current_url), | 304 std::string(), GURL(current_url), base::string16(), |
305 metrics::OmniboxEventProto::OTHER, false, false, true, | 305 metrics::OmniboxEventProto::OTHER, false, false, true, |
306 true, true, TestSchemeClassifier()); | 306 true, true, TestSchemeClassifier()); |
307 history::MostVisitedURLList urls; | 307 history::MostVisitedURLList urls; |
308 history::MostVisitedURL url(GURL("http://foo.com/"), | 308 history::MostVisitedURL url(GURL("http://foo.com/"), |
309 base::ASCIIToUTF16("Foo")); | 309 base::ASCIIToUTF16("Foo")); |
310 urls.push_back(url); | 310 urls.push_back(url); |
311 | 311 |
312 provider_->Start(input, false); | 312 provider_->Start(input, false); |
313 EXPECT_TRUE(provider_->matches().empty()); | 313 EXPECT_TRUE(provider_->matches().empty()); |
314 // Stop() doesn't always get called. | 314 // Stop() doesn't always get called. |
315 | 315 |
316 std::string search_url("https://www.google.com/?q=flowers"); | 316 std::string search_url("https://www.google.com/?q=flowers"); |
317 AutocompleteInput srp_input( | 317 AutocompleteInput srp_input( |
318 base::ASCIIToUTF16(search_url), base::string16::npos, std::string(), | 318 base::ASCIIToUTF16(search_url), base::string16::npos, std::string(), |
319 GURL(search_url), | 319 GURL(search_url), base::string16(), |
320 metrics::OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT, | 320 metrics::OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT, |
321 false, false, true, true, true, TestSchemeClassifier()); | 321 false, false, true, true, true, TestSchemeClassifier()); |
322 | 322 |
323 provider_->Start(srp_input, false); | 323 provider_->Start(srp_input, false); |
324 EXPECT_TRUE(provider_->matches().empty()); | 324 EXPECT_TRUE(provider_->matches().empty()); |
325 // Most visited results arriving after a new request has been started. | 325 // Most visited results arriving after a new request has been started. |
326 scoped_refptr<history::TopSites> top_sites = client_->GetTopSites(); | 326 scoped_refptr<history::TopSites> top_sites = client_->GetTopSites(); |
327 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); | 327 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
328 EXPECT_TRUE(provider_->matches().empty()); | 328 EXPECT_TRUE(provider_->matches().empty()); |
329 } | 329 } |
330 | 330 |
331 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { | 331 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { |
332 CreatePersonalizedFieldTrial(); | 332 CreatePersonalizedFieldTrial(); |
333 | 333 |
334 // Ensure the cache is empty. | 334 // Ensure the cache is empty. |
335 PrefService* prefs = client_->GetPrefs(); | 335 PrefService* prefs = client_->GetPrefs(); |
336 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string()); | 336 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string()); |
337 | 337 |
338 std::string url("http://www.cnn.com/"); | 338 std::string url("http://www.cnn.com/"); |
339 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, | 339 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
340 std::string(), GURL(url), | 340 std::string(), GURL(url), base::string16(), |
341 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 341 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
342 true, true, true, TestSchemeClassifier()); | 342 true, true, true, TestSchemeClassifier()); |
343 | 343 |
344 provider_->Start(input, false); | 344 provider_->Start(input, false); |
345 | 345 |
346 EXPECT_TRUE(prefs->GetString(omnibox::kZeroSuggestCachedResults).empty()); | 346 EXPECT_TRUE(prefs->GetString(omnibox::kZeroSuggestCachedResults).empty()); |
347 EXPECT_TRUE(provider_->matches().empty()); | 347 EXPECT_TRUE(provider_->matches().empty()); |
348 | 348 |
349 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); | 349 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); |
350 ASSERT_TRUE(fetcher); | 350 ASSERT_TRUE(fetcher); |
351 fetcher->set_response_code(200); | 351 fetcher->set_response_code(200); |
352 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 352 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
353 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 353 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
354 "\"google:verbatimrelevance\":1300}]"); | 354 "\"google:verbatimrelevance\":1300}]"); |
355 fetcher->SetResponseString(json_response); | 355 fetcher->SetResponseString(json_response); |
356 fetcher->delegate()->OnURLFetchComplete(fetcher); | 356 fetcher->delegate()->OnURLFetchComplete(fetcher); |
357 | 357 |
358 base::RunLoop().RunUntilIdle(); | 358 base::RunLoop().RunUntilIdle(); |
359 | 359 |
360 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim | 360 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim |
361 EXPECT_EQ(json_response, | 361 EXPECT_EQ(json_response, |
362 prefs->GetString(omnibox::kZeroSuggestCachedResults)); | 362 prefs->GetString(omnibox::kZeroSuggestCachedResults)); |
363 } | 363 } |
364 | 364 |
365 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { | 365 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { |
366 CreatePersonalizedFieldTrial(); | 366 CreatePersonalizedFieldTrial(); |
367 | 367 |
368 std::string url("http://www.cnn.com/"); | 368 std::string url("http://www.cnn.com/"); |
369 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, | 369 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
370 std::string(), GURL(url), | 370 std::string(), GURL(url), base::string16(), |
371 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 371 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
372 true, true, true, TestSchemeClassifier()); | 372 true, true, true, TestSchemeClassifier()); |
373 | 373 |
374 // Set up the pref to cache the response from the previous run. | 374 // Set up the pref to cache the response from the previous run. |
375 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 375 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
376 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 376 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
377 "\"google:verbatimrelevance\":1300}]"); | 377 "\"google:verbatimrelevance\":1300}]"); |
378 PrefService* prefs = client_->GetPrefs(); | 378 PrefService* prefs = client_->GetPrefs(); |
379 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); | 379 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); |
380 | 380 |
(...skipping 25 matching lines...) Expand all Loading... |
406 // Expect the new results have been stored. | 406 // Expect the new results have been stored. |
407 EXPECT_EQ(json_response2, | 407 EXPECT_EQ(json_response2, |
408 prefs->GetString(omnibox::kZeroSuggestCachedResults)); | 408 prefs->GetString(omnibox::kZeroSuggestCachedResults)); |
409 } | 409 } |
410 | 410 |
411 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { | 411 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { |
412 CreatePersonalizedFieldTrial(); | 412 CreatePersonalizedFieldTrial(); |
413 | 413 |
414 std::string url("http://www.cnn.com/"); | 414 std::string url("http://www.cnn.com/"); |
415 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, | 415 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
416 std::string(), GURL(url), | 416 std::string(), GURL(url), base::string16(), |
417 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 417 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
418 true, true, true, TestSchemeClassifier()); | 418 true, true, true, TestSchemeClassifier()); |
419 | 419 |
420 // Set up the pref to cache the response from the previous run. | 420 // Set up the pref to cache the response from the previous run. |
421 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 421 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
422 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 422 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
423 "\"google:verbatimrelevance\":1300}]"); | 423 "\"google:verbatimrelevance\":1300}]"); |
424 PrefService* prefs = client_->GetPrefs(); | 424 PrefService* prefs = client_->GetPrefs(); |
425 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); | 425 prefs->SetString(omnibox::kZeroSuggestCachedResults, json_response); |
426 | 426 |
(...skipping 14 matching lines...) Expand all Loading... |
441 | 441 |
442 base::RunLoop().RunUntilIdle(); | 442 base::RunLoop().RunUntilIdle(); |
443 | 443 |
444 // Expect that the matches have been cleared. | 444 // Expect that the matches have been cleared. |
445 ASSERT_TRUE(provider_->matches().empty()); | 445 ASSERT_TRUE(provider_->matches().empty()); |
446 | 446 |
447 // Expect the new results have been stored. | 447 // Expect the new results have been stored. |
448 EXPECT_EQ(empty_response, | 448 EXPECT_EQ(empty_response, |
449 prefs->GetString(omnibox::kZeroSuggestCachedResults)); | 449 prefs->GetString(omnibox::kZeroSuggestCachedResults)); |
450 } | 450 } |
OLD | NEW |