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

Side by Side Diff: chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc

Issue 62193002: Integrate UI and reset logic into AutomaticProfileResetter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clean-up. Created 7 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/values_test_util.h" 19 #include "base/test/values_test_util.h"
19 #include "base/values.h" 20 #include "base/values.h"
21 #include "chrome/app/chrome_command_ids.h"
20 #include "chrome/browser/chrome_notification_types.h" 22 #include "chrome/browser/chrome_notification_types.h"
23 #include "chrome/browser/extensions/extension_service_unittest.h"
24 #include "chrome/browser/google/google_util.h"
25 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
26 #include "chrome/browser/profile_resetter/profile_reset_global_error.h"
21 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 27 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
22 #include "chrome/browser/search_engines/template_url_service.h" 28 #include "chrome/browser/search_engines/template_url_service.h"
23 #include "chrome/browser/search_engines/template_url_service_factory.h" 29 #include "chrome/browser/search_engines/template_url_service_factory.h"
24 #include "chrome/browser/search_engines/template_url_service_test_util.h" 30 #include "chrome/browser/search_engines/template_url_service_test_util.h"
31 #include "chrome/browser/ui/global_error/global_error.h"
32 #include "chrome/browser/ui/global_error/global_error_service.h"
33 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
25 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h" 35 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h" 36 #include "chrome/test/base/testing_profile.h"
28 #include "content/public/browser/notification_service.h" 37 #include "content/public/browser/notification_service.h"
38 #include "net/http/http_response_headers.h"
39 #include "net/url_request/test_url_fetcher_factory.h"
29 #include "testing/gmock/include/gmock/gmock.h" 40 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
31 42
32 #if defined(OS_WIN) 43 #if defined(OS_WIN)
33 #include "chrome/browser/enumerate_modules_model_win.h" 44 #include "chrome/browser/enumerate_modules_model_win.h"
34 #endif 45 #endif
35 46
36 namespace { 47 namespace {
37 48
38 // Test fixtures ------------------------------------------------------------- 49 const char kTestBrandcode[] = "FOOBAR";
39 50
40 class AutomaticProfileResetterDelegateTest : public testing::Test { 51 const char kTestHomepage[] = "http://google.com";
41 protected: 52 const char kTestBrandedHomepage[] = "http://example.com";
42 AutomaticProfileResetterDelegateTest() {}
43 virtual ~AutomaticProfileResetterDelegateTest() {}
44 53
45 virtual void SetUp() OVERRIDE { 54 const ProfileResetter::ResettableFlags kResettableAspectsForTest =
46 resetter_delegate_.reset(new AutomaticProfileResetterDelegateImpl(NULL)); 55 ProfileResetter::ALL & ~ProfileResetter::COOKIES_AND_SITE_DATA;
47 }
48 56
49 virtual void TearDown() OVERRIDE { resetter_delegate_.reset(); } 57 // Helpers -------------------------------------------------------------------
50 58
51 AutomaticProfileResetterDelegate* resetter_delegate() { 59 // A testing version of the AutomaticProfileResetterDelegate that differs from
52 return resetter_delegate_.get(); 60 // the real one only in that it has its feedback reporting mocked out, and it
61 // will not reset COOKIES_AND_SITE_DATA, due to difficulties to set up some
62 // required URLRequestContexts in unit tests.
63 class AutomaticProfileResetterDelegateUnderTest
64 : public AutomaticProfileResetterDelegateImpl {
65 public:
66 explicit AutomaticProfileResetterDelegateUnderTest(Profile* profile)
67 : AutomaticProfileResetterDelegateImpl(
68 profile, kResettableAspectsForTest) {}
69 virtual ~AutomaticProfileResetterDelegateUnderTest() {}
70
71 MOCK_CONST_METHOD1(SendFeedback, void(const std::string&));
72
73 private:
74 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateUnderTest);
75 };
76
77 class MockCallbackTarget {
78 public:
79 MockCallbackTarget() {}
80 ~MockCallbackTarget() {}
81
82 MOCK_CONST_METHOD0(Run, void(void));
83
84 base::Closure CreateClosure() {
85 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this));
53 } 86 }
54 87
55 private: 88 private:
56 content::TestBrowserThreadBundle thread_bundle_; 89 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget);
57 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
58
59 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest);
60 }; 90 };
61 91
62 class AutomaticProfileResetterDelegateTestTemplateURLs : public testing::Test {
63 protected:
64 AutomaticProfileResetterDelegateTestTemplateURLs() {}
65 virtual ~AutomaticProfileResetterDelegateTestTemplateURLs() {}
66
67 virtual void SetUp() OVERRIDE {
68 test_util_.SetUp();
69 resetter_delegate_.reset(
70 new AutomaticProfileResetterDelegateImpl(test_util_.model()));
71 }
72
73 virtual void TearDown() OVERRIDE {
74 resetter_delegate_.reset();
75 test_util_.TearDown();
76 }
77
78 TestingProfile* profile() { return test_util_.profile(); }
79
80 AutomaticProfileResetterDelegate* resetter_delegate() {
81 return resetter_delegate_.get();
82 }
83
84 scoped_ptr<TemplateURL> CreateTestTemplateURL() {
85 TemplateURLData data;
86
87 data.SetURL("http://example.com/search?q={searchTerms}");
88 data.suggestions_url = "http://example.com/suggest?q={searchTerms}";
89 data.instant_url = "http://example.com/instant?q={searchTerms}";
90 data.image_url = "http://example.com/image?q={searchTerms}";
91 data.search_url_post_params = "search-post-params";
92 data.suggestions_url_post_params = "suggest-post-params";
93 data.instant_url_post_params = "instant-post-params";
94 data.image_url_post_params = "image-post-params";
95
96 data.favicon_url = GURL("http://example.com/favicon.ico");
97 data.new_tab_url = "http://example.com/newtab.html";
98 data.alternate_urls.push_back("http://example.com/s?q={searchTerms}");
99
100 data.short_name = base::ASCIIToUTF16("name");
101 data.SetKeyword(base::ASCIIToUTF16("keyword"));
102 data.search_terms_replacement_key = "search-terms-replacment-key";
103 data.prepopulate_id = 42;
104 data.input_encodings.push_back("UTF-8");
105 data.safe_for_autoreplace = true;
106
107 return scoped_ptr<TemplateURL>(new TemplateURL(profile(), data));
108 }
109
110 TemplateURLServiceTestUtil test_util_;
111
112 private:
113 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
114
115 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTestTemplateURLs);
116 };
117
118 // Helper classes and functions ----------------------------------------------
119
120 // Returns the details of the default search provider from |prefs| in a format 92 // Returns the details of the default search provider from |prefs| in a format
121 // suitable for usage as |expected_details| in ExpectDetailsMatch(). 93 // suitable for usage as |expected_details| in ExpectDetailsMatch().
122 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetailsFromPrefs( 94 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetailsFromPrefs(
123 const PrefService* prefs) { 95 const PrefService* prefs) {
124 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; 96 const char kDefaultSearchProviderPrefix[] = "default_search_provider";
125 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( 97 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
126 prefs->GetPreferenceValues()); 98 prefs->GetPreferenceValues());
127 const base::DictionaryValue* dsp_details = NULL; 99 const base::DictionaryValue* dsp_details = NULL;
128 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary( 100 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary(
129 kDefaultSearchProviderPrefix, &dsp_details)); 101 kDefaultSearchProviderPrefix, &dsp_details));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 actual_encodings_vector.push_back(encoding); 135 actual_encodings_vector.push_back(encoding);
164 } 136 }
165 EXPECT_EQ(expected_encodings, JoinString(actual_encodings_vector, ';')); 137 EXPECT_EQ(expected_encodings, JoinString(actual_encodings_vector, ';'));
166 } else { 138 } else {
167 // Everything else is the same format. 139 // Everything else is the same format.
168 EXPECT_TRUE(actual_value->Equals(expected_value)); 140 EXPECT_TRUE(actual_value->Equals(expected_value));
169 } 141 }
170 } 142 }
171 } 143 }
172 144
173 class MockCallbackTarget { 145 // If |simulate_failure| is false, then replies to the pending request on
174 public: 146 // |fetcher| with a brandcoded config that only specifies a home page URL.
175 MockCallbackTarget() {} 147 // If |simulate_failure| is true, replies with 404.
176 ~MockCallbackTarget() {} 148 void ServicePendingBrancodedConfigFetch(net::TestURLFetcher* fetcher,
149 bool simulate_failure) {
150 const char kBrandcodedXmlSettings[] =
151 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
152 "<response protocol=\"3.0\" server=\"prod\">"
153 "<app appid=\"{8A69D345-D564-463C-AFF1-A69D9E530F96}\" status=\"ok\">"
154 "<data index=\"skipfirstrunui-importsearch-defaultbrowser\" "
155 "name=\"install\" status=\"ok\">"
156 "{\"homepage\" : \"$1\"}"
157 "</data>"
158 "</app>"
159 "</response>";
177 160
178 MOCK_CONST_METHOD0(Run, void(void)); 161 fetcher->set_response_code(simulate_failure ? 404 : 200);
162 scoped_refptr<net::HttpResponseHeaders> response_headers(
163 new net::HttpResponseHeaders(""));
164 response_headers->AddHeader("Content-Type: text/xml");
165 fetcher->set_response_headers(response_headers);
166 if (!simulate_failure) {
167 std::string response(kBrandcodedXmlSettings);
168 size_t placeholder_index = response.find("$1");
169 ASSERT_NE(std::string::npos, placeholder_index);
170 response.replace(placeholder_index, 2, kTestBrandedHomepage);
171 fetcher->SetResponseString(response);
172 }
173 fetcher->delegate()->OnURLFetchComplete(fetcher);
174 }
179 175
180 base::Closure CreateClosure() { 176
181 return base::Closure( 177 // Test fixture --------------------------------------------------------------
182 base::Bind(&MockCallbackTarget::Run, base::Unretained(this))); 178
179 // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService,
180 // we then add the TemplateURLService, so the ProfileResetter can be exercised.
181 class AutomaticProfileResetterDelegateTest
182 : public ExtensionServiceTestBase,
183 public TemplateURLServiceTestUtilBase {
184 protected:
185 AutomaticProfileResetterDelegateTest() {}
186 virtual ~AutomaticProfileResetterDelegateTest() {}
187
188 virtual void SetUp() OVERRIDE {
189 ExtensionServiceTestBase::SetUp();
190 ExtensionServiceInitParams params = CreateDefaultInitParams();
191 params.pref_file.clear(); // Prescribes a TestingPrefService to be created.
192 InitializeExtensionService(params);
193 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
194 resetter_delegate_.reset(
195 new AutomaticProfileResetterDelegateUnderTest(profile()));
183 } 196 }
184 197
198 virtual void TearDown() OVERRIDE {
199 resetter_delegate_.reset();
200 ExtensionServiceTestBase::TearDown();
201 }
202
203 scoped_ptr<TemplateURL> CreateTestTemplateURL() {
204 TemplateURLData data;
205
206 data.SetURL("http://example.com/search?q={searchTerms}");
207 data.suggestions_url = "http://example.com/suggest?q={searchTerms}";
208 data.instant_url = "http://example.com/instant?q={searchTerms}";
209 data.image_url = "http://example.com/image?q={searchTerms}";
210 data.search_url_post_params = "search-post-params";
211 data.suggestions_url_post_params = "suggest-post-params";
212 data.instant_url_post_params = "instant-post-params";
213 data.image_url_post_params = "image-post-params";
214
215 data.favicon_url = GURL("http://example.com/favicon.ico");
216 data.new_tab_url = "http://example.com/newtab.html";
217 data.alternate_urls.push_back("http://example.com/s?q={searchTerms}");
218
219 data.short_name = base::ASCIIToUTF16("name");
220 data.SetKeyword(base::ASCIIToUTF16("keyword"));
221 data.search_terms_replacement_key = "search-terms-replacment-key";
222 data.prepopulate_id = 42;
223 data.input_encodings.push_back("UTF-8");
224 data.safe_for_autoreplace = true;
225
226 return scoped_ptr<TemplateURL>(new TemplateURL(profile(), data));
227 }
228
229 void ExpectNoPendingBrandcodedConfigFetch() {
230 EXPECT_FALSE(test_url_fetcher_factory_.GetFetcherByID(0));
231 }
232
233 void ExpectAndServicePendingBrandcodedConfigFetch(bool simulate_failure) {
234 net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0);
235 ASSERT_TRUE(fetcher);
236 EXPECT_THAT(fetcher->upload_data(),
237 testing::HasSubstr(kTestBrandcode));
238 ServicePendingBrancodedConfigFetch(fetcher, simulate_failure);
239 }
240
241 void ExpectResetPromptState(bool active) {
242 GlobalErrorService* global_error_service =
243 GlobalErrorServiceFactory::GetForProfile(profile());
244 GlobalError* global_error = global_error_service->
245 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE);
246 EXPECT_EQ(active, !!global_error);
247 }
248
249 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() {
250 return resetter_delegate_.get();
251 }
252
253 // TemplateURLServiceTestUtilBase:
254 virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); }
255
185 private: 256 private:
186 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); 257 net::TestURLFetcherFactory test_url_fetcher_factory_;
258 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_;
259
260 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest);
187 }; 261 };
188 262
263
189 // Tests --------------------------------------------------------------------- 264 // Tests ---------------------------------------------------------------------
190 265
191 TEST_F(AutomaticProfileResetterDelegateTest, 266 TEST_F(AutomaticProfileResetterDelegateTest,
192 TriggerAndWaitOnModuleEnumeration) { 267 TriggerAndWaitOnModuleEnumeration) {
193 testing::StrictMock<MockCallbackTarget> mock_target;
194
195 // Expect ready_callback to be called just after the modules have been 268 // Expect ready_callback to be called just after the modules have been
196 // enumerated. Fail if it is not called. Note: as the EnumerateModulesModel is 269 // enumerated. Fail if it is not called. Note: as the EnumerateModulesModel is
197 // a global singleton, the callback might be invoked immediately if another 270 // a global singleton, the callback might be invoked immediately if another
198 // test-case (e.g. the one below) has already performed module enumeration. 271 // test-case (e.g. the one below) has already performed module enumeration.
272 testing::StrictMock<MockCallbackTarget> mock_target;
199 EXPECT_CALL(mock_target, Run()); 273 EXPECT_CALL(mock_target, Run());
200 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( 274 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
201 mock_target.CreateClosure()); 275 mock_target.CreateClosure());
202 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); 276 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
203 base::RunLoop().RunUntilIdle(); 277 base::RunLoop().RunUntilIdle();
204 278
205 testing::Mock::VerifyAndClearExpectations(&mock_target); 279 testing::Mock::VerifyAndClearExpectations(&mock_target);
206 280
207 // Expect ready_callback to be posted immediately when the modules have 281 // Expect ready_callback to be posted immediately when the modules have
208 // already been enumerated. 282 // already been enumerated.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 316
243 ASSERT_TRUE((*it)->GetAsString(&digest_hex)); 317 ASSERT_TRUE((*it)->GetAsString(&digest_hex));
244 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); 318 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw));
245 EXPECT_EQ(16u, digest_raw.size()); 319 EXPECT_EQ(16u, digest_raw.size());
246 } 320 }
247 #if defined(OS_WIN) 321 #if defined(OS_WIN)
248 EXPECT_LE(1u, module_name_digests->GetSize()); 322 EXPECT_LE(1u, module_name_digests->GetSize());
249 #endif 323 #endif
250 } 324 }
251 325
252 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, 326 TEST_F(AutomaticProfileResetterDelegateTest, LoadAndWaitOnTemplateURLService) {
253 LoadAndWaitOnTemplateURLService) {
254 testing::StrictMock<MockCallbackTarget> mock_target;
255
256 // Expect ready_callback to be called just after the template URL service gets 327 // Expect ready_callback to be called just after the template URL service gets
257 // initialized. Fail if it is not called, or called too early. 328 // initialized. Fail if it is not called, or called too early.
329 testing::StrictMock<MockCallbackTarget> mock_target;
258 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( 330 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
259 mock_target.CreateClosure()); 331 mock_target.CreateClosure());
260 base::RunLoop().RunUntilIdle(); 332 base::RunLoop().RunUntilIdle();
261 333
262 EXPECT_CALL(mock_target, Run()); 334 EXPECT_CALL(mock_target, Run());
263 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); 335 resetter_delegate()->LoadTemplateURLServiceIfNeeded();
264 base::RunLoop().RunUntilIdle(); 336 base::RunLoop().RunUntilIdle();
265 337
266 testing::Mock::VerifyAndClearExpectations(&mock_target); 338 testing::Mock::VerifyAndClearExpectations(&mock_target);
267 339
268 // Expect ready_callback to be posted immediately when the template URL 340 // Expect ready_callback to be posted immediately when the template URL
269 // service is already initialized. 341 // service is already initialized.
270 EXPECT_CALL(mock_target, Run()); 342 EXPECT_CALL(mock_target, Run());
271 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( 343 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
272 mock_target.CreateClosure()); 344 mock_target.CreateClosure());
273 base::RunLoop().RunUntilIdle(); 345 base::RunLoop().RunUntilIdle();
274 346
275 testing::Mock::VerifyAndClearExpectations(&mock_target); 347 testing::Mock::VerifyAndClearExpectations(&mock_target);
276 348
277 // Expect ready_callback to be posted immediately even when the template URL 349 // Expect ready_callback to be posted immediately even when the template URL
278 // service had already been initialized when the delegate was constructed. 350 // service had already been initialized when the delegate was constructed.
279 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( 351 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
280 new AutomaticProfileResetterDelegateImpl(test_util_.model())); 352 new AutomaticProfileResetterDelegateImpl(profile(),
353 ProfileResetter::ALL));
281 354
282 EXPECT_CALL(mock_target, Run()); 355 EXPECT_CALL(mock_target, Run());
283 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( 356 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded(
284 mock_target.CreateClosure()); 357 mock_target.CreateClosure());
285 base::RunLoop().RunUntilIdle(); 358 base::RunLoop().RunUntilIdle();
286 } 359 }
287 360
288 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, 361 TEST_F(AutomaticProfileResetterDelegateTest,
289 DefaultSearchProviderDataWhenNotManaged) { 362 DefaultSearchProviderDataWhenNotManaged) {
290 TemplateURLService* template_url_service = test_util_.model(); 363 TemplateURLService* template_url_service =
291 test_util_.VerifyLoad(); 364 TemplateURLServiceFactory::GetForProfile(profile());
365 TemplateURLServiceTestUtilBase::VerifyLoad();
292 366
293 // Check that the "managed state" and the details returned by the delegate are 367 // Check that the "managed state" and the details returned by the delegate are
294 // correct. We verify the details against the data stored by 368 // correct. We verify the details against the data stored by
295 // TemplateURLService into Prefs. 369 // TemplateURLService into Prefs.
296 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); 370 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL());
297 TemplateURL* custom_dsp = owned_custom_dsp.get(); 371 TemplateURL* custom_dsp = owned_custom_dsp.get();
298 template_url_service->Add(owned_custom_dsp.release()); 372 template_url_service->Add(owned_custom_dsp.release());
299 template_url_service->SetDefaultSearchProvider(custom_dsp); 373 template_url_service->SetDefaultSearchProvider(custom_dsp);
300 374
301 PrefService* prefs = profile()->GetPrefs(); 375 PrefService* prefs = profile()->GetPrefs();
302 ASSERT_TRUE(prefs); 376 ASSERT_TRUE(prefs);
303 scoped_ptr<base::DictionaryValue> dsp_details( 377 scoped_ptr<base::DictionaryValue> dsp_details(
304 resetter_delegate()->GetDefaultSearchProviderDetails()); 378 resetter_delegate()->GetDefaultSearchProviderDetails());
305 scoped_ptr<base::DictionaryValue> expected_dsp_details( 379 scoped_ptr<base::DictionaryValue> expected_dsp_details(
306 GetDefaultSearchProviderDetailsFromPrefs(prefs)); 380 GetDefaultSearchProviderDetailsFromPrefs(prefs));
307 381
308 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); 382 ExpectDetailsMatch(*expected_dsp_details, *dsp_details);
309 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); 383 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
310 } 384 }
311 385
312 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, 386 TEST_F(AutomaticProfileResetterDelegateTest,
313 DefaultSearchProviderDataWhenManaged) { 387 DefaultSearchProviderDataWhenManaged) {
314 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; 388 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}";
315 const char kTestName[] = "name"; 389 const char kTestName[] = "name";
316 const char kTestKeyword[] = "keyword"; 390 const char kTestKeyword[] = "keyword";
317 391
318 test_util_.VerifyLoad(); 392 TemplateURLServiceTestUtilBase::VerifyLoad();
319 393
320 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); 394 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
321 395
322 // Set managed preferences to emulate a default search provider set by policy. 396 // Set managed preferences to emulate a default search provider set by policy.
323 test_util_.SetManagedDefaultSearchPreferences( 397 SetManagedDefaultSearchPreferences(
324 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), 398 true, kTestName, kTestKeyword, kTestSearchURL, std::string(),
325 std::string(), std::string(), std::string(), std::string()); 399 std::string(), std::string(), std::string(), std::string());
326 400
327 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); 401 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
328 scoped_ptr<base::DictionaryValue> dsp_details( 402 scoped_ptr<base::DictionaryValue> dsp_details(
329 resetter_delegate()->GetDefaultSearchProviderDetails()); 403 resetter_delegate()->GetDefaultSearchProviderDetails());
330 // Checking that all details are correct is already done by the above test. 404 // Checking that all details are correct is already done by the above test.
331 // Just make sure details are reported about the correct engine. 405 // Just make sure details are reported about the correct engine.
332 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); 406 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url");
333 407
334 // Set managed preferences to emulate that having a default search provider is 408 // Set managed preferences to emulate that having a default search provider is
335 // disabled by policy. 409 // disabled by policy.
336 test_util_.RemoveManagedDefaultSearchPreferences(); 410 RemoveManagedDefaultSearchPreferences();
337 test_util_.SetManagedDefaultSearchPreferences( 411 SetManagedDefaultSearchPreferences(
338 true, std::string(), std::string(), std::string(), std::string(), 412 true, std::string(), std::string(), std::string(), std::string(),
339 std::string(), std::string(), std::string(), std::string()); 413 std::string(), std::string(), std::string(), std::string());
340 414
341 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); 415 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails();
342 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); 416 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
343 EXPECT_TRUE(dsp_details->empty()); 417 EXPECT_TRUE(dsp_details->empty());
344 } 418 }
345 419
346 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, 420 TEST_F(AutomaticProfileResetterDelegateTest,
347 GetPrepopulatedSearchProvidersDetails) { 421 GetPrepopulatedSearchProvidersDetails) {
348 TemplateURLService* template_url_service = test_util_.model(); 422 TemplateURLService* template_url_service =
349 test_util_.VerifyLoad(); 423 TemplateURLServiceFactory::GetForProfile(profile());
424 TemplateURLServiceTestUtilBase::VerifyLoad();
350 425
351 scoped_ptr<base::ListValue> search_engines_details( 426 scoped_ptr<base::ListValue> search_engines_details(
352 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); 427 resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
353 428
354 // Do the same kind of verification as for GetDefaultSearchEngineDetails: 429 // Do the same kind of verification as for GetDefaultSearchEngineDetails:
355 // subsequently set each pre-populated engine as the default, so we can verify 430 // subsequently set each pre-populated engine as the default, so we can verify
356 // that the details returned by the delegate about one particular engine are 431 // that the details returned by the delegate about one particular engine are
357 // correct in comparison to what has been stored to the Prefs. 432 // correct in comparison to what has been stored to the Prefs.
358 std::vector<TemplateURL*> prepopulated_engines = 433 std::vector<TemplateURL*> prepopulated_engines =
359 template_url_service->GetTemplateURLs(); 434 template_url_service->GetTemplateURLs();
(...skipping 12 matching lines...) Expand all
372 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); 447 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]);
373 448
374 PrefService* prefs = profile()->GetPrefs(); 449 PrefService* prefs = profile()->GetPrefs();
375 ASSERT_TRUE(prefs); 450 ASSERT_TRUE(prefs);
376 scoped_ptr<base::DictionaryValue> expected_dsp_details( 451 scoped_ptr<base::DictionaryValue> expected_dsp_details(
377 GetDefaultSearchProviderDetailsFromPrefs(prefs)); 452 GetDefaultSearchProviderDetailsFromPrefs(prefs));
378 ExpectDetailsMatch(*expected_dsp_details, *details); 453 ExpectDetailsMatch(*expected_dsp_details, *details);
379 } 454 }
380 } 455 }
381 456
457 TEST_F(AutomaticProfileResetterDelegateTest,
458 FetchAndWaitOnDefaultSettingsVanilla) {
459 // Expect ready_callback to be called just after empty brandcoded settings
460 // are loaded, given this is a vanilla build. Fail if it is not called, or
461 // called too early.
462 testing::StrictMock<MockCallbackTarget> mock_target;
463 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
464 mock_target.CreateClosure());
465 base::RunLoop().RunUntilIdle();
466 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults());
467
468 EXPECT_CALL(mock_target, Run());
469 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
470 base::RunLoop().RunUntilIdle();
471 ExpectNoPendingBrandcodedConfigFetch();
472
473 testing::Mock::VerifyAndClearExpectations(&mock_target);
474 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults());
475
476 // Expect ready_callback to be posted immediately when the brandcoded settings
477 // have already been loaded.
478 EXPECT_CALL(mock_target, Run());
479 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
480 mock_target.CreateClosure());
481 base::RunLoop().RunUntilIdle();
482
483 // No test for a new instance of AutomaticProfileResetterDelegate. That will
484 // need to fetch the brandcoded settings again.
485 }
486
487 TEST_F(AutomaticProfileResetterDelegateTest,
488 FetchAndWaitOnDefaultSettingsBranded) {
489 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode);
490
491 // Expect ready_callback to be called just after the brandcoded settings are
492 // downloaded. Fail if it is not called, or called too early.
493 testing::StrictMock<MockCallbackTarget> mock_target;
494 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
495 mock_target.CreateClosure());
496 base::RunLoop().RunUntilIdle();
497 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults());
498
499 EXPECT_CALL(mock_target, Run());
500 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
501 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
502 base::RunLoop().RunUntilIdle();
503
504 testing::Mock::VerifyAndClearExpectations(&mock_target);
505 const BrandcodedDefaultSettings* brandcoded_defaults =
506 resetter_delegate()->brandcoded_defaults();
507 ASSERT_TRUE(brandcoded_defaults);
508 std::string homepage_url;
509 EXPECT_TRUE(brandcoded_defaults->GetHomepage(&homepage_url));
510 EXPECT_EQ(kTestBrandedHomepage, homepage_url);
511
512 // Expect ready_callback to be posted immediately when the brandcoded settings
513 // have already been downloaded.
514 EXPECT_CALL(mock_target, Run());
515 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
516 mock_target.CreateClosure());
517 base::RunLoop().RunUntilIdle();
518 }
519
520 TEST_F(AutomaticProfileResetterDelegateTest,
521 FetchAndWaitOnDefaultSettingsBrandedFailure) {
522 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode);
523
524 // Expect ready_callback to be called just after the brandcoded settings have
525 // failed to download. Fail if it is not called, or called too early.
526 testing::StrictMock<MockCallbackTarget> mock_target;
527 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
528 mock_target.CreateClosure());
529 base::RunLoop().RunUntilIdle();
530
531 EXPECT_CALL(mock_target, Run());
532 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
533 ExpectAndServicePendingBrandcodedConfigFetch(true /*simulate_failure*/);
534 base::RunLoop().RunUntilIdle();
535
536 testing::Mock::VerifyAndClearExpectations(&mock_target);
537 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults());
538
539 // Expect ready_callback to be posted immediately when the brandcoded settings
540 // have already been attempted to be downloaded, but failed.
541 EXPECT_CALL(mock_target, Run());
542 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
543 mock_target.CreateClosure());
544 base::RunLoop().RunUntilIdle();
545 }
546
547 TEST_F(AutomaticProfileResetterDelegateTest, TriggerReset) {
548 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode);
549
550 PrefService* prefs = profile()->GetPrefs();
551 DCHECK(prefs);
552 prefs->SetString(prefs::kHomePage, kTestHomepage);
553
554 testing::StrictMock<MockCallbackTarget> mock_target;
555 EXPECT_CALL(mock_target, Run());
556 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0);
557 resetter_delegate()->TriggerProfileSettingsReset(
558 false /*send_feedback*/, mock_target.CreateClosure());
559 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
560 base::RunLoop().RunUntilIdle();
561
562 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage));
563 }
564
565 TEST_F(AutomaticProfileResetterDelegateTest,
566 TriggerResetWithDefaultSettingsAlreadyLoaded) {
567 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode);
568
569 PrefService* prefs = profile()->GetPrefs();
570 DCHECK(prefs);
571 prefs->SetString(prefs::kHomePage, kTestHomepage);
572
573 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
574 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
575 base::RunLoop().RunUntilIdle();
576
577 testing::StrictMock<MockCallbackTarget> mock_target;
578 EXPECT_CALL(mock_target, Run());
579 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0);
580 resetter_delegate()->TriggerProfileSettingsReset(
581 false /*send_feedback*/, mock_target.CreateClosure());
582 base::RunLoop().RunUntilIdle();
583
584 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage));
585 }
586
587 TEST_F(AutomaticProfileResetterDelegateTest,
588 TriggerResetAndSendFeedback) {
589 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode);
590
591 PrefService* prefs = profile()->GetPrefs();
592 DCHECK(prefs);
593 prefs->SetString(prefs::kHomePage, kTestHomepage);
594
595 testing::StrictMock<MockCallbackTarget> mock_target;
596 EXPECT_CALL(mock_target, Run());
597 EXPECT_CALL(*resetter_delegate(),
598 SendFeedback(testing::HasSubstr(kTestHomepage)));
599
600 resetter_delegate()->TriggerProfileSettingsReset(
601 true /*send_feedback*/, mock_target.CreateClosure());
602 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
603 base::RunLoop().RunUntilIdle();
604 }
605
606 TEST_F(AutomaticProfileResetterDelegateTest, ShowAndDismissPrompt) {
607 resetter_delegate()->TriggerPrompt();
608 if (ProfileResetGlobalError::IsSupportedOnPlatform())
609 ExpectResetPromptState(true /*active*/);
610 else
611 ExpectResetPromptState(false /*active*/);
612 resetter_delegate()->DismissPrompt();
613 ExpectResetPromptState(false /*active*/);
614 resetter_delegate()->DismissPrompt();
615 }
616
382 } // namespace 617 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698