geolocation_service_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/bind.h"
  5. #include "base/run_loop.h"
  6. #include "base/test/bind.h"
  7. #include "base/test/scoped_feature_list.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. #if BUILDFLAG(IS_CHROMEOS_ASH)
  11. #include "chromeos/ash/components/dbus/shill/shill_clients.h"
  12. #include "chromeos/ash/components/network/geolocation_handler.h"
  13. #endif
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "net/base/network_change_notifier.h"
  16. #include "services/device/device_service_test_base.h"
  17. #include "services/device/geolocation/geolocation_provider_impl.h"
  18. #include "services/device/geolocation/network_location_request.h"
  19. #include "services/device/public/cpp/device_features.h"
  20. #include "services/device/public/mojom/geolocation.mojom.h"
  21. #include "services/device/public/mojom/geolocation_config.mojom.h"
  22. #include "services/device/public/mojom/geolocation_context.mojom.h"
  23. #include "services/device/public/mojom/geolocation_control.mojom.h"
  24. #if BUILDFLAG(IS_MAC)
  25. #include "services/device/public/cpp/test/fake_geolocation_manager.h"
  26. #endif
  27. namespace device {
  28. namespace {
  29. void CheckBoolReturnValue(base::OnceClosure quit_closure,
  30. bool expect,
  31. bool result) {
  32. EXPECT_EQ(expect, result);
  33. std::move(quit_closure).Run();
  34. }
  35. class GeolocationServiceUnitTest : public DeviceServiceTestBase {
  36. public:
  37. GeolocationServiceUnitTest() = default;
  38. GeolocationServiceUnitTest(const GeolocationServiceUnitTest&) = delete;
  39. GeolocationServiceUnitTest& operator=(const GeolocationServiceUnitTest&) =
  40. delete;
  41. ~GeolocationServiceUnitTest() override = default;
  42. protected:
  43. void SetUp() override {
  44. #if BUILDFLAG(IS_CHROMEOS_ASH)
  45. ash::shill_clients::InitializeFakes();
  46. ash::NetworkHandler::Initialize();
  47. #endif
  48. network_change_notifier_ = net::NetworkChangeNotifier::CreateMockIfNeeded();
  49. // We need to initialize the above *before* the base fixture instantiates
  50. // the device service.
  51. DeviceServiceTestBase::SetUp();
  52. device_service()->BindGeolocationControl(
  53. geolocation_control_.BindNewPipeAndPassReceiver());
  54. geolocation_control_->UserDidOptIntoLocationServices();
  55. device_service()->BindGeolocationContext(
  56. geolocation_context_.BindNewPipeAndPassReceiver());
  57. geolocation_context_->BindGeolocation(
  58. geolocation_.BindNewPipeAndPassReceiver(), GURL::EmptyGURL());
  59. }
  60. void TearDown() override {
  61. DeviceServiceTestBase::TearDown();
  62. #if BUILDFLAG(IS_CHROMEOS_ASH)
  63. ash::NetworkHandler::Shutdown();
  64. ash::shill_clients::Shutdown();
  65. #endif
  66. // Let the GeolocationImpl destruct earlier than GeolocationProviderImpl to
  67. // make sure the base::RepeatingCallbackList<> member in
  68. // GeolocationProviderImpl is empty.
  69. geolocation_.reset();
  70. GeolocationProviderImpl::GetInstance()
  71. ->clear_user_did_opt_into_location_services_for_testing();
  72. base::RunLoop().RunUntilIdle();
  73. }
  74. void BindGeolocationConfig() {
  75. device_service()->BindGeolocationConfig(
  76. geolocation_config_.BindNewPipeAndPassReceiver());
  77. }
  78. std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
  79. mojo::Remote<mojom::GeolocationControl> geolocation_control_;
  80. mojo::Remote<mojom::GeolocationContext> geolocation_context_;
  81. mojo::Remote<mojom::Geolocation> geolocation_;
  82. mojo::Remote<mojom::GeolocationConfig> geolocation_config_;
  83. };
  84. #if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_ANDROID)
  85. // ChromeOS fails to perform network geolocation when zero wifi networks are
  86. // detected in a scan: https://crbug.com/767300.
  87. #else
  88. TEST_F(GeolocationServiceUnitTest, UrlWithApiKey) {
  89. // To align with user expectation we do not make Network Location Requests
  90. // on macOS unless the browser has Location Permission from the OS.
  91. #if BUILDFLAG(IS_MAC)
  92. fake_geolocation_manager_->SetSystemPermission(
  93. LocationSystemPermissionStatus::kAllowed);
  94. #endif
  95. base::RunLoop loop;
  96. test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
  97. [&loop](const network::ResourceRequest& request) {
  98. // Verify the full URL including a fake Google API key.
  99. std::string expected_url =
  100. "https://www.googleapis.com/geolocation/v1/geolocate?key=";
  101. expected_url.append(kTestGeolocationApiKey);
  102. if (request.url == expected_url)
  103. loop.Quit();
  104. }));
  105. geolocation_->SetHighAccuracy(true);
  106. loop.Run();
  107. // Clearing interceptor callback to ensure it does not outlive this scope.
  108. test_url_loader_factory_.SetInterceptor(base::NullCallback());
  109. }
  110. #endif
  111. // TODO(https://crbug.com/912057): Flaky on Chrome OS / Fails often on *San.
  112. // TODO(https://crbug.com/999409): Also flaky on other platforms.
  113. TEST_F(GeolocationServiceUnitTest, DISABLED_GeolocationConfig) {
  114. BindGeolocationConfig();
  115. {
  116. base::RunLoop run_loop;
  117. geolocation_config_->IsHighAccuracyLocationBeingCaptured(
  118. base::BindOnce(&CheckBoolReturnValue, run_loop.QuitClosure(), false));
  119. run_loop.Run();
  120. }
  121. geolocation_->SetHighAccuracy(true);
  122. {
  123. base::RunLoop run_loop;
  124. geolocation_config_->IsHighAccuracyLocationBeingCaptured(
  125. base::BindOnce(&CheckBoolReturnValue, run_loop.QuitClosure(), true));
  126. run_loop.Run();
  127. }
  128. }
  129. } // namespace
  130. } // namespace device