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

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: Updated location_provider_android.h Created 4 years, 4 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 Geoposition& position) {}
95
96 } // namespace
97
116 class GeolocationProviderTest : public testing::Test { 98 class GeolocationProviderTest : public testing::Test {
117 protected: 99 protected:
118 GeolocationProviderTest() 100 GeolocationProviderTest() : arbitrator_(new MockLocationProvider) {
119 : message_loop_(), provider_(new LocationProviderForTestArbitrator) {} 101 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_));
102 }
120 103
121 ~GeolocationProviderTest() override {} 104 ~GeolocationProviderTest() override {}
122 105
123 LocationProviderForTestArbitrator* provider() { return provider_.get(); } 106 GeolocationProviderImpl* provider() {
107 return GeolocationProviderImpl::GetInstance();
108 }
109
110 MockLocationProvider* arbitrator() { return arbitrator_; }
124 111
125 // Called on test thread. 112 // Called on test thread.
126 bool ProvidersStarted(); 113 bool ProvidersStarted();
127 void SendMockLocation(const Geoposition& position); 114 void SendMockLocation(const Geoposition& position);
128 115
129 private: 116 private:
130 // Called on provider thread. 117 // Called on provider thread.
131 void GetProvidersStarted(bool* started); 118 void GetProvidersStarted(bool* started);
132 119
120 // |at_exit| must be initialized before all other variables so that it is
121 // available to register with Singletons and can handle tear down when the
122 // test completes.
123 base::AtExitManager at_exit_;
124
125 // |message_loop_| must come next to include all other variable construction
Kevin M 2016/08/15 19:12:46 Relative statements like "come next to" is confusi
CJ 2016/08/15 20:11:51 Done.
126 // on the correct thread.
133 base::MessageLoopForUI message_loop_; 127 base::MessageLoopForUI message_loop_;
134 std::unique_ptr<LocationProviderForTestArbitrator> provider_; 128
129 // Owned by the GeolocationProviderImpl class.
130 MockLocationProvider* arbitrator_;
Kevin M 2016/08/15 19:12:46 DISALLOW_COPY_AND_ASSIGN
CJ 2016/08/15 20:11:51 Done.
135 }; 131 };
136 132
137 bool GeolocationProviderTest::ProvidersStarted() { 133 bool GeolocationProviderTest::ProvidersStarted() {
138 DCHECK(provider_->IsRunning()); 134 DCHECK(provider()->IsRunning());
139 DCHECK(base::MessageLoop::current() == &message_loop_); 135 DCHECK(base::MessageLoop::current() == &message_loop_);
136
140 bool started; 137 bool started;
141 provider_->task_runner()->PostTaskAndReply( 138 provider()->task_runner()->PostTaskAndReply(
142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, 139 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
143 base::Unretained(this), &started), 140 base::Unretained(this), &started),
144 base::MessageLoop::QuitWhenIdleClosure()); 141 base::MessageLoop::QuitWhenIdleClosure());
145 base::RunLoop().Run(); 142 base::RunLoop().Run();
146 return started; 143 return started;
147 } 144 }
148 145
149 void GeolocationProviderTest::GetProvidersStarted(bool* started) { 146 void GeolocationProviderTest::GetProvidersStarted(bool* started) {
150 DCHECK(provider_->task_runner()->BelongsToCurrentThread()); 147 DCHECK(provider()->task_runner()->BelongsToCurrentThread());
151 *started = provider_->mock_arbitrator()->providers_started(); 148 *started = arbitrator()->providers_started();
152 } 149 }
153 150
154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { 151 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
155 DCHECK(provider_->IsRunning()); 152 DCHECK(provider()->IsRunning());
156 DCHECK(base::MessageLoop::current() == &message_loop_); 153 DCHECK(base::MessageLoop::current() == &message_loop_);
157 provider_->task_runner()->PostTask( 154 provider()->task_runner()->PostTask(
158 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, 155 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate,
159 base::Unretained(provider_.get()), position)); 156 base::Unretained(provider()), position));
160 } 157 }
161 158
162 // Regression test for http://crbug.com/59377 159 // Regression test for http://crbug.com/59377
163 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { 160 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) {
161 provider()->SetArbitratorForTesting(nullptr);
164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); 162 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing());
165 provider()->UserDidOptIntoLocationServices(); 163 provider()->UserDidOptIntoLocationServices();
166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); 164 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing());
167 } 165 }
168 166
169 void DummyFunction(const Geoposition& position) {}
170
171 TEST_F(GeolocationProviderTest, StartStop) { 167 TEST_F(GeolocationProviderTest, StartStop) {
172 EXPECT_FALSE(provider()->IsRunning()); 168 EXPECT_FALSE(provider()->IsRunning());
173 GeolocationProviderImpl::LocationUpdateCallback callback = 169 GeolocationProviderImpl::LocationUpdateCallback callback =
174 base::Bind(&DummyFunction); 170 base::Bind(&DummyFunction);
175 std::unique_ptr<GeolocationProvider::Subscription> subscription = 171 std::unique_ptr<GeolocationProvider::Subscription> subscription =
176 provider()->AddLocationUpdateCallback(callback, false); 172 provider()->AddLocationUpdateCallback(callback, false);
177 EXPECT_TRUE(provider()->IsRunning()); 173 EXPECT_TRUE(provider()->IsRunning());
178 EXPECT_TRUE(ProvidersStarted()); 174 EXPECT_TRUE(ProvidersStarted());
179 175
180 subscription.reset(); 176 subscription.reset();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, 238 base::Bind(&MockGeolocationObserver::OnLocationUpdate,
243 base::Unretained(&mock_observer)); 239 base::Unretained(&mock_observer));
244 std::unique_ptr<GeolocationProvider::Subscription> subscription = 240 std::unique_ptr<GeolocationProvider::Subscription> subscription =
245 provider()->AddLocationUpdateCallback(callback, false); 241 provider()->AddLocationUpdateCallback(callback, false);
246 subscription.reset(); 242 subscription.reset();
247 // Wait for the providers to be stopped now that all clients are gone. 243 // Wait for the providers to be stopped now that all clients are gone.
248 EXPECT_FALSE(ProvidersStarted()); 244 EXPECT_FALSE(ProvidersStarted());
249 } 245 }
250 246
251 } // namespace device 247 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698