1 // Copyright 2014 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "content/browser/gamepad/gamepad_consumer.h"
8 #include "content/browser/gamepad/gamepad_service.h"
9 #include "content/browser/gamepad/gamepad_test_helpers.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 static const int kNumberOfGamepads
= blink::WebGamepads::itemsLengthCap
;
19 using blink::WebGamepads
;
21 class ConnectionListener
: public GamepadConsumer
{
23 ConnectionListener() {
27 void OnGamepadConnected(unsigned index
,
28 const blink::WebGamepad
& gamepad
) override
{
31 void OnGamepadDisconnected(unsigned index
,
32 const blink::WebGamepad
& gamepad
) override
{
33 disconnected_counter_
++;
36 void ClearCounters() {
37 connected_counter_
= 0;
38 disconnected_counter_
= 0;
41 int connected_counter() const { return connected_counter_
; }
42 int disconnected_counter() const { return disconnected_counter_
; }
45 int connected_counter_
;
46 int disconnected_counter_
;
49 class GamepadServiceTest
: public testing::Test
{
52 ~GamepadServiceTest() override
;
54 void SetPadsConnected(bool connected
);
57 int GetConnectedCounter() const {
58 return connection_listener_
->connected_counter();
60 int GetDisconnectedCounter() const {
61 return connection_listener_
->disconnected_counter();
64 void SetUp() override
;
67 MockGamepadDataFetcher
* fetcher_
;
68 GamepadService
* service_
;
69 scoped_ptr
<ConnectionListener
> connection_listener_
;
70 TestBrowserThreadBundle browser_thread_
;
71 WebGamepads test_data_
;
73 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest
);
76 GamepadServiceTest::GamepadServiceTest()
77 : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP
) {
78 memset(&test_data_
, 0, sizeof(test_data_
));
80 // Set it so that we have user gesture.
81 test_data_
.items
[0].buttonsLength
= 1;
82 test_data_
.items
[0].buttons
[0].value
= 1.f
;
83 test_data_
.items
[0].buttons
[0].pressed
= true;
86 GamepadServiceTest::~GamepadServiceTest() {
90 void GamepadServiceTest::SetUp() {
91 fetcher_
= new MockGamepadDataFetcher(test_data_
);
92 service_
= new GamepadService(scoped_ptr
<GamepadDataFetcher
>(fetcher_
));
93 connection_listener_
.reset((new ConnectionListener
));
94 service_
->ConsumerBecameActive(connection_listener_
.get());
97 void GamepadServiceTest::SetPadsConnected(bool connected
) {
98 for (int i
= 0; i
< kNumberOfGamepads
; ++i
) {
99 test_data_
.items
[i
].connected
= connected
;
101 fetcher_
->SetTestData(test_data_
);
104 void GamepadServiceTest::WaitForData() {
105 connection_listener_
->ClearCounters();
106 fetcher_
->WaitForDataReadAndCallbacksIssued();
107 base::RunLoop().RunUntilIdle();
110 TEST_F(GamepadServiceTest
, ConnectionsTest
) {
112 EXPECT_EQ(0, GetConnectedCounter());
113 EXPECT_EQ(0, GetDisconnectedCounter());
115 SetPadsConnected(true);
117 EXPECT_EQ(kNumberOfGamepads
, GetConnectedCounter());
118 EXPECT_EQ(0, GetDisconnectedCounter());
120 SetPadsConnected(false);
122 EXPECT_EQ(0, GetConnectedCounter());
123 EXPECT_EQ(kNumberOfGamepads
, GetDisconnectedCounter());
126 EXPECT_EQ(0, GetConnectedCounter());
127 EXPECT_EQ(0, GetDisconnectedCounter());
130 } // namespace content