OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "device/generic_sensor/fake_platform_sensor.h" |
| 6 #include "device/generic_sensor/fake_platform_sensor_provider.h" |
| 7 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
| 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace device { |
| 12 |
| 13 using mojom::SensorInitParams; |
| 14 using mojom::SensorType; |
| 15 |
| 16 namespace { |
| 17 |
| 18 uint64_t GetBufferOffset(mojom::SensorType type) { |
| 19 return (static_cast<uint64_t>(SensorType::LAST) - |
| 20 static_cast<uint64_t>(type)) * |
| 21 SensorInitParams::kReadBufferSize; |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 26 class PlatformSensorTestClient : public PlatformSensor::Client { |
| 27 public: |
| 28 PlatformSensorTestClient() |
| 29 : notification_suspended_(false), |
| 30 sensor_reading_changed_(false), |
| 31 sensor_error_(false) {} |
| 32 |
| 33 ~PlatformSensorTestClient() override { |
| 34 if (sensor_) |
| 35 sensor_->RemoveClient(this); |
| 36 } |
| 37 |
| 38 // PlatformSensor::Client override. |
| 39 void OnSensorReadingChanged() override { sensor_reading_changed_ = true; } |
| 40 |
| 41 void OnSensorError() override { sensor_error_ = true; } |
| 42 |
| 43 bool IsNotificationSuspended() override { return notification_suspended_; } |
| 44 |
| 45 void set_notification_suspended(bool value) { |
| 46 notification_suspended_ = value; |
| 47 } |
| 48 |
| 49 void SetSensor(scoped_refptr<PlatformSensor> sensor) { |
| 50 sensor_ = sensor; |
| 51 sensor_->AddClient(this); |
| 52 } |
| 53 |
| 54 bool sensor_reading_changed() const { return sensor_reading_changed_; } |
| 55 |
| 56 bool sensor_error() const { return sensor_error_; } |
| 57 |
| 58 private: |
| 59 scoped_refptr<PlatformSensor> sensor_; |
| 60 bool notification_suspended_; |
| 61 bool sensor_reading_changed_; |
| 62 bool sensor_error_; |
| 63 }; |
| 64 |
| 65 class PlatformSensorProviderTest : public ::testing::Test { |
| 66 public: |
| 67 PlatformSensorProviderTest() |
| 68 : sensor_client_(new PlatformSensorTestClient()) {} |
| 69 |
| 70 protected: |
| 71 scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) { |
| 72 return FakePlatformSensorProvider::GetInstance()->CreateSensor( |
| 73 type, SensorInitParams::kReadBufferSize, GetBufferOffset(type)); |
| 74 } |
| 75 |
| 76 std::unique_ptr<PlatformSensorTestClient> sensor_client_; |
| 77 }; |
| 78 |
| 79 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) { |
| 80 scoped_refptr<PlatformSensor> sensor1 = |
| 81 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 82 EXPECT_TRUE(sensor1); |
| 83 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
| 84 |
| 85 scoped_refptr<PlatformSensor> sensor2 = CreateSensor(SensorType::PROXIMITY); |
| 86 EXPECT_TRUE(sensor2); |
| 87 EXPECT_EQ(SensorType::PROXIMITY, sensor2->GetType()); |
| 88 |
| 89 scoped_refptr<PlatformSensor> sensor3 = |
| 90 CreateSensor(SensorType::ACCELEROMETER); |
| 91 EXPECT_TRUE(sensor3); |
| 92 EXPECT_EQ(SensorType::ACCELEROMETER, sensor3->GetType()); |
| 93 |
| 94 scoped_refptr<PlatformSensor> sensor4 = CreateSensor(SensorType::GYROSCOPE); |
| 95 EXPECT_TRUE(sensor4); |
| 96 EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType()); |
| 97 |
| 98 scoped_refptr<PlatformSensor> sensor5 = CreateSensor(SensorType::PRESSURE); |
| 99 EXPECT_TRUE(sensor5); |
| 100 EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType()); |
| 101 } |
| 102 |
| 103 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) { |
| 104 PlatformSensorProvider* sensor_provider = |
| 105 FakePlatformSensorProvider::GetInstance(); |
| 106 |
| 107 // Create Ambient Light sensor. |
| 108 scoped_refptr<PlatformSensor> sensor1 = |
| 109 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 110 EXPECT_TRUE(sensor1); |
| 111 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
| 112 |
| 113 // Try to get Gyroscope sensor, which has not been created yet. |
| 114 scoped_refptr<PlatformSensor> sensor2 = |
| 115 sensor_provider->GetSensor(SensorType::GYROSCOPE); |
| 116 EXPECT_FALSE(sensor2); |
| 117 |
| 118 // Get Ambient Light sensor. |
| 119 scoped_refptr<PlatformSensor> sensor3 = |
| 120 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); |
| 121 EXPECT_TRUE(sensor3); |
| 122 |
| 123 EXPECT_EQ(sensor1->GetType(), sensor3->GetType()); |
| 124 |
| 125 // Try to create a sensor with zero buffer and offset. |
| 126 scoped_refptr<PlatformSensor> sensor4 = |
| 127 sensor_provider->CreateSensor(SensorType::GYROSCOPE, 0, 0); |
| 128 EXPECT_FALSE(sensor4); |
| 129 |
| 130 scoped_refptr<PlatformSensor> sensor5 = |
| 131 sensor_provider->GetSensor(SensorType::GYROSCOPE); |
| 132 EXPECT_FALSE(sensor5); |
| 133 } |
| 134 |
| 135 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) { |
| 136 PlatformSensorProvider* sensor_provider = |
| 137 FakePlatformSensorProvider::GetInstance(); |
| 138 |
| 139 // Create Ambient Light sensor. |
| 140 scoped_refptr<PlatformSensor> sensor1 = |
| 141 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 142 EXPECT_TRUE(sensor1); |
| 143 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType()); |
| 144 |
| 145 // Sensor should be automatically destroyed. |
| 146 sensor1 = nullptr; |
| 147 scoped_refptr<PlatformSensor> sensor2 = |
| 148 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT); |
| 149 EXPECT_FALSE(sensor2); |
| 150 } |
| 151 |
| 152 // This test assumes that a mock sensor has a constant maximum frequency value |
| 153 // of 50 hz (different from the base sensor class that has a range from 0 to |
| 154 // 60) and tests whether a mock sensor can be started with a value range from 0 |
| 155 // to 60. |
| 156 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { |
| 157 const double too_high_frequency = 60; |
| 158 const double normal_frequency = 39; |
| 159 scoped_refptr<PlatformSensor> sensor = |
| 160 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 161 FakePlatformSensor* fake_sensor = |
| 162 static_cast<FakePlatformSensor*>(sensor.get()); |
| 163 EXPECT_TRUE(fake_sensor); |
| 164 sensor_client_->SetSensor(sensor); |
| 165 |
| 166 PlatformSensorConfiguration config(too_high_frequency); |
| 167 EXPECT_EQ(too_high_frequency, config.frequency()); |
| 168 EXPECT_FALSE(fake_sensor->StartListening(sensor_client_.get(), config)); |
| 169 EXPECT_FALSE(fake_sensor->started()); |
| 170 |
| 171 config.set_frequency(normal_frequency); |
| 172 EXPECT_EQ(normal_frequency, config.frequency()); |
| 173 EXPECT_TRUE(fake_sensor->StartListening(sensor_client_.get(), config)); |
| 174 EXPECT_TRUE(fake_sensor->started()); |
| 175 |
| 176 EXPECT_TRUE(fake_sensor->StopListening(sensor_client_.get(), config)); |
| 177 EXPECT_FALSE(fake_sensor->started()); |
| 178 } |
| 179 |
| 180 // If a client is in a suspended mode, a NotifySensorReadingChanged() |
| 181 // notification must not be sent to the client but NotifySensorError() must be. |
| 182 TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) { |
| 183 const int num = 5; |
| 184 scoped_refptr<PlatformSensor> sensor = CreateSensor(SensorType::GYROSCOPE); |
| 185 FakePlatformSensor* fake_sensor = |
| 186 static_cast<FakePlatformSensor*>(sensor.get()); |
| 187 |
| 188 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; |
| 189 for (int i = 0; i < num; i++) { |
| 190 std::unique_ptr<PlatformSensorTestClient> client( |
| 191 new PlatformSensorTestClient()); |
| 192 client->SetSensor(fake_sensor); |
| 193 clients.push_back(std::move(client)); |
| 194 } |
| 195 |
| 196 clients.front()->set_notification_suspended(true); |
| 197 fake_sensor->NotifySensorReadingChanged(); |
| 198 fake_sensor->NotifySensorError(); |
| 199 for (auto const& client : clients) { |
| 200 if (client == clients.front()) { |
| 201 EXPECT_FALSE(client->sensor_reading_changed()); |
| 202 EXPECT_TRUE(client->sensor_error()); |
| 203 continue; |
| 204 } |
| 205 EXPECT_TRUE(client->sensor_reading_changed()); |
| 206 EXPECT_TRUE(client->sensor_error()); |
| 207 } |
| 208 |
| 209 clients.front()->set_notification_suspended(false); |
| 210 fake_sensor->NotifySensorReadingChanged(); |
| 211 fake_sensor->NotifySensorError(); |
| 212 for (auto const& client : clients) { |
| 213 EXPECT_TRUE(client->sensor_reading_changed()); |
| 214 EXPECT_TRUE(client->sensor_error()); |
| 215 } |
| 216 } |
| 217 |
| 218 // Tests that when all clients are removed, config maps are removed as well. |
| 219 TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) { |
| 220 const int num = 5; |
| 221 |
| 222 scoped_refptr<PlatformSensor> sensor = |
| 223 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 224 FakePlatformSensor* fake_sensor = |
| 225 static_cast<FakePlatformSensor*>(sensor.get()); |
| 226 EXPECT_TRUE(fake_sensor->config_map().empty()); |
| 227 |
| 228 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; |
| 229 PlatformSensorConfiguration config(30); |
| 230 for (int i = 0; i < num; i++) { |
| 231 std::unique_ptr<PlatformSensorTestClient> client( |
| 232 new PlatformSensorTestClient()); |
| 233 client->SetSensor(fake_sensor); |
| 234 EXPECT_TRUE(fake_sensor->StartListening(client.get(), config)); |
| 235 EXPECT_TRUE(fake_sensor->started()); |
| 236 |
| 237 clients.push_back(std::move(client)); |
| 238 } |
| 239 EXPECT_FALSE(fake_sensor->config_map().empty()); |
| 240 |
| 241 for (const auto& client : clients) |
| 242 fake_sensor->RemoveClient(client.get()); |
| 243 |
| 244 EXPECT_TRUE(fake_sensor->config_map().empty()); |
| 245 } |
| 246 |
| 247 // Tests a sensor cannot be updated if it has one suspended client. |
| 248 TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) { |
| 249 scoped_refptr<PlatformSensor> sensor = |
| 250 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 251 FakePlatformSensor* fake_sensor = |
| 252 static_cast<FakePlatformSensor*>(sensor.get()); |
| 253 EXPECT_TRUE(fake_sensor->config_map().empty()); |
| 254 |
| 255 sensor_client_->SetSensor(fake_sensor); |
| 256 |
| 257 PlatformSensorConfiguration config(30); |
| 258 fake_sensor->StartListening(sensor_client_.get(), config); |
| 259 |
| 260 sensor_client_->set_notification_suspended(true); |
| 261 EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); |
| 262 |
| 263 fake_sensor->UpdateSensor(); |
| 264 EXPECT_FALSE(fake_sensor->started()); |
| 265 |
| 266 sensor_client_->set_notification_suspended(false); |
| 267 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
| 268 |
| 269 fake_sensor->UpdateSensor(); |
| 270 EXPECT_TRUE(fake_sensor->started()); |
| 271 } |
| 272 |
| 273 // Tests a sensor can be updated if it has one suspended client and other |
| 274 // clients are not suspended. |
| 275 TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) { |
| 276 const int num = 5; |
| 277 |
| 278 scoped_refptr<PlatformSensor> sensor = |
| 279 CreateSensor(SensorType::AMBIENT_LIGHT); |
| 280 FakePlatformSensor* fake_sensor = |
| 281 static_cast<FakePlatformSensor*>(sensor.get()); |
| 282 EXPECT_TRUE(fake_sensor->config_map().empty()); |
| 283 |
| 284 sensor_client_->SetSensor(fake_sensor); |
| 285 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; |
| 286 for (int i = 0; i < num; i++) { |
| 287 std::unique_ptr<PlatformSensorTestClient> client( |
| 288 new PlatformSensorTestClient()); |
| 289 client->SetSensor(fake_sensor); |
| 290 clients.push_back(std::move(client)); |
| 291 } |
| 292 |
| 293 double sensor_frequency = 30; |
| 294 PlatformSensorConfiguration config(sensor_frequency++); |
| 295 fake_sensor->StartListening(sensor_client_.get(), config); |
| 296 for (const auto& client : clients) { |
| 297 PlatformSensorConfiguration config(sensor_frequency++); |
| 298 fake_sensor->StartListening(client.get(), config); |
| 299 } |
| 300 |
| 301 sensor_client_->set_notification_suspended(true); |
| 302 EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); |
| 303 for (const auto& client : clients) |
| 304 EXPECT_FALSE(client->IsNotificationSuspended()); |
| 305 |
| 306 fake_sensor->UpdateSensor(); |
| 307 EXPECT_TRUE(fake_sensor->started()); |
| 308 |
| 309 sensor_client_->set_notification_suspended(false); |
| 310 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); |
| 311 for (const auto& client : clients) |
| 312 EXPECT_FALSE(client->IsNotificationSuspended()); |
| 313 |
| 314 fake_sensor->UpdateSensor(); |
| 315 EXPECT_TRUE(fake_sensor->started()); |
| 316 } |
| 317 |
| 318 } // namespace device |
OLD | NEW |