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

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

Issue 2306333002: [sensors] Add Generic Sensor platform unit tests. (Closed)
Patch Set: Comments Created 4 years, 3 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 "device/generic_sensor/mock_platform_sensor.h"
6 #include "device/generic_sensor/mock_platform_sensor_provider.h"
7 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace device {
12
13 using mojom::SensorReadBuffer;
14 using mojom::SensorType;
15
16 namespace {
17
18 uint64_t GetBufferOffset(mojom::SensorType type) {
19 return (static_cast<uint64_t>(SensorType::LAST) -
20 static_cast<uint64_t>(type)) *
21 SensorReadBuffer::SensorReadBuffer::kReadBufferSize;
22 }
23
24 } // namespace
25
26 class PlatformSensorTestClient : public PlatformSensor::Client {
27 public:
28 PlatformSensorTestClient()
29 : notification_suspended_(false),
30 sensor_reading_changed_(false),
31 sensor_error_(false) {}
32
33 ~PlatformSensorTestClient() override {
34 if (sensor_)
35 sensor_->RemoveClient(this);
36 }
37
38 // PlatformSensor::Client override.
39 void OnSensorReadingChanged() override { sensor_reading_changed_ = true; }
40
41 void OnSensorError() override { sensor_error_ = true; }
42
43 bool IsNotificationSuspended() override { return notification_suspended_; }
44
45 void set_suspended(bool value) { notification_suspended_ = value; }
Mikhail 2016/09/12 08:12:43 set_notification_suspended()
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
46
47 void set_sensor(scoped_refptr<PlatformSensor> sensor) {
Mikhail 2016/09/12 08:12:44 SetSensor // not a trivial setter
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
48 sensor_ = sensor;
49 sensor_->AddClient(this);
50 }
51
52 bool IsSensorReadingChanged() { return sensor_reading_changed_; }
Mikhail 2016/09/12 08:12:43 bool is_sensor_reading_changed() const // trivial
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
53
54 bool SensorHasError() { return sensor_error_; }
Mikhail 2016/09/12 08:12:44 ditto https://google.github.io/styleguide/cppguide
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
55
56 void remove_sensor(scoped_refptr<PlatformSensor> sensor) {
Mikhail 2016/09/12 08:12:43 this method is not needed, client code can call se
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
57 EXPECT_EQ(sensor, sensor_);
58 sensor_->RemoveClient(this);
59 }
60
61 private:
62 scoped_refptr<PlatformSensor> sensor_;
63 bool notification_suspended_;
64 bool sensor_reading_changed_;
65 bool sensor_error_;
66 };
67
68 class PlatformSensorProviderTest : public ::testing::Test {
69 public:
70 PlatformSensorProviderTest() {
71 sensor_client_.reset(new PlatformSensorTestClient());
Mikhail 2016/09/12 08:12:43 : sensor_client_(new PlatformSensorTestClient())
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
72 sensor_provider_ = MockPlatformSensorProvider::GetInstance();
73 EXPECT_TRUE(sensor_provider_);
Mikhail 2016/09/12 08:12:43 I don't think we should test standard singleton cl
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
74 }
75
76 protected:
77 scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) {
78 return sensor_provider_->CreateSensor(
Mikhail 2016/09/12 08:12:43 MockPlatformSensorProvider::GetInstance()->CreateS
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
79 type, SensorReadBuffer::kReadBufferSize, GetBufferOffset(type));
80 }
81
82 PlatformSensorProvider* sensor_provider_;
83 std::unique_ptr<PlatformSensorTestClient> sensor_client_;
84 };
85
86 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) {
87 scoped_refptr<PlatformSensor> sensor1 = sensor_provider_->CreateSensor(
Mikhail 2016/09/12 08:12:44 why not call 'CreateSensor(SensorType::AMBIENT_LIG
maksims (do not use this acc) 2016/09/13 08:01:02 I've forgotten to modify the callers here. First,
88 SensorType::AMBIENT_LIGHT, SensorReadBuffer::kReadBufferSize,
89 GetBufferOffset(SensorType::AMBIENT_LIGHT));
90 EXPECT_TRUE(sensor1);
91 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
92
93 scoped_refptr<PlatformSensor> sensor2 = sensor_provider_->CreateSensor(
94 SensorType::PROXIMITY, SensorReadBuffer::kReadBufferSize,
95 GetBufferOffset(SensorType::PROXIMITY));
96 EXPECT_TRUE(sensor2);
97 EXPECT_EQ(SensorType::PROXIMITY, sensor2->GetType());
98
99 scoped_refptr<PlatformSensor> sensor3 = sensor_provider_->CreateSensor(
100 SensorType::ACCELEROMETER, SensorReadBuffer::kReadBufferSize,
101 GetBufferOffset(SensorType::ACCELEROMETER));
102 EXPECT_TRUE(sensor3);
103 EXPECT_EQ(SensorType::ACCELEROMETER, sensor3->GetType());
104
105 scoped_refptr<PlatformSensor> sensor4 = sensor_provider_->CreateSensor(
106 SensorType::GYROSCOPE, SensorReadBuffer::kReadBufferSize,
107 GetBufferOffset(SensorType::GYROSCOPE));
108 EXPECT_TRUE(sensor4);
109 EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType());
110
111 scoped_refptr<PlatformSensor> sensor5 = sensor_provider_->CreateSensor(
112 SensorType::PRESSURE, SensorReadBuffer::kReadBufferSize,
113 GetBufferOffset(SensorType::PRESSURE));
114 EXPECT_TRUE(sensor5);
115 EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType());
116 }
117
118 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) {
119 // Create Ambient Light sensor.
120 scoped_refptr<PlatformSensor> sensor1 =
121 CreateSensor(SensorType::AMBIENT_LIGHT);
122 EXPECT_TRUE(sensor1);
123 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
124
125 // Try to get Gyroscope sensor, which has not been created yet.
126 scoped_refptr<PlatformSensor> sensor2 =
127 sensor_provider_->GetSensor(SensorType::GYROSCOPE);
128 EXPECT_FALSE(sensor2);
129
130 // Get Ambient Light sensor.
131 scoped_refptr<PlatformSensor> sensor3 =
132 sensor_provider_->GetSensor(SensorType::AMBIENT_LIGHT);
133 EXPECT_TRUE(sensor3);
134
135 EXPECT_EQ(sensor1->GetType(), sensor3->GetType());
136
137 // Try to create a sensor with zero buffer and offset.
138 scoped_refptr<PlatformSensor> sensor4 =
139 sensor_provider_->CreateSensor(SensorType::GYROSCOPE, 0, 0);
140 EXPECT_FALSE(sensor4);
141
142 scoped_refptr<PlatformSensor> sensor5 =
143 sensor_provider_->GetSensor(SensorType::GYROSCOPE);
144 EXPECT_FALSE(sensor5);
145 }
146
147 // This test assumes that a mock sensor has a constant maximum frequency value
148 // of 50 hz (different from the base sensor class that has a range from 0 to
149 // 60) and tests whether a mock sensor can be started with a value range from 0
150 // to 60.
151 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) {
152 const double too_high_frequency = 60;
153 const double normal_frequency = 39;
154 scoped_refptr<PlatformSensor> sensor =
155 CreateSensor(SensorType::AMBIENT_LIGHT);
156 MockPlatformSensor* mock_sensor =
157 static_cast<MockPlatformSensor*>(sensor.get());
158 EXPECT_TRUE(mock_sensor);
159 sensor_client_->set_sensor(sensor);
160
161 PlatformSensorConfiguration config(too_high_frequency);
162 EXPECT_EQ(too_high_frequency, config.frequency());
163 EXPECT_FALSE(mock_sensor->StartListening(sensor_client_.get(), config));
164 EXPECT_FALSE(mock_sensor->started());
165
166 config.set_frequency(normal_frequency);
167 EXPECT_EQ(normal_frequency, config.frequency());
168 EXPECT_TRUE(mock_sensor->StartListening(sensor_client_.get(), config));
169 EXPECT_TRUE(mock_sensor->started());
170
171 EXPECT_TRUE(mock_sensor->StopListening(sensor_client_.get(), config));
172 EXPECT_FALSE(mock_sensor->started());
173 }
174
175 // If a client is in a suspended mode, a NotifySensorReadingChanged()
176 // notification must not be sent to the client but NotifySensorError() must be.
177 TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) {
178 const int num = 5;
179 scoped_refptr<PlatformSensor> sensor = CreateSensor(SensorType::GYROSCOPE);
180 MockPlatformSensor* mock_sensor =
181 static_cast<MockPlatformSensor*>(sensor.get());
182
183 std::unique_ptr<PlatformSensorTestClient> clients[num];
Mikhail 2016/09/12 08:12:43 pls don't make arrays with smart pointers. https:/
maksims (do not use this acc) 2016/09/13 08:01:02 Done.
184 for (int i = 0; i < num; i++) {
185 clients[i].reset(new PlatformSensorTestClient());
186 clients[i]->set_sensor(mock_sensor);
187 }
188
189 clients[1]->set_suspended(true);
190 mock_sensor->NotifySensorReadingChanged();
191 mock_sensor->NotifySensorError();
192 for (int i = 0; i < num; i++) {
193 if (i == 1) {
194 EXPECT_FALSE(clients[i]->IsSensorReadingChanged());
195 EXPECT_TRUE(clients[i]->SensorHasError());
196 continue;
197 }
198 EXPECT_TRUE(clients[i]->IsSensorReadingChanged());
199 EXPECT_TRUE(clients[i]->SensorHasError());
200 }
201
202 clients[1]->set_suspended(false);
203 mock_sensor->NotifySensorReadingChanged();
204 mock_sensor->NotifySensorError();
205 for (int i = 0; i < num; i++) {
206 EXPECT_TRUE(clients[i]->IsSensorReadingChanged());
207 EXPECT_TRUE(clients[i]->SensorHasError());
208 }
209 }
210
211 // Tests that when all clients are removed, config maps are removed as well.
212 TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) {
213 const int num = 5;
214 const double frq = 30;
215
216 scoped_refptr<PlatformSensor> sensor =
217 CreateSensor(SensorType::AMBIENT_LIGHT);
218 MockPlatformSensor* mock_sensor =
219 static_cast<MockPlatformSensor*>(sensor.get());
220 EXPECT_TRUE(mock_sensor->config_map().empty());
221
222 std::unique_ptr<PlatformSensorTestClient> clients[num];
223 PlatformSensorConfiguration config(frq);
224 for (int i = 0; i < num; i++) {
225 clients[i].reset(new PlatformSensorTestClient());
226 clients[i]->set_sensor(mock_sensor);
227 EXPECT_TRUE(mock_sensor->StartListening(clients[i].get(), config));
228 EXPECT_TRUE(mock_sensor->started());
229 }
230 EXPECT_FALSE(mock_sensor->config_map().empty());
231
232 for (int i = 0; i < num; i++)
233 clients[i]->remove_sensor(mock_sensor);
234
235 EXPECT_TRUE(mock_sensor->config_map().empty());
236 }
237
238 // Tests a sensor cannot be updated if it has one suspended client.
239 TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) {
240 const double frq = 30;
241
242 scoped_refptr<PlatformSensor> sensor =
243 CreateSensor(SensorType::AMBIENT_LIGHT);
244 MockPlatformSensor* mock_sensor =
245 static_cast<MockPlatformSensor*>(sensor.get());
246 EXPECT_TRUE(mock_sensor->config_map().empty());
247
248 sensor_client_->set_sensor(mock_sensor);
249
250 PlatformSensorConfiguration config(frq);
251 mock_sensor->StartListening(sensor_client_.get(), config);
252
253 sensor_client_->set_suspended(true);
254 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
255
256 mock_sensor->UpdateSensor();
257 EXPECT_FALSE(mock_sensor->started());
258
259 sensor_client_->set_suspended(false);
260 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
261
262 mock_sensor->UpdateSensor();
263 EXPECT_TRUE(mock_sensor->started());
264 }
265
266 // Tests a sensor can be updated if it has one suspended client and other
267 // clients are not suspended.
268 TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) {
269 const int num = 5;
270 const double frq = 30;
271
272 scoped_refptr<PlatformSensor> sensor =
273 CreateSensor(SensorType::AMBIENT_LIGHT);
274 MockPlatformSensor* mock_sensor =
275 static_cast<MockPlatformSensor*>(sensor.get());
276 EXPECT_TRUE(mock_sensor->config_map().empty());
277
278 sensor_client_->set_sensor(mock_sensor);
279 std::unique_ptr<PlatformSensorTestClient> clients[num];
280 for (int i = 0; i < num; i++) {
281 clients[i].reset(new PlatformSensorTestClient());
282 clients[i]->set_sensor(mock_sensor);
283 }
284
285 PlatformSensorConfiguration config(frq);
286 mock_sensor->StartListening(sensor_client_.get(), config);
287 for (int i = 0; i < num; i++) {
288 PlatformSensorConfiguration config(frq + i);
289 mock_sensor->StartListening(clients[i].get(), config);
290 }
291
292 sensor_client_->set_suspended(true);
293 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
294 for (int i = 0; i < num; i++) {
295 EXPECT_FALSE(clients[i]->IsNotificationSuspended());
296 }
297
298 mock_sensor->UpdateSensor();
299 EXPECT_TRUE(mock_sensor->started());
300
301 sensor_client_->set_suspended(false);
302 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
303 for (int i = 0; i < num; i++) {
304 EXPECT_FALSE(clients[i]->IsNotificationSuspended());
305 }
306
307 mock_sensor->UpdateSensor();
308 EXPECT_TRUE(mock_sensor->started());
309 }
310
311 } // namespace device
OLDNEW
« device/generic_sensor/mock_platform_sensor.h ('K') | « device/generic_sensor/platform_sensor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698