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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "content/browser/geolocation/fake_access_token_store.h" | 12 #include "content/browser/geolocation/fake_access_token_store.h" |
13 #include "content/browser/geolocation/network_location_provider.h" | 13 #include "content/browser/geolocation/network_location_provider.h" |
14 #include "content/test/test_url_fetcher_factory.h" | 14 #include "content/test/test_url_fetcher_factory.h" |
15 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using content::FakeAccessTokenStore; | 18 using content::FakeAccessTokenStore; |
| 19 using content::Geoposition; |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Constants used in multiple tests. | 23 // Constants used in multiple tests. |
23 const char kTestServerUrl[] = "https://www.geolocation.test/service"; | 24 const char kTestServerUrl[] = "https://www.geolocation.test/service"; |
24 const char kTestJson[] = "?browser=chromium&sensor=true"; | 25 const char kTestJson[] = "?browser=chromium&sensor=true"; |
25 const char kTestBrowser[] = "browser=chromium"; | 26 const char kTestBrowser[] = "browser=chromium"; |
26 const char kTestSensor[] = "sensor=true"; | 27 const char kTestSensor[] = "sensor=true"; |
27 // Using #define so we can easily paste this into various other strings. | 28 // Using #define so we can easily paste this into various other strings. |
28 #define REFERENCE_ACCESS_TOKEN "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe" | 29 #define REFERENCE_ACCESS_TOKEN "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe" |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 " \"status\": \"ZERO_RESULTS\"" | 367 " \"status\": \"ZERO_RESULTS\"" |
367 "}"; | 368 "}"; |
368 fetcher->set_url(test_server_url_); | 369 fetcher->set_url(test_server_url_); |
369 fetcher->set_status(net::URLRequestStatus()); | 370 fetcher->set_status(net::URLRequestStatus()); |
370 fetcher->set_response_code(200); // OK | 371 fetcher->set_response_code(200); // OK |
371 fetcher->SetResponseString(kNoFixNetworkResponse); | 372 fetcher->SetResponseString(kNoFixNetworkResponse); |
372 fetcher->delegate()->OnURLFetchComplete(fetcher); | 373 fetcher->delegate()->OnURLFetchComplete(fetcher); |
373 | 374 |
374 Geoposition position; | 375 Geoposition position; |
375 provider->GetPosition(&position); | 376 provider->GetPosition(&position); |
376 EXPECT_FALSE(position.IsValidFix()); | 377 EXPECT_FALSE(position.Validate()); |
377 | 378 |
378 // Now wifi data arrives -- SetData will notify listeners. | 379 // Now wifi data arrives -- SetData will notify listeners. |
379 const int kFirstScanAps = 6; | 380 const int kFirstScanAps = 6; |
380 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 381 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
381 main_message_loop_.RunAllPending(); | 382 main_message_loop_.RunAllPending(); |
382 fetcher = get_url_fetcher_and_advance_id(); | 383 fetcher = get_url_fetcher_and_advance_id(); |
383 ASSERT_TRUE(fetcher != NULL); | 384 ASSERT_TRUE(fetcher != NULL); |
384 // The request should have the wifi data. | 385 // The request should have the wifi data. |
385 CheckRequestIsValid( | 386 CheckRequestIsValid( |
386 fetcher->GetOriginalURL().spec(), 0, kFirstScanAps, 0, ""); | 387 fetcher->GetOriginalURL().spec(), 0, kFirstScanAps, 0, ""); |
(...skipping 12 matching lines...) Expand all Loading... |
399 fetcher->set_url(test_server_url_); | 400 fetcher->set_url(test_server_url_); |
400 fetcher->set_status(net::URLRequestStatus()); | 401 fetcher->set_status(net::URLRequestStatus()); |
401 fetcher->set_response_code(200); // OK | 402 fetcher->set_response_code(200); // OK |
402 fetcher->SetResponseString(kReferenceNetworkResponse); | 403 fetcher->SetResponseString(kReferenceNetworkResponse); |
403 fetcher->delegate()->OnURLFetchComplete(fetcher); | 404 fetcher->delegate()->OnURLFetchComplete(fetcher); |
404 | 405 |
405 provider->GetPosition(&position); | 406 provider->GetPosition(&position); |
406 EXPECT_EQ(51.0, position.latitude); | 407 EXPECT_EQ(51.0, position.latitude); |
407 EXPECT_EQ(-0.1, position.longitude); | 408 EXPECT_EQ(-0.1, position.longitude); |
408 EXPECT_EQ(1200.4, position.accuracy); | 409 EXPECT_EQ(1200.4, position.accuracy); |
409 EXPECT_TRUE(position.is_valid_timestamp()); | 410 EXPECT_FALSE(position.timestamp.is_null()); |
410 EXPECT_TRUE(position.IsValidFix()); | 411 EXPECT_TRUE(position.Validate()); |
411 | 412 |
412 // Token should be in the store. | 413 // Token should be in the store. |
413 EXPECT_EQ(UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), | 414 EXPECT_EQ(UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), |
414 access_token_store_->access_token_set_[test_server_url_]); | 415 access_token_store_->access_token_set_[test_server_url_]); |
415 | 416 |
416 // Wifi updated again, with one less AP. This is 'close enough' to the | 417 // Wifi updated again, with one less AP. This is 'close enough' to the |
417 // previous scan, so no new request made. | 418 // previous scan, so no new request made. |
418 const int kSecondScanAps = kFirstScanAps - 1; | 419 const int kSecondScanAps = kFirstScanAps - 1; |
419 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); | 420 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); |
420 main_message_loop_.RunAllPending(); | 421 main_message_loop_.RunAllPending(); |
421 fetcher = get_url_fetcher_and_advance_id(); | 422 fetcher = get_url_fetcher_and_advance_id(); |
422 EXPECT_FALSE(fetcher); | 423 EXPECT_FALSE(fetcher); |
423 | 424 |
424 provider->GetPosition(&position); | 425 provider->GetPosition(&position); |
425 EXPECT_EQ(51.0, position.latitude); | 426 EXPECT_EQ(51.0, position.latitude); |
426 EXPECT_EQ(-0.1, position.longitude); | 427 EXPECT_EQ(-0.1, position.longitude); |
427 EXPECT_TRUE(position.IsValidFix()); | 428 EXPECT_TRUE(position.Validate()); |
428 | 429 |
429 // Now a third scan with more than twice the original amount -> new request. | 430 // Now a third scan with more than twice the original amount -> new request. |
430 const int kThirdScanAps = kFirstScanAps * 2 + 1; | 431 const int kThirdScanAps = kFirstScanAps * 2 + 1; |
431 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); | 432 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); |
432 main_message_loop_.RunAllPending(); | 433 main_message_loop_.RunAllPending(); |
433 fetcher = get_url_fetcher_and_advance_id(); | 434 fetcher = get_url_fetcher_and_advance_id(); |
434 EXPECT_TRUE(fetcher); | 435 EXPECT_TRUE(fetcher); |
435 CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, | 436 CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, |
436 kThirdScanAps, 0, | 437 kThirdScanAps, 0, |
437 REFERENCE_ACCESS_TOKEN); | 438 REFERENCE_ACCESS_TOKEN); |
438 // ...reply with a network error. | 439 // ...reply with a network error. |
439 | 440 |
440 fetcher->set_url(test_server_url_); | 441 fetcher->set_url(test_server_url_); |
441 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); | 442 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, -1)); |
442 fetcher->set_response_code(200); // should be ignored | 443 fetcher->set_response_code(200); // should be ignored |
443 fetcher->SetResponseString(""); | 444 fetcher->SetResponseString(""); |
444 fetcher->delegate()->OnURLFetchComplete(fetcher); | 445 fetcher->delegate()->OnURLFetchComplete(fetcher); |
445 | 446 |
446 // Error means we now no longer have a fix. | 447 // Error means we now no longer have a fix. |
447 provider->GetPosition(&position); | 448 provider->GetPosition(&position); |
448 EXPECT_FALSE(position.is_valid_latlong()); | 449 EXPECT_FALSE(position.Validate()); |
449 EXPECT_FALSE(position.IsValidFix()); | |
450 | 450 |
451 // Wifi scan returns to original set: should be serviced from cache. | 451 // Wifi scan returns to original set: should be serviced from cache. |
452 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 452 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
453 main_message_loop_.RunAllPending(); | 453 main_message_loop_.RunAllPending(); |
454 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. | 454 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. |
455 | 455 |
456 provider->GetPosition(&position); | 456 provider->GetPosition(&position); |
457 EXPECT_EQ(51.0, position.latitude); | 457 EXPECT_EQ(51.0, position.latitude); |
458 EXPECT_EQ(-0.1, position.longitude); | 458 EXPECT_EQ(-0.1, position.longitude); |
459 EXPECT_TRUE(position.IsValidFix()); | 459 EXPECT_TRUE(position.Validate()); |
460 } | 460 } |
461 | 461 |
462 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { | 462 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { |
463 MessageLoopQuitListener listener; | 463 MessageLoopQuitListener listener; |
464 wifi_data_provider_->set_got_data(false); | 464 wifi_data_provider_->set_got_data(false); |
465 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); | 465 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
466 EXPECT_TRUE(provider->StartProvider(false)); | 466 EXPECT_TRUE(provider->StartProvider(false)); |
467 provider->RegisterListener(&listener); | 467 provider->RegisterListener(&listener); |
468 | 468 |
469 main_message_loop_.RunAllPending(); | 469 main_message_loop_.RunAllPending(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); | 547 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
548 } else { | 548 } else { |
549 const int evicted = i - kCacheSize; | 549 const int evicted = i - kCacheSize; |
550 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 550 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
551 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 551 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
552 } | 552 } |
553 } | 553 } |
554 } | 554 } |
555 | 555 |
556 } // namespace | 556 } // namespace |
OLD | NEW |