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

Side by Side Diff: content/browser/geolocation/location_arbitrator_impl_unittest.cc

Issue 2126893003: Geolocation cleanup: make GeolocationDelegate::OverrideSystemLocationProvider return unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: wez@ comments; refactored GeolocationLocationArbitratorTest and TestingLocationArbitrator Created 4 years, 5 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/geolocation/location_arbitrator_impl.h" 5 #include "content/browser/geolocation/location_arbitrator_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 namespace { 68 namespace {
69 69
70 class FakeGeolocationDelegate : public GeolocationDelegate { 70 class FakeGeolocationDelegate : public GeolocationDelegate {
71 public: 71 public:
72 FakeGeolocationDelegate() = default; 72 FakeGeolocationDelegate() = default;
73 73
74 bool UseNetworkLocationProviders() override { return use_network_; } 74 bool UseNetworkLocationProviders() override { return use_network_; }
75 void set_use_network(bool use_network) { use_network_ = use_network; } 75 void set_use_network(bool use_network) { use_network_ = use_network; }
76 76
77 LocationProvider* OverrideSystemLocationProvider() override { 77 std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override {
78 if (!system_location_provider_) 78 return base::WrapUnique(new MockLocationProvider);
79 system_location_provider_ = base::WrapUnique(new MockLocationProvider);
80 return system_location_provider_.get();
81 }
82
83 LocationProvider* system_location_provider() const {
84 return system_location_provider_.get();
85 } 79 }
86 80
87 private: 81 private:
88 bool use_network_ = true; 82 bool use_network_ = true;
89 std::unique_ptr<LocationProvider> system_location_provider_;
90 83
91 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); 84 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate);
92 }; 85 };
93 86
87 } // namespace
88
94 class TestingLocationArbitrator : public LocationArbitratorImpl { 89 class TestingLocationArbitrator : public LocationArbitratorImpl {
95 public: 90 public:
96 TestingLocationArbitrator( 91 TestingLocationArbitrator(
97 const LocationArbitratorImpl::LocationUpdateCallback& callback, 92 const LocationArbitratorImpl::LocationUpdateCallback& callback,
98 AccessTokenStore* access_token_store, 93 AccessTokenStore* access_token_store,
99 GeolocationDelegate* delegate, 94 GeolocationDelegate* delegate)
100 bool is_fake_delegate)
101 : LocationArbitratorImpl(callback, delegate), 95 : LocationArbitratorImpl(callback, delegate),
102 is_fake_delegate_(is_fake_delegate),
103 cell_(nullptr), 96 cell_(nullptr),
104 gps_(nullptr), 97 gps_(nullptr),
105 access_token_store_(access_token_store) {} 98 access_token_store_(access_token_store) {}
106 99
107 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } 100 base::Time GetTimeNow() const override { return GetTimeNowForTest(); }
108 101
109 AccessTokenStore* NewAccessTokenStore() override { 102 AccessTokenStore* NewAccessTokenStore() override {
110 return access_token_store_.get(); 103 return access_token_store_.get();
111 } 104 }
112 105
113 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( 106 std::unique_ptr<LocationProvider> NewNetworkLocationProvider(
114 AccessTokenStore* access_token_store, 107 AccessTokenStore* access_token_store,
115 net::URLRequestContextGetter* context, 108 net::URLRequestContextGetter* context,
116 const GURL& url, 109 const GURL& url,
117 const base::string16& access_token) override { 110 const base::string16& access_token) override {
118 cell_ = new MockLocationProvider; 111 cell_ = new MockLocationProvider;
119 return base::WrapUnique(cell_); 112 return base::WrapUnique(cell_);
120 } 113 }
121 114
122 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { 115 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override {
123 gps_ = new MockLocationProvider; 116 gps_ = new MockLocationProvider;
124 return base::WrapUnique(gps_); 117 return base::WrapUnique(gps_);
125 } 118 }
126 119
127 FakeGeolocationDelegate* GetFakeGeolocationDelegate() { 120 FakeGeolocationDelegate* GetFakeGeolocationDelegate() {
128 DCHECK(is_fake_delegate_); 121 return static_cast<FakeGeolocationDelegate*>(delegate_);
129 return static_cast<FakeGeolocationDelegate*>(GetDelegateForTesting());
130 } 122 }
131 123
132 LocationProvider* GetLocationProvider() { 124 MockLocationProvider* GetMockLocationProvider() {
133 if (is_fake_delegate_) 125 // Return the first location provider, which is anyway the only one.
134 return GetFakeGeolocationDelegate()->system_location_provider(); 126 return static_cast<MockLocationProvider*>(providers_.front().get());
Wez 2016/07/08 23:12:57 |providers_| is private, so I don't think you can
mcasas 2016/07/09 01:24:18 I can, because I made TestingLocationArbitrator fr
135 return GetDelegateForTesting()->OverrideSystemLocationProvider();
136 } 127 }
137 128
138 const bool is_fake_delegate_;
139
140 // Two location providers, with nice short names to make the tests more 129 // Two location providers, with nice short names to make the tests more
141 // readable. Note |gps_| will only be set when there is a high accuracy 130 // readable. Note |gps_| will only be set when there is a high accuracy
142 // observer registered (and |cell_| when there's at least one observer of any 131 // observer registered (and |cell_| when there's at least one observer of any
143 // type). 132 // type).
144
145 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and 133 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and
146 // |gps_| to |system_location_provider_| 134 // |gps_| to |gps_location_provider_|
147 MockLocationProvider* cell_; 135 MockLocationProvider* cell_;
148 MockLocationProvider* gps_; 136 MockLocationProvider* gps_;
137 std::unique_ptr<LocationProvider> system_location_provider_;
149 scoped_refptr<AccessTokenStore> access_token_store_; 138 scoped_refptr<AccessTokenStore> access_token_store_;
150 }; 139 };
151 140
152 } // namespace
153
154 class GeolocationLocationArbitratorTest : public testing::Test { 141 class GeolocationLocationArbitratorTest : public testing::Test {
155 protected: 142 protected:
156 // testing::Test 143 // testing::Test
157 void SetUp() override { 144 void SetUp() override {
158 access_token_store_ = new NiceMock<FakeAccessTokenStore>; 145 access_token_store_ = new NiceMock<FakeAccessTokenStore>;
159 observer_.reset(new MockLocationObserver); 146 observer_.reset(new MockLocationObserver);
160 } 147 }
161 148
162 // There are two types of test cases: those using FakeGeolocationDelegate and 149 // There are two types of test cases: those using FakeGeolocationDelegate and
163 // the ones exercising whatever the embedder provides. Test cases call this 150 // the ones exercising whatever the embedder provides. Test cases call this
164 // method to choose the appropriate one. 151 // method to choose the appropriate one.
165 void InitializeArbitrator(bool use_fake_delegate) { 152 void InitializeArbitrator(bool use_fake_delegate) {
Wez 2016/07/08 23:12:57 Rather than use true/false at call-sites and docum
mcasas 2016/07/09 01:24:18 Done. Made InitializeArbitrator() accept an overri
153 use_fake_delegate_ = use_fake_delegate;
166 delegate_.reset(use_fake_delegate ? new FakeGeolocationDelegate() 154 delegate_.reset(use_fake_delegate ? new FakeGeolocationDelegate()
167 : new GeolocationDelegate); 155 : new GeolocationDelegate);
168 const LocationArbitratorImpl::LocationUpdateCallback callback = 156 const LocationArbitratorImpl::LocationUpdateCallback callback =
169 base::Bind(&MockLocationObserver::OnLocationUpdate, 157 base::Bind(&MockLocationObserver::OnLocationUpdate,
170 base::Unretained(observer_.get())); 158 base::Unretained(observer_.get()));
171 arbitrator_.reset( 159 arbitrator_.reset(new TestingLocationArbitrator(
172 new TestingLocationArbitrator(callback, access_token_store_.get(), 160 callback, access_token_store_.get(), delegate_.get()));
173 delegate_.get(), use_fake_delegate));
174 } 161 }
175 162
176 // testing::Test 163 // testing::Test
177 void TearDown() override {} 164 void TearDown() override {}
178 165
179 void CheckLastPositionInfo(double latitude, 166 void CheckLastPositionInfo(double latitude,
180 double longitude, 167 double longitude,
181 double accuracy) { 168 double accuracy) {
182 Geoposition geoposition = observer_->last_position_; 169 Geoposition geoposition = observer_->last_position_;
183 EXPECT_TRUE(geoposition.Validate()); 170 EXPECT_TRUE(geoposition.Validate());
(...skipping 10 matching lines...) Expand all
194 181
195 MockLocationProvider* cell() { 182 MockLocationProvider* cell() {
196 return arbitrator_->cell_; 183 return arbitrator_->cell_;
197 } 184 }
198 185
199 MockLocationProvider* gps() { 186 MockLocationProvider* gps() {
200 return arbitrator_->gps_; 187 return arbitrator_->gps_;
201 } 188 }
202 189
203 MockLocationProvider* GetSystemLocationProviderOverride() { 190 MockLocationProvider* GetSystemLocationProviderOverride() {
204 return static_cast<MockLocationProvider*>( 191 if (!use_fake_delegate_)
205 arbitrator_->GetLocationProvider()); 192 return nullptr;
193 return arbitrator_->GetMockLocationProvider();
206 } 194 }
207 195
208 scoped_refptr<FakeAccessTokenStore> access_token_store_; 196 scoped_refptr<FakeAccessTokenStore> access_token_store_;
209 std::unique_ptr<MockLocationObserver> observer_; 197 std::unique_ptr<MockLocationObserver> observer_;
210 std::unique_ptr<TestingLocationArbitrator> arbitrator_; 198 std::unique_ptr<TestingLocationArbitrator> arbitrator_;
199 bool use_fake_delegate_;
211 std::unique_ptr<GeolocationDelegate> delegate_; 200 std::unique_ptr<GeolocationDelegate> delegate_;
212 base::MessageLoop loop_; 201 base::MessageLoop loop_;
213 }; 202 };
214 203
215 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { 204 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) {
216 EXPECT_TRUE(access_token_store_.get()); 205 EXPECT_TRUE(access_token_store_.get());
217 InitializeArbitrator(true /* use_fake_delegate */); 206 InitializeArbitrator(true /* use_fake_delegate */);
218 EXPECT_TRUE(arbitrator_); 207 EXPECT_TRUE(arbitrator_);
219 arbitrator_.reset(); 208 arbitrator_.reset();
220 SUCCEED(); 209 SUCCEED();
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 448
460 // Advance the time a short while to simulate successive calls. 449 // Advance the time a short while to simulate successive calls.
461 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); 450 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5));
462 451
463 // Update with a less accurate position to verify 240956. 452 // Update with a less accurate position to verify 240956.
464 SetPositionFix(cell(), 3, 139, 150); 453 SetPositionFix(cell(), 3, 139, 150);
465 CheckLastPositionInfo(3, 139, 150); 454 CheckLastPositionInfo(3, 139, 150);
466 } 455 }
467 456
468 } // namespace content 457 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698