OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/memory/singleton.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/bind_helpers.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" |
6 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
7 #include "content/browser/geolocation/arbitrator_dependency_factories_for_test.h
" | 13 #include "base/time.h" |
| 14 #include "content/browser/geolocation/arbitrator_dependency_factory.h" |
8 #include "content/browser/geolocation/fake_access_token_store.h" | 15 #include "content/browser/geolocation/fake_access_token_store.h" |
| 16 #include "content/browser/geolocation/geolocation_observer.h" |
9 #include "content/browser/geolocation/geolocation_provider.h" | 17 #include "content/browser/geolocation/geolocation_provider.h" |
10 #include "content/browser/geolocation/location_arbitrator.h" | 18 #include "content/browser/geolocation/location_arbitrator.h" |
11 #include "content/browser/geolocation/mock_location_provider.h" | 19 #include "content/browser/geolocation/mock_location_provider.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/test/test_browser_thread.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
14 | 24 |
15 using content::AccessTokenStore; | |
16 using content::FakeAccessTokenStore; | |
17 using content::Geoposition; | 25 using content::Geoposition; |
18 using testing::_; | 26 using testing::_; |
19 using testing::DoAll; | 27 using testing::DoAll; |
20 using testing::DoDefault; | |
21 using testing::Invoke; | 28 using testing::Invoke; |
22 using testing::InvokeWithoutArgs; | 29 using testing::InvokeWithoutArgs; |
| 30 using testing::MakeMatcher; |
| 31 using testing::Matcher; |
| 32 using testing::MatcherInterface; |
| 33 using testing::MatchResultListener; |
23 | 34 |
24 namespace { | 35 namespace { |
| 36 class NonSingletonGeolocationProvider : public GeolocationProvider { |
| 37 public: |
| 38 NonSingletonGeolocationProvider() {} |
| 39 |
| 40 virtual ~NonSingletonGeolocationProvider() {} |
| 41 }; |
| 42 |
| 43 class StartStopMockLocationProvider : public MockLocationProvider { |
| 44 public: |
| 45 explicit StartStopMockLocationProvider() : MockLocationProvider(&instance_) { |
| 46 } |
| 47 |
| 48 virtual ~StartStopMockLocationProvider() { |
| 49 Die(); |
| 50 } |
| 51 |
| 52 MOCK_METHOD0(Die, void()); |
| 53 }; |
| 54 |
| 55 class TestingDependencyFactory |
| 56 : public DefaultGeolocationArbitratorDependencyFactory { |
| 57 public: |
| 58 TestingDependencyFactory(base::WaitableEvent* event) : event_(event) { |
| 59 } |
| 60 |
| 61 virtual content::AccessTokenStore* NewAccessTokenStore() OVERRIDE { |
| 62 content::FakeAccessTokenStore* store = new content::FakeAccessTokenStore(); |
| 63 EXPECT_CALL(*store, LoadAccessTokens(_)) |
| 64 .WillRepeatedly(DoAll( |
| 65 Invoke(store, |
| 66 &content::FakeAccessTokenStore::DefaultLoadAccessTokens), |
| 67 InvokeWithoutArgs(store, |
| 68 &content::FakeAccessTokenStore:: |
| 69 NotifyDelegateTokensLoaded), |
| 70 InvokeWithoutArgs(event_, &base::WaitableEvent::Signal))); |
| 71 return store; |
| 72 } |
| 73 |
| 74 virtual LocationProviderBase* NewNetworkLocationProvider( |
| 75 content::AccessTokenStore* access_token_store, |
| 76 net::URLRequestContextGetter* context, |
| 77 const GURL& url, |
| 78 const string16& access_token) OVERRIDE { |
| 79 StartStopMockLocationProvider* provider = |
| 80 new StartStopMockLocationProvider(); |
| 81 EXPECT_CALL(*provider, Die()) |
| 82 .Times(1) |
| 83 .WillOnce(InvokeWithoutArgs(event_, &base::WaitableEvent::Signal)); |
| 84 return provider; |
| 85 } |
| 86 |
| 87 virtual LocationProviderBase* NewSystemLocationProvider() OVERRIDE { |
| 88 return NULL; |
| 89 } |
| 90 |
| 91 private: |
| 92 base::WaitableEvent* event_; |
| 93 }; |
| 94 |
| 95 class NullGeolocationObserver : public GeolocationObserver { |
| 96 public: |
| 97 // GeolocationObserver |
| 98 virtual void OnLocationUpdate(const content::Geoposition& position) {} |
| 99 }; |
| 100 |
| 101 class MockGeolocationObserver : public GeolocationObserver { |
| 102 public: |
| 103 // GeolocationObserver |
| 104 MOCK_METHOD1(OnLocationUpdate, void(const content::Geoposition& position)); |
| 105 }; |
| 106 |
| 107 class MockGeolocationCallbackWrapper { |
| 108 public: |
| 109 MOCK_METHOD1(Callback, void(const content::Geoposition& position)); |
| 110 }; |
| 111 |
| 112 class GeopositionEqMatcher |
| 113 : public MatcherInterface<const content::Geoposition&> { |
| 114 public: |
| 115 explicit GeopositionEqMatcher(const content::Geoposition& expected) |
| 116 : expected_(expected) {} |
| 117 |
| 118 virtual bool MatchAndExplain(const content::Geoposition& actual, |
| 119 MatchResultListener* listener) const OVERRIDE { |
| 120 return actual.latitude == expected_.latitude && |
| 121 actual.longitude == expected_.longitude && |
| 122 actual.altitude == expected_.altitude && |
| 123 actual.accuracy == expected_.accuracy && |
| 124 actual.altitude_accuracy == expected_.altitude_accuracy && |
| 125 actual.heading == expected_.heading && |
| 126 actual.speed == expected_.speed && |
| 127 actual.timestamp == expected_.timestamp && |
| 128 actual.error_code == expected_.error_code && |
| 129 actual.error_message == expected_.error_message; |
| 130 } |
| 131 |
| 132 virtual void DescribeTo(::std::ostream* os) const OVERRIDE { |
| 133 *os << "which matches the expected position"; |
| 134 } |
| 135 |
| 136 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE{ |
| 137 *os << "which does not match the expected position"; |
| 138 } |
| 139 |
| 140 private: |
| 141 content::Geoposition expected_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher); |
| 144 }; |
| 145 |
| 146 Matcher<const content::Geoposition&> GeopositionEq( |
| 147 const content::Geoposition& expected) { |
| 148 return MakeMatcher(new GeopositionEqMatcher(expected)); |
| 149 } |
25 | 150 |
26 class GeolocationProviderTest : public testing::Test { | 151 class GeolocationProviderTest : public testing::Test { |
27 protected: | 152 protected: |
28 GeolocationProviderTest() | 153 GeolocationProviderTest() |
29 : provider_(new GeolocationProvider), | 154 : message_loop_(), |
30 dependency_factory_( | 155 io_thread_(content::BrowserThread::IO, &message_loop_), |
31 new GeolocationArbitratorDependencyFactoryWithLocationProvider( | 156 event_(false, false), |
32 &NewAutoSuccessMockNetworkLocationProvider)) | 157 dependency_factory_(new TestingDependencyFactory(&event_)), |
33 { | 158 provider_(new NonSingletonGeolocationProvider) { |
34 } | |
35 | |
36 ~GeolocationProviderTest() { | |
37 DefaultSingletonTraits<GeolocationProvider>::Delete(provider_); | |
38 } | |
39 | |
40 // testing::Test | |
41 virtual void SetUp() { | |
42 GeolocationArbitrator::SetDependencyFactoryForTest( | 159 GeolocationArbitrator::SetDependencyFactoryForTest( |
43 dependency_factory_.get()); | 160 dependency_factory_.get()); |
44 } | 161 } |
45 | 162 |
46 // testing::Test | 163 ~GeolocationProviderTest() { |
47 virtual void TearDown() { | |
48 provider_->Stop(); | |
49 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | 164 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); |
50 } | 165 } |
51 | 166 |
52 // Message loop for main thread, as provider depends on it existing. | |
53 MessageLoop message_loop_; | 167 MessageLoop message_loop_; |
54 | 168 content::TestBrowserThread io_thread_; |
55 // Object under tests. Owned, but not a scoped_ptr due to specialized | 169 |
56 // destruction protocol. | 170 base::WaitableEvent event_; |
57 GeolocationProvider* provider_; | 171 scoped_refptr<TestingDependencyFactory> dependency_factory_; |
58 | 172 scoped_ptr<NonSingletonGeolocationProvider> provider_; |
59 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory_; | |
60 }; | 173 }; |
61 | 174 |
62 // Regression test for http://crbug.com/59377 | 175 // Regression test for http://crbug.com/59377 |
63 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 176 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
64 EXPECT_FALSE(provider_->HasPermissionBeenGranted()); | 177 EXPECT_FALSE(provider_->HasPermissionBeenGranted()); |
65 provider_->OnPermissionGranted(); | 178 provider_->OnPermissionGranted(); |
66 EXPECT_TRUE(provider_->HasPermissionBeenGranted()); | 179 EXPECT_TRUE(provider_->HasPermissionBeenGranted()); |
67 } | 180 } |
68 | 181 |
69 class NullGeolocationObserver : public GeolocationObserver { | |
70 public: | |
71 // GeolocationObserver | |
72 virtual void OnLocationUpdate(const Geoposition& position) {} | |
73 }; | |
74 | |
75 class MockGeolocationObserver : public GeolocationObserver { | |
76 public: | |
77 // GeolocationObserver | |
78 MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position)); | |
79 }; | |
80 | |
81 class StartStopMockLocationProvider : public MockLocationProvider { | |
82 public: | |
83 explicit StartStopMockLocationProvider(MessageLoop* test_loop) | |
84 : MockLocationProvider(&instance_), | |
85 test_loop_(test_loop) { | |
86 } | |
87 | |
88 virtual ~StartStopMockLocationProvider() { | |
89 Die(); | |
90 } | |
91 | |
92 MOCK_METHOD0(Die, void()); | |
93 | |
94 virtual bool StartProvider(bool high_accuracy) { | |
95 bool result = MockLocationProvider::StartProvider(high_accuracy); | |
96 test_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
97 return result; | |
98 } | |
99 | |
100 virtual void StopProvider() { | |
101 MockLocationProvider::StopProvider(); | |
102 test_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
103 } | |
104 | |
105 private: | |
106 MessageLoop* test_loop_; | |
107 }; | |
108 | |
109 class MockDependencyFactory : public GeolocationArbitratorDependencyFactory { | |
110 public: | |
111 MockDependencyFactory(MessageLoop* test_loop_, | |
112 AccessTokenStore* access_token_store) | |
113 : test_loop_(test_loop_), | |
114 access_token_store_(access_token_store) { | |
115 } | |
116 | |
117 virtual net::URLRequestContextGetter* GetContextGetter() { | |
118 return NULL; | |
119 } | |
120 | |
121 virtual GeolocationArbitrator::GetTimeNow GetTimeFunction() { | |
122 return base::Time::Now; | |
123 } | |
124 | |
125 virtual AccessTokenStore* NewAccessTokenStore() { | |
126 return access_token_store_.get(); | |
127 } | |
128 | |
129 virtual LocationProviderBase* NewNetworkLocationProvider( | |
130 AccessTokenStore* access_token_store, | |
131 net::URLRequestContextGetter* context, | |
132 const GURL& url, | |
133 const string16& access_token) { | |
134 return new StartStopMockLocationProvider(test_loop_); | |
135 } | |
136 | |
137 virtual LocationProviderBase* NewSystemLocationProvider() { | |
138 return NULL; | |
139 } | |
140 | |
141 private: | |
142 virtual ~MockDependencyFactory() {} | |
143 | |
144 MessageLoop* test_loop_; | |
145 scoped_refptr<AccessTokenStore> access_token_store_; | |
146 }; | |
147 | |
148 TEST_F(GeolocationProviderTest, StartStop) { | 182 TEST_F(GeolocationProviderTest, StartStop) { |
149 scoped_refptr<FakeAccessTokenStore> fake_access_token_store = | |
150 new FakeAccessTokenStore; | |
151 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory = | |
152 new MockDependencyFactory(&message_loop_, fake_access_token_store.get()); | |
153 base::WaitableEvent event(false, false); | |
154 | |
155 EXPECT_CALL(*(fake_access_token_store.get()), LoadAccessTokens(_)) | |
156 .Times(1) | |
157 .WillOnce(DoAll(Invoke(fake_access_token_store.get(), | |
158 &FakeAccessTokenStore::DefaultLoadAccessTokens), | |
159 InvokeWithoutArgs(&event, &base::WaitableEvent::Signal))); | |
160 | |
161 GeolocationArbitrator::SetDependencyFactoryForTest(dependency_factory.get()); | |
162 | |
163 EXPECT_FALSE(provider_->IsRunning()); | 183 EXPECT_FALSE(provider_->IsRunning()); |
164 NullGeolocationObserver null_observer; | 184 NullGeolocationObserver null_observer; |
165 GeolocationObserverOptions options; | 185 GeolocationObserverOptions options; |
166 provider_->AddObserver(&null_observer, options); | 186 provider_->AddObserver(&null_observer, options); |
167 EXPECT_TRUE(provider_->IsRunning()); | 187 EXPECT_TRUE(provider_->IsRunning()); |
168 // Wait for token load request from the arbitrator to come through. | 188 // Wait for token load request from the arbitrator to come through. |
169 event.Wait(); | 189 event_.Wait(); |
170 event.Reset(); | 190 |
171 // The GeolocationArbitrator won't start the providers until it has | 191 event_.Reset(); |
172 // finished loading access tokens. | |
173 fake_access_token_store->NotifyDelegateTokensLoaded(); | |
174 message_loop_.Run(); | |
175 EXPECT_EQ(MockLocationProvider::instance_->state_, | 192 EXPECT_EQ(MockLocationProvider::instance_->state_, |
176 MockLocationProvider::LOW_ACCURACY); | 193 MockLocationProvider::LOW_ACCURACY); |
177 EXPECT_CALL(*(static_cast<StartStopMockLocationProvider*>( | |
178 MockLocationProvider::instance_)), | |
179 Die()) | |
180 .Times(1) | |
181 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); | |
182 provider_->RemoveObserver(&null_observer); | 194 provider_->RemoveObserver(&null_observer); |
183 // Wait for the providers to be stopped. | 195 // Wait for the providers to be stopped. |
184 event.Wait(); | 196 event_.Wait(); |
185 event.Reset(); | |
186 EXPECT_TRUE(provider_->IsRunning()); | 197 EXPECT_TRUE(provider_->IsRunning()); |
187 | |
188 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | |
189 } | 198 } |
190 | 199 |
191 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 200 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
192 scoped_refptr<FakeAccessTokenStore> fake_access_token_store = | 201 content::Geoposition position; |
193 new FakeAccessTokenStore; | 202 position.error_code = content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
194 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory = | 203 provider_->OverrideLocationForTesting(position); |
195 new MockDependencyFactory(&message_loop_, fake_access_token_store.get()); | 204 // Adding an observer when the location is overridden should synchronously |
| 205 // update the observer with our overridden position. |
196 MockGeolocationObserver mock_observer; | 206 MockGeolocationObserver mock_observer; |
197 EXPECT_CALL(*(fake_access_token_store.get()), LoadAccessTokens(_)); | 207 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
198 | |
199 GeolocationArbitrator::SetDependencyFactoryForTest(dependency_factory.get()); | |
200 | |
201 Geoposition override_position; | |
202 override_position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
203 provider_->OverrideLocationForTesting(override_position); | |
204 // Adding an observer when the location is overriden should synchronously | |
205 // update the observer with our overriden position. | |
206 EXPECT_CALL(mock_observer, OnLocationUpdate( | |
207 testing::Field( | |
208 &Geoposition::error_code, | |
209 testing::Eq(Geoposition::ERROR_CODE_POSITION_UNAVAILABLE)))); | |
210 provider_->AddObserver(&mock_observer, GeolocationObserverOptions()); | 208 provider_->AddObserver(&mock_observer, GeolocationObserverOptions()); |
211 provider_->RemoveObserver(&mock_observer); | 209 provider_->RemoveObserver(&mock_observer); |
212 | 210 } |
213 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | 211 |
| 212 TEST_F(GeolocationProviderTest, Callback) { |
| 213 MockGeolocationCallbackWrapper callback_wrapper; |
| 214 provider_->RequestCallback( |
| 215 base::Bind(&MockGeolocationCallbackWrapper::Callback, |
| 216 base::Unretained(&callback_wrapper))); |
| 217 |
| 218 content::Geoposition position; |
| 219 position.latitude = 12; |
| 220 position.longitude = 34; |
| 221 position.accuracy = 56; |
| 222 position.timestamp = base::Time::Now(); |
| 223 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); |
| 224 provider_->OverrideLocationForTesting(position); |
214 } | 225 } |
215 | 226 |
216 } // namespace | 227 } // namespace |
OLD | NEW |