Make sensors share memory in DeviceOrientation and DeviceMotion tests

Currently in the DeviceOrientation and DeviceMotion unit tests and
browser tests, each fake sensor creates a new shared memory handle.
While in the production code, only one shared memory handle is created
and all sensors share it, and write data to that shared memory using
different offset. This CL updates the FakeSensor and FakeSensorProvider
to match the behavior of production code.

Bug: 824591
Change-Id: I545d73b0d864a3b7093a387d007cab26fb912d91
Reviewed-on: https://chromium-review.googlesource.com/978856
Commit-Queue: Jun Cai <juncai@chromium.org>
Reviewed-by: Tim Volodine <timvolodine@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547848}
diff --git a/content/browser/device_sensors/device_sensor_browsertest.cc b/content/browser/device_sensors/device_sensor_browsertest.cc
index 9fe346b5..f4dc655 100644
--- a/content/browser/device_sensors/device_sensor_browsertest.cc
+++ b/content/browser/device_sensors/device_sensor_browsertest.cc
@@ -32,6 +32,7 @@
 #include "net/dns/mock_host_resolver.h"
 #include "services/device/public/cpp/generic_sensor/platform_sensor_configuration.h"
 #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
+#include "services/device/public/cpp/test/fake_sensor_and_provider.h"
 #include "services/device/public/mojom/constants.mojom.h"
 #include "services/device/public/mojom/sensor.mojom.h"
 #include "services/device/public/mojom/sensor_provider.mojom.h"
@@ -41,232 +42,7 @@
 
 namespace {
 
-class FakeSensor : public device::mojom::Sensor {
- public:
-  FakeSensor(device::mojom::SensorType sensor_type)
-      : sensor_type_(sensor_type) {
-    shared_buffer_handle_ = mojo::SharedBufferHandle::Create(
-        sizeof(device::SensorReadingSharedBuffer) *
-        static_cast<uint64_t>(device::mojom::SensorType::LAST));
-
-    if (!shared_buffer_handle_.is_valid())
-      return;
-
-    // Create read/write mapping now, to ensure it is kept writable
-    // after the region is sealed read-only on Android.
-    shared_buffer_mapping_ = shared_buffer_handle_->MapAtOffset(
-        device::mojom::SensorInitParams::kReadBufferSizeForTests,
-        GetBufferOffset());
-  }
-
-  ~FakeSensor() override = default;
-
-  // device::mojom::Sensor:
-  void AddConfiguration(
-      const device::PlatformSensorConfiguration& configuration,
-      AddConfigurationCallback callback) override {
-    std::move(callback).Run(true);
-    SensorReadingChanged();
-  }
-
-  // device::mojom::Sensor:
-  void GetDefaultConfiguration(
-      GetDefaultConfigurationCallback callback) override {
-    std::move(callback).Run(GetDefaultConfiguration());
-  }
-
-  // device::mojom::Sensor:
-  void RemoveConfiguration(
-      const device::PlatformSensorConfiguration& configuration) override {}
-
-  // device::mojom::Sensor:
-  void Suspend() override {}
-  void Resume() override {}
-  void ConfigureReadingChangeNotifications(bool enabled) override {
-    reading_notification_enabled_ = enabled;
-  }
-
-  device::PlatformSensorConfiguration GetDefaultConfiguration() {
-    return device::PlatformSensorConfiguration(60 /* frequency */);
-  }
-
-  device::mojom::ReportingMode GetReportingMode() {
-    return device::mojom::ReportingMode::ON_CHANGE;
-  }
-
-  double GetMaximumSupportedFrequency() { return 60.0; }
-  double GetMinimumSupportedFrequency() { return 1.0; }
-
-  device::mojom::SensorClientRequest GetClient() {
-    return mojo::MakeRequest(&client_);
-  }
-
-  mojo::ScopedSharedBufferHandle GetSharedBufferHandle() {
-    return shared_buffer_handle_->Clone(
-        mojo::SharedBufferHandle::AccessMode::READ_ONLY);
-  }
-
-  uint64_t GetBufferOffset() {
-    return device::SensorReadingSharedBuffer::GetOffset(sensor_type_);
-  }
-
-  void set_reading(device::SensorReading reading) { reading_ = reading; }
-
-  void SensorReadingChanged() {
-    if (!shared_buffer_mapping_.get())
-      return;
-
-    device::SensorReadingSharedBuffer* buffer =
-        static_cast<device::SensorReadingSharedBuffer*>(
-            shared_buffer_mapping_.get());
-
-    auto& seqlock = buffer->seqlock.value();
-    seqlock.WriteBegin();
-    buffer->reading = reading_;
-    seqlock.WriteEnd();
-
-    if (client_ && reading_notification_enabled_)
-      client_->SensorReadingChanged();
-  }
-
- private:
-  device::mojom::SensorType sensor_type_;
-  bool reading_notification_enabled_ = true;
-  mojo::ScopedSharedBufferHandle shared_buffer_handle_;
-  mojo::ScopedSharedBufferMapping shared_buffer_mapping_;
-  device::mojom::SensorClientPtr client_;
-  device::SensorReading reading_;
-
-  DISALLOW_COPY_AND_ASSIGN(FakeSensor);
-};
-
-class FakeSensorProvider : public device::mojom::SensorProvider {
- public:
-  FakeSensorProvider() : binding_(this) {}
-  ~FakeSensorProvider() override = default;
-
-  void Bind(const std::string& interface_name,
-            mojo::ScopedMessagePipeHandle handle,
-            const service_manager::BindSourceInfo& source_info) {
-    DCHECK(!binding_.is_bound());
-    binding_.Bind(device::mojom::SensorProviderRequest(std::move(handle)));
-  }
-
-  void set_accelerometer_is_available(bool accelerometer_is_available) {
-    accelerometer_is_available_ = accelerometer_is_available;
-  }
-
-  void set_linear_acceleration_sensor_is_available(
-      bool linear_acceleration_sensor_is_available) {
-    linear_acceleration_sensor_is_available_ =
-        linear_acceleration_sensor_is_available;
-  }
-
-  void set_gyroscope_is_available(bool gyroscope_is_available) {
-    gyroscope_is_available_ = gyroscope_is_available;
-  }
-
-  void set_relative_orientation_sensor_is_available(
-      bool relative_orientation_sensor_is_available) {
-    relative_orientation_sensor_is_available_ =
-        relative_orientation_sensor_is_available;
-  }
-
-  void set_absolute_orientation_sensor_is_available(
-      bool absolute_orientation_sensor_is_available) {
-    absolute_orientation_sensor_is_available_ =
-        absolute_orientation_sensor_is_available;
-  }
-
-  // device::mojom::sensorProvider:
-  void GetSensor(device::mojom::SensorType type,
-                 GetSensorCallback callback) override {
-    std::unique_ptr<FakeSensor> sensor;
-    device::SensorReading reading;
-    reading.raw.timestamp =
-        (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-
-    switch (type) {
-      case device::mojom::SensorType::ACCELEROMETER:
-        if (accelerometer_is_available_) {
-          sensor = std::make_unique<FakeSensor>(
-              device::mojom::SensorType::ACCELEROMETER);
-          reading.accel.x = 4;
-          reading.accel.y = 5;
-          reading.accel.z = 6;
-        }
-        break;
-      case device::mojom::SensorType::LINEAR_ACCELERATION:
-        if (linear_acceleration_sensor_is_available_) {
-          sensor = std::make_unique<FakeSensor>(
-              device::mojom::SensorType::LINEAR_ACCELERATION);
-          reading.accel.x = 1;
-          reading.accel.y = 2;
-          reading.accel.z = 3;
-        }
-        break;
-      case device::mojom::SensorType::GYROSCOPE:
-        if (gyroscope_is_available_) {
-          sensor = std::make_unique<FakeSensor>(
-              device::mojom::SensorType::GYROSCOPE);
-          reading.gyro.x = 7;
-          reading.gyro.y = 8;
-          reading.gyro.z = 9;
-        }
-        break;
-      case device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
-        if (relative_orientation_sensor_is_available_) {
-          sensor = std::make_unique<FakeSensor>(
-              device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES);
-          reading.orientation_euler.x = 2;  // beta
-          reading.orientation_euler.y = 3;  // gamma
-          reading.orientation_euler.z = 1;  // alpha
-        }
-        break;
-      case device::mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
-        if (absolute_orientation_sensor_is_available_) {
-          sensor = std::make_unique<FakeSensor>(
-              device::mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES);
-          reading.orientation_euler.x = 5;  // beta
-          reading.orientation_euler.y = 6;  // gamma
-          reading.orientation_euler.z = 4;  // alpha
-        }
-        break;
-      default:
-        NOTIMPLEMENTED();
-    }
-
-    if (sensor) {
-      sensor->set_reading(reading);
-
-      auto init_params = device::mojom::SensorInitParams::New();
-      init_params->client_request = sensor->GetClient();
-      init_params->memory = sensor->GetSharedBufferHandle();
-      init_params->buffer_offset = sensor->GetBufferOffset();
-      init_params->default_configuration = sensor->GetDefaultConfiguration();
-      init_params->maximum_frequency = sensor->GetMaximumSupportedFrequency();
-      init_params->minimum_frequency = sensor->GetMinimumSupportedFrequency();
-
-      mojo::MakeStrongBinding(std::move(sensor),
-                              mojo::MakeRequest(&init_params->sensor));
-      std::move(callback).Run(device::mojom::SensorCreationResult::SUCCESS,
-                              std::move(init_params));
-    } else {
-      std::move(callback).Run(
-          device::mojom::SensorCreationResult::ERROR_NOT_AVAILABLE, nullptr);
-    }
-  }
-
- private:
-  mojo::Binding<device::mojom::SensorProvider> binding_;
-  bool accelerometer_is_available_ = true;
-  bool linear_acceleration_sensor_is_available_ = true;
-  bool gyroscope_is_available_ = true;
-  bool relative_orientation_sensor_is_available_ = true;
-  bool absolute_orientation_sensor_is_available_ = true;
-
-  DISALLOW_COPY_AND_ASSIGN(FakeSensorProvider);
-};
+using device::FakeSensorProvider;
 
 class DeviceSensorBrowserTest : public ContentBrowserTest {
  public:
@@ -287,6 +63,11 @@
     https_embedded_test_server_->StartAcceptingConnections();
 
     sensor_provider_ = std::make_unique<FakeSensorProvider>();
+    sensor_provider_->SetAccelerometerData(4, 5, 6);
+    sensor_provider_->SetLinearAccelerationSensorData(1, 2, 3);
+    sensor_provider_->SetGyroscopeData(7, 8, 9);
+    sensor_provider_->SetRelativeOrientationSensorData(1, 2, 3);
+    sensor_provider_->SetAbsoluteOrientationSensorData(4, 5, 6);
     BrowserThread::PostTask(
         BrowserThread::IO, FROM_HERE,
         base::BindOnce(&DeviceSensorBrowserTest::SetUpOnIOThread,
@@ -306,8 +87,7 @@
     // it.
     service_manager::ServiceContext::SetGlobalBinderForTesting(
         device::mojom::kServiceName, device::mojom::SensorProvider::Name_,
-        base::Bind(&FakeSensorProvider::Bind,
-                   base::Unretained(sensor_provider_.get())));
+        base::Bind(&DeviceSensorBrowserTest::Bind, base::Unretained(this)));
 
     io_loop_finished_event_.Signal();
   }
@@ -333,6 +113,13 @@
   std::unique_ptr<net::EmbeddedTestServer> https_embedded_test_server_;
 
  private:
+  void Bind(const std::string& interface_name,
+            mojo::ScopedMessagePipeHandle handle,
+            const service_manager::BindSourceInfo& source_info) {
+    sensor_provider_->Bind(
+        device::mojom::SensorProviderRequest(std::move(handle)));
+  }
+
   base::WaitableEvent io_loop_finished_event_;
 };
 
diff --git a/content/browser/generic_sensor/generic_sensor_browsertest.cc b/content/browser/generic_sensor/generic_sensor_browsertest.cc
index 7d2bbda..ec3dff1a5 100644
--- a/content/browser/generic_sensor/generic_sensor_browsertest.cc
+++ b/content/browser/generic_sensor/generic_sensor_browsertest.cc
@@ -28,6 +28,7 @@
 #include "services/device/public/cpp/generic_sensor/platform_sensor_configuration.h"
 #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
 #include "services/device/public/cpp/generic_sensor/sensor_traits.h"
+#include "services/device/public/cpp/test/fake_sensor_and_provider.h"
 #include "services/device/public/mojom/constants.mojom.h"
 #include "services/device/public/mojom/sensor.mojom.h"
 #include "services/device/public/mojom/sensor_provider.mojom.h"
@@ -37,138 +38,7 @@
 
 namespace {
 
-class FakeAmbientLightSensor : public device::mojom::Sensor {
- public:
-  FakeAmbientLightSensor() {
-    shared_buffer_handle_ = mojo::SharedBufferHandle::Create(
-        sizeof(device::SensorReadingSharedBuffer) *
-        static_cast<uint64_t>(device::mojom::SensorType::LAST));
-
-    if (!shared_buffer_handle_.is_valid())
-      return;
-
-    // Create read/write mapping now, to ensure it is kept writable
-    // after the region is sealed read-only on Android.
-    shared_buffer_mapping_ = shared_buffer_handle_->MapAtOffset(
-        device::mojom::SensorInitParams::kReadBufferSizeForTests,
-        GetBufferOffset());
-  }
-
-  ~FakeAmbientLightSensor() override = default;
-
-  // device::mojom::Sensor implemenation:
-  void AddConfiguration(
-      const device::PlatformSensorConfiguration& configuration,
-      AddConfigurationCallback callback) override {
-    std::move(callback).Run(true);
-    SensorReadingChanged();
-  }
-
-  void GetDefaultConfiguration(
-      GetDefaultConfigurationCallback callback) override {
-    std::move(callback).Run(GetDefaultConfiguration());
-  }
-
-  void RemoveConfiguration(
-      const device::PlatformSensorConfiguration& configuration) override {}
-
-  void Suspend() override {}
-  void Resume() override {}
-  void ConfigureReadingChangeNotifications(bool enabled) override {}
-
-  device::PlatformSensorConfiguration GetDefaultConfiguration() {
-    return device::PlatformSensorConfiguration(
-        device::SensorTraits<
-            device::mojom::SensorType::AMBIENT_LIGHT>::kDefaultFrequency);
-  }
-
-  device::mojom::ReportingMode GetReportingMode() {
-    return device::mojom::ReportingMode::ON_CHANGE;
-  }
-
-  double GetMaximumSupportedFrequency() {
-    return device::SensorTraits<
-        device::mojom::SensorType::AMBIENT_LIGHT>::kMaxAllowedFrequency;
-  }
-  double GetMinimumSupportedFrequency() { return 1.0; }
-
-  device::mojom::SensorClientRequest GetClient() {
-    return mojo::MakeRequest(&client_);
-  }
-
-  mojo::ScopedSharedBufferHandle GetSharedBufferHandle() {
-    return shared_buffer_handle_->Clone(
-        mojo::SharedBufferHandle::AccessMode::READ_ONLY);
-  }
-
-  uint64_t GetBufferOffset() {
-    return device::SensorReadingSharedBuffer::GetOffset(
-        device::mojom::SensorType::AMBIENT_LIGHT);
-  }
-
-  void SensorReadingChanged() {
-    if (!shared_buffer_mapping_.get())
-      return;
-
-    device::SensorReading reading;
-    reading.als.timestamp =
-        (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-    reading.als.value = 50;
-
-    device::SensorReadingSharedBuffer* buffer =
-        static_cast<device::SensorReadingSharedBuffer*>(
-            shared_buffer_mapping_.get());
-    auto& seqlock = buffer->seqlock.value();
-    seqlock.WriteBegin();
-    buffer->reading = reading;
-    seqlock.WriteEnd();
-
-    if (client_)
-      client_->SensorReadingChanged();
-  }
-
- private:
-  mojo::ScopedSharedBufferHandle shared_buffer_handle_;
-  mojo::ScopedSharedBufferMapping shared_buffer_mapping_;
-  device::mojom::SensorClientPtr client_;
-
-  DISALLOW_COPY_AND_ASSIGN(FakeAmbientLightSensor);
-};
-
-class FakeSensorProvider : public device::mojom::SensorProvider {
- public:
-  FakeSensorProvider() = default;
-  ~FakeSensorProvider() override = default;
-
-  // device::mojom::sensorProvider implementation.
-  void GetSensor(device::mojom::SensorType type,
-                 GetSensorCallback callback) override {
-    switch (type) {
-      case device::mojom::SensorType::AMBIENT_LIGHT: {
-        auto sensor = std::make_unique<FakeAmbientLightSensor>();
-
-        auto init_params = device::mojom::SensorInitParams::New();
-        init_params->client_request = sensor->GetClient();
-        init_params->memory = sensor->GetSharedBufferHandle();
-        init_params->buffer_offset = sensor->GetBufferOffset();
-        init_params->default_configuration = sensor->GetDefaultConfiguration();
-        init_params->maximum_frequency = sensor->GetMaximumSupportedFrequency();
-        init_params->minimum_frequency = sensor->GetMinimumSupportedFrequency();
-
-        mojo::MakeStrongBinding(std::move(sensor),
-                                mojo::MakeRequest(&init_params->sensor));
-        std::move(callback).Run(device::mojom::SensorCreationResult::SUCCESS,
-                                std::move(init_params));
-        break;
-      }
-      default:
-        NOTIMPLEMENTED();
-    }
-  }
-
- private:
-  DISALLOW_COPY_AND_ASSIGN(FakeSensorProvider);
-};
+using device::FakeSensorProvider;
 
 class GenericSensorBrowserTest : public ContentBrowserTest {
  public:
@@ -227,11 +97,12 @@
     if (!sensor_provider_available_)
       return;
 
-    if (!fake_sensor_provider_)
+    if (!fake_sensor_provider_) {
       fake_sensor_provider_ = std::make_unique<FakeSensorProvider>();
+      fake_sensor_provider_->SetAmbientLightSensorData(50);
+    }
 
-    sensor_provider_bindings_.AddBinding(
-        fake_sensor_provider_.get(),
+    fake_sensor_provider_->Bind(
         device::mojom::SensorProviderRequest(std::move(handle)));
   }
 
@@ -247,7 +118,6 @@
   base::WaitableEvent io_loop_finished_event_;
   bool sensor_provider_available_ = true;
   std::unique_ptr<FakeSensorProvider> fake_sensor_provider_;
-  mojo::BindingSet<device::mojom::SensorProvider> sensor_provider_bindings_;
 
   DISALLOW_COPY_AND_ASSIGN(GenericSensorBrowserTest);
 };
diff --git a/content/renderer/device_sensors/device_motion_event_pump_unittest.cc b/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
index 5d72c91..75d248f 100644
--- a/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
+++ b/content/renderer/device_sensors/device_motion_event_pump_unittest.cc
@@ -15,9 +15,9 @@
 #include "base/threading/thread_task_runner_handle.h"
 #include "base/time/time.h"
 #include "content/public/test/test_utils.h"
-#include "content/renderer/device_sensors/fake_sensor_and_provider.h"
 #include "mojo/public/cpp/bindings/interface_request.h"
 #include "services/device/public/cpp/generic_sensor/motion_data.h"
+#include "services/device/public/cpp/test/fake_sensor_and_provider.h"
 #include "services/device/public/mojom/sensor.mojom.h"
 #include "services/device/public/mojom/sensor_provider.mojom.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -27,6 +27,8 @@
 
 namespace content {
 
+using device::FakeSensorProvider;
+
 class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
  public:
   MockDeviceMotionListener()
@@ -216,9 +218,9 @@
 
   ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAccelerometerData(1, 2, 3);
-  sensor_provider()->SetLinearAccelerationSensorData(4, 5, 6);
-  sensor_provider()->SetGyroscopeData(7, 8, 9);
+  sensor_provider()->UpdateAccelerometerData(1, 2, 3);
+  sensor_provider()->UpdateLinearAccelerationSensorData(4, 5, 6);
+  sensor_provider()->UpdateGyroscopeData(7, 8, 9);
 
   FireEvent();
 
@@ -262,8 +264,8 @@
       DeviceMotionEventPump::SensorState::NOT_INITIALIZED);
   ExpectGyroscopeStateToBe(DeviceMotionEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAccelerometerData(1, 2, 3);
-  sensor_provider()->SetGyroscopeData(7, 8, 9);
+  sensor_provider()->UpdateAccelerometerData(1, 2, 3);
+  sensor_provider()->UpdateGyroscopeData(7, 8, 9);
 
   FireEvent();
 
@@ -302,9 +304,9 @@
 
   ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAccelerometerData(NAN, 2, 3);
-  sensor_provider()->SetLinearAccelerationSensorData(4, NAN, 6);
-  sensor_provider()->SetGyroscopeData(7, 8, NAN);
+  sensor_provider()->UpdateAccelerometerData(NAN, 2, 3);
+  sensor_provider()->UpdateLinearAccelerationSensorData(4, NAN, 6);
+  sensor_provider()->UpdateGyroscopeData(7, 8, NAN);
 
   FireEvent();
 
@@ -379,15 +381,15 @@
   FireEvent();
   EXPECT_FALSE(listener()->did_change_device_motion());
 
-  sensor_provider()->SetAccelerometerData(1, 2, 3);
+  sensor_provider()->UpdateAccelerometerData(1, 2, 3);
   FireEvent();
   EXPECT_FALSE(listener()->did_change_device_motion());
 
-  sensor_provider()->SetLinearAccelerationSensorData(4, 5, 6);
+  sensor_provider()->UpdateLinearAccelerationSensorData(4, 5, 6);
   FireEvent();
   EXPECT_FALSE(listener()->did_change_device_motion());
 
-  sensor_provider()->SetGyroscopeData(7, 8, 9);
+  sensor_provider()->UpdateGyroscopeData(7, 8, 9);
   FireEvent();
   // Event is fired only after all the available sensors have data.
   EXPECT_TRUE(listener()->did_change_device_motion());
@@ -410,9 +412,9 @@
 
   ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAccelerometerData(1, 2, 3);
-  sensor_provider()->SetLinearAccelerationSensorData(4, 5, 6);
-  sensor_provider()->SetGyroscopeData(7, 8, 9);
+  sensor_provider()->UpdateAccelerometerData(1, 2, 3);
+  sensor_provider()->UpdateLinearAccelerationSensorData(4, 5, 6);
+  sensor_provider()->UpdateGyroscopeData(7, 8, 9);
 
   blink::scheduler::GetSingleThreadTaskRunnerForTesting()->PostDelayedTask(
       FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
diff --git a/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc b/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc
index 836d5f1..2d58a77 100644
--- a/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc
+++ b/content/renderer/device_sensors/device_orientation_event_pump_unittest.cc
@@ -12,9 +12,9 @@
 #include "base/message_loop/message_loop.h"
 #include "base/run_loop.h"
 #include "content/public/test/test_utils.h"
-#include "content/renderer/device_sensors/fake_sensor_and_provider.h"
 #include "mojo/public/cpp/bindings/interface_request.h"
 #include "services/device/public/cpp/generic_sensor/orientation_data.h"
+#include "services/device/public/cpp/test/fake_sensor_and_provider.h"
 #include "services/device/public/mojom/sensor.mojom.h"
 #include "services/device/public/mojom/sensor_provider.mojom.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -29,6 +29,8 @@
 
 namespace content {
 
+using device::FakeSensorProvider;
+
 class MockDeviceOrientationListener
     : public blink::WebDeviceOrientationListener {
  public:
@@ -344,7 +346,7 @@
   ExpectRelativeOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetRelativeOrientationSensorData(
+  sensor_provider()->UpdateRelativeOrientationSensorData(
       1 /* alpha */, 2 /* beta */, 3 /* gamma */);
 
   FireEvent();
@@ -379,7 +381,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, 5 /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -415,7 +417,7 @@
   ExpectRelativeOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetRelativeOrientationSensorData(
+  sensor_provider()->UpdateRelativeOrientationSensorData(
       NAN /* alpha */, 2 /* beta */, 3 /* gamma */);
 
   FireEvent();
@@ -448,7 +450,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, NAN /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -556,7 +558,7 @@
   ExpectRelativeOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetRelativeOrientationSensorData(
+  sensor_provider()->UpdateRelativeOrientationSensorData(
       1 /* alpha */, 2 /* beta */, 3 /* gamma */);
 
   FireEvent();
@@ -576,7 +578,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetRelativeOrientationSensorData(
+  sensor_provider()->UpdateRelativeOrientationSensorData(
       1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0 /* alpha */,
       2 /* beta */, 3 /* gamma */);
 
@@ -595,7 +597,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetRelativeOrientationSensorData(
+  sensor_provider()->UpdateRelativeOrientationSensorData(
       1 + DeviceOrientationEventPump::kOrientationThreshold /* alpha */,
       2 /* beta */, 3 /* gamma */);
 
@@ -631,7 +633,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, 5 /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -654,7 +656,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */,
       5 + DeviceOrientationEventPump::kOrientationThreshold / 2.0 /* beta */,
       6 /* gamma */);
@@ -674,7 +676,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */,
       5 + DeviceOrientationEventPump::kOrientationThreshold +
           kEpsilon /* beta */,
@@ -845,7 +847,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, 5 /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -875,7 +877,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, NAN /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -948,7 +950,7 @@
   ExpectAbsoluteOrientationSensorStateToBe(
       DeviceOrientationEventPump::SensorState::ACTIVE);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */, 5 /* beta */, 6 /* gamma */);
 
   FireEvent();
@@ -966,7 +968,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */,
       5 + DeviceOrientationEventPump::kOrientationThreshold / 2.0 /* beta */,
       6 /* gamma */);
@@ -986,7 +988,7 @@
 
   listener()->set_did_change_device_orientation(false);
 
-  sensor_provider()->SetAbsoluteOrientationSensorData(
+  sensor_provider()->UpdateAbsoluteOrientationSensorData(
       4 /* alpha */,
       5 + DeviceOrientationEventPump::kOrientationThreshold +
           kEpsilon /* beta */,
diff --git a/content/renderer/device_sensors/fake_sensor_and_provider.cc b/content/renderer/device_sensors/fake_sensor_and_provider.cc
deleted file mode 100644
index 84e29b5..0000000
--- a/content/renderer/device_sensors/fake_sensor_and_provider.cc
+++ /dev/null
@@ -1,241 +0,0 @@
-// Copyright 2018 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 "content/renderer/device_sensors/fake_sensor_and_provider.h"
-
-#include <memory>
-#include <utility>
-
-#include "base/logging.h"
-#include "base/time/time.h"
-#include "mojo/public/cpp/bindings/interface_request.h"
-#include "mojo/public/cpp/bindings/strong_binding.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace content {
-
-FakeSensor::FakeSensor(device::mojom::SensorType sensor_type)
-    : sensor_type_(sensor_type) {
-  shared_buffer_handle_ = mojo::SharedBufferHandle::Create(
-      sizeof(device::SensorReadingSharedBuffer) *
-      static_cast<uint64_t>(device::mojom::SensorType::LAST));
-
-  if (!shared_buffer_handle_.is_valid())
-    return;
-
-  // Create read/write mapping now, to ensure it is kept writable
-  // after the region is sealed read-only on Android.
-  shared_buffer_mapping_ = shared_buffer_handle_->MapAtOffset(
-      device::mojom::SensorInitParams::kReadBufferSizeForTests,
-      GetBufferOffset());
-}
-
-FakeSensor::~FakeSensor() = default;
-
-void FakeSensor::AddConfiguration(
-    const device::PlatformSensorConfiguration& configuration,
-    AddConfigurationCallback callback) {
-  std::move(callback).Run(true);
-}
-
-void FakeSensor::GetDefaultConfiguration(
-    GetDefaultConfigurationCallback callback) {
-  std::move(callback).Run(GetDefaultConfiguration());
-}
-
-void FakeSensor::RemoveConfiguration(
-    const device::PlatformSensorConfiguration& configuration) {}
-
-void FakeSensor::Suspend() {}
-
-void FakeSensor::Resume() {}
-
-void FakeSensor::ConfigureReadingChangeNotifications(bool enabled) {
-  reading_notification_enabled_ = enabled;
-}
-
-device::PlatformSensorConfiguration FakeSensor::GetDefaultConfiguration() {
-  return device::PlatformSensorConfiguration(60 /* frequency */);
-}
-
-device::mojom::ReportingMode FakeSensor::GetReportingMode() {
-  return device::mojom::ReportingMode::ON_CHANGE;
-}
-
-double FakeSensor::GetMaximumSupportedFrequency() {
-  return 60.0;
-}
-
-double FakeSensor::GetMinimumSupportedFrequency() {
-  return 1.0;
-}
-
-device::mojom::SensorClientRequest FakeSensor::GetClient() {
-  return mojo::MakeRequest(&client_);
-}
-
-mojo::ScopedSharedBufferHandle FakeSensor::GetSharedBufferHandle() {
-  return shared_buffer_handle_->Clone(
-      mojo::SharedBufferHandle::AccessMode::READ_ONLY);
-}
-
-uint64_t FakeSensor::GetBufferOffset() {
-  return device::SensorReadingSharedBuffer::GetOffset(sensor_type_);
-}
-
-void FakeSensor::SetReading(device::SensorReading reading) {
-  reading_ = reading;
-  SensorReadingChanged();
-}
-
-void FakeSensor::SensorReadingChanged() {
-  if (!shared_buffer_mapping_.get())
-    return;
-
-  auto* buffer = static_cast<device::SensorReadingSharedBuffer*>(
-      shared_buffer_mapping_.get());
-
-  auto& seqlock = buffer->seqlock.value();
-  seqlock.WriteBegin();
-  buffer->reading = reading_;
-  seqlock.WriteEnd();
-
-  if (client_ && reading_notification_enabled_)
-    client_->SensorReadingChanged();
-}
-
-FakeSensorProvider::FakeSensorProvider() : binding_(this) {}
-
-FakeSensorProvider::~FakeSensorProvider() = default;
-
-void FakeSensorProvider::GetSensor(device::mojom::SensorType type,
-                                   GetSensorCallback callback) {
-  std::unique_ptr<FakeSensor> sensor;
-
-  switch (type) {
-    case device::mojom::SensorType::ACCELEROMETER:
-      if (accelerometer_is_available_) {
-        sensor = std::make_unique<FakeSensor>(
-            device::mojom::SensorType::ACCELEROMETER);
-        accelerometer_ = sensor.get();
-      }
-      break;
-    case device::mojom::SensorType::LINEAR_ACCELERATION:
-      if (linear_acceleration_sensor_is_available_) {
-        sensor = std::make_unique<FakeSensor>(
-            device::mojom::SensorType::LINEAR_ACCELERATION);
-        linear_acceleration_sensor_ = sensor.get();
-      }
-      break;
-    case device::mojom::SensorType::GYROSCOPE:
-      if (gyroscope_is_available_) {
-        sensor =
-            std::make_unique<FakeSensor>(device::mojom::SensorType::GYROSCOPE);
-        gyroscope_ = sensor.get();
-      }
-      break;
-    case device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
-      if (relative_orientation_sensor_is_available_) {
-        sensor = std::make_unique<FakeSensor>(
-            device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES);
-        relative_orientation_sensor_ = sensor.get();
-      }
-      break;
-    case device::mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
-      if (absolute_orientation_sensor_is_available_) {
-        sensor = std::make_unique<FakeSensor>(
-            device::mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES);
-        absolute_orientation_sensor_ = sensor.get();
-      }
-      break;
-    default:
-      NOTIMPLEMENTED();
-  }
-
-  if (sensor) {
-    auto init_params = device::mojom::SensorInitParams::New();
-    init_params->client_request = sensor->GetClient();
-    init_params->memory = sensor->GetSharedBufferHandle();
-    init_params->buffer_offset = sensor->GetBufferOffset();
-    init_params->default_configuration = sensor->GetDefaultConfiguration();
-    init_params->maximum_frequency = sensor->GetMaximumSupportedFrequency();
-    init_params->minimum_frequency = sensor->GetMinimumSupportedFrequency();
-
-    mojo::MakeStrongBinding(std::move(sensor),
-                            mojo::MakeRequest(&init_params->sensor));
-    std::move(callback).Run(device::mojom::SensorCreationResult::SUCCESS,
-                            std::move(init_params));
-  } else {
-    std::move(callback).Run(
-        device::mojom::SensorCreationResult::ERROR_NOT_AVAILABLE, nullptr);
-  }
-}
-
-void FakeSensorProvider::Bind(device::mojom::SensorProviderRequest request) {
-  DCHECK(!binding_.is_bound());
-  binding_.Bind(std::move(request));
-}
-
-void FakeSensorProvider::SetAccelerometerData(double x, double y, double z) {
-  device::SensorReading reading;
-  reading.raw.timestamp =
-      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-  reading.accel.x = x;
-  reading.accel.y = y;
-  reading.accel.z = z;
-  EXPECT_TRUE(accelerometer_);
-  accelerometer_->SetReading(reading);
-}
-
-void FakeSensorProvider::SetLinearAccelerationSensorData(double x,
-                                                         double y,
-                                                         double z) {
-  device::SensorReading reading;
-  reading.raw.timestamp =
-      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-  reading.accel.x = x;
-  reading.accel.y = y;
-  reading.accel.z = z;
-  EXPECT_TRUE(linear_acceleration_sensor_);
-  linear_acceleration_sensor_->SetReading(reading);
-}
-
-void FakeSensorProvider::SetGyroscopeData(double x, double y, double z) {
-  device::SensorReading reading;
-  reading.raw.timestamp =
-      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-  reading.gyro.x = x;
-  reading.gyro.y = y;
-  reading.gyro.z = z;
-  EXPECT_TRUE(gyroscope_);
-  gyroscope_->SetReading(reading);
-}
-
-void FakeSensorProvider::SetRelativeOrientationSensorData(double alpha,
-                                                          double beta,
-                                                          double gamma) {
-  device::SensorReading reading;
-  reading.raw.timestamp =
-      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-  reading.orientation_euler.x = beta;
-  reading.orientation_euler.y = gamma;
-  reading.orientation_euler.z = alpha;
-  EXPECT_TRUE(relative_orientation_sensor_);
-  relative_orientation_sensor_->SetReading(reading);
-}
-
-void FakeSensorProvider::SetAbsoluteOrientationSensorData(double alpha,
-                                                          double beta,
-                                                          double gamma) {
-  device::SensorReading reading;
-  reading.raw.timestamp =
-      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
-  reading.orientation_euler.x = beta;
-  reading.orientation_euler.y = gamma;
-  reading.orientation_euler.z = alpha;
-  EXPECT_TRUE(absolute_orientation_sensor_);
-  absolute_orientation_sensor_->SetReading(reading);
-}
-
-}  // namespace content
diff --git a/content/renderer/device_sensors/fake_sensor_and_provider.h b/content/renderer/device_sensors/fake_sensor_and_provider.h
deleted file mode 100644
index fb934e3..0000000
--- a/content/renderer/device_sensors/fake_sensor_and_provider.h
+++ /dev/null
@@ -1,136 +0,0 @@
-// Copyright 2018 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 CONTENT_RENDERER_DEVICE_SENSORS_FAKE_SENSOR_AND_PROVIDER_H_
-#define CONTENT_RENDERER_DEVICE_SENSORS_FAKE_SENSOR_AND_PROVIDER_H_
-
-#include "base/macros.h"
-#include "mojo/public/cpp/bindings/binding.h"
-#include "mojo/public/cpp/system/buffer.h"
-#include "services/device/public/cpp/generic_sensor/sensor_reading.h"
-#include "services/device/public/mojom/sensor.mojom.h"
-#include "services/device/public/mojom/sensor_provider.mojom.h"
-
-// TODO(juncai): Move this file in a new
-// //services/device/public/cpp/generic_sensor:test_support source_set and
-// share it with device_sensor_browsertest.cc and generic_sensor_browsertest.cc.
-
-namespace content {
-
-class FakeSensor : public device::mojom::Sensor {
- public:
-  FakeSensor(device::mojom::SensorType sensor_type);
-  ~FakeSensor() override;
-
-  // device::mojom::Sensor:
-  void AddConfiguration(
-      const device::PlatformSensorConfiguration& configuration,
-      AddConfigurationCallback callback) override;
-  void GetDefaultConfiguration(
-      GetDefaultConfigurationCallback callback) override;
-  void RemoveConfiguration(
-      const device::PlatformSensorConfiguration& configuration) override;
-  void Suspend() override;
-  void Resume() override;
-  void ConfigureReadingChangeNotifications(bool enabled) override;
-
-  device::PlatformSensorConfiguration GetDefaultConfiguration();
-
-  device::mojom::ReportingMode GetReportingMode();
-
-  double GetMaximumSupportedFrequency();
-
-  double GetMinimumSupportedFrequency();
-
-  device::mojom::SensorClientRequest GetClient();
-
-  mojo::ScopedSharedBufferHandle GetSharedBufferHandle();
-
-  uint64_t GetBufferOffset();
-
-  void SetReading(device::SensorReading reading);
-
- private:
-  void SensorReadingChanged();
-
-  device::mojom::SensorType sensor_type_;
-  bool reading_notification_enabled_ = true;
-  mojo::ScopedSharedBufferHandle shared_buffer_handle_;
-  mojo::ScopedSharedBufferMapping shared_buffer_mapping_;
-  device::mojom::SensorClientPtr client_;
-  device::SensorReading reading_;
-
-  DISALLOW_COPY_AND_ASSIGN(FakeSensor);
-};
-
-class FakeSensorProvider : public device::mojom::SensorProvider {
- public:
-  FakeSensorProvider();
-  ~FakeSensorProvider() override;
-
-  // device::mojom::sensorProvider:
-  void GetSensor(device::mojom::SensorType type,
-                 GetSensorCallback callback) override;
-
-  void Bind(device::mojom::SensorProviderRequest request);
-
-  void set_accelerometer_is_available(bool accelerometer_is_available) {
-    accelerometer_is_available_ = accelerometer_is_available;
-  }
-
-  void set_linear_acceleration_sensor_is_available(
-      bool linear_acceleration_sensor_is_available) {
-    linear_acceleration_sensor_is_available_ =
-        linear_acceleration_sensor_is_available;
-  }
-
-  void set_gyroscope_is_available(bool gyroscope_is_available) {
-    gyroscope_is_available_ = gyroscope_is_available;
-  }
-
-  void set_relative_orientation_sensor_is_available(
-      bool relative_orientation_sensor_is_available) {
-    relative_orientation_sensor_is_available_ =
-        relative_orientation_sensor_is_available;
-  }
-
-  void set_absolute_orientation_sensor_is_available(
-      bool absolute_orientation_sensor_is_available) {
-    absolute_orientation_sensor_is_available_ =
-        absolute_orientation_sensor_is_available;
-  }
-
-  void SetAccelerometerData(double x, double y, double z);
-
-  void SetLinearAccelerationSensorData(double x, double y, double z);
-
-  void SetGyroscopeData(double x, double y, double z);
-
-  void SetRelativeOrientationSensorData(double alpha,
-                                        double beta,
-                                        double gamma);
-
-  void SetAbsoluteOrientationSensorData(double alpha,
-                                        double beta,
-                                        double gamma);
-
- private:
-  FakeSensor* accelerometer_ = nullptr;
-  FakeSensor* linear_acceleration_sensor_ = nullptr;
-  FakeSensor* gyroscope_ = nullptr;
-  FakeSensor* relative_orientation_sensor_ = nullptr;
-  FakeSensor* absolute_orientation_sensor_ = nullptr;
-  bool accelerometer_is_available_ = true;
-  bool linear_acceleration_sensor_is_available_ = true;
-  bool gyroscope_is_available_ = true;
-  bool relative_orientation_sensor_is_available_ = true;
-  bool absolute_orientation_sensor_is_available_ = true;
-  mojo::Binding<device::mojom::SensorProvider> binding_;
-
-  DISALLOW_COPY_AND_ASSIGN(FakeSensorProvider);
-};
-
-}  // namespace content
-
-#endif  // CONTENT_RENDERER_DEVICE_SENSORS_FAKE_SENSOR_AND_PROVIDER_H_
diff --git a/content/test/BUILD.gn b/content/test/BUILD.gn
index 3cfe3421..2c8c5ea 100644
--- a/content/test/BUILD.gn
+++ b/content/test/BUILD.gn
@@ -908,6 +908,7 @@
     "//services/catalog:lib",
     "//services/device/public/cpp:device_features",
     "//services/device/public/cpp/generic_sensor",
+    "//services/device/public/cpp/test:test_support",
     "//services/device/public/mojom",
     "//services/device/public/mojom:generic_sensor",
     "//services/network:test_support",
@@ -1570,8 +1571,6 @@
     "../renderer/child_frame_compositing_helper_unittest.cc",
     "../renderer/device_sensors/device_motion_event_pump_unittest.cc",
     "../renderer/device_sensors/device_orientation_event_pump_unittest.cc",
-    "../renderer/device_sensors/fake_sensor_and_provider.cc",
-    "../renderer/device_sensors/fake_sensor_and_provider.h",
     "../renderer/dom_storage/dom_storage_cached_area_unittest.cc",
     "../renderer/dom_storage/local_storage_cached_area_unittest.cc",
     "../renderer/dom_storage/local_storage_cached_areas_unittest.cc",
@@ -1721,6 +1720,7 @@
     "//printing",
     "//services/catalog:lib",
     "//services/device/public/cpp/generic_sensor",
+    "//services/device/public/cpp/test:test_support",
     "//services/device/public/mojom",
     "//services/file:lib",
     "//services/file/public/mojom",
diff --git a/services/device/public/cpp/generic_sensor/sensor_traits.cc b/services/device/public/cpp/generic_sensor/sensor_traits.cc
index f284493..849b1dc 100644
--- a/services/device/public/cpp/generic_sensor/sensor_traits.cc
+++ b/services/device/public/cpp/generic_sensor/sensor_traits.cc
@@ -37,10 +37,44 @@
     case SensorType::RELATIVE_ORIENTATION_QUATERNION:
       return SensorTraits<
           SensorType::RELATIVE_ORIENTATION_QUATERNION>::kMaxAllowedFrequency;
-    default:
-      NOTREACHED() << "Unknown sensor type " << type;
-      return SensorTraits<SensorType::LAST>::kMaxAllowedFrequency;
+    // No default so the compiler will warn us if a new type is added.
   }
+  NOTREACHED() << "Unknown sensor type " << type;
+  return SensorTraits<SensorType::LAST>::kMaxAllowedFrequency;
+}
+
+double GetSensorDefaultFrequency(mojom::SensorType type) {
+  switch (type) {
+    case SensorType::AMBIENT_LIGHT:
+      return SensorTraits<SensorType::AMBIENT_LIGHT>::kDefaultFrequency;
+    case SensorType::PROXIMITY:
+      return SensorTraits<SensorType::PROXIMITY>::kDefaultFrequency;
+    case SensorType::ACCELEROMETER:
+      return SensorTraits<SensorType::ACCELEROMETER>::kDefaultFrequency;
+    case SensorType::LINEAR_ACCELERATION:
+      return SensorTraits<SensorType::LINEAR_ACCELERATION>::kDefaultFrequency;
+    case SensorType::GYROSCOPE:
+      return SensorTraits<SensorType::GYROSCOPE>::kDefaultFrequency;
+    case SensorType::MAGNETOMETER:
+      return SensorTraits<SensorType::MAGNETOMETER>::kDefaultFrequency;
+    case SensorType::PRESSURE:
+      return SensorTraits<SensorType::PRESSURE>::kDefaultFrequency;
+    case SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
+      return SensorTraits<
+          SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES>::kDefaultFrequency;
+    case SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
+      return SensorTraits<
+          SensorType::ABSOLUTE_ORIENTATION_QUATERNION>::kDefaultFrequency;
+    case SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
+      return SensorTraits<
+          SensorType::RELATIVE_ORIENTATION_EULER_ANGLES>::kDefaultFrequency;
+    case SensorType::RELATIVE_ORIENTATION_QUATERNION:
+      return SensorTraits<
+          SensorType::RELATIVE_ORIENTATION_QUATERNION>::kDefaultFrequency;
+    // No default so the compiler will warn us if a new type is added.
+  }
+  NOTREACHED() << "Unknown sensor type " << type;
+  return SensorTraits<SensorType::LAST>::kDefaultFrequency;
 }
 
 }  // namespace device
diff --git a/services/device/public/cpp/generic_sensor/sensor_traits.h b/services/device/public/cpp/generic_sensor/sensor_traits.h
index 8a76ee9..6d935129 100644
--- a/services/device/public/cpp/generic_sensor/sensor_traits.h
+++ b/services/device/public/cpp/generic_sensor/sensor_traits.h
@@ -30,6 +30,8 @@
 
 double GetSensorMaxAllowedFrequency(mojom::SensorType type);
 
+double GetSensorDefaultFrequency(mojom::SensorType type);
+
 }  // namespace device
 
 #endif  // SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_TRAITS_H_
diff --git a/services/device/public/cpp/test/BUILD.gn b/services/device/public/cpp/test/BUILD.gn
index 3a8db4a..a560983 100644
--- a/services/device/public/cpp/test/BUILD.gn
+++ b/services/device/public/cpp/test/BUILD.gn
@@ -3,18 +3,25 @@
 # found in the LICENSE file.
 
 source_set("test_support") {
+  testonly = true
+
   sources = [
+    "fake_sensor_and_provider.cc",
+    "fake_sensor_and_provider.h",
     "test_wake_lock_provider.cc",
     "test_wake_lock_provider.h",
   ]
 
   public_deps = [
     "//base",
+    "//services/device/public/cpp/generic_sensor",
     "//services/device/public/mojom",
     "//services/service_manager/public/cpp",
   ]
 
   deps = [
     "//mojo/public/cpp/bindings",
+    "//mojo/public/cpp/system",
+    "//testing/gtest",
   ]
 }
diff --git a/services/device/public/cpp/test/fake_sensor_and_provider.cc b/services/device/public/cpp/test/fake_sensor_and_provider.cc
new file mode 100644
index 0000000..80976ef
--- /dev/null
+++ b/services/device/public/cpp/test/fake_sensor_and_provider.cc
@@ -0,0 +1,317 @@
+// Copyright 2018 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 "services/device/public/cpp/test/fake_sensor_and_provider.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/logging.h"
+#include "base/time/time.h"
+#include "mojo/public/cpp/bindings/interface_request.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+#include "services/device/public/cpp/generic_sensor/sensor_traits.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const uint64_t kReadingBufferSize = sizeof(device::SensorReadingSharedBuffer);
+const uint64_t kSharedBufferSizeInBytes =
+    kReadingBufferSize * static_cast<uint64_t>(device::mojom::SensorType::LAST);
+
+}  // namespace
+
+namespace device {
+
+FakeSensor::FakeSensor(mojom::SensorType sensor_type,
+                       SensorReadingSharedBuffer* buffer)
+    : sensor_type_(sensor_type), buffer_(buffer) {}
+
+FakeSensor::~FakeSensor() = default;
+
+void FakeSensor::AddConfiguration(
+    const PlatformSensorConfiguration& configuration,
+    AddConfigurationCallback callback) {
+  std::move(callback).Run(true);
+  SensorReadingChanged();
+}
+
+void FakeSensor::GetDefaultConfiguration(
+    GetDefaultConfigurationCallback callback) {
+  std::move(callback).Run(GetDefaultConfiguration());
+}
+
+void FakeSensor::RemoveConfiguration(
+    const PlatformSensorConfiguration& configuration) {}
+
+void FakeSensor::Suspend() {}
+
+void FakeSensor::Resume() {}
+
+void FakeSensor::ConfigureReadingChangeNotifications(bool enabled) {
+  reading_notification_enabled_ = enabled;
+}
+
+PlatformSensorConfiguration FakeSensor::GetDefaultConfiguration() {
+  return PlatformSensorConfiguration(GetSensorDefaultFrequency(sensor_type_));
+}
+
+mojom::ReportingMode FakeSensor::GetReportingMode() {
+  return mojom::ReportingMode::ON_CHANGE;
+}
+
+double FakeSensor::GetMaximumSupportedFrequency() {
+  return GetSensorMaxAllowedFrequency(sensor_type_);
+}
+
+double FakeSensor::GetMinimumSupportedFrequency() {
+  return 1.0;
+}
+
+mojom::SensorClientRequest FakeSensor::GetClient() {
+  return mojo::MakeRequest(&client_);
+}
+
+uint64_t FakeSensor::GetBufferOffset() {
+  return SensorReadingSharedBuffer::GetOffset(sensor_type_);
+}
+
+void FakeSensor::SetReading(SensorReading reading) {
+  reading_ = reading;
+  SensorReadingChanged();
+}
+
+void FakeSensor::SensorReadingChanged() {
+  auto& seqlock = buffer_->seqlock.value();
+  seqlock.WriteBegin();
+  buffer_->reading = reading_;
+  seqlock.WriteEnd();
+
+  if (client_ && reading_notification_enabled_)
+    client_->SensorReadingChanged();
+}
+
+FakeSensorProvider::FakeSensorProvider() : binding_(this) {}
+
+FakeSensorProvider::~FakeSensorProvider() = default;
+
+void FakeSensorProvider::GetSensor(mojom::SensorType type,
+                                   GetSensorCallback callback) {
+  if (!CreateSharedBufferIfNeeded()) {
+    std::move(callback).Run(mojom::SensorCreationResult::ERROR_NOT_AVAILABLE,
+                            nullptr);
+    return;
+  }
+
+  SensorReadingSharedBuffer* buffer = GetSensorReadingSharedBufferForType(type);
+
+  std::unique_ptr<FakeSensor> sensor;
+
+  switch (type) {
+    case mojom::SensorType::AMBIENT_LIGHT:
+      if (ambient_light_sensor_is_available_) {
+        sensor = std::make_unique<FakeSensor>(mojom::SensorType::AMBIENT_LIGHT,
+                                              buffer);
+        ambient_light_sensor_ = sensor.get();
+        ambient_light_sensor_->SetReading(ambient_light_sensor_reading_);
+      }
+      break;
+    case mojom::SensorType::ACCELEROMETER:
+      if (accelerometer_is_available_) {
+        sensor = std::make_unique<FakeSensor>(mojom::SensorType::ACCELEROMETER,
+                                              buffer);
+        accelerometer_ = sensor.get();
+        accelerometer_->SetReading(accelerometer_reading_);
+      }
+      break;
+    case mojom::SensorType::LINEAR_ACCELERATION:
+      if (linear_acceleration_sensor_is_available_) {
+        sensor = std::make_unique<FakeSensor>(
+            mojom::SensorType::LINEAR_ACCELERATION, buffer);
+        linear_acceleration_sensor_ = sensor.get();
+        linear_acceleration_sensor_->SetReading(
+            linear_acceleration_sensor_reading_);
+      }
+      break;
+    case mojom::SensorType::GYROSCOPE:
+      if (gyroscope_is_available_) {
+        sensor =
+            std::make_unique<FakeSensor>(mojom::SensorType::GYROSCOPE, buffer);
+        gyroscope_ = sensor.get();
+        gyroscope_->SetReading(gyroscope_reading_);
+      }
+      break;
+    case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
+      if (relative_orientation_sensor_is_available_) {
+        sensor = std::make_unique<FakeSensor>(
+            mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES, buffer);
+        relative_orientation_sensor_ = sensor.get();
+        relative_orientation_sensor_->SetReading(
+            relative_orientation_sensor_reading_);
+      }
+      break;
+    case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
+      if (absolute_orientation_sensor_is_available_) {
+        sensor = std::make_unique<FakeSensor>(
+            mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES, buffer);
+        absolute_orientation_sensor_ = sensor.get();
+        absolute_orientation_sensor_->SetReading(
+            absolute_orientation_sensor_reading_);
+      }
+      break;
+    default:
+      NOTIMPLEMENTED();
+  }
+
+  if (sensor) {
+    auto init_params = mojom::SensorInitParams::New();
+    init_params->client_request = sensor->GetClient();
+    init_params->memory = shared_buffer_handle_->Clone(
+        mojo::SharedBufferHandle::AccessMode::READ_ONLY);
+    init_params->buffer_offset = sensor->GetBufferOffset();
+    init_params->default_configuration = sensor->GetDefaultConfiguration();
+    init_params->maximum_frequency = sensor->GetMaximumSupportedFrequency();
+    init_params->minimum_frequency = sensor->GetMinimumSupportedFrequency();
+
+    mojo::MakeStrongBinding(std::move(sensor),
+                            mojo::MakeRequest(&init_params->sensor));
+    std::move(callback).Run(mojom::SensorCreationResult::SUCCESS,
+                            std::move(init_params));
+  } else {
+    std::move(callback).Run(mojom::SensorCreationResult::ERROR_NOT_AVAILABLE,
+                            nullptr);
+  }
+}
+
+void FakeSensorProvider::Bind(mojom::SensorProviderRequest request) {
+  DCHECK(!binding_.is_bound());
+  binding_.Bind(std::move(request));
+}
+
+void FakeSensorProvider::SetAmbientLightSensorData(double value) {
+  ambient_light_sensor_reading_.als.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  ambient_light_sensor_reading_.als.value = value;
+}
+
+void FakeSensorProvider::SetAccelerometerData(double x, double y, double z) {
+  accelerometer_reading_.raw.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  accelerometer_reading_.accel.x = x;
+  accelerometer_reading_.accel.y = y;
+  accelerometer_reading_.accel.z = z;
+}
+
+void FakeSensorProvider::SetLinearAccelerationSensorData(double x,
+                                                         double y,
+                                                         double z) {
+  linear_acceleration_sensor_reading_.raw.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  linear_acceleration_sensor_reading_.accel.x = x;
+  linear_acceleration_sensor_reading_.accel.y = y;
+  linear_acceleration_sensor_reading_.accel.z = z;
+}
+
+void FakeSensorProvider::SetGyroscopeData(double x, double y, double z) {
+  gyroscope_reading_.raw.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  gyroscope_reading_.gyro.x = x;
+  gyroscope_reading_.gyro.y = y;
+  gyroscope_reading_.gyro.z = z;
+}
+
+void FakeSensorProvider::SetRelativeOrientationSensorData(double alpha,
+                                                          double beta,
+                                                          double gamma) {
+  relative_orientation_sensor_reading_.raw.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  relative_orientation_sensor_reading_.orientation_euler.x = beta;
+  relative_orientation_sensor_reading_.orientation_euler.y = gamma;
+  relative_orientation_sensor_reading_.orientation_euler.z = alpha;
+}
+
+void FakeSensorProvider::SetAbsoluteOrientationSensorData(double alpha,
+                                                          double beta,
+                                                          double gamma) {
+  absolute_orientation_sensor_reading_.raw.timestamp =
+      (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
+  absolute_orientation_sensor_reading_.orientation_euler.x = beta;
+  absolute_orientation_sensor_reading_.orientation_euler.y = gamma;
+  absolute_orientation_sensor_reading_.orientation_euler.z = alpha;
+}
+
+void FakeSensorProvider::UpdateAmbientLightSensorData(double value) {
+  SetAmbientLightSensorData(value);
+  EXPECT_TRUE(ambient_light_sensor_);
+  ambient_light_sensor_->SetReading(ambient_light_sensor_reading_);
+}
+
+void FakeSensorProvider::UpdateAccelerometerData(double x, double y, double z) {
+  SetAccelerometerData(x, y, z);
+  EXPECT_TRUE(accelerometer_);
+  accelerometer_->SetReading(accelerometer_reading_);
+}
+
+void FakeSensorProvider::UpdateLinearAccelerationSensorData(double x,
+                                                            double y,
+                                                            double z) {
+  SetLinearAccelerationSensorData(x, y, z);
+  EXPECT_TRUE(linear_acceleration_sensor_);
+  linear_acceleration_sensor_->SetReading(linear_acceleration_sensor_reading_);
+}
+
+void FakeSensorProvider::UpdateGyroscopeData(double x, double y, double z) {
+  SetGyroscopeData(x, y, z);
+  EXPECT_TRUE(gyroscope_);
+  gyroscope_->SetReading(gyroscope_reading_);
+}
+
+void FakeSensorProvider::UpdateRelativeOrientationSensorData(double alpha,
+                                                             double beta,
+                                                             double gamma) {
+  SetRelativeOrientationSensorData(alpha, beta, gamma);
+  EXPECT_TRUE(relative_orientation_sensor_);
+  relative_orientation_sensor_->SetReading(
+      relative_orientation_sensor_reading_);
+}
+
+void FakeSensorProvider::UpdateAbsoluteOrientationSensorData(double alpha,
+                                                             double beta,
+                                                             double gamma) {
+  SetAbsoluteOrientationSensorData(alpha, beta, gamma);
+  EXPECT_TRUE(absolute_orientation_sensor_);
+  absolute_orientation_sensor_->SetReading(
+      absolute_orientation_sensor_reading_);
+}
+
+bool FakeSensorProvider::CreateSharedBufferIfNeeded() {
+  if (shared_buffer_mapping_.get())
+    return true;
+
+  if (!shared_buffer_handle_.is_valid()) {
+    shared_buffer_handle_ =
+        mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
+    if (!shared_buffer_handle_.is_valid())
+      return false;
+  }
+
+  // Create read/write mapping now, to ensure it is kept writable
+  // after the region is sealed read-only on Android.
+  shared_buffer_mapping_ = shared_buffer_handle_->Map(kSharedBufferSizeInBytes);
+  return shared_buffer_mapping_.get() != nullptr;
+}
+
+SensorReadingSharedBuffer*
+FakeSensorProvider::GetSensorReadingSharedBufferForType(
+    mojom::SensorType type) {
+  auto* ptr = static_cast<char*>(shared_buffer_mapping_.get());
+  if (!ptr)
+    return nullptr;
+
+  ptr += SensorReadingSharedBuffer::GetOffset(type);
+  memset(ptr, 0, kReadingBufferSize);
+  return reinterpret_cast<SensorReadingSharedBuffer*>(ptr);
+}
+
+}  // namespace device
diff --git a/services/device/public/cpp/test/fake_sensor_and_provider.h b/services/device/public/cpp/test/fake_sensor_and_provider.h
new file mode 100644
index 0000000..c973056
--- /dev/null
+++ b/services/device/public/cpp/test/fake_sensor_and_provider.h
@@ -0,0 +1,151 @@
+// Copyright 2018 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 SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_
+#define SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_
+
+#include "base/macros.h"
+#include "mojo/public/cpp/bindings/binding.h"
+#include "mojo/public/cpp/system/buffer.h"
+#include "services/device/public/cpp/generic_sensor/sensor_reading.h"
+#include "services/device/public/mojom/sensor.mojom.h"
+#include "services/device/public/mojom/sensor_provider.mojom.h"
+
+namespace device {
+
+class FakeSensor : public mojom::Sensor {
+ public:
+  FakeSensor(mojom::SensorType sensor_type, SensorReadingSharedBuffer* buffer);
+  ~FakeSensor() override;
+
+  // mojom::Sensor:
+  void AddConfiguration(const PlatformSensorConfiguration& configuration,
+                        AddConfigurationCallback callback) override;
+  void GetDefaultConfiguration(
+      GetDefaultConfigurationCallback callback) override;
+  void RemoveConfiguration(
+      const PlatformSensorConfiguration& configuration) override;
+  void Suspend() override;
+  void Resume() override;
+  void ConfigureReadingChangeNotifications(bool enabled) override;
+
+  PlatformSensorConfiguration GetDefaultConfiguration();
+  mojom::ReportingMode GetReportingMode();
+  double GetMaximumSupportedFrequency();
+  double GetMinimumSupportedFrequency();
+  mojom::SensorClientRequest GetClient();
+  mojo::ScopedSharedBufferHandle GetSharedBufferHandle();
+  uint64_t GetBufferOffset();
+  void SetReading(SensorReading reading);
+
+ private:
+  void SensorReadingChanged();
+
+  mojom::SensorType sensor_type_;
+  SensorReadingSharedBuffer* buffer_;
+  bool reading_notification_enabled_ = true;
+  mojom::SensorClientPtr client_;
+  SensorReading reading_;
+
+  DISALLOW_COPY_AND_ASSIGN(FakeSensor);
+};
+
+class FakeSensorProvider : public mojom::SensorProvider {
+ public:
+  FakeSensorProvider();
+  ~FakeSensorProvider() override;
+
+  // mojom::sensorProvider:
+  void GetSensor(mojom::SensorType type, GetSensorCallback callback) override;
+
+  void Bind(mojom::SensorProviderRequest request);
+
+  void set_ambient_light_sensor_is_available(
+      bool ambient_light_sensor_is_available) {
+    ambient_light_sensor_is_available_ = ambient_light_sensor_is_available;
+  }
+  void set_accelerometer_is_available(bool accelerometer_is_available) {
+    accelerometer_is_available_ = accelerometer_is_available;
+  }
+  void set_linear_acceleration_sensor_is_available(
+      bool linear_acceleration_sensor_is_available) {
+    linear_acceleration_sensor_is_available_ =
+        linear_acceleration_sensor_is_available;
+  }
+  void set_gyroscope_is_available(bool gyroscope_is_available) {
+    gyroscope_is_available_ = gyroscope_is_available;
+  }
+  void set_relative_orientation_sensor_is_available(
+      bool relative_orientation_sensor_is_available) {
+    relative_orientation_sensor_is_available_ =
+        relative_orientation_sensor_is_available;
+  }
+  void set_absolute_orientation_sensor_is_available(
+      bool absolute_orientation_sensor_is_available) {
+    absolute_orientation_sensor_is_available_ =
+        absolute_orientation_sensor_is_available;
+  }
+
+  void SetAmbientLightSensorData(double value);
+  void SetAccelerometerData(double x, double y, double z);
+  void SetLinearAccelerationSensorData(double x, double y, double z);
+  void SetGyroscopeData(double x, double y, double z);
+  void SetRelativeOrientationSensorData(double alpha,
+                                        double beta,
+                                        double gamma);
+  void SetAbsoluteOrientationSensorData(double alpha,
+                                        double beta,
+                                        double gamma);
+
+  // The Update* functions here write the sensor data to the shared memory and
+  // notify sensor's client that the sensor data has changed. The Set*
+  // functions above only set |*_reading_| member variable for corresponding
+  // sensor which will be the value when the sensor is first created.
+  void UpdateAmbientLightSensorData(double value);
+  void UpdateAccelerometerData(double x, double y, double z);
+  void UpdateLinearAccelerationSensorData(double x, double y, double z);
+  void UpdateGyroscopeData(double x, double y, double z);
+  void UpdateRelativeOrientationSensorData(double alpha,
+                                           double beta,
+                                           double gamma);
+  void UpdateAbsoluteOrientationSensorData(double alpha,
+                                           double beta,
+                                           double gamma);
+
+ private:
+  bool CreateSharedBufferIfNeeded();
+  SensorReadingSharedBuffer* GetSensorReadingSharedBufferForType(
+      mojom::SensorType type);
+
+  // The following sensor pointers are owned by the caller of
+  // FakeSensorProvider::GetSensor().
+  FakeSensor* ambient_light_sensor_ = nullptr;
+  FakeSensor* accelerometer_ = nullptr;
+  FakeSensor* linear_acceleration_sensor_ = nullptr;
+  FakeSensor* gyroscope_ = nullptr;
+  FakeSensor* relative_orientation_sensor_ = nullptr;
+  FakeSensor* absolute_orientation_sensor_ = nullptr;
+
+  SensorReading ambient_light_sensor_reading_;
+  SensorReading accelerometer_reading_;
+  SensorReading linear_acceleration_sensor_reading_;
+  SensorReading gyroscope_reading_;
+  SensorReading relative_orientation_sensor_reading_;
+  SensorReading absolute_orientation_sensor_reading_;
+  bool ambient_light_sensor_is_available_ = true;
+  bool accelerometer_is_available_ = true;
+  bool linear_acceleration_sensor_is_available_ = true;
+  bool gyroscope_is_available_ = true;
+  bool relative_orientation_sensor_is_available_ = true;
+  bool absolute_orientation_sensor_is_available_ = true;
+  mojo::Binding<mojom::SensorProvider> binding_;
+  mojo::ScopedSharedBufferHandle shared_buffer_handle_;
+  mojo::ScopedSharedBufferMapping shared_buffer_mapping_;
+
+  DISALLOW_COPY_AND_ASSIGN(FakeSensorProvider);
+};
+
+}  // namespace device
+
+#endif  // SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_