forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad_service_unittest.cc
518 lines (434 loc) · 17.5 KB
/
gamepad_service_unittest.cc
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/gamepad/gamepad_service.h"
#include <string.h>
#include <memory>
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "device/gamepad/gamepad_consumer.h"
#include "device/gamepad/gamepad_test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace device {
namespace {
constexpr int kNumberOfGamepads = Gamepads::kItemsLengthCap;
} // namespace
class ConnectionListener : public GamepadConsumer {
public:
ConnectionListener() = default;
// GamepadConsumer implementation.
void OnGamepadConnected(uint32_t index, const Gamepad& gamepad) override {
connected_counter_++;
}
void OnGamepadDisconnected(uint32_t index, const Gamepad& gamepad) override {
disconnected_counter_++;
}
void OnGamepadButtonOrAxisChanged(uint32_t index,
const Gamepad& gamepad) override {}
void ClearCounters() {
connected_counter_ = 0;
disconnected_counter_ = 0;
}
int connected_counter() const { return connected_counter_; }
int disconnected_counter() const { return disconnected_counter_; }
private:
int connected_counter_ = 0;
int disconnected_counter_ = 0;
};
class GamepadServiceTest : public testing::Test {
protected:
GamepadServiceTest() {
memset(&test_data_, 0, sizeof(test_data_));
// Configure the pad to have one button. We need our mock gamepad
// to have at least one input so we can simulate a user gesture.
test_data_.items[0].buttons_length = 1;
}
~GamepadServiceTest() override = default;
GamepadService* service() const { return service_; }
void SetUp() override {
auto fetcher = std::make_unique<MockGamepadDataFetcher>(test_data_);
fetcher_ = fetcher.get();
service_ = new GamepadService(std::move(fetcher));
service_->SetSanitizationEnabled(false);
}
void TearDown() override {
// Calling SetInstance will destroy the GamepadService instance.
GamepadService::SetInstance(nullptr);
}
ConnectionListener* CreateConsumer() {
consumers_.push_back(std::make_unique<ConnectionListener>());
return consumers_.back().get();
}
void ClearCounters() {
for (auto& consumer : consumers_)
consumer->ClearCounters();
}
void SetPadsConnected(bool connected) {
for (int i = 0; i < kNumberOfGamepads; ++i)
test_data_.items[i].connected = connected;
fetcher_->SetTestData(test_data_);
}
void SimulateUserGesture(bool has_gesture) {
test_data_.items[0].buttons[0].value = has_gesture ? 1.f : 0.f;
test_data_.items[0].buttons[0].pressed = has_gesture ? true : false;
fetcher_->SetTestData(test_data_);
}
void SimulatePageReload(GamepadConsumer* consumer) {
EXPECT_TRUE(service_->ConsumerBecameInactive(consumer));
EXPECT_TRUE(service_->ConsumerBecameActive(consumer));
}
void WaitForData() {
// Block until work on the polling thread is complete. The data fetcher will
// read gamepad data on the polling thread, which may cause the provider to
// post user gesture or gamepad connection callbacks to the main thread.
fetcher_->WaitForDataReadAndCallbacksIssued();
// Allow the user gesture and gamepad connection callbacks to run.
base::RunLoop().RunUntilIdle();
}
private:
base::test::SingleThreadTaskEnvironment task_environment_;
MockGamepadDataFetcher* fetcher_;
GamepadService* service_;
std::vector<std::unique_ptr<ConnectionListener>> consumers_;
Gamepads test_data_;
DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest);
};
TEST_F(GamepadServiceTest, ConnectionsTest) {
auto* consumer = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer));
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
SimulateUserGesture(true);
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
}
TEST_F(GamepadServiceTest, ConnectionThenGestureTest) {
auto* consumer = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer));
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
// No connection events are sent until a user gesture is seen.
ClearCounters();
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
SimulateUserGesture(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
}
TEST_F(GamepadServiceTest, ReloadTest) {
auto* consumer = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer));
// No connection events are sent until a user gesture is seen.
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
SimulatePageReload(consumer);
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
// After a user gesture, the connection listener is notified about connected
// gamepads.
ClearCounters();
SimulateUserGesture(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
// After a reload, if the gamepads were already connected (and we have seen
// a user gesture) then the connection listener is notified about connected
// gamepads.
ClearCounters();
SimulatePageReload(consumer);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer->connected_counter());
EXPECT_EQ(0, consumer->disconnected_counter());
}
TEST_F(GamepadServiceTest, SecondConsumerGestureTest) {
auto* consumer1 = CreateConsumer();
auto* consumer2 = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer1));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
// Simulate a user gesture. The gesture is received before the second
// consumer is active.
ClearCounters();
SetPadsConnected(true);
SimulateUserGesture(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
// The second consumer becomes active, but should not receive connection
// events until a new user gesture is received.
ClearCounters();
SimulateUserGesture(false);
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
// Connection events should only be sent to the second consumer.
ClearCounters();
SimulateUserGesture(true);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
}
TEST_F(GamepadServiceTest, ConnectWhileInactiveTest) {
auto* consumer1 = CreateConsumer();
auto* consumer2 = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer1));
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
// Ensure the initial user gesture is received by both consumers.
SimulateUserGesture(true);
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->disconnected_counter());
// Check that connecting gamepads while a consumer is inactive will notify
// once the consumer is active.
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameInactive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
}
TEST_F(GamepadServiceTest, ConnectAndDisconnectWhileInactiveTest) {
auto* consumer1 = CreateConsumer();
auto* consumer2 = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer1));
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
// Ensure the initial user gesture is received by both consumers.
SimulateUserGesture(true);
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->disconnected_counter());
// Check that a connection and then disconnection is NOT reported once the
// consumer is active.
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameInactive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
}
TEST_F(GamepadServiceTest, DisconnectWhileInactiveTest) {
auto* consumer1 = CreateConsumer();
auto* consumer2 = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer1));
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
// Ensure the initial user gesture is received by both consumers.
SimulateUserGesture(true);
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
// Check that disconnecting gamepads while a consumer is inactive will notify
// once the consumer is active.
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameInactive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->disconnected_counter());
ClearCounters();
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
}
TEST_F(GamepadServiceTest, DisconnectAndConnectWhileInactiveTest) {
auto* consumer1 = CreateConsumer();
auto* consumer2 = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer1));
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
// Ensure the initial user gesture is received by both consumers.
SimulateUserGesture(true);
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
// Check that a disconnection and then connection is reported as a connection
// (and no disconnection) once the consumer is active.
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameInactive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(false);
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
SetPadsConnected(true);
WaitForData();
EXPECT_EQ(kNumberOfGamepads, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(0, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
ClearCounters();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer2));
WaitForData();
EXPECT_EQ(0, consumer1->connected_counter());
EXPECT_EQ(0, consumer1->disconnected_counter());
EXPECT_EQ(kNumberOfGamepads, consumer2->connected_counter());
EXPECT_EQ(0, consumer2->disconnected_counter());
}
TEST_F(GamepadServiceTest, ActiveConsumerBecameActive) {
// Mark |consumer| active.
auto* consumer = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer));
// Mark |consumer| active a second time. ConsumerBecameActive should fail.
EXPECT_FALSE(service()->ConsumerBecameActive(consumer));
}
TEST_F(GamepadServiceTest, InactiveConsumerBecameInactive) {
// Mark |consumer| active.
auto* consumer = CreateConsumer();
EXPECT_TRUE(service()->ConsumerBecameActive(consumer));
// Mark |consumer| inactive.
EXPECT_TRUE(service()->ConsumerBecameInactive(consumer));
// Mark |consumer| inactive a second time. ConsumerBecameInactive should fail.
EXPECT_FALSE(service()->ConsumerBecameInactive(consumer));
}
TEST_F(GamepadServiceTest, UnregisteredConsumerBecameInactive) {
auto* consumer = CreateConsumer();
// |consumer| has not yet been added to the gamepad service through a call to
// ConsumerBecameActive. ConsumerBecameInactive should fail.
EXPECT_FALSE(service()->ConsumerBecameInactive(consumer));
}
TEST_F(GamepadServiceTest, RemoveUnregisteredConsumer) {
auto* consumer = CreateConsumer();
// |consumer| has not yet been added to the gamepad service through a call to
// ConsumerBecameActive. RemoveConsumer should fail.
EXPECT_FALSE(service()->RemoveConsumer(consumer));
}
} // namespace device