1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_ 6 #define SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_ 7 8 #include "base/macros.h" 9 #include "mojo/public/cpp/bindings/pending_receiver.h" 10 #include "mojo/public/cpp/bindings/receiver_set.h" 11 #include "mojo/public/cpp/bindings/remote.h" 12 #include "mojo/public/cpp/system/buffer.h" 13 #include "services/device/public/cpp/generic_sensor/sensor_reading.h" 14 #include "services/device/public/mojom/sensor.mojom.h" 15 #include "services/device/public/mojom/sensor_provider.mojom.h" 16 17 namespace device { 18 19 class FakeSensor : public mojom::Sensor { 20 public: 21 FakeSensor(mojom::SensorType sensor_type, SensorReadingSharedBuffer* buffer); 22 ~FakeSensor() override; 23 24 // mojom::Sensor: 25 void AddConfiguration(const PlatformSensorConfiguration& configuration, 26 AddConfigurationCallback callback) override; 27 void GetDefaultConfiguration( 28 GetDefaultConfigurationCallback callback) override; 29 void RemoveConfiguration( 30 const PlatformSensorConfiguration& configuration) override; 31 void Suspend() override; 32 void Resume() override; 33 void ConfigureReadingChangeNotifications(bool enabled) override; 34 35 PlatformSensorConfiguration GetDefaultConfiguration(); 36 mojom::ReportingMode GetReportingMode(); 37 double GetMaximumSupportedFrequency(); 38 double GetMinimumSupportedFrequency(); 39 mojo::PendingReceiver<mojom::SensorClient> GetClient(); 40 mojo::ScopedSharedBufferHandle GetSharedBufferHandle(); 41 uint64_t GetBufferOffset(); 42 void SetReading(SensorReading reading); 43 44 private: 45 void SensorReadingChanged(); 46 47 mojom::SensorType sensor_type_; 48 SensorReadingSharedBuffer* buffer_; 49 bool reading_notification_enabled_ = true; 50 mojo::Remote<mojom::SensorClient> client_; 51 SensorReading reading_; 52 53 DISALLOW_COPY_AND_ASSIGN(FakeSensor); 54 }; 55 56 class FakeSensorProvider : public mojom::SensorProvider { 57 public: 58 FakeSensorProvider(); 59 ~FakeSensorProvider() override; 60 61 // mojom::sensorProvider: 62 void GetSensor(mojom::SensorType type, GetSensorCallback callback) override; 63 64 void Bind(mojo::PendingReceiver<mojom::SensorProvider> receiver); 65 set_ambient_light_sensor_is_available(bool ambient_light_sensor_is_available)66 void set_ambient_light_sensor_is_available( 67 bool ambient_light_sensor_is_available) { 68 ambient_light_sensor_is_available_ = ambient_light_sensor_is_available; 69 } set_accelerometer_is_available(bool accelerometer_is_available)70 void set_accelerometer_is_available(bool accelerometer_is_available) { 71 accelerometer_is_available_ = accelerometer_is_available; 72 } set_linear_acceleration_sensor_is_available(bool linear_acceleration_sensor_is_available)73 void set_linear_acceleration_sensor_is_available( 74 bool linear_acceleration_sensor_is_available) { 75 linear_acceleration_sensor_is_available_ = 76 linear_acceleration_sensor_is_available; 77 } set_gyroscope_is_available(bool gyroscope_is_available)78 void set_gyroscope_is_available(bool gyroscope_is_available) { 79 gyroscope_is_available_ = gyroscope_is_available; 80 } set_relative_orientation_sensor_is_available(bool relative_orientation_sensor_is_available)81 void set_relative_orientation_sensor_is_available( 82 bool relative_orientation_sensor_is_available) { 83 relative_orientation_sensor_is_available_ = 84 relative_orientation_sensor_is_available; 85 } set_absolute_orientation_sensor_is_available(bool absolute_orientation_sensor_is_available)86 void set_absolute_orientation_sensor_is_available( 87 bool absolute_orientation_sensor_is_available) { 88 absolute_orientation_sensor_is_available_ = 89 absolute_orientation_sensor_is_available; 90 } 91 92 void SetAmbientLightSensorData(double value); 93 void SetAccelerometerData(double x, double y, double z); 94 void SetLinearAccelerationSensorData(double x, double y, double z); 95 void SetGyroscopeData(double x, double y, double z); 96 void SetRelativeOrientationSensorData(double alpha, 97 double beta, 98 double gamma); 99 void SetAbsoluteOrientationSensorData(double alpha, 100 double beta, 101 double gamma); 102 103 // The Update* functions here write the sensor data to the shared memory and 104 // notify sensor's client that the sensor data has changed. The Set* 105 // functions above only set |*_reading_| member variable for corresponding 106 // sensor which will be the value when the sensor is first created. 107 void UpdateAmbientLightSensorData(double value); 108 void UpdateAccelerometerData(double x, double y, double z); 109 void UpdateLinearAccelerationSensorData(double x, double y, double z); 110 void UpdateGyroscopeData(double x, double y, double z); 111 void UpdateRelativeOrientationSensorData(double alpha, 112 double beta, 113 double gamma); 114 void UpdateAbsoluteOrientationSensorData(double alpha, 115 double beta, 116 double gamma); 117 118 private: 119 bool CreateSharedBufferIfNeeded(); 120 SensorReadingSharedBuffer* GetSensorReadingSharedBufferForType( 121 mojom::SensorType type); 122 123 // The following sensor pointers are owned by the caller of 124 // FakeSensorProvider::GetSensor(). 125 FakeSensor* ambient_light_sensor_ = nullptr; 126 FakeSensor* accelerometer_ = nullptr; 127 FakeSensor* linear_acceleration_sensor_ = nullptr; 128 FakeSensor* gyroscope_ = nullptr; 129 FakeSensor* relative_orientation_sensor_ = nullptr; 130 FakeSensor* absolute_orientation_sensor_ = nullptr; 131 132 SensorReading ambient_light_sensor_reading_; 133 SensorReading accelerometer_reading_; 134 SensorReading linear_acceleration_sensor_reading_; 135 SensorReading gyroscope_reading_; 136 SensorReading relative_orientation_sensor_reading_; 137 SensorReading absolute_orientation_sensor_reading_; 138 bool ambient_light_sensor_is_available_ = true; 139 bool accelerometer_is_available_ = true; 140 bool linear_acceleration_sensor_is_available_ = true; 141 bool gyroscope_is_available_ = true; 142 bool relative_orientation_sensor_is_available_ = true; 143 bool absolute_orientation_sensor_is_available_ = true; 144 mojo::ReceiverSet<mojom::SensorProvider> receivers_{}; 145 mojo::ScopedSharedBufferHandle shared_buffer_handle_; 146 mojo::ScopedSharedBufferMapping shared_buffer_mapping_; 147 148 DISALLOW_COPY_AND_ASSIGN(FakeSensorProvider); 149 }; 150 151 } // namespace device 152 153 #endif // SERVICES_DEVICE_PUBLIC_CPP_TEST_FAKE_SENSOR_AND_PROVIDER_H_ 154