Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Unified Diff: device/generic_sensor/platform_sensor_provider_unittest.cc

Issue 2306333002: [sensors] Add Generic Sensor platform unit tests. (Closed)
Patch Set: Comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..72d6b5830514af8224c46884e9cfa6fe00ca1fca
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_provider_unittest.cc
@@ -0,0 +1,311 @@
+// 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 "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace device {
+
+using mojom::SensorReadBuffer;
+using mojom::SensorType;
+
+namespace {
+
+uint64_t GetBufferOffset(mojom::SensorType type) {
+ return (static_cast<uint64_t>(SensorType::LAST) -
+ static_cast<uint64_t>(type)) *
+ SensorReadBuffer::SensorReadBuffer::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; }
Mikhail 2016/09/12 08:12:43 set_notification_suspended()
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+
+ void set_sensor(scoped_refptr<PlatformSensor> sensor) {
Mikhail 2016/09/12 08:12:44 SetSensor // not a trivial setter
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ sensor_ = sensor;
+ sensor_->AddClient(this);
+ }
+
+ bool IsSensorReadingChanged() { return sensor_reading_changed_; }
Mikhail 2016/09/12 08:12:43 bool is_sensor_reading_changed() const // trivial
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+
+ bool SensorHasError() { return sensor_error_; }
Mikhail 2016/09/12 08:12:44 ditto https://google.github.io/styleguide/cppguide
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+
+ void remove_sensor(scoped_refptr<PlatformSensor> sensor) {
Mikhail 2016/09/12 08:12:43 this method is not needed, client code can call se
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ 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());
Mikhail 2016/09/12 08:12:43 : sensor_client_(new PlatformSensorTestClient())
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ sensor_provider_ = MockPlatformSensorProvider::GetInstance();
+ EXPECT_TRUE(sensor_provider_);
Mikhail 2016/09/12 08:12:43 I don't think we should test standard singleton cl
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ }
+
+ protected:
+ scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) {
+ return sensor_provider_->CreateSensor(
Mikhail 2016/09/12 08:12:43 MockPlatformSensorProvider::GetInstance()->CreateS
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ type, SensorReadBuffer::kReadBufferSize, GetBufferOffset(type));
+ }
+
+ PlatformSensorProvider* sensor_provider_;
+ std::unique_ptr<PlatformSensorTestClient> sensor_client_;
+};
+
+TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) {
+ scoped_refptr<PlatformSensor> sensor1 = sensor_provider_->CreateSensor(
Mikhail 2016/09/12 08:12:44 why not call 'CreateSensor(SensorType::AMBIENT_LIG
maksims (do not use this acc) 2016/09/13 08:01:02 I've forgotten to modify the callers here. First,
+ SensorType::AMBIENT_LIGHT, SensorReadBuffer::kReadBufferSize,
+ GetBufferOffset(SensorType::AMBIENT_LIGHT));
+ EXPECT_TRUE(sensor1);
+ EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
+
+ scoped_refptr<PlatformSensor> sensor2 = sensor_provider_->CreateSensor(
+ SensorType::PROXIMITY, SensorReadBuffer::kReadBufferSize,
+ GetBufferOffset(SensorType::PROXIMITY));
+ EXPECT_TRUE(sensor2);
+ EXPECT_EQ(SensorType::PROXIMITY, sensor2->GetType());
+
+ scoped_refptr<PlatformSensor> sensor3 = sensor_provider_->CreateSensor(
+ SensorType::ACCELEROMETER, SensorReadBuffer::kReadBufferSize,
+ GetBufferOffset(SensorType::ACCELEROMETER));
+ EXPECT_TRUE(sensor3);
+ EXPECT_EQ(SensorType::ACCELEROMETER, sensor3->GetType());
+
+ scoped_refptr<PlatformSensor> sensor4 = sensor_provider_->CreateSensor(
+ SensorType::GYROSCOPE, SensorReadBuffer::kReadBufferSize,
+ GetBufferOffset(SensorType::GYROSCOPE));
+ EXPECT_TRUE(sensor4);
+ EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType());
+
+ scoped_refptr<PlatformSensor> sensor5 = sensor_provider_->CreateSensor(
+ SensorType::PRESSURE, SensorReadBuffer::kReadBufferSize,
+ GetBufferOffset(SensorType::PRESSURE));
+ EXPECT_TRUE(sensor5);
+ EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType());
+}
+
+TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) {
+ // Create Ambient Light sensor.
+ scoped_refptr<PlatformSensor> sensor1 =
+ CreateSensor(SensorType::AMBIENT_LIGHT);
+ EXPECT_TRUE(sensor1);
+ EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
+
+ // Try to get Gyroscope sensor, which has not been created yet.
+ scoped_refptr<PlatformSensor> sensor2 =
+ sensor_provider_->GetSensor(SensorType::GYROSCOPE);
+ EXPECT_FALSE(sensor2);
+
+ // Get Ambient Light sensor.
+ scoped_refptr<PlatformSensor> sensor3 =
+ sensor_provider_->GetSensor(SensorType::AMBIENT_LIGHT);
+ 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(SensorType::GYROSCOPE, 0, 0);
+ EXPECT_FALSE(sensor4);
+
+ scoped_refptr<PlatformSensor> sensor5 =
+ sensor_provider_->GetSensor(SensorType::GYROSCOPE);
+ 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(SensorType::AMBIENT_LIGHT);
+ 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->started());
+
+ config.set_frequency(normal_frequency);
+ EXPECT_EQ(normal_frequency, config.frequency());
+ EXPECT_TRUE(mock_sensor->StartListening(sensor_client_.get(), config));
+ EXPECT_TRUE(mock_sensor->started());
+
+ EXPECT_TRUE(mock_sensor->StopListening(sensor_client_.get(), config));
+ EXPECT_FALSE(mock_sensor->started());
+}
+
+// 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(SensorType::GYROSCOPE);
+ MockPlatformSensor* mock_sensor =
+ static_cast<MockPlatformSensor*>(sensor.get());
+
+ std::unique_ptr<PlatformSensorTestClient> clients[num];
Mikhail 2016/09/12 08:12:43 pls don't make arrays with smart pointers. https:/
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
+ 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(SensorType::AMBIENT_LIGHT);
+ MockPlatformSensor* mock_sensor =
+ static_cast<MockPlatformSensor*>(sensor.get());
+ EXPECT_TRUE(mock_sensor->config_map().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->started());
+ }
+ EXPECT_FALSE(mock_sensor->config_map().empty());
+
+ for (int i = 0; i < num; i++)
+ clients[i]->remove_sensor(mock_sensor);
+
+ EXPECT_TRUE(mock_sensor->config_map().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(SensorType::AMBIENT_LIGHT);
+ MockPlatformSensor* mock_sensor =
+ static_cast<MockPlatformSensor*>(sensor.get());
+ EXPECT_TRUE(mock_sensor->config_map().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->started());
+
+ sensor_client_->set_suspended(false);
+ EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
+
+ mock_sensor->UpdateSensor();
+ EXPECT_TRUE(mock_sensor->started());
+}
+
+// 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(SensorType::AMBIENT_LIGHT);
+ MockPlatformSensor* mock_sensor =
+ static_cast<MockPlatformSensor*>(sensor.get());
+ EXPECT_TRUE(mock_sensor->config_map().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->started());
+
+ 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->started());
+}
+
+} // namespace device
« device/generic_sensor/mock_platform_sensor.h ('K') | « device/generic_sensor/platform_sensor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698