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

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 "
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/callback.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/test/scoped_feature_list.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
14 #include "chrome/common/chrome_features.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/content_settings/core/common/pref_names.h"
19 #include "components/prefs/pref_service.h"
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 auto test_delegate = base::MakeUnique<TestSearchEngineDelegate>();
77 test_delegate_ = test_delegate.get();
78 GetService()->SetSearchEngineDelegateForTest(std::move(test_delegate));
79 }
80
81 void TearDown() override {
82 test_delegate_ = nullptr;
83 profile_.reset();
84 }
85
86 TestingProfile* profile() { return profile_.get(); }
87
88 TestSearchEngineDelegate* test_delegate() { return test_delegate_; }
89
90 SearchGeolocationService* GetService() {
91 return SearchGeolocationService::Factory::GetForBrowserContext(profile());
92 }
93
94 void SetContentSetting(const std::string& origin_string,
95 ContentSetting setting) {
96 GURL url(origin_string);
97 HostContentSettingsMapFactory::GetForProfile(profile())
98 ->SetContentSettingDefaultScope(url, url,
99 CONTENT_SETTINGS_TYPE_GEOLOCATION,
100 std::string(), setting);
101 }
102
103 ContentSetting GetContentSetting(const std::string& origin_string) {
104 GURL url(origin_string);
105 return HostContentSettingsMapFactory::GetForProfile(profile())
106 ->GetContentSetting(url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION,
107 std::string());
108 }
109
110 // Simulates the initialization that happens when recreating the service. If
111 // |clear_pref| is true, then it simulates the first time the service is ever
112 // created.
113 void ReinitializeService(bool clear_pref) {
114 if (clear_pref)
115 profile()->GetPrefs()->ClearPref(prefs::kGoogleDSEGeolocationSetting);
116
117 GetService()->InitializeDSEGeolocationSettingIfNeeded();
118 }
119
120 private:
121 base::test::ScopedFeatureList scoped_feature_list_;
122 std::unique_ptr<TestingProfile> profile_;
123 content::TestBrowserThreadBundle thread_bundle_;
124
125 // This is owned by the SearchGeolocationService which is owned by the
126 // profile.
127 TestSearchEngineDelegate* test_delegate_;
128 };
129
130 TEST_F(SearchGeolocationServiceTest, Initialization) {
131 test_delegate()->SetDSECCTLD(kGoogleURL);
132
133 // DSE setting initialized to true if the content setting is ALLOW.
134 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
135 ReinitializeService(true /* clear_pref */);
136 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
137 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
138
139 // DSE setting initialized to true if the content setting is ASK.
140 SetContentSetting(kGoogleURL, CONTENT_SETTING_ASK);
141 ReinitializeService(true /* clear_pref */);
142 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
143 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
144
145 // DSE setting initialized to false if the content setting is BLOCK.
146 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
147 ReinitializeService(true /* clear_pref */);
148 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
149 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
150
151 // Nothing happens if the pref is already set when the service is initialized.
152 SetContentSetting(kGoogleURL, CONTENT_SETTING_ASK);
153 ReinitializeService(false /* clear_pref */);
154 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
155 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
156
157 // For non-Google search engine, the setting may be initialized to true, but
158 // it won't be used.
159 test_delegate()->SetDSECCTLD(kExampleURL);
160 SetContentSetting(kExampleURL, CONTENT_SETTING_ALLOW);
161 ReinitializeService(true /* clear_pref */);
162 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kExampleURL)));
163 }
164
165 TEST_F(SearchGeolocationServiceTest, OffTheRecord) {
166 // Service isn't constructed for an OTR profile.
167 Profile* otr_profile = profile()->GetOffTheRecordProfile();
168 SearchGeolocationService* service =
169 SearchGeolocationService::Factory::GetForBrowserContext(otr_profile);
170 EXPECT_EQ(nullptr, service);
171 }
172
173 TEST_F(SearchGeolocationServiceTest, UseDSEGeolocationSetting) {
174 // True for origin that matches the CCTLD and meets all requirements.
175 test_delegate()->SetDSECCTLD(kGoogleURL);
176 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
177
178 // False for different origin.
179 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
180
181 // False for http origin.
182 test_delegate()->SetDSECCTLD(kGoogleHTTPURL);
183 EXPECT_FALSE(
184 GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleHTTPURL)));
185
186 // False if the feature is disabled.
187 test_delegate()->SetDSECCTLD(kGoogleURL);
188 {
189 base::test::ScopedFeatureList scoped_feature_list;
190 scoped_feature_list.InitAndDisableFeature(
191 features::kConsistentOmniboxGeolocation);
192 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
193 }
194
195 // False if the content setting is enterprise ask.
196 profile()->GetTestingPrefService()->SetManagedPref(
197 prefs::kManagedDefaultGeolocationSetting,
198 new base::FundamentalValue(CONTENT_SETTING_ASK));
199 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
200 }
201
202 TEST_F(SearchGeolocationServiceTest, GetDSEGeolocationSetting) {
203 test_delegate()->SetDSECCTLD(kGoogleURL);
204
205 // The case where the pref is set to true.
206 GetService()->SetDSEGeolocationSetting(true);
207 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
208
209 // Make the content setting conflict. Check that it gets made consistent
210 // again.
211 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
212 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
213
214 // The case where the pref is set to false.
215 SetContentSetting(kGoogleURL, CONTENT_SETTING_DEFAULT);
216 GetService()->SetDSEGeolocationSetting(false);
217 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
218
219 // Make the content setting conflict. Check that it gets made consistent
220 // again.
221 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
222 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
223 }
224
225 TEST_F(SearchGeolocationServiceTest, SetDSEGeolocationSetting) {
226 test_delegate()->SetDSECCTLD(kGoogleURL);
227
228 GetService()->SetDSEGeolocationSetting(true);
229 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
230
231 GetService()->SetDSEGeolocationSetting(false);
232 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
233
234 // Check that the content setting is always reset when setting the DSE
235 // setting.
236 SetContentSetting(kGoogleURL, CONTENT_SETTING_ALLOW);
237 GetService()->SetDSEGeolocationSetting(true);
238 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleURL));
239
240 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
241 GetService()->SetDSEGeolocationSetting(false);
242 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleURL));
243
244 // Check that the pref doesn't change if it's not user settable.
245 GetService()->SetDSEGeolocationSetting(true);
246 profile()->GetTestingPrefService()->SetManagedPref(
247 prefs::kManagedDefaultGeolocationSetting,
248 new base::FundamentalValue(CONTENT_SETTING_ASK));
249 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
250 GetService()->SetDSEGeolocationSetting(false);
251 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
252 }
253
254 TEST_F(SearchGeolocationServiceTest, DSEChanges) {
255 test_delegate()->SetDSECCTLD(kGoogleURL);
256 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
257 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
258
259 // Change to google.com.au, setting should remain the same.
260 test_delegate()->SetDSECCTLD(kGoogleAusURL);
261 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
262 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
263 EXPECT_TRUE(GetService()->GetDSEGeolocationSetting());
264
265 // Set the content setting for google.com to block. When we change back to
266 // google.com, the setting should be set to false.
267 SetContentSetting(kGoogleURL, CONTENT_SETTING_BLOCK);
268 test_delegate()->SetDSECCTLD(kGoogleURL);
269 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleURL)));
270 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
271
272 // Now set the content setting for google.com.au to ALLOW. When we change to
273 // google.com.au, its content setting should be reset and the setting should
274 // still be false.
275 SetContentSetting(kGoogleAusURL, CONTENT_SETTING_ALLOW);
276 test_delegate()->SetDSECCTLD(kGoogleAusURL);
277 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
278 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
279 EXPECT_EQ(CONTENT_SETTING_ASK, GetContentSetting(kGoogleAusURL));
280
281 // Now set to a non-google search. The setting should never be used.
282 test_delegate()->SetDSECCTLD(kExampleURL);
283 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
284 EXPECT_FALSE(GetService()->UseDSEGeolocationSetting(ToOrigin(kExampleURL)));
285
286 // Go back to google.com.au. The setting should still be false because that's
287 // what it last was.
288 test_delegate()->SetDSECCTLD(kGoogleAusURL);
289 EXPECT_TRUE(GetService()->UseDSEGeolocationSetting(ToOrigin(kGoogleAusURL)));
290 EXPECT_FALSE(GetService()->GetDSEGeolocationSetting());
291 }
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