OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/gamepad/gamepad_service.h" | 5 #include "device/gamepad/gamepad_service.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "device/gamepad/gamepad_consumer.h" | 13 #include "device/gamepad/gamepad_consumer.h" |
15 #include "device/gamepad/gamepad_test_helpers.h" | 14 #include "device/gamepad/gamepad_test_helpers.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace content { | 17 namespace device { |
19 | 18 |
20 namespace { | 19 namespace { |
21 static const int kNumberOfGamepads = blink::WebGamepads::itemsLengthCap; | 20 static const int kNumberOfGamepads = blink::WebGamepads::itemsLengthCap; |
22 } | 21 } |
23 | 22 |
24 using blink::WebGamepads; | 23 using blink::WebGamepads; |
25 | 24 |
26 class ConnectionListener : public device::GamepadConsumer { | 25 class ConnectionListener : public device::GamepadConsumer { |
27 public: | 26 public: |
28 ConnectionListener() { | 27 ConnectionListener() { ClearCounters(); } |
29 ClearCounters(); | |
30 } | |
31 | 28 |
32 void OnGamepadConnected(unsigned index, | 29 void OnGamepadConnected(unsigned index, |
33 const blink::WebGamepad& gamepad) override { | 30 const blink::WebGamepad& gamepad) override { |
34 connected_counter_++; | 31 connected_counter_++; |
35 } | 32 } |
36 void OnGamepadDisconnected(unsigned index, | 33 void OnGamepadDisconnected(unsigned index, |
37 const blink::WebGamepad& gamepad) override { | 34 const blink::WebGamepad& gamepad) override { |
38 disconnected_counter_++; | 35 disconnected_counter_++; |
39 } | 36 } |
40 | 37 |
(...skipping 21 matching lines...) Expand all Loading... |
62 int GetConnectedCounter() const { | 59 int GetConnectedCounter() const { |
63 return connection_listener_->connected_counter(); | 60 return connection_listener_->connected_counter(); |
64 } | 61 } |
65 int GetDisconnectedCounter() const { | 62 int GetDisconnectedCounter() const { |
66 return connection_listener_->disconnected_counter(); | 63 return connection_listener_->disconnected_counter(); |
67 } | 64 } |
68 | 65 |
69 void SetUp() override; | 66 void SetUp() override; |
70 | 67 |
71 private: | 68 private: |
| 69 base::MessageLoop message_loop_; |
72 device::MockGamepadDataFetcher* fetcher_; | 70 device::MockGamepadDataFetcher* fetcher_; |
73 GamepadService* service_; | 71 GamepadService* service_; |
74 std::unique_ptr<ConnectionListener> connection_listener_; | 72 std::unique_ptr<ConnectionListener> connection_listener_; |
75 TestBrowserThreadBundle browser_thread_; | |
76 WebGamepads test_data_; | 73 WebGamepads test_data_; |
77 | 74 |
78 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest); | 75 DISALLOW_COPY_AND_ASSIGN(GamepadServiceTest); |
79 }; | 76 }; |
80 | 77 |
81 GamepadServiceTest::GamepadServiceTest() | 78 GamepadServiceTest::GamepadServiceTest() { |
82 : browser_thread_(TestBrowserThreadBundle::IO_MAINLOOP) { | |
83 memset(&test_data_, 0, sizeof(test_data_)); | 79 memset(&test_data_, 0, sizeof(test_data_)); |
84 | 80 |
85 // Set it so that we have user gesture. | 81 // Set it so that we have user gesture. |
86 test_data_.items[0].buttonsLength = 1; | 82 test_data_.items[0].buttonsLength = 1; |
87 test_data_.items[0].buttons[0].value = 1.f; | 83 test_data_.items[0].buttons[0].value = 1.f; |
88 test_data_.items[0].buttons[0].pressed = true; | 84 test_data_.items[0].buttons[0].pressed = true; |
89 } | 85 } |
90 | 86 |
91 GamepadServiceTest::~GamepadServiceTest() { | 87 GamepadServiceTest::~GamepadServiceTest() { |
92 delete service_; | 88 delete service_; |
93 } | 89 } |
94 | 90 |
95 void GamepadServiceTest::SetUp() { | 91 void GamepadServiceTest::SetUp() { |
96 fetcher_ = new device::MockGamepadDataFetcher(test_data_); | 92 fetcher_ = new device::MockGamepadDataFetcher(test_data_); |
97 service_ = new GamepadService( | 93 service_ = |
98 std::unique_ptr<device::GamepadDataFetcher>(fetcher_)); | 94 new GamepadService(std::unique_ptr<device::GamepadDataFetcher>(fetcher_)); |
99 connection_listener_.reset((new ConnectionListener)); | 95 connection_listener_.reset((new ConnectionListener)); |
100 service_->SetSanitizationEnabled(false); | 96 service_->SetSanitizationEnabled(false); |
101 service_->ConsumerBecameActive(connection_listener_.get()); | 97 service_->ConsumerBecameActive(connection_listener_.get()); |
102 } | 98 } |
103 | 99 |
104 void GamepadServiceTest::SetPadsConnected(bool connected) { | 100 void GamepadServiceTest::SetPadsConnected(bool connected) { |
105 for (int i = 0; i < kNumberOfGamepads; ++i) { | 101 for (int i = 0; i < kNumberOfGamepads; ++i) { |
106 test_data_.items[i].connected = connected; | 102 test_data_.items[i].connected = connected; |
107 } | 103 } |
108 fetcher_->SetTestData(test_data_); | 104 fetcher_->SetTestData(test_data_); |
(...skipping 18 matching lines...) Expand all Loading... |
127 SetPadsConnected(false); | 123 SetPadsConnected(false); |
128 WaitForData(); | 124 WaitForData(); |
129 EXPECT_EQ(0, GetConnectedCounter()); | 125 EXPECT_EQ(0, GetConnectedCounter()); |
130 EXPECT_EQ(kNumberOfGamepads, GetDisconnectedCounter()); | 126 EXPECT_EQ(kNumberOfGamepads, GetDisconnectedCounter()); |
131 | 127 |
132 WaitForData(); | 128 WaitForData(); |
133 EXPECT_EQ(0, GetConnectedCounter()); | 129 EXPECT_EQ(0, GetConnectedCounter()); |
134 EXPECT_EQ(0, GetDisconnectedCounter()); | 130 EXPECT_EQ(0, GetDisconnectedCounter()); |
135 } | 131 } |
136 | 132 |
137 } // namespace content | 133 } // namespace device |
OLD | NEW |