Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: device/generic_sensor/platform_sensor_and_provider_unittest_linux.cc

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

Powered by Google App Engine
This is Rietveld 408576698