OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/scoped_ptr.h" | |
6 #include "base/metrics/histogram.h" | |
7 #include "base/metrics/histogram_samples.h" | |
8 #include "base/metrics/statistics_recorder.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/protector/base_setting_change.h" | |
12 #include "chrome/browser/protector/histograms.h" | |
13 #include "chrome/browser/protector/mock_protector_service.h" | |
14 #include "chrome/browser/protector/protector_service_factory.h" | |
15 #include "chrome/browser/search_engines/template_url.h" | |
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
17 #include "chrome/browser/search_engines/template_url_service.h" | |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
19 #include "chrome/browser/ui/browser.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "chrome/test/base/in_process_browser_test.h" | |
22 #include "chrome/test/base/ui_test_utils.h" | |
23 #include "googleurl/src/gurl.h" | |
24 #include "grit/generated_resources.h" | |
25 #include "ui/base/l10n/l10n_util.h" | |
26 | |
27 using ::testing::Invoke; | |
28 using ::testing::NiceMock; | |
29 using ::testing::Return; | |
30 | |
31 namespace protector { | |
32 | |
33 namespace { | |
34 | |
35 // Keyword names and URLs used for testing. | |
36 const string16 example_info = ASCIIToUTF16("Example.info"); | |
37 const string16 example_info_long = ASCIIToUTF16("ExampleSearchEngine.info"); | |
38 const std::string http_example_info = "http://example.info/?q={searchTerms}"; | |
39 const std::string http_example_info_no_search_terms = | |
40 "http://example.net/"; | |
41 const std::string example_info_domain = "example.info"; | |
42 const string16 example_com = ASCIIToUTF16("Example.com"); | |
43 const string16 example_com_long = ASCIIToUTF16("ExampleSearchEngine.com"); | |
44 const std::string http_example_com = "http://example.com/?q={searchTerms}"; | |
45 const std::string http_example_com_no_search_terms = | |
46 "http://example.com/"; | |
47 const std::string example_com_domain = "example.com"; | |
48 const string16 example_net = ASCIIToUTF16("Example.net"); | |
49 const std::string http_example_net = "http://example.net/?q={searchTerms}"; | |
50 const std::string example_domain = "example.net"; | |
51 | |
52 const BaseSettingChange::DisplayName kNoDisplayName( | |
53 BaseSettingChange::kDefaultNamePriority, string16()); | |
54 | |
55 } // namespace | |
56 | |
57 class DefaultSearchProviderChangeTest : public InProcessBrowserTest { | |
58 public: | |
59 virtual void SetUpOnMainThread() OVERRIDE { | |
60 Profile* profile = browser()->profile(); | |
61 mock_protector_service_ = MockProtectorService::BuildForProfile(profile); | |
62 | |
63 // Ensure that TemplateURLService is loaded. | |
64 turl_service_ = TemplateURLServiceFactory::GetForProfile(profile); | |
65 ui_test_utils::WaitForTemplateURLServiceToLoad(turl_service_); | |
66 | |
67 prepopulated_url_.reset( | |
68 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); | |
69 } | |
70 | |
71 virtual void CleanUpOnMainThread() OVERRIDE { | |
72 EXPECT_CALL(*mock_protector_service_, Shutdown()); | |
73 } | |
74 | |
75 TemplateURL* MakeTemplateURL(const string16& short_name, | |
76 const string16& keyword, | |
77 const std::string& url) { | |
78 TemplateURLData data; | |
79 data.short_name = short_name; | |
80 data.SetKeyword(keyword); | |
81 data.SetURL(url); | |
82 return new TemplateURL(browser()->profile(), data); | |
83 } | |
84 | |
85 TemplateURL* FindTemplateURL(const std::string& search_url) { | |
86 TemplateURLService::TemplateURLVector urls = | |
87 turl_service_->GetTemplateURLs(); | |
88 for (TemplateURLService::TemplateURLVector::const_iterator | |
89 it = urls.begin(); it != urls.end(); ++it) { | |
90 if ((*it)->url() == search_url) | |
91 return *it; | |
92 } | |
93 return NULL; | |
94 } | |
95 | |
96 void AddAndSetDefault(TemplateURL* turl) { | |
97 turl_service_->Add(turl); | |
98 turl_service_->SetDefaultSearchProvider(turl); | |
99 } | |
100 | |
101 string16 GetBubbleMessage(const string16& short_name = string16()) { | |
102 return short_name.empty() ? | |
103 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_MESSAGE) : | |
104 l10n_util::GetStringFUTF16(IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, | |
105 short_name); | |
106 } | |
107 | |
108 string16 GetChangeSearchButtonText(const string16& short_name = string16()) { | |
109 return short_name.empty() ? | |
110 l10n_util::GetStringUTF16(IDS_CHANGE_SEARCH_ENGINE_NO_NAME) : | |
111 l10n_util::GetStringFUTF16(IDS_CHANGE_SEARCH_ENGINE, short_name); | |
112 } | |
113 | |
114 string16 GetKeepSearchButtonText(const string16& short_name = string16()) { | |
115 return short_name.empty() ? | |
116 l10n_util::GetStringUTF16(IDS_KEEP_SETTING) : | |
117 l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, short_name); | |
118 } | |
119 | |
120 string16 GetOpenSettingsButtonText() { | |
121 return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); | |
122 } | |
123 | |
124 void ExpectSettingsOpened(const std::string& subpage) { | |
125 GURL settings_url(chrome::kChromeUISettingsURL + subpage); | |
126 EXPECT_CALL(*mock_protector_service_, OpenTab(settings_url, browser())); | |
127 } | |
128 | |
129 void ExpectHistogramCount(const std::string& name, | |
130 base::HistogramBase::Sample sample, | |
131 base::Histogram::Count expected_count) { | |
132 base::Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); | |
133 EXPECT_TRUE(histogram != NULL); | |
134 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | |
135 EXPECT_EQ(expected_count, samples->GetCount(sample)) | |
136 << "Invalid histogram " << name << " count for sample: " << sample; | |
137 } | |
138 | |
139 protected: | |
140 MockProtectorService* mock_protector_service_; | |
141 TemplateURLService* turl_service_; | |
142 scoped_ptr<TemplateURL> prepopulated_url_; | |
143 }; | |
144 | |
145 // Tests below call both Apply and Discard on a single change instance. | |
146 // This is test-only and should not be happening in real code. | |
147 | |
148 // |backup_url| in further test cases is owned by DefaultSearchProviderChange | |
149 // instance, while other TemplateURLs are owned by TemplateURLService. | |
150 | |
151 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValid) { | |
152 // Most common case: current default search provider exists, backup is valid, | |
153 // they are different. | |
154 TemplateURL* backup_url = | |
155 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
156 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url); | |
157 TemplateURL* current_url = | |
158 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
159 int current_histogram_id = | |
160 protector::GetSearchProviderHistogramID(current_url); | |
161 | |
162 Profile* profile = browser()->profile(); | |
163 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
164 AddAndSetDefault(current_url); | |
165 | |
166 scoped_ptr<BaseSettingChange> change( | |
167 CreateDefaultSearchProviderChange(current_url, backup_url)); | |
168 ASSERT_TRUE(change.get()); | |
169 ASSERT_TRUE(change->Init(profile)); | |
170 | |
171 // Verify that backup is active. | |
172 EXPECT_EQ(FindTemplateURL(http_example_info), | |
173 turl_service_->GetDefaultSearchProvider()); | |
174 | |
175 // Verify histograms. | |
176 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, | |
177 current_histogram_id, 1); | |
178 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
179 backup_histogram_id, 1); | |
180 | |
181 // Verify text messages. | |
182 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
183 EXPECT_EQ(GetChangeSearchButtonText(example_com), | |
184 change->GetApplyButtonText()); | |
185 EXPECT_EQ(GetKeepSearchButtonText(example_info), | |
186 change->GetDiscardButtonText()); | |
187 EXPECT_EQ(example_com_domain, change->GetNewSettingURL().host()); | |
188 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); | |
189 | |
190 // Discard does nothing - backup was already active. | |
191 change->Discard(browser()); | |
192 EXPECT_EQ(FindTemplateURL(http_example_info), | |
193 turl_service_->GetDefaultSearchProvider()); | |
194 ExpectHistogramCount(kProtectorHistogramSearchProviderDiscarded, | |
195 current_histogram_id, 1); | |
196 | |
197 // Verify that Apply switches back to |current_url|. | |
198 change->Apply(browser()); | |
199 EXPECT_EQ(FindTemplateURL(http_example_com), | |
200 turl_service_->GetDefaultSearchProvider()); | |
201 ExpectHistogramCount(kProtectorHistogramSearchProviderApplied, | |
202 current_histogram_id, 1); | |
203 } | |
204 | |
205 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValidLongNames) { | |
206 // Verify that search provider names that are too long are not displayed. | |
207 TemplateURL* backup_url = | |
208 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
209 TemplateURL* backup_url_long = | |
210 MakeTemplateURL(example_info_long, ASCIIToUTF16("a"), http_example_info); | |
211 TemplateURL* current_url = | |
212 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
213 TemplateURL* current_url_long = | |
214 MakeTemplateURL(example_com_long, ASCIIToUTF16("b"), http_example_com); | |
215 | |
216 Profile* profile = browser()->profile(); | |
217 { | |
218 // Backup name too long. | |
219 turl_service_->Add(new TemplateURL(profile, backup_url_long->data())); | |
220 AddAndSetDefault(current_url); | |
221 | |
222 scoped_ptr<BaseSettingChange> change( | |
223 CreateDefaultSearchProviderChange(current_url, backup_url_long)); | |
224 ASSERT_TRUE(change.get()); | |
225 ASSERT_TRUE(change->Init(profile)); | |
226 | |
227 // Verify text messages. | |
228 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
229 EXPECT_EQ(GetChangeSearchButtonText(example_com), | |
230 change->GetApplyButtonText()); | |
231 EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText()); | |
232 EXPECT_EQ(example_com_domain, change->GetNewSettingURL().host()); | |
233 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); | |
234 } | |
235 | |
236 { | |
237 // Current name too long. | |
238 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
239 AddAndSetDefault(current_url_long); | |
240 | |
241 scoped_ptr<BaseSettingChange> change( | |
242 CreateDefaultSearchProviderChange(current_url_long, backup_url)); | |
243 ASSERT_TRUE(change.get()); | |
244 ASSERT_TRUE(change->Init(profile)); | |
245 | |
246 // Verify text messages. | |
247 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
248 EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText()); | |
249 EXPECT_EQ(GetKeepSearchButtonText(example_info), | |
250 change->GetDiscardButtonText()); | |
251 EXPECT_EQ(example_com_domain, change->GetNewSettingURL().host()); | |
252 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
253 } | |
254 } | |
255 | |
256 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { | |
257 // Backup is invalid, current search provider exists, fallback to the | |
258 // prepopulated default search, which exists among keywords. | |
259 int prepopulated_histogram_id = | |
260 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | |
261 TemplateURL* current_url = | |
262 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
263 int current_histogram_id = | |
264 protector::GetSearchProviderHistogramID(current_url); | |
265 | |
266 AddAndSetDefault(current_url); | |
267 | |
268 // Prepopulated default search must exist. | |
269 ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url())); | |
270 | |
271 scoped_ptr<BaseSettingChange> change( | |
272 CreateDefaultSearchProviderChange(current_url, NULL)); | |
273 ASSERT_TRUE(change.get()); | |
274 ASSERT_TRUE(change->Init(browser()->profile())); | |
275 | |
276 // Verify that the prepopulated default search is active. | |
277 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
278 turl_service_->GetDefaultSearchProvider()); | |
279 | |
280 // Verify histograms. | |
281 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | |
282 current_histogram_id, 1); | |
283 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
284 prepopulated_histogram_id, 1); | |
285 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | |
286 prepopulated_histogram_id, 1); | |
287 | |
288 // Verify text messages. | |
289 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | |
290 change->GetBubbleMessage()); | |
291 EXPECT_EQ(GetChangeSearchButtonText(example_com), | |
292 change->GetApplyButtonText()); | |
293 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | |
294 | |
295 // Verify that search engine settings are opened by Discard. | |
296 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
297 change->Discard(browser()); | |
298 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
299 turl_service_->GetDefaultSearchProvider()); | |
300 | |
301 // Verify that Apply switches back to |current_url|. | |
302 change->Apply(browser()); | |
303 EXPECT_EQ(FindTemplateURL(http_example_com), | |
304 turl_service_->GetDefaultSearchProvider()); | |
305 } | |
306 | |
307 | |
308 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
309 BackupValidCannotBeDefault) { | |
310 // Backup is a valid keyword but cannot be the default search provider (has no | |
311 // search terms), current search provider exists, fallback to the prepopulated | |
312 // default search, which exists among keywords. | |
313 TemplateURL* backup_url = | |
314 MakeTemplateURL(example_info, ASCIIToUTF16("a"), | |
315 http_example_info_no_search_terms); | |
316 int prepopulated_histogram_id = | |
317 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | |
318 TemplateURL* current_url = | |
319 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
320 int current_histogram_id = | |
321 protector::GetSearchProviderHistogramID(current_url); | |
322 | |
323 AddAndSetDefault(current_url); | |
324 | |
325 // Prepopulated default search must exist. | |
326 ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url())); | |
327 | |
328 scoped_ptr<BaseSettingChange> change( | |
329 CreateDefaultSearchProviderChange(current_url, backup_url)); | |
330 ASSERT_TRUE(change.get()); | |
331 ASSERT_TRUE(change->Init(browser()->profile())); | |
332 | |
333 // Verify that the prepopulated default search is active. | |
334 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
335 turl_service_->GetDefaultSearchProvider()); | |
336 | |
337 // Verify histograms. | |
338 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, | |
339 current_histogram_id, 1); | |
340 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
341 prepopulated_histogram_id, 1); | |
342 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | |
343 prepopulated_histogram_id, 1); | |
344 | |
345 // Verify text messages. | |
346 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | |
347 change->GetBubbleMessage()); | |
348 EXPECT_EQ(GetChangeSearchButtonText(example_com), | |
349 change->GetApplyButtonText()); | |
350 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | |
351 | |
352 // Verify that search engine settings are opened by Discard. | |
353 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
354 change->Discard(browser()); | |
355 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
356 turl_service_->GetDefaultSearchProvider()); | |
357 | |
358 // Verify that Apply switches back to |current_url|. | |
359 change->Apply(browser()); | |
360 EXPECT_EQ(FindTemplateURL(http_example_com), | |
361 turl_service_->GetDefaultSearchProvider()); | |
362 } | |
363 | |
364 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
365 BackupInvalidFallbackRemoved) { | |
366 // Backup is invalid, current search provider exists, fallback to the | |
367 // prepopulated default search, which was removed from keywords (will be | |
368 // added). | |
369 int prepopulated_histogram_id = | |
370 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | |
371 TemplateURL* current_url = | |
372 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
373 int current_histogram_id = | |
374 protector::GetSearchProviderHistogramID(current_url); | |
375 | |
376 AddAndSetDefault(current_url); | |
377 | |
378 TemplateURL* prepopulated_default = FindTemplateURL(prepopulated_url_->url()); | |
379 // Prepopulated default search must exist, remove it. | |
380 ASSERT_TRUE(prepopulated_default); | |
381 turl_service_->Remove(prepopulated_default); | |
382 | |
383 scoped_ptr<BaseSettingChange> change( | |
384 CreateDefaultSearchProviderChange(current_url, NULL)); | |
385 ASSERT_TRUE(change.get()); | |
386 ASSERT_TRUE(change->Init(browser()->profile())); | |
387 | |
388 // Verify that the prepopulated default search is active. | |
389 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
390 turl_service_->GetDefaultSearchProvider()); | |
391 | |
392 // Verify histograms. | |
393 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | |
394 current_histogram_id, 1); | |
395 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
396 prepopulated_histogram_id, 1); | |
397 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | |
398 prepopulated_histogram_id, 1); | |
399 ExpectHistogramCount(kProtectorHistogramSearchProviderMissing, | |
400 prepopulated_histogram_id, 1); | |
401 | |
402 // Verify text messages. | |
403 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | |
404 change->GetBubbleMessage()); | |
405 EXPECT_EQ(GetChangeSearchButtonText(example_com), | |
406 change->GetApplyButtonText()); | |
407 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | |
408 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
409 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); | |
410 | |
411 // Verify that search engine settings are opened by Discard. | |
412 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
413 change->Discard(browser()); | |
414 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
415 turl_service_->GetDefaultSearchProvider()); | |
416 | |
417 // Verify that Apply switches back to |current_url|. | |
418 change->Apply(browser()); | |
419 EXPECT_EQ(FindTemplateURL(http_example_com), | |
420 turl_service_->GetDefaultSearchProvider()); | |
421 } | |
422 | |
423 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
424 BackupValidCurrentRemoved) { | |
425 // Backup is valid, no current search provider. | |
426 TemplateURL* backup_url = | |
427 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
428 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url); | |
429 | |
430 Profile* profile = browser()->profile(); | |
431 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
432 turl_service_->SetDefaultSearchProvider(NULL); | |
433 | |
434 scoped_ptr<BaseSettingChange> change( | |
435 CreateDefaultSearchProviderChange(NULL, backup_url)); | |
436 ASSERT_TRUE(change.get()); | |
437 ASSERT_TRUE(change->Init(profile)); | |
438 | |
439 // Verify that backup is active. | |
440 EXPECT_EQ(FindTemplateURL(http_example_info), | |
441 turl_service_->GetDefaultSearchProvider()); | |
442 | |
443 // Verify histograms. | |
444 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, | |
445 protector::GetSearchProviderHistogramID(NULL), 1); | |
446 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
447 backup_histogram_id, 1); | |
448 | |
449 // Verify text messages. | |
450 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
451 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); | |
452 EXPECT_EQ(GetKeepSearchButtonText(example_info), | |
453 change->GetDiscardButtonText()); | |
454 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
455 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
456 | |
457 // Discard does nothing - backup was already active. | |
458 change->Discard(browser()); | |
459 EXPECT_EQ(FindTemplateURL(http_example_info), | |
460 turl_service_->GetDefaultSearchProvider()); | |
461 | |
462 // Verify that search engine settings are opened by Apply (no other changes). | |
463 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
464 change->Apply(browser()); | |
465 EXPECT_EQ(FindTemplateURL(http_example_info), | |
466 turl_service_->GetDefaultSearchProvider()); | |
467 } | |
468 | |
469 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
470 BackupValidCurrentCannotBeDefault) { | |
471 // Backup is valid, current search provider has no search terms. | |
472 TemplateURL* backup_url = | |
473 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
474 int backup_histogram_id = protector::GetSearchProviderHistogramID(backup_url); | |
475 TemplateURL* current_url = | |
476 MakeTemplateURL(example_com, ASCIIToUTF16("b"), | |
477 http_example_com_no_search_terms); | |
478 int current_histogram_id = | |
479 protector::GetSearchProviderHistogramID(current_url); | |
480 | |
481 Profile* profile = browser()->profile(); | |
482 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
483 // TODO(ivankr): this may become unsupported soon. | |
484 AddAndSetDefault(current_url); | |
485 | |
486 scoped_ptr<BaseSettingChange> change( | |
487 CreateDefaultSearchProviderChange(current_url, backup_url)); | |
488 ASSERT_TRUE(change.get()); | |
489 ASSERT_TRUE(change->Init(profile)); | |
490 | |
491 // Verify that backup is active. | |
492 EXPECT_EQ(FindTemplateURL(http_example_info), | |
493 turl_service_->GetDefaultSearchProvider()); | |
494 | |
495 // Verify histograms. | |
496 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, | |
497 current_histogram_id, 1); | |
498 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
499 backup_histogram_id, 1); | |
500 | |
501 // Verify text messages. | |
502 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
503 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); | |
504 EXPECT_EQ(GetKeepSearchButtonText(example_info), | |
505 change->GetDiscardButtonText()); | |
506 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
507 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
508 | |
509 // Discard does nothing - backup was already active. | |
510 change->Discard(browser()); | |
511 EXPECT_EQ(FindTemplateURL(http_example_info), | |
512 turl_service_->GetDefaultSearchProvider()); | |
513 | |
514 // Verify that search engine settings are opened by Apply (no other changes). | |
515 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
516 change->Apply(browser()); | |
517 EXPECT_EQ(FindTemplateURL(http_example_info), | |
518 turl_service_->GetDefaultSearchProvider()); | |
519 } | |
520 | |
521 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
522 BackupInvalidCurrentRemoved) { | |
523 // Backup is invalid, no current search provider, fallback to the prepopulated | |
524 // default search. | |
525 int prepopulated_histogram_id = | |
526 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | |
527 turl_service_->SetDefaultSearchProvider(NULL); | |
528 | |
529 scoped_ptr<BaseSettingChange> change( | |
530 CreateDefaultSearchProviderChange(NULL, NULL)); | |
531 ASSERT_TRUE(change.get()); | |
532 ASSERT_TRUE(change->Init(browser()->profile())); | |
533 | |
534 // Verify that the prepopulated default search is active. | |
535 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
536 turl_service_->GetDefaultSearchProvider()); | |
537 | |
538 // Verify histograms. | |
539 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | |
540 protector::GetSearchProviderHistogramID(NULL), 1); | |
541 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
542 prepopulated_histogram_id, 1); | |
543 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | |
544 prepopulated_histogram_id, 1); | |
545 | |
546 // Verify text messages. | |
547 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | |
548 change->GetBubbleMessage()); | |
549 EXPECT_EQ(string16(), change->GetApplyButtonText()); | |
550 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | |
551 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
552 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
553 | |
554 // Verify that search engine settings are opened by Discard. | |
555 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
556 change->Discard(browser()); | |
557 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | |
558 turl_service_->GetDefaultSearchProvider()); | |
559 } | |
560 | |
561 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
562 BackupInvalidFallbackSameAsCurrent) { | |
563 // Backup is invalid, fallback to the prepopulated default search which is | |
564 // same as the current search provider. | |
565 int prepopulated_histogram_id = | |
566 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | |
567 TemplateURL* current_url = turl_service_->GetDefaultSearchProvider(); | |
568 | |
569 // Verify that current search provider is same as the prepopulated default. | |
570 ASSERT_TRUE(current_url); | |
571 ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url())); | |
572 | |
573 scoped_ptr<BaseSettingChange> change( | |
574 CreateDefaultSearchProviderChange(current_url, NULL)); | |
575 ASSERT_TRUE(change.get()); | |
576 ASSERT_TRUE(change->Init(browser()->profile())); | |
577 | |
578 // Verify that the default search has not changed. | |
579 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); | |
580 | |
581 // Verify histograms. | |
582 ExpectHistogramCount(kProtectorHistogramSearchProviderCorrupt, | |
583 prepopulated_histogram_id, 1); | |
584 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | |
585 prepopulated_histogram_id, 1); | |
586 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | |
587 prepopulated_histogram_id, 1); | |
588 | |
589 // Verify text messages. | |
590 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | |
591 change->GetBubbleMessage()); | |
592 EXPECT_EQ(string16(), change->GetApplyButtonText()); | |
593 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | |
594 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
595 EXPECT_EQ(current_url->short_name(), change->GetApplyDisplayName().second); | |
596 | |
597 // Verify that search engine settings are opened by Discard. | |
598 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
599 change->Discard(browser()); | |
600 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); | |
601 } | |
602 | |
603 | |
604 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
605 DefaultSearchProviderChangedByUser) { | |
606 // Default search provider is changed by user while the error is active. | |
607 // Setup is the same as in BackupValid test case. | |
608 TemplateURL* backup_url = | |
609 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
610 TemplateURL* current_url = | |
611 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
612 TemplateURL* new_url = | |
613 MakeTemplateURL(example_net, ASCIIToUTF16("c"), http_example_net); | |
614 | |
615 Profile* profile = browser()->profile(); | |
616 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
617 AddAndSetDefault(current_url); | |
618 | |
619 scoped_ptr<BaseSettingChange> change( | |
620 CreateDefaultSearchProviderChange(current_url, backup_url)); | |
621 ASSERT_TRUE(change.get()); | |
622 ASSERT_TRUE(change->Init(profile)); | |
623 | |
624 // Verify that backup is active. | |
625 EXPECT_EQ(FindTemplateURL(http_example_info), | |
626 turl_service_->GetDefaultSearchProvider()); | |
627 | |
628 // Verify that changing search provider externally dismissed the change. | |
629 EXPECT_CALL(*mock_protector_service_, DismissChange(change.get())); | |
630 AddAndSetDefault(new_url); | |
631 } | |
632 | |
633 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | |
634 CurrentSearchProviderRemovedByUser) { | |
635 // Current search provider is removed by user while the error is active. | |
636 // Setup is the same as in BackupValid test case. | |
637 TemplateURL* backup_url = | |
638 MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); | |
639 TemplateURL* current_url = | |
640 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | |
641 | |
642 Profile* profile = browser()->profile(); | |
643 turl_service_->Add(new TemplateURL(profile, backup_url->data())); | |
644 AddAndSetDefault(current_url); | |
645 | |
646 scoped_ptr<BaseSettingChange> change( | |
647 CreateDefaultSearchProviderChange(current_url, backup_url)); | |
648 ASSERT_TRUE(change.get()); | |
649 ASSERT_TRUE(change->Init(profile)); | |
650 | |
651 // Verify that backup is active. | |
652 EXPECT_EQ(FindTemplateURL(http_example_info), | |
653 turl_service_->GetDefaultSearchProvider()); | |
654 | |
655 turl_service_->Remove(current_url); | |
656 | |
657 // Verify that text messages altered after removing |current_url|. | |
658 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | |
659 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); | |
660 EXPECT_EQ(GetKeepSearchButtonText(example_info), | |
661 change->GetDiscardButtonText()); | |
662 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
663 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
664 | |
665 // Verify that search engine settings are opened by Apply. | |
666 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | |
667 change->Apply(browser()); | |
668 EXPECT_EQ(FindTemplateURL(http_example_info), | |
669 turl_service_->GetDefaultSearchProvider()); | |
670 | |
671 // Discard does nothing - backup was already active. | |
672 change->Discard(browser()); | |
673 EXPECT_EQ(FindTemplateURL(http_example_info), | |
674 turl_service_->GetDefaultSearchProvider()); | |
675 } | |
676 | |
677 } // namespace protector | |
OLD | NEW |