fake_platform_sensor_and_provider.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef SERVICES_DEVICE_GENERIC_SENSOR_FAKE_PLATFORM_SENSOR_AND_PROVIDER_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_FAKE_PLATFORM_SENSOR_AND_PROVIDER_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "services/device/generic_sensor/platform_sensor.h"
  8. #include "services/device/generic_sensor/platform_sensor_provider.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. namespace device {
  11. class FakePlatformSensor : public PlatformSensor {
  12. public:
  13. FakePlatformSensor(mojom::SensorType type,
  14. SensorReadingSharedBuffer* reading_buffer,
  15. PlatformSensorProvider* provider);
  16. FakePlatformSensor(const FakePlatformSensor&) = delete;
  17. FakePlatformSensor& operator=(const FakePlatformSensor&) = delete;
  18. // PlatformSensor:
  19. MOCK_METHOD1(StartSensor,
  20. bool(const PlatformSensorConfiguration& configuration));
  21. void set_maximum_supported_frequency(double maximum_supported_frequency) {
  22. maximum_supported_frequency_ = maximum_supported_frequency;
  23. }
  24. // Public interface to UpdateSharedBufferAndNotifyClients().
  25. void AddNewReading(const SensorReading& reading);
  26. protected:
  27. void StopSensor() override {}
  28. bool CheckSensorConfiguration(
  29. const PlatformSensorConfiguration& configuration) override;
  30. PlatformSensorConfiguration GetDefaultConfiguration() override;
  31. mojom::ReportingMode GetReportingMode() override;
  32. double GetMaximumSupportedFrequency() override;
  33. double GetMinimumSupportedFrequency() override;
  34. double maximum_supported_frequency_ = 50.0;
  35. ~FakePlatformSensor() override;
  36. };
  37. class FakePlatformSensorProvider : public PlatformSensorProvider {
  38. public:
  39. FakePlatformSensorProvider();
  40. FakePlatformSensorProvider(const FakePlatformSensorProvider&) = delete;
  41. FakePlatformSensorProvider& operator=(const FakePlatformSensorProvider&) =
  42. delete;
  43. ~FakePlatformSensorProvider() override;
  44. MOCK_METHOD0(FreeResources, void());
  45. MOCK_METHOD3(DoCreateSensorInternal,
  46. void(mojom::SensorType,
  47. scoped_refptr<PlatformSensor>,
  48. CreateSensorCallback));
  49. SensorReadingSharedBuffer* GetSensorReadingBuffer(mojom::SensorType type);
  50. private:
  51. void CreateSensorInternal(mojom::SensorType type,
  52. SensorReadingSharedBuffer* reading_buffer,
  53. CreateSensorCallback callback) override;
  54. };
  55. // Mock for PlatformSensor's client interface that is used to deliver
  56. // error and data changes notifications.
  57. class MockPlatformSensorClient : public PlatformSensor::Client {
  58. public:
  59. MockPlatformSensorClient();
  60. // For the given |sensor| this client will be automatically
  61. // added in the costructor and removed in the destructor.
  62. explicit MockPlatformSensorClient(scoped_refptr<PlatformSensor> sensor);
  63. MockPlatformSensorClient(const MockPlatformSensorClient&) = delete;
  64. MockPlatformSensorClient& operator=(const MockPlatformSensorClient&) = delete;
  65. ~MockPlatformSensorClient() override;
  66. scoped_refptr<PlatformSensor> sensor() const { return sensor_; }
  67. // PlatformSensor::Client:
  68. MOCK_METHOD1(OnSensorReadingChanged, void(mojom::SensorType type));
  69. MOCK_METHOD0(OnSensorError, void());
  70. MOCK_METHOD0(IsSuspended, bool());
  71. private:
  72. scoped_refptr<PlatformSensor> sensor_;
  73. };
  74. } // namespace device
  75. #endif // SERVICES_DEVICE_GENERIC_SENSOR_FAKE_PLATFORM_SENSOR_AND_PROVIDER_H_