1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
ash / accelerometer / accelerometer_provider_mojo_unittest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/raw_ptr.h"
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "ash/accelerometer/accelerometer_provider_mojo.h"
#include <memory>
#include <utility>
#include "ash/accelerometer/accelerometer_constants.h"
#include "ash/accelerometer/accelerometer_reader.h"
#include "ash/test/ash_test_helper.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "chromeos/components/sensors/ash/sensor_hal_dispatcher.h"
#include "chromeos/components/sensors/fake_sensor_device.h"
#include "chromeos/components/sensors/fake_sensor_hal_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
constexpr double kFakeScaleValue = 10.0;
constexpr int kFakeLidAccelerometerId = 1;
constexpr int kFakeBaseAccelerometerId = 2;
constexpr int kFakeLidAngleId = 3;
constexpr int64_t kFakeSampleData[] = {1, 2, 3};
class FakeObserver : public AccelerometerReader::Observer {
public:
void OnECLidAngleDriverStatusChanged(bool is_supported) override {
is_supported_ = is_supported;
}
void OnAccelerometerUpdated(const AccelerometerUpdate& update) override {
for (uint32_t index = 0; index < ACCELEROMETER_SOURCE_COUNT; ++index) {
auto source = static_cast<AccelerometerSource>(index);
if (!update.has(source))
continue;
EXPECT_EQ(update.get(source).x, kFakeSampleData[0] * kFakeScaleValue);
EXPECT_EQ(update.get(source).y, kFakeSampleData[1] * kFakeScaleValue);
EXPECT_EQ(update.get(source).z, kFakeSampleData[2] * kFakeScaleValue);
}
update_ = update;
}
std::optional<bool> is_supported_;
AccelerometerUpdate update_;
};
} // namespace
class AccelerometerProviderMojoTest : public ::testing::Test {
protected:
void SetUp() override {
ash::AshTestHelper::InitParams init_params;
init_params.start_session = false;
ash_test_helper_.SetUp(std::move(init_params));
sensor_hal_server_ =
std::make_unique<chromeos::sensors::FakeSensorHalServer>();
provider_ = new AccelerometerProviderMojo();
chromeos::sensors::SensorHalDispatcher::Initialize();
provider_->PrepareAndInitialize();
provider_->AddObserver(&observer_);
}
void TearDown() override {
chromeos::sensors::SensorHalDispatcher::Shutdown();
}
void AddDevice(int32_t iio_device_id,
chromeos::sensors::mojom::DeviceType type,
std::optional<std::string> scale,
std::optional<std::string> location) {
std::set<chromeos::sensors::mojom::DeviceType> types;
types.emplace(type);
std::vector<chromeos::sensors::FakeSensorDevice::ChannelData> channels_data;
if (type == chromeos::sensors::mojom::DeviceType::ACCEL) {
channels_data.resize(kNumberOfAxes);
for (uint32_t i = 0; i < kNumberOfAxes; ++i) {
channels_data[i].id = kAccelerometerChannels[i];
channels_data[i].sample_data = kFakeSampleData[i];
}
}
std::unique_ptr<chromeos::sensors::FakeSensorDevice> sensor_device(
new chromeos::sensors::FakeSensorDevice(std::move(channels_data)));
if (scale.has_value())
sensor_device->SetAttribute(chromeos::sensors::mojom::kScale,
scale.value());
if (location.has_value())
sensor_device->SetAttribute(chromeos::sensors::mojom::kLocation,
location.value());
sensor_devices_.emplace(iio_device_id, sensor_device.get());
sensor_hal_server_->GetSensorService()->SetDevice(
iio_device_id, std::move(types), std::move(sensor_device));
}
void AddLidAccelerometer() {
AddDevice(kFakeLidAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_SCREEN]);
}
void TriggerNewDevicesTimeout() {
provider_->OnNewDevicesTimeout();
// Wait until task |OnECLidAngleDriverStatusChanged| arrives at |observer_|.
base::RunLoop().RunUntilIdle();
}
void TriggerSamples() {
// Simulate a disconnection of IIO Service.
sensor_hal_server_->GetSensorService()->ClearReceivers();
sensor_hal_server_->OnServerDisconnect();
// Wait until the disconnect arrives at the dispatcher.
base::RunLoop().RunUntilIdle();
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
}
FakeObserver observer_;
std::unique_ptr<chromeos::sensors::FakeSensorHalServer> sensor_hal_server_;
std::map<int32_t,
raw_ptr<chromeos::sensors::FakeSensorDevice, CtnExperimental>>
sensor_devices_;
scoped_refptr<AccelerometerProviderMojo> provider_;
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::MainThreadType::UI};
ash::AshTestHelper ash_test_helper_;
};
TEST_F(AccelerometerProviderMojoTest, CheckNoScale) {
AddLidAccelerometer();
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL, std::nullopt,
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until all tasks done and no samples updated.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID);
}
TEST_F(AccelerometerProviderMojoTest, CheckNoLocation) {
AddLidAccelerometer();
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue), std::nullopt);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until all tasks done and no samples updated.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID);
}
TEST_F(AccelerometerProviderMojoTest, GetSamplesOfOneAccel) {
AddLidAccelerometer();
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
TEST_F(AccelerometerProviderMojoTest, GetSamplesWithNoLidAngle) {
AddLidAccelerometer();
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID_BASE);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// Simulate a disconnection of the accelerometer's mojo channel in IIO
// Service.
sensor_devices_[kFakeLidAccelerometerId]->ClearReceivers();
// Wait until the disconnection is done.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(sensor_hal_server_->GetSensorService()->HasReceivers());
}
TEST_F(AccelerometerProviderMojoTest, GetSamplesWithLidAngle) {
AddLidAccelerometer();
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
AddDevice(kFakeLidAngleId, chromeos::sensors::mojom::DeviceType::ANGL,
std::nullopt, std::nullopt);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until all setups are finished and no samples updated.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_TRUE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL_LID);
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
provider_->TriggerRead();
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// Simulate a disconnection of IIO Service.
sensor_hal_server_->GetSensorService()->ClearReceivers();
sensor_hal_server_->OnServerDisconnect();
// Wait until the disconnect arrives at the dispatcher.
base::RunLoop().RunUntilIdle();
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
TEST_F(AccelerometerProviderMojoTest, GetSamplesOfNewDevices) {
// New device: lid-accelerometer.
AddLidAccelerometer();
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID);
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// New device: base-accelerometer.
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
TriggerSamples();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID_BASE);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// New device: EC Lid Angle Driver.
AddDevice(kFakeLidAngleId, chromeos::sensors::mojom::DeviceType::ANGL,
std::nullopt, std::nullopt);
TriggerSamples();
EXPECT_TRUE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL_LID);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
TEST_F(AccelerometerProviderMojoTest, NoSamplesFromBaseOnly) {
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(provider_->GetInitializationStateForTesting(),
MojoState::INITIALIZING);
EXPECT_FALSE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
// New device: base-accelerometer.
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
// Wait until all setups are finished and no samples updated.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::BASE);
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
// New device: lid-accelerometer.
AddLidAccelerometer();
TriggerSamples();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID_BASE);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// New device: EC Lid Angle Driver.
AddDevice(kFakeLidAngleId, chromeos::sensors::mojom::DeviceType::ANGL,
std::nullopt, std::nullopt);
TriggerSamples();
EXPECT_TRUE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL_LID);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
TEST_F(AccelerometerProviderMojoTest, NoSamplesFromLidAngle) {
// New device: EC Lid Angle Driver.
AddDevice(kFakeLidAngleId, chromeos::sensors::mojom::DeviceType::ANGL,
std::nullopt, std::nullopt);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until all setups are finished and no samples updated.
base::RunLoop().RunUntilIdle();
provider_->TriggerRead();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL);
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_TRUE(observer_.is_supported_.value());
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
// New device: base-accelerometer.
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
// Wait until all setups are finished and no samples updated.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL);
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
// New device: lid-accelerometer.
AddLidAccelerometer();
TriggerSamples();
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::ANGL_LID);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
TEST_F(AccelerometerProviderMojoTest, ResetStatesWithNoLidAngle) {
AddLidAccelerometer();
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ACCEL,
base::NumberToString(kFakeScaleValue),
kLocationStrings[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD]);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
// Wait until samples are received.
base::RunLoop().RunUntilIdle();
// Simulate timeout to check |ec_lid_angle_driver_status_|.
TriggerNewDevicesTimeout();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID_BASE);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
observer_.update_.Reset();
// Simulate a disconnection of the base accelerometer's mojo channel in IIO
// Service with the reason of DEVICE_REMOVED.
sensor_devices_[kFakeBaseAccelerometerId]->ClearReceiversWithReason(
chromeos::sensors::mojom::SensorDeviceDisconnectReason::DEVICE_REMOVED,
"Device was removed");
// Overwrite the base accelerometer in the iioservice.
AddDevice(kFakeBaseAccelerometerId,
chromeos::sensors::mojom::DeviceType::ANGLVEL, std::nullopt,
std::nullopt);
// Wait until the disconnection and the re-initialization are done.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer_.is_supported_.has_value());
EXPECT_FALSE(observer_.is_supported_.value());
EXPECT_EQ(provider_->GetInitializationStateForTesting(), MojoState::LID);
EXPECT_TRUE(observer_.update_.has(ACCELEROMETER_SOURCE_SCREEN));
EXPECT_FALSE(observer_.update_.has(ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD));
}
} // namespace ash