[email protected] | a980b05 | 2012-04-20 12:42:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 5 | #include "base/bind.h" |
| 6 | #include "base/bind_helpers.h" |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 7 | #include "base/memory/ref_counted.h" |
| 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "base/message_loop.h" |
[email protected] | f40000c6 | 2012-05-11 15:57:07 | [diff] [blame] | 10 | #include "base/string16.h" |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 12 | #include "content/browser/geolocation/geolocation_provider_impl.h" |
[email protected] | f40000c6 | 2012-05-11 15:57:07 | [diff] [blame] | 13 | #include "content/browser/geolocation/location_provider.h" |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 14 | #include "content/browser/geolocation/mock_location_arbitrator.h" |
[email protected] | f40000c6 | 2012-05-11 15:57:07 | [diff] [blame] | 15 | #include "content/public/browser/access_token_store.h" |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 16 | #include "content/public/browser/browser_thread.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 17 | #include "content/public/test/test_browser_thread.h" |
[email protected] | f40000c6 | 2012-05-11 15:57:07 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
[email protected] | 60349d9 | 2011-09-06 11:00:41 | [diff] [blame] | 19 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 22 | using testing::MakeMatcher; |
| 23 | using testing::Matcher; |
| 24 | using testing::MatcherInterface; |
| 25 | using testing::MatchResultListener; |
[email protected] | 60349d9 | 2011-09-06 11:00:41 | [diff] [blame] | 26 | |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 27 | namespace content { |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 28 | |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 29 | class LocationProviderForTestArbitrator : public GeolocationProviderImpl { |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 30 | public: |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 31 | LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 32 | virtual ~LocationProviderForTestArbitrator() {} |
| 33 | |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 34 | // Only valid for use on the geolocation thread. |
| 35 | MockGeolocationArbitrator* mock_arbitrator() const { |
| 36 | return mock_arbitrator_; |
| 37 | } |
| 38 | |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 39 | protected: |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 40 | // GeolocationProviderImpl implementation: |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 41 | virtual GeolocationArbitrator* CreateArbitrator() OVERRIDE; |
| 42 | |
| 43 | private: |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 44 | MockGeolocationArbitrator* mock_arbitrator_; |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 45 | }; |
| 46 | |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 47 | GeolocationArbitrator* LocationProviderForTestArbitrator::CreateArbitrator() { |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 48 | DCHECK(mock_arbitrator_ == NULL); |
| 49 | mock_arbitrator_ = new MockGeolocationArbitrator; |
| 50 | return mock_arbitrator_; |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 53 | class GeolocationObserver { |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 54 | public: |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 55 | virtual ~GeolocationObserver() {} |
| 56 | virtual void OnLocationUpdate(const Geoposition& position) = 0; |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | class MockGeolocationObserver : public GeolocationObserver { |
| 60 | public: |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 61 | MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position)); |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 62 | }; |
| 63 | |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 64 | class AsyncMockGeolocationObserver : public MockGeolocationObserver { |
| 65 | public: |
[email protected] | c3e3589 | 2013-02-12 02:08:01 | [diff] [blame] | 66 | virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE { |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 67 | MockGeolocationObserver::OnLocationUpdate(position); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 68 | base::MessageLoop::current()->Quit(); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 69 | } |
| 70 | }; |
| 71 | |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 72 | class MockGeolocationCallbackWrapper { |
| 73 | public: |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 74 | MOCK_METHOD1(Callback, void(const Geoposition& position)); |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | class GeopositionEqMatcher |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 78 | : public MatcherInterface<const Geoposition&> { |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 79 | public: |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 80 | explicit GeopositionEqMatcher(const Geoposition& expected) |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 81 | : expected_(expected) {} |
| 82 | |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 83 | virtual bool MatchAndExplain(const Geoposition& actual, |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 84 | MatchResultListener* listener) const OVERRIDE { |
| 85 | return actual.latitude == expected_.latitude && |
| 86 | actual.longitude == expected_.longitude && |
| 87 | actual.altitude == expected_.altitude && |
| 88 | actual.accuracy == expected_.accuracy && |
| 89 | actual.altitude_accuracy == expected_.altitude_accuracy && |
| 90 | actual.heading == expected_.heading && |
| 91 | actual.speed == expected_.speed && |
| 92 | actual.timestamp == expected_.timestamp && |
| 93 | actual.error_code == expected_.error_code && |
| 94 | actual.error_message == expected_.error_message; |
| 95 | } |
| 96 | |
| 97 | virtual void DescribeTo(::std::ostream* os) const OVERRIDE { |
| 98 | *os << "which matches the expected position"; |
| 99 | } |
| 100 | |
| 101 | virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE{ |
| 102 | *os << "which does not match the expected position"; |
| 103 | } |
| 104 | |
| 105 | private: |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 106 | Geoposition expected_; |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher); |
| 109 | }; |
| 110 | |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 111 | Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 112 | return MakeMatcher(new GeopositionEqMatcher(expected)); |
| 113 | } |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 114 | |
| 115 | class GeolocationProviderTest : public testing::Test { |
| 116 | protected: |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 117 | GeolocationProviderTest() |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 118 | : message_loop_(), |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 119 | io_thread_(BrowserThread::IO, &message_loop_), |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 120 | provider_(new LocationProviderForTestArbitrator) { |
[email protected] | b7d6acc | 2011-02-03 11:01:50 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | c3e3589 | 2013-02-12 02:08:01 | [diff] [blame] | 123 | virtual ~GeolocationProviderTest() {} |
[email protected] | b7d6acc | 2011-02-03 11:01:50 | [diff] [blame] | 124 | |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 125 | LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
| 126 | |
| 127 | // Called on test thread. |
| 128 | bool ProvidersStarted(); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 129 | void SendMockLocation(const Geoposition& position); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | // Called on provider thread. |
| 133 | void GetProvidersStarted(bool* started); |
[email protected] | d4cee67 | 2012-05-09 17:36:07 | [diff] [blame] | 134 | |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 135 | base::MessageLoop message_loop_; |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 136 | TestBrowserThread io_thread_; |
[email protected] | b7c5c26 | 2012-11-26 13:17:18 | [diff] [blame] | 137 | scoped_ptr<LocationProviderForTestArbitrator> provider_; |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 138 | }; |
| 139 | |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 140 | |
| 141 | bool GeolocationProviderTest::ProvidersStarted() { |
| 142 | DCHECK(provider_->IsRunning()); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 143 | DCHECK(base::MessageLoop::current() == &message_loop_); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 144 | bool started; |
| 145 | provider_->message_loop_proxy()->PostTaskAndReply( |
| 146 | FROM_HERE, |
| 147 | base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| 148 | base::Unretained(this), |
| 149 | &started), |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 150 | base::MessageLoop::QuitClosure()); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 151 | message_loop_.Run(); |
| 152 | return started; |
| 153 | } |
| 154 | |
| 155 | void GeolocationProviderTest::GetProvidersStarted(bool* started) { |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 156 | DCHECK(base::MessageLoop::current() == provider_->message_loop()); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 157 | *started = provider_->mock_arbitrator()->providers_started(); |
| 158 | } |
| 159 | |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 160 | void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
| 161 | DCHECK(provider_->IsRunning()); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 162 | DCHECK(base::MessageLoop::current() == &message_loop_); |
| 163 | provider_->message_loop() |
| 164 | ->PostTask(FROM_HERE, |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 165 | base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 166 | base::Unretained(provider_.get()), |
| 167 | position)); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 168 | } |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 169 | |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 170 | // Regression test for http://crbug.com/59377 |
| 171 | TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 172 | EXPECT_FALSE(provider()->LocationServicesOptedIn()); |
| 173 | provider()->UserDidOptIntoLocationServices(); |
| 174 | EXPECT_TRUE(provider()->LocationServicesOptedIn()); |
[email protected] | fa7e91b8 | 2010-10-20 09:29:27 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | b7d6acc | 2011-02-03 11:01:50 | [diff] [blame] | 177 | TEST_F(GeolocationProviderTest, StartStop) { |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 178 | EXPECT_FALSE(provider()->IsRunning()); |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 179 | GeolocationProviderImpl::LocationUpdateCallback null_callback; |
| 180 | provider()->AddLocationUpdateCallback(null_callback, false); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 181 | EXPECT_TRUE(provider()->IsRunning()); |
| 182 | EXPECT_TRUE(ProvidersStarted()); |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 183 | |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 184 | provider()->RemoveLocationUpdateCallback(null_callback); |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 185 | EXPECT_FALSE(ProvidersStarted()); |
| 186 | EXPECT_TRUE(provider()->IsRunning()); |
[email protected] | b7d6acc | 2011-02-03 11:01:50 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 189 | TEST_F(GeolocationProviderTest, StalePositionNotSent) { |
| 190 | Geoposition first_position; |
| 191 | first_position.latitude = 12; |
| 192 | first_position.longitude = 34; |
| 193 | first_position.accuracy = 56; |
| 194 | first_position.timestamp = base::Time::Now(); |
| 195 | |
| 196 | AsyncMockGeolocationObserver first_observer; |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 197 | GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( |
| 198 | &MockGeolocationObserver::OnLocationUpdate, |
| 199 | base::Unretained(&first_observer)); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 200 | EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 201 | provider()->AddLocationUpdateCallback(first_callback, false); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 202 | SendMockLocation(first_position); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 203 | base::MessageLoop::current()->Run(); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 204 | |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 205 | provider()->RemoveLocationUpdateCallback(first_callback); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 206 | |
| 207 | Geoposition second_position; |
| 208 | second_position.latitude = 13; |
| 209 | second_position.longitude = 34; |
| 210 | second_position.accuracy = 56; |
| 211 | second_position.timestamp = base::Time::Now(); |
| 212 | |
| 213 | AsyncMockGeolocationObserver second_observer; |
| 214 | |
| 215 | // After adding a second observer, check that no unexpected position update |
| 216 | // is sent. |
| 217 | EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 218 | GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( |
| 219 | &MockGeolocationObserver::OnLocationUpdate, |
| 220 | base::Unretained(&second_observer)); |
| 221 | provider()->AddLocationUpdateCallback(second_callback, false); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 222 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 223 | |
| 224 | // The second observer should receive the new position now. |
| 225 | EXPECT_CALL(second_observer, |
| 226 | OnLocationUpdate(GeopositionEq(second_position))); |
| 227 | SendMockLocation(second_position); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 228 | base::MessageLoop::current()->Run(); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 229 | |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 230 | provider()->RemoveLocationUpdateCallback(second_callback); |
[email protected] | baaf184 | 2012-12-03 20:54:54 | [diff] [blame] | 231 | EXPECT_FALSE(ProvidersStarted()); |
| 232 | } |
| 233 | |
[email protected] | f40000c6 | 2012-05-11 15:57:07 | [diff] [blame] | 234 | TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 235 | Geoposition position; |
| 236 | position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 237 | provider()->OverrideLocationForTesting(position); |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 238 | // Adding an observer when the location is overridden should synchronously |
| 239 | // update the observer with our overridden position. |
[email protected] | 02a5fe29 | 2012-04-25 21:47:56 | [diff] [blame] | 240 | MockGeolocationObserver mock_observer; |
[email protected] | 439eb40 | 2012-05-04 13:04:44 | [diff] [blame] | 241 | EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
[email protected] | 6d444ddf | 2013-05-07 17:10:44 | [diff] [blame^] | 242 | GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( |
| 243 | &MockGeolocationObserver::OnLocationUpdate, |
| 244 | base::Unretained(&mock_observer)); |
| 245 | provider()->AddLocationUpdateCallback(callback, false); |
| 246 | provider()->RemoveLocationUpdateCallback(callback); |
[email protected] | d4cee67 | 2012-05-09 17:36:07 | [diff] [blame] | 247 | // Wait for the providers to be stopped now that all clients are gone. |
[email protected] | 90984b0 | 2012-11-30 13:00:40 | [diff] [blame] | 248 | EXPECT_FALSE(ProvidersStarted()); |
[email protected] | 02a5fe29 | 2012-04-25 21:47:56 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | 6bd4083 | 2012-10-29 03:01:49 | [diff] [blame] | 251 | } // namespace content |