OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/gamepad/gamepad_service.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include <memory> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/run_loop.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "device/gamepad/gamepad_consumer.h" | |
15 #include "device/gamepad/gamepad_test_helpers.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace content { | |
19 | |
20 namespace { | |
21 static const int kNumberOfGamepads = blink::WebGamepads::itemsLengthCap; | |
22 } | |
23 | |
24 using blink::WebGamepads; | |
25 | |
26 class ConnectionListener : public device::GamepadConsumer { | |
27 public: | |
28 ConnectionListener() { | |
29 ClearCounters(); | |
30 } | |
31 | |
32 void OnGamepadConnected(unsigned index, | |
33 const blink::WebGamepad& gamepad) override { | |
34 connected_counter_++; | |
35 } | |
36 void OnGamepadDisconnected(unsigned index, | |
37 const blink::WebGamepad& gamepad) override { | |
38 disconnected_counter_++; | |
39 } | |
40 | |
41 void ClearCounters() { | |
42 connected_counter_ = 0; | |
43 disconnected_counter_ = 0; | |
44 } | |
45 | |
46 int connected_counter() const { return connected_counter_; } | |
47 int disconnected_counter() const { return disconnected_counter_; } | |
48 | |
49 private: | |
50 int connected_counter_; | |
51 int disconnected_counter_; | |
52 }; | |
53 | |
54 class GamepadServiceTest : public testing::Test { | |
55 protected: | |
56 GamepadServiceTest(); | |
57 ~GamepadServiceTest() override; | |
58 | |
59 void SetPadsConnected(bool connected); | |
60 void WaitForData(); | |
61 | |
62 int GetConnectedCounter() const { | |
63 return connection_listener_->connected_counter(); | |
64 } | |
65 int GetDisconnectedCounter() const { | |
66 return connection_listener_->disconnected_counter(); | |
67 } | |
68 | |
69 void SetUp() override; | |
70 | |
71 private: | |
72 device::MockGamepadDataFetcher* fetcher_; | |
73 GamepadService* service_; | |
74 std::unique_ptr<ConnectionListener> connection_listener_; | |
75 TestBrowserThreadBundle browser_thread_; | |
76 WebGamepads test_data_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest); | |
79 }; | |
80 | |
81 GamepadServiceTest::GamepadServiceTest() | |
82 : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP) { | |
83 memset(&test_data_, 0, sizeof(test_data_)); | |
84 | |
85 // Set it so that we have user gesture. | |
86 test_data_.items[0].buttonsLength = 1; | |
87 test_data_.items[0].buttons[0].value = 1.f; | |
88 test_data_.items[0].buttons[0].pressed = true; | |
89 } | |
90 | |
91 GamepadServiceTest::~GamepadServiceTest() { | |
92 delete service_; | |
93 } | |
94 | |
95 void GamepadServiceTest::SetUp() { | |
96 fetcher_ = new device::MockGamepadDataFetcher(test_data_); | |
97 service_ = new GamepadService( | |
98 std::unique_ptr<device::GamepadDataFetcher>(fetcher_)); | |
99 connection_listener_.reset((new ConnectionListener)); | |
100 service_->SetSanitizationEnabled(false); | |
101 service_->ConsumerBecameActive(connection_listener_.get()); | |
102 } | |
103 | |
104 void GamepadServiceTest::SetPadsConnected(bool connected) { | |
105 for (int i = 0; i < kNumberOfGamepads; ++i) { | |
106 test_data_.items[i].connected = connected; | |
107 } | |
108 fetcher_->SetTestData(test_data_); | |
109 } | |
110 | |
111 void GamepadServiceTest::WaitForData() { | |
112 connection_listener_->ClearCounters(); | |
113 fetcher_->WaitForDataReadAndCallbacksIssued(); | |
114 base::RunLoop().RunUntilIdle(); | |
115 } | |
116 | |
117 TEST_F(GamepadServiceTest, ConnectionsTest) { | |
118 WaitForData(); | |
119 EXPECT_EQ(0, GetConnectedCounter()); | |
120 EXPECT_EQ(0, GetDisconnectedCounter()); | |
121 | |
122 SetPadsConnected(true); | |
123 WaitForData(); | |
124 EXPECT_EQ(kNumberOfGamepads, GetConnectedCounter()); | |
125 EXPECT_EQ(0, GetDisconnectedCounter()); | |
126 | |
127 SetPadsConnected(false); | |
128 WaitForData(); | |
129 EXPECT_EQ(0, GetConnectedCounter()); | |
130 EXPECT_EQ(kNumberOfGamepads, GetDisconnectedCounter()); | |
131 | |
132 WaitForData(); | |
133 EXPECT_EQ(0, GetConnectedCounter()); | |
134 EXPECT_EQ(0, GetDisconnectedCounter()); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |