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 "content/browser/browser_thread_impl.h" | 5 #include "content/browser/browser_thread_impl.h" |
6 #include "content/browser/geolocation/gps_location_provider_linux.h" | 6 #include "content/browser/geolocation/gps_location_provider_linux.h" |
7 #include "content/browser/geolocation/libgps_wrapper_linux.h" | 7 #include "content/browser/geolocation/libgps_wrapper_linux.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 | 11 |
12 using content::BrowserThread; | 12 using content::BrowserThread; |
13 using content::BrowserThreadImpl; | 13 using content::BrowserThreadImpl; |
14 | 14 |
15 namespace { | 15 namespace { |
16 class MockLibGps : public LibGps { | 16 class MockLibGps : public LibGps { |
17 public: | 17 public: |
18 MockLibGps(); | 18 MockLibGps(); |
19 ~MockLibGps(); | 19 ~MockLibGps(); |
20 | 20 |
21 virtual bool GetPositionIfFixed(Geoposition* position) { | 21 virtual bool GetPositionIfFixed(content::Geoposition* position) { |
22 CHECK(position); | 22 CHECK(position); |
23 ++get_position_calls_; | 23 ++get_position_calls_; |
24 *position = get_position_; | 24 *position = get_position_; |
25 return get_position_ret_; | 25 return get_position_ret_; |
26 } | 26 } |
27 | 27 |
28 static int gps_open_stub(const char*, const char*, struct gps_data_t*) { | 28 static int gps_open_stub(const char*, const char*, struct gps_data_t*) { |
29 CHECK(g_instance_); | 29 CHECK(g_instance_); |
30 g_instance_->gps_open_calls_++; | 30 g_instance_->gps_open_calls_++; |
31 return g_instance_->gps_open_ret_; | 31 return g_instance_->gps_open_ret_; |
32 } | 32 } |
33 | 33 |
34 static int gps_close_stub(struct gps_data_t*) { | 34 static int gps_close_stub(struct gps_data_t*) { |
35 return 0; | 35 return 0; |
36 } | 36 } |
37 | 37 |
38 static int gps_read_stub(struct gps_data_t*) { | 38 static int gps_read_stub(struct gps_data_t*) { |
39 CHECK(g_instance_); | 39 CHECK(g_instance_); |
40 g_instance_->gps_read_calls_++; | 40 g_instance_->gps_read_calls_++; |
41 return g_instance_->gps_read_ret_; | 41 return g_instance_->gps_read_ret_; |
42 } | 42 } |
43 | 43 |
44 int get_position_calls_; | 44 int get_position_calls_; |
45 bool get_position_ret_; | 45 bool get_position_ret_; |
46 int gps_open_calls_; | 46 int gps_open_calls_; |
47 int gps_open_ret_; | 47 int gps_open_ret_; |
48 int gps_read_calls_; | 48 int gps_read_calls_; |
49 int gps_read_ret_; | 49 int gps_read_ret_; |
50 Geoposition get_position_; | 50 content::Geoposition get_position_; |
51 static MockLibGps* g_instance_; | 51 static MockLibGps* g_instance_; |
52 }; | 52 }; |
53 | 53 |
54 class LocaionProviderListenerLoopQuitter | 54 class LocaionProviderListenerLoopQuitter |
55 : public LocationProviderBase::ListenerInterface { | 55 : public LocationProviderBase::ListenerInterface { |
56 // LocationProviderBase::ListenerInterface | 56 // LocationProviderBase::ListenerInterface |
57 virtual void LocationUpdateAvailable(LocationProviderBase* provider) { | 57 virtual void LocationUpdateAvailable(LocationProviderBase* provider) { |
58 MessageLoop::current()->Quit(); | 58 MessageLoop::current()->Quit(); |
59 } | 59 } |
60 }; | 60 }; |
(...skipping 10 matching lines...) Expand all Loading... |
71 return NULL; | 71 return NULL; |
72 } | 72 } |
73 | 73 |
74 protected: | 74 protected: |
75 MessageLoop message_loop_; | 75 MessageLoop message_loop_; |
76 BrowserThreadImpl ui_thread_; | 76 BrowserThreadImpl ui_thread_; |
77 LocaionProviderListenerLoopQuitter location_listener_; | 77 LocaionProviderListenerLoopQuitter location_listener_; |
78 scoped_ptr<GpsLocationProviderLinux> provider_; | 78 scoped_ptr<GpsLocationProviderLinux> provider_; |
79 }; | 79 }; |
80 | 80 |
81 void CheckValidPosition(const Geoposition& expected, | 81 void CheckValidPosition(const content::Geoposition& expected, |
82 const Geoposition& actual) { | 82 const content::Geoposition& actual) { |
83 EXPECT_TRUE(actual.IsValidFix()); | 83 EXPECT_TRUE(actual.Validate()); |
84 EXPECT_DOUBLE_EQ(expected.latitude, actual.latitude); | 84 EXPECT_DOUBLE_EQ(expected.latitude, actual.latitude); |
85 EXPECT_DOUBLE_EQ(expected.longitude, actual.longitude); | 85 EXPECT_DOUBLE_EQ(expected.longitude, actual.longitude); |
86 EXPECT_DOUBLE_EQ(expected.accuracy, actual.accuracy); | 86 EXPECT_DOUBLE_EQ(expected.accuracy, actual.accuracy); |
87 } | 87 } |
88 | 88 |
89 MockLibGps* MockLibGps::g_instance_ = NULL; | 89 MockLibGps* MockLibGps::g_instance_ = NULL; |
90 | 90 |
91 MockLibGps::MockLibGps() | 91 MockLibGps::MockLibGps() |
92 : LibGps(NULL, gps_open_stub, gps_close_stub, gps_read_stub), | 92 : LibGps(NULL, gps_open_stub, gps_close_stub, gps_read_stub), |
93 get_position_calls_(0), | 93 get_position_calls_(0), |
94 get_position_ret_(true), | 94 get_position_ret_(true), |
95 gps_open_calls_(0), | 95 gps_open_calls_(0), |
96 gps_open_ret_(0), | 96 gps_open_ret_(0), |
97 gps_read_calls_(0), | 97 gps_read_calls_(0), |
98 gps_read_ret_(0) { | 98 gps_read_ret_(0) { |
99 get_position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 99 get_position_.error_code = |
| 100 content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
100 EXPECT_FALSE(g_instance_); | 101 EXPECT_FALSE(g_instance_); |
101 g_instance_ = this; | 102 g_instance_ = this; |
102 } | 103 } |
103 | 104 |
104 MockLibGps::~MockLibGps() { | 105 MockLibGps::~MockLibGps() { |
105 EXPECT_EQ(this, g_instance_); | 106 EXPECT_EQ(this, g_instance_); |
106 g_instance_ = NULL; | 107 g_instance_ = NULL; |
107 } | 108 } |
108 | 109 |
109 GeolocationGpsProviderLinuxTests::GeolocationGpsProviderLinuxTests() | 110 GeolocationGpsProviderLinuxTests::GeolocationGpsProviderLinuxTests() |
110 : ui_thread_(BrowserThread::IO, &message_loop_), | 111 : ui_thread_(BrowserThread::IO, &message_loop_), |
111 provider_(new GpsLocationProviderLinux(NewMockLibGps)) { | 112 provider_(new GpsLocationProviderLinux(NewMockLibGps)) { |
112 provider_->RegisterListener(&location_listener_); | 113 provider_->RegisterListener(&location_listener_); |
113 } | 114 } |
114 | 115 |
115 GeolocationGpsProviderLinuxTests::~GeolocationGpsProviderLinuxTests() { | 116 GeolocationGpsProviderLinuxTests::~GeolocationGpsProviderLinuxTests() { |
116 provider_->UnregisterListener(&location_listener_); | 117 provider_->UnregisterListener(&location_listener_); |
117 } | 118 } |
118 | 119 |
119 TEST_F(GeolocationGpsProviderLinuxTests, NoLibGpsInstalled) { | 120 TEST_F(GeolocationGpsProviderLinuxTests, NoLibGpsInstalled) { |
120 provider_.reset(new GpsLocationProviderLinux(NoLibGpsFactory)); | 121 provider_.reset(new GpsLocationProviderLinux(NoLibGpsFactory)); |
121 ASSERT_TRUE(provider_.get()); | 122 ASSERT_TRUE(provider_.get()); |
122 const bool ok = provider_->StartProvider(true); | 123 const bool ok = provider_->StartProvider(true); |
123 EXPECT_FALSE(ok); | 124 EXPECT_FALSE(ok); |
124 Geoposition position; | 125 content::Geoposition position; |
125 provider_->GetPosition(&position); | 126 provider_->GetPosition(&position); |
126 EXPECT_TRUE(position.IsInitialized()); | 127 EXPECT_FALSE(position.Validate()); |
127 EXPECT_FALSE(position.IsValidFix()); | 128 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, |
128 EXPECT_EQ(Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, position.error_code); | 129 position.error_code); |
129 } | 130 } |
130 | 131 |
131 #if defined(OS_CHROMEOS) | 132 #if defined(OS_CHROMEOS) |
132 | 133 |
133 TEST_F(GeolocationGpsProviderLinuxTests, GetPosition) { | 134 TEST_F(GeolocationGpsProviderLinuxTests, GetPosition) { |
134 ASSERT_TRUE(provider_.get()); | 135 ASSERT_TRUE(provider_.get()); |
135 const bool ok = provider_->StartProvider(true); | 136 const bool ok = provider_->StartProvider(true); |
136 EXPECT_TRUE(ok); | 137 EXPECT_TRUE(ok); |
137 ASSERT_TRUE(MockLibGps::g_instance_); | 138 ASSERT_TRUE(MockLibGps::g_instance_); |
138 EXPECT_EQ(0, MockLibGps::g_instance_->get_position_calls_); | 139 EXPECT_EQ(0, MockLibGps::g_instance_->get_position_calls_); |
139 EXPECT_EQ(0, MockLibGps::g_instance_->gps_open_calls_); | 140 EXPECT_EQ(0, MockLibGps::g_instance_->gps_open_calls_); |
140 EXPECT_EQ(0, MockLibGps::g_instance_->gps_read_calls_); | 141 EXPECT_EQ(0, MockLibGps::g_instance_->gps_read_calls_); |
141 Geoposition position; | 142 content::Geoposition position; |
142 provider_->GetPosition(&position); | 143 provider_->GetPosition(&position); |
143 EXPECT_TRUE(position.IsInitialized()); | 144 EXPECT_FALSE(position.Validate()); |
144 EXPECT_FALSE(position.IsValidFix()); | 145 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, |
145 EXPECT_EQ(Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, position.error_code); | 146 position.error_code); |
146 MockLibGps::g_instance_->get_position_.error_code = | 147 MockLibGps::g_instance_->get_position_.error_code = |
147 Geoposition::ERROR_CODE_NONE; | 148 content::Geoposition::ERROR_CODE_NONE; |
148 MockLibGps::g_instance_->get_position_.latitude = 4.5; | 149 MockLibGps::g_instance_->get_position_.latitude = 4.5; |
149 MockLibGps::g_instance_->get_position_.longitude = -34.1; | 150 MockLibGps::g_instance_->get_position_.longitude = -34.1; |
150 MockLibGps::g_instance_->get_position_.accuracy = 345; | 151 MockLibGps::g_instance_->get_position_.accuracy = 345; |
151 MockLibGps::g_instance_->get_position_.timestamp = | 152 MockLibGps::g_instance_->get_position_.timestamp = |
152 base::Time::FromDoubleT(200); | 153 base::Time::FromDoubleT(200); |
153 EXPECT_TRUE(MockLibGps::g_instance_->get_position_.IsValidFix()); | 154 EXPECT_TRUE(MockLibGps::g_instance_->get_position_.Validate()); |
154 MessageLoop::current()->Run(); | 155 MessageLoop::current()->Run(); |
155 EXPECT_EQ(1, MockLibGps::g_instance_->get_position_calls_); | 156 EXPECT_EQ(1, MockLibGps::g_instance_->get_position_calls_); |
156 EXPECT_EQ(1, MockLibGps::g_instance_->gps_open_calls_); | 157 EXPECT_EQ(1, MockLibGps::g_instance_->gps_open_calls_); |
157 EXPECT_EQ(1, MockLibGps::g_instance_->gps_read_calls_); | 158 EXPECT_EQ(1, MockLibGps::g_instance_->gps_read_calls_); |
158 provider_->GetPosition(&position); | 159 provider_->GetPosition(&position); |
159 CheckValidPosition(MockLibGps::g_instance_->get_position_, position); | 160 CheckValidPosition(MockLibGps::g_instance_->get_position_, position); |
160 | 161 |
161 // Movement. This will block for up to half a second. | 162 // Movement. This will block for up to half a second. |
162 MockLibGps::g_instance_->get_position_.latitude += 0.01; | 163 MockLibGps::g_instance_->get_position_.latitude += 0.01; |
163 MessageLoop::current()->Run(); | 164 MessageLoop::current()->Run(); |
(...skipping 12 matching lines...) Expand all Loading... |
176 TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) { | 177 TEST_F(GeolocationGpsProviderLinuxTests, LibGpsReconnect) { |
177 // Setup gpsd reconnect interval to be 1000ms to speed up test. | 178 // Setup gpsd reconnect interval to be 1000ms to speed up test. |
178 provider_->SetGpsdReconnectIntervalMillis(1000); | 179 provider_->SetGpsdReconnectIntervalMillis(1000); |
179 provider_->SetPollPeriodMovingMillis(200); | 180 provider_->SetPollPeriodMovingMillis(200); |
180 const bool ok = provider_->StartProvider(true); | 181 const bool ok = provider_->StartProvider(true); |
181 EXPECT_TRUE(ok); | 182 EXPECT_TRUE(ok); |
182 ASSERT_TRUE(MockLibGps::g_instance_); | 183 ASSERT_TRUE(MockLibGps::g_instance_); |
183 // Let gps_open() fails, and so will LibGps::Start(). | 184 // Let gps_open() fails, and so will LibGps::Start(). |
184 // Reconnect will happen in 1000ms. | 185 // Reconnect will happen in 1000ms. |
185 MockLibGps::g_instance_->gps_open_ret_ = 1; | 186 MockLibGps::g_instance_->gps_open_ret_ = 1; |
186 Geoposition position; | 187 content::Geoposition position; |
187 MockLibGps::g_instance_->get_position_.error_code = | 188 MockLibGps::g_instance_->get_position_.error_code = |
188 Geoposition::ERROR_CODE_NONE; | 189 content::Geoposition::ERROR_CODE_NONE; |
189 MockLibGps::g_instance_->get_position_.latitude = 4.5; | 190 MockLibGps::g_instance_->get_position_.latitude = 4.5; |
190 MockLibGps::g_instance_->get_position_.longitude = -34.1; | 191 MockLibGps::g_instance_->get_position_.longitude = -34.1; |
191 MockLibGps::g_instance_->get_position_.accuracy = 345; | 192 MockLibGps::g_instance_->get_position_.accuracy = 345; |
192 MockLibGps::g_instance_->get_position_.timestamp = | 193 MockLibGps::g_instance_->get_position_.timestamp = |
193 base::Time::FromDoubleT(200); | 194 base::Time::FromDoubleT(200); |
194 EXPECT_TRUE(MockLibGps::g_instance_->get_position_.IsValidFix()); | 195 EXPECT_TRUE(MockLibGps::g_instance_->get_position_.Validate()); |
195 // This task makes gps_open() and LibGps::Start() to succeed after | 196 // This task makes gps_open() and LibGps::Start() to succeed after |
196 // 1500ms. | 197 // 1500ms. |
197 MessageLoop::current()->PostDelayedTask( | 198 MessageLoop::current()->PostDelayedTask( |
198 FROM_HERE, | 199 FROM_HERE, |
199 base::Bind(&EnableGpsOpenCallback), | 200 base::Bind(&EnableGpsOpenCallback), |
200 base::TimeDelta::FromMilliseconds(1500)); | 201 base::TimeDelta::FromMilliseconds(1500)); |
201 MessageLoop::current()->Run(); | 202 MessageLoop::current()->Run(); |
202 provider_->GetPosition(&position); | 203 provider_->GetPosition(&position); |
203 EXPECT_TRUE(position.IsInitialized()); | 204 EXPECT_TRUE(position.Validate()); |
204 EXPECT_TRUE(position.IsValidFix()); | |
205 // 3 gps_open() calls are expected (2 failures and 1 success) | 205 // 3 gps_open() calls are expected (2 failures and 1 success) |
206 EXPECT_EQ(1, MockLibGps::g_instance_->get_position_calls_); | 206 EXPECT_EQ(1, MockLibGps::g_instance_->get_position_calls_); |
207 EXPECT_EQ(3, MockLibGps::g_instance_->gps_open_calls_); | 207 EXPECT_EQ(3, MockLibGps::g_instance_->gps_open_calls_); |
208 EXPECT_EQ(1, MockLibGps::g_instance_->gps_read_calls_); | 208 EXPECT_EQ(1, MockLibGps::g_instance_->gps_read_calls_); |
209 } | 209 } |
210 | 210 |
211 #endif // #if defined(OS_CHROMEOS) | 211 #endif // #if defined(OS_CHROMEOS) |
212 | 212 |
213 } // namespace | 213 } // namespace |
OLD | NEW |