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 "device/geolocation/network_location_provider.h" | 5 #include "device/geolocation/network_location_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
| 10 #include <string> |
10 #include <utility> | 11 #include <utility> |
| 12 #include <vector> |
11 | 13 |
12 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
13 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
14 #include "base/macros.h" | 16 #include "base/macros.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
20 #include "base/values.h" | 22 #include "base/values.h" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 std::unique_ptr<base::Value> parsed_json = | 272 std::unique_ptr<base::Value> parsed_json = |
271 base::JSONReader::ReadAndReturnError(upload_data, base::JSON_PARSE_RFC, | 273 base::JSONReader::ReadAndReturnError(upload_data, base::JSON_PARSE_RFC, |
272 nullptr, &json_parse_error_msg); | 274 nullptr, &json_parse_error_msg); |
273 EXPECT_TRUE(json_parse_error_msg.empty()); | 275 EXPECT_TRUE(json_parse_error_msg.empty()); |
274 ASSERT_TRUE(parsed_json); | 276 ASSERT_TRUE(parsed_json); |
275 | 277 |
276 const base::DictionaryValue* request_json; | 278 const base::DictionaryValue* request_json; |
277 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json)); | 279 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json)); |
278 | 280 |
279 if (!is_default_url) { | 281 if (!is_default_url) { |
280 if (expected_access_token.empty()) | 282 if (expected_access_token.empty()) { |
281 ASSERT_FALSE(request_json->HasKey(kAccessTokenString)); | 283 ASSERT_FALSE(request_json->HasKey(kAccessTokenString)); |
282 else { | 284 } else { |
283 std::string access_token; | 285 std::string access_token; |
284 EXPECT_TRUE(request_json->GetString(kAccessTokenString, &access_token)); | 286 EXPECT_TRUE(request_json->GetString(kAccessTokenString, &access_token)); |
285 EXPECT_EQ(expected_access_token, access_token); | 287 EXPECT_EQ(expected_access_token, access_token); |
286 } | 288 } |
287 } | 289 } |
288 | 290 |
289 if (expected_wifi_aps) { | 291 if (expected_wifi_aps) { |
290 base::ListValue expected_wifi_aps_json; | 292 base::ListValue expected_wifi_aps_json; |
291 CreateReferenceWifiScanDataJson(expected_wifi_aps, wifi_start_index, | 293 CreateReferenceWifiScanDataJson(expected_wifi_aps, wifi_start_index, |
292 &expected_wifi_aps_json); | 294 &expected_wifi_aps_json); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 const char* kNoFixNetworkResponse = | 376 const char* kNoFixNetworkResponse = |
375 "{" | 377 "{" |
376 " \"status\": \"ZERO_RESULTS\"" | 378 " \"status\": \"ZERO_RESULTS\"" |
377 "}"; | 379 "}"; |
378 fetcher->set_url(test_server_url_); | 380 fetcher->set_url(test_server_url_); |
379 fetcher->set_status(net::URLRequestStatus()); | 381 fetcher->set_status(net::URLRequestStatus()); |
380 fetcher->set_response_code(200); // OK | 382 fetcher->set_response_code(200); // OK |
381 fetcher->SetResponseString(kNoFixNetworkResponse); | 383 fetcher->SetResponseString(kNoFixNetworkResponse); |
382 fetcher->delegate()->OnURLFetchComplete(fetcher); | 384 fetcher->delegate()->OnURLFetchComplete(fetcher); |
383 | 385 |
384 Geoposition position; | 386 Geoposition position = provider->GetPosition(); |
385 provider->GetPosition(&position); | |
386 EXPECT_FALSE(position.Validate()); | 387 EXPECT_FALSE(position.Validate()); |
387 | 388 |
388 // Now wifi data arrives -- SetData will notify listeners. | 389 // Now wifi data arrives -- SetData will notify listeners. |
389 const int kFirstScanAps = 6; | 390 const int kFirstScanAps = 6; |
390 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 391 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
391 base::RunLoop().RunUntilIdle(); | 392 base::RunLoop().RunUntilIdle(); |
392 fetcher = get_url_fetcher_and_advance_id(); | 393 fetcher = get_url_fetcher_and_advance_id(); |
393 ASSERT_TRUE(fetcher); | 394 ASSERT_TRUE(fetcher); |
394 // The request should have the wifi data. | 395 // The request should have the wifi data. |
395 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string()); | 396 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string()); |
396 | 397 |
397 // Send a reply with good position fix. | 398 // Send a reply with good position fix. |
398 const char* kReferenceNetworkResponse = | 399 const char* kReferenceNetworkResponse = |
399 "{" | 400 "{" |
400 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN | 401 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN |
401 "\"," | 402 "\"," |
402 " \"accuracy\": 1200.4," | 403 " \"accuracy\": 1200.4," |
403 " \"location\": {" | 404 " \"location\": {" |
404 " \"lat\": 51.0," | 405 " \"lat\": 51.0," |
405 " \"lng\": -0.1" | 406 " \"lng\": -0.1" |
406 " }" | 407 " }" |
407 "}"; | 408 "}"; |
408 fetcher->set_url(test_server_url_); | 409 fetcher->set_url(test_server_url_); |
409 fetcher->set_status(net::URLRequestStatus()); | 410 fetcher->set_status(net::URLRequestStatus()); |
410 fetcher->set_response_code(200); // OK | 411 fetcher->set_response_code(200); // OK |
411 fetcher->SetResponseString(kReferenceNetworkResponse); | 412 fetcher->SetResponseString(kReferenceNetworkResponse); |
412 fetcher->delegate()->OnURLFetchComplete(fetcher); | 413 fetcher->delegate()->OnURLFetchComplete(fetcher); |
413 | 414 |
414 provider->GetPosition(&position); | 415 position = provider->GetPosition(); |
415 EXPECT_EQ(51.0, position.latitude); | 416 EXPECT_EQ(51.0, position.latitude); |
416 EXPECT_EQ(-0.1, position.longitude); | 417 EXPECT_EQ(-0.1, position.longitude); |
417 EXPECT_EQ(1200.4, position.accuracy); | 418 EXPECT_EQ(1200.4, position.accuracy); |
418 EXPECT_FALSE(position.timestamp.is_null()); | 419 EXPECT_FALSE(position.timestamp.is_null()); |
419 EXPECT_TRUE(position.Validate()); | 420 EXPECT_TRUE(position.Validate()); |
420 | 421 |
421 // Token should be in the store. | 422 // Token should be in the store. |
422 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), | 423 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), |
423 access_token_store_->access_token_map_[test_server_url_]); | 424 access_token_store_->access_token_map_[test_server_url_]); |
424 | 425 |
425 // Wifi updated again, with one less AP. This is 'close enough' to the | 426 // Wifi updated again, with one less AP. This is 'close enough' to the |
426 // previous scan, so no new request made. | 427 // previous scan, so no new request made. |
427 const int kSecondScanAps = kFirstScanAps - 1; | 428 const int kSecondScanAps = kFirstScanAps - 1; |
428 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); | 429 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); |
429 base::RunLoop().RunUntilIdle(); | 430 base::RunLoop().RunUntilIdle(); |
430 fetcher = get_url_fetcher_and_advance_id(); | 431 fetcher = get_url_fetcher_and_advance_id(); |
431 EXPECT_FALSE(fetcher); | 432 EXPECT_FALSE(fetcher); |
432 | 433 |
433 provider->GetPosition(&position); | 434 position = provider->GetPosition(); |
434 EXPECT_EQ(51.0, position.latitude); | 435 EXPECT_EQ(51.0, position.latitude); |
435 EXPECT_EQ(-0.1, position.longitude); | 436 EXPECT_EQ(-0.1, position.longitude); |
436 EXPECT_TRUE(position.Validate()); | 437 EXPECT_TRUE(position.Validate()); |
437 | 438 |
438 // Now a third scan with more than twice the original amount -> new request. | 439 // Now a third scan with more than twice the original amount -> new request. |
439 const int kThirdScanAps = kFirstScanAps * 2 + 1; | 440 const int kThirdScanAps = kFirstScanAps * 2 + 1; |
440 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); | 441 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); |
441 base::RunLoop().RunUntilIdle(); | 442 base::RunLoop().RunUntilIdle(); |
442 fetcher = get_url_fetcher_and_advance_id(); | 443 fetcher = get_url_fetcher_and_advance_id(); |
443 EXPECT_TRUE(fetcher); | 444 EXPECT_TRUE(fetcher); |
444 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN); | 445 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN); |
445 // ...reply with a network error. | 446 // ...reply with a network error. |
446 | 447 |
447 fetcher->set_url(test_server_url_); | 448 fetcher->set_url(test_server_url_); |
448 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); | 449 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); |
449 fetcher->set_response_code(200); // should be ignored | 450 fetcher->set_response_code(200); // should be ignored |
450 fetcher->SetResponseString(std::string()); | 451 fetcher->SetResponseString(std::string()); |
451 fetcher->delegate()->OnURLFetchComplete(fetcher); | 452 fetcher->delegate()->OnURLFetchComplete(fetcher); |
452 | 453 |
453 // Error means we now no longer have a fix. | 454 // Error means we now no longer have a fix. |
454 provider->GetPosition(&position); | 455 position = provider->GetPosition(); |
455 EXPECT_FALSE(position.Validate()); | 456 EXPECT_FALSE(position.Validate()); |
456 | 457 |
457 // Wifi scan returns to original set: should be serviced from cache. | 458 // Wifi scan returns to original set: should be serviced from cache. |
458 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 459 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
459 base::RunLoop().RunUntilIdle(); | 460 base::RunLoop().RunUntilIdle(); |
460 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. | 461 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. |
461 | 462 |
462 provider->GetPosition(&position); | 463 position = provider->GetPosition(); |
463 EXPECT_EQ(51.0, position.latitude); | 464 EXPECT_EQ(51.0, position.latitude); |
464 EXPECT_EQ(-0.1, position.longitude); | 465 EXPECT_EQ(-0.1, position.longitude); |
465 EXPECT_TRUE(position.Validate()); | 466 EXPECT_TRUE(position.Validate()); |
466 } | 467 } |
467 | 468 |
468 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { | 469 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { |
469 MessageLoopQuitListener listener; | 470 MessageLoopQuitListener listener; |
470 wifi_data_provider_->set_got_data(false); | 471 wifi_data_provider_->set_got_data(false); |
471 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); | 472 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
472 EXPECT_TRUE(provider->StartProvider(false)); | 473 EXPECT_TRUE(provider->StartProvider(false)); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); | 554 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
554 } else { | 555 } else { |
555 const int evicted = i - kCacheSize; | 556 const int evicted = i - kCacheSize; |
556 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 557 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
557 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 558 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
558 } | 559 } |
559 } | 560 } |
560 } | 561 } |
561 | 562 |
562 } // namespace device | 563 } // namespace device |
OLD | NEW |