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

Side by Side Diff: chromeos/geolocation/simple_geolocation_unittest.cc

Issue 1819173002: SimpleGeolocation should support sending WiFi AP data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: UMA added. Created 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_manager_client.h"
10 #include "chromeos/geolocation/simple_geolocation_provider.h" 14 #include "chromeos/geolocation/simple_geolocation_provider.h"
15 #include "chromeos/geolocation/simple_geolocation_request_test_monitor.h"
16 #include "chromeos/network/geolocation_handler.h"
17 #include "chromeos/network/network_handler.h"
11 #include "net/http/http_response_headers.h" 18 #include "net/http/http_response_headers.h"
12 #include "net/http/http_status_code.h" 19 #include "net/http/http_status_code.h"
13 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_fetcher_impl.h" 21 #include "net/url_request/url_fetcher_impl.h"
15 #include "net/url_request/url_request_status.h" 22 #include "net/url_request/url_request_status.h"
16 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
17 25
18 namespace { 26 namespace {
19 27
20 const int kRequestRetryIntervalMilliSeconds = 200; 28 const int kRequestRetryIntervalMilliSeconds = 200;
21 29
22 // This should be different from default to prevent SimpleGeolocationRequest 30 // This should be different from default to prevent SimpleGeolocationRequest
23 // from modifying it. 31 // from modifying it.
24 const char kTestGeolocationProviderUrl[] = 32 const char kTestGeolocationProviderUrl[] =
25 "https://localhost/geolocation/v1/geolocate?"; 33 "https://localhost/geolocation/v1/geolocate?";
26 34
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 bool server_error() const { return server_error_; } 163 bool server_error() const { return server_error_; }
156 base::TimeDelta elapsed() const { return elapsed_; } 164 base::TimeDelta elapsed() const { return elapsed_; }
157 165
158 private: 166 private:
159 Geoposition position_; 167 Geoposition position_;
160 bool server_error_; 168 bool server_error_;
161 base::TimeDelta elapsed_; 169 base::TimeDelta elapsed_;
162 scoped_ptr<base::RunLoop> message_loop_runner_; 170 scoped_ptr<base::RunLoop> message_loop_runner_;
163 }; 171 };
164 172
173 class WiFiTestMonitor : public SimpleGeolocationRequestTestMonitor {
174 public:
175 WiFiTestMonitor() {}
176
177 void OnRequestCreated(SimpleGeolocationRequest* request) override {}
178 void OnStart(SimpleGeolocationRequest* request) override {
179 last_request_body_ = request->FormatRequestBodyForTesting();
180 }
181
182 const std::string& last_request_body() const { return last_request_body_; }
183
184 private:
185 std::string last_request_body_;
186
187 DISALLOW_COPY_AND_ASSIGN(WiFiTestMonitor);
188 };
189
165 class SimpleGeolocationTest : public testing::Test { 190 class SimpleGeolocationTest : public testing::Test {
166 private: 191 private:
167 base::MessageLoop message_loop_; 192 base::MessageLoop message_loop_;
168 }; 193 };
169 194
170 TEST_F(SimpleGeolocationTest, ResponseOK) { 195 TEST_F(SimpleGeolocationTest, ResponseOK) {
171 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 196 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl));
172 197
173 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 198 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
174 std::string(kSimpleResponseBody), 199 std::string(kSimpleResponseBody),
175 0 /* require_retries */, 200 0 /* require_retries */,
176 &provider); 201 &provider);
177 202
178 GeolocationReceiver receiver; 203 GeolocationReceiver receiver;
179 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), 204 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
180 base::Bind(&GeolocationReceiver::OnRequestDone, 205 base::Bind(&GeolocationReceiver::OnRequestDone,
181 base::Unretained(&receiver))); 206 base::Unretained(&receiver)));
182 receiver.WaitUntilRequestDone(); 207 receiver.WaitUntilRequestDone();
183 208
184 EXPECT_EQ( 209 EXPECT_EQ(
185 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " 210 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
186 "error_code=0, error_message='', status=1 (OK)", 211 "error_code=0, error_message='', status=1 (OK)",
187 receiver.position().ToString()); 212 receiver.position().ToString());
188 EXPECT_FALSE(receiver.server_error()); 213 EXPECT_FALSE(receiver.server_error());
189 EXPECT_EQ(1U, url_factory.attempts()); 214 EXPECT_EQ(1U, url_factory.attempts());
190 } 215 }
191 216
192 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { 217 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) {
193 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 218 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl));
194 219
195 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 220 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
196 std::string(kSimpleResponseBody), 221 std::string(kSimpleResponseBody),
197 3 /* require_retries */, 222 3 /* require_retries */,
198 &provider); 223 &provider);
199 224
200 GeolocationReceiver receiver; 225 GeolocationReceiver receiver;
201 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), 226 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
202 base::Bind(&GeolocationReceiver::OnRequestDone, 227 base::Bind(&GeolocationReceiver::OnRequestDone,
203 base::Unretained(&receiver))); 228 base::Unretained(&receiver)));
204 receiver.WaitUntilRequestDone(); 229 receiver.WaitUntilRequestDone();
205 EXPECT_EQ( 230 EXPECT_EQ(
206 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " 231 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
207 "error_code=0, error_message='', status=1 (OK)", 232 "error_code=0, error_message='', status=1 (OK)",
208 receiver.position().ToString()); 233 receiver.position().ToString());
209 EXPECT_FALSE(receiver.server_error()); 234 EXPECT_FALSE(receiver.server_error());
210 EXPECT_EQ(4U, url_factory.attempts()); 235 EXPECT_EQ(4U, url_factory.attempts());
211 } 236 }
212 237
213 TEST_F(SimpleGeolocationTest, InvalidResponse) { 238 TEST_F(SimpleGeolocationTest, InvalidResponse) {
214 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 239 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl));
215 240
216 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 241 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
217 "invalid JSON string", 242 "invalid JSON string",
218 0 /* require_retries */, 243 0 /* require_retries */,
219 &provider); 244 &provider);
220 245
221 GeolocationReceiver receiver; 246 GeolocationReceiver receiver;
222 247
223 const int timeout_seconds = 1; 248 const int timeout_seconds = 1;
224 size_t expected_retries = static_cast<size_t>( 249 size_t expected_retries = static_cast<size_t>(
225 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds); 250 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds);
226 ASSERT_GE(expected_retries, 2U); 251 ASSERT_GE(expected_retries, 2U);
227 252
228 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), 253 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds),
254 false,
229 base::Bind(&GeolocationReceiver::OnRequestDone, 255 base::Bind(&GeolocationReceiver::OnRequestDone,
230 base::Unretained(&receiver))); 256 base::Unretained(&receiver)));
231 receiver.WaitUntilRequestDone(); 257 receiver.WaitUntilRequestDone();
232 258
233 EXPECT_EQ( 259 EXPECT_EQ(
234 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, " 260 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, "
235 "error_code=0, error_message='SimpleGeolocation provider at " 261 "error_code=0, error_message='SimpleGeolocation provider at "
236 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, " 262 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, "
237 "Unexpected token..', status=4 (TIMEOUT)", 263 "Unexpected token..', status=4 (TIMEOUT)",
238 receiver.position().ToString()); 264 receiver.position().ToString());
239 EXPECT_TRUE(receiver.server_error()); 265 EXPECT_TRUE(receiver.server_error());
240 EXPECT_GE(url_factory.attempts(), 2U); 266 EXPECT_GE(url_factory.attempts(), 2U);
241 if (url_factory.attempts() > expected_retries + 1) { 267 if (url_factory.attempts() > expected_retries + 1) {
242 LOG(WARNING) 268 LOG(WARNING)
243 << "SimpleGeolocationTest::InvalidResponse: Too many attempts (" 269 << "SimpleGeolocationTest::InvalidResponse: Too many attempts ("
244 << url_factory.attempts() << "), no more then " << expected_retries + 1 270 << url_factory.attempts() << "), no more then " << expected_retries + 1
245 << " expected."; 271 << " expected.";
246 } 272 }
247 if (url_factory.attempts() < expected_retries - 1) { 273 if (url_factory.attempts() < expected_retries - 1) {
248 LOG(WARNING) 274 LOG(WARNING)
249 << "SimpleGeolocationTest::InvalidResponse: Too little attempts (" 275 << "SimpleGeolocationTest::InvalidResponse: Too little attempts ("
250 << url_factory.attempts() << "), greater then " << expected_retries - 1 276 << url_factory.attempts() << "), greater then " << expected_retries - 1
251 << " expected."; 277 << " expected.";
252 } 278 }
253 } 279 }
254 280
281 TEST_F(SimpleGeolocationTest, NoWiFi) {
282 // This initializes DBusThreadManager and markes it "for tests only".
283 DBusThreadManager::GetSetterForTesting();
284 NetworkHandler::Initialize();
285
286 WiFiTestMonitor requests_monitor;
287 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
288
289 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl));
stevenjb 2016/03/22 16:39:49 s/NULL/nullptr/ throughout.
Alexander Alekseev 2016/03/23 00:04:45 Done.
290
291 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
292 std::string(kSimpleResponseBody),
293 0 /* require_retries */, &provider);
294
295 GeolocationReceiver receiver;
296 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true,
297 base::Bind(&GeolocationReceiver::OnRequestDone,
298 base::Unretained(&receiver)));
299 receiver.WaitUntilRequestDone();
300 EXPECT_EQ("{\"considerIP\": \"true\"}", requests_monitor.last_request_body());
301
302 EXPECT_EQ(
303 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
304 "error_code=0, error_message='', status=1 (OK)",
305 receiver.position().ToString());
306 EXPECT_FALSE(receiver.server_error());
307 EXPECT_EQ(1U, url_factory.attempts());
308
309 NetworkHandler::Shutdown();
310 DBusThreadManager::Shutdown();
311 }
312
313 // Test sending of WiFi Access points.
314 // (This is mostly derived from GeolocationHandlerTest.)
315 class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> {
316 public:
317 SimpleGeolocationWiFiTest() : manager_test_(NULL) {}
318
319 ~SimpleGeolocationWiFiTest() override {}
320
321 void SetUp() override {
322 // This initializes DBusThreadManager and markes it "for tests only".
323 DBusThreadManager::GetSetterForTesting();
324 // Get the test interface for manager / device.
325 manager_test_ =
326 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
327 ASSERT_TRUE(manager_test_);
328 geolocation_handler_.reset(new GeolocationHandler());
329 geolocation_handler_->Init();
330 message_loop_.RunUntilIdle();
331 }
332
333 void TearDown() override {
334 geolocation_handler_.reset();
335 DBusThreadManager::Shutdown();
336 }
337
338 bool GetWifiAccessPoints() {
339 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_,
340 NULL);
341 }
342
343 void AddAccessPoint(int idx) {
344 base::DictionaryValue properties;
345 std::string mac_address =
346 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0);
347 std::string channel = base::IntToString(idx);
348 std::string strength = base::IntToString(idx * 10);
349 properties.SetStringWithoutPathExpansion(shill::kGeoMacAddressProperty,
350 mac_address);
351 properties.SetStringWithoutPathExpansion(shill::kGeoChannelProperty,
352 channel);
353 properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty,
354 strength);
355 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties);
356 message_loop_.RunUntilIdle();
357 }
358
359 protected:
360 base::MessageLoopForUI message_loop_;
361 scoped_ptr<GeolocationHandler> geolocation_handler_;
362 ShillManagerClient::TestInterface* manager_test_;
363 WifiAccessPointVector wifi_access_points_;
364
365 private:
366 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWiFiTest);
367 };
368
369 // Parameter is enable/disable sending of WiFi data.
370 TEST_P(SimpleGeolocationWiFiTest, WiFiExists) {
371 NetworkHandler::Initialize();
372
373 WiFiTestMonitor requests_monitor;
374 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
375
376 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl));
377
378 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
379 std::string(kSimpleResponseBody),
380 0 /* require_retries */, &provider);
381 {
382 GeolocationReceiver receiver;
383 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
384 base::Bind(&GeolocationReceiver::OnRequestDone,
385 base::Unretained(&receiver)));
386 receiver.WaitUntilRequestDone();
387 EXPECT_EQ("{\"considerIP\": \"true\"}",
388 requests_monitor.last_request_body());
389
390 EXPECT_EQ(
391 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
392 "error_code=0, error_message='', status=1 (OK)",
393 receiver.position().ToString());
394 EXPECT_FALSE(receiver.server_error());
395 EXPECT_EQ(1U, url_factory.attempts());
396 }
397
398 // Add an acces point.
399 AddAccessPoint(1);
400 message_loop_.RunUntilIdle();
401 // Inititial call should return false and request access points.
402 EXPECT_FALSE(GetWifiAccessPoints());
403 message_loop_.RunUntilIdle();
404 // Second call should return true since we have an access point.
405 EXPECT_TRUE(GetWifiAccessPoints());
406 ASSERT_EQ(1u, wifi_access_points_.size());
407 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address);
408 EXPECT_EQ(1, wifi_access_points_[0].channel);
409
410 {
411 GeolocationReceiver receiver;
412 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
413 base::Bind(&GeolocationReceiver::OnRequestDone,
414 base::Unretained(&receiver)));
415 receiver.WaitUntilRequestDone();
416 if (GetParam()) {
417 // Sending WiFi data is enabled.
418 EXPECT_EQ("{"
419 "\"considerIp\":true,"
420 "\"wifiAccessPoints\":["
421 "{"
422 "\"channel\":1,"
423 "\"macAddress\":\"01:00:00:00:00:00\","
424 "\"signalStrength\":10,"
425 "\"signalToNoiseRatio\":0"
426 "}"
427 "]"
428 "}", requests_monitor.last_request_body());
stevenjb 2016/03/22 16:39:49 Declare a constant for human readable strings.
Alexander Alekseev 2016/03/23 00:04:45 Done.
429 } else {
430 // Sending WiFi data is disabled.
431 EXPECT_EQ("{\"considerIP\": \"true\"}",
432 requests_monitor.last_request_body());
433 }
434
435 EXPECT_EQ(
436 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
437 "error_code=0, error_message='', status=1 (OK)",
438 receiver.position().ToString());
439 EXPECT_FALSE(receiver.server_error());
440 // This is total.
441 EXPECT_EQ(2U, url_factory.attempts());
442 }
443 NetworkHandler::Shutdown();
444 }
445
446 // This test verifies that WiFi data is sent only if sending was requested.
447 INSTANTIATE_TEST_CASE_P(EnableDisableSendingWifiData,
448 SimpleGeolocationWiFiTest,
449 testing::Bool());
450
255 } // namespace chromeos 451 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698