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

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

Issue 2306333002: [sensors] Add Generic Sensor platform unit tests. (Closed)
Patch Set: Mikhail's 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_notification_suspended(bool value) {
46 notification_suspended_ = value;
47 }
48
49 void set_sensor(scoped_refptr<PlatformSensor> sensor) {
Mikhail 2016/09/13 10:31:17 SetSensor
maksims (do not use this acc) 2016/09/16 09:22:19 Done.
50 sensor_ = sensor;
51 sensor_->AddClient(this);
52 }
53
54 bool is_sensor_reading_changed() const { return sensor_reading_changed_; }
timvolodine 2016/09/13 19:38:53 sensor_reading_changed() i.e. trivial getter
maksims (do not use this acc) 2016/09/16 09:22:19 Done.
55
56 bool sensor_has_error() const { return sensor_error_; }
timvolodine 2016/09/13 19:38:53 just sensor_error() ?
maksims (do not use this acc) 2016/09/16 09:22:20 Well, if I name this method sensor_error(), I woul
timvolodine 2016/09/19 17:14:29 you are returning sensor_error_ which is a boolean
maksims (do not use this acc) 2016/09/21 11:05:19 Done.
57
58 private:
59 scoped_refptr<PlatformSensor> sensor_;
timvolodine 2016/09/13 19:38:53 is there a reason this cannot be a unique_ptr?
maksims (do not use this acc) 2016/09/16 09:22:19 It's implementation specific. PlatformSensor retur
60 bool notification_suspended_;
61 bool sensor_reading_changed_;
62 bool sensor_error_;
63 };
64
65 class PlatformSensorProviderTest : public ::testing::Test {
66 public:
67 PlatformSensorProviderTest()
68 : sensor_client_(new PlatformSensorTestClient()) {}
69
70 protected:
71 scoped_refptr<PlatformSensor> CreateSensor(mojom::SensorType type) {
72 return MockPlatformSensorProvider::GetInstance()->CreateSensor(
73 type, SensorReadBuffer::kReadBufferSize, GetBufferOffset(type));
74 }
75
76 std::unique_ptr<PlatformSensorTestClient> sensor_client_;
77 };
78
79 TEST_F(PlatformSensorProviderTest, CreateSensorsAndCheckType) {
80 scoped_refptr<PlatformSensor> sensor1 =
timvolodine 2016/09/13 19:38:53 same here, why not use unique_ptr? and also everyw
maksims (do not use this acc) 2016/09/16 09:22:20 answered above.
81 CreateSensor(SensorType::AMBIENT_LIGHT);
82 EXPECT_TRUE(sensor1);
83 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
84
85 scoped_refptr<PlatformSensor> sensor2 = CreateSensor(SensorType::PROXIMITY);
86 EXPECT_TRUE(sensor2);
87 EXPECT_EQ(SensorType::PROXIMITY, sensor2->GetType());
88
89 scoped_refptr<PlatformSensor> sensor3 =
90 CreateSensor(SensorType::ACCELEROMETER);
91 EXPECT_TRUE(sensor3);
92 EXPECT_EQ(SensorType::ACCELEROMETER, sensor3->GetType());
93
94 scoped_refptr<PlatformSensor> sensor4 = CreateSensor(SensorType::GYROSCOPE);
95 EXPECT_TRUE(sensor4);
96 EXPECT_EQ(SensorType::GYROSCOPE, sensor4->GetType());
97
98 scoped_refptr<PlatformSensor> sensor5 = CreateSensor(SensorType::PRESSURE);
99 EXPECT_TRUE(sensor5);
100 EXPECT_EQ(SensorType::PRESSURE, sensor5->GetType());
101 }
102
103 TEST_F(PlatformSensorProviderTest, CreateAndGetSensor) {
104 PlatformSensorProvider* sensor_provider =
105 MockPlatformSensorProvider::GetInstance();
106
107 // Create Ambient Light sensor.
108 scoped_refptr<PlatformSensor> sensor1 =
109 CreateSensor(SensorType::AMBIENT_LIGHT);
110 EXPECT_TRUE(sensor1);
111 EXPECT_EQ(SensorType::AMBIENT_LIGHT, sensor1->GetType());
timvolodine 2016/09/13 19:38:53 can we also have a test for leaks? something like:
maksims (do not use this acc) 2016/09/16 09:22:19 No, sensor1 is scoped_refptr. If it is reassigned
timvolodine 2016/09/19 17:14:29 why? should it crash? it's refptr so I would expec
maksims (do not use this acc) 2016/09/21 11:05:19 Please check now. I've added this case here.
112
113 // Try to get Gyroscope sensor, which has not been created yet.
114 scoped_refptr<PlatformSensor> sensor2 =
115 sensor_provider->GetSensor(SensorType::GYROSCOPE);
116 EXPECT_FALSE(sensor2);
117
118 // Get Ambient Light sensor.
119 scoped_refptr<PlatformSensor> sensor3 =
120 sensor_provider->GetSensor(SensorType::AMBIENT_LIGHT);
121 EXPECT_TRUE(sensor3);
122
123 EXPECT_EQ(sensor1->GetType(), sensor3->GetType());
124
125 // Try to create a sensor with zero buffer and offset.
126 scoped_refptr<PlatformSensor> sensor4 =
127 sensor_provider->CreateSensor(SensorType::GYROSCOPE, 0, 0);
128 EXPECT_FALSE(sensor4);
129
130 scoped_refptr<PlatformSensor> sensor5 =
131 sensor_provider->GetSensor(SensorType::GYROSCOPE);
132 EXPECT_FALSE(sensor5);
133 }
134
135 // This test assumes that a mock sensor has a constant maximum frequency value
136 // of 50 hz (different from the base sensor class that has a range from 0 to
137 // 60) and tests whether a mock sensor can be started with a value range from 0
138 // to 60.
139 TEST_F(PlatformSensorProviderTest, StartListeningWithDifferentParameters) {
140 const double too_high_frequency = 60;
141 const double normal_frequency = 39;
142 scoped_refptr<PlatformSensor> sensor =
143 CreateSensor(SensorType::AMBIENT_LIGHT);
144 MockPlatformSensor* mock_sensor =
145 static_cast<MockPlatformSensor*>(sensor.get());
146 EXPECT_TRUE(mock_sensor);
147 sensor_client_->set_sensor(sensor);
148
149 PlatformSensorConfiguration config(too_high_frequency);
150 EXPECT_EQ(too_high_frequency, config.frequency());
151 EXPECT_FALSE(mock_sensor->StartListening(sensor_client_.get(), config));
152 EXPECT_FALSE(mock_sensor->started());
153
154 config.set_frequency(normal_frequency);
155 EXPECT_EQ(normal_frequency, config.frequency());
156 EXPECT_TRUE(mock_sensor->StartListening(sensor_client_.get(), config));
157 EXPECT_TRUE(mock_sensor->started());
158
159 EXPECT_TRUE(mock_sensor->StopListening(sensor_client_.get(), config));
160 EXPECT_FALSE(mock_sensor->started());
161 }
162
163 // If a client is in a suspended mode, a NotifySensorReadingChanged()
164 // notification must not be sent to the client but NotifySensorError() must be.
165 TEST_F(PlatformSensorProviderTest, TestNotificationSuspended) {
166 const int num = 5;
167 scoped_refptr<PlatformSensor> sensor = CreateSensor(SensorType::GYROSCOPE);
168 MockPlatformSensor* mock_sensor =
169 static_cast<MockPlatformSensor*>(sensor.get());
170
171 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
172 for (int i = 0; i < num; i++) {
173 std::unique_ptr<PlatformSensorTestClient> client(
174 new PlatformSensorTestClient());
175 client->set_sensor(mock_sensor);
176 clients.push_back(std::move(client));
177 }
178
179 clients.front()->set_notification_suspended(true);
180 mock_sensor->NotifySensorReadingChanged();
181 mock_sensor->NotifySensorError();
182 for (auto const& client : clients) {
183 if (client == clients.front()) {
184 EXPECT_FALSE(client->is_sensor_reading_changed());
185 EXPECT_TRUE(client->sensor_has_error());
186 continue;
187 }
188 EXPECT_TRUE(client->is_sensor_reading_changed());
189 EXPECT_TRUE(client->sensor_has_error());
190 }
191
192 clients.front()->set_notification_suspended(false);
193 mock_sensor->NotifySensorReadingChanged();
194 mock_sensor->NotifySensorError();
195 for (auto const& client : clients) {
196 EXPECT_TRUE(client->is_sensor_reading_changed());
197 EXPECT_TRUE(client->sensor_has_error());
198 }
199 }
200
201 // Tests that when all clients are removed, config maps are removed as well.
202 TEST_F(PlatformSensorProviderTest, TestAddRemoveClients) {
203 const int num = 5;
204 const double frq = 30;
205
206 scoped_refptr<PlatformSensor> sensor =
207 CreateSensor(SensorType::AMBIENT_LIGHT);
208 MockPlatformSensor* mock_sensor =
209 static_cast<MockPlatformSensor*>(sensor.get());
210 EXPECT_TRUE(mock_sensor->config_map().empty());
211
212 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
213 PlatformSensorConfiguration config(frq);
214 for (int i = 0; i < num; i++) {
215 std::unique_ptr<PlatformSensorTestClient> client(
216 new PlatformSensorTestClient());
217 client->set_sensor(mock_sensor);
218 EXPECT_TRUE(mock_sensor->StartListening(client.get(), config));
219 EXPECT_TRUE(mock_sensor->started());
220
221 clients.push_back(std::move(client));
222 }
223 EXPECT_FALSE(mock_sensor->config_map().empty());
224
225 for (const auto& client : clients)
226 mock_sensor->RemoveClient(client.get());
227
228 EXPECT_TRUE(mock_sensor->config_map().empty());
229 }
230
231 // Tests a sensor cannot be updated if it has one suspended client.
232 TEST_F(PlatformSensorProviderTest, TestUpdateSensorOneClient) {
233 const double frq = 30;
timvolodine 2016/09/13 19:38:53 any chance of a better name? probably not necessar
timvolodine 2016/09/19 17:14:29 ?
maksims (do not use this acc) 2016/09/21 11:05:19 removed
234
235 scoped_refptr<PlatformSensor> sensor =
236 CreateSensor(SensorType::AMBIENT_LIGHT);
237 MockPlatformSensor* mock_sensor =
238 static_cast<MockPlatformSensor*>(sensor.get());
239 EXPECT_TRUE(mock_sensor->config_map().empty());
240
241 sensor_client_->set_sensor(mock_sensor);
242
243 PlatformSensorConfiguration config(frq);
244 mock_sensor->StartListening(sensor_client_.get(), config);
245
246 sensor_client_->set_notification_suspended(true);
247 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
248
249 mock_sensor->UpdateSensor();
250 EXPECT_FALSE(mock_sensor->started());
251
252 sensor_client_->set_notification_suspended(false);
253 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
254
255 mock_sensor->UpdateSensor();
256 EXPECT_TRUE(mock_sensor->started());
257 }
258
259 // Tests a sensor can be updated if it has one suspended client and other
260 // clients are not suspended.
261 TEST_F(PlatformSensorProviderTest, TestUpdateSensorManyClients) {
262 const int num = 5;
263
264 scoped_refptr<PlatformSensor> sensor =
265 CreateSensor(SensorType::AMBIENT_LIGHT);
266 MockPlatformSensor* mock_sensor =
267 static_cast<MockPlatformSensor*>(sensor.get());
268 EXPECT_TRUE(mock_sensor->config_map().empty());
269
270 sensor_client_->set_sensor(mock_sensor);
271 std::vector<std::unique_ptr<PlatformSensorTestClient>> clients;
272 for (int i = 0; i < num; i++) {
273 std::unique_ptr<PlatformSensorTestClient> client(
274 new PlatformSensorTestClient());
275 client->set_sensor(mock_sensor);
276 clients.push_back(std::move(client));
277 }
278
279 double frq = 30;
timvolodine 2016/09/13 19:38:53 same here regarding naming
maksims (do not use this acc) 2016/09/16 09:22:20 Done.
280 PlatformSensorConfiguration config(frq++);
timvolodine 2016/09/13 19:38:53 why frq++ here?
maksims (do not use this acc) 2016/09/16 09:22:20 Nothing special. Just wanted to have different cli
281 mock_sensor->StartListening(sensor_client_.get(), config);
282 for (const auto& client : clients) {
283 PlatformSensorConfiguration config(frq++);
284 mock_sensor->StartListening(client.get(), config);
285 }
286
287 sensor_client_->set_notification_suspended(true);
288 EXPECT_TRUE(sensor_client_->IsNotificationSuspended());
289 for (const auto& client : clients)
290 EXPECT_FALSE(client->IsNotificationSuspended());
291
292 mock_sensor->UpdateSensor();
293 EXPECT_TRUE(mock_sensor->started());
294
295 sensor_client_->set_notification_suspended(false);
296 EXPECT_FALSE(sensor_client_->IsNotificationSuspended());
297 for (const auto& client : clients)
298 EXPECT_FALSE(client->IsNotificationSuspended());
299
300 mock_sensor->UpdateSensor();
301 EXPECT_TRUE(mock_sensor->started());
302 }
303
304 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698