| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/geolocation/geolocation_provider.h" | |
| 6 | |
| 7 #include "base/singleton.h" | |
| 8 #include "chrome/browser/geolocation/arbitrator_dependency_factories_for_test.h" | |
| 9 #include "chrome/browser/geolocation/fake_access_token_store.h" | |
| 10 #include "chrome/browser/geolocation/location_arbitrator.h" | |
| 11 #include "chrome/browser/geolocation/mock_location_provider.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 class GeolocationProviderTest : public testing::Test { | |
| 17 protected: | |
| 18 GeolocationProviderTest() | |
| 19 : provider_(new GeolocationProvider), | |
| 20 dependency_factory_( | |
| 21 new GeolocationArbitratorDependencyFactoryWithLocationProvider( | |
| 22 &NewAutoSuccessMockNetworkLocationProvider)) | |
| 23 { | |
| 24 } | |
| 25 | |
| 26 ~GeolocationProviderTest() { | |
| 27 DefaultSingletonTraits<GeolocationProvider>::Delete(provider_); | |
| 28 } | |
| 29 | |
| 30 // testing::Test | |
| 31 virtual void SetUp() { | |
| 32 GeolocationArbitrator::SetDependencyFactoryForTest( | |
| 33 dependency_factory_.get()); | |
| 34 } | |
| 35 | |
| 36 // testing::Test | |
| 37 virtual void TearDown() { | |
| 38 provider_->Stop(); | |
| 39 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | |
| 40 } | |
| 41 | |
| 42 // Message loop for main thread, as provider depends on it existing. | |
| 43 MessageLoop message_loop_; | |
| 44 | |
| 45 // Object under tests. Owned, but not a scoped_ptr due to specialized | |
| 46 // destruction protocol. | |
| 47 GeolocationProvider* provider_; | |
| 48 | |
| 49 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory_; | |
| 50 }; | |
| 51 | |
| 52 // Regression test for http://crbug.com/59377 | |
| 53 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | |
| 54 EXPECT_FALSE(provider_->HasPermissionBeenGranted()); | |
| 55 provider_->OnPermissionGranted(GURL("http://example.com")); | |
| 56 EXPECT_TRUE(provider_->HasPermissionBeenGranted()); | |
| 57 } | |
| 58 | |
| 59 class NullGeolocationObserver : public GeolocationObserver { | |
| 60 public: | |
| 61 // GeolocationObserver | |
| 62 virtual void OnLocationUpdate(const Geoposition& position) {} | |
| 63 }; | |
| 64 | |
| 65 class StartStopMockLocationProvider : public MockLocationProvider { | |
| 66 public: | |
| 67 explicit StartStopMockLocationProvider(MessageLoop* test_loop) | |
| 68 : MockLocationProvider(&instance_), | |
| 69 test_loop_(test_loop) { | |
| 70 } | |
| 71 | |
| 72 virtual bool StartProvider(bool high_accuracy) { | |
| 73 bool result = MockLocationProvider::StartProvider(high_accuracy); | |
| 74 test_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask); | |
| 75 return result; | |
| 76 } | |
| 77 | |
| 78 virtual void StopProvider() { | |
| 79 MockLocationProvider::StopProvider(); | |
| 80 test_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask); | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 MessageLoop* test_loop_; | |
| 85 }; | |
| 86 | |
| 87 class MockDependencyFactory : public GeolocationArbitratorDependencyFactory { | |
| 88 public: | |
| 89 MockDependencyFactory(MessageLoop* test_loop_, | |
| 90 AccessTokenStore* access_token_store) | |
| 91 : test_loop_(test_loop_), | |
| 92 access_token_store_(access_token_store) { | |
| 93 } | |
| 94 | |
| 95 virtual URLRequestContextGetter* GetContextGetter() { | |
| 96 return NULL; | |
| 97 } | |
| 98 | |
| 99 virtual GeolocationArbitrator::GetTimeNow GetTimeFunction() { | |
| 100 return base::Time::Now; | |
| 101 } | |
| 102 | |
| 103 virtual AccessTokenStore* NewAccessTokenStore() { | |
| 104 return access_token_store_.get(); | |
| 105 } | |
| 106 | |
| 107 virtual LocationProviderBase* NewNetworkLocationProvider( | |
| 108 AccessTokenStore* access_token_store, | |
| 109 URLRequestContextGetter* context, | |
| 110 const GURL& url, | |
| 111 const string16& access_token) { | |
| 112 return new StartStopMockLocationProvider(test_loop_); | |
| 113 } | |
| 114 | |
| 115 virtual LocationProviderBase* NewSystemLocationProvider() { | |
| 116 return NULL; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 MessageLoop* test_loop_; | |
| 121 scoped_refptr<AccessTokenStore> access_token_store_; | |
| 122 }; | |
| 123 | |
| 124 TEST_F(GeolocationProviderTest, StartStop) { | |
| 125 scoped_refptr<FakeAccessTokenStore> fake_access_token_store = | |
| 126 new FakeAccessTokenStore; | |
| 127 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory = | |
| 128 new MockDependencyFactory(&message_loop_, fake_access_token_store.get()); | |
| 129 | |
| 130 GeolocationArbitrator::SetDependencyFactoryForTest(dependency_factory.get()); | |
| 131 | |
| 132 EXPECT_FALSE(provider_->IsRunning()); | |
| 133 NullGeolocationObserver null_observer; | |
| 134 GeolocationObserverOptions options; | |
| 135 provider_->AddObserver(&null_observer, options); | |
| 136 // The GeolocationArbitrator won't start the providers until it has | |
| 137 // finished loading access tokens. | |
| 138 fake_access_token_store->NotifyDelegateTokensLoaded(); | |
| 139 EXPECT_TRUE(provider_->IsRunning()); | |
| 140 message_loop_.Run(); | |
| 141 EXPECT_EQ(MockLocationProvider::instance_->state_, | |
| 142 MockLocationProvider::LOW_ACCURACY); | |
| 143 provider_->RemoveObserver(&null_observer); | |
| 144 message_loop_.Run(); | |
| 145 EXPECT_EQ(MockLocationProvider::instance_->state_, | |
| 146 MockLocationProvider::STOPPED); | |
| 147 EXPECT_TRUE(provider_->IsRunning()); | |
| 148 | |
| 149 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | |
| 150 } | |
| 151 | |
| 152 } // namespace | |
| OLD | NEW |