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