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 "base/files/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" |
| 11 |
| 12 #include "device/generic_sensor/generic_sensor_consts.h" |
| 13 #include "device/generic_sensor/linux/platform_sensor_manager.h" |
| 14 #include "device/generic_sensor/linux/sensor_data_linux.h" |
| 15 #include "device/generic_sensor/platform_sensor_provider_linux.h" |
| 16 |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using ::testing::_; |
| 21 using ::testing::Invoke; |
| 22 using ::testing::IsNull; |
| 23 using ::testing::NiceMock; |
| 24 using ::testing::NotNull; |
| 25 using ::testing::Return; |
| 26 |
| 27 namespace device { |
| 28 |
| 29 namespace { |
| 30 |
| 31 using mojom::SensorType; |
| 32 |
| 33 // Zero value can mean whether value is not being not used or zero value. |
| 34 constexpr double kZero = 0.0; |
| 35 |
| 36 constexpr double kAccelerometerFrequencyValue = 10.0; |
| 37 constexpr double kAccelerometerOffsetValue = 1.0; |
| 38 constexpr double kAccelerometerScalingValue = 0.009806; |
| 39 |
| 40 constexpr double kGyroscopeFrequencyValue = 6.0; |
| 41 constexpr double kGyroscopeOffsetValue = 2.0; |
| 42 constexpr double kGyroscopeScalingValue = 0.000017; |
| 43 |
| 44 constexpr double kMagnetometerFrequencyValue = 7.0; |
| 45 constexpr double kMagnetometerOffsetValue = 3.0; |
| 46 constexpr double kMagnetometerScalingValue = 0.000001; |
| 47 |
| 48 void DeleteFile(const base::FilePath& file) { |
| 49 EXPECT_TRUE(base::DeleteFile(file, true)); |
| 50 } |
| 51 |
| 52 void WriteValueToFile(const base::FilePath& path, double value) { |
| 53 const std::string str = base::DoubleToString(value); |
| 54 int bytes_written = base::WriteFile(path, str.data(), str.size()); |
| 55 EXPECT_EQ(static_cast<size_t>(bytes_written), str.size()); |
| 56 } |
| 57 |
| 58 std::string ReadValueFromFile(const base::FilePath& path, |
| 59 const std::string& file) { |
| 60 base::FilePath file_path = base::FilePath(path).Append(file); |
| 61 std::string new_read_value; |
| 62 if (!base::ReadFileToString(file_path, &new_read_value)) |
| 63 return std::string(); |
| 64 return new_read_value; |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 69 // Mock for SensorDeviceService that SensorDeviceManager owns. |
| 70 // This mock is used to emulate udev events and send found sensor devices |
| 71 // to SensorDeviceManager. |
| 72 class MockSensorDeviceManager : public SensorDeviceManager { |
| 73 public: |
| 74 MockSensorDeviceManager() = default; |
| 75 ~MockSensorDeviceManager() override {} |
| 76 |
| 77 MOCK_METHOD1(GetUdevDeviceGetSubsystem, std::string(udev_device*)); |
| 78 MOCK_METHOD1(GetUdevDeviceGetSyspath, std::string(udev_device*)); |
| 79 MOCK_METHOD1(GetUdevDeviceGetDevnode, std::string(udev_device* dev)); |
| 80 MOCK_METHOD2(GetUdevDeviceGetSysattrValue, |
| 81 std::string(udev_device*, const std::string&)); |
| 82 MOCK_METHOD1(Start, void(Delegate*)); |
| 83 |
| 84 void InitializeService(Delegate* delegate) { delegate_ = delegate; } |
| 85 |
| 86 void EnumerationReady() { |
| 87 bool success = task_runner_->PostTask( |
| 88 FROM_HERE, |
| 89 base::Bind(&SensorDeviceManager::Delegate::OnSensorNodesEnumerated, |
| 90 base::Unretained(delegate_))); |
| 91 ASSERT_TRUE(success); |
| 92 } |
| 93 |
| 94 void DeviceAdded(udev_device* dev) { |
| 95 SensorDeviceManager::OnDeviceAdded(dev); |
| 96 } |
| 97 |
| 98 void DeviceRemoved(udev_device* dev) { |
| 99 SensorDeviceManager::OnDeviceRemoved(dev); |
| 100 } |
| 101 |
| 102 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(MockSensorDeviceManager); |
| 104 }; |
| 105 |
| 106 // Mock for PlatformSensor's client interface that is used to deliver |
| 107 // error and data changes notifications. |
| 108 class MockPlatformSensorClient : public PlatformSensor::Client { |
| 109 public: |
| 110 MockPlatformSensorClient() = default; |
| 111 explicit MockPlatformSensorClient(scoped_refptr<PlatformSensor> sensor) |
| 112 : sensor_(sensor) { |
| 113 if (sensor_) |
| 114 sensor_->AddClient(this); |
| 115 |
| 116 ON_CALL(*this, IsNotificationSuspended()).WillByDefault(Return(false)); |
| 117 } |
| 118 |
| 119 ~MockPlatformSensorClient() override { |
| 120 if (sensor_) |
| 121 sensor_->RemoveClient(this); |
| 122 } |
| 123 |
| 124 // PlatformSensor::Client interface. |
| 125 MOCK_METHOD0(OnSensorReadingChanged, void()); |
| 126 MOCK_METHOD0(OnSensorError, void()); |
| 127 MOCK_METHOD0(IsNotificationSuspended, bool()); |
| 128 |
| 129 private: |
| 130 scoped_refptr<PlatformSensor> sensor_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(MockPlatformSensorClient); |
| 133 }; |
| 134 |
| 135 class PlatformSensorAndProviderLinuxTest : public ::testing::Test { |
| 136 public: |
| 137 void SetUp() override { |
| 138 provider_ = PlatformSensorProviderLinux::GetInstance(); |
| 139 provider_->SetFileTaskRunnerForTesting(message_loop_.task_runner()); |
| 140 |
| 141 auto manager = base::MakeUnique<NiceMock<MockSensorDeviceManager>>(); |
| 142 manager_ = manager.get(); |
| 143 provider_->SetSensorDeviceManagerForTesting(std::move(manager)); |
| 144 |
| 145 ASSERT_TRUE(sensors_dir_.CreateUniqueTempDir()); |
| 146 } |
| 147 |
| 148 void TearDown() override { |
| 149 provider_->SetSensorDeviceManagerForTesting(nullptr); |
| 150 ASSERT_TRUE(sensors_dir_.Delete()); |
| 151 base::RunLoop().RunUntilIdle(); |
| 152 } |
| 153 |
| 154 protected: |
| 155 void SensorsCreated(scoped_refptr<PlatformSensor> sensor) { |
| 156 platform_sensor_vector_.push_back(sensor); |
| 157 } |
| 158 |
| 159 void SensorCreated(scoped_refptr<PlatformSensor> sensor) { |
| 160 platform_sensor_ = sensor; |
| 161 run_loop_->Quit(); |
| 162 } |
| 163 |
| 164 // Sensor creation is asynchronous, therefore inner loop is used to wait for |
| 165 // PlatformSensorProvider::CreateSensorCallback completion. |
| 166 scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) { |
| 167 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 168 provider_->CreateSensor( |
| 169 type, base::Bind(&PlatformSensorAndProviderLinuxTest::SensorCreated, |
| 170 base::Unretained(this))); |
| 171 run_loop_->Run(); |
| 172 scoped_refptr<PlatformSensor> sensor; |
| 173 sensor.swap(platform_sensor_); |
| 174 run_loop_ = nullptr; |
| 175 return sensor; |
| 176 } |
| 177 |
| 178 // Creates sensor files according to SensorPathsLinux. |
| 179 // Existence of sensor read files mean existence of a sensor. |
| 180 void InitializeSupportedSensor(SensorType type, |
| 181 double frequency, |
| 182 double offset, |
| 183 double scaling, |
| 184 double values[3]) { |
| 185 SensorPathsLinux data; |
| 186 EXPECT_TRUE(InitSensorData(type, &data)); |
| 187 |
| 188 base::FilePath sensor_dir = sensors_dir_.GetPath(); |
| 189 if (!data.sensor_scale_name.empty()) { |
| 190 base::FilePath sensor_scale_file = |
| 191 base::FilePath(sensor_dir).Append(data.sensor_scale_name); |
| 192 WriteValueToFile(sensor_scale_file, scaling); |
| 193 } |
| 194 |
| 195 if (!data.sensor_offset_file_name.empty()) { |
| 196 base::FilePath sensor_offset_file = |
| 197 base::FilePath(sensor_dir).Append(data.sensor_offset_file_name); |
| 198 WriteValueToFile(sensor_offset_file, offset); |
| 199 } |
| 200 |
| 201 if (!data.sensor_frequency_file_name.empty()) { |
| 202 base::FilePath sensor_frequency_file = |
| 203 base::FilePath(sensor_dir).Append(data.sensor_frequency_file_name); |
| 204 WriteValueToFile(sensor_frequency_file, frequency); |
| 205 } |
| 206 |
| 207 uint32_t i = 0; |
| 208 for (const auto& file_names : data.sensor_file_names) { |
| 209 for (const auto& name : file_names) { |
| 210 base::FilePath sensor_file = base::FilePath(sensor_dir).Append(name); |
| 211 WriteValueToFile(sensor_file, values[i++]); |
| 212 break; |
| 213 } |
| 214 } |
| 215 } |
| 216 |
| 217 // Initializes mock udev methods that emulate system methods by |
| 218 // just reading values from files, which SensorDeviceService has specified |
| 219 // calling udev methods. |
| 220 void InitializeMockUdevMethods(const base::FilePath& sensor_dir) { |
| 221 ON_CALL(*manager_, GetUdevDeviceGetSubsystem(IsNull())) |
| 222 .WillByDefault(Invoke([this](udev_device* dev) { return "iio"; })); |
| 223 |
| 224 ON_CALL(*manager_, GetUdevDeviceGetSyspath(IsNull())) |
| 225 .WillByDefault(Invoke( |
| 226 [sensor_dir](udev_device* dev) { return sensor_dir.value(); })); |
| 227 |
| 228 ON_CALL(*manager_, GetUdevDeviceGetDevnode(IsNull())) |
| 229 .WillByDefault( |
| 230 Invoke([this](udev_device* dev) { return "/dev/test"; })); |
| 231 |
| 232 ON_CALL(*manager_, GetUdevDeviceGetSysattrValue(IsNull(), _)) |
| 233 .WillByDefault(Invoke( |
| 234 [sensor_dir](udev_device* dev, const std::string& attribute) { |
| 235 return ReadValueFromFile(sensor_dir, attribute); |
| 236 })); |
| 237 } |
| 238 |
| 239 // Emulates device enumerations and initial udev events. Once all |
| 240 // devices are added, tells manager its ready. |
| 241 void SetServiceStart() { |
| 242 EXPECT_CALL(*manager_, Start(NotNull())) |
| 243 .WillOnce(Invoke([this](SensorDeviceManager::Delegate* delegate) { |
| 244 manager_->InitializeService(delegate); |
| 245 udev_device* dev = nullptr; |
| 246 manager_->DeviceAdded(dev /* not used */); |
| 247 manager_->EnumerationReady(); |
| 248 })); |
| 249 } |
| 250 |
| 251 // Waits before OnSensorReadingChanged is called. |
| 252 void WaitOnSensorReadingChangedEvent(MockPlatformSensorClient* client) { |
| 253 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 254 EXPECT_CALL(*client, OnSensorReadingChanged()).WillOnce(Invoke([this]() { |
| 255 run_loop_->Quit(); |
| 256 })); |
| 257 run_loop_->Run(); |
| 258 run_loop_ = nullptr; |
| 259 } |
| 260 |
| 261 // Waits before OnSensorError is called. |
| 262 void WaitOnSensorErrorEvent(MockPlatformSensorClient* client) { |
| 263 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 264 EXPECT_CALL(*client, OnSensorError()).WillOnce(Invoke([this]() { |
| 265 run_loop_->Quit(); |
| 266 })); |
| 267 run_loop_->Run(); |
| 268 run_loop_ = nullptr; |
| 269 } |
| 270 |
| 271 // Generates a "remove device" event by removed sensors' directory and |
| 272 // notifies the mock service about "removed" event. |
| 273 void GenerateDeviceRemovedEvent(const base::FilePath& sensor_dir) { |
| 274 udev_device* dev = nullptr; |
| 275 DeleteFile(sensor_dir); |
| 276 bool success = base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 277 FROM_HERE, base::Bind(&MockSensorDeviceManager::DeviceRemoved, |
| 278 base::Unretained(manager_), dev /* not used */)); |
| 279 ASSERT_TRUE(success); |
| 280 } |
| 281 |
| 282 MockSensorDeviceManager* manager_; |
| 283 scoped_refptr<PlatformSensor> platform_sensor_; |
| 284 std::vector<scoped_refptr<PlatformSensor>> platform_sensor_vector_; |
| 285 base::MessageLoop message_loop_; |
| 286 std::unique_ptr<base::RunLoop> run_loop_; |
| 287 PlatformSensorProviderLinux* provider_; |
| 288 // Holds base dir where a sensor dir is located. |
| 289 base::ScopedTempDir sensors_dir_; |
| 290 }; |
| 291 |
| 292 // Tests sensor is not returned if not implemented. |
| 293 TEST_F(PlatformSensorAndProviderLinuxTest, SensorIsNotImplemented) { |
| 294 double sensor_value[3] = {5}; |
| 295 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 296 sensor_value); |
| 297 SetServiceStart(); |
| 298 EXPECT_FALSE(CreateSensor(SensorType::PROXIMITY)); |
| 299 } |
| 300 |
| 301 // Tests sensor is not returned if not supported by hardware. |
| 302 TEST_F(PlatformSensorAndProviderLinuxTest, SensorIsNotSupported) { |
| 303 double sensor_value[3] = {5}; |
| 304 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 305 sensor_value); |
| 306 SetServiceStart(); |
| 307 EXPECT_FALSE(CreateSensor(SensorType::ACCELEROMETER)); |
| 308 } |
| 309 |
| 310 // Tests sensor is returned if supported. |
| 311 TEST_F(PlatformSensorAndProviderLinuxTest, SensorIsSupported) { |
| 312 double sensor_value[3] = {5}; |
| 313 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 314 sensor_value); |
| 315 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 316 SetServiceStart(); |
| 317 |
| 318 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 319 EXPECT_TRUE(sensor); |
| 320 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor->GetType()); |
| 321 } |
| 322 |
| 323 // Tests that PlatformSensor::StartListening fails when provided reporting |
| 324 // frequency is above hardware capabilities. |
| 325 TEST_F(PlatformSensorAndProviderLinuxTest, StartFails) { |
| 326 double sensor_value[3] = {5}; |
| 327 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 328 sensor_value); |
| 329 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 330 SetServiceStart(); |
| 331 |
| 332 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 333 EXPECT_TRUE(sensor); |
| 334 |
| 335 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 336 PlatformSensorConfiguration configuration(10); |
| 337 EXPECT_FALSE(sensor->StartListening(client.get(), configuration)); |
| 338 } |
| 339 |
| 340 // Tests that PlatformSensor::StartListening succeeds and notification about |
| 341 // modified sensor reading is sent to the PlatformSensor::Client interface. |
| 342 TEST_F(PlatformSensorAndProviderLinuxTest, SensorStarted) { |
| 343 double sensor_value[3] = {5}; |
| 344 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 345 sensor_value); |
| 346 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 347 SetServiceStart(); |
| 348 |
| 349 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 350 EXPECT_TRUE(sensor); |
| 351 |
| 352 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 353 PlatformSensorConfiguration configuration(5); |
| 354 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 355 WaitOnSensorReadingChangedEvent(client.get()); |
| 356 EXPECT_TRUE(sensor->StopListening(client.get(), configuration)); |
| 357 } |
| 358 |
| 359 // Tests that OnSensorError is called when sensor is disconnected. |
| 360 TEST_F(PlatformSensorAndProviderLinuxTest, SensorRemoved) { |
| 361 double sensor_value[3] = {1}; |
| 362 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 363 sensor_value); |
| 364 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 365 SetServiceStart(); |
| 366 |
| 367 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 368 EXPECT_TRUE(sensor); |
| 369 |
| 370 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 371 PlatformSensorConfiguration configuration(5); |
| 372 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 373 GenerateDeviceRemovedEvent(sensors_dir_.GetPath()); |
| 374 WaitOnSensorErrorEvent(client.get()); |
| 375 } |
| 376 |
| 377 // Tests that sensor is not returned if not connected and |
| 378 // is created after it has been added. |
| 379 TEST_F(PlatformSensorAndProviderLinuxTest, SensorAddedAndRemoved) { |
| 380 double sensor_value[3] = {1, 2, 4}; |
| 381 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 382 sensor_value); |
| 383 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 384 SetServiceStart(); |
| 385 |
| 386 auto als_sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 387 EXPECT_TRUE(als_sensor); |
| 388 auto gyro_sensor = CreateSensor(SensorType::GYROSCOPE); |
| 389 EXPECT_FALSE(gyro_sensor); |
| 390 |
| 391 InitializeSupportedSensor(SensorType::GYROSCOPE, kGyroscopeFrequencyValue, |
| 392 kGyroscopeOffsetValue, kGyroscopeScalingValue, |
| 393 sensor_value); |
| 394 udev_device* dev = nullptr; |
| 395 manager_->DeviceAdded(dev /* not used */); |
| 396 base::RunLoop().RunUntilIdle(); |
| 397 gyro_sensor = CreateSensor(SensorType::GYROSCOPE); |
| 398 EXPECT_TRUE(gyro_sensor); |
| 399 EXPECT_EQ(gyro_sensor->GetType(), SensorType::GYROSCOPE); |
| 400 } |
| 401 |
| 402 // Checks the main fields of all sensors and initialized right. |
| 403 TEST_F(PlatformSensorAndProviderLinuxTest, CheckAllSupportedSensors) { |
| 404 double sensor_value[3] = {1, 2, 3}; |
| 405 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 406 sensor_value); |
| 407 InitializeSupportedSensor( |
| 408 SensorType::ACCELEROMETER, kAccelerometerFrequencyValue, |
| 409 kAccelerometerOffsetValue, kAccelerometerScalingValue, sensor_value); |
| 410 InitializeSupportedSensor(SensorType::GYROSCOPE, kGyroscopeFrequencyValue, |
| 411 kGyroscopeOffsetValue, kGyroscopeScalingValue, |
| 412 sensor_value); |
| 413 InitializeSupportedSensor( |
| 414 SensorType::MAGNETOMETER, kMagnetometerFrequencyValue, |
| 415 kMagnetometerOffsetValue, kMagnetometerScalingValue, sensor_value); |
| 416 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 417 SetServiceStart(); |
| 418 |
| 419 auto als_sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 420 EXPECT_TRUE(als_sensor); |
| 421 EXPECT_EQ(als_sensor->GetType(), SensorType::AMBIENT_LIGHT); |
| 422 EXPECT_THAT(als_sensor->GetDefaultConfiguration().frequency(), |
| 423 kDefaultAmbientLightFrequencyHz); |
| 424 |
| 425 auto accel_sensor = CreateSensor(SensorType::ACCELEROMETER); |
| 426 EXPECT_TRUE(accel_sensor); |
| 427 EXPECT_EQ(accel_sensor->GetType(), SensorType::ACCELEROMETER); |
| 428 EXPECT_THAT(accel_sensor->GetDefaultConfiguration().frequency(), |
| 429 kAccelerometerFrequencyValue); |
| 430 |
| 431 auto gyro_sensor = CreateSensor(SensorType::GYROSCOPE); |
| 432 EXPECT_TRUE(gyro_sensor); |
| 433 EXPECT_EQ(gyro_sensor->GetType(), SensorType::GYROSCOPE); |
| 434 EXPECT_THAT(gyro_sensor->GetDefaultConfiguration().frequency(), |
| 435 kGyroscopeFrequencyValue); |
| 436 |
| 437 auto magn_sensor = CreateSensor(SensorType::MAGNETOMETER); |
| 438 EXPECT_TRUE(magn_sensor); |
| 439 EXPECT_EQ(magn_sensor->GetType(), SensorType::MAGNETOMETER); |
| 440 EXPECT_THAT(magn_sensor->GetDefaultConfiguration().frequency(), |
| 441 kMagnetometerFrequencyValue); |
| 442 } |
| 443 |
| 444 // Tests that GetMaximumSupportedFrequency provides correct value. |
| 445 TEST_F(PlatformSensorAndProviderLinuxTest, GetMaximumSupportedFrequency) { |
| 446 double sensor_value[3] = {5}; |
| 447 InitializeSupportedSensor( |
| 448 SensorType::ACCELEROMETER, kAccelerometerFrequencyValue, |
| 449 kAccelerometerOffsetValue, kAccelerometerScalingValue, sensor_value); |
| 450 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 451 SetServiceStart(); |
| 452 |
| 453 auto sensor = CreateSensor(SensorType::ACCELEROMETER); |
| 454 EXPECT_TRUE(sensor); |
| 455 EXPECT_THAT(sensor->GetMaximumSupportedFrequency(), |
| 456 kAccelerometerFrequencyValue); |
| 457 } |
| 458 |
| 459 // Tests that GetMaximumSupportedFrequency provides correct value when |
| 460 // OS does not provide any information about frequency. |
| 461 TEST_F(PlatformSensorAndProviderLinuxTest, |
| 462 GetMaximumSupportedFrequencyDefault) { |
| 463 double sensor_value[3] = {5}; |
| 464 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 465 sensor_value); |
| 466 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 467 SetServiceStart(); |
| 468 |
| 469 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 470 EXPECT_TRUE(sensor); |
| 471 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor->GetType()); |
| 472 EXPECT_THAT(sensor->GetMaximumSupportedFrequency(), |
| 473 kDefaultAmbientLightFrequencyHz); |
| 474 } |
| 475 |
| 476 // Tests that Ambient Light sensor is correctly read. |
| 477 TEST_F(PlatformSensorAndProviderLinuxTest, CheckAmbientLightReadings) { |
| 478 mojo::ScopedSharedBufferHandle handle = provider_->CloneSharedBufferHandle(); |
| 479 mojo::ScopedSharedBufferMapping mapping = handle->MapAtOffset( |
| 480 sizeof(SensorReadingSharedBuffer), |
| 481 SensorReadingSharedBuffer::GetOffset(SensorType::AMBIENT_LIGHT)); |
| 482 |
| 483 double sensor_value[3] = {22}; |
| 484 InitializeSupportedSensor(SensorType::AMBIENT_LIGHT, kZero, kZero, kZero, |
| 485 sensor_value); |
| 486 |
| 487 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 488 SetServiceStart(); |
| 489 |
| 490 auto sensor = CreateSensor(SensorType::AMBIENT_LIGHT); |
| 491 EXPECT_TRUE(sensor); |
| 492 EXPECT_EQ(sensor->GetReportingMode(), mojom::ReportingMode::ON_CHANGE); |
| 493 |
| 494 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 495 PlatformSensorConfiguration configuration( |
| 496 sensor->GetMaximumSupportedFrequency()); |
| 497 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 498 WaitOnSensorReadingChangedEvent(client.get()); |
| 499 EXPECT_TRUE(sensor->StopListening(client.get(), configuration)); |
| 500 |
| 501 SensorReadingSharedBuffer* buffer = |
| 502 static_cast<SensorReadingSharedBuffer*>(mapping.get()); |
| 503 EXPECT_THAT(buffer->reading.values[0], sensor_value[0]); |
| 504 } |
| 505 |
| 506 // Tests that Accelerometer readings are correctly converted. |
| 507 TEST_F(PlatformSensorAndProviderLinuxTest, |
| 508 CheckAccelerometerReadingConversion) { |
| 509 mojo::ScopedSharedBufferHandle handle = provider_->CloneSharedBufferHandle(); |
| 510 mojo::ScopedSharedBufferMapping mapping = handle->MapAtOffset( |
| 511 sizeof(SensorReadingSharedBuffer), |
| 512 SensorReadingSharedBuffer::GetOffset(SensorType::ACCELEROMETER)); |
| 513 |
| 514 double sensor_values[3] = {4.5, -2.45, -3.29}; |
| 515 InitializeSupportedSensor( |
| 516 SensorType::ACCELEROMETER, kAccelerometerFrequencyValue, |
| 517 kAccelerometerOffsetValue, kAccelerometerScalingValue, sensor_values); |
| 518 |
| 519 // As long as WaitOnSensorReadingChangedEvent() waits until client gets a |
| 520 // a notification about readings changed, the frequency file must be deleted |
| 521 // to make the sensor device manager identify this sensor with ON_CHANGE |
| 522 // reporting mode. This will allow the MockPlatformSensorClient to |
| 523 // receive a notification and test if reading values are right. Otherwise |
| 524 // the test will not know when data is ready. |
| 525 SensorPathsLinux data; |
| 526 EXPECT_TRUE(InitSensorData(SensorType::ACCELEROMETER, &data)); |
| 527 base::FilePath frequency_file = base::FilePath(sensors_dir_.GetPath()) |
| 528 .Append(data.sensor_frequency_file_name); |
| 529 DeleteFile(frequency_file); |
| 530 |
| 531 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 532 SetServiceStart(); |
| 533 |
| 534 auto sensor = CreateSensor(SensorType::ACCELEROMETER); |
| 535 EXPECT_TRUE(sensor); |
| 536 // The reporting mode is ON_CHANGE only for this test. |
| 537 EXPECT_EQ(sensor->GetReportingMode(), mojom::ReportingMode::ON_CHANGE); |
| 538 |
| 539 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 540 PlatformSensorConfiguration configuration(10); |
| 541 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 542 WaitOnSensorReadingChangedEvent(client.get()); |
| 543 EXPECT_TRUE(sensor->StopListening(client.get(), configuration)); |
| 544 |
| 545 SensorReadingSharedBuffer* buffer = |
| 546 static_cast<SensorReadingSharedBuffer*>(mapping.get()); |
| 547 #if defined(OS_CHROMEOS) |
| 548 double scaling = kMeanGravity / kAccelerometerScalingValue; |
| 549 EXPECT_THAT(buffer->reading.values[0], scaling * sensor_values[0]); |
| 550 EXPECT_THAT(buffer->reading.values[1], scaling * sensor_values[1]); |
| 551 EXPECT_THAT(buffer->reading.values[2], scaling * sensor_values[2]); |
| 552 #else |
| 553 double scaling = kAccelerometerScalingValue + kAccelerometerOffsetValue; |
| 554 EXPECT_THAT(buffer->reading.values[0], -scaling * sensor_values[0]); |
| 555 EXPECT_THAT(buffer->reading.values[1], -scaling * sensor_values[1]); |
| 556 EXPECT_THAT(buffer->reading.values[2], -scaling * sensor_values[2]); |
| 557 #endif |
| 558 } |
| 559 |
| 560 // Tests that Gyroscope readings are correctly converted. |
| 561 TEST_F(PlatformSensorAndProviderLinuxTest, CheckGyroscopeReadingConversion) { |
| 562 mojo::ScopedSharedBufferHandle handle = provider_->CloneSharedBufferHandle(); |
| 563 mojo::ScopedSharedBufferMapping mapping = handle->MapAtOffset( |
| 564 sizeof(SensorReadingSharedBuffer), |
| 565 SensorReadingSharedBuffer::GetOffset(SensorType::GYROSCOPE)); |
| 566 |
| 567 double sensor_values[3] = {2.2, -3.8, -108.7}; |
| 568 InitializeSupportedSensor(SensorType::GYROSCOPE, kGyroscopeFrequencyValue, |
| 569 kGyroscopeOffsetValue, kGyroscopeScalingValue, |
| 570 sensor_values); |
| 571 |
| 572 // As long as WaitOnSensorReadingChangedEvent() waits until client gets a |
| 573 // a notification about readings changed, the frequency file must be deleted |
| 574 // to make the sensor device manager identify this sensor with ON_CHANGE |
| 575 // reporting mode. This will allow the MockPlatformSensorClient to |
| 576 // receive a notification and test if reading values are right. Otherwise |
| 577 // the test will not know when data is ready. |
| 578 SensorPathsLinux data; |
| 579 EXPECT_TRUE(InitSensorData(SensorType::GYROSCOPE, &data)); |
| 580 base::FilePath frequency_file = base::FilePath(sensors_dir_.GetPath()) |
| 581 .Append(data.sensor_frequency_file_name); |
| 582 DeleteFile(frequency_file); |
| 583 |
| 584 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 585 SetServiceStart(); |
| 586 |
| 587 auto sensor = CreateSensor(SensorType::GYROSCOPE); |
| 588 EXPECT_TRUE(sensor); |
| 589 // The reporting mode is ON_CHANGE only for this test. |
| 590 EXPECT_EQ(sensor->GetReportingMode(), mojom::ReportingMode::ON_CHANGE); |
| 591 |
| 592 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 593 PlatformSensorConfiguration configuration(10); |
| 594 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 595 WaitOnSensorReadingChangedEvent(client.get()); |
| 596 EXPECT_TRUE(sensor->StopListening(client.get(), configuration)); |
| 597 |
| 598 SensorReadingSharedBuffer* buffer = |
| 599 static_cast<SensorReadingSharedBuffer*>(mapping.get()); |
| 600 #if defined(OS_CHROMEOS) |
| 601 double scaling = |
| 602 kMeanGravity * kRadiansInDegreesPerSecond / kGyroscopeScalingValue; |
| 603 EXPECT_THAT(buffer->reading.values[0], -scaling * sensor_values[0]); |
| 604 EXPECT_THAT(buffer->reading.values[1], -scaling * sensor_values[1]); |
| 605 EXPECT_THAT(buffer->reading.values[2], -scaling * sensor_values[2]); |
| 606 #else |
| 607 double scaling = kGyroscopeScalingValue + kGyroscopeOffsetValue; |
| 608 EXPECT_THAT(buffer->reading.values[0], scaling * sensor_values[0]); |
| 609 EXPECT_THAT(buffer->reading.values[1], scaling * sensor_values[1]); |
| 610 EXPECT_THAT(buffer->reading.values[2], scaling * sensor_values[2]); |
| 611 #endif |
| 612 } |
| 613 |
| 614 // Tests that Magnetometer readings are correctly converted. |
| 615 TEST_F(PlatformSensorAndProviderLinuxTest, CheckMagnetometerReadingConversion) { |
| 616 mojo::ScopedSharedBufferHandle handle = provider_->CloneSharedBufferHandle(); |
| 617 mojo::ScopedSharedBufferMapping mapping = handle->MapAtOffset( |
| 618 sizeof(SensorReadingSharedBuffer), |
| 619 SensorReadingSharedBuffer::GetOffset(SensorType::MAGNETOMETER)); |
| 620 |
| 621 double sensor_values[3] = {2.2, -3.8, -108.7}; |
| 622 InitializeSupportedSensor( |
| 623 SensorType::MAGNETOMETER, kMagnetometerFrequencyValue, |
| 624 kMagnetometerOffsetValue, kMagnetometerScalingValue, sensor_values); |
| 625 |
| 626 // As long as WaitOnSensorReadingChangedEvent() waits until client gets a |
| 627 // a notification about readings changed, the frequency file must be deleted |
| 628 // to make the sensor device manager identify this sensor with ON_CHANGE |
| 629 // reporting mode. This will allow the MockPlatformSensorClient to |
| 630 // receive a notification and test if reading values are right. Otherwise |
| 631 // the test will not know when data is ready. |
| 632 SensorPathsLinux data; |
| 633 EXPECT_TRUE(InitSensorData(SensorType::MAGNETOMETER, &data)); |
| 634 base::FilePath frequency_file = base::FilePath(sensors_dir_.GetPath()) |
| 635 .Append(data.sensor_frequency_file_name); |
| 636 DeleteFile(frequency_file); |
| 637 |
| 638 InitializeMockUdevMethods(sensors_dir_.GetPath()); |
| 639 SetServiceStart(); |
| 640 |
| 641 auto sensor = CreateSensor(SensorType::MAGNETOMETER); |
| 642 EXPECT_TRUE(sensor); |
| 643 // The reporting mode is ON_CHANGE only for this test. |
| 644 EXPECT_EQ(sensor->GetReportingMode(), mojom::ReportingMode::ON_CHANGE); |
| 645 |
| 646 auto client = base::MakeUnique<NiceMock<MockPlatformSensorClient>>(sensor); |
| 647 PlatformSensorConfiguration configuration(10); |
| 648 EXPECT_TRUE(sensor->StartListening(client.get(), configuration)); |
| 649 WaitOnSensorReadingChangedEvent(client.get()); |
| 650 EXPECT_TRUE(sensor->StopListening(client.get(), configuration)); |
| 651 |
| 652 SensorReadingSharedBuffer* buffer = |
| 653 static_cast<SensorReadingSharedBuffer*>(mapping.get()); |
| 654 double scaling = (kMagnetometerScalingValue + kMagnetometerOffsetValue) * |
| 655 kMicroteslaInGauss; |
| 656 EXPECT_THAT(buffer->reading.values[0], scaling * sensor_values[0]); |
| 657 EXPECT_THAT(buffer->reading.values[1], scaling * sensor_values[1]); |
| 658 EXPECT_THAT(buffer->reading.values[2], scaling * sensor_values[2]); |
| 659 } |
| 660 |
| 661 } // namespace device |
OLD | NEW |