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