OLD | NEW |
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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/metrics/histogram.h" | 6 #include "base/metrics/histogram.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/protector/histograms.h" | 9 #include "chrome/browser/protector/histograms.h" |
10 #include "chrome/browser/protector/mock_protector_service.h" | 10 #include "chrome/browser/protector/mock_protector_service.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 url->set_keyword(keyword); | 72 url->set_keyword(keyword); |
73 url->SetURL(search_url); | 73 url->SetURL(search_url); |
74 return url; | 74 return url; |
75 } | 75 } |
76 | 76 |
77 const TemplateURL* FindTemplateURL(const std::string& search_url) { | 77 const TemplateURL* FindTemplateURL(const std::string& search_url) { |
78 TemplateURLService::TemplateURLVector urls = | 78 TemplateURLService::TemplateURLVector urls = |
79 turl_service_->GetTemplateURLs(); | 79 turl_service_->GetTemplateURLs(); |
80 for (TemplateURLService::TemplateURLVector::const_iterator | 80 for (TemplateURLService::TemplateURLVector::const_iterator |
81 it = urls.begin(); it != urls.end(); ++it) { | 81 it = urls.begin(); it != urls.end(); ++it) { |
82 if ((*it)->url() == search_url) | 82 if ((*it)->url()->url() == search_url) |
83 return *it; | 83 return *it; |
84 } | 84 } |
85 return NULL; | 85 return NULL; |
86 } | 86 } |
87 | 87 |
88 // Adds a copy of |turl| that will be owned by TemplateURLService. | 88 // Adds a copy of |turl| that will be owned by TemplateURLService. |
89 void AddCopy(TemplateURL* turl) { | 89 void AddCopy(TemplateURL* turl) { |
90 TemplateURL* turl_copy = new TemplateURL(*turl); | 90 TemplateURL* turl_copy = new TemplateURL(*turl); |
91 turl_service_->Add(turl_copy); | 91 turl_service_->Add(turl_copy); |
92 } | 92 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 int prepopulated_histogram_id = | 250 int prepopulated_histogram_id = |
251 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | 251 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); |
252 TemplateURL* current_url = | 252 TemplateURL* current_url = |
253 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | 253 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); |
254 int current_histogram_id = | 254 int current_histogram_id = |
255 protector::GetSearchProviderHistogramID(current_url); | 255 protector::GetSearchProviderHistogramID(current_url); |
256 | 256 |
257 AddAndSetDefault(current_url); | 257 AddAndSetDefault(current_url); |
258 | 258 |
259 // Prepopulated default search must exist. | 259 // Prepopulated default search must exist. |
260 ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url())); | 260 ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url()->url())); |
261 | 261 |
262 scoped_ptr<BaseSettingChange> change( | 262 scoped_ptr<BaseSettingChange> change( |
263 CreateDefaultSearchProviderChange(current_url, NULL)); | 263 CreateDefaultSearchProviderChange(current_url, NULL)); |
264 ASSERT_TRUE(change.get()); | 264 ASSERT_TRUE(change.get()); |
265 ASSERT_TRUE(change->Init(browser()->profile())); | 265 ASSERT_TRUE(change->Init(browser()->profile())); |
266 | 266 |
267 // Verify that the prepopulated default search is active. | 267 // Verify that the prepopulated default search is active. |
268 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 268 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
269 turl_service_->GetDefaultSearchProvider()); | 269 turl_service_->GetDefaultSearchProvider()); |
270 | 270 |
271 // Verify histograms. | 271 // Verify histograms. |
272 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | 272 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, |
273 current_histogram_id, 1); | 273 current_histogram_id, 1); |
274 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 274 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
275 prepopulated_histogram_id, 1); | 275 prepopulated_histogram_id, 1); |
276 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | 276 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, |
277 prepopulated_histogram_id, 1); | 277 prepopulated_histogram_id, 1); |
278 | 278 |
279 // Verify text messages. | 279 // Verify text messages. |
280 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 280 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
281 change->GetBubbleMessage()); | 281 change->GetBubbleMessage()); |
282 EXPECT_EQ(GetChangeSearchButtonText(example_com), | 282 EXPECT_EQ(GetChangeSearchButtonText(example_com), |
283 change->GetApplyButtonText()); | 283 change->GetApplyButtonText()); |
284 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 284 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
285 | 285 |
286 // Verify that search engine settings are opened by Discard. | 286 // Verify that search engine settings are opened by Discard. |
287 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 287 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
288 change->Discard(browser()); | 288 change->Discard(browser()); |
289 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 289 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
290 turl_service_->GetDefaultSearchProvider()); | 290 turl_service_->GetDefaultSearchProvider()); |
291 | 291 |
292 // Verify that Apply switches back to |current_url|. | 292 // Verify that Apply switches back to |current_url|. |
293 change->Apply(browser()); | 293 change->Apply(browser()); |
294 EXPECT_EQ(FindTemplateURL(http_example_com), | 294 EXPECT_EQ(FindTemplateURL(http_example_com), |
295 turl_service_->GetDefaultSearchProvider()); | 295 turl_service_->GetDefaultSearchProvider()); |
296 } | 296 } |
297 | 297 |
298 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | 298 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, |
299 BackupInvalidFallbackRemoved) { | 299 BackupInvalidFallbackRemoved) { |
300 // Backup is invalid, current search provider exists, fallback to the | 300 // Backup is invalid, current search provider exists, fallback to the |
301 // prepopulated default search, which was removed from keywords (will be | 301 // prepopulated default search, which was removed from keywords (will be |
302 // added). | 302 // added). |
303 int prepopulated_histogram_id = | 303 int prepopulated_histogram_id = |
304 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | 304 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); |
305 TemplateURL* current_url = | 305 TemplateURL* current_url = |
306 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | 306 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); |
307 int current_histogram_id = | 307 int current_histogram_id = |
308 protector::GetSearchProviderHistogramID(current_url); | 308 protector::GetSearchProviderHistogramID(current_url); |
309 | 309 |
310 AddAndSetDefault(current_url); | 310 AddAndSetDefault(current_url); |
311 | 311 |
312 const TemplateURL* prepopulated_default = | 312 const TemplateURL* prepopulated_default = |
313 FindTemplateURL(prepopulated_url_->url()); | 313 FindTemplateURL(prepopulated_url_->url()->url()); |
314 // Prepopulated default search must exist, remove it. | 314 // Prepopulated default search must exist, remove it. |
315 ASSERT_TRUE(prepopulated_default); | 315 ASSERT_TRUE(prepopulated_default); |
316 turl_service_->Remove(prepopulated_default); | 316 turl_service_->Remove(prepopulated_default); |
317 | 317 |
318 scoped_ptr<BaseSettingChange> change( | 318 scoped_ptr<BaseSettingChange> change( |
319 CreateDefaultSearchProviderChange(current_url, NULL)); | 319 CreateDefaultSearchProviderChange(current_url, NULL)); |
320 ASSERT_TRUE(change.get()); | 320 ASSERT_TRUE(change.get()); |
321 ASSERT_TRUE(change->Init(browser()->profile())); | 321 ASSERT_TRUE(change->Init(browser()->profile())); |
322 | 322 |
323 // Verify that the prepopulated default search is active. | 323 // Verify that the prepopulated default search is active. |
324 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 324 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
325 turl_service_->GetDefaultSearchProvider()); | 325 turl_service_->GetDefaultSearchProvider()); |
326 | 326 |
327 // Verify histograms. | 327 // Verify histograms. |
328 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | 328 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, |
329 current_histogram_id, 1); | 329 current_histogram_id, 1); |
330 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 330 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
331 prepopulated_histogram_id, 1); | 331 prepopulated_histogram_id, 1); |
332 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | 332 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, |
333 prepopulated_histogram_id, 1); | 333 prepopulated_histogram_id, 1); |
334 ExpectHistogramCount(kProtectorHistogramSearchProviderMissing, | 334 ExpectHistogramCount(kProtectorHistogramSearchProviderMissing, |
335 prepopulated_histogram_id, 1); | 335 prepopulated_histogram_id, 1); |
336 | 336 |
337 // Verify text messages. | 337 // Verify text messages. |
338 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 338 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
339 change->GetBubbleMessage()); | 339 change->GetBubbleMessage()); |
340 EXPECT_EQ(GetChangeSearchButtonText(example_com), | 340 EXPECT_EQ(GetChangeSearchButtonText(example_com), |
341 change->GetApplyButtonText()); | 341 change->GetApplyButtonText()); |
342 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 342 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
343 | 343 |
344 // Verify that search engine settings are opened by Discard. | 344 // Verify that search engine settings are opened by Discard. |
345 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 345 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
346 change->Discard(browser()); | 346 change->Discard(browser()); |
347 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 347 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
348 turl_service_->GetDefaultSearchProvider()); | 348 turl_service_->GetDefaultSearchProvider()); |
349 | 349 |
350 // Verify that Apply switches back to |current_url|. | 350 // Verify that Apply switches back to |current_url|. |
351 change->Apply(browser()); | 351 change->Apply(browser()); |
352 EXPECT_EQ(FindTemplateURL(http_example_com), | 352 EXPECT_EQ(FindTemplateURL(http_example_com), |
353 turl_service_->GetDefaultSearchProvider()); | 353 turl_service_->GetDefaultSearchProvider()); |
354 } | 354 } |
355 | 355 |
356 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | 356 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, |
357 BackupValidCurrentRemoved) { | 357 BackupValidCurrentRemoved) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 int prepopulated_histogram_id = | 403 int prepopulated_histogram_id = |
404 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | 404 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); |
405 turl_service_->SetDefaultSearchProvider(NULL); | 405 turl_service_->SetDefaultSearchProvider(NULL); |
406 | 406 |
407 scoped_ptr<BaseSettingChange> change( | 407 scoped_ptr<BaseSettingChange> change( |
408 CreateDefaultSearchProviderChange(NULL, NULL)); | 408 CreateDefaultSearchProviderChange(NULL, NULL)); |
409 ASSERT_TRUE(change.get()); | 409 ASSERT_TRUE(change.get()); |
410 ASSERT_TRUE(change->Init(browser()->profile())); | 410 ASSERT_TRUE(change->Init(browser()->profile())); |
411 | 411 |
412 // Verify that the prepopulated default search is active. | 412 // Verify that the prepopulated default search is active. |
413 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 413 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
414 turl_service_->GetDefaultSearchProvider()); | 414 turl_service_->GetDefaultSearchProvider()); |
415 | 415 |
416 // Verify histograms. | 416 // Verify histograms. |
417 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | 417 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, |
418 protector::GetSearchProviderHistogramID(NULL), 1); | 418 protector::GetSearchProviderHistogramID(NULL), 1); |
419 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 419 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
420 prepopulated_histogram_id, 1); | 420 prepopulated_histogram_id, 1); |
421 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | 421 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, |
422 prepopulated_histogram_id, 1); | 422 prepopulated_histogram_id, 1); |
423 | 423 |
424 // Verify text messages. | 424 // Verify text messages. |
425 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 425 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
426 change->GetBubbleMessage()); | 426 change->GetBubbleMessage()); |
427 EXPECT_EQ(string16(), change->GetApplyButtonText()); | 427 EXPECT_EQ(string16(), change->GetApplyButtonText()); |
428 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 428 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
429 | 429 |
430 // Verify that search engine settings are opened by Discard. | 430 // Verify that search engine settings are opened by Discard. |
431 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 431 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
432 change->Discard(browser()); | 432 change->Discard(browser()); |
433 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 433 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), |
434 turl_service_->GetDefaultSearchProvider()); | 434 turl_service_->GetDefaultSearchProvider()); |
435 } | 435 } |
436 | 436 |
437 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | 437 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, |
438 BackupInvalidFallbackSameAsCurrent) { | 438 BackupInvalidFallbackSameAsCurrent) { |
439 // Backup is invalid, fallback to the prepopulated default search which is | 439 // Backup is invalid, fallback to the prepopulated default search which is |
440 // same as the current search provider. | 440 // same as the current search provider. |
441 int prepopulated_histogram_id = | 441 int prepopulated_histogram_id = |
442 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | 442 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); |
443 const TemplateURL* current_url = turl_service_->GetDefaultSearchProvider(); | 443 const TemplateURL* current_url = turl_service_->GetDefaultSearchProvider(); |
444 | 444 |
445 // Verify that current search provider is same as the prepopulated default. | 445 // Verify that current search provider is same as the prepopulated default. |
446 ASSERT_TRUE(current_url); | 446 ASSERT_TRUE(current_url); |
447 ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url())); | 447 ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url()->url())); |
448 | 448 |
449 scoped_ptr<BaseSettingChange> change( | 449 scoped_ptr<BaseSettingChange> change( |
450 CreateDefaultSearchProviderChange(current_url, NULL)); | 450 CreateDefaultSearchProviderChange(current_url, NULL)); |
451 ASSERT_TRUE(change.get()); | 451 ASSERT_TRUE(change.get()); |
452 ASSERT_TRUE(change->Init(browser()->profile())); | 452 ASSERT_TRUE(change->Init(browser()->profile())); |
453 | 453 |
454 // Verify that the default search has not changed. | 454 // Verify that the default search has not changed. |
455 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); | 455 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); |
456 | 456 |
457 // Verify histograms. | 457 // Verify histograms. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 EXPECT_EQ(FindTemplateURL(http_example_info), | 538 EXPECT_EQ(FindTemplateURL(http_example_info), |
539 turl_service_->GetDefaultSearchProvider()); | 539 turl_service_->GetDefaultSearchProvider()); |
540 | 540 |
541 // Discard does nothing - backup was already active. | 541 // Discard does nothing - backup was already active. |
542 change->Discard(browser()); | 542 change->Discard(browser()); |
543 EXPECT_EQ(FindTemplateURL(http_example_info), | 543 EXPECT_EQ(FindTemplateURL(http_example_info), |
544 turl_service_->GetDefaultSearchProvider()); | 544 turl_service_->GetDefaultSearchProvider()); |
545 } | 545 } |
546 | 546 |
547 } // namespace protector | 547 } // namespace protector |
OLD | NEW |