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

Side by Side Diff: chrome/browser/android/search_geolocation/search_geolocation_service_unittest.cc

Issue 2633103002: Add a unittest for SearchGeolocationService (Closed)
Patch Set: Add a unittest for SearchGeolocationService Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright 2017 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/android/search_geolocation/search_geolocation_service.h "
dominickn 2017/01/18 06:14:11 Nit: #include <memory> #include <utility> #inclu
raymes 2017/01/18 23:49:36 Done.
6
7 #include "base/strings/utf_string_conversions.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 Done.
8 #include "base/test/scoped_feature_list.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/search_engines/template_url_service_factory.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 Done.
11 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 Done.
12 #include "chrome/common/chrome_features.h"
13 #include "chrome/common/pref_names.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 I think this one is used for the pref name
14 #include "chrome/test/base/search_test_utils.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 Done.
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/content_settings/core/browser/host_content_settings_map.h"
17 #include "components/content_settings/core/common/pref_names.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 This one is used for the content setting pref name
18 #include "components/prefs/pref_service.h"
19 #include "components/search_engines/template_url_service.h"
dominickn 2017/01/18 06:14:11 Not used?
raymes 2017/01/18 23:49:36 Done.
20 #include "components/sync_preferences/testing_pref_service_syncable.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "url/origin.h"
24
25 namespace {
26
27 const char kGoogleURL[] = "https://www.google.com";
28 const char kGoogleAusURL[] = "https://www.google.com.au";
29 const char kGoogleHTTPURL[] = "http://www.google.com";
30 const char kExampleURL[] = "https://www.example.com";
31
32 url::Origin ToOrigin(const char* url) {
33 return url::Origin(GURL(url));
34 }
35
36 // The test delegate is used to mock out search-engine related functionality.
37 class TestSearchEngineDelegate
38 : public SearchGeolocationService::SearchEngineDelegate {
39 public:
40 bool IsDSEGoogle() override {
41 // A rough heuristic that is good enough for this test.
42 return dse_cctld_.host().find("google.com") != std::string::npos;
43 }
44
45 url::Origin GetGoogleDSECCTLD() override {
46 if (!IsDSEGoogle())
47 return url::Origin();
48
49 return dse_cctld_;
50 }
51
52 void SetDSEChangedCallback(const base::Closure& callback) override {
53 dse_changed_callback_ = callback;
54 }
55
56 void SetDSECCTLD(const std::string& dse_cctld) {
57 dse_cctld_ = url::Origin(GURL(dse_cctld));
58 dse_changed_callback_.Run();
59 }
60
61 private:
62 url::Origin dse_cctld_;
63 base::Closure dse_changed_callback_;
64 };
65
66 } // namespace
67
68 class SearchGeolocationServiceTest : public testing::Test {
69 public:
70 void SetUp() override {
71 scoped_feature_list_.InitAndEnableFeature(
72 features::kConsistentOmniboxGeolocation);
73
74 profile_.reset(new TestingProfile);
75
76 std::unique_ptr<TestSearchEngineDelegate> test_delegate =
dominickn 2017/01/18 06:14:11 auto would make this shorter.
raymes 2017/01/18 23:49:36 Done.
77 base::MakeUnique<TestSearchEngineDelegate>();
78 test_delegate_ = test_delegate.get();
79 GetService()->SetSearchEngineDelegateForTest(std::move(test_delegate));
80 }
81
82 void TearDown() override { profile_.reset(); }
83
84 TestingProfile* profile() {
85 return profile_.get();
86 test_delegate_ = nullptr;
dominickn 2017/01/18 06:14:11 This test_delegate_ line looks out of place.
raymes 2017/01/18 23:49:36 Whoops!
87 }
88
89 TestSearchEngineDelegate* test_delegate() { return test_delegate_; }
dominickn 2017/01/18 06:14:11 Either format this like profile() or format profil
raymes 2017/01/18 23:49:36 This was just what clang format did. If it fits on
90
91 SearchGeolocationService* GetService() {
92 return SearchGeolocationService::Factory::GetForBrowserContext(profile());
93 }
94
95 void SetContentSetting(const std::string& origin_string,
96 ContentSetting setting) {
97 GURL url(origin_string);
98 HostContentSettingsMapFactory::GetForProfile(profile())
99 ->SetContentSettingDefaultScope(url, url,
100 CONTENT_SETTINGS_TYPE_GEOLOCATION,
101 std::string(), setting);
102 }
103
104 ContentSetting GetContentSetting(const std::string& origin_string) {
105 GURL url(origin_string);
106 return HostContentSettingsMapFactory::GetForProfile(profile())
107 ->GetContentSetting(url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION,
108 std::string());
109 }
110
111 // Simulates the initialization that happens when recreating the service. If
112 // |clear_pref| is true, then it simulates the first time the service is ever
113 // created.
114 void ReinitializeService(bool clear_pref) {
115 if (clear_pref)
116 profile()->GetPrefs()->ClearPref(prefs::kGoogleDSEGeolocationSetting);
117
118 GetService()->InitializeDSEGeolocationSettingIfNeeded();
119 }
120
121 private:
122 base::test::ScopedFeatureList scoped_feature_list_;
123 std::unique_ptr<TestingProfile> profile_;
124 content::TestBrowserThreadBundle thread_bundle_;
125
126 // This is owned by the SearchGeolocationService which is owned by the
127 // profile.
128 TestSearchEngineDelegate* test_delegate_;
129 };
130
131 TEST_F(SearchGeolocationServiceTest, Initialization) {
132 test_delegate()->SetDSECCTLD(kGoogleURL);
133
134 // DSE setting initialized to true if the content setting is ALLOW.
135 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
136 ReinitializeService(true /* clear_pref */);
137 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
138 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
139
140 // DSE setting initialized to true if the content setting is ASK.
141 SetContentSetting(kGoogleURL, CONTENT_SETTING_ASK);
142 ReinitializeService(true /* clear_pref */);
143 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
144 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
145
146 // DSE setting initialized to false if the content setting is BLOCK.
147 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
148 ReinitializeService(true /* clear_pref */);
149 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
150 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
151
152 // Nothing happens if the pref is already set when the service is initialized.
153 SetContentSetting(kGoogleURL, CONTENT_SETTING_ASK);
154 ReinitializeService(false /* clear_pref */);
155 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
156 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
157
158 // For non-Google search engine, the setting may be initialized to true, but
159 // it won't be used.
160 test_delegate()->SetDSECCTLD(kExampleURL);
161 SetContentSetting(kExampleURL, CONTENT_SETTING_ALLOW);
162 ReinitializeService(true /* clear_pref */);
163 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kExampleURL)));
164 }
165
166 TEST_F(SearchGeolocationServiceTest, OffTheRecord) {
167 // Service isn't constructed for an OTR profile.
168 Profile* otr_profile = profile()->GetOffTheRecordProfile();
169 SearchGeolocationService* service =
170 SearchGeolocationService::Factory::GetForBrowserContext(otr_profile);
171 EXPECT_EQ(nullptr, service);
172 }
173
174 TEST_F(SearchGeolocationServiceTest, UseDSEGeolocationSetting) {
175 // True for origin that matches the CCTLD and meets all requirements.
176 test_delegate()->SetDSECCTLD(kGoogleURL);
177 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
178
179 // False for different origin.
180 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
181
182 // False for http origin.
183 test_delegate()->SetDSECCTLD(kGoogleHTTPURL);
184 EXPECT_FALSE(
185 GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleHTTPURL)));
186
187 // False if the feature is disabled.
188 test_delegate()->SetDSECCTLD(kGoogleURL);
189 {
190 base::test::ScopedFeatureList scoped_feature_list;
191 scoped_feature_list.InitAndDisableFeature(
192 features::kConsistentOmniboxGeolocation);
193 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
194 }
195
196 // False if the content setting is enterprise ask.
197 profile()->GetTestingPrefService()->SetManagedPref(
198 prefs::kManagedDefaultGeolocationSetting,
199 new base::FundamentalValue(CONTENT_SETTING_ASK));
200 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
201 }
202
203 TEST_F(SearchGeolocationServiceTest, GetDSEGeolocationSetting) {
204 test_delegate()->SetDSECCTLD(kGoogleURL);
205
206 // The case where the pref is set to true.
207 GetService()->SetDSEGeolocationSetting(true);
208 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
209
210 // Make the content setting conflict. Check that it gets made consistent
211 // again.
212 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
213 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
214
215 // The case where the pref is set to false.
216 SetContentSetting(kGoogleURL, CONTENT_SETTING_DEFAULT);
217 GetService()->SetDSEGeolocationSetting(false);
218 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
219
220 // Make the content setting conflict. Check that it gets made consistent
221 // again.
222 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
223 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
224 }
225
226 TEST_F(SearchGeolocationServiceTest, SetDSEGeolocationSetting) {
227 test_delegate()->SetDSECCTLD(kGoogleURL);
228
229 GetService()->SetDSEGeolocationSetting(true);
230 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
231
232 GetService()->SetDSEGeolocationSetting(false);
233 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
234
235 // Check that the content setting is always reset when setting the DSE
236 // setting.
237 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
238 GetService()->SetDSEGeolocationSetting(true);
239 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleURL));
240
241 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
242 GetService()->SetDSEGeolocationSetting(false);
243 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleURL));
244
245 // Check that the pref doesn't change if it's not user settable.
246 GetService()->SetDSEGeolocationSetting(true);
247 profile()->GetTestingPrefService()->SetManagedPref(
248 prefs::kManagedDefaultGeolocationSetting,
249 new base::FundamentalValue(CONTENT_SETTING_ASK));
250 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
251 GetService()->SetDSEGeolocationSetting(false);
252 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
253 }
254
255 TEST_F(SearchGeolocationServiceTest, DSEChanges) {
256 test_delegate()->SetDSECCTLD(kGoogleURL);
257 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
258 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
259
260 // Change to google.com.au, setting should remain the same.
261 test_delegate()->SetDSECCTLD(kGoogleAusURL);
262 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
263 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
264 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
265
266 // Set the content setting for google.com to block. When we change back to
267 // google.com, the setting should be set to false.
268 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
269 test_delegate()->SetDSECCTLD(kGoogleURL);
270 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
271 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
272
273 // Now set the content setting for google.com.au to ALLOW. When we change to
274 // google.com.au, its content setting should be reset and the setting should
275 // still be false.
276 SetContentSetting(kGoogleAusURL, CONTENT_SETTING_ALLOW);
277 test_delegate()->SetDSECCTLD(kGoogleAusURL);
278 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
279 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
280 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleAusURL));
281
282 // Now set to a non-google search. The setting should never be used.
283 test_delegate()->SetDSECCTLD(kExampleURL);
284 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
285 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kExampleURL)));
286
287 // Go back to google.com.au. The setting should still be false because that's
288 // what it last was.
289 test_delegate()->SetDSECCTLD(kGoogleAusURL);
290 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
291 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
292 }
OLDNEW
« no previous file with comments | « chrome/browser/android/search_geolocation/search_geolocation_service.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698