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