| 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 <utility> | 10 #include <utility> |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 const char* kNoFixNetworkResponse = | 374 const char* kNoFixNetworkResponse = |
| 375 "{" | 375 "{" |
| 376 " \"status\": \"ZERO_RESULTS\"" | 376 " \"status\": \"ZERO_RESULTS\"" |
| 377 "}"; | 377 "}"; |
| 378 fetcher->set_url(test_server_url_); | 378 fetcher->set_url(test_server_url_); |
| 379 fetcher->set_status(net::URLRequestStatus()); | 379 fetcher->set_status(net::URLRequestStatus()); |
| 380 fetcher->set_response_code(200); // OK | 380 fetcher->set_response_code(200); // OK |
| 381 fetcher->SetResponseString(kNoFixNetworkResponse); | 381 fetcher->SetResponseString(kNoFixNetworkResponse); |
| 382 fetcher->delegate()->OnURLFetchComplete(fetcher); | 382 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 383 | 383 |
| 384 Geoposition position; | 384 Geoposition position = provider->GetPosition(); |
| 385 provider->GetPosition(&position); | |
| 386 EXPECT_FALSE(position.Validate()); | 385 EXPECT_FALSE(position.Validate()); |
| 387 | 386 |
| 388 // Now wifi data arrives -- SetData will notify listeners. | 387 // Now wifi data arrives -- SetData will notify listeners. |
| 389 const int kFirstScanAps = 6; | 388 const int kFirstScanAps = 6; |
| 390 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 389 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
| 391 base::RunLoop().RunUntilIdle(); | 390 base::RunLoop().RunUntilIdle(); |
| 392 fetcher = get_url_fetcher_and_advance_id(); | 391 fetcher = get_url_fetcher_and_advance_id(); |
| 393 ASSERT_TRUE(fetcher); | 392 ASSERT_TRUE(fetcher); |
| 394 // The request should have the wifi data. | 393 // The request should have the wifi data. |
| 395 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string()); | 394 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string()); |
| 396 | 395 |
| 397 // Send a reply with good position fix. | 396 // Send a reply with good position fix. |
| 398 const char* kReferenceNetworkResponse = | 397 const char* kReferenceNetworkResponse = |
| 399 "{" | 398 "{" |
| 400 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN | 399 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN |
| 401 "\"," | 400 "\"," |
| 402 " \"accuracy\": 1200.4," | 401 " \"accuracy\": 1200.4," |
| 403 " \"location\": {" | 402 " \"location\": {" |
| 404 " \"lat\": 51.0," | 403 " \"lat\": 51.0," |
| 405 " \"lng\": -0.1" | 404 " \"lng\": -0.1" |
| 406 " }" | 405 " }" |
| 407 "}"; | 406 "}"; |
| 408 fetcher->set_url(test_server_url_); | 407 fetcher->set_url(test_server_url_); |
| 409 fetcher->set_status(net::URLRequestStatus()); | 408 fetcher->set_status(net::URLRequestStatus()); |
| 410 fetcher->set_response_code(200); // OK | 409 fetcher->set_response_code(200); // OK |
| 411 fetcher->SetResponseString(kReferenceNetworkResponse); | 410 fetcher->SetResponseString(kReferenceNetworkResponse); |
| 412 fetcher->delegate()->OnURLFetchComplete(fetcher); | 411 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 413 | 412 |
| 414 provider->GetPosition(&position); | 413 position = provider->GetPosition(); |
| 415 EXPECT_EQ(51.0, position.latitude); | 414 EXPECT_EQ(51.0, position.latitude); |
| 416 EXPECT_EQ(-0.1, position.longitude); | 415 EXPECT_EQ(-0.1, position.longitude); |
| 417 EXPECT_EQ(1200.4, position.accuracy); | 416 EXPECT_EQ(1200.4, position.accuracy); |
| 418 EXPECT_FALSE(position.timestamp.is_null()); | 417 EXPECT_FALSE(position.timestamp.is_null()); |
| 419 EXPECT_TRUE(position.Validate()); | 418 EXPECT_TRUE(position.Validate()); |
| 420 | 419 |
| 421 // Token should be in the store. | 420 // Token should be in the store. |
| 422 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), | 421 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), |
| 423 access_token_store_->access_token_map_[test_server_url_]); | 422 access_token_store_->access_token_map_[test_server_url_]); |
| 424 | 423 |
| 425 // Wifi updated again, with one less AP. This is 'close enough' to the | 424 // Wifi updated again, with one less AP. This is 'close enough' to the |
| 426 // previous scan, so no new request made. | 425 // previous scan, so no new request made. |
| 427 const int kSecondScanAps = kFirstScanAps - 1; | 426 const int kSecondScanAps = kFirstScanAps - 1; |
| 428 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); | 427 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); |
| 429 base::RunLoop().RunUntilIdle(); | 428 base::RunLoop().RunUntilIdle(); |
| 430 fetcher = get_url_fetcher_and_advance_id(); | 429 fetcher = get_url_fetcher_and_advance_id(); |
| 431 EXPECT_FALSE(fetcher); | 430 EXPECT_FALSE(fetcher); |
| 432 | 431 |
| 433 provider->GetPosition(&position); | 432 position = provider->GetPosition(); |
| 434 EXPECT_EQ(51.0, position.latitude); | 433 EXPECT_EQ(51.0, position.latitude); |
| 435 EXPECT_EQ(-0.1, position.longitude); | 434 EXPECT_EQ(-0.1, position.longitude); |
| 436 EXPECT_TRUE(position.Validate()); | 435 EXPECT_TRUE(position.Validate()); |
| 437 | 436 |
| 438 // Now a third scan with more than twice the original amount -> new request. | 437 // Now a third scan with more than twice the original amount -> new request. |
| 439 const int kThirdScanAps = kFirstScanAps * 2 + 1; | 438 const int kThirdScanAps = kFirstScanAps * 2 + 1; |
| 440 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); | 439 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); |
| 441 base::RunLoop().RunUntilIdle(); | 440 base::RunLoop().RunUntilIdle(); |
| 442 fetcher = get_url_fetcher_and_advance_id(); | 441 fetcher = get_url_fetcher_and_advance_id(); |
| 443 EXPECT_TRUE(fetcher); | 442 EXPECT_TRUE(fetcher); |
| 444 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN); | 443 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN); |
| 445 // ...reply with a network error. | 444 // ...reply with a network error. |
| 446 | 445 |
| 447 fetcher->set_url(test_server_url_); | 446 fetcher->set_url(test_server_url_); |
| 448 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); | 447 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); |
| 449 fetcher->set_response_code(200); // should be ignored | 448 fetcher->set_response_code(200); // should be ignored |
| 450 fetcher->SetResponseString(std::string()); | 449 fetcher->SetResponseString(std::string()); |
| 451 fetcher->delegate()->OnURLFetchComplete(fetcher); | 450 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 452 | 451 |
| 453 // Error means we now no longer have a fix. | 452 // Error means we now no longer have a fix. |
| 454 provider->GetPosition(&position); | 453 position = provider->GetPosition(); |
| 455 EXPECT_FALSE(position.Validate()); | 454 EXPECT_FALSE(position.Validate()); |
| 456 | 455 |
| 457 // Wifi scan returns to original set: should be serviced from cache. | 456 // Wifi scan returns to original set: should be serviced from cache. |
| 458 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 457 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
| 459 base::RunLoop().RunUntilIdle(); | 458 base::RunLoop().RunUntilIdle(); |
| 460 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. | 459 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. |
| 461 | 460 |
| 462 provider->GetPosition(&position); | 461 position = provider->GetPosition(); |
| 463 EXPECT_EQ(51.0, position.latitude); | 462 EXPECT_EQ(51.0, position.latitude); |
| 464 EXPECT_EQ(-0.1, position.longitude); | 463 EXPECT_EQ(-0.1, position.longitude); |
| 465 EXPECT_TRUE(position.Validate()); | 464 EXPECT_TRUE(position.Validate()); |
| 466 } | 465 } |
| 467 | 466 |
| 468 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { | 467 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { |
| 469 MessageLoopQuitListener listener; | 468 MessageLoopQuitListener listener; |
| 470 wifi_data_provider_->set_got_data(false); | 469 wifi_data_provider_->set_got_data(false); |
| 471 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); | 470 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
| 472 EXPECT_TRUE(provider->StartProvider(false)); | 471 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))); | 552 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
| 554 } else { | 553 } else { |
| 555 const int evicted = i - kCacheSize; | 554 const int evicted = i - kCacheSize; |
| 556 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 555 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
| 557 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 556 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
| 558 } | 557 } |
| 559 } | 558 } |
| 560 } | 559 } |
| 561 | 560 |
| 562 } // namespace device | 561 } // namespace device |
| OLD | NEW |