OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "ash/shell.h" | |
6 #include "base/command_line.h" | |
7 #include "components/exo/buffer.h" | |
8 #include "components/exo/gamepad.h" | |
9 #include "components/exo/gamepad_delegate.h" | |
10 #include "components/exo/shell_surface.h" | |
11 #include "components/exo/surface.h" | |
12 #include "components/exo/test/exo_test_base.h" | |
13 #include "components/exo/test/exo_test_helper.h" | |
14 #include "device/gamepad/gamepad_test_helpers.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 #include "ui/aura/client/focus_client.h" | |
18 | |
19 namespace exo { | |
20 namespace { | |
21 | |
22 class MockGamepadDelegate : public GamepadDelegate { | |
23 public: | |
24 MockGamepadDelegate() {} | |
25 | |
26 // Overridden from GamepadDelegate: | |
27 MOCK_METHOD1(OnGamepadDestroying, void(Gamepad*)); | |
28 MOCK_CONST_METHOD1(CanAcceptGamepadEventsForSurface, bool(Surface*)); | |
29 MOCK_METHOD1(OnStateChange, void(bool)); | |
30 MOCK_METHOD2(OnAxis, void(int, double)); | |
31 MOCK_METHOD3(OnButton, void(int, bool, double)); | |
32 MOCK_METHOD0(OnFrame, void()); | |
33 }; | |
34 | |
35 class GamepadTest : public test::ExoTestBase { | |
reveman
2016/06/30 18:06:12
Consider avoiding this test fixture to be consiste
denniskempin
2016/07/01 02:34:36
I am not quite sure I understand your reasoning to
reveman
2016/07/07 22:14:02
Consistency would be the benefit and imo a suffici
| |
36 public: | |
37 GamepadTest() {} | |
38 | |
39 void FocusNonExo() { | |
reveman
2016/06/30 18:06:12
nit: FocusNonClientSurface. However, I think it's
denniskempin
2016/07/01 02:34:35
no longer needed.
| |
40 aura::client::FocusClient* focus_client = | |
41 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); | |
42 focus_client->FocusWindow(nullptr); | |
43 } | |
44 | |
45 void FocusExo() { | |
reveman
2016/06/30 18:06:12
ditto
denniskempin
2016/07/01 02:34:36
no longer needed.
| |
46 aura::client::FocusClient* focus_client = | |
47 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); | |
48 focus_client->FocusWindow(surface_->window()); | |
49 } | |
50 | |
51 void SetUp() override { | |
52 test::ExoTestBase::SetUp(); | |
53 surface_.reset(new Surface); | |
54 shell_surface_.reset(new ShellSurface(surface_.get())); | |
55 gfx::Size buffer_size(10, 10); | |
56 buffer_.reset( | |
57 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); | |
58 surface_->Attach(buffer_.get()); | |
59 surface_->Commit(); | |
60 } | |
61 | |
62 void TearDown() override { | |
63 buffer_.reset(); | |
64 shell_surface_.reset(); | |
65 surface_.reset(); | |
66 test::ExoTestBase::TearDown(); | |
67 } | |
68 | |
69 void InitializeGamepad(MockGamepadDelegate* delegate) { | |
70 blink::WebGamepads initial_data; | |
71 mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data); | |
reveman
2016/06/30 18:06:11
nit: can you refactor the Gamepad code so it doesn
denniskempin
2016/07/01 02:34:36
Done.
| |
72 | |
73 gamepad_.reset(new Gamepad( | |
74 delegate, | |
75 std::unique_ptr<device::GamepadDataFetcher>(mock_data_fetcher_))); | |
76 } | |
77 | |
78 void DestroyGamepad(MockGamepadDelegate* delegate) { | |
79 EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1); | |
80 gamepad_.reset(); | |
81 } | |
82 | |
83 void SetDataAndWaitForDelegate(const blink::WebGamepads& new_data) { | |
84 mock_data_fetcher_->SetTestData(new_data); | |
85 // Wait for 2 polling cycles for data to be read and callbacks | |
86 // to be processed. | |
reveman
2016/06/30 18:06:11
It's strongly discouraged to have tests that depen
denniskempin
2016/07/01 02:34:36
This is not based on timers, but based on the poll
denniskempin
2016/07/01 17:16:50
I've been looking into just providing a TaskRunner
reveman
2016/07/07 22:14:02
Does changing the polling frequency to 16 seconds
| |
87 for (int i = 0; i < 2; ++i) { | |
88 mock_data_fetcher_->WaitForDataRead(); | |
89 base::MessageLoop::current()->RunUntilIdle(); | |
90 } | |
91 } | |
92 | |
93 protected: | |
94 std::unique_ptr<Gamepad> gamepad_; | |
95 | |
96 // Pointer owned by the gamepad instance. | |
97 device::MockGamepadDataFetcher* mock_data_fetcher_; | |
98 | |
99 std::unique_ptr<Surface> surface_; | |
100 std::unique_ptr<ShellSurface> shell_surface_; | |
101 std::unique_ptr<Buffer> buffer_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(GamepadTest); | |
104 }; | |
105 | |
106 TEST_F(GamepadTest, ConnectDisconnect) { | |
reveman
2016/06/30 18:06:12
Can you name the tests based on the delegate funct
denniskempin
2016/07/01 02:34:35
Done.
| |
107 testing::StrictMock<MockGamepadDelegate> delegate; | |
108 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
109 .WillOnce(testing::Return(true)); | |
110 | |
111 InitializeGamepad(&delegate); | |
112 | |
113 // Gamepad connected. | |
114 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
115 blink::WebGamepads gamepad_connected; | |
116 gamepad_connected.length = 1; | |
117 gamepad_connected.items[0].connected = true; | |
118 gamepad_connected.items[0].timestamp = 1; | |
119 SetDataAndWaitForDelegate(gamepad_connected); | |
120 | |
121 // Gamepad disconnected. | |
122 blink::WebGamepads all_disconnected; | |
123 EXPECT_CALL(delegate, OnStateChange(false)).Times(1); | |
124 SetDataAndWaitForDelegate(all_disconnected); | |
125 | |
126 DestroyGamepad(&delegate); | |
127 } | |
128 | |
129 TEST_F(GamepadTest, AxisMove) { | |
reveman
2016/06/30 18:06:12
s/AxisMove/OnAxis/?
denniskempin
2016/07/01 02:34:35
Done.
| |
130 testing::StrictMock<MockGamepadDelegate> delegate; | |
131 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
132 .WillOnce(testing::Return(true)); | |
133 | |
134 InitializeGamepad(&delegate); | |
135 | |
136 blink::WebGamepads axis_moved; | |
137 axis_moved.length = 1; | |
138 axis_moved.items[0].connected = true; | |
139 axis_moved.items[0].timestamp = 1; | |
140 axis_moved.items[0].axesLength = 1; | |
141 axis_moved.items[0].axes[0] = 1.0; | |
142 | |
143 // Gamepad connected. | |
144 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
145 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1); | |
146 EXPECT_CALL(delegate, OnFrame()).Times(1); | |
147 SetDataAndWaitForDelegate(axis_moved); | |
148 | |
149 DestroyGamepad(&delegate); | |
150 } | |
151 | |
152 TEST_F(GamepadTest, FocusHandling) { | |
reveman
2016/06/30 18:06:12
s/FocusHandling/CanAcceptGamepadEventsForSurface/
denniskempin
2016/07/01 02:34:35
I had to remove the focus handling test since I am
| |
153 testing::StrictMock<MockGamepadDelegate> delegate; | |
154 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
155 .WillOnce(testing::Return(true)); | |
156 | |
157 InitializeGamepad(&delegate); | |
158 FocusNonExo(); | |
159 | |
160 blink::WebGamepads axis_moved; | |
161 axis_moved.length = 1; | |
162 axis_moved.items[0].connected = true; | |
163 axis_moved.items[0].timestamp = 1; | |
164 axis_moved.items[0].axesLength = 2; | |
165 axis_moved.items[0].axes[0] = 1.0; | |
166 axis_moved.items[0].axes[1] = 0; | |
167 | |
168 // Exo is not in focus.. should not cause any delegate calls. | |
169 SetDataAndWaitForDelegate(axis_moved); | |
170 testing::Mock::VerifyAndClearExpectations(&delegate); | |
171 | |
172 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
173 .WillOnce(testing::Return(true)); | |
174 | |
175 // Exo becomes focused, update delegate with full state on next gamepad state. | |
176 FocusExo(); | |
177 blink::WebGamepads axis_moved_2 = axis_moved; | |
178 axis_moved_2.items[0].timestamp = 2; | |
179 axis_moved_2.items[0].axes[1] = 1.0; | |
180 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
181 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1); | |
182 EXPECT_CALL(delegate, OnAxis(1, 1.0)).Times(1); | |
183 EXPECT_CALL(delegate, OnFrame()).Times(1); | |
184 SetDataAndWaitForDelegate(axis_moved_2); | |
185 | |
186 DestroyGamepad(&delegate); | |
187 } | |
188 | |
189 } // namespace | |
reveman
2016/06/30 18:06:12
Can you add a simple test for OnButton too?
denniskempin
2016/07/01 02:34:35
Done.
| |
190 } // namespace exo | |
OLD | NEW |