Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: content/browser/geolocation/geolocation_provider_unittest.cc

Issue 6597044: Revert 76228 - Move core pieces of geolocation from chrome to content.This is... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/singleton.h"
6 #include "content/browser/geolocation/arbitrator_dependency_factories_for_test.h "
7 #include "content/browser/geolocation/fake_access_token_store.h"
8 #include "content/browser/geolocation/geolocation_provider.h"
9 #include "content/browser/geolocation/location_arbitrator.h"
10 #include "content/browser/geolocation/mock_location_provider.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 class GeolocationProviderTest : public testing::Test {
16 protected:
17 GeolocationProviderTest()
18 : provider_(new GeolocationProvider),
19 dependency_factory_(
20 new GeolocationArbitratorDependencyFactoryWithLocationProvider(
21 &NewAutoSuccessMockNetworkLocationProvider))
22 {
23 }
24
25 ~GeolocationProviderTest() {
26 DefaultSingletonTraits<GeolocationProvider>::Delete(provider_);
27 }
28
29 // testing::Test
30 virtual void SetUp() {
31 GeolocationArbitrator::SetDependencyFactoryForTest(
32 dependency_factory_.get());
33 }
34
35 // testing::Test
36 virtual void TearDown() {
37 provider_->Stop();
38 GeolocationArbitrator::SetDependencyFactoryForTest(NULL);
39 }
40
41 // Message loop for main thread, as provider depends on it existing.
42 MessageLoop message_loop_;
43
44 // Object under tests. Owned, but not a scoped_ptr due to specialized
45 // destruction protocol.
46 GeolocationProvider* provider_;
47
48 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory_;
49 };
50
51 // Regression test for http://crbug.com/59377
52 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) {
53 EXPECT_FALSE(provider_->HasPermissionBeenGranted());
54 provider_->OnPermissionGranted(GURL("http://example.com"));
55 EXPECT_TRUE(provider_->HasPermissionBeenGranted());
56 }
57
58 class NullGeolocationObserver : public GeolocationObserver {
59 public:
60 // GeolocationObserver
61 virtual void OnLocationUpdate(const Geoposition& position) {}
62 };
63
64 class StartStopMockLocationProvider : public MockLocationProvider {
65 public:
66 explicit StartStopMockLocationProvider(MessageLoop* test_loop)
67 : MockLocationProvider(&instance_),
68 test_loop_(test_loop) {
69 }
70
71 virtual bool StartProvider(bool high_accuracy) {
72 bool result = MockLocationProvider::StartProvider(high_accuracy);
73 test_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask);
74 return result;
75 }
76
77 virtual void StopProvider() {
78 MockLocationProvider::StopProvider();
79 test_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask);
80 }
81
82 private:
83 MessageLoop* test_loop_;
84 };
85
86 class MockDependencyFactory : public GeolocationArbitratorDependencyFactory {
87 public:
88 MockDependencyFactory(MessageLoop* test_loop_,
89 AccessTokenStore* access_token_store)
90 : test_loop_(test_loop_),
91 access_token_store_(access_token_store) {
92 }
93
94 virtual URLRequestContextGetter* GetContextGetter() {
95 return NULL;
96 }
97
98 virtual GeolocationArbitrator::GetTimeNow GetTimeFunction() {
99 return base::Time::Now;
100 }
101
102 virtual AccessTokenStore* NewAccessTokenStore() {
103 return access_token_store_.get();
104 }
105
106 virtual LocationProviderBase* NewNetworkLocationProvider(
107 AccessTokenStore* access_token_store,
108 URLRequestContextGetter* context,
109 const GURL& url,
110 const string16& access_token) {
111 return new StartStopMockLocationProvider(test_loop_);
112 }
113
114 virtual LocationProviderBase* NewSystemLocationProvider() {
115 return NULL;
116 }
117
118 private:
119 MessageLoop* test_loop_;
120 scoped_refptr<AccessTokenStore> access_token_store_;
121 };
122
123 TEST_F(GeolocationProviderTest, StartStop) {
124 scoped_refptr<FakeAccessTokenStore> fake_access_token_store =
125 new FakeAccessTokenStore;
126 scoped_refptr<GeolocationArbitratorDependencyFactory> dependency_factory =
127 new MockDependencyFactory(&message_loop_, fake_access_token_store.get());
128
129 GeolocationArbitrator::SetDependencyFactoryForTest(dependency_factory.get());
130
131 EXPECT_FALSE(provider_->IsRunning());
132 NullGeolocationObserver null_observer;
133 GeolocationObserverOptions options;
134 provider_->AddObserver(&null_observer, options);
135 // The GeolocationArbitrator won't start the providers until it has
136 // finished loading access tokens.
137 fake_access_token_store->NotifyDelegateTokensLoaded();
138 EXPECT_TRUE(provider_->IsRunning());
139 message_loop_.Run();
140 EXPECT_EQ(MockLocationProvider::instance_->state_,
141 MockLocationProvider::LOW_ACCURACY);
142 provider_->RemoveObserver(&null_observer);
143 message_loop_.Run();
144 EXPECT_EQ(MockLocationProvider::instance_->state_,
145 MockLocationProvider::STOPPED);
146 EXPECT_TRUE(provider_->IsRunning());
147
148 GeolocationArbitrator::SetDependencyFactoryForTest(NULL);
149 }
150
151 } // namespace
OLDNEW
« no previous file with comments | « content/browser/geolocation/geolocation_provider.cc ('k') | content/browser/geolocation/gps_location_provider_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698