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
ash / sensor_info / sensor_provider_unittest.cc [blame]
// Copyright 2023 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/sensor_info/sensor_provider.h"
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include <vector>
#include "ash/accelerometer/accelerometer_constants.h"
#include "ash/sensor_info/sensor_types.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 "chromeos/components/sensors/mojom/sensor.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
using ::chromeos::sensors::mojom::DeviceType;
namespace {
constexpr double kFakeScaleValue = 10.0;
constexpr int kFakeLidAccelerometerId = 1;
constexpr int kFakeBaseAccelerometerId = 2;
constexpr int kFakeBaseGyroscopeId = 3;
constexpr int kFakeLidAngleId = 4;
constexpr int64_t kFakeSampleData[] = {1, 2, 3};
class FakeObserver : public SensorObserver {
public:
void OnSensorUpdated(const SensorUpdate& update) override {
for (int index = 0; index < static_cast<int>(SensorType::kSensorTypeCount);
++index) {
auto source = static_cast<SensorType>(index);
if (!update.has(source)) {
continue;
}
if (source != SensorType::kLidAngle) {
EXPECT_DOUBLE_EQ(update.get(source)->x,
kFakeSampleData[0] * kFakeScaleValue);
EXPECT_DOUBLE_EQ(update.get(source)->y,
kFakeSampleData[1] * kFakeScaleValue);
EXPECT_DOUBLE_EQ(update.get(source)->z,
kFakeSampleData[2] * kFakeScaleValue);
} else {
EXPECT_DOUBLE_EQ(update.get(source)->x, kFakeSampleData[0]);
}
}
update_ = update;
}
SensorUpdate update_;
};
} // namespace
class SensorProviderTest : public testing::Test {
protected:
void SetUp() override {
chromeos::sensors::SensorHalDispatcher::Initialize();
sensor_hal_server_ =
std::make_unique<chromeos::sensors::FakeSensorHalServer>();
provider_ = std::make_unique<SensorProvider>();
provider_->AddObserver(&observer_);
}
void TearDown() override {
chromeos::sensors::SensorHalDispatcher::Shutdown();
}
void AddDevice(int32_t iio_device_id,
std::set<DeviceType> types,
std::optional<std::string> scale,
std::optional<std::string> location) {
std::vector<chromeos::sensors::FakeSensorDevice::ChannelData> channels_data;
int size = 0;
if (base::Contains(types, DeviceType::ANGL)) {
channels_data.resize(1);
channels_data[0].id = "angl";
channels_data[0].sample_data = 1;
channels_data[0].attrs["raw"] = "1";
}
if (base::Contains(types, DeviceType::ACCEL)) {
size += kNumberOfAxes;
channels_data.resize(size);
for (uint32_t i = 0; i < kNumberOfAxes; ++i) {
channels_data[size - kNumberOfAxes + i].id = kAccelerometerChannels[i];
channels_data[size - kNumberOfAxes + i].sample_data =
kFakeSampleData[i];
}
}
if (base::Contains(types, DeviceType::ANGLVEL)) {
size += kNumberOfAxes;
channels_data.resize(size);
for (uint32_t i = 0; i < kNumberOfAxes; ++i) {
channels_data[size - kNumberOfAxes + i].id = kGyroscopeChannels[i];
channels_data[size - kNumberOfAxes + i].sample_data =
kFakeSampleData[i];
}
}
std::unique_ptr<chromeos::sensors::FakeSensorDevice> sensor_device(
std::make_unique<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_[iio_device_id] = sensor_device.get();
mojo::Remote<chromeos::sensors::mojom::SensorDevice> sensor;
sensor_device->AddReceiver(sensor.BindNewPipeAndPassReceiver());
sensor_hal_server_->GetSensorService()->SetDevice(
iio_device_id, std::move(types), std::move(sensor_device));
}
void StartConnection() {
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterServer(
sensor_hal_server_->PassRemote());
}
base::test::SingleThreadTaskEnvironment task_environment;
FakeObserver observer_;
std::unique_ptr<chromeos::sensors::FakeSensorHalServer> sensor_hal_server_;
std::map<int32_t,
raw_ptr<chromeos::sensors::FakeSensorDevice, CtnExperimental>>
sensor_devices_;
std::unique_ptr<SensorProvider> provider_;
};
TEST_F(SensorProviderTest, CheckNoScale) {
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
std::nullopt, kLocationStrings[1]);
StartConnection();
provider_->EnableSensorReading();
// Wait until all tasks done and no samples updated.
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{false, false, false, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_FALSE(observer_.update_.has(SensorType::kAccelerometerBase));
}
TEST_F(SensorProviderTest, CheckNoLocation) {
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), std::nullopt);
StartConnection();
provider_->EnableSensorReading();
// Wait until all tasks done and no samples updated.
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{false, false, false, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_FALSE(observer_.update_.has(SensorType::kAccelerometerLid));
EXPECT_FALSE(observer_.update_.has(SensorType::kAccelerometerBase));
}
TEST_F(SensorProviderTest, GetSamplesOfLidAccel) {
AddDevice(kFakeLidAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[0]);
StartConnection();
provider_->EnableSensorReading();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{false, false, true, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_TRUE(sensor_hal_server_->GetSensorService()->HasReceivers());
EXPECT_TRUE(base::Contains(sensor_devices_, kFakeLidAccelerometerId));
EXPECT_TRUE(sensor_devices_[kFakeLidAccelerometerId]->HasReceivers());
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerLid));
}
TEST_F(SensorProviderTest, GetSamplesOfLidAngleAndLidAccel) {
AddDevice(kFakeLidAngleId, std::set<DeviceType>{DeviceType::ANGL},
base::NumberToString(kFakeScaleValue), kLocationStrings[0]);
// LidAngle sample update is generated by reading a sensor property, which
// causes endless SensorsSamples updates if LidAngle is the only present
// sensor. It is OK in production but not good for this test. So we add a
// AccelerometerLid to let SensorProvider generate only one update.
AddDevice(kFakeLidAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[0]);
StartConnection();
provider_->EnableSensorReading();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{true, false, true, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_TRUE(observer_.update_.has(SensorType::kLidAngle));
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerLid));
}
TEST_F(SensorProviderTest, GetSamplesOfBaseGyroscope) {
AddDevice(kFakeBaseGyroscopeId, std::set<DeviceType>{DeviceType::ANGLVEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
StartConnection();
provider_->EnableSensorReading();
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{false, false, false, true, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_TRUE(observer_.update_.has(SensorType::kGyroscopeBase));
}
TEST_F(SensorProviderTest, GetSamplesOfBaseGyroscopeAndBaseAccel) {
// Creates device: GyroscopeBase and AccelerometerBase.
AddDevice(kFakeBaseGyroscopeId, std::set<DeviceType>{DeviceType::ANGLVEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
StartConnection();
provider_->EnableSensorReading();
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected1{false, true, false, true, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected1);
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerBase));
EXPECT_TRUE(observer_.update_.has(SensorType::kGyroscopeBase));
// Triggering SensorProvider::OnSensorServiceDisconnect.
sensor_hal_server_->GetSensorService()->ClearReceivers();
sensor_hal_server_->OnServerDisconnect();
// Wait until the disconnect arrives at the dispatcher.
base::RunLoop().RunUntilIdle();
// Overwriting with invalid AccelerometerBase.
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
std::nullopt, std::nullopt);
StartConnection();
// Wait until the re-initialization done.
base::RunLoop().RunUntilIdle();
std::vector<bool> expected2{false, false, false, true, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected2);
EXPECT_FALSE(observer_.update_.has(SensorType::kAccelerometerBase));
EXPECT_TRUE(observer_.update_.has(SensorType::kGyroscopeBase));
}
TEST_F(SensorProviderTest, AddSensorsWhileSampling) {
StartConnection();
provider_->EnableSensorReading();
// Wait until a sample is received.
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected1{false, false, false, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected1);
// New device: AccelerometerBase.
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
// Wait until all setups are finished and no samples updated.
base::RunLoop().RunUntilIdle();
std::vector<bool> expected2{false, true, false, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected2);
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerBase));
// New device: AccelerometerLid. Disconnect AccelerometerBase and reconnect.
AddDevice(kFakeLidAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[0]);
// 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 AccelerometerBase with valid device.
AddDevice(kFakeBaseAccelerometerId, std::set<DeviceType>{DeviceType::ACCEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
// Wait until the disconnection and the re-initialization are done.
base::RunLoop().RunUntilIdle();
std::vector<bool> expected3{false, true, true, false, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected3);
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerLid));
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerBase));
}
TEST_F(SensorProviderTest, GetSamplesOfAccelGyroDevices) {
// New device: GyroscopeBase and AccelerometerBase.
AddDevice(kFakeBaseGyroscopeId,
std::set<DeviceType>{DeviceType::ACCEL, DeviceType::ANGLVEL},
base::NumberToString(kFakeScaleValue), kLocationStrings[1]);
StartConnection();
provider_->EnableSensorReading();
base::RunLoop().RunUntilIdle();
// This vector is the state vector for {LidAngle, AccelerometerBase,
// AccelerometerLid, GyroscopeBase, GyroscopeLid} sensors, where true
// meaning the sensor detected and false means the sensor not detected.
std::vector<bool> expected{false, true, false, true, false};
EXPECT_EQ(provider_->GetStateForTesting(), expected);
EXPECT_TRUE(observer_.update_.has(SensorType::kAccelerometerBase));
EXPECT_TRUE(observer_.update_.has(SensorType::kGyroscopeBase));
}
} // namespace ash