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" |
| 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 Loading... |
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. |
209 EXPECT_CALL(mock_target, Run()); | 283 EXPECT_CALL(mock_target, Run()); |
210 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( | 284 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( |
211 mock_target.CreateClosure()); | 285 mock_target.CreateClosure()); |
212 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
213 | 287 |
214 #if defined(OS_WIN) | 288 #if defined(OS_WIN) |
215 testing::Mock::VerifyAndClearExpectations(&mock_target); | 289 testing::Mock::VerifyAndClearExpectations(&mock_target); |
216 | 290 |
217 // Expect ready_callback to be posted immediately even when the modules had | 291 // Expect ready_callback to be posted immediately even when the modules had |
218 // already been enumerated when the delegate was constructed. | 292 // already been enumerated when the delegate was constructed. |
219 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | 293 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( |
220 new AutomaticProfileResetterDelegateImpl(NULL)); | 294 new AutomaticProfileResetterDelegateImpl(profile(), |
| 295 ProfileResetter::ALL)); |
221 | 296 |
222 EXPECT_CALL(mock_target, Run()); | 297 EXPECT_CALL(mock_target, Run()); |
223 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated( | 298 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated( |
224 mock_target.CreateClosure()); | 299 mock_target.CreateClosure()); |
225 base::RunLoop().RunUntilIdle(); | 300 base::RunLoop().RunUntilIdle(); |
226 #endif | 301 #endif |
227 } | 302 } |
228 | 303 |
229 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { | 304 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { |
230 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | 305 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); |
(...skipping 11 matching lines...) Expand all Loading... |
242 | 317 |
243 ASSERT_TRUE((*it)->GetAsString(&digest_hex)); | 318 ASSERT_TRUE((*it)->GetAsString(&digest_hex)); |
244 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); | 319 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); |
245 EXPECT_EQ(16u, digest_raw.size()); | 320 EXPECT_EQ(16u, digest_raw.size()); |
246 } | 321 } |
247 #if defined(OS_WIN) | 322 #if defined(OS_WIN) |
248 EXPECT_LE(1u, module_name_digests->GetSize()); | 323 EXPECT_LE(1u, module_name_digests->GetSize()); |
249 #endif | 324 #endif |
250 } | 325 } |
251 | 326 |
252 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | 327 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 | 328 // 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. | 329 // initialized. Fail if it is not called, or called too early. |
| 330 testing::StrictMock<MockCallbackTarget> mock_target; |
258 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | 331 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( |
259 mock_target.CreateClosure()); | 332 mock_target.CreateClosure()); |
260 base::RunLoop().RunUntilIdle(); | 333 base::RunLoop().RunUntilIdle(); |
261 | 334 |
262 EXPECT_CALL(mock_target, Run()); | 335 EXPECT_CALL(mock_target, Run()); |
263 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); | 336 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); |
264 base::RunLoop().RunUntilIdle(); | 337 base::RunLoop().RunUntilIdle(); |
265 | 338 |
266 testing::Mock::VerifyAndClearExpectations(&mock_target); | 339 testing::Mock::VerifyAndClearExpectations(&mock_target); |
267 | 340 |
268 // Expect ready_callback to be posted immediately when the template URL | 341 // Expect ready_callback to be posted immediately when the template URL |
269 // service is already initialized. | 342 // service is already initialized. |
270 EXPECT_CALL(mock_target, Run()); | 343 EXPECT_CALL(mock_target, Run()); |
271 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | 344 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( |
272 mock_target.CreateClosure()); | 345 mock_target.CreateClosure()); |
273 base::RunLoop().RunUntilIdle(); | 346 base::RunLoop().RunUntilIdle(); |
274 | 347 |
275 testing::Mock::VerifyAndClearExpectations(&mock_target); | 348 testing::Mock::VerifyAndClearExpectations(&mock_target); |
276 | 349 |
277 // Expect ready_callback to be posted immediately even when the template URL | 350 // Expect ready_callback to be posted immediately even when the template URL |
278 // service had already been initialized when the delegate was constructed. | 351 // service had already been initialized when the delegate was constructed. |
279 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | 352 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( |
280 new AutomaticProfileResetterDelegateImpl(test_util_.model())); | 353 new AutomaticProfileResetterDelegateImpl(profile(), |
| 354 ProfileResetter::ALL)); |
281 | 355 |
282 EXPECT_CALL(mock_target, Run()); | 356 EXPECT_CALL(mock_target, Run()); |
283 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( | 357 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( |
284 mock_target.CreateClosure()); | 358 mock_target.CreateClosure()); |
285 base::RunLoop().RunUntilIdle(); | 359 base::RunLoop().RunUntilIdle(); |
286 } | 360 } |
287 | 361 |
288 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | 362 TEST_F(AutomaticProfileResetterDelegateTest, |
289 DefaultSearchProviderDataWhenNotManaged) { | 363 DefaultSearchProviderDataWhenNotManaged) { |
290 TemplateURLService* template_url_service = test_util_.model(); | 364 TemplateURLService* template_url_service = |
291 test_util_.VerifyLoad(); | 365 TemplateURLServiceFactory::GetForProfile(profile()); |
| 366 TemplateURLServiceTestUtilBase::VerifyLoad(); |
292 | 367 |
293 // Check that the "managed state" and the details returned by the delegate are | 368 // Check that the "managed state" and the details returned by the delegate are |
294 // correct. We verify the details against the data stored by | 369 // correct. We verify the details against the data stored by |
295 // TemplateURLService into Prefs. | 370 // TemplateURLService into Prefs. |
296 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 371 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
297 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 372 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
298 template_url_service->Add(owned_custom_dsp.release()); | 373 template_url_service->Add(owned_custom_dsp.release()); |
299 template_url_service->SetDefaultSearchProvider(custom_dsp); | 374 template_url_service->SetDefaultSearchProvider(custom_dsp); |
300 | 375 |
301 PrefService* prefs = profile()->GetPrefs(); | 376 PrefService* prefs = profile()->GetPrefs(); |
302 ASSERT_TRUE(prefs); | 377 ASSERT_TRUE(prefs); |
303 scoped_ptr<base::DictionaryValue> dsp_details( | 378 scoped_ptr<base::DictionaryValue> dsp_details( |
304 resetter_delegate()->GetDefaultSearchProviderDetails()); | 379 resetter_delegate()->GetDefaultSearchProviderDetails()); |
305 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 380 scoped_ptr<base::DictionaryValue> expected_dsp_details( |
306 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 381 GetDefaultSearchProviderDetailsFromPrefs(prefs)); |
307 | 382 |
308 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 383 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
309 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 384 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
310 } | 385 } |
311 | 386 |
312 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | 387 TEST_F(AutomaticProfileResetterDelegateTest, |
313 DefaultSearchProviderDataWhenManaged) { | 388 DefaultSearchProviderDataWhenManaged) { |
314 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 389 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
315 const char kTestName[] = "name"; | 390 const char kTestName[] = "name"; |
316 const char kTestKeyword[] = "keyword"; | 391 const char kTestKeyword[] = "keyword"; |
317 | 392 |
318 test_util_.VerifyLoad(); | 393 TemplateURLServiceTestUtilBase::VerifyLoad(); |
319 | 394 |
320 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 395 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
321 | 396 |
322 // Set managed preferences to emulate a default search provider set by policy. | 397 // Set managed preferences to emulate a default search provider set by policy. |
323 test_util_.SetManagedDefaultSearchPreferences( | 398 SetManagedDefaultSearchPreferences( |
324 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), | 399 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), |
325 std::string(), std::string(), std::string(), std::string()); | 400 std::string(), std::string(), std::string(), std::string()); |
326 | 401 |
327 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 402 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
328 scoped_ptr<base::DictionaryValue> dsp_details( | 403 scoped_ptr<base::DictionaryValue> dsp_details( |
329 resetter_delegate()->GetDefaultSearchProviderDetails()); | 404 resetter_delegate()->GetDefaultSearchProviderDetails()); |
330 // Checking that all details are correct is already done by the above test. | 405 // Checking that all details are correct is already done by the above test. |
331 // Just make sure details are reported about the correct engine. | 406 // Just make sure details are reported about the correct engine. |
332 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | 407 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); |
333 | 408 |
334 // Set managed preferences to emulate that having a default search provider is | 409 // Set managed preferences to emulate that having a default search provider is |
335 // disabled by policy. | 410 // disabled by policy. |
336 test_util_.RemoveManagedDefaultSearchPreferences(); | 411 RemoveManagedDefaultSearchPreferences(); |
337 test_util_.SetManagedDefaultSearchPreferences( | 412 SetManagedDefaultSearchPreferences( |
338 true, std::string(), std::string(), std::string(), std::string(), | 413 true, std::string(), std::string(), std::string(), std::string(), |
339 std::string(), std::string(), std::string(), std::string()); | 414 std::string(), std::string(), std::string(), std::string()); |
340 | 415 |
341 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | 416 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); |
342 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 417 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
343 EXPECT_TRUE(dsp_details->empty()); | 418 EXPECT_TRUE(dsp_details->empty()); |
344 } | 419 } |
345 | 420 |
346 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | 421 TEST_F(AutomaticProfileResetterDelegateTest, |
347 GetPrepopulatedSearchProvidersDetails) { | 422 GetPrepopulatedSearchProvidersDetails) { |
348 TemplateURLService* template_url_service = test_util_.model(); | 423 TemplateURLService* template_url_service = |
349 test_util_.VerifyLoad(); | 424 TemplateURLServiceFactory::GetForProfile(profile()); |
| 425 TemplateURLServiceTestUtilBase::VerifyLoad(); |
350 | 426 |
351 scoped_ptr<base::ListValue> search_engines_details( | 427 scoped_ptr<base::ListValue> search_engines_details( |
352 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | 428 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); |
353 | 429 |
354 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | 430 // Do the same kind of verification as for GetDefaultSearchEngineDetails: |
355 // subsequently set each pre-populated engine as the default, so we can verify | 431 // 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 | 432 // that the details returned by the delegate about one particular engine are |
357 // correct in comparison to what has been stored to the Prefs. | 433 // correct in comparison to what has been stored to the Prefs. |
358 std::vector<TemplateURL*> prepopulated_engines = | 434 std::vector<TemplateURL*> prepopulated_engines = |
359 template_url_service->GetTemplateURLs(); | 435 template_url_service->GetTemplateURLs(); |
(...skipping 12 matching lines...) Expand all Loading... |
372 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); | 448 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); |
373 | 449 |
374 PrefService* prefs = profile()->GetPrefs(); | 450 PrefService* prefs = profile()->GetPrefs(); |
375 ASSERT_TRUE(prefs); | 451 ASSERT_TRUE(prefs); |
376 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 452 scoped_ptr<base::DictionaryValue> expected_dsp_details( |
377 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 453 GetDefaultSearchProviderDetailsFromPrefs(prefs)); |
378 ExpectDetailsMatch(*expected_dsp_details, *details); | 454 ExpectDetailsMatch(*expected_dsp_details, *details); |
379 } | 455 } |
380 } | 456 } |
381 | 457 |
| 458 TEST_F(AutomaticProfileResetterDelegateTest, |
| 459 FetchAndWaitOnDefaultSettingsVanilla) { |
| 460 google_util::BrandForTesting scoped_brand_for_testing((std::string())); |
| 461 |
| 462 // Expect ready_callback to be called just after empty brandcoded settings |
| 463 // are loaded, given this is a vanilla build. Fail if it is not called, or |
| 464 // called too early. |
| 465 testing::StrictMock<MockCallbackTarget> mock_target; |
| 466 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 467 mock_target.CreateClosure()); |
| 468 base::RunLoop().RunUntilIdle(); |
| 469 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults()); |
| 470 |
| 471 EXPECT_CALL(mock_target, Run()); |
| 472 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); |
| 473 base::RunLoop().RunUntilIdle(); |
| 474 ExpectNoPendingBrandcodedConfigFetch(); |
| 475 |
| 476 testing::Mock::VerifyAndClearExpectations(&mock_target); |
| 477 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults()); |
| 478 |
| 479 // Expect ready_callback to be posted immediately when the brandcoded settings |
| 480 // have already been loaded. |
| 481 EXPECT_CALL(mock_target, Run()); |
| 482 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 483 mock_target.CreateClosure()); |
| 484 base::RunLoop().RunUntilIdle(); |
| 485 |
| 486 // No test for a new instance of AutomaticProfileResetterDelegate. That will |
| 487 // need to fetch the brandcoded settings again. |
| 488 } |
| 489 |
| 490 TEST_F(AutomaticProfileResetterDelegateTest, |
| 491 FetchAndWaitOnDefaultSettingsBranded) { |
| 492 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); |
| 493 |
| 494 // Expect ready_callback to be called just after the brandcoded settings are |
| 495 // downloaded. Fail if it is not called, or called too early. |
| 496 testing::StrictMock<MockCallbackTarget> mock_target; |
| 497 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 498 mock_target.CreateClosure()); |
| 499 base::RunLoop().RunUntilIdle(); |
| 500 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults()); |
| 501 |
| 502 EXPECT_CALL(mock_target, Run()); |
| 503 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); |
| 504 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); |
| 505 base::RunLoop().RunUntilIdle(); |
| 506 |
| 507 testing::Mock::VerifyAndClearExpectations(&mock_target); |
| 508 const BrandcodedDefaultSettings* brandcoded_defaults = |
| 509 resetter_delegate()->brandcoded_defaults(); |
| 510 ASSERT_TRUE(brandcoded_defaults); |
| 511 std::string homepage_url; |
| 512 EXPECT_TRUE(brandcoded_defaults->GetHomepage(&homepage_url)); |
| 513 EXPECT_EQ(kTestBrandedHomepage, homepage_url); |
| 514 |
| 515 // Expect ready_callback to be posted immediately when the brandcoded settings |
| 516 // have already been downloaded. |
| 517 EXPECT_CALL(mock_target, Run()); |
| 518 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 519 mock_target.CreateClosure()); |
| 520 base::RunLoop().RunUntilIdle(); |
| 521 } |
| 522 |
| 523 TEST_F(AutomaticProfileResetterDelegateTest, |
| 524 FetchAndWaitOnDefaultSettingsBrandedFailure) { |
| 525 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); |
| 526 |
| 527 // Expect ready_callback to be called just after the brandcoded settings have |
| 528 // failed to download. Fail if it is not called, or called too early. |
| 529 testing::StrictMock<MockCallbackTarget> mock_target; |
| 530 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 531 mock_target.CreateClosure()); |
| 532 base::RunLoop().RunUntilIdle(); |
| 533 |
| 534 EXPECT_CALL(mock_target, Run()); |
| 535 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); |
| 536 ExpectAndServicePendingBrandcodedConfigFetch(true /*simulate_failure*/); |
| 537 base::RunLoop().RunUntilIdle(); |
| 538 |
| 539 testing::Mock::VerifyAndClearExpectations(&mock_target); |
| 540 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults()); |
| 541 |
| 542 // Expect ready_callback to be posted immediately when the brandcoded settings |
| 543 // have already been attempted to be downloaded, but failed. |
| 544 EXPECT_CALL(mock_target, Run()); |
| 545 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( |
| 546 mock_target.CreateClosure()); |
| 547 base::RunLoop().RunUntilIdle(); |
| 548 } |
| 549 |
| 550 TEST_F(AutomaticProfileResetterDelegateTest, TriggerReset) { |
| 551 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); |
| 552 |
| 553 PrefService* prefs = profile()->GetPrefs(); |
| 554 DCHECK(prefs); |
| 555 prefs->SetString(prefs::kHomePage, kTestHomepage); |
| 556 |
| 557 testing::StrictMock<MockCallbackTarget> mock_target; |
| 558 EXPECT_CALL(mock_target, Run()); |
| 559 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0); |
| 560 resetter_delegate()->TriggerProfileSettingsReset( |
| 561 false /*send_feedback*/, mock_target.CreateClosure()); |
| 562 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); |
| 563 base::RunLoop().RunUntilIdle(); |
| 564 |
| 565 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage)); |
| 566 } |
| 567 |
| 568 TEST_F(AutomaticProfileResetterDelegateTest, |
| 569 TriggerResetWithDefaultSettingsAlreadyLoaded) { |
| 570 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); |
| 571 |
| 572 PrefService* prefs = profile()->GetPrefs(); |
| 573 DCHECK(prefs); |
| 574 prefs->SetString(prefs::kHomePage, kTestHomepage); |
| 575 |
| 576 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); |
| 577 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); |
| 578 base::RunLoop().RunUntilIdle(); |
| 579 |
| 580 testing::StrictMock<MockCallbackTarget> mock_target; |
| 581 EXPECT_CALL(mock_target, Run()); |
| 582 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0); |
| 583 resetter_delegate()->TriggerProfileSettingsReset( |
| 584 false /*send_feedback*/, mock_target.CreateClosure()); |
| 585 base::RunLoop().RunUntilIdle(); |
| 586 |
| 587 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage)); |
| 588 } |
| 589 |
| 590 TEST_F(AutomaticProfileResetterDelegateTest, |
| 591 TriggerResetAndSendFeedback) { |
| 592 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); |
| 593 |
| 594 PrefService* prefs = profile()->GetPrefs(); |
| 595 DCHECK(prefs); |
| 596 prefs->SetString(prefs::kHomePage, kTestHomepage); |
| 597 |
| 598 testing::StrictMock<MockCallbackTarget> mock_target; |
| 599 EXPECT_CALL(mock_target, Run()); |
| 600 EXPECT_CALL(*resetter_delegate(), |
| 601 SendFeedback(testing::HasSubstr(kTestHomepage))); |
| 602 |
| 603 resetter_delegate()->TriggerProfileSettingsReset( |
| 604 true /*send_feedback*/, mock_target.CreateClosure()); |
| 605 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); |
| 606 base::RunLoop().RunUntilIdle(); |
| 607 } |
| 608 |
| 609 TEST_F(AutomaticProfileResetterDelegateTest, ShowAndDismissPrompt) { |
| 610 resetter_delegate()->TriggerPrompt(); |
| 611 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
| 612 ExpectResetPromptState(true /*active*/); |
| 613 else |
| 614 ExpectResetPromptState(false /*active*/); |
| 615 resetter_delegate()->DismissPrompt(); |
| 616 ExpectResetPromptState(false /*active*/); |
| 617 resetter_delegate()->DismissPrompt(); |
| 618 } |
| 619 |
382 } // namespace | 620 } // namespace |
OLD | NEW |