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

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

Issue 27030002: Added collecting of data to be fed to the JTL interpreter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments by vasilii@. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/values_test_util.h"
15 #include "base/values.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/search_engines/template_url_service_test_util.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/browser/notification_service.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 #if defined(OS_WIN)
28 #include "chrome/browser/enumerate_modules_model_win.h"
29 #endif
30
31 using base::UTF8ToUTF16;
Peter Kasting 2013/10/15 01:31:28 This is unnecessary (you already qualify the two c
engedy 2013/10/15 22:13:07 Done.
32
33 namespace {
34
35 // Keep this value in sync with prefs::kDefaultSearchProviderURL and friends.
36 const char kDefaultSearchProviderPrefix[] = "default_search_provider";
Peter Kasting 2013/10/15 01:31:28 (1) For any of these constants used in just one pl
engedy 2013/10/15 22:13:07 Done.
37
38 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}";
39 const char kTestSuggestURL[] = "http://example.com/suggest?q={searchTerms}";
40 const char kTestInstantURL[] = "http://example.com/instant?q={searchTerms}";
41 const char kTestImageURL[] = "http://example.com/image?q={searchTerms}";
42 const char kTestSearchURLPostParams[] = "search-post-params";
43 const char kTestSuggestURLPostParams[] = "suggest-post-params";
44 const char kTestInstantURLPostParams[] = "instant-post-params";
45 const char kTestImageURLPostParams[] = "image-post-params";
46
47 const char kTestIconURL[] = "http://example.com/favicon.ico";
48 const char kTestNewTabURL[] = "http://example.com/newtab.html";
49 const char kTestAlternateURL[] = "http://example.com/s?q={searchTerms}";
50
51 const char kTestName[] = "name";
52 const char kTestKeyword[] = "keyword";
53 const char kTestSearchTermReplacementKey[] = "key";
54 const char kTestPrepopulateId[] = "2";
55 const char kTestEncoding[] = "UTF-8";
56
57 // Test fixtures -------------------------------------------------------------
58
59 class GenericTestBase : public testing::Test {
Peter Kasting 2013/10/15 01:31:28 I'm surprised there isn't already some sort of "Te
engedy 2013/10/15 22:13:07 I might have missed something, but could not find
60 protected:
61 GenericTestBase() {}
62
63 virtual void SetUp() { profile_.reset(new TestingProfile()); }
64
65 TestingProfile* profile() { return profile_.get(); }
66
67 private:
68 content::TestBrowserThreadBundle thread_bundle_;
69 scoped_ptr<TestingProfile> profile_;
70
71 DISALLOW_COPY_AND_ASSIGN(GenericTestBase);
72 };
73
74 class TemplateURLSpecificTestBase : public testing::Test {
75 protected:
76 TemplateURLSpecificTestBase() {}
77
78 virtual void SetUp() { test_util_.SetUp(); }
79
80 virtual void TearDown() { test_util_.TearDown(); }
81
82 TestingProfile* profile() { return test_util_.profile(); }
83
84 TemplateURL* CreateTestTemplateURL() {
Peter Kasting 2013/10/15 01:31:28 Nit: Again, consider returning scoped_ptr. Anothe
engedy 2013/10/15 22:13:07 Done.
85 TemplateURLData data;
86
87 data.SetURL(kTestSearchURL);
88 data.suggestions_url = kTestSuggestURL;
89 data.instant_url = kTestInstantURL;
90 data.image_url = kTestImageURL;
91 data.search_url_post_params = kTestSearchURLPostParams;
92 data.suggestions_url_post_params = kTestSuggestURLPostParams;
93 data.instant_url_post_params = kTestInstantURLPostParams;
94 data.image_url_post_params = kTestImageURLPostParams;
95
96 data.favicon_url = GURL(kTestIconURL);
97 data.new_tab_url = kTestNewTabURL;
98 data.alternate_urls.push_back(kTestAlternateURL);
99
100 data.short_name = base::UTF8ToUTF16(kTestName);
101 data.SetKeyword(base::UTF8ToUTF16(kTestKeyword));
102 data.search_terms_replacement_key = kTestSearchTermReplacementKey;
103 EXPECT_TRUE(base::StringToInt(kTestPrepopulateId, &data.prepopulate_id));
104 data.input_encodings.push_back(kTestEncoding);
105 data.safe_for_autoreplace = true;
106
107 return new TemplateURL(profile(), data);
108 }
109
110 TemplateURLServiceTestUtil test_util_;
111
112 private:
113 DISALLOW_COPY_AND_ASSIGN(TemplateURLSpecificTestBase);
114 };
115
116 template <class BaseTestFixture>
117 class ResetterDelegateMixin : public BaseTestFixture {
118 protected:
119 ResetterDelegateMixin() {}
120
121 virtual void SetUp() OVERRIDE {
122 BaseTestFixture::SetUp();
123 resetter_delegate_.reset(
124 new AutomaticProfileResetterDelegateImpl(BaseTestFixture::profile()));
125 }
126
127 virtual void TearDown() OVERRIDE {
128 resetter_delegate_.reset();
129 BaseTestFixture::TearDown();
130 }
131
132 AutomaticProfileResetterDelegate* resetter_delegate() {
133 return resetter_delegate_.get();
134 }
135
136 private:
137 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
138
139 DISALLOW_COPY_AND_ASSIGN(ResetterDelegateMixin);
140 };
141
142 typedef ResetterDelegateMixin<GenericTestBase>
143 AutomaticProfileResetterDelegateTest;
144
145 typedef ResetterDelegateMixin<TemplateURLSpecificTestBase>
146 AutomaticProfileResetterDelegateTestTemplateURLs;
147
148 // Helper classes and functions ----------------------------------------------
149
150 // Verifies that the |details| of a search engine as provided by the delegate
151 // are correct in comparison to the |expected_details| coming from the Prefs.
152 void VerifyDetails(const base::DictionaryValue& expected_details,
153 const base::DictionaryValue& details) {
154 const char* keys_to_verify[] = {
Peter Kasting 2013/10/15 01:31:28 Again, consider listing the differences rather tha
engedy 2013/10/15 22:13:07 Done.
155 "search_url", "search_terms_replacement_key",
156 "suggest_url", "instant_url",
157 "image_url", "new_tab_url",
158 "icon_url", "search_url_post_params",
159 "suggest_url_post_params", "instant_url_post_params",
160 "image_url_post_params", "name",
161 "keyword", "encodings",
162 "prepopulate_id", "alternate_urls"};
163
164 for (size_t i = 0; i < arraysize(keys_to_verify); ++i) {
165 SCOPED_TRACE(testing::Message() << "Key: " << keys_to_verify[i]);
166 const base::Value* expected_value;
167 const base::Value* actual_value;
168 ASSERT_TRUE(expected_details.Get(keys_to_verify[i], &expected_value));
169 ASSERT_TRUE(details.Get(keys_to_verify[i], &actual_value));
170 EXPECT_TRUE(actual_value->Equals(expected_value));
171 }
172 }
173
174 class MockCallbackTarget {
175 public:
176 MockCallbackTarget() {}
177
178 MOCK_CONST_METHOD0(Run, void(void));
179
180 base::Closure CreateClosure() {
181 return base::Closure(
182 base::Bind(&MockCallbackTarget::Run, base::Unretained(this)));
183 }
184
185 private:
186 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget);
187 };
188
189 // Tests ---------------------------------------------------------------------
190
191 TEST_F(AutomaticProfileResetterDelegateTest,
192 TriggerAndWaitOnModuleEnumeration) {
193 testing::StrictMock<MockCallbackTarget> mock_target;
194
195 // Expect ready_callback to be called just after the modules have been
196 // enumerated. Fail if it is not called, or called too early.
197 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
198 mock_target.CreateClosure());
199 base::RunLoop().RunUntilIdle();
200
201 EXPECT_CALL(mock_target, Run());
202 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
203 base::RunLoop().RunUntilIdle();
204
205 testing::Mock::VerifyAndClearExpectations(&mock_target);
206
207 // Expect ready_callback to be posted immediately when the modules have
208 // already been enumerated.
209 EXPECT_CALL(mock_target, Run());
210 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
211 mock_target.CreateClosure());
212 base::RunLoop().RunUntilIdle();
213
214 #if defined(OS_WIN)
215 testing::Mock::VerifyAndClearExpectations(&mock_target);
216
217 // Expect ready_callback to be posted immediately even when the modules had
218 // already been enumerated when the delegate was constructed.
219 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
220 new AutomaticProfileResetterDelegateImpl(profile()));
221
222 EXPECT_CALL(mock_target, Run());
223 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated(
224 mock_target.CreateClosure());
225 base::RunLoop().RunUntilIdle();
226 #endif
227 }
228
229 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) {
230 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
231 base::RunLoop().RunUntilIdle();
232 scoped_ptr<base::ListValue> module_name_digests(
233 resetter_delegate()->GetLoadedModuleNameDigests());
234
235 // Just verify that each element looks like an MD5 hash in hexadecimal, and
236 // also that we have at least one element on Win.
237 ASSERT_TRUE(module_name_digests);
238 for (base::ListValue::const_iterator it = module_name_digests->begin();
239 it != module_name_digests->end();
240 ++it) {
241 std::string digest_hex;
242 std::vector<uint8> digest_raw;
243
244 ASSERT_TRUE((*it)->GetAsString(&digest_hex));
245 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw));
246 EXPECT_EQ(16u, digest_raw.size());
247 }
248 #if defined(OS_WIN)
249 EXPECT_LE(1u, module_name_digests->GetSize());
250 #endif
251 }
252
253 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
254 LoadAndWaitOnTemplateURLService) {
255 testing::StrictMock<MockCallbackTarget> mock_target;
256
257 // Expect ready_callback to be called just after the template URL service gets
258 // initialized. Fail if it is not called, or called too early.
259 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
260 mock_target.CreateClosure());
261 base::RunLoop().RunUntilIdle();
262
263 EXPECT_CALL(mock_target, Run());
264 resetter_delegate()->LoadTemplateURLServiceIfNeeded();
265 base::RunLoop().RunUntilIdle();
266
267 testing::Mock::VerifyAndClearExpectations(&mock_target);
268
269 // Expect ready_callback to be posted immediately when the template URL
270 // service is already initialized.
271 EXPECT_CALL(mock_target, Run());
272 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
273 mock_target.CreateClosure());
274 base::RunLoop().RunUntilIdle();
275
276 testing::Mock::VerifyAndClearExpectations(&mock_target);
277
278 // Expect ready_callback to be posted immediately even when the template URL
279 // service had already been initialized when the delegate was constructed.
280 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
281 new AutomaticProfileResetterDelegateImpl(profile()));
282
283 EXPECT_CALL(mock_target, Run());
284 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded(
285 mock_target.CreateClosure());
286 base::RunLoop().RunUntilIdle();
287 }
288
289 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
290 GetDefaultSearchProviderDetails) {
291 TemplateURLService* template_url_service = test_util_.model();
292 test_util_.VerifyLoad();
293
294 // Create a custom search provider, and make it the default. Note that this
295 // will update all data related to the default search provider in Prefs.
296 TemplateURL* custom_dsp = CreateTestTemplateURL();
297 template_url_service->Add(custom_dsp);
298 template_url_service->SetDefaultSearchProvider(custom_dsp);
299
300 scoped_ptr<base::DictionaryValue> dsp_details(
301 resetter_delegate()->GetDefaultSearchProviderDetails());
302
303 // Verify above details against the user preferences that have been stored by
304 // TemplateURLService. We leverage on the fact that the names for all these
305 // preferences start with the same prefix. However, as preferences are stored
306 // (and normally can only be accessed) without path expansion, this trickery
307 // below is needed.
Peter Kasting 2013/10/15 01:31:28 Nit: This comment is just sort of confusing... it'
engedy 2013/10/15 22:13:07 Clarified comment.
308 PrefService* prefs = profile()->GetPrefs();
309 ASSERT_TRUE(prefs);
310 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
311 prefs->GetPreferenceValues());
312 base::DictionaryValue* expected_dsp_details;
313 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary(
314 kDefaultSearchProviderPrefix, &expected_dsp_details));
315 VerifyDetails(*expected_dsp_details, *dsp_details);
316 }
317
318 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
319 IsDefaultSearchProviderManaged) {
320 test_util_.VerifyLoad();
321
322 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
323
324 // Enable having a default search provider, and also set one from policy.
325 test_util_.SetManagedDefaultSearchPreferences(
326 true, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", "");
Peter Kasting 2013/10/15 01:31:28 Nit: "" -> std::string() where possible (many plac
engedy 2013/10/15 22:13:07 Done.
327
328 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
329 scoped_ptr<base::DictionaryValue> dsp_details(
330 resetter_delegate()->GetDefaultSearchProviderDetails());
331 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url");
332
333 // Disable having a default search provider, but nevertheless, set one from
334 // policy.
335 test_util_.RemoveManagedDefaultSearchPreferences();
336 test_util_.SetManagedDefaultSearchPreferences(
337 false, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", "");
338
339 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
340 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
341
342 // Disable having a default search provider, and set an invalid one from
343 // policy.
344 test_util_.RemoveManagedDefaultSearchPreferences();
345 test_util_.SetManagedDefaultSearchPreferences(
346 true, "", "", "", "", "", "", "", "");
347
348 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
349 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
350 }
351
352 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
353 DISABLED_IsDefaultSearchProviderManagedSubtle) {
354 test_util_.VerifyLoad();
355
356 // Only set a single policy to disable having a default search provider.
357 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
358 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
359 new base::FundamentalValue(false));
360 test_util_.model()->Observe(
361 chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
362 content::NotificationService::AllSources(),
363 content::NotificationService::NoDetails());
364
365 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
366 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
367 }
368
369 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
370 GetPrepopulatedSearchProvidersDetails) {
371 TemplateURLService* template_url_service = test_util_.model();
372 test_util_.VerifyLoad();
373
374 scoped_ptr<base::ListValue> prepopulated_search_engines_details(
375 resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
376
377 // Do the same kind of verification as for GetDefaultSearchEngineDetails:
378 // subsequently set each pre-populated engine as the default, so we can verify
379 // that the details returned by the delegate about one particular engine are
380 // correct in comparison to what has been stored to the Prefs.
381 std::vector<TemplateURL*> prepopulated_engines =
382 template_url_service->GetTemplateURLs();
383
384 ASSERT_EQ(prepopulated_engines.size(),
385 prepopulated_search_engines_details->GetSize());
386
387 // This assumes that the order of engines are preserved.
Peter Kasting 2013/10/15 01:31:28 Are preserved where? In the prefs? That seems li
engedy 2013/10/15 22:13:07 Replaced with a more solid assumption, and clarifi
388 for (size_t i = 0; i < prepopulated_engines.size(); ++i) {
389 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]);
390
391 PrefService* prefs = profile()->GetPrefs();
392 ASSERT_TRUE(prefs);
393 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
394 prefs->GetPreferenceValues());
395 base::DictionaryValue* expected_dsp_details;
396 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary(
397 kDefaultSearchProviderPrefix, &expected_dsp_details));
398
399 base::DictionaryValue* details;
400 ASSERT_TRUE(
401 prepopulated_search_engines_details->GetDictionary(i, &details));
402
403 SCOPED_TRACE(testing::Message() << "Details: " << *details);
404 VerifyDetails(*expected_dsp_details, *details);
405 }
406 }
407
408 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698