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

Side by Side Diff: device/geolocation/geolocation_provider_impl_unittest.cc

Issue 2226143002: Gets rid of the LocationArbitrator interface, in preference for LocationProvider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into lai Created 4 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "device/geolocation/geolocation_provider_impl.h" 5 #include "device/geolocation/geolocation_provider_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/at_exit.h"
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/location.h" 12 #include "base/location.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/run_loop.h" 16 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "device/geolocation/access_token_store.h" 20 #include "device/geolocation/access_token_store.h"
20 #include "device/geolocation/mock_location_arbitrator.h" 21 #include "device/geolocation/mock_location_provider.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 using testing::MakeMatcher; 25 using testing::MakeMatcher;
25 using testing::Matcher; 26 using testing::Matcher;
26 using testing::MatcherInterface; 27 using testing::MatcherInterface;
27 using testing::MatchResultListener; 28 using testing::MatchResultListener;
28 29
29 namespace device { 30 namespace device {
30 31 namespace {
31 class LocationProviderForTestArbitrator : public GeolocationProviderImpl {
32 public:
33 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {}
34 ~LocationProviderForTestArbitrator() override {}
35
36 // Only valid for use on the geolocation thread.
37 MockLocationArbitrator* mock_arbitrator() const { return mock_arbitrator_; }
38
39 protected:
40 // GeolocationProviderImpl implementation:
41 std::unique_ptr<LocationArbitrator> CreateArbitrator() override;
42
43 private:
44 // An alias to the arbitrator stored in the super class, where it is owned.
45 MockLocationArbitrator* mock_arbitrator_;
46 };
47
48 std::unique_ptr<LocationArbitrator>
49 LocationProviderForTestArbitrator::CreateArbitrator() {
50 DCHECK(mock_arbitrator_ == NULL);
51 mock_arbitrator_ = new MockLocationArbitrator;
52 return base::WrapUnique(mock_arbitrator_);
53 }
54 32
55 class GeolocationObserver { 33 class GeolocationObserver {
56 public: 34 public:
57 virtual ~GeolocationObserver() {} 35 virtual ~GeolocationObserver() {}
58 virtual void OnLocationUpdate(const Geoposition& position) = 0; 36 virtual void OnLocationUpdate(const Geoposition& position) = 0;
59 }; 37 };
60 38
61 class MockGeolocationObserver : public GeolocationObserver { 39 class MockGeolocationObserver : public GeolocationObserver {
62 public: 40 public:
63 MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position)); 41 MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 private: 84 private:
107 Geoposition expected_; 85 Geoposition expected_;
108 86
109 DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher); 87 DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher);
110 }; 88 };
111 89
112 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { 90 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) {
113 return MakeMatcher(new GeopositionEqMatcher(expected)); 91 return MakeMatcher(new GeopositionEqMatcher(expected));
114 } 92 }
115 93
94 void DummyFunction(const LocationProvider* provider,
95 const Geoposition& position) {}
96
97 } // namespace
98
116 class GeolocationProviderTest : public testing::Test { 99 class GeolocationProviderTest : public testing::Test {
117 protected: 100 protected:
118 GeolocationProviderTest() 101 GeolocationProviderTest() : arbitrator_(new MockLocationProvider) {
119 : message_loop_(), provider_(new LocationProviderForTestArbitrator) {} 102 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_));
103 }
120 104
121 ~GeolocationProviderTest() override {} 105 ~GeolocationProviderTest() override {}
122 106
123 LocationProviderForTestArbitrator* provider() { return provider_.get(); } 107 GeolocationProviderImpl* provider() {
108 return GeolocationProviderImpl::GetInstance();
109 }
110
111 MockLocationProvider* arbitrator() { return arbitrator_; }
124 112
125 // Called on test thread. 113 // Called on test thread.
126 bool ProvidersStarted(); 114 bool ProvidersStarted();
127 void SendMockLocation(const Geoposition& position); 115 void SendMockLocation(const Geoposition& position);
128 116
129 private: 117 private:
130 // Called on provider thread. 118 // Called on provider thread.
131 void GetProvidersStarted(bool* started); 119 void GetProvidersStarted(bool* started);
132 120
121 // |at_exit| must be initialized before all other variables so that it is
122 // available to register with Singletons and can handle tear down when the
123 // test completes.
124 base::ShadowingAtExitManager at_exit_;
125
133 base::MessageLoopForUI message_loop_; 126 base::MessageLoopForUI message_loop_;
134 std::unique_ptr<LocationProviderForTestArbitrator> provider_; 127
128 // Owned by the GeolocationProviderImpl class.
129 MockLocationProvider* arbitrator_;
130
131 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest);
135 }; 132 };
136 133
137 bool GeolocationProviderTest::ProvidersStarted() { 134 bool GeolocationProviderTest::ProvidersStarted() {
138 DCHECK(provider_->IsRunning()); 135 DCHECK(provider()->IsRunning());
139 DCHECK(base::MessageLoop::current() == &message_loop_); 136 DCHECK(base::MessageLoop::current() == &message_loop_);
137
140 bool started; 138 bool started;
141 provider_->task_runner()->PostTaskAndReply( 139 provider()->task_runner()->PostTaskAndReply(
142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, 140 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
143 base::Unretained(this), &started), 141 base::Unretained(this), &started),
144 base::MessageLoop::QuitWhenIdleClosure()); 142 base::MessageLoop::QuitWhenIdleClosure());
145 base::RunLoop().Run(); 143 base::RunLoop().Run();
146 return started; 144 return started;
147 } 145 }
148 146
149 void GeolocationProviderTest::GetProvidersStarted(bool* started) { 147 void GeolocationProviderTest::GetProvidersStarted(bool* started) {
150 DCHECK(provider_->task_runner()->BelongsToCurrentThread()); 148 DCHECK(provider()->task_runner()->BelongsToCurrentThread());
151 *started = provider_->mock_arbitrator()->providers_started(); 149 *started = arbitrator()->is_started();
152 } 150 }
153 151
154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { 152 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
155 DCHECK(provider_->IsRunning()); 153 DCHECK(provider()->IsRunning());
156 DCHECK(base::MessageLoop::current() == &message_loop_); 154 DCHECK(base::MessageLoop::current() == &message_loop_);
157 provider_->task_runner()->PostTask( 155 provider()->task_runner()->PostTask(
158 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, 156 FROM_HERE,
159 base::Unretained(provider_.get()), position)); 157 base::Bind(&GeolocationProviderImpl::OnLocationUpdate,
158 base::Unretained(provider()), arbitrator_, position));
160 } 159 }
161 160
162 // Regression test for http://crbug.com/59377 161 // Regression test for http://crbug.com/59377
163 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { 162 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) {
163 // Clear |provider|'s arbitrator so the default arbitrator can be used.
164 provider()->SetArbitratorForTesting(nullptr);
164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); 165 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing());
165 provider()->UserDidOptIntoLocationServices(); 166 provider()->UserDidOptIntoLocationServices();
166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); 167 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing());
167 } 168 }
168 169
169 void DummyFunction(const Geoposition& position) {}
170
171 TEST_F(GeolocationProviderTest, StartStop) { 170 TEST_F(GeolocationProviderTest, StartStop) {
172 EXPECT_FALSE(provider()->IsRunning()); 171 EXPECT_FALSE(provider()->IsRunning());
173 GeolocationProviderImpl::LocationUpdateCallback callback = 172 LocationProvider::LocationProviderUpdateCallback callback =
174 base::Bind(&DummyFunction); 173 base::Bind(&DummyFunction);
175 std::unique_ptr<GeolocationProvider::Subscription> subscription = 174 std::unique_ptr<GeolocationProvider::Subscription> subscription =
176 provider()->AddLocationUpdateCallback(callback, false); 175 provider()->AddLocationUpdateCallback(base::Bind(callback, arbitrator()),
176 false);
177 EXPECT_TRUE(provider()->IsRunning()); 177 EXPECT_TRUE(provider()->IsRunning());
178 EXPECT_TRUE(ProvidersStarted()); 178 EXPECT_TRUE(ProvidersStarted());
179 179
180 subscription.reset(); 180 subscription.reset();
181 181
182 EXPECT_FALSE(ProvidersStarted()); 182 EXPECT_FALSE(ProvidersStarted());
183 EXPECT_TRUE(provider()->IsRunning()); 183 EXPECT_TRUE(provider()->IsRunning());
184 } 184 }
185 185
186 TEST_F(GeolocationProviderTest, StalePositionNotSent) { 186 TEST_F(GeolocationProviderTest, StalePositionNotSent) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, 242 base::Bind(&MockGeolocationObserver::OnLocationUpdate,
243 base::Unretained(&mock_observer)); 243 base::Unretained(&mock_observer));
244 std::unique_ptr<GeolocationProvider::Subscription> subscription = 244 std::unique_ptr<GeolocationProvider::Subscription> subscription =
245 provider()->AddLocationUpdateCallback(callback, false); 245 provider()->AddLocationUpdateCallback(callback, false);
246 subscription.reset(); 246 subscription.reset();
247 // Wait for the providers to be stopped now that all clients are gone. 247 // Wait for the providers to be stopped now that all clients are gone.
248 EXPECT_FALSE(ProvidersStarted()); 248 EXPECT_FALSE(ProvidersStarted());
249 } 249 }
250 250
251 } // namespace device 251 } // namespace device
OLDNEW
« no previous file with comments | « device/geolocation/geolocation_provider_impl.cc ('k') | device/geolocation/location_arbitrator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698