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 "content/browser/geolocation/network_location_provider.h" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 | 8 |
| 9 #include <memory> |
| 10 |
7 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
9 #include "base/macros.h" | 13 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 18 #include "base/values.h" |
16 #include "content/browser/geolocation/fake_access_token_store.h" | 19 #include "content/browser/geolocation/fake_access_token_store.h" |
17 #include "content/browser/geolocation/location_arbitrator_impl.h" | 20 #include "content/browser/geolocation/location_arbitrator_impl.h" |
18 #include "content/browser/geolocation/network_location_provider.h" | |
19 #include "content/browser/geolocation/wifi_data_provider.h" | 21 #include "content/browser/geolocation/wifi_data_provider.h" |
20 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
21 #include "net/url_request/test_url_fetcher_factory.h" | 23 #include "net/url_request/test_url_fetcher_factory.h" |
22 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 namespace content { | 27 namespace content { |
26 | 28 |
27 // Constants used in multiple tests. | 29 // Constants used in multiple tests. |
28 const char kTestServerUrl[] = "https://www.geolocation.test/service"; | 30 const char kTestServerUrl[] = "https://www.geolocation.test/service"; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 257 |
256 // Check to see that the api key is being appended for the default | 258 // Check to see that the api key is being appended for the default |
257 // network provider url. | 259 // network provider url. |
258 bool is_default_url = UrlWithoutQuery(request_url) == | 260 bool is_default_url = UrlWithoutQuery(request_url) == |
259 UrlWithoutQuery(LocationArbitratorImpl::DefaultNetworkProviderURL()); | 261 UrlWithoutQuery(LocationArbitratorImpl::DefaultNetworkProviderURL()); |
260 EXPECT_EQ(is_default_url, !request_url.query().empty()); | 262 EXPECT_EQ(is_default_url, !request_url.query().empty()); |
261 | 263 |
262 const std::string& upload_data = request.upload_data(); | 264 const std::string& upload_data = request.upload_data(); |
263 ASSERT_FALSE(upload_data.empty()); | 265 ASSERT_FALSE(upload_data.empty()); |
264 std::string json_parse_error_msg; | 266 std::string json_parse_error_msg; |
265 scoped_ptr<base::Value> parsed_json = base::JSONReader::ReadAndReturnError( | 267 std::unique_ptr<base::Value> parsed_json = |
266 upload_data, base::JSON_PARSE_RFC, NULL, &json_parse_error_msg); | 268 base::JSONReader::ReadAndReturnError(upload_data, base::JSON_PARSE_RFC, |
| 269 NULL, &json_parse_error_msg); |
267 EXPECT_TRUE(json_parse_error_msg.empty()); | 270 EXPECT_TRUE(json_parse_error_msg.empty()); |
268 ASSERT_TRUE(parsed_json.get() != NULL); | 271 ASSERT_TRUE(parsed_json.get() != NULL); |
269 | 272 |
270 const base::DictionaryValue* request_json; | 273 const base::DictionaryValue* request_json; |
271 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json)); | 274 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json)); |
272 | 275 |
273 if (!is_default_url) { | 276 if (!is_default_url) { |
274 if (expected_access_token.empty()) | 277 if (expected_access_token.empty()) |
275 ASSERT_FALSE(request_json->HasKey(kAccessTokenString)); | 278 ASSERT_FALSE(request_json->HasKey(kAccessTokenString)); |
276 else { | 279 else { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 GURL test_server_url_; | 316 GURL test_server_url_; |
314 base::MessageLoop main_message_loop_; | 317 base::MessageLoop main_message_loop_; |
315 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 318 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
316 net::TestURLFetcherFactory url_fetcher_factory_; | 319 net::TestURLFetcherFactory url_fetcher_factory_; |
317 scoped_refptr<MockWifiDataProvider> wifi_data_provider_; | 320 scoped_refptr<MockWifiDataProvider> wifi_data_provider_; |
318 }; | 321 }; |
319 | 322 |
320 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { | 323 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { |
321 // Test fixture members were SetUp correctly. | 324 // Test fixture members were SetUp correctly. |
322 EXPECT_EQ(&main_message_loop_, base::MessageLoop::current()); | 325 EXPECT_EQ(&main_message_loop_, base::MessageLoop::current()); |
323 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 326 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
324 EXPECT_TRUE(NULL != provider.get()); | 327 EXPECT_TRUE(NULL != provider.get()); |
325 provider.reset(); | 328 provider.reset(); |
326 SUCCEED(); | 329 SUCCEED(); |
327 } | 330 } |
328 | 331 |
329 TEST_F(GeolocationNetworkProviderTest, StartProvider) { | 332 TEST_F(GeolocationNetworkProviderTest, StartProvider) { |
330 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 333 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
331 EXPECT_TRUE(provider->StartProvider(false)); | 334 EXPECT_TRUE(provider->StartProvider(false)); |
332 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 335 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
333 ASSERT_TRUE(fetcher != NULL); | 336 ASSERT_TRUE(fetcher != NULL); |
334 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string()); | 337 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string()); |
335 } | 338 } |
336 | 339 |
337 TEST_F(GeolocationNetworkProviderTest, StartProviderDefaultUrl) { | 340 TEST_F(GeolocationNetworkProviderTest, StartProviderDefaultUrl) { |
338 test_server_url_ = LocationArbitratorImpl::DefaultNetworkProviderURL(); | 341 test_server_url_ = LocationArbitratorImpl::DefaultNetworkProviderURL(); |
339 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 342 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
340 EXPECT_TRUE(provider->StartProvider(false)); | 343 EXPECT_TRUE(provider->StartProvider(false)); |
341 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 344 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
342 ASSERT_TRUE(fetcher != NULL); | 345 ASSERT_TRUE(fetcher != NULL); |
343 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string()); | 346 CheckRequestIsValid(*fetcher, 0, 0, 0, std::string()); |
344 } | 347 } |
345 | 348 |
346 TEST_F(GeolocationNetworkProviderTest, StartProviderLongRequest) { | 349 TEST_F(GeolocationNetworkProviderTest, StartProviderLongRequest) { |
347 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 350 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
348 EXPECT_TRUE(provider->StartProvider(false)); | 351 EXPECT_TRUE(provider->StartProvider(false)); |
349 const int kFirstScanAps = 20; | 352 const int kFirstScanAps = 20; |
350 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 353 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
351 main_message_loop_.RunUntilIdle(); | 354 main_message_loop_.RunUntilIdle(); |
352 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 355 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
353 ASSERT_TRUE(fetcher != NULL); | 356 ASSERT_TRUE(fetcher != NULL); |
354 // The request url should have been shortened to less than 2048 characters | 357 // The request url should have been shortened to less than 2048 characters |
355 // in length by not including access points with the lowest signal strength | 358 // in length by not including access points with the lowest signal strength |
356 // in the request. | 359 // in the request. |
357 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048)); | 360 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048)); |
358 CheckRequestIsValid(*fetcher, 0, 16, 4, std::string()); | 361 CheckRequestIsValid(*fetcher, 0, 16, 4, std::string()); |
359 } | 362 } |
360 | 363 |
361 TEST_F(GeolocationNetworkProviderTest, MultipleWifiScansComplete) { | 364 TEST_F(GeolocationNetworkProviderTest, MultipleWifiScansComplete) { |
362 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 365 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
363 EXPECT_TRUE(provider->StartProvider(false)); | 366 EXPECT_TRUE(provider->StartProvider(false)); |
364 | 367 |
365 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 368 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
366 ASSERT_TRUE(fetcher != NULL); | 369 ASSERT_TRUE(fetcher != NULL); |
367 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL())); | 370 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL())); |
368 | 371 |
369 // Complete the network request with bad position fix. | 372 // Complete the network request with bad position fix. |
370 const char* kNoFixNetworkResponse = | 373 const char* kNoFixNetworkResponse = |
371 "{" | 374 "{" |
372 " \"status\": \"ZERO_RESULTS\"" | 375 " \"status\": \"ZERO_RESULTS\"" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 | 459 |
457 provider->GetPosition(&position); | 460 provider->GetPosition(&position); |
458 EXPECT_EQ(51.0, position.latitude); | 461 EXPECT_EQ(51.0, position.latitude); |
459 EXPECT_EQ(-0.1, position.longitude); | 462 EXPECT_EQ(-0.1, position.longitude); |
460 EXPECT_TRUE(position.Validate()); | 463 EXPECT_TRUE(position.Validate()); |
461 } | 464 } |
462 | 465 |
463 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { | 466 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { |
464 MessageLoopQuitListener listener; | 467 MessageLoopQuitListener listener; |
465 wifi_data_provider_->set_got_data(false); | 468 wifi_data_provider_->set_got_data(false); |
466 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 469 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
467 EXPECT_TRUE(provider->StartProvider(false)); | 470 EXPECT_TRUE(provider->StartProvider(false)); |
468 | 471 |
469 provider->SetUpdateCallback(base::Bind( | 472 provider->SetUpdateCallback(base::Bind( |
470 &MessageLoopQuitListener::OnLocationUpdate, base::Unretained(&listener))); | 473 &MessageLoopQuitListener::OnLocationUpdate, base::Unretained(&listener))); |
471 | 474 |
472 main_message_loop_.RunUntilIdle(); | 475 main_message_loop_.RunUntilIdle(); |
473 EXPECT_FALSE(get_url_fetcher_and_advance_id()) | 476 EXPECT_FALSE(get_url_fetcher_and_advance_id()) |
474 << "Network request should not be created right away on startup when " | 477 << "Network request should not be created right away on startup when " |
475 "wifi data has not yet arrived"; | 478 "wifi data has not yet arrived"; |
476 | 479 |
477 wifi_data_provider_->SetData(CreateReferenceWifiScanData(1)); | 480 wifi_data_provider_->SetData(CreateReferenceWifiScanData(1)); |
478 main_message_loop_.RunUntilIdle(); | 481 main_message_loop_.RunUntilIdle(); |
479 EXPECT_TRUE(get_url_fetcher_and_advance_id()); | 482 EXPECT_TRUE(get_url_fetcher_and_advance_id()); |
480 } | 483 } |
481 | 484 |
482 TEST_F(GeolocationNetworkProviderTest, NewDataReplacesExistingNetworkRequest) { | 485 TEST_F(GeolocationNetworkProviderTest, NewDataReplacesExistingNetworkRequest) { |
483 // Send initial request with empty data | 486 // Send initial request with empty data |
484 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 487 std::unique_ptr<LocationProvider> provider(CreateProvider(true)); |
485 EXPECT_TRUE(provider->StartProvider(false)); | 488 EXPECT_TRUE(provider->StartProvider(false)); |
486 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 489 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
487 EXPECT_TRUE(fetcher); | 490 EXPECT_TRUE(fetcher); |
488 | 491 |
489 // Now wifi data arrives; new request should be sent. | 492 // Now wifi data arrives; new request should be sent. |
490 wifi_data_provider_->SetData(CreateReferenceWifiScanData(4)); | 493 wifi_data_provider_->SetData(CreateReferenceWifiScanData(4)); |
491 main_message_loop_.RunUntilIdle(); | 494 main_message_loop_.RunUntilIdle(); |
492 fetcher = get_url_fetcher_and_advance_id(); | 495 fetcher = get_url_fetcher_and_advance_id(); |
493 EXPECT_TRUE(fetcher); | 496 EXPECT_TRUE(fetcher); |
494 } | 497 } |
495 | 498 |
496 TEST_F(GeolocationNetworkProviderTest, NetworkRequestDeferredForPermission) { | 499 TEST_F(GeolocationNetworkProviderTest, NetworkRequestDeferredForPermission) { |
497 scoped_ptr<LocationProvider> provider(CreateProvider(false)); | 500 std::unique_ptr<LocationProvider> provider(CreateProvider(false)); |
498 EXPECT_TRUE(provider->StartProvider(false)); | 501 EXPECT_TRUE(provider->StartProvider(false)); |
499 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 502 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
500 EXPECT_FALSE(fetcher); | 503 EXPECT_FALSE(fetcher); |
501 provider->OnPermissionGranted(); | 504 provider->OnPermissionGranted(); |
502 | 505 |
503 fetcher = get_url_fetcher_and_advance_id(); | 506 fetcher = get_url_fetcher_and_advance_id(); |
504 ASSERT_TRUE(fetcher != NULL); | 507 ASSERT_TRUE(fetcher != NULL); |
505 | 508 |
506 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL())); | 509 EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL())); |
507 } | 510 } |
508 | 511 |
509 TEST_F(GeolocationNetworkProviderTest, | 512 TEST_F(GeolocationNetworkProviderTest, |
510 NetworkRequestWithWifiDataDeferredForPermission) { | 513 NetworkRequestWithWifiDataDeferredForPermission) { |
511 access_token_store_->access_token_map_[test_server_url_] = | 514 access_token_store_->access_token_map_[test_server_url_] = |
512 base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN); | 515 base::UTF8ToUTF16(REFERENCE_ACCESS_TOKEN); |
513 scoped_ptr<LocationProvider> provider(CreateProvider(false)); | 516 std::unique_ptr<LocationProvider> provider(CreateProvider(false)); |
514 EXPECT_TRUE(provider->StartProvider(false)); | 517 EXPECT_TRUE(provider->StartProvider(false)); |
515 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 518 net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
516 EXPECT_FALSE(fetcher); | 519 EXPECT_FALSE(fetcher); |
517 | 520 |
518 static const int kScanCount = 4; | 521 static const int kScanCount = 4; |
519 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kScanCount)); | 522 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kScanCount)); |
520 main_message_loop_.RunUntilIdle(); | 523 main_message_loop_.RunUntilIdle(); |
521 | 524 |
522 fetcher = get_url_fetcher_and_advance_id(); | 525 fetcher = get_url_fetcher_and_advance_id(); |
523 EXPECT_FALSE(fetcher); | 526 EXPECT_FALSE(fetcher); |
(...skipping 24 matching lines...) Expand all Loading... |
548 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); | 551 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
549 } else { | 552 } else { |
550 const int evicted = i - kCacheSize; | 553 const int evicted = i - kCacheSize; |
551 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 554 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
552 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 555 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
553 } | 556 } |
554 } | 557 } |
555 } | 558 } |
556 | 559 |
557 } // namespace content | 560 } // namespace content |
OLD | NEW |