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

Side by Side Diff: content/browser/geolocation/network_location_provider_unittest.cc

Issue 2082343002: Remove calls to deprecated MessageLoop methods in content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR Created 4 years, 5 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 (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/geolocation/network_location_provider.h" 5 #include "content/browser/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>
11 11
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "content/browser/geolocation/fake_access_token_store.h" 21 #include "content/browser/geolocation/fake_access_token_store.h"
21 #include "content/browser/geolocation/location_arbitrator_impl.h" 22 #include "content/browser/geolocation/location_arbitrator_impl.h"
22 #include "content/browser/geolocation/wifi_data_provider.h" 23 #include "content/browser/geolocation/wifi_data_provider.h"
23 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
24 #include "net/url_request/test_url_fetcher_factory.h" 25 #include "net/url_request/test_url_fetcher_factory.h"
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); 346 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
346 ASSERT_TRUE(fetcher != NULL); 347 ASSERT_TRUE(fetcher != NULL);
347 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string()); 348 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string());
348 } 349 }
349 350
350 TEST_F(GeolocationNetworkProviderTest, StartProviderLongRequest) { 351 TEST_F(GeolocationNetworkProviderTest, StartProviderLongRequest) {
351 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); 352 std::unique_ptr<LocationProvider> provider(CreateProvider(true));
352 EXPECT_TRUE(provider->StartProvider(false)); 353 EXPECT_TRUE(provider->StartProvider(false));
353 const int kFirstScanAps = 20; 354 const int kFirstScanAps = 20;
354 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); 355 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps));
355 main_message_loop_.RunUntilIdle(); 356 base::RunLoop().RunUntilIdle();
356 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); 357 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
357 ASSERT_TRUE(fetcher != NULL); 358 ASSERT_TRUE(fetcher != NULL);
358 // The request url should have been shortened to less than 2048 characters 359 // The request url should have been shortened to less than 2048 characters
359 // in length by not including access points with the lowest signal strength 360 // in length by not including access points with the lowest signal strength
360 // in the request. 361 // in the request.
361 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048)); 362 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048));
362 CheckRequestIsValid(*fetcher, 0, 16, 4, std::string()); 363 CheckRequestIsValid(*fetcher, 0, 16, 4, std::string());
363 } 364 }
364 365
365 TEST_F(GeolocationNetworkProviderTest, MultipleWifiScansComplete) { 366 TEST_F(GeolocationNetworkProviderTest, MultipleWifiScansComplete) {
(...skipping 15 matching lines...) Expand all
381 fetcher->SetResponseString(kNoFixNetworkResponse); 382 fetcher->SetResponseString(kNoFixNetworkResponse);
382 fetcher->delegate()->OnURLFetchComplete(fetcher); 383 fetcher->delegate()->OnURLFetchComplete(fetcher);
383 384
384 Geoposition position; 385 Geoposition position;
385 provider->GetPosition(&position); 386 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 main_message_loop_.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 != NULL); 394 ASSERT_TRUE(fetcher != NULL);
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 " \"accuracy\": 1200.4," 402 " \"accuracy\": 1200.4,"
(...skipping 16 matching lines...) Expand all
418 EXPECT_TRUE(position.Validate()); 419 EXPECT_TRUE(position.Validate());
419 420
420 // Token should be in the store. 421 // Token should be in the store.
421 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), 422 EXPECT_EQ(base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN),
422 access_token_store_->access_token_map_[test_server_url_]); 423 access_token_store_->access_token_map_[test_server_url_]);
423 424
424 // Wifi updated again, with one less AP. This is 'close enough' to the 425 // Wifi updated again, with one less AP. This is 'close enough' to the
425 // previous scan, so no new request made. 426 // previous scan, so no new request made.
426 const int kSecondScanAps = kFirstScanAps - 1; 427 const int kSecondScanAps = kFirstScanAps - 1;
427 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps)); 428 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kSecondScanAps));
428 main_message_loop_.RunUntilIdle(); 429 base::RunLoop().RunUntilIdle();
429 fetcher = get_url_fetcher_and_advance_id(); 430 fetcher = get_url_fetcher_and_advance_id();
430 EXPECT_FALSE(fetcher); 431 EXPECT_FALSE(fetcher);
431 432
432 provider->GetPosition(&position); 433 provider->GetPosition(&position);
433 EXPECT_EQ(51.0, position.latitude); 434 EXPECT_EQ(51.0, position.latitude);
434 EXPECT_EQ(-0.1, position.longitude); 435 EXPECT_EQ(-0.1, position.longitude);
435 EXPECT_TRUE(position.Validate()); 436 EXPECT_TRUE(position.Validate());
436 437
437 // Now a third scan with more than twice the original amount -> new request. 438 // Now a third scan with more than twice the original amount -> new request.
438 const int kThirdScanAps = kFirstScanAps * 2 + 1; 439 const int kThirdScanAps = kFirstScanAps * 2 + 1;
439 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps)); 440 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kThirdScanAps));
440 main_message_loop_.RunUntilIdle(); 441 base::RunLoop().RunUntilIdle();
441 fetcher = get_url_fetcher_and_advance_id(); 442 fetcher = get_url_fetcher_and_advance_id();
442 EXPECT_TRUE(fetcher); 443 EXPECT_TRUE(fetcher);
443 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN); 444 CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN);
444 // ...reply with a network error. 445 // ...reply with a network error.
445 446
446 fetcher->set_url(test_server_url_); 447 fetcher->set_url(test_server_url_);
447 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); 448 fetcher->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED));
448 fetcher->set_response_code(200); // should be ignored 449 fetcher->set_response_code(200); // should be ignored
449 fetcher->SetResponseString(std::string()); 450 fetcher->SetResponseString(std::string());
450 fetcher->delegate()->OnURLFetchComplete(fetcher); 451 fetcher->delegate()->OnURLFetchComplete(fetcher);
451 452
452 // Error means we now no longer have a fix. 453 // Error means we now no longer have a fix.
453 provider->GetPosition(&position); 454 provider->GetPosition(&position);
454 EXPECT_FALSE(position.Validate()); 455 EXPECT_FALSE(position.Validate());
455 456
456 // Wifi scan returns to original set: should be serviced from cache. 457 // Wifi scan returns to original set: should be serviced from cache.
457 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); 458 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps));
458 main_message_loop_.RunUntilIdle(); 459 base::RunLoop().RunUntilIdle();
459 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. 460 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created.
460 461
461 provider->GetPosition(&position); 462 provider->GetPosition(&position);
462 EXPECT_EQ(51.0, position.latitude); 463 EXPECT_EQ(51.0, position.latitude);
463 EXPECT_EQ(-0.1, position.longitude); 464 EXPECT_EQ(-0.1, position.longitude);
464 EXPECT_TRUE(position.Validate()); 465 EXPECT_TRUE(position.Validate());
465 } 466 }
466 467
467 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { 468 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) {
468 MessageLoopQuitListener listener; 469 MessageLoopQuitListener listener;
469 wifi_data_provider_->set_got_data(false); 470 wifi_data_provider_->set_got_data(false);
470 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); 471 std::unique_ptr<LocationProvider> provider(CreateProvider(true));
471 EXPECT_TRUE(provider->StartProvider(false)); 472 EXPECT_TRUE(provider->StartProvider(false));
472 473
473 provider->SetUpdateCallback(base::Bind( 474 provider->SetUpdateCallback(base::Bind(
474 &MessageLoopQuitListener::OnLocationUpdate, base::Unretained(&listener))); 475 &MessageLoopQuitListener::OnLocationUpdate, base::Unretained(&listener)));
475 476
476 main_message_loop_.RunUntilIdle(); 477 base::RunLoop().RunUntilIdle();
477 EXPECT_FALSE(get_url_fetcher_and_advance_id()) 478 EXPECT_FALSE(get_url_fetcher_and_advance_id())
478 << "Network request should not be created right away on startup when " 479 << "Network request should not be created right away on startup when "
479 "wifi data has not yet arrived"; 480 "wifi data has not yet arrived";
480 481
481 wifi_data_provider_->SetData(CreateReferenceWifiScanData(1)); 482 wifi_data_provider_->SetData(CreateReferenceWifiScanData(1));
482 main_message_loop_.RunUntilIdle(); 483 base::RunLoop().RunUntilIdle();
483 EXPECT_TRUE(get_url_fetcher_and_advance_id()); 484 EXPECT_TRUE(get_url_fetcher_and_advance_id());
484 } 485 }
485 486
486 TEST_F(GeolocationNetworkProviderTest, NewDataReplacesExistingNetworkRequest) { 487 TEST_F(GeolocationNetworkProviderTest, NewDataReplacesExistingNetworkRequest) {
487 // Send initial request with empty data 488 // Send initial request with empty data
488 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); 489 std::unique_ptr<LocationProvider> provider(CreateProvider(true));
489 EXPECT_TRUE(provider->StartProvider(false)); 490 EXPECT_TRUE(provider->StartProvider(false));
490 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); 491 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
491 EXPECT_TRUE(fetcher); 492 EXPECT_TRUE(fetcher);
492 493
493 // Now wifi data arrives; new request should be sent. 494 // Now wifi data arrives; new request should be sent.
494 wifi_data_provider_->SetData(CreateReferenceWifiScanData(4)); 495 wifi_data_provider_->SetData(CreateReferenceWifiScanData(4));
495 main_message_loop_.RunUntilIdle(); 496 base::RunLoop().RunUntilIdle();
496 fetcher = get_url_fetcher_and_advance_id(); 497 fetcher = get_url_fetcher_and_advance_id();
497 EXPECT_TRUE(fetcher); 498 EXPECT_TRUE(fetcher);
498 } 499 }
499 500
500 TEST_F(GeolocationNetworkProviderTest, NetworkRequestDeferredForPermission) { 501 TEST_F(GeolocationNetworkProviderTest, NetworkRequestDeferredForPermission) {
501 std::unique_ptr<LocationProvider> provider(CreateProvider(false)); 502 std::unique_ptr<LocationProvider> provider(CreateProvider(false));
502 EXPECT_TRUE(provider->StartProvider(false)); 503 EXPECT_TRUE(provider->StartProvider(false));
503 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); 504 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
504 EXPECT_FALSE(fetcher); 505 EXPECT_FALSE(fetcher);
505 provider->OnPermissionGranted(); 506 provider->OnPermissionGranted();
506 507
507 fetcher = get_url_fetcher_and_advance_id(); 508 fetcher = get_url_fetcher_and_advance_id();
508 ASSERT_TRUE(fetcher != NULL); 509 ASSERT_TRUE(fetcher != NULL);
509 510
510 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL())); 511 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL()));
511 } 512 }
512 513
513 TEST_F(GeolocationNetworkProviderTest, 514 TEST_F(GeolocationNetworkProviderTest,
514 NetworkRequestWithWifiDataDeferredForPermission) { 515 NetworkRequestWithWifiDataDeferredForPermission) {
515 access_token_store_->access_token_map_[test_server_url_] = 516 access_token_store_->access_token_map_[test_server_url_] =
516 base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN); 517 base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN);
517 std::unique_ptr<LocationProvider> provider(CreateProvider(false)); 518 std::unique_ptr<LocationProvider> provider(CreateProvider(false));
518 EXPECT_TRUE(provider->StartProvider(false)); 519 EXPECT_TRUE(provider->StartProvider(false));
519 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); 520 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
520 EXPECT_FALSE(fetcher); 521 EXPECT_FALSE(fetcher);
521 522
522 static const int kScanCount = 4; 523 static const int kScanCount = 4;
523 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kScanCount)); 524 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kScanCount));
524 main_message_loop_.RunUntilIdle(); 525 base::RunLoop().RunUntilIdle();
525 526
526 fetcher = get_url_fetcher_and_advance_id(); 527 fetcher = get_url_fetcher_and_advance_id();
527 EXPECT_FALSE(fetcher); 528 EXPECT_FALSE(fetcher);
528 529
529 provider->OnPermissionGranted(); 530 provider->OnPermissionGranted();
530 531
531 fetcher = get_url_fetcher_and_advance_id(); 532 fetcher = get_url_fetcher_and_advance_id();
532 ASSERT_TRUE(fetcher != NULL); 533 ASSERT_TRUE(fetcher != NULL);
533 534
534 CheckRequestIsValid(*fetcher, 0, kScanCount, 0, REFERENCE_ACCESS_TOKEN); 535 CheckRequestIsValid(*fetcher, 0, kScanCount, 0, REFERENCE_ACCESS_TOKEN);
(...skipping 17 matching lines...) Expand all
552 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); 553 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1)));
553 } else { 554 } else {
554 const int evicted = i - kCacheSize; 555 const int evicted = i - kCacheSize;
555 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); 556 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted)));
556 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); 557 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1)));
557 } 558 }
558 } 559 }
559 } 560 }
560 561
561 } // namespace content 562 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698