fake_platform_sensor_and_provider.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/generic_sensor/fake_platform_sensor_and_provider.h"
  5. #include <utility>
  6. using ::testing::_;
  7. using ::testing::Invoke;
  8. using ::testing::Return;
  9. namespace device {
  10. FakePlatformSensor::FakePlatformSensor(
  11. mojom::SensorType type,
  12. SensorReadingSharedBuffer* reading_buffer,
  13. PlatformSensorProvider* provider)
  14. : PlatformSensor(type, reading_buffer, provider) {
  15. ON_CALL(*this, StartSensor(_))
  16. .WillByDefault(
  17. Invoke([this](const PlatformSensorConfiguration& configuration) {
  18. SensorReading reading;
  19. // Only mocking the shared memory update for AMBIENT_LIGHT and
  20. // PRESSURE type is enough.
  21. // Set the shared buffer value as frequency for testing purpose.
  22. switch (GetType()) {
  23. case mojom::SensorType::AMBIENT_LIGHT:
  24. reading.als.value = configuration.frequency();
  25. AddNewReading(reading);
  26. break;
  27. case mojom::SensorType::PRESSURE:
  28. reading.pressure.value = configuration.frequency();
  29. AddNewReading(reading);
  30. break;
  31. default:
  32. break;
  33. }
  34. return true;
  35. }));
  36. }
  37. FakePlatformSensor::~FakePlatformSensor() = default;
  38. bool FakePlatformSensor::CheckSensorConfiguration(
  39. const PlatformSensorConfiguration& configuration) {
  40. return configuration.frequency() <= GetMaximumSupportedFrequency() &&
  41. configuration.frequency() >= GetMinimumSupportedFrequency();
  42. }
  43. PlatformSensorConfiguration FakePlatformSensor::GetDefaultConfiguration() {
  44. return PlatformSensorConfiguration(30.0);
  45. }
  46. mojom::ReportingMode FakePlatformSensor::GetReportingMode() {
  47. // Set the ReportingMode as ON_CHANGE, so we can test the
  48. // SensorReadingChanged() mojo interface.
  49. return mojom::ReportingMode::ON_CHANGE;
  50. }
  51. double FakePlatformSensor::GetMaximumSupportedFrequency() {
  52. return maximum_supported_frequency_;
  53. }
  54. double FakePlatformSensor::GetMinimumSupportedFrequency() {
  55. return 1.0;
  56. }
  57. void FakePlatformSensor::AddNewReading(const SensorReading& reading) {
  58. UpdateSharedBufferAndNotifyClients(reading);
  59. }
  60. FakePlatformSensorProvider::FakePlatformSensorProvider() {
  61. ON_CALL(*this, DoCreateSensorInternal(_, _, _))
  62. .WillByDefault(
  63. Invoke([](mojom::SensorType, scoped_refptr<PlatformSensor> sensor,
  64. PlatformSensorProvider::CreateSensorCallback callback) {
  65. std::move(callback).Run(std::move(sensor));
  66. }));
  67. }
  68. FakePlatformSensorProvider::~FakePlatformSensorProvider() = default;
  69. SensorReadingSharedBuffer* FakePlatformSensorProvider::GetSensorReadingBuffer(
  70. mojom::SensorType type) {
  71. return CreateSharedBufferIfNeeded()
  72. ? GetSensorReadingSharedBufferForType(type)
  73. : nullptr;
  74. }
  75. void FakePlatformSensorProvider::CreateSensorInternal(
  76. mojom::SensorType type,
  77. SensorReadingSharedBuffer* reading_buffer,
  78. CreateSensorCallback callback) {
  79. DCHECK(type >= mojom::SensorType::kMinValue &&
  80. type <= mojom::SensorType::kMaxValue);
  81. auto sensor =
  82. base::MakeRefCounted<FakePlatformSensor>(type, reading_buffer, this);
  83. DoCreateSensorInternal(type, std::move(sensor), std::move(callback));
  84. }
  85. MockPlatformSensorClient::MockPlatformSensorClient() {
  86. ON_CALL(*this, IsSuspended()).WillByDefault(Return(false));
  87. }
  88. MockPlatformSensorClient::MockPlatformSensorClient(
  89. scoped_refptr<PlatformSensor> sensor)
  90. : MockPlatformSensorClient() {
  91. DCHECK(sensor);
  92. sensor_ = std::move(sensor);
  93. sensor_->AddClient(this);
  94. }
  95. MockPlatformSensorClient::~MockPlatformSensorClient() {
  96. if (sensor_)
  97. sensor_->RemoveClient(this);
  98. }
  99. } // namespace device