Chromium Code Reviews| 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 | |
| 150 // This should never run. | |
|
timvolodine
2016/09/27 19:11:00
If this should never run why is it here? EXPECT_FA
maksims (do not use this acc)
2016/09/28 07:58:41
Done.
| |
| 151 if (sensor2) { | |
| 152 EXPECT_EQ(SensorType::AMBIENT_LIGHT, | |
| 153 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT)->GetType()); | |
| 154 } | |
| 155 | |
| 156 EXPECT_FALSE(sensor2); | |
| 157 } | |
| 158 | |
| 159 // This test assumes that a mock sensor has a constant maximum frequency value | |
| 160 // of 50 hz (different from the base sensor class that has a range from 0 to | |
| 161 // 60) and tests whether a mock sensor can be started with a value range from 0 | |
| 162 // to 60. | |
| 163 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) { | |
| 164 const double too_high_frequency = 60; | |
| 165 const double normal_frequency = 39; | |
| 166 scoped_refptr<PlatformSensor> sensor = | |
| 167 CreateSensor(SensorType::AMBIENT_LIGHT); | |
| 168 FakePlatformSensor* fake_sensor = | |
| 169 static_cast<FakePlatformSensor*>(sensor.get()); | |
| 170 EXPECT_TRUE(fake_sensor); | |
| 171 sensor_client_->SetSensor(sensor); | |
| 172 | |
| 173 PlatformSensorConfiguration config(too_high_frequency); | |
| 174 EXPECT_EQ(too_high_frequency, config.frequency()); | |
| 175 EXPECT_FALSE(fake_sensor->StartListening(sensor_client_.get(), config)); | |
| 176 EXPECT_FALSE(fake_sensor->started()); | |
| 177 | |
| 178 config.set_frequency(normal_frequency); | |
| 179 EXPECT_EQ(normal_frequency, config.frequency()); | |
| 180 EXPECT_TRUE(fake_sensor->StartListening(sensor_client_.get(), config)); | |
| 181 EXPECT_TRUE(fake_sensor->started()); | |
| 182 | |
| 183 EXPECT_TRUE(fake_sensor->StopListening(sensor_client_.get(), config)); | |
| 184 EXPECT_FALSE(fake_sensor->started()); | |
| 185 } | |
| 186 | |
| 187 // If a client is in a suspended mode, a NotifySensorReadingChanged() | |
| 188 // notification must not be sent to the client but NotifySensorError() must be. | |
| 189 TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) { | |
| 190 const int num = 5; | |
| 191 scoped_refptr<PlatformSensor> sensor = CreateSensor(SensorType::GYROSCOPE); | |
| 192 FakePlatformSensor* fake_sensor = | |
| 193 static_cast<FakePlatformSensor*>(sensor.get()); | |
| 194 | |
| 195 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; | |
| 196 for (int i = 0; i < num; i++) { | |
| 197 std::unique_ptr<PlatformSensorTestClient> client( | |
| 198 new PlatformSensorTestClient()); | |
| 199 client->SetSensor(fake_sensor); | |
| 200 clients.push_back(std::move(client)); | |
| 201 } | |
| 202 | |
| 203 clients.front()->set_notification_suspended(true); | |
| 204 fake_sensor->NotifySensorReadingChanged(); | |
| 205 fake_sensor->NotifySensorError(); | |
| 206 for (auto const& client : clients) { | |
| 207 if (client == clients.front()) { | |
| 208 EXPECT_FALSE(client->sensor_reading_changed()); | |
| 209 EXPECT_TRUE(client->sensor_error()); | |
| 210 continue; | |
| 211 } | |
| 212 EXPECT_TRUE(client->sensor_reading_changed()); | |
| 213 EXPECT_TRUE(client->sensor_error()); | |
| 214 } | |
| 215 | |
| 216 clients.front()->set_notification_suspended(false); | |
| 217 fake_sensor->NotifySensorReadingChanged(); | |
| 218 fake_sensor->NotifySensorError(); | |
| 219 for (auto const& client : clients) { | |
| 220 EXPECT_TRUE(client->sensor_reading_changed()); | |
| 221 EXPECT_TRUE(client->sensor_error()); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 // Tests that when all clients are removed, config maps are removed as well. | |
| 226 TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) { | |
| 227 const int num = 5; | |
| 228 | |
| 229 scoped_refptr<PlatformSensor> sensor = | |
| 230 CreateSensor(SensorType::AMBIENT_LIGHT); | |
| 231 FakePlatformSensor* fake_sensor = | |
| 232 static_cast<FakePlatformSensor*>(sensor.get()); | |
| 233 EXPECT_TRUE(fake_sensor->config_map().empty()); | |
| 234 | |
| 235 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; | |
| 236 PlatformSensorConfiguration config(30); | |
| 237 for (int i = 0; i < num; i++) { | |
| 238 std::unique_ptr<PlatformSensorTestClient> client( | |
| 239 new PlatformSensorTestClient()); | |
| 240 client->SetSensor(fake_sensor); | |
| 241 EXPECT_TRUE(fake_sensor->StartListening(client.get(), config)); | |
| 242 EXPECT_TRUE(fake_sensor->started()); | |
| 243 | |
| 244 clients.push_back(std::move(client)); | |
| 245 } | |
| 246 EXPECT_FALSE(fake_sensor->config_map().empty()); | |
| 247 | |
| 248 for (const auto& client : clients) | |
| 249 fake_sensor->RemoveClient(client.get()); | |
| 250 | |
| 251 EXPECT_TRUE(fake_sensor->config_map().empty()); | |
| 252 } | |
| 253 | |
| 254 // Tests a sensor cannot be updated if it has one suspended client. | |
| 255 TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) { | |
| 256 scoped_refptr<PlatformSensor> sensor = | |
| 257 CreateSensor(SensorType::AMBIENT_LIGHT); | |
| 258 FakePlatformSensor* fake_sensor = | |
| 259 static_cast<FakePlatformSensor*>(sensor.get()); | |
| 260 EXPECT_TRUE(fake_sensor->config_map().empty()); | |
| 261 | |
| 262 sensor_client_->SetSensor(fake_sensor); | |
| 263 | |
| 264 PlatformSensorConfiguration config(30); | |
| 265 fake_sensor->StartListening(sensor_client_.get(), config); | |
| 266 | |
| 267 sensor_client_->set_notification_suspended(true); | |
| 268 EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); | |
| 269 | |
| 270 fake_sensor->UpdateSensor(); | |
| 271 EXPECT_FALSE(fake_sensor->started()); | |
| 272 | |
| 273 sensor_client_->set_notification_suspended(false); | |
| 274 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); | |
| 275 | |
| 276 fake_sensor->UpdateSensor(); | |
| 277 EXPECT_TRUE(fake_sensor->started()); | |
| 278 } | |
| 279 | |
| 280 // Tests a sensor can be updated if it has one suspended client and other | |
| 281 // clients are not suspended. | |
| 282 TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) { | |
| 283 const int num = 5; | |
| 284 | |
| 285 scoped_refptr<PlatformSensor> sensor = | |
| 286 CreateSensor(SensorType::AMBIENT_LIGHT); | |
| 287 FakePlatformSensor* fake_sensor = | |
| 288 static_cast<FakePlatformSensor*>(sensor.get()); | |
| 289 EXPECT_TRUE(fake_sensor->config_map().empty()); | |
| 290 | |
| 291 sensor_client_->SetSensor(fake_sensor); | |
| 292 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients; | |
| 293 for (int i = 0; i < num; i++) { | |
| 294 std::unique_ptr<PlatformSensorTestClient> client( | |
| 295 new PlatformSensorTestClient()); | |
| 296 client->SetSensor(fake_sensor); | |
| 297 clients.push_back(std::move(client)); | |
| 298 } | |
| 299 | |
| 300 double sensor_frequency = 30; | |
| 301 PlatformSensorConfiguration config(sensor_frequency++); | |
| 302 fake_sensor->StartListening(sensor_client_.get(), config); | |
| 303 for (const auto& client : clients) { | |
| 304 PlatformSensorConfiguration config(sensor_frequency++); | |
| 305 fake_sensor->StartListening(client.get(), config); | |
| 306 } | |
| 307 | |
| 308 sensor_client_->set_notification_suspended(true); | |
| 309 EXPECT_TRUE(sensor_client_->IsNotificationSuspended()); | |
| 310 for (const auto& client : clients) | |
| 311 EXPECT_FALSE(client->IsNotificationSuspended()); | |
| 312 | |
| 313 fake_sensor->UpdateSensor(); | |
| 314 EXPECT_TRUE(fake_sensor->started()); | |
| 315 | |
| 316 sensor_client_->set_notification_suspended(false); | |
| 317 EXPECT_FALSE(sensor_client_->IsNotificationSuspended()); | |
| 318 for (const auto& client : clients) | |
| 319 EXPECT_FALSE(client->IsNotificationSuspended()); | |
| 320 | |
| 321 fake_sensor->UpdateSensor(); | |
| 322 EXPECT_TRUE(fake_sensor->started()); | |
| 323 } | |
| 324 | |
| 325 } // namespace device | |
| OLD | NEW |