Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_provider_unittest.cc |
| diff --git a/device/generic_sensor/platform_sensor_provider_unittest.cc b/device/generic_sensor/platform_sensor_provider_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..32a32ed571cee6f4fc74dcb954400780a0387e9a |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_provider_unittest.cc |
| @@ -0,0 +1,305 @@ |
| +// Copyright 2016 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 "device/generic_sensor/mock_platform_sensor.h" |
| +#include "device/generic_sensor/mock_platform_sensor_provider.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace device { |
| + |
| +namespace { |
| + |
| +static const uint64_t kReadBufferSize = 32ULL; |
|
Mikhail
2016/09/07 10:46:19
that can be taken from mojom::SensorReadBuffer
maksims (do not use this acc)
2016/09/09 10:39:06
Done.
|
| + |
| +const mojom::SensorType kAmbientLight = mojom::SensorType::AMBIENT_LIGHT; |
| +const mojom::SensorType kProximity = mojom::SensorType::PROXIMITY; |
| +const mojom::SensorType kAccelerometer = mojom::SensorType::ACCELEROMETER; |
| +const mojom::SensorType kGyroscope = mojom::SensorType::GYROSCOPE; |
| +const mojom::SensorType kPressure = mojom::SensorType::PRESSURE; |
|
Mikhail
2016/09/07 10:46:19
let's not hide the actual types, would 'using mojo
maksims (do not use this acc)
2016/09/09 10:39:06
Done.
|
| + |
| +uint64_t GetBufferOffset(mojom::SensorType type) { |
| + return (static_cast<uint64_t>(mojom::SensorType::LAST) - |
| + static_cast<uint64_t>(type)) * |
| + kReadBufferSize; |
| +} |
| + |
| +} // namespace |
| + |
| +class PlatformSensorTestClient : public PlatformSensor::Client { |
| + public: |
| + PlatformSensorTestClient() |
| + : notification_suspended_(false), |
| + sensor_reading_changed_(false), |
| + sensor_error_(false) {} |
| + |
| + ~PlatformSensorTestClient() override { |
| + if (sensor_) |
| + sensor_->RemoveClient(this); |
| + } |
| + |
| + // PlatformSensor::Client override. |
| + void OnSensorReadingChanged() override { sensor_reading_changed_ = true; } |
| + |
| + void OnSensorError() override { sensor_error_ = true; } |
| + |
| + bool IsNotificationSuspended() override { return notification_suspended_; } |
| + |
| + void set_suspended(bool value) { notification_suspended_ = value; } |
| + |
| + void set_sensor(scoped_refptr<PlatformSensor> sensor) { |
| + sensor_ = sensor; |
| + sensor_->AddClient(this); |
| + } |
| + |
| + bool IsSensorReadingChanged() { return sensor_reading_changed_; } |
| + |
| + bool SensorHasError() { return sensor_error_; } |
| + |
| + void remove_sensor(scoped_refptr<PlatformSensor> sensor) { |
| + EXPECT_EQ(sensor, sensor_); |
| + sensor_->RemoveClient(this); |
| + } |
| + |
| + private: |
| + scoped_refptr<PlatformSensor> sensor_; |
| + bool notification_suspended_; |
| + bool sensor_reading_changed_; |
| + bool sensor_error_; |
| +}; |
| + |
| +class PlatformSensorProviderTest : public ::testing::Test { |
| + public: |
| + PlatformSensorProviderTest() { |
| + sensor_client_.reset(new PlatformSensorTestClient()); |
| + sensor_provider_ = MockPlatformSensorProvider::GetInstance(); |
| + EXPECT_TRUE(sensor_provider_); |
| + } |
| + |
| + protected: |
| + scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) { |
| + return sensor_provider_->CreateSensor(type, kReadBufferSize, |
| + GetBufferOffset(type)); |
| + } |
| + |
| + PlatformSensorProvider* sensor_provider_; |
| + std::unique_ptr<PlatformSensorTestClient> sensor_client_; |
| +}; |
| + |
| +TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { |
| + scoped_refptr<PlatformSensor> sensor1 = sensor_provider_->CreateSensor( |
| + kAmbientLight, kReadBufferSize, GetBufferOffset(kAmbientLight)); |
| + EXPECT_TRUE(sensor1); |
| + EXPECT_EQ(kAmbientLight, sensor1->GetType()); |
| + |
| + scoped_refptr<PlatformSensor> sensor2 = sensor_provider_->CreateSensor( |
| + kProximity, kReadBufferSize, GetBufferOffset(kProximity)); |
| + EXPECT_TRUE(sensor2); |
| + EXPECT_EQ(kProximity, sensor2->GetType()); |
| + |
| + scoped_refptr<PlatformSensor> sensor3 = sensor_provider_->CreateSensor( |
| + kAccelerometer, kReadBufferSize, GetBufferOffset(kAccelerometer)); |
| + EXPECT_TRUE(sensor3); |
| + EXPECT_EQ(kAccelerometer, sensor3->GetType()); |
| + |
| + scoped_refptr<PlatformSensor> sensor4 = sensor_provider_->CreateSensor( |
| + kGyroscope, kReadBufferSize, GetBufferOffset(kGyroscope)); |
| + EXPECT_TRUE(sensor4); |
| + EXPECT_EQ(kGyroscope, sensor4->GetType()); |
| + |
| + scoped_refptr<PlatformSensor> sensor5 = sensor_provider_->CreateSensor( |
| + kPressure, kReadBufferSize, GetBufferOffset(kPressure)); |
| + EXPECT_TRUE(sensor5); |
| + EXPECT_EQ(kPressure, sensor5->GetType()); |
| +} |
| + |
| +TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) { |
| + // Create Ambient Light sensor. |
| + scoped_refptr<PlatformSensor> sensor1 = CreateSensor(kAmbientLight); |
| + EXPECT_TRUE(sensor1); |
| + EXPECT_EQ(kAmbientLight, sensor1->GetType()); |
| + |
| + // Try to get Gyroscope sensor, which has not been created yet. |
| + scoped_refptr<PlatformSensor> sensor2 = |
| + sensor_provider_->GetSensor(kGyroscope); |
| + EXPECT_FALSE(sensor2); |
| + |
| + // Get Ambient Light sensor. |
| + scoped_refptr<PlatformSensor> sensor3 = |
| + sensor_provider_->GetSensor(kAmbientLight); |
| + EXPECT_TRUE(sensor3); |
| + |
| + EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); |
| + |
| + // Try to create a sensor with zero buffer and offset. |
| + scoped_refptr<PlatformSensor> sensor4 = |
| + sensor_provider_->CreateSensor(kGyroscope, 0, 0); |
| + EXPECT_FALSE(sensor4); |
| + |
| + scoped_refptr<PlatformSensor> sensor5 = |
| + sensor_provider_->GetSensor(kGyroscope); |
| + EXPECT_FALSE(sensor5); |
| +} |
| + |
| +// This test assumes that a mock sensor has a constant maximum frequency value |
| +// of 50 hz (different from the base sensor class that has a range from 0 to |
| +// 60) and tests whether a mock sensor can be started with a value range from 0 |
| +// to 60. |
| +TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { |
| + const double too_high_frequency = 60; |
| + const double normal_frequency = 39; |
| + scoped_refptr<PlatformSensor> sensor = CreateSensor(kAmbientLight); |
| + MockPlatformSensor* mock_sensor = |
| + static_cast<MockPlatformSensor*>(sensor.get()); |
| + EXPECT_TRUE(mock_sensor); |
| + sensor_client_->set_sensor(sensor); |
| + |
| + PlatformSensorConfiguration config(too_high_frequency); |
| + EXPECT_EQ(too_high_frequency, config.frequency()); |
| + EXPECT_FALSE(mock_sensor->StartListening(sensor_client_.get(), config)); |
| + EXPECT_FALSE(mock_sensor->IsStarted()); |
| + |
| + config.set_frequency(normal_frequency); |
| + EXPECT_EQ(normal_frequency, config.frequency()); |
| + EXPECT_TRUE(mock_sensor->StartListening(sensor_client_.get(), config)); |
| + EXPECT_TRUE(mock_sensor->IsStarted()); |
| + |
| + EXPECT_TRUE(mock_sensor->StopListening(sensor_client_.get(), config)); |
| + EXPECT_FALSE(mock_sensor->IsStarted()); |
| +} |
| + |
| +// If a client is in a suspended mode, a NotifySensorReadingChanged() |
| +// notification must not be sent to the client but NotifySensorError() must be. |
| +TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) { |
| + const int num = 5; |
| + scoped_refptr<PlatformSensor> sensor = CreateSensor(kGyroscope); |
| + MockPlatformSensor* mock_sensor = |
| + static_cast<MockPlatformSensor*>(sensor.get()); |
| + |
| + std::unique_ptr<PlatformSensorTestClient> clients[num]; |
|
Mikhail
2016/09/07 10:46:19
here and below, pls. use std::vector<std::unique_p
|
| + for (int i = 0; i < num; i++) { |
| + clients[i].reset(new PlatformSensorTestClient()); |
| + clients[i]->set_sensor(mock_sensor); |
| + } |
| + |
| + clients[1]->set_suspended(true); |
| + mock_sensor->NotifySensorReadingChanged(); |
| + mock_sensor->NotifySensorError(); |
| + for (int i = 0; i < num; i++) { |
| + if (i == 1) { |
| + EXPECT_FALSE(clients[i]->IsSensorReadingChanged()); |
| + EXPECT_TRUE(clients[i]->SensorHasError()); |
| + continue; |
| + } |
| + EXPECT_TRUE(clients[i]->IsSensorReadingChanged()); |
| + EXPECT_TRUE(clients[i]->SensorHasError()); |
| + } |
| + |
| + clients[1]->set_suspended(false); |
| + mock_sensor->NotifySensorReadingChanged(); |
| + mock_sensor->NotifySensorError(); |
| + for (int i = 0; i < num; i++) { |
| + EXPECT_TRUE(clients[i]->IsSensorReadingChanged()); |
| + EXPECT_TRUE(clients[i]->SensorHasError()); |
| + } |
| +} |
| + |
| +// Tests that when all clients are removed, config maps are removed as well. |
| +TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) { |
| + const int num = 5; |
| + const double frq = 30; |
| + |
| + scoped_refptr<PlatformSensor> sensor = CreateSensor(kAmbientLight); |
| + MockPlatformSensor* mock_sensor = |
| + static_cast<MockPlatformSensor*>(sensor.get()); |
| + EXPECT_TRUE(mock_sensor->GetConfigMap().empty()); |
| + |
| + std::unique_ptr<PlatformSensorTestClient> clients[num]; |
| + PlatformSensorConfiguration config(frq); |
| + for (int i = 0; i < num; i++) { |
| + clients[i].reset(new PlatformSensorTestClient()); |
| + clients[i]->set_sensor(mock_sensor); |
| + EXPECT_TRUE(mock_sensor->StartListening(clients[i].get(), config)); |
| + EXPECT_TRUE(mock_sensor->IsStarted()); |
| + } |
| + EXPECT_FALSE(mock_sensor->GetConfigMap().empty()); |
| + |
| + for (int i = 0; i < num; i++) |
| + clients[i]->remove_sensor(mock_sensor); |
| + |
| + EXPECT_TRUE(mock_sensor->GetConfigMap().empty()); |
| +} |
| + |
| +// Tests a sensor cannot be updated if it has one suspended client. |
| +TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) { |
| + const double frq = 30; |
| + |
| + scoped_refptr<PlatformSensor> sensor = CreateSensor(kAmbientLight); |
| + MockPlatformSensor* mock_sensor = |
| + static_cast<MockPlatformSensor*>(sensor.get()); |
| + EXPECT_TRUE(mock_sensor->GetConfigMap().empty()); |
| + |
| + sensor_client_->set_sensor(mock_sensor); |
| + |
| + PlatformSensorConfiguration config(frq); |
| + mock_sensor->StartListening(sensor_client_.get(), config); |
| + |
| + sensor_client_->set_suspended(true); |
| + EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); |
| + |
| + mock_sensor->UpdateSensor(); |
| + EXPECT_FALSE(mock_sensor->IsStarted()); |
| + |
| + sensor_client_->set_suspended(false); |
| + EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
| + |
| + mock_sensor->UpdateSensor(); |
| + EXPECT_TRUE(mock_sensor->IsStarted()); |
| +} |
| + |
| +// Tests a sensor can be updated if it has one suspended client and other |
| +// clients are not suspended. |
| +TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) { |
| + const int num = 5; |
| + const double frq = 30; |
| + |
| + scoped_refptr<PlatformSensor> sensor = CreateSensor(kAmbientLight); |
| + MockPlatformSensor* mock_sensor = |
| + static_cast<MockPlatformSensor*>(sensor.get()); |
| + EXPECT_TRUE(mock_sensor->GetConfigMap().empty()); |
| + |
| + sensor_client_->set_sensor(mock_sensor); |
| + std::unique_ptr<PlatformSensorTestClient> clients[num]; |
| + for (int i = 0; i < num; i++) { |
| + clients[i].reset(new PlatformSensorTestClient()); |
| + clients[i]->set_sensor(mock_sensor); |
| + } |
| + |
| + PlatformSensorConfiguration config(frq); |
| + mock_sensor->StartListening(sensor_client_.get(), config); |
| + for (int i = 0; i < num; i++) { |
| + PlatformSensorConfiguration config(frq + i); |
| + mock_sensor->StartListening(clients[i].get(), config); |
| + } |
| + |
| + sensor_client_->set_suspended(true); |
| + EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); |
| + for (int i = 0; i < num; i++) { |
| + EXPECT_FALSE(clients[i]->IsNotificationSuspended()); |
| + } |
| + |
| + mock_sensor->UpdateSensor(); |
| + EXPECT_TRUE(mock_sensor->IsStarted()); |
| + |
| + sensor_client_->set_suspended(false); |
| + EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
| + for (int i = 0; i < num; i++) { |
| + EXPECT_FALSE(clients[i]->IsNotificationSuspended()); |
| + } |
| + |
| + mock_sensor->UpdateSensor(); |
| + EXPECT_TRUE(mock_sensor->IsStarted()); |
| +} |
| + |
| +} // namespace device |