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

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: 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 "chrome/browser/search_engines/template_url_service.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/search_engines/template_url_service_test_util.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using base::UTF8ToUTF16;
23
24 namespace {
25
26 // Keep this value in sync with prefs::kDefaultSearchProviderURL and friends.
27 const char kDefaultSearchProviderPrefix[] = "default_search_provider";
28
29 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}";
30 const char kTestSuggestURL[] = "http://example.com/suggest?q={searchTerms}";
31 const char kTestInstantURL[] = "http://example.com/instant?q={searchTerms}";
32 const char kTestImageURL[] = "http://example.com/image?q={searchTerms}";
33 const char kTestSearchURLPostParams[] = "search-post-params";
34 const char kTestSuggestURLPostParams[] = "suggest-post-params";
35 const char kTestInstantURLPostParams[] = "instant-post-params";
36 const char kTestImageURLPostParams[] = "image-post-params";
37
38 const char kTestIconURL[] = "http://example.com/favicon.ico";
39 const char kTestNewTabURL[] = "http://example.com/newtab.html";
40 const char kTestAlternateURL[] = "http://example.com/s?q={searchTerms}";
41
42 const char kTestName[] = "name";
43 const char kTestKeyword[] = "keyword";
44 const char kTestSearchTermReplacementKey[] = "key";
45 const char kTestPrepopulateId[] = "2";
46 const char kTestEncoding[] = "UTF-8";
47
48 // Test fixtures -------------------------------------------------------------
49
50 class TemplateURLSpecificTestBase : public testing::Test {
51 protected:
52 TemplateURLSpecificTestBase() {}
53
54 virtual void SetUp() { test_util_.SetUp(); }
55
56 virtual void TearDown() { test_util_.TearDown(); }
57
58 TestingProfile* profile() { return test_util_.profile(); }
59
60 TemplateURL* CreateTestTemplateURL() {
61 TemplateURLData data;
62
63 data.SetURL(kTestSearchURL);
64 data.suggestions_url = kTestSuggestURL;
65 data.instant_url = kTestInstantURL;
66 data.image_url = kTestImageURL;
67 data.search_url_post_params = kTestSearchURLPostParams;
68 data.suggestions_url_post_params = kTestSuggestURLPostParams;
69 data.instant_url_post_params = kTestInstantURLPostParams;
70 data.image_url_post_params = kTestImageURLPostParams;
71
72 data.favicon_url = GURL(kTestIconURL);
73 data.new_tab_url = kTestNewTabURL;
74 data.alternate_urls.push_back(kTestAlternateURL);
75
76 data.short_name = base::UTF8ToUTF16(kTestName);
77 data.SetKeyword(base::UTF8ToUTF16(kTestKeyword));
78 data.search_terms_replacement_key = kTestSearchTermReplacementKey;
79 EXPECT_TRUE(base::StringToInt(kTestPrepopulateId, &data.prepopulate_id));
80 data.input_encodings.push_back(kTestEncoding);
81 data.safe_for_autoreplace = true;
82
83 return new TemplateURL(profile(), data);
84 }
85
86 TemplateURLServiceTestUtil test_util_;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(TemplateURLSpecificTestBase);
90 };
91
92 template <class BaseTestFixture>
93 class ResetterDelegateMixin : public BaseTestFixture {
94 protected:
95 ResetterDelegateMixin() {}
96
97 virtual void SetUp() OVERRIDE {
98 BaseTestFixture::SetUp();
99 resetter_delegate_.reset(
100 new AutomaticProfileResetterDelegateImpl(BaseTestFixture::profile()));
101 }
102
103 virtual void TearDown() OVERRIDE {
104 resetter_delegate_.reset();
105 BaseTestFixture::TearDown();
106 }
107
108 AutomaticProfileResetterDelegate* resetter_delegate() {
109 return resetter_delegate_.get();
110 }
111
112 private:
113 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_;
114
115 DISALLOW_COPY_AND_ASSIGN(ResetterDelegateMixin);
116 };
117
118 typedef ResetterDelegateMixin<TemplateURLSpecificTestBase>
119 AutomaticProfileResetterDelegateTestTemplateURLs;
120
121 // Helper classes and functions ----------------------------------------------
122
123 // Verifies that the |details| of a search engine as provided by the delegate
124 // are correct in comparison to the |expected_details| coming from the Prefs.
125 void VerifyDetails(const base::DictionaryValue& expected_details,
126 const base::DictionaryValue& details) {
127 const char* keys_to_verify[] = {
128 "search_url", "search_terms_replacement_key",
129 "suggest_url", "instant_url",
130 "image_url", "new_tab_url",
131 "icon_url", "search_url_post_params",
132 "suggest_url_post_params", "instant_url_post_params",
133 "image_url_post_params", "name",
134 "keyword", "encodings",
135 "prepopulate_id", "alternate_urls"};
136
137 for (size_t i = 0; i < arraysize(keys_to_verify); ++i) {
138 SCOPED_TRACE(testing::Message() << "Key: " << keys_to_verify[i]);
139 const base::Value* expected_value;
140 const base::Value* actual_value;
141 ASSERT_TRUE(expected_details.Get(keys_to_verify[i], &expected_value));
142 EXPECT_TRUE(details.Get(keys_to_verify[i], &actual_value) &&
143 actual_value->Equals(expected_value));
144 }
145 }
146
147 class MockCallbackTarget {
148 public:
149 MockCallbackTarget() {}
150
151 MOCK_CONST_METHOD0(Run, void(void));
152
153 base::Closure CreateClosure() {
154 return base::Closure(
155 base::Bind(&MockCallbackTarget::Run, base::Unretained(this)));
156 }
157
158 private:
159 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget);
160 };
161
162 // Tests ---------------------------------------------------------------------
163
164 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
165 LoadAndWaitOnTemplateURLService) {
166 testing::StrictMock<MockCallbackTarget> mock_target;
167
168 // Expect ready_callback to be called just after the template URL service gets
169 // initialized. Fail if it is not called, or called too early.
170 resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure());
171 base::RunLoop().RunUntilIdle();
172
173 testing::Mock::VerifyAndClearExpectations(&mock_target);
174
175 EXPECT_CALL(mock_target, Run());
176 resetter_delegate()->LoadTemplateURLServiceIfNeeded();
177 base::RunLoop().RunUntilIdle();
178
179 testing::Mock::VerifyAndClearExpectations(&mock_target);
180
181 // Expect ready_callback to be posted immediately when the template URL
182 // service is already initialized.
183 EXPECT_CALL(mock_target, Run());
184 resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure());
185 base::RunLoop().RunUntilIdle();
186
187 testing::Mock::VerifyAndClearExpectations(&mock_target);
188
189 // Expect ready_callback to be posted immediately even when the template URL
190 // service had already been initialized when the delegate was constructed.
191 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate(
192 new AutomaticProfileResetterDelegateImpl(profile()));
193
194 EXPECT_CALL(mock_target, Run());
195 late_resetter_delegate->WaitOnTemplateURLService(mock_target.CreateClosure());
196 base::RunLoop().RunUntilIdle();
197 }
198
199 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
200 GetDefaultSearchProviderDetails) {
201 TemplateURLService* template_url_service = test_util_.model();
202 test_util_.VerifyLoad();
203
204 // Create a custom search provider, and make it the default. Note that this
205 // will update all data related to the default search provider in Prefs.
206 TemplateURL* custom_dsp = CreateTestTemplateURL();
207 template_url_service->Add(custom_dsp);
208 template_url_service->SetDefaultSearchProvider(custom_dsp);
209
210 scoped_ptr<base::DictionaryValue> dsp_details(
211 resetter_delegate()->GetDefaultSearchProviderDetails());
212
213 // Verify above details against the user preferences that have been stored by
214 // TemplateURLService. We leverage on the fact that the names for all these
215 // preferences start with the same prefix.
216 PrefService* prefs = profile()->GetPrefs();
217 ASSERT_TRUE(prefs);
218 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
219 prefs->GetPreferenceValues());
220 base::DictionaryValue* expected_dsp_details;
221 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary(
222 kDefaultSearchProviderPrefix, &expected_dsp_details));
223 VerifyDetails(*expected_dsp_details, *dsp_details);
224 }
225
226 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
227 IsDefaultSearchProviderManaged) {
228 test_util_.VerifyLoad();
229
230 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
231
232 test_util_.SetManagedDefaultSearchPreferences(
233 true, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", "");
234
235 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
236 scoped_ptr<base::DictionaryValue> dsp_details(
237 resetter_delegate()->GetDefaultSearchProviderDetails());
238 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url");
239
240 test_util_.RemoveManagedDefaultSearchPreferences();
241 test_util_.SetManagedDefaultSearchPreferences(
242 false, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", "");
243
244 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
245 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
246
247 test_util_.RemoveManagedDefaultSearchPreferences();
248 test_util_.SetManagedDefaultSearchPreferences(
249 true, "", "", "", "", "", "", "", "");
250
251 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
252 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
253 }
254
255 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs,
256 GetPrepopulatedSearchProvidersDetails) {
257 TemplateURLService* template_url_service = test_util_.model();
258 test_util_.VerifyLoad();
259
260 scoped_ptr<base::ListValue> prepopulated_search_engines_details(
261 resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
262
263 // Do the same kind of verification as for GetDefaultSearchEngineDetails:
264 // subsequently set each pre-populated engine as the default, so we can verify
265 // that the details returned by the delegate about one particular engine are
266 // correct (we do not assume the same ordering, so we just try all).
267 std::vector<TemplateURL*> prepopulated_engines =
268 template_url_service->GetTemplateURLs();
269
270 ASSERT_EQ(prepopulated_engines.size(),
271 prepopulated_search_engines_details->GetSize());
272
273 for (size_t i = 0; i < prepopulated_engines.size(); ++i) {
274 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]);
275
276 PrefService* prefs = profile()->GetPrefs();
277 ASSERT_TRUE(prefs);
278 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion(
279 prefs->GetPreferenceValues());
280 base::DictionaryValue* expected_dsp_details;
281 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary(
282 kDefaultSearchProviderPrefix, &expected_dsp_details));
283
284 base::DictionaryValue* details;
285 ASSERT_TRUE(
286 prepopulated_search_engines_details->GetDictionary(i, &details));
287
288 SCOPED_TRACE(testing::Message() << "Details: " << *details);
289 VerifyDetails(*expected_dsp_details, *details);
290 }
291 }
292
293 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698