OLD | NEW |
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/at_exit.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 19 #include "base/test/scoped_task_environment.h" |
| 20 #include "base/threading/thread_checker.h" |
19 #include "base/time/time.h" | 21 #include "base/time/time.h" |
20 #include "device/geolocation/access_token_store.h" | 22 #include "device/geolocation/access_token_store.h" |
21 #include "device/geolocation/fake_location_provider.h" | 23 #include "device/geolocation/fake_location_provider.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 using testing::MakeMatcher; | 27 using testing::MakeMatcher; |
26 using testing::Matcher; | 28 using testing::Matcher; |
27 using testing::MatcherInterface; | 29 using testing::MatcherInterface; |
28 using testing::MatchResultListener; | 30 using testing::MatchResultListener; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return MakeMatcher(new GeopositionEqMatcher(expected)); | 93 return MakeMatcher(new GeopositionEqMatcher(expected)); |
92 } | 94 } |
93 | 95 |
94 void DummyFunction(const LocationProvider* provider, | 96 void DummyFunction(const LocationProvider* provider, |
95 const Geoposition& position) {} | 97 const Geoposition& position) {} |
96 | 98 |
97 } // namespace | 99 } // namespace |
98 | 100 |
99 class GeolocationProviderTest : public testing::Test { | 101 class GeolocationProviderTest : public testing::Test { |
100 protected: | 102 protected: |
101 GeolocationProviderTest() : arbitrator_(new FakeLocationProvider) { | 103 GeolocationProviderTest() |
| 104 : scoped_task_environment_( |
| 105 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 106 arbitrator_(new FakeLocationProvider) { |
102 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); | 107 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); |
103 } | 108 } |
104 | 109 |
105 ~GeolocationProviderTest() override {} | 110 ~GeolocationProviderTest() override {} |
106 | 111 |
107 GeolocationProviderImpl* provider() { | 112 GeolocationProviderImpl* provider() { |
108 return GeolocationProviderImpl::GetInstance(); | 113 return GeolocationProviderImpl::GetInstance(); |
109 } | 114 } |
110 | 115 |
111 FakeLocationProvider* arbitrator() { return arbitrator_; } | 116 FakeLocationProvider* arbitrator() { return arbitrator_; } |
112 | 117 |
113 // Called on test thread. | 118 // Called on test thread. |
114 bool ProvidersStarted(); | 119 bool ProvidersStarted(); |
115 void SendMockLocation(const Geoposition& position); | 120 void SendMockLocation(const Geoposition& position); |
116 | 121 |
117 private: | 122 private: |
118 // Called on provider thread. | 123 // Called on provider thread. |
119 void GetProvidersStarted(); | 124 void GetProvidersStarted(); |
120 | 125 |
121 // |at_exit| must be initialized before all other variables so that it is | 126 // |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 | 127 // available to register with Singletons and can handle tear down when the |
123 // test completes. | 128 // test completes. |
124 base::ShadowingAtExitManager at_exit_; | 129 base::ShadowingAtExitManager at_exit_; |
125 | 130 |
126 base::MessageLoopForUI message_loop_; | 131 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 132 |
| 133 base::ThreadChecker thread_checker_; |
127 | 134 |
128 // Owned by the GeolocationProviderImpl class. | 135 // Owned by the GeolocationProviderImpl class. |
129 FakeLocationProvider* arbitrator_; | 136 FakeLocationProvider* arbitrator_; |
130 | 137 |
131 // True if |arbitrator_| is started. | 138 // True if |arbitrator_| is started. |
132 bool is_started_; | 139 bool is_started_; |
133 | 140 |
134 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); | 141 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); |
135 }; | 142 }; |
136 | 143 |
137 bool GeolocationProviderTest::ProvidersStarted() { | 144 bool GeolocationProviderTest::ProvidersStarted() { |
138 DCHECK(provider()->IsRunning()); | 145 DCHECK(provider()->IsRunning()); |
139 DCHECK(base::MessageLoop::current() == &message_loop_); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
140 | 147 |
141 provider()->task_runner()->PostTaskAndReply( | 148 provider()->task_runner()->PostTaskAndReply( |
142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 149 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
143 base::Unretained(this)), | 150 base::Unretained(this)), |
144 base::MessageLoop::QuitWhenIdleClosure()); | 151 base::MessageLoop::QuitWhenIdleClosure()); |
145 base::RunLoop().Run(); | 152 base::RunLoop().Run(); |
146 return is_started_; | 153 return is_started_; |
147 } | 154 } |
148 | 155 |
149 void GeolocationProviderTest::GetProvidersStarted() { | 156 void GeolocationProviderTest::GetProvidersStarted() { |
150 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); | 157 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); |
151 is_started_ = arbitrator()->state() != FakeLocationProvider::STOPPED; | 158 is_started_ = arbitrator()->state() != FakeLocationProvider::STOPPED; |
152 } | 159 } |
153 | 160 |
154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { | 161 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
155 DCHECK(provider()->IsRunning()); | 162 DCHECK(provider()->IsRunning()); |
156 DCHECK(base::MessageLoop::current() == &message_loop_); | 163 DCHECK(thread_checker_.CalledOnValidThread()); |
157 provider()->task_runner()->PostTask( | 164 provider()->task_runner()->PostTask( |
158 FROM_HERE, | 165 FROM_HERE, |
159 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, | 166 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
160 base::Unretained(provider()), arbitrator_, position)); | 167 base::Unretained(provider()), arbitrator_, position)); |
161 } | 168 } |
162 | 169 |
163 // Regression test for http://crbug.com/59377 | 170 // Regression test for http://crbug.com/59377 |
164 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 171 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
165 // Clear |provider|'s arbitrator so the default arbitrator can be used. | 172 // Clear |provider|'s arbitrator so the default arbitrator can be used. |
166 provider()->SetArbitratorForTesting(nullptr); | 173 provider()->SetArbitratorForTesting(nullptr); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, | 249 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
243 base::Unretained(&mock_observer)); | 250 base::Unretained(&mock_observer)); |
244 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 251 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
245 provider()->AddLocationUpdateCallback(callback, false); | 252 provider()->AddLocationUpdateCallback(callback, false); |
246 subscription.reset(); | 253 subscription.reset(); |
247 // Wait for the providers to be stopped now that all clients are gone. | 254 // Wait for the providers to be stopped now that all clients are gone. |
248 EXPECT_FALSE(ProvidersStarted()); | 255 EXPECT_FALSE(ProvidersStarted()); |
249 } | 256 } |
250 | 257 |
251 } // namespace device | 258 } // namespace device |
OLD | NEW |