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

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

Issue 2865263002: Move //device/generic_sensor to be part of the internal implementation of the Device Service. (Closed)
Patch Set: code rebase Created 3 years, 7 months 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/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8
9 #include "device/generic_sensor/fake_platform_sensor.h"
10 #include "device/generic_sensor/fake_platform_sensor_provider.h"
11 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
12
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace device {
16
17 using mojom::SensorInitParams;
18 using mojom::SensorType;
19
20 class TestSensorCreateCallback {
21 public:
22 TestSensorCreateCallback()
23 : callback_(base::Bind(&TestSensorCreateCallback::SetResult,
24 base::Unretained(this))) {}
25
26 scoped_refptr<PlatformSensor> WaitForResult() {
27 run_loop_.Run();
28 scoped_refptr<PlatformSensor> sensor = sensor_;
29 sensor_ = nullptr;
30 return sensor;
31 }
32
33 const PlatformSensorProvider::CreateSensorCallback& callback() const {
34 return callback_;
35 }
36
37 private:
38 void SetResult(scoped_refptr<PlatformSensor> sensor) {
39 sensor_ = sensor;
40 run_loop_.Quit();
41 }
42
43 const PlatformSensorProvider::CreateSensorCallback callback_;
44 base::RunLoop run_loop_;
45 scoped_refptr<PlatformSensor> sensor_;
46 };
47
48 class PlatformSensorTestClient : public PlatformSensor::Client {
49 public:
50 PlatformSensorTestClient()
51 : notification_suspended_(false),
52 sensor_reading_changed_(false),
53 sensor_error_(false) {}
54
55 ~PlatformSensorTestClient() override {
56 if (sensor_)
57 sensor_->RemoveClient(this);
58 }
59
60 // PlatformSensor::Client override.
61 void OnSensorReadingChanged() override { sensor_reading_changed_ = true; }
62
63 void OnSensorError() override { sensor_error_ = true; }
64
65 bool IsNotificationSuspended() override { return notification_suspended_; }
66
67 void set_notification_suspended(bool value) {
68 notification_suspended_ = value;
69 }
70
71 void SetSensor(scoped_refptr<PlatformSensor> sensor) {
72 sensor_ = sensor;
73 sensor_->AddClient(this);
74 }
75
76 bool sensor_reading_changed() const { return sensor_reading_changed_; }
77
78 bool sensor_error() const { return sensor_error_; }
79
80 private:
81 scoped_refptr<PlatformSensor> sensor_;
82 bool notification_suspended_;
83 bool sensor_reading_changed_;
84 bool sensor_error_;
85 };
86
87 class PlatformSensorProviderTest : public ::testing::Test {
88 public:
89 PlatformSensorProviderTest()
90 : provider_(new FakePlatformSensorProvider()),
91 sensor_client_(new PlatformSensorTestClient()),
92 message_loop_(new base::MessageLoopForIO) {}
93
94 protected:
95 scoped_refptr<PlatformSensor> CreateSensor(
96 mojom::SensorType type,
97 TestSensorCreateCallback* callback) {
98 provider_->CreateSensor(type, callback->callback());
99 return callback->WaitForResult();
100 }
101
102 std::unique_ptr<FakePlatformSensorProvider> provider_;
103 std::unique_ptr<PlatformSensorTestClient> sensor_client_;
104 std::unique_ptr<base::MessageLoop> message_loop_;
105 };
106
107 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) {
108 {
109 TestSensorCreateCallback callback;
110 scoped_refptr<PlatformSensor> sensor =
111 CreateSensor(SensorType::AMBIENT_LIGHT, &callback);
112 EXPECT_TRUE(sensor);
113 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor->GetType());
114 }
115
116 {
117 TestSensorCreateCallback callback;
118 scoped_refptr<PlatformSensor> sensor =
119 CreateSensor(SensorType::PROXIMITY, &callback);
120 EXPECT_TRUE(sensor);
121 EXPECT_EQ(SensorType::PROXIMITY, sensor->GetType());
122 }
123
124 {
125 TestSensorCreateCallback callback;
126 scoped_refptr<PlatformSensor> sensor =
127 CreateSensor(SensorType::ACCELEROMETER, &callback);
128 EXPECT_TRUE(sensor);
129 EXPECT_EQ(SensorType::ACCELEROMETER, sensor->GetType());
130 }
131
132 {
133 TestSensorCreateCallback callback;
134 scoped_refptr<PlatformSensor> sensor =
135 CreateSensor(SensorType::LINEAR_ACCELERATION, &callback);
136 EXPECT_TRUE(sensor);
137 EXPECT_EQ(SensorType::LINEAR_ACCELERATION, sensor->GetType());
138 }
139
140 {
141 TestSensorCreateCallback callback;
142 scoped_refptr<PlatformSensor> sensor =
143 CreateSensor(SensorType::GYROSCOPE, &callback);
144 EXPECT_TRUE(sensor);
145 EXPECT_EQ(SensorType::GYROSCOPE, sensor->GetType());
146 }
147
148 {
149 TestSensorCreateCallback callback;
150 scoped_refptr<PlatformSensor> sensor =
151 CreateSensor(SensorType::MAGNETOMETER, &callback);
152 EXPECT_TRUE(sensor);
153 EXPECT_EQ(SensorType::MAGNETOMETER, sensor->GetType());
154 }
155
156 {
157 TestSensorCreateCallback callback;
158 scoped_refptr<PlatformSensor> sensor =
159 CreateSensor(SensorType::PRESSURE, &callback);
160 EXPECT_TRUE(sensor);
161 EXPECT_EQ(SensorType::PRESSURE, sensor->GetType());
162 }
163
164 {
165 TestSensorCreateCallback callback;
166 scoped_refptr<PlatformSensor> sensor =
167 CreateSensor(SensorType::ABSOLUTE_ORIENTATION, &callback);
168 EXPECT_TRUE(sensor);
169 EXPECT_EQ(SensorType::ABSOLUTE_ORIENTATION, sensor->GetType());
170 }
171
172 {
173 TestSensorCreateCallback callback;
174 scoped_refptr<PlatformSensor> sensor =
175 CreateSensor(SensorType::RELATIVE_ORIENTATION, &callback);
176 EXPECT_TRUE(sensor);
177 EXPECT_EQ(SensorType::RELATIVE_ORIENTATION, sensor->GetType());
178 }
179 }
180
181 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) {
182 // Create Ambient Light sensor.
183 TestSensorCreateCallback callback1;
184 scoped_refptr<PlatformSensor> sensor1 =
185 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1);
186 EXPECT_TRUE(sensor1);
187 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
188
189 // Try to get Gyroscope sensor, which has not been created yet.
190 scoped_refptr<PlatformSensor> sensor2 =
191 provider_->GetSensor(SensorType::GYROSCOPE);
192 EXPECT_FALSE(sensor2);
193
194 // Get Ambient Light sensor.
195 scoped_refptr<PlatformSensor> sensor3 =
196 provider_->GetSensor(SensorType::AMBIENT_LIGHT);
197 EXPECT_TRUE(sensor3);
198
199 EXPECT_EQ(sensor1->GetType(), sensor3->GetType());
200 }
201
202 TEST_F(PlatformSensorProviderTest, CreateAndRemoveSensors) {
203 TestSensorCreateCallback callback1;
204 scoped_refptr<PlatformSensor> sensor1 =
205 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1);
206 EXPECT_TRUE(sensor1);
207
208 TestSensorCreateCallback callback2;
209 scoped_refptr<PlatformSensor> sensor2 =
210 CreateSensor(SensorType::PROXIMITY, &callback2);
211 EXPECT_TRUE(sensor2);
212
213 EXPECT_TRUE(provider_->HasSensors());
214
215 EXPECT_CALL(*provider_, AllSensorsRemoved()).Times(1);
216
217 sensor1 = nullptr;
218 sensor2 = nullptr;
219
220 EXPECT_FALSE(provider_->HasSensors());
221 }
222
223 TEST_F(PlatformSensorProviderTest, TestSensorLeaks) {
224 // Create Ambient Light sensor.
225 TestSensorCreateCallback callback1;
226 scoped_refptr<PlatformSensor> sensor1 =
227 CreateSensor(SensorType::AMBIENT_LIGHT, &callback1);
228 EXPECT_TRUE(sensor1);
229 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
230
231 // Sensor should be automatically destroyed.
232 sensor1 = nullptr;
233 scoped_refptr<PlatformSensor> sensor2 =
234 provider_->GetSensor(SensorType::AMBIENT_LIGHT);
235 EXPECT_FALSE(sensor2);
236 }
237
238 // This test assumes that a mock sensor has a constant maximum frequency value
239 // of 50 hz (different from the base sensor class that has a range from 0 to
240 // 60) and tests whether a mock sensor can be started with a value range from 0
241 // to 60.
242 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) {
243 const double too_high_frequency = 60;
244 const double normal_frequency = 39;
245 TestSensorCreateCallback callback;
246 scoped_refptr<PlatformSensor> sensor =
247 CreateSensor(SensorType::AMBIENT_LIGHT, &callback);
248 FakePlatformSensor* fake_sensor =
249 static_cast<FakePlatformSensor*>(sensor.get());
250 EXPECT_TRUE(fake_sensor);
251 sensor_client_->SetSensor(sensor);
252
253 PlatformSensorConfiguration config(too_high_frequency);
254 EXPECT_EQ(too_high_frequency, config.frequency());
255 EXPECT_FALSE(fake_sensor->StartListening(sensor_client_.get(), config));
256 EXPECT_FALSE(fake_sensor->started());
257
258 config.set_frequency(normal_frequency);
259 EXPECT_EQ(normal_frequency, config.frequency());
260 EXPECT_TRUE(fake_sensor->StartListening(sensor_client_.get(), config));
261 EXPECT_TRUE(fake_sensor->started());
262
263 EXPECT_TRUE(fake_sensor->StopListening(sensor_client_.get(), config));
264 EXPECT_FALSE(fake_sensor->started());
265 }
266
267 // If a client is in a suspended mode, a NotifySensorReadingChanged()
268 // notification must not be sent to the client but NotifySensorError() must be.
269 TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) {
270 const int num = 5;
271 TestSensorCreateCallback callback;
272 scoped_refptr<PlatformSensor> sensor =
273 CreateSensor(SensorType::GYROSCOPE, &callback);
274 FakePlatformSensor* fake_sensor =
275 static_cast<FakePlatformSensor*>(sensor.get());
276
277 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
278 for (int i = 0; i < num; i++) {
279 std::unique_ptr<PlatformSensorTestClient> client(
280 new PlatformSensorTestClient());
281 client->SetSensor(fake_sensor);
282 clients.push_back(std::move(client));
283 }
284
285 clients.front()->set_notification_suspended(true);
286 fake_sensor->NotifySensorReadingChanged();
287 fake_sensor->NotifySensorError();
288 for (auto const& client : clients) {
289 if (client == clients.front()) {
290 EXPECT_FALSE(client->sensor_reading_changed());
291 EXPECT_TRUE(client->sensor_error());
292 continue;
293 }
294 EXPECT_TRUE(client->sensor_reading_changed());
295 EXPECT_TRUE(client->sensor_error());
296 }
297
298 clients.front()->set_notification_suspended(false);
299 fake_sensor->NotifySensorReadingChanged();
300 fake_sensor->NotifySensorError();
301 for (auto const& client : clients) {
302 EXPECT_TRUE(client->sensor_reading_changed());
303 EXPECT_TRUE(client->sensor_error());
304 }
305 }
306
307 // Tests that when all clients are removed, config maps are removed as well.
308 TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) {
309 const int num = 5;
310
311 TestSensorCreateCallback callback;
312 scoped_refptr<PlatformSensor> sensor =
313 CreateSensor(SensorType::AMBIENT_LIGHT, &callback);
314 FakePlatformSensor* fake_sensor =
315 static_cast<FakePlatformSensor*>(sensor.get());
316 EXPECT_TRUE(fake_sensor->config_map().empty());
317
318 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
319 PlatformSensorConfiguration config(30);
320 for (int i = 0; i < num; i++) {
321 std::unique_ptr<PlatformSensorTestClient> client(
322 new PlatformSensorTestClient());
323 client->SetSensor(fake_sensor);
324 EXPECT_TRUE(fake_sensor->StartListening(client.get(), config));
325 EXPECT_TRUE(fake_sensor->started());
326
327 clients.push_back(std::move(client));
328 }
329 EXPECT_FALSE(fake_sensor->config_map().empty());
330
331 for (const auto& client : clients)
332 fake_sensor->RemoveClient(client.get());
333
334 EXPECT_TRUE(fake_sensor->config_map().empty());
335 }
336
337 // Tests a sensor cannot be updated if it has one suspended client.
338 TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) {
339 TestSensorCreateCallback callback;
340 scoped_refptr<PlatformSensor> sensor =
341 CreateSensor(SensorType::AMBIENT_LIGHT, &callback);
342 FakePlatformSensor* fake_sensor =
343 static_cast<FakePlatformSensor*>(sensor.get());
344 EXPECT_TRUE(fake_sensor->config_map().empty());
345
346 sensor_client_->SetSensor(fake_sensor);
347
348 PlatformSensorConfiguration config(30);
349 fake_sensor->StartListening(sensor_client_.get(), config);
350
351 sensor_client_->set_notification_suspended(true);
352 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
353
354 fake_sensor->UpdateSensor();
355 EXPECT_FALSE(fake_sensor->started());
356
357 sensor_client_->set_notification_suspended(false);
358 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
359
360 fake_sensor->UpdateSensor();
361 EXPECT_TRUE(fake_sensor->started());
362 }
363
364 // Tests a sensor can be updated if it has one suspended client and other
365 // clients are not suspended.
366 TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) {
367 const int num = 5;
368
369 TestSensorCreateCallback callback;
370 scoped_refptr<PlatformSensor> sensor =
371 CreateSensor(SensorType::AMBIENT_LIGHT, &callback);
372 FakePlatformSensor* fake_sensor =
373 static_cast<FakePlatformSensor*>(sensor.get());
374 EXPECT_TRUE(fake_sensor->config_map().empty());
375
376 sensor_client_->SetSensor(fake_sensor);
377 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
378 for (int i = 0; i < num; i++) {
379 std::unique_ptr<PlatformSensorTestClient> client(
380 new PlatformSensorTestClient());
381 client->SetSensor(fake_sensor);
382 clients.push_back(std::move(client));
383 }
384
385 double sensor_frequency = 30;
386 PlatformSensorConfiguration config(sensor_frequency++);
387 fake_sensor->StartListening(sensor_client_.get(), config);
388 for (const auto& client : clients) {
389 PlatformSensorConfiguration config(sensor_frequency++);
390 fake_sensor->StartListening(client.get(), config);
391 }
392
393 sensor_client_->set_notification_suspended(true);
394 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
395 for (const auto& client : clients)
396 EXPECT_FALSE(client->IsNotificationSuspended());
397
398 fake_sensor->UpdateSensor();
399 EXPECT_TRUE(fake_sensor->started());
400
401 sensor_client_->set_notification_suspended(false);
402 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
403 for (const auto& client : clients)
404 EXPECT_FALSE(client->IsNotificationSuspended());
405
406 fake_sensor->UpdateSensor();
407 EXPECT_TRUE(fake_sensor->started());
408 }
409
410 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_mac.cc ('k') | device/generic_sensor/platform_sensor_provider_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698