Commit 94a5b918 authored by Ke He's avatar Ke He Committed by Commit Bot

Remove OverrideLocationForTesting() from test clients

Tests shouldn't call the OverrideLocationForTesting() since the Geolocation
core will be moved to //service/device.

In this CL a common ScopedGeolocationOverrider implementation is moved to
//device/geolocation/public/cpp, so it can be reused by multiple clients.

The ui_test_utils::OverrideGeolocation() is not removed in this CL because
it is still needed by UrlWithApiKey in GeolocationBrowserTest. We'll rewrite
it as a Service-Unittest when moving geolocation-core into //service/device
folder, and remove the ui_test_utils::OverrideGeolocation() then.

BUG=788298

Change-Id: I43c029bb5e82058673e738138cdadf139a076ff2
Reviewed-on: https://chromium-review.googlesource.com/778488
Commit-Queue: Ke He <ke.he@intel.com>
Reviewed-by: default avatarMiguel Casas <mcasas@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524312}
parent d0d768d3
...@@ -1743,6 +1743,7 @@ split_static_library("browser") { ...@@ -1743,6 +1743,7 @@ split_static_library("browser") {
"//crypto", "//crypto",
"//crypto:platform", "//crypto:platform",
"//device/base", "//device/base",
"//device/geolocation",
"//device/usb/mojo", "//device/usb/mojo",
"//device/usb/public/interfaces", "//device/usb/public/interfaces",
"//device/vr/features", "//device/vr/features",
......
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
#include "content/public/test/test_navigation_observer.h" #include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_renderer_host.h" #include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
#include "extensions/browser/api/declarative/rules_registry.h" #include "extensions/browser/api/declarative/rules_registry.h"
#include "extensions/browser/api/declarative/rules_registry_service.h" #include "extensions/browser/api/declarative/rules_registry_service.h"
#include "extensions/browser/api/declarative/test_rules_registry.h" #include "extensions/browser/api/declarative/test_rules_registry.h"
...@@ -514,7 +515,8 @@ class WebViewTestBase : public extensions::PlatformAppBrowserTest { ...@@ -514,7 +515,8 @@ class WebViewTestBase : public extensions::PlatformAppBrowserTest {
// Mock out geolocation for geolocation specific tests. // Mock out geolocation for geolocation specific tests.
if (!strncmp(test_info->name(), "GeolocationAPI", if (!strncmp(test_info->name(), "GeolocationAPI",
strlen("GeolocationAPI"))) { strlen("GeolocationAPI"))) {
ui_test_utils::OverrideGeolocation(10, 20); geolocation_overrider_ =
std::make_unique<device::ScopedGeolocationOverrider>(10, 20);
} }
} }
...@@ -839,6 +841,7 @@ class WebViewTestBase : public extensions::PlatformAppBrowserTest { ...@@ -839,6 +841,7 @@ class WebViewTestBase : public extensions::PlatformAppBrowserTest {
return !strncmp(test_info->name(), name, strlen(name)); return !strncmp(test_info->name(), name, strlen(name));
} }
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
std::unique_ptr<content::FakeSpeechRecognitionManager> std::unique_ptr<content::FakeSpeechRecognitionManager>
fake_speech_recognition_manager_; fake_speech_recognition_manager_;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/test/base/ui_test_utils.h" #include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
class GeolocationApiTest : public ExtensionApiTest { class GeolocationApiTest : public ExtensionApiTest {
public: public:
...@@ -12,8 +12,12 @@ class GeolocationApiTest : public ExtensionApiTest { ...@@ -12,8 +12,12 @@ class GeolocationApiTest : public ExtensionApiTest {
// InProcessBrowserTest // InProcessBrowserTest
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
ui_test_utils::OverrideGeolocation(0, 0); geolocation_overrider_ =
std::make_unique<device::ScopedGeolocationOverrider>(0, 0);
} }
private:
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
}; };
// http://crbug.com/68287 // http://crbug.com/68287
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h" #include "content/public/test/browser_test_utils.h"
#include "device/geolocation/network_location_request.h" #include "device/geolocation/network_location_request.h"
#include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
#include "device/geolocation/public/interfaces/geoposition.mojom.h" #include "device/geolocation/public/interfaces/geoposition.mojom.h"
#include "google_apis/google_api_keys.h" #include "google_apis/google_api_keys.h"
#include "net/base/escape.h" #include "net/base/escape.h"
...@@ -232,10 +233,9 @@ class GeolocationBrowserTest : public InProcessBrowserTest { ...@@ -232,10 +233,9 @@ class GeolocationBrowserTest : public InProcessBrowserTest {
}; };
GeolocationBrowserTest(); GeolocationBrowserTest();
~GeolocationBrowserTest() override; ~GeolocationBrowserTest() override = default;
// InProcessBrowserTest: // InProcessBrowserTest:
void SetUpOnMainThread() override;
void TearDownInProcessBrowserTestFixture() override; void TearDownInProcessBrowserTestFixture() override;
Browser* current_browser() { return current_browser_; } Browser* current_browser() { return current_browser_; }
...@@ -293,6 +293,12 @@ class GeolocationBrowserTest : public InProcessBrowserTest { ...@@ -293,6 +293,12 @@ class GeolocationBrowserTest : public InProcessBrowserTest {
// Convenience method to look up the number of queued permission requests. // Convenience method to look up the number of queued permission requests.
int GetRequestQueueSize(PermissionRequestManager* manager); int GetRequestQueueSize(PermissionRequestManager* manager);
protected:
// The values used for the position override.
double fake_latitude_ = 1.23;
double fake_longitude_ = 4.56;
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
private: private:
// Calls watchPosition() in JavaScript and accepts or denies the resulting // Calls watchPosition() in JavaScript and accepts or denies the resulting
// permission request. Returns the JavaScript response. // permission request. Returns the JavaScript response.
...@@ -314,23 +320,41 @@ class GeolocationBrowserTest : public InProcessBrowserTest { ...@@ -314,23 +320,41 @@ class GeolocationBrowserTest : public InProcessBrowserTest {
// The urls for the iframes loaded by LoadIFrames. // The urls for the iframes loaded by LoadIFrames.
std::vector<GURL> iframe_urls_; std::vector<GURL> iframe_urls_;
// The values used for the position override.
double fake_latitude_ = 1.23;
double fake_longitude_ = 4.56;
DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTest); DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTest);
}; };
GeolocationBrowserTest::GeolocationBrowserTest() { // This class is only used by test case of UrlWithApiKey which connects the
} // real geolocation implementation instead of the FakeGeolocation.
// TODO(ke.he@intel.com): crbug.com/788298. Remove this class and rewrite the
// test case of UrlWithApiKey as a services_unittest. Also remove the
// ui_test_utils::OverrideGeolocation() then.
class GeolocationBrowserTestWithoutOverrider : public GeolocationBrowserTest {
public:
GeolocationBrowserTestWithoutOverrider();
~GeolocationBrowserTestWithoutOverrider() override = default;
void SetUpOnMainThread() override;
GeolocationBrowserTest::~GeolocationBrowserTest() { DISALLOW_COPY_AND_ASSIGN(GeolocationBrowserTestWithoutOverrider);
};
GeolocationBrowserTestWithoutOverrider::
GeolocationBrowserTestWithoutOverrider() {
geolocation_overrider_.reset();
} }
void GeolocationBrowserTest::SetUpOnMainThread() { void GeolocationBrowserTestWithoutOverrider::SetUpOnMainThread() {
ui_test_utils::OverrideGeolocation(fake_latitude_, fake_longitude_); ui_test_utils::OverrideGeolocation(fake_latitude_, fake_longitude_);
} }
// WebContentImpl tries to connect Device Service earlier than
// of SetUpOnMainThread(), so create the |geolocation_overrider_| here.
GeolocationBrowserTest::GeolocationBrowserTest()
: geolocation_overrider_(
std::make_unique<device::ScopedGeolocationOverrider>(
fake_latitude_,
fake_longitude_)) {}
void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() { void GeolocationBrowserTest::TearDownInProcessBrowserTestFixture() {
LOG(WARNING) << "TearDownInProcessBrowserTestFixture. Test Finished."; LOG(WARNING) << "TearDownInProcessBrowserTestFixture. Test Finished.";
} }
...@@ -452,7 +476,8 @@ bool GeolocationBrowserTest::SetPositionAndWaitUntilUpdated(double latitude, ...@@ -452,7 +476,8 @@ bool GeolocationBrowserTest::SetPositionAndWaitUntilUpdated(double latitude,
fake_latitude_ = latitude; fake_latitude_ = latitude;
fake_longitude_ = longitude; fake_longitude_ = longitude;
ui_test_utils::OverrideGeolocation(latitude, longitude);
geolocation_overrider_->UpdateLocation(fake_latitude_, fake_longitude_);
std::string result; std::string result;
if (!dom_message_queue.WaitForMessage(&result)) if (!dom_message_queue.WaitForMessage(&result))
...@@ -495,7 +520,8 @@ IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, Geoposition) { ...@@ -495,7 +520,8 @@ IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, Geoposition) {
#endif #endif
// Tests that Chrome makes a network geolocation request to the correct URL // Tests that Chrome makes a network geolocation request to the correct URL
// including Google API key query param. // including Google API key query param.
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, MAYBE_UrlWithApiKey) { IN_PROC_BROWSER_TEST_F(GeolocationBrowserTestWithoutOverrider,
MAYBE_UrlWithApiKey) {
ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT)); ASSERT_NO_FATAL_FAILURE(Initialize(INITIALIZATION_DEFAULT));
// Unique ID (derived from Gerrit CL number): // Unique ID (derived from Gerrit CL number):
......
...@@ -445,7 +445,8 @@ if (!is_ios) { ...@@ -445,7 +445,8 @@ if (!is_ios) {
"//content/test:browsertest_support", "//content/test:browsertest_support",
"//content/test:test_support", "//content/test:test_support",
"//device/bluetooth", "//device/bluetooth",
"//device/geolocation", "//device/geolocation/public/cpp:test_support",
"//device/geolocation/public/interfaces",
"//ipc:test_support", "//ipc:test_support",
"//net:test_support", "//net:test_support",
"//printing/features", "//printing/features",
......
...@@ -33,7 +33,6 @@ static_library("browser") { ...@@ -33,7 +33,6 @@ static_library("browser") {
"//components/user_prefs", "//components/user_prefs",
"//content/public/browser", "//content/public/browser",
"//content/public/common", "//content/public/common",
"//device/geolocation",
"//device/geolocation/public/cpp", "//device/geolocation/public/cpp",
"//device/geolocation/public/interfaces", "//device/geolocation/public/interfaces",
"//gpu/config", "//gpu/config",
......
...@@ -15,15 +15,12 @@ ...@@ -15,15 +15,12 @@
#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h" #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
#include "content/public/browser/gpu_data_manager.h" #include "content/public/browser/gpu_data_manager.h"
#include "content/public/common/screen_info.h" #include "content/public/common/screen_info.h"
#include "content/public/common/service_manager_connection.h"
#include "content/public/test/content_browser_test.h" #include "content/public/test/content_browser_test.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "device/geolocation/public/interfaces/geolocation.mojom.h" #include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
#include "device/geolocation/public/interfaces/geolocation_context.mojom.h"
#include "device/geolocation/public/interfaces/geoposition.mojom.h" #include "device/geolocation/public/interfaces/geoposition.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/device/public/interfaces/constants.mojom.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/interfaces/connector.mojom.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebRect.h" #include "third_party/WebKit/public/platform/WebRect.h"
...@@ -31,41 +28,6 @@ ...@@ -31,41 +28,6 @@
using testing::ElementsAre; using testing::ElementsAre;
namespace {
class FakeGeolocation : public device::mojom::GeolocationContext,
public device::mojom::Geolocation {
public:
explicit FakeGeolocation(device::mojom::Geoposition& position)
: binding_context_(this), binding_(this), position_(position) {}
~FakeGeolocation() override {}
void Bind(mojo::ScopedMessagePipeHandle handle) {
binding_context_.Bind(
device::mojom::GeolocationContextRequest(std::move(handle)));
}
// device::mojom::Geolocation implementation:
void QueryNextPosition(QueryNextPositionCallback callback) override {
std::move(callback).Run(position_.Clone());
}
void SetHighAccuracy(bool high_accuracy) override {}
// device::mojom::GeolocationContext implementation:
void BindGeolocation(device::mojom::GeolocationRequest request) override {
binding_.Bind(std::move(request));
}
void SetOverride(device::mojom::GeopositionPtr geoposition) override {}
void ClearOverride() override {}
private:
mojo::Binding<device::mojom::GeolocationContext> binding_context_;
mojo::Binding<device::mojom::Geolocation> binding_;
device::mojom::Geoposition position_;
};
} // namespace
namespace autofill { namespace autofill {
namespace risk { namespace risk {
...@@ -123,15 +85,9 @@ class AutofillRiskFingerprintTest : public content::ContentBrowserTest { ...@@ -123,15 +85,9 @@ class AutofillRiskFingerprintTest : public content::ContentBrowserTest {
position.accuracy = kAccuracy; position.accuracy = kAccuracy;
position.timestamp = base::Time::UnixEpoch() + position.timestamp = base::Time::UnixEpoch() +
base::TimeDelta::FromMilliseconds(kGeolocationTime); base::TimeDelta::FromMilliseconds(kGeolocationTime);
fake_geolocation_ = std::make_unique<FakeGeolocation>(position);
service_manager::mojom::ConnectorRequest request; geolocation_overrider_ =
connector_ = service_manager::Connector::Create(&request); std::make_unique<device::ScopedGeolocationOverrider>(position);
service_manager::Connector::TestApi test_api(connector_.get());
test_api.OverrideBinderForTesting(
device::mojom::kServiceName, device::mojom::GeolocationContext::Name_,
base::Bind(&FakeGeolocation::Bind,
base::Unretained(fake_geolocation_.get())));
} }
void GetFingerprintTestCallback(base::OnceClosure continuation_callback, void GetFingerprintTestCallback(base::OnceClosure continuation_callback,
...@@ -242,8 +198,7 @@ class AutofillRiskFingerprintTest : public content::ContentBrowserTest { ...@@ -242,8 +198,7 @@ class AutofillRiskFingerprintTest : public content::ContentBrowserTest {
const gfx::Rect available_screen_bounds_; const gfx::Rect available_screen_bounds_;
const gfx::Rect unavailable_screen_bounds_; const gfx::Rect unavailable_screen_bounds_;
std::unique_ptr<service_manager::Connector> connector_; std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
std::unique_ptr<FakeGeolocation> fake_geolocation_;
// A message loop to block on the asynchronous loading of the fingerprint. // A message loop to block on the asynchronous loading of the fingerprint.
base::MessageLoopForUI message_loop_; base::MessageLoopForUI message_loop_;
...@@ -264,7 +219,7 @@ IN_PROC_BROWSER_TEST_F(AutofillRiskFingerprintTest, GetFingerprint) { ...@@ -264,7 +219,7 @@ IN_PROC_BROWSER_TEST_F(AutofillRiskFingerprintTest, GetFingerprint) {
base::TimeDelta::FromDays(1), // Ought to be longer than any test run. base::TimeDelta::FromDays(1), // Ought to be longer than any test run.
base::Bind(&AutofillRiskFingerprintTest::GetFingerprintTestCallback, base::Bind(&AutofillRiskFingerprintTest::GetFingerprintTestCallback,
base::Unretained(this), run_loop.QuitWhenIdleClosure()), base::Unretained(this), run_loop.QuitWhenIdleClosure()),
connector_.get()); content::ServiceManagerConnection::GetForProcess()->GetConnector());
// Wait for the callback to be called. // Wait for the callback to be called.
run_loop.Run(); run_loop.Run();
......
...@@ -251,7 +251,7 @@ static_library("content_shell_lib") { ...@@ -251,7 +251,7 @@ static_library("content_shell_lib") {
"//device/bluetooth", "//device/bluetooth",
"//device/bluetooth:fake_bluetooth", "//device/bluetooth:fake_bluetooth",
"//device/bluetooth:mocks", "//device/bluetooth:mocks",
"//device/geolocation", "//device/geolocation/public/cpp:test_support",
"//device/geolocation/public/interfaces", "//device/geolocation/public/interfaces",
"//device/sensors/public/cpp:full", "//device/sensors/public/cpp:full",
"//gin", "//gin",
......
...@@ -2,7 +2,7 @@ include_rules = [ ...@@ -2,7 +2,7 @@ include_rules = [
"-content/shell/browser/layout_test", "-content/shell/browser/layout_test",
"+components/keyed_service/content", "+components/keyed_service/content",
"+components/network_session_configurator/common", "+components/network_session_configurator/common",
"+device/geolocation", "+device/geolocation/public",
"+services/service_manager/public/cpp", "+services/service_manager/public/cpp",
"+services/service_manager/sandbox", "+services/service_manager/sandbox",
] ]
......
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
#include "content/shell/browser/layout_test/layout_test_url_request_context_getter.h" #include "content/shell/browser/layout_test/layout_test_url_request_context_getter.h"
#include "content/shell/browser/shell_url_request_context_getter.h" #include "content/shell/browser/shell_url_request_context_getter.h"
#include "content/test/mock_background_sync_controller.h" #include "content/test/mock_background_sync_controller.h"
#include "device/geolocation/geolocation_provider.h" #include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
#include "device/geolocation/public/interfaces/geoposition.mojom.h"
#if defined(OS_WIN) #if defined(OS_WIN)
#include "base/base_paths_win.h" #include "base/base_paths_win.h"
...@@ -38,23 +37,12 @@ namespace content { ...@@ -38,23 +37,12 @@ namespace content {
LayoutTestBrowserContext::LayoutTestBrowserContext(bool off_the_record, LayoutTestBrowserContext::LayoutTestBrowserContext(bool off_the_record,
net::NetLog* net_log) net::NetLog* net_log)
: ShellBrowserContext(off_the_record, net_log) { : ShellBrowserContext(off_the_record, net_log) {
Init(); // Overrides geolocation coordinates for testing.
geolocation_overrider_ =
std::make_unique<device::ScopedGeolocationOverrider>(0, 0);
} }
LayoutTestBrowserContext::~LayoutTestBrowserContext() { LayoutTestBrowserContext::~LayoutTestBrowserContext() {}
}
void LayoutTestBrowserContext::Init() {
// Fake geolocation coordinates for testing.
device::mojom::Geoposition position;
position.latitude = 0;
position.longitude = 0;
position.altitude = 0;
position.accuracy = 0;
position.timestamp = base::Time::Now();
device::GeolocationProvider::GetInstance()->OverrideLocationForTesting(
position);
}
ShellURLRequestContextGetter* ShellURLRequestContextGetter*
LayoutTestBrowserContext::CreateURLRequestContextGetter( LayoutTestBrowserContext::CreateURLRequestContextGetter(
......
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
#include "base/macros.h" #include "base/macros.h"
#include "content/shell/browser/shell_browser_context.h" #include "content/shell/browser/shell_browser_context.h"
namespace device {
class ScopedGeolocationOverrider;
}
namespace net { namespace net {
class NetLog; class NetLog;
} }
...@@ -27,8 +31,6 @@ class LayoutTestBrowserContext : public ShellBrowserContext { ...@@ -27,8 +31,6 @@ class LayoutTestBrowserContext : public ShellBrowserContext {
LayoutTestBrowserContext(bool off_the_record, net::NetLog* net_log); LayoutTestBrowserContext(bool off_the_record, net::NetLog* net_log);
~LayoutTestBrowserContext() override; ~LayoutTestBrowserContext() override;
void Init();
// BrowserContext implementation. // BrowserContext implementation.
DownloadManagerDelegate* GetDownloadManagerDelegate() override; DownloadManagerDelegate* GetDownloadManagerDelegate() override;
PushMessagingService* GetPushMessagingService() override; PushMessagingService* GetPushMessagingService() override;
...@@ -46,6 +48,7 @@ class LayoutTestBrowserContext : public ShellBrowserContext { ...@@ -46,6 +48,7 @@ class LayoutTestBrowserContext : public ShellBrowserContext {
std::unique_ptr<LayoutTestPushMessagingService> push_messaging_service_; std::unique_ptr<LayoutTestPushMessagingService> push_messaging_service_;
std::unique_ptr<PermissionManager> permission_manager_; std::unique_ptr<PermissionManager> permission_manager_;
std::unique_ptr<BackgroundSyncController> background_sync_controller_; std::unique_ptr<BackgroundSyncController> background_sync_controller_;
std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_;
DISALLOW_COPY_AND_ASSIGN(LayoutTestBrowserContext); DISALLOW_COPY_AND_ASSIGN(LayoutTestBrowserContext);
}; };
......
...@@ -313,6 +313,8 @@ static_library("test_support") { ...@@ -313,6 +313,8 @@ static_library("test_support") {
"//content/utility:for_content_tests", "//content/utility:for_content_tests",
"//device/base/synchronization", "//device/base/synchronization",
"//device/geolocation", "//device/geolocation",
"//device/geolocation/public/cpp:test_support",
"//device/geolocation/public/interfaces",
"//ipc:test_support", "//ipc:test_support",
"//media", "//media",
"//media/capture", "//media/capture",
......
...@@ -9,7 +9,22 @@ source_set("cpp") { ...@@ -9,7 +9,22 @@ source_set("cpp") {
"location_provider.h", "location_provider.h",
] ]
deps = [ public_deps = [
"//device/geolocation/public/interfaces", "//device/geolocation/public/interfaces",
] ]
} }
source_set("test_support") {
testonly = true
sources = [
"scoped_geolocation_overrider.cc",
"scoped_geolocation_overrider.h",
]
deps = [
":cpp",
"//services/device/public/interfaces",
"//services/service_manager/public/cpp",
]
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "device/geolocation/public/cpp/geoposition.h"
#include "device/geolocation/public/cpp/scoped_geolocation_overrider.h"
#include "services/device/public/interfaces/constants.mojom.h"
#include "services/service_manager/public/cpp/service_context.h"
namespace device {
// This class is a fake implementation of GeolocationContext and Geolocation
// mojo interfaces for those tests which want to set an override geoposition
// value and verify their code where there are geolocation mojo calls.
class ScopedGeolocationOverrider::FakeGeolocationContext
: public mojom::GeolocationContext {
public:
explicit FakeGeolocationContext(const mojom::Geoposition& position);
~FakeGeolocationContext() override;
void UpdateLocation(const mojom::Geoposition& position);
const mojom::Geoposition& GetGeoposition() const;
void BindForOverrideService(
const std::string& interface_name,
mojo::ScopedMessagePipeHandle handle,
const service_manager::BindSourceInfo& source_info);
// mojom::GeolocationContext implementation:
void BindGeolocation(mojom::GeolocationRequest request) override;
void SetOverride(mojom::GeopositionPtr geoposition) override;
void ClearOverride() override;
private:
mojom::Geoposition position_;
mojom::GeopositionPtr override_position_;
std::vector<std::unique_ptr<FakeGeolocation>> impls_;
mojo::BindingSet<mojom::GeolocationContext> context_bindings_;
};
class ScopedGeolocationOverrider::FakeGeolocation : public mojom::Geolocation {
public:
FakeGeolocation(mojom::GeolocationRequest request,
const FakeGeolocationContext* context);
~FakeGeolocation() override;
void UpdateLocation(const mojom::Geoposition& position);
// mojom::Geolocation implementation:
void QueryNextPosition(QueryNextPositionCallback callback) override;
void SetHighAccuracy(bool high_accuracy) override;
private:
const FakeGeolocationContext* context_;
bool has_new_position_;
QueryNextPositionCallback position_callback_;
mojo::Binding<mojom::Geolocation> binding_;
};
ScopedGeolocationOverrider::ScopedGeolocationOverrider(
const mojom::Geoposition& position) {
OverrideGeolocation(position);
}
ScopedGeolocationOverrider::ScopedGeolocationOverrider(double latitude,
double longitude) {
mojom::Geoposition position;
position.latitude = latitude;
position.longitude = longitude;
position.altitude = 0.;
position.accuracy = 0.;
position.timestamp = base::Time::Now();
OverrideGeolocation(position);
}
ScopedGeolocationOverrider::~ScopedGeolocationOverrider() {
service_manager::ServiceContext::ClearGlobalBindersForTesting(
mojom::kServiceName);
}
void ScopedGeolocationOverrider::OverrideGeolocation(
const mojom::Geoposition& position) {
geolocation_context_ = std::make_unique<FakeGeolocationContext>(position);
service_manager::ServiceContext::SetGlobalBinderForTesting(
mojom::kServiceName, mojom::GeolocationContext::Name_,
base::BindRepeating(&FakeGeolocationContext::BindForOverrideService,
base::Unretained(geolocation_context_.get())));
}
void ScopedGeolocationOverrider::UpdateLocation(
const mojom::Geoposition& position) {
geolocation_context_->UpdateLocation(position);
}
void ScopedGeolocationOverrider::UpdateLocation(double latitude,
double longitude) {
mojom::Geoposition position;
position.latitude = latitude;
position.longitude = longitude;
position.altitude = 0.;
position.accuracy = 0.;
position.timestamp = base::Time::Now();
UpdateLocation(position);
}
ScopedGeolocationOverrider::FakeGeolocationContext::FakeGeolocationContext(
const mojom::Geoposition& position)
: position_(position) {
position_.valid = false;
if (ValidateGeoposition(position_))
position_.valid = true;
}
ScopedGeolocationOverrider::FakeGeolocationContext::~FakeGeolocationContext() {}
void ScopedGeolocationOverrider::FakeGeolocationContext::UpdateLocation(
const mojom::Geoposition& position) {
position_ = position;
position_.valid = false;
if (ValidateGeoposition(position_))
position_.valid = true;
for (auto& impl : impls_) {
impl->UpdateLocation(position_);
}
}
const mojom::Geoposition&
ScopedGeolocationOverrider::FakeGeolocationContext::GetGeoposition() const {
if (!override_position_.is_null())
return *override_position_;
return position_;
}
void ScopedGeolocationOverrider::FakeGeolocationContext::BindForOverrideService(
const std::string& interface_name,
mojo::ScopedMessagePipeHandle handle,
const service_manager::BindSourceInfo& source_info) {
context_bindings_.AddBinding(
this, mojom::GeolocationContextRequest(std::move(handle)));
}
void ScopedGeolocationOverrider::FakeGeolocationContext::BindGeolocation(
mojom::GeolocationRequest request) {
impls_.push_back(std::make_unique<FakeGeolocation>(std::move(request), this));
}
void ScopedGeolocationOverrider::FakeGeolocationContext::SetOverride(
mojom::GeopositionPtr geoposition) {
override_position_ = std::move(geoposition);
if (override_position_.is_null())
return;
override_position_->valid = false;
if (ValidateGeoposition(*override_position_))
override_position_->valid = true;
for (auto& impl : impls_) {
impl->UpdateLocation(*override_position_);
}
}
void ScopedGeolocationOverrider::FakeGeolocationContext::ClearOverride() {
override_position_.reset();
}
ScopedGeolocationOverrider::FakeGeolocation::FakeGeolocation(
mojom::GeolocationRequest request,
const FakeGeolocationContext* context)
: context_(context), has_new_position_(true), binding_(this) {
binding_.Bind(std::move(request));
}
ScopedGeolocationOverrider::FakeGeolocation::~FakeGeolocation() {}
void ScopedGeolocationOverrider::FakeGeolocation::UpdateLocation(
const mojom::Geoposition& position) {
has_new_position_ = true;
if (!position_callback_.is_null()) {
std::move(position_callback_).Run(position.Clone());
has_new_position_ = false;
}
}
void ScopedGeolocationOverrider::FakeGeolocation::QueryNextPosition(
QueryNextPositionCallback callback) {
// Pending callbacks might be overrided.
position_callback_ = std::move(callback);
if (has_new_position_) {
std::move(position_callback_).Run(context_->GetGeoposition().Clone());
has_new_position_ = false;
}
}
void ScopedGeolocationOverrider::FakeGeolocation::SetHighAccuracy(
bool high_accuracy) {}
} // namespace device
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_GEOLOCATION_PUBLIC_CPP_SCOPED_GEOLOCATION_OVERRIDER_H_
#define DEVICE_GEOLOCATION_PUBLIC_CPP_SCOPED_GEOLOCATION_OVERRIDER_H_
#include "base/bind.h"
#include "device/geolocation/public/interfaces/geolocation.mojom.h"
#include "device/geolocation/public/interfaces/geolocation_context.mojom.h"
#include "device/geolocation/public/interfaces/geoposition.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/service_manager/public/cpp/bind_source_info.h"
namespace device {
// A helper class which owns a FakeGeolocationContext by which the geolocation
// is overriden to a given position or latitude and longitude values.
// The FakeGeolocationContext overrides the binder of Device Service by
// service_manager::ServiceContext::SetGlobalBinderForTesting().
// The override of the geolocation implementation will be in effect for the
// duration of this object's lifetime.
class ScopedGeolocationOverrider {
public:
explicit ScopedGeolocationOverrider(const mojom::Geoposition& position);
ScopedGeolocationOverrider(double latitude, double longitude);
~ScopedGeolocationOverrider();
void OverrideGeolocation(const mojom::Geoposition& position);
void UpdateLocation(const mojom::Geoposition& position);
void UpdateLocation(double latitude, double longitude);
private:
class FakeGeolocation;
class FakeGeolocationContext;
std::unique_ptr<FakeGeolocationContext> geolocation_context_;
};
} // namespace device
#endif // DEVICE_GEOLOCATION_PUBLIC_CPP_SCOPED_GEOLOCATION_OVERRIDER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment