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

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 from 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 <algorithm>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/values_test_util.h"
19 #include "base/values.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
22 #include "chrome/browser/search_engines/template_url_service.h"
23 #include "chrome/browser/search_engines/template_url_service_factory.h"
24 #include "chrome/browser/search_engines/template_url_service_test_util.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "content/public/browser/notification_service.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 #if defined(OS_WIN)
33 #include "chrome/browser/enumerate_modules_model_win.h"
34 #endif
35
36 namespace {
37
38 // Test fixtures -------------------------------------------------------------
39
40 class AutomaticProfileResetterDelegateTest : public testing::Test {
41 protected:
42 AutomaticProfileResetterDelegateTest() {}
vasilii 2013/10/17 10:57:57 Here and below add virtual destructor.
engedy 2013/10/17 15:13:47 Done.
43
44 virtual void SetUp() {
vasilii 2013/10/17 10:57:57 Here and below use "OVERRIDE"
engedy 2013/10/17 15:13:47 Done.
45 resetter_delegate_.reset(new AutomaticProfileResetterDelegateImpl(NULL));
46 }
47
48 virtual void TearDown() { resetter_delegate_.reset(); }
49
50 AutomaticProfileResetterDelegate* resetter_delegate() {
51 return resetter_delegate_.get();
52 }
53
54 private:
55 content::TestBrowserThreadBundle thread_bundle_;
56 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
57
58 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest);
59 };
60
61 class AutomaticProfileResetterDelegateTestTemplateURLs : public testing::Test {
62 protected:
63 AutomaticProfileResetterDelegateTestTemplateURLs() {}
64
65 virtual void SetUp() {
66 test_util_.SetUp();
67 resetter_delegate_.reset(
68 new AutomaticProfileResetterDelegateImpl(test_util_.model()));
69 }
70
71 virtual void TearDown() {
72 resetter_delegate_.reset();
73 test_util_.TearDown();
74 }
75
76 TestingProfile* profile() { return test_util_.profile(); }
77
78 AutomaticProfileResetterDelegate* resetter_delegate() {
79 return resetter_delegate_.get();
80 }
81
82 scoped_ptr<TemplateURL> CreateTestTemplateURL() {
83 TemplateURLData data;
84
85 data.SetURL("http://example.com/search?q={searchTerms}");
86 data.suggestions_url = "http://example.com/suggest?q={searchTerms}";
87 data.instant_url = "http://example.com/instant?q={searchTerms}";
88 data.image_url = "http://example.com/image?q={searchTerms}";
89 data.search_url_post_params = "search-post-params";
90 data.suggestions_url_post_params = "suggest-post-params";
91 data.instant_url_post_params = "instant-post-params";
92 data.image_url_post_params = "image-post-params";
93
94 data.favicon_url = GURL("http://example.com/favicon.ico");
95 data.new_tab_url = "http://example.com/newtab.html";
96 data.alternate_urls.push_back("http://example.com/s?q={searchTerms}");
97
98 data.short_name = base::UTF8ToUTF16("name");
vasilii 2013/10/17 10:57:57 I would say this is ASCIIToUTF16
engedy 2013/10/17 15:13:47 True, done.
99 data.SetKeyword(base::UTF8ToUTF16("keyword"));
100 data.search_terms_replacement_key = "search-terms-replacment-key";
101 data.prepopulate_id = 42;
102 data.input_encodings.push_back("UTF-8");
103 data.safe_for_autoreplace = true;
104
105 return scoped_ptr<TemplateURL>(new TemplateURL(profile(), data));
106 }
107
108 TemplateURLServiceTestUtil test_util_;
109
110 private:
111 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
112
113 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTestTemplateURLs);
114 };
115
116 // Helper classes and functions ----------------------------------------------
117
118 // Returns the details of the default search provider from |prefs| in a format
119 // suitable for usage as |expected_details| in VerifyDetails().
120 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetails(
121 const PrefService* prefs) {
122 const char kDefaultSearchProviderPrefix[] = "default_search_provider";
123 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
124 prefs->GetPreferenceValues());
125 const base::DictionaryValue* dsp_details = NULL;
126 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary(
127 kDefaultSearchProviderPrefix, &dsp_details));
128 return scoped_ptr<base::DictionaryValue>(
129 dsp_details ? dsp_details->DeepCopy() : new base::DictionaryValue);
130 }
131
132 // Verifies that the |details| of a search engine as provided by the delegate
133 // are correct in comparison to the |expected_details| coming from the Prefs.
134 void VerifyDetails(const base::DictionaryValue& expected_details,
135 const base::DictionaryValue& details) {
136 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd();
137 it.Advance()) {
138 SCOPED_TRACE(testing::Message("Key: ") << it.key());
139 if (it.key() == "enabled" || it.key() == "synced_guid") {
140 // These attributes should not be present.
141 EXPECT_FALSE(details.HasKey(it.key()));
142 continue;
143 }
144 const base::Value* expected_value = &it.value();
145 const base::Value* actual_value = NULL;
146 ASSERT_TRUE(details.Get(it.key(), &actual_value));
147 if (it.key() == "id") {
148 // Ignore ID as it is dynamically assigned by the TemplateURLService.
149 } else if (it.key() == "encodings") {
150 // Encoding list is stored in Prefs as a single string with tokens
151 // delimited by semicolons.
152 std::string expected_encodings;
153 ASSERT_TRUE(expected_value->GetAsString(&expected_encodings));
154 const base::ListValue* actual_encodings_list = NULL;
155 ASSERT_TRUE(actual_value->GetAsList(&actual_encodings_list));
156 std::vector<std::string> actual_encodings_vector;
157 for (base::ListValue::const_iterator it = actual_encodings_list->begin();
158 it != actual_encodings_list->end(); ++it) {
vasilii 2013/10/17 10:57:57 Optional: you can write small helper function 'std
engedy 2013/10/17 15:13:47 Will do as soon as we need to do this in at least
159 std::string encoding;
160 ASSERT_TRUE((*it)->GetAsString(&encoding));
161 actual_encodings_vector.push_back(encoding);
162 }
163 EXPECT_EQ(expected_encodings, JoinString(actual_encodings_vector, ';'));
164 } else {
165 // Everything else is the same format.
166 EXPECT_TRUE(actual_value->Equals(expected_value));
167 }
168 }
169 }
170
171 class MockCallbackTarget {
172 public:
173 MockCallbackTarget() {}
vasilii 2013/10/17 10:57:57 Destructor
engedy 2013/10/17 15:13:47 Done.
174
175 MOCK_CONST_METHOD0(Run, void(void));
176
177 base::Closure CreateClosure() {
178 return base::Closure(
179 base::Bind(&MockCallbackTarget::Run, base::Unretained(this)));
180 }
181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget);
184 };
185
186 // Tests ---------------------------------------------------------------------
187
188 TEST_F(AutomaticProfileResetterDelegateTest,
189 TriggerAndWaitOnModuleEnumeration) {
190 testing::StrictMock<MockCallbackTarget> mock_target;
191
192 // Expect ready_callback to be called just after the modules have been
193 // enumerated. Fail if it is not called, or called too early.
194 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
195 mock_target.CreateClosure());
196 base::RunLoop().RunUntilIdle();
197
198 EXPECT_CALL(mock_target, Run());
199 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
200 base::RunLoop().RunUntilIdle();
201
202 testing::Mock::VerifyAndClearExpectations(&mock_target);
203
204 // Expect ready_callback to be posted immediately when the modules have
205 // already been enumerated.
206 EXPECT_CALL(mock_target, Run());
207 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
208 mock_target.CreateClosure());
209 base::RunLoop().RunUntilIdle();
210
211 #if defined(OS_WIN)
212 testing::Mock::VerifyAndClearExpectations(&mock_target);
213
214 // Expect ready_callback to be posted immediately even when the modules had
215 // already been enumerated when the delegate was constructed.
216 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
217 new AutomaticProfileResetterDelegateImpl(NULL));
218
219 EXPECT_CALL(mock_target, Run());
220 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated(
221 mock_target.CreateClosure());
222 base::RunLoop().RunUntilIdle();
223 #endif
224 }
225
226 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) {
227 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
228 base::RunLoop().RunUntilIdle();
229 scoped_ptr<base::ListValue> module_name_digests(
230 resetter_delegate()->GetLoadedModuleNameDigests());
231
232 // Just verify that each element looks like an MD5 hash in hexadecimal, and
233 // also that we have at least one element on Win.
234 ASSERT_TRUE(module_name_digests);
235 for (base::ListValue::const_iterator it = module_name_digests->begin();
236 it != module_name_digests->end(); ++it) {
237 std::string digest_hex;
238 std::vector<uint8> digest_raw;
239
240 ASSERT_TRUE((*it)->GetAsString(&digest_hex));
241 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw));
242 EXPECT_EQ(16u, digest_raw.size());
243 }
244 #if defined(OS_WIN)
245 EXPECT_LE(1u, module_name_digests->GetSize());
246 #endif
247 }
248
249 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
250 LoadAndWaitOnTemplateURLService) {
251 testing::StrictMock<MockCallbackTarget> mock_target;
252
253 // Expect ready_callback to be called just after the template URL service gets
254 // initialized. Fail if it is not called, or called too early.
255 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
256 mock_target.CreateClosure());
257 base::RunLoop().RunUntilIdle();
258
259 EXPECT_CALL(mock_target, Run());
260 resetter_delegate()->LoadTemplateURLServiceIfNeeded();
261 base::RunLoop().RunUntilIdle();
262
263 testing::Mock::VerifyAndClearExpectations(&mock_target);
264
265 // Expect ready_callback to be posted immediately when the template URL
266 // service is already initialized.
267 EXPECT_CALL(mock_target, Run());
268 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
269 mock_target.CreateClosure());
270 base::RunLoop().RunUntilIdle();
271
272 testing::Mock::VerifyAndClearExpectations(&mock_target);
273
274 // Expect ready_callback to be posted immediately even when the template URL
275 // service had already been initialized when the delegate was constructed.
276 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
277 new AutomaticProfileResetterDelegateImpl(test_util_.model()));
278
279 EXPECT_CALL(mock_target, Run());
280 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded(
281 mock_target.CreateClosure());
282 base::RunLoop().RunUntilIdle();
283 }
284
285 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
286 GetDefaultSearchProviderDetails) {
287 TemplateURLService* template_url_service = test_util_.model();
288 test_util_.VerifyLoad();
289
290 // Create a custom search provider, and make it the default. Note that this
291 // will update all data related to the default search provider in Prefs.
292 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL());
293 TemplateURL* custom_dsp = owned_custom_dsp.get();
294 template_url_service->Add(owned_custom_dsp.release());
295 template_url_service->SetDefaultSearchProvider(custom_dsp);
296
297 scoped_ptr<base::DictionaryValue> dsp_details(
298 resetter_delegate()->GetDefaultSearchProviderDetails());
299
300 // Verify above details against the user preferences that have been stored by
301 // TemplateURLService.
302 PrefService* prefs = profile()->GetPrefs();
303 ASSERT_TRUE(prefs);
304 scoped_ptr<base::DictionaryValue> expected_dsp_details(
305 GetDefaultSearchProviderDetails(prefs));
306 VerifyDetails(*expected_dsp_details, *dsp_details);
307 }
308
309 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
310 IsDefaultSearchProviderManaged) {
311 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}";
312 const char kTestName[] = "name";
313 const char kTestKeyword[] = "keyword";
314
315 test_util_.VerifyLoad();
316
317 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
318
319 // Enable having a default search provider, and also set one from policy.
320 test_util_.SetManagedDefaultSearchPreferences(
321 true, kTestName, kTestKeyword, kTestSearchURL, std::string(),
322 std::string(), std::string(), std::string(), std::string());
323
324 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
325 scoped_ptr<base::DictionaryValue> dsp_details(
326 resetter_delegate()->GetDefaultSearchProviderDetails());
327 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url");
328
329 // Disable having a default search provider, but nevertheless, set one from
330 // policy.
331 test_util_.RemoveManagedDefaultSearchPreferences();
332 test_util_.SetManagedDefaultSearchPreferences(
333 false, kTestName, kTestKeyword, kTestSearchURL, std::string(),
334 std::string(), std::string(), std::string(), std::string());
335
336 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails();
337 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
338 EXPECT_TRUE(dsp_details->empty());
339
340 // Disable having a default search provider, and set an invalid one from
341 // policy.
342 test_util_.RemoveManagedDefaultSearchPreferences();
343 test_util_.SetManagedDefaultSearchPreferences(
344 true, std::string(), std::string(), std::string(), std::string(),
345 std::string(), std::string(), std::string(), std::string());
346
347 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails();
348 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
349 EXPECT_TRUE(dsp_details->empty());
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 scoped_ptr<base::DictionaryValue> dsp_details(
366 resetter_delegate()->GetDefaultSearchProviderDetails());
367 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
368 EXPECT_TRUE(dsp_details->empty());
369 }
370
371 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
372 GetPrepopulatedSearchProvidersDetails) {
373 TemplateURLService* template_url_service = test_util_.model();
374 test_util_.VerifyLoad();
375
376 scoped_ptr<base::ListValue> search_engines_details(
377 resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
378
379 // Do the same kind of verification as for GetDefaultSearchEngineDetails:
380 // subsequently set each pre-populated engine as the default, so we can verify
381 // that the details returned by the delegate about one particular engine are
382 // correct in comparison to what has been stored to the Prefs.
383 std::vector<TemplateURL*> prepopulated_engines =
384 template_url_service->GetTemplateURLs();
385
386 ASSERT_EQ(prepopulated_engines.size(), search_engines_details->GetSize());
387
388 for (size_t i = 0; i < search_engines_details->GetSize(); ++i) {
389 const base::DictionaryValue* details = NULL;
390 ASSERT_TRUE(search_engines_details->GetDictionary(i, &details));
391
392 std::string keyword;
393 ASSERT_TRUE(details->GetString("keyword", &keyword));
394 TemplateURL* search_engine =
395 template_url_service->GetTemplateURLForKeyword(UTF8ToUTF16(keyword));
396 ASSERT_TRUE(search_engine);
397 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]);
398
399 PrefService* prefs = profile()->GetPrefs();
400 ASSERT_TRUE(prefs);
401 scoped_ptr<base::DictionaryValue> expected_dsp_details(
402 GetDefaultSearchProviderDetails(prefs));
403 VerifyDetails(*expected_dsp_details, *details);
404 }
405 }
406
407 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698