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

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: Updates LocationProviderBase derived classes. 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(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 to handle
121 // correctly reinitializing GeolocationProviderImpl (Singleton)
Wez 2016/08/12 00:33:44 It needs to be before all other variables so that
CJ 2016/08/12 20:22:46 Done.
122 // |message_loop_| must come next to include all other variable construction
123 // on the correct thread.
Wez 2016/08/12 00:33:44 nit: Move this comment down to the message_loop_ m
CJ 2016/08/12 20:22:46 Done.
124 base::AtExitManager at_exit_;
133 base::MessageLoopForUI message_loop_; 125 base::MessageLoopForUI message_loop_;
134 std::unique_ptr<LocationProviderForTestArbitrator> provider_; 126
127 // Owned by the GeolocationProviderImpl class.
128 MockLocationProvider* arbitrator_;
135 }; 129 };
136 130
137 bool GeolocationProviderTest::ProvidersStarted() { 131 bool GeolocationProviderTest::ProvidersStarted() {
138 DCHECK(provider_->IsRunning()); 132 DCHECK(provider()->IsRunning());
139 DCHECK(base::MessageLoop::current() == &message_loop_); 133 DCHECK(base::MessageLoop::current() == &message_loop_);
134
140 bool started; 135 bool started;
141 provider_->task_runner()->PostTaskAndReply( 136 provider()->task_runner()->PostTaskAndReply(
142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, 137 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
143 base::Unretained(this), &started), 138 base::Unretained(this), &started),
144 base::MessageLoop::QuitWhenIdleClosure()); 139 base::MessageLoop::QuitWhenIdleClosure());
145 base::RunLoop().Run(); 140 base::RunLoop().Run();
146 return started; 141 return started;
147 } 142 }
148 143
149 void GeolocationProviderTest::GetProvidersStarted(bool* started) { 144 void GeolocationProviderTest::GetProvidersStarted(bool* started) {
150 DCHECK(provider_->task_runner()->BelongsToCurrentThread()); 145 DCHECK(provider()->task_runner()->BelongsToCurrentThread());
151 *started = provider_->mock_arbitrator()->providers_started(); 146 *started = arbitrator()->providers_started();
152 } 147 }
153 148
154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { 149 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
155 DCHECK(provider_->IsRunning()); 150 DCHECK(provider()->IsRunning());
156 DCHECK(base::MessageLoop::current() == &message_loop_); 151 DCHECK(base::MessageLoop::current() == &message_loop_);
157 provider_->task_runner()->PostTask( 152 provider()->task_runner()->PostTask(
158 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, 153 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate,
159 base::Unretained(provider_.get()), position)); 154 base::Unretained(provider()), position));
160 } 155 }
161 156
162 // Regression test for http://crbug.com/59377 157 // Regression test for http://crbug.com/59377
163 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { 158 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) {
159 provider()->SetArbitratorForTesting(nullptr);
164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); 160 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing());
165 provider()->UserDidOptIntoLocationServices(); 161 provider()->UserDidOptIntoLocationServices();
166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); 162 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing());
167 } 163 }
168 164
169 void DummyFunction(const Geoposition& position) {}
170
171 TEST_F(GeolocationProviderTest, StartStop) { 165 TEST_F(GeolocationProviderTest, StartStop) {
172 EXPECT_FALSE(provider()->IsRunning()); 166 EXPECT_FALSE(provider()->IsRunning());
173 GeolocationProviderImpl::LocationUpdateCallback callback = 167 GeolocationProviderImpl::LocationUpdateCallback callback =
174 base::Bind(&DummyFunction); 168 base::Bind(&DummyFunction);
175 std::unique_ptr<GeolocationProvider::Subscription> subscription = 169 std::unique_ptr<GeolocationProvider::Subscription> subscription =
176 provider()->AddLocationUpdateCallback(callback, false); 170 provider()->AddLocationUpdateCallback(callback, false);
177 EXPECT_TRUE(provider()->IsRunning()); 171 EXPECT_TRUE(provider()->IsRunning());
178 EXPECT_TRUE(ProvidersStarted()); 172 EXPECT_TRUE(ProvidersStarted());
179 173
180 subscription.reset(); 174 subscription.reset();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, 236 base::Bind(&MockGeolocationObserver::OnLocationUpdate,
243 base::Unretained(&mock_observer)); 237 base::Unretained(&mock_observer));
244 std::unique_ptr<GeolocationProvider::Subscription> subscription = 238 std::unique_ptr<GeolocationProvider::Subscription> subscription =
245 provider()->AddLocationUpdateCallback(callback, false); 239 provider()->AddLocationUpdateCallback(callback, false);
246 subscription.reset(); 240 subscription.reset();
247 // Wait for the providers to be stopped now that all clients are gone. 241 // Wait for the providers to be stopped now that all clients are gone.
248 EXPECT_FALSE(ProvidersStarted()); 242 EXPECT_FALSE(ProvidersStarted());
249 } 243 }
250 244
251 } // namespace device 245 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698