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 virtual void OnGamepadConnected(
29 const blink::WebGamepad
& gamepad
) OVERRIDE
{
32 virtual void OnGamepadDisconnected(
34 const blink::WebGamepad
& gamepad
) OVERRIDE
{
35 disconnected_counter_
++;
38 void ClearCounters() {
39 connected_counter_
= 0;
40 disconnected_counter_
= 0;
43 int connected_counter() const { return connected_counter_
; }
44 int disconnected_counter() const { return disconnected_counter_
; }
47 int connected_counter_
;
48 int disconnected_counter_
;
51 class GamepadServiceTest
: public testing::Test
{
54 virtual ~GamepadServiceTest() OVERRIDE
;
56 void SetPadsConnected(bool connected
);
59 int GetConnectedCounter() const {
60 return connection_listener_
->connected_counter();
62 int GetDisconnectedCounter() const {
63 return connection_listener_
->disconnected_counter();
66 virtual void SetUp() OVERRIDE
;
69 MockGamepadDataFetcher
* fetcher_
;
70 GamepadService
* service_
;
71 scoped_ptr
<ConnectionListener
> connection_listener_
;
72 TestBrowserThreadBundle browser_thread_
;
73 WebGamepads test_data_
;
75 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest
);
78 GamepadServiceTest::GamepadServiceTest()
79 : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP
) {
80 memset(&test_data_
, 0, sizeof(test_data_
));
82 // Set it so that we have user gesture.
83 test_data_
.items
[0].buttonsLength
= 1;
84 test_data_
.items
[0].buttons
[0].value
= 1.f
;
85 test_data_
.items
[0].buttons
[0].pressed
= true;
88 GamepadServiceTest::~GamepadServiceTest() {
92 void GamepadServiceTest::SetUp() {
93 fetcher_
= new MockGamepadDataFetcher(test_data_
);
94 service_
= new GamepadService(scoped_ptr
<GamepadDataFetcher
>(fetcher_
));
95 connection_listener_
.reset((new ConnectionListener
));
96 service_
->ConsumerBecameActive(connection_listener_
.get());
99 void GamepadServiceTest::SetPadsConnected(bool connected
) {
100 for (int i
= 0; i
< kNumberOfGamepads
; ++i
) {
101 test_data_
.items
[i
].connected
= connected
;
103 fetcher_
->SetTestData(test_data_
);
106 void GamepadServiceTest::WaitForData() {
107 connection_listener_
->ClearCounters();
108 fetcher_
->WaitForDataReadAndCallbacksIssued();
109 base::RunLoop().RunUntilIdle();
112 TEST_F(GamepadServiceTest
, ConnectionsTest
) {
114 EXPECT_EQ(0, GetConnectedCounter());
115 EXPECT_EQ(0, GetDisconnectedCounter());
117 SetPadsConnected(true);
119 EXPECT_EQ(kNumberOfGamepads
, GetConnectedCounter());
120 EXPECT_EQ(0, GetDisconnectedCounter());
122 SetPadsConnected(false);
124 EXPECT_EQ(0, GetConnectedCounter());
125 EXPECT_EQ(kNumberOfGamepads
, GetDisconnectedCounter());
128 EXPECT_EQ(0, GetConnectedCounter());
129 EXPECT_EQ(0, GetDisconnectedCounter());
132 } // namespace content