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/time/time.h" | 19 #include "base/time/time.h" |
20 #include "device/geolocation/access_token_store.h" | 20 #include "device/geolocation/access_token_store.h" |
21 #include "device/geolocation/mock_location_provider.h" | 21 #include "device/geolocation/fake_location_provider.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using testing::MakeMatcher; | 25 using testing::MakeMatcher; |
26 using testing::Matcher; | 26 using testing::Matcher; |
27 using testing::MatcherInterface; | 27 using testing::MatcherInterface; |
28 using testing::MatchResultListener; | 28 using testing::MatchResultListener; |
29 | 29 |
30 namespace device { | 30 namespace device { |
31 namespace { | 31 namespace { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { | 90 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
91 return MakeMatcher(new GeopositionEqMatcher(expected)); | 91 return MakeMatcher(new GeopositionEqMatcher(expected)); |
92 } | 92 } |
93 | 93 |
94 void DummyFunction(const Geoposition& position) {} | 94 void DummyFunction(const Geoposition& position) {} |
95 | 95 |
96 } // namespace | 96 } // namespace |
97 | 97 |
98 class GeolocationProviderTest : public testing::Test { | 98 class GeolocationProviderTest : public testing::Test { |
99 protected: | 99 protected: |
100 GeolocationProviderTest() : arbitrator_(new MockLocationProvider) { | 100 GeolocationProviderTest() : arbitrator_(new FakeLocationProvider) { |
101 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); | 101 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); |
102 } | 102 } |
103 | 103 |
104 ~GeolocationProviderTest() override {} | 104 ~GeolocationProviderTest() override {} |
105 | 105 |
106 GeolocationProviderImpl* provider() { | 106 GeolocationProviderImpl* provider() { |
107 return GeolocationProviderImpl::GetInstance(); | 107 return GeolocationProviderImpl::GetInstance(); |
108 } | 108 } |
109 | 109 |
110 MockLocationProvider* arbitrator() { return arbitrator_; } | 110 FakeLocationProvider* arbitrator() { return arbitrator_; } |
111 | 111 |
112 // Called on test thread. | 112 // Called on test thread. |
113 bool ProvidersStarted(); | 113 bool ProvidersStarted(); |
114 void SendMockLocation(const Geoposition& position); | 114 void SendMockLocation(const Geoposition& position); |
115 | 115 |
116 private: | 116 private: |
117 // Called on provider thread. | 117 // Called on provider thread. |
118 void GetProvidersStarted(bool* started); | 118 void GetProvidersStarted(bool* started); |
119 | 119 |
120 // |at_exit| must be initialized before all other variables so that it is | 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 | 121 // available to register with Singletons and can handle tear down when the |
122 // test completes. | 122 // test completes. |
123 base::ShadowingAtExitManager at_exit_; | 123 base::ShadowingAtExitManager at_exit_; |
124 | 124 |
125 base::MessageLoopForUI message_loop_; | 125 base::MessageLoopForUI message_loop_; |
126 | 126 |
127 // Owned by the GeolocationProviderImpl class. | 127 // Owned by the GeolocationProviderImpl class. |
128 MockLocationProvider* arbitrator_; | 128 FakeLocationProvider* arbitrator_; |
129 | 129 |
130 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); | 130 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); |
131 }; | 131 }; |
132 | 132 |
133 bool GeolocationProviderTest::ProvidersStarted() { | 133 bool GeolocationProviderTest::ProvidersStarted() { |
134 DCHECK(provider()->IsRunning()); | 134 DCHECK(provider()->IsRunning()); |
135 DCHECK(base::MessageLoop::current() == &message_loop_); | 135 DCHECK(base::MessageLoop::current() == &message_loop_); |
136 | 136 |
137 bool started; | 137 bool started; |
138 provider()->task_runner()->PostTaskAndReply( | 138 provider()->task_runner()->PostTaskAndReply( |
139 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 139 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
140 base::Unretained(this), &started), | 140 base::Unretained(this), &started), |
141 base::MessageLoop::QuitWhenIdleClosure()); | 141 base::MessageLoop::QuitWhenIdleClosure()); |
142 base::RunLoop().Run(); | 142 base::RunLoop().Run(); |
143 return started; | 143 return started; |
144 } | 144 } |
145 | 145 |
146 void GeolocationProviderTest::GetProvidersStarted(bool* started) { | 146 void GeolocationProviderTest::GetProvidersStarted(bool* started) { |
147 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); | 147 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); |
148 *started = arbitrator()->IsProviderStarted(); | 148 *started = arbitrator()->get_state() != FakeLocationProvider::STOPPED; |
Wez
2016/08/20 01:42:11
You've defined get_state() as returning a bool, no
CJ
2016/08/22 17:56:40
Done.
| |
149 } | 149 } |
150 | 150 |
151 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { | 151 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
152 DCHECK(provider()->IsRunning()); | 152 DCHECK(provider()->IsRunning()); |
153 DCHECK(base::MessageLoop::current() == &message_loop_); | 153 DCHECK(base::MessageLoop::current() == &message_loop_); |
154 provider()->task_runner()->PostTask( | 154 provider()->task_runner()->PostTask( |
155 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, | 155 FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
156 base::Unretained(provider()), position)); | 156 base::Unretained(provider()), position)); |
157 } | 157 } |
158 | 158 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 base::Bind(&MockGeolocationObserver::OnLocationUpdate, | 238 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
239 base::Unretained(&mock_observer)); | 239 base::Unretained(&mock_observer)); |
240 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 240 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
241 provider()->AddLocationUpdateCallback(callback, false); | 241 provider()->AddLocationUpdateCallback(callback, false); |
242 subscription.reset(); | 242 subscription.reset(); |
243 // 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. |
244 EXPECT_FALSE(ProvidersStarted()); | 244 EXPECT_FALSE(ProvidersStarted()); |
245 } | 245 } |
246 | 246 |
247 } // namespace device | 247 } // namespace device |
OLD | NEW |