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

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: Addresses kmarshall's #41 comments 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::ShadowingAtExitManager at_exit_;
124
125 // All other variables must be delcared after |at_exit_| to ensure they
Kevin M 2016/08/15 21:34:54 typo: declared Also, this comment is already spel
CJ 2016/08/15 23:09:52 Done.
126 // will be constructed 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_;
131
132 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest);
135 }; 133 };
136 134
137 bool GeolocationProviderTest::ProvidersStarted() { 135 bool GeolocationProviderTest::ProvidersStarted() {
138 DCHECK(provider_->IsRunning()); 136 DCHECK(provider()->IsRunning());
139 DCHECK(base::MessageLoop::current() == &message_loop_); 137 DCHECK(base::MessageLoop::current() == &message_loop_);
138
140 bool started; 139 bool started;
141 provider_->task_runner()->PostTaskAndReply( 140 provider()->task_runner()->PostTaskAndReply(
142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, 141 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
143 base::Unretained(this), &started), 142 base::Unretained(this), &started),
144 base::MessageLoop::QuitWhenIdleClosure()); 143 base::MessageLoop::QuitWhenIdleClosure());
145 base::RunLoop().Run(); 144 base::RunLoop().Run();
146 return started; 145 return started;
147 } 146 }
148 147
149 void GeolocationProviderTest::GetProvidersStarted(bool* started) { 148 void GeolocationProviderTest::GetProvidersStarted(bool* started) {
150 DCHECK(provider_->task_runner()->BelongsToCurrentThread()); 149 DCHECK(provider()->task_runner()->BelongsToCurrentThread());
151 *started = provider_->mock_arbitrator()->providers_started(); 150 *started = arbitrator()->IsProviderStarted();
152 } 151 }
153 152
154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { 153 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
155 DCHECK(provider_->IsRunning()); 154 DCHECK(provider()->IsRunning());
156 DCHECK(base::MessageLoop::current() == &message_loop_); 155 DCHECK(base::MessageLoop::current() == &message_loop_);
157 provider_->task_runner()->PostTask( 156 provider()->task_runner()->PostTask(
158 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, 157 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate,
159 base::Unretained(provider_.get()), position)); 158 base::Unretained(provider()), 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 provider()->SetArbitratorForTesting(nullptr);
Wez 2016/08/19 01:33:11 Add a comment to explain that we're clearing this
CJ 2016/08/22 17:41:31 Done.
164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); 164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing());
165 provider()->UserDidOptIntoLocationServices(); 165 provider()->UserDidOptIntoLocationServices();
166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); 166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing());
167 } 167 }
168 168
169 void DummyFunction(const Geoposition& position) {}
170
171 TEST_F(GeolocationProviderTest, StartStop) { 169 TEST_F(GeolocationProviderTest, StartStop) {
172 EXPECT_FALSE(provider()->IsRunning()); 170 EXPECT_FALSE(provider()->IsRunning());
173 GeolocationProviderImpl::LocationUpdateCallback callback = 171 GeolocationProviderImpl::LocationUpdateCallback callback =
174 base::Bind(&DummyFunction); 172 base::Bind(&DummyFunction);
175 std::unique_ptr<GeolocationProvider::Subscription> subscription = 173 std::unique_ptr<GeolocationProvider::Subscription> subscription =
176 provider()->AddLocationUpdateCallback(callback, false); 174 provider()->AddLocationUpdateCallback(callback, false);
177 EXPECT_TRUE(provider()->IsRunning()); 175 EXPECT_TRUE(provider()->IsRunning());
178 EXPECT_TRUE(ProvidersStarted()); 176 EXPECT_TRUE(ProvidersStarted());
179 177
180 subscription.reset(); 178 subscription.reset();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, 240 base::Bind(&MockGeolocationObserver::OnLocationUpdate,
243 base::Unretained(&mock_observer)); 241 base::Unretained(&mock_observer));
244 std::unique_ptr<GeolocationProvider::Subscription> subscription = 242 std::unique_ptr<GeolocationProvider::Subscription> subscription =
245 provider()->AddLocationUpdateCallback(callback, false); 243 provider()->AddLocationUpdateCallback(callback, false);
246 subscription.reset(); 244 subscription.reset();
247 // Wait for the providers to be stopped now that all clients are gone. 245 // Wait for the providers to be stopped now that all clients are gone.
248 EXPECT_FALSE(ProvidersStarted()); 246 EXPECT_FALSE(ProvidersStarted());
249 } 247 }
250 248
251 } // namespace device 249 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698