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