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

Side by Side Diff: chrome/browser/protector/default_search_provider_change_browsertest.cc

Issue 9982018: Move most TemplateURL data members to a new struct, TemplateURLData. This allows us to eliminate t… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months 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 | Annotate | Revision Log
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 "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 22 matching lines...) Expand all
33 33
34 const string16 example_info = ASCIIToUTF16("Example.info"); 34 const string16 example_info = ASCIIToUTF16("Example.info");
35 const string16 example_info_long = ASCIIToUTF16("ExampleSearchEngine.info"); 35 const string16 example_info_long = ASCIIToUTF16("ExampleSearchEngine.info");
36 const std::string http_example_info = "http://example.info/%s"; 36 const std::string http_example_info = "http://example.info/%s";
37 const string16 example_com = ASCIIToUTF16("Example.com"); 37 const string16 example_com = ASCIIToUTF16("Example.com");
38 const string16 example_com_long = ASCIIToUTF16("ExampleSearchEngine.com"); 38 const string16 example_com_long = ASCIIToUTF16("ExampleSearchEngine.com");
39 const std::string http_example_com = "http://example.com/%s"; 39 const std::string http_example_com = "http://example.com/%s";
40 const string16 example_net = ASCIIToUTF16("Example.net"); 40 const string16 example_net = ASCIIToUTF16("Example.net");
41 const std::string http_example_net = "http://example.net/%s"; 41 const std::string http_example_net = "http://example.net/%s";
42 42
43 // Convenience function.
44 TemplateURL* MakeTemplateURL(const string16& short_name,
45 const string16& keyword,
46 const std::string& url) {
47 TemplateURLData data;
48 data.short_name = short_name;
49 if (keyword.empty())
50 data.SetAutogenerateKeyword(true);
51 else
52 data.SetKeyword(keyword);
53 data.SetURL(url);
54 return new TemplateURL(data);
55 }
56
43 }; 57 };
44 58
45 class DefaultSearchProviderChangeTest : public InProcessBrowserTest { 59 class DefaultSearchProviderChangeTest : public InProcessBrowserTest {
46 public: 60 public:
47 virtual void SetUpOnMainThread() OVERRIDE { 61 virtual void SetUpOnMainThread() OVERRIDE {
48 mock_protector_service_ = 62 mock_protector_service_ =
49 MockProtectorService::BuildForProfile(browser()->profile()); 63 MockProtectorService::BuildForProfile(browser()->profile());
50 64
51 // Ensure that TemplateURLService is loaded. 65 // Ensure that TemplateURLService is loaded.
52 turl_service_ = 66 turl_service_ =
53 TemplateURLServiceFactory::GetForProfile(browser()->profile()); 67 TemplateURLServiceFactory::GetForProfile(browser()->profile());
54 ui_test_utils::WaitForTemplateURLServiceToLoad(turl_service_); 68 ui_test_utils::WaitForTemplateURLServiceToLoad(turl_service_);
55 69
56 prepopulated_url_.reset( 70 prepopulated_url_.reset(
57 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 71 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
58 } 72 }
59 73
60 virtual void CleanUpOnMainThread() OVERRIDE { 74 virtual void CleanUpOnMainThread() OVERRIDE {
61 EXPECT_CALL(*mock_protector_service_, Shutdown()); 75 EXPECT_CALL(*mock_protector_service_, Shutdown());
62 } 76 }
63 77
64 TemplateURL* MakeTemplateURL(const string16& short_name,
65 const string16& keyword,
66 const std::string& search_url) {
67 TemplateURL* url = new TemplateURL;
68 url->set_short_name(short_name);
69 if (keyword.empty())
70 url->set_autogenerate_keyword(true);
71 else
72 url->set_keyword(keyword);
73 url->SetURL(search_url);
74 return url;
75 }
76
77 const TemplateURL* FindTemplateURL(const std::string& search_url) { 78 const TemplateURL* FindTemplateURL(const std::string& search_url) {
78 TemplateURLService::TemplateURLVector urls = 79 TemplateURLService::TemplateURLVector urls =
79 turl_service_->GetTemplateURLs(); 80 turl_service_->GetTemplateURLs();
80 for (TemplateURLService::TemplateURLVector::const_iterator 81 for (TemplateURLService::TemplateURLVector::const_iterator
81 it = urls.begin(); it != urls.end(); ++it) { 82 it = urls.begin(); it != urls.end(); ++it) {
82 if ((*it)->url() == search_url) 83 if ((*it)->url() == search_url)
83 return *it; 84 return *it;
84 } 85 }
85 return NULL; 86 return NULL;
86 } 87 }
87 88
88 // Adds a copy of |turl| that will be owned by TemplateURLService.
89 void AddCopy(TemplateURL* turl) {
90 TemplateURL* turl_copy = new TemplateURL(*turl);
91 turl_service_->Add(turl_copy);
92 }
93
94 void AddAndSetDefault(TemplateURL* turl) { 89 void AddAndSetDefault(TemplateURL* turl) {
95 turl_service_->Add(turl); 90 turl_service_->Add(turl);
96 turl_service_->SetDefaultSearchProvider(turl); 91 turl_service_->SetDefaultSearchProvider(turl);
97 } 92 }
98 93
99 string16 GetBubbleMessage(const string16& short_name = string16()) { 94 string16 GetBubbleMessage(const string16& short_name = string16()) {
100 return short_name.empty() ? 95 return short_name.empty() ?
101 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_MESSAGE) : 96 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_MESSAGE) :
102 l10n_util::GetStringFUTF16(IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, 97 l10n_util::GetStringFUTF16(IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE,
103 short_name); 98 short_name);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // Most common case: current default search provider exists, backup is valid, 146 // Most common case: current default search provider exists, backup is valid,
152 // they are different. 147 // they are different.
153 TemplateURL* backup_url = 148 TemplateURL* backup_url =
154 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); 149 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info);
155 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url); 150 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url);
156 TemplateURL* current_url = 151 TemplateURL* current_url =
157 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); 152 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com);
158 int current_histogram_id = 153 int current_histogram_id =
159 protector::GetSearchProviderHistogramID(current_url); 154 protector::GetSearchProviderHistogramID(current_url);
160 155
161 AddCopy(backup_url); 156 turl_service_->Add(new TemplateURL(backup_url->data()));
162 AddAndSetDefault(current_url); 157 AddAndSetDefault(current_url);
163 158
164 scoped_ptr<BaseSettingChange> change( 159 scoped_ptr<BaseSettingChange> change(
165 CreateDefaultSearchProviderChange(current_url, backup_url)); 160 CreateDefaultSearchProviderChange(current_url, backup_url));
166 ASSERT_TRUE(change.get()); 161 ASSERT_TRUE(change.get());
167 ASSERT_TRUE(change->Init(browser()->profile())); 162 ASSERT_TRUE(change->Init(browser()->profile()));
168 163
169 // Verify that backup is active. 164 // Verify that backup is active.
170 EXPECT_EQ(FindTemplateURL(http_example_info), 165 EXPECT_EQ(FindTemplateURL(http_example_info),
171 turl_service_->GetDefaultSearchProvider()); 166 turl_service_->GetDefaultSearchProvider());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); 199 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info);
205 TemplateURL* backup_url_long = 200 TemplateURL* backup_url_long =
206 MakeTemplateURL(example_info_long, ASCIIToUTF16("a"), http_example_info); 201 MakeTemplateURL(example_info_long, ASCIIToUTF16("a"), http_example_info);
207 TemplateURL* current_url = 202 TemplateURL* current_url =
208 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); 203 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com);
209 TemplateURL* current_url_long = 204 TemplateURL* current_url_long =
210 MakeTemplateURL(example_com_long, ASCIIToUTF16("b"), http_example_com); 205 MakeTemplateURL(example_com_long, ASCIIToUTF16("b"), http_example_com);
211 206
212 { 207 {
213 // Backup name too long. 208 // Backup name too long.
214 AddCopy(backup_url_long); 209 turl_service_->Add(new TemplateURL(backup_url_long->data()));
215 AddAndSetDefault(current_url); 210 AddAndSetDefault(current_url);
216 211
217 scoped_ptr<BaseSettingChange> change( 212 scoped_ptr<BaseSettingChange> change(
218 CreateDefaultSearchProviderChange(current_url, backup_url_long)); 213 CreateDefaultSearchProviderChange(current_url, backup_url_long));
219 ASSERT_TRUE(change.get()); 214 ASSERT_TRUE(change.get());
220 ASSERT_TRUE(change->Init(browser()->profile())); 215 ASSERT_TRUE(change->Init(browser()->profile()));
221 216
222 // Verify text messages. 217 // Verify text messages.
223 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); 218 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage());
224 EXPECT_EQ(GetChangeSearchButtonText(example_com), 219 EXPECT_EQ(GetChangeSearchButtonText(example_com),
225 change->GetApplyButtonText()); 220 change->GetApplyButtonText());
226 EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText()); 221 EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText());
227 } 222 }
228 223
229 { 224 {
230 // Current name too long. 225 // Current name too long.
231 AddCopy(backup_url); 226 turl_service_->Add(new TemplateURL(backup_url->data()));
232 AddAndSetDefault(current_url_long); 227 AddAndSetDefault(current_url_long);
233 228
234 scoped_ptr<BaseSettingChange> change( 229 scoped_ptr<BaseSettingChange> change(
235 CreateDefaultSearchProviderChange(current_url_long, backup_url)); 230 CreateDefaultSearchProviderChange(current_url_long, backup_url));
236 ASSERT_TRUE(change.get()); 231 ASSERT_TRUE(change.get());
237 ASSERT_TRUE(change->Init(browser()->profile())); 232 ASSERT_TRUE(change->Init(browser()->profile()));
238 233
239 // Verify text messages. 234 // Verify text messages.
240 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); 235 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage());
241 EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText()); 236 EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText());
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 turl_service_->GetDefaultSearchProvider()); 348 turl_service_->GetDefaultSearchProvider());
354 } 349 }
355 350
356 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, 351 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest,
357 BackupValidCurrentRemoved) { 352 BackupValidCurrentRemoved) {
358 // Backup is valid, no current search provider. 353 // Backup is valid, no current search provider.
359 TemplateURL* backup_url = 354 TemplateURL* backup_url =
360 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); 355 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info);
361 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url); 356 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url);
362 357
363 AddCopy(backup_url); 358 turl_service_->Add(new TemplateURL(backup_url->data()));
364 turl_service_->SetDefaultSearchProvider(NULL); 359 turl_service_->SetDefaultSearchProvider(NULL);
365 360
366 scoped_ptr<BaseSettingChange> change( 361 scoped_ptr<BaseSettingChange> change(
367 CreateDefaultSearchProviderChange(NULL, backup_url)); 362 CreateDefaultSearchProviderChange(NULL, backup_url));
368 ASSERT_TRUE(change.get()); 363 ASSERT_TRUE(change.get());
369 ASSERT_TRUE(change->Init(browser()->profile())); 364 ASSERT_TRUE(change->Init(browser()->profile()));
370 365
371 // Verify that backup is active. 366 // Verify that backup is active.
372 EXPECT_EQ(FindTemplateURL(http_example_info), 367 EXPECT_EQ(FindTemplateURL(http_example_info),
373 turl_service_->GetDefaultSearchProvider()); 368 turl_service_->GetDefaultSearchProvider());
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 DefaultSearchProviderChangedByUser) { 474 DefaultSearchProviderChangedByUser) {
480 // Default search provider is changed by user while the error is active. 475 // Default search provider is changed by user while the error is active.
481 // Setup is the same as in BackupValid test case. 476 // Setup is the same as in BackupValid test case.
482 TemplateURL* backup_url = 477 TemplateURL* backup_url =
483 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); 478 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info);
484 TemplateURL* current_url = 479 TemplateURL* current_url =
485 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); 480 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com);
486 TemplateURL* new_url = 481 TemplateURL* new_url =
487 MakeTemplateURL(example_net, ASCIIToUTF16("c"), http_example_net); 482 MakeTemplateURL(example_net, ASCIIToUTF16("c"), http_example_net);
488 483
489 AddCopy(backup_url); 484 turl_service_->Add(new TemplateURL(backup_url->data()));
490 AddAndSetDefault(current_url); 485 AddAndSetDefault(current_url);
491 486
492 scoped_ptr<BaseSettingChange> change( 487 scoped_ptr<BaseSettingChange> change(
493 CreateDefaultSearchProviderChange(current_url, backup_url)); 488 CreateDefaultSearchProviderChange(current_url, backup_url));
494 ASSERT_TRUE(change.get()); 489 ASSERT_TRUE(change.get());
495 ASSERT_TRUE(change->Init(browser()->profile())); 490 ASSERT_TRUE(change->Init(browser()->profile()));
496 491
497 // Verify that backup is active. 492 // Verify that backup is active.
498 EXPECT_EQ(FindTemplateURL(http_example_info), 493 EXPECT_EQ(FindTemplateURL(http_example_info),
499 turl_service_->GetDefaultSearchProvider()); 494 turl_service_->GetDefaultSearchProvider());
500 495
501 // Verify that changing search provider externally dismissed the change. 496 // Verify that changing search provider externally dismissed the change.
502 EXPECT_CALL(*mock_protector_service_, DismissChange(change.get())); 497 EXPECT_CALL(*mock_protector_service_, DismissChange(change.get()));
503 AddAndSetDefault(new_url); 498 AddAndSetDefault(new_url);
504 } 499 }
505 500
506 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, 501 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest,
507 CurrentSearchProviderRemovedByUser) { 502 CurrentSearchProviderRemovedByUser) {
508 // Current search provider is removed by user while the error is active. 503 // Current search provider is removed by user while the error is active.
509 // Setup is the same as in BackupValid test case. 504 // Setup is the same as in BackupValid test case.
510 TemplateURL* backup_url = 505 TemplateURL* backup_url =
511 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); 506 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info);
512 TemplateURL* current_url = 507 TemplateURL* current_url =
513 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); 508 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com);
514 509
515 AddCopy(backup_url); 510 turl_service_->Add(new TemplateURL(backup_url->data()));
516 AddAndSetDefault(current_url); 511 AddAndSetDefault(current_url);
517 512
518 scoped_ptr<BaseSettingChange> change( 513 scoped_ptr<BaseSettingChange> change(
519 CreateDefaultSearchProviderChange(current_url, backup_url)); 514 CreateDefaultSearchProviderChange(current_url, backup_url));
520 ASSERT_TRUE(change.get()); 515 ASSERT_TRUE(change.get());
521 ASSERT_TRUE(change->Init(browser()->profile())); 516 ASSERT_TRUE(change->Init(browser()->profile()));
522 517
523 // Verify that backup is active. 518 // Verify that backup is active.
524 EXPECT_EQ(FindTemplateURL(http_example_info), 519 EXPECT_EQ(FindTemplateURL(http_example_info),
525 turl_service_->GetDefaultSearchProvider()); 520 turl_service_->GetDefaultSearchProvider());
(...skipping 12 matching lines...) Expand all
538 EXPECT_EQ(FindTemplateURL(http_example_info), 533 EXPECT_EQ(FindTemplateURL(http_example_info),
539 turl_service_->GetDefaultSearchProvider()); 534 turl_service_->GetDefaultSearchProvider());
540 535
541 // Discard does nothing - backup was already active. 536 // Discard does nothing - backup was already active.
542 change->Discard(browser()); 537 change->Discard(browser());
543 EXPECT_EQ(FindTemplateURL(http_example_info), 538 EXPECT_EQ(FindTemplateURL(http_example_info),
544 turl_service_->GetDefaultSearchProvider()); 539 turl_service_->GetDefaultSearchProvider());
545 } 540 }
546 541
547 } // namespace protector 542 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/browser/policy/configuration_policy_handler.cc ('k') | chrome/browser/search_engines/search_host_to_urls_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698