Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(839)

Side by Side Diff: components/exo/gamepad_unittest.cc

Issue 2076013002: exo: Implement wayland gamepad support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serv
Patch Set: moved polling thread resources into separate class. pass task_runner into Gamepad. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/test/test_simple_task_runner.h"
8 #include "components/exo/buffer.h"
9 #include "components/exo/gamepad.h"
10 #include "components/exo/gamepad_delegate.h"
11 #include "components/exo/shell_surface.h"
12 #include "components/exo/surface.h"
13 #include "components/exo/test/exo_test_base.h"
14 #include "components/exo/test/exo_test_helper.h"
15 #include "device/gamepad/gamepad_test_helpers.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/aura/client/focus_client.h"
19
20 namespace exo {
21 namespace {
22
23 class MockGamepadDelegate : public GamepadDelegate {
24 public:
25 MockGamepadDelegate() {}
26
27 // Overridden from GamepadDelegate:
28 MOCK_METHOD1(OnGamepadDestroying, void(Gamepad*));
29 MOCK_CONST_METHOD1(CanAcceptGamepadEventsForSurface, bool(Surface*));
30 MOCK_METHOD1(OnStateChange, void(bool));
31 MOCK_METHOD2(OnAxis, void(int, double));
32 MOCK_METHOD3(OnButton, void(int, bool, double));
33 MOCK_METHOD0(OnFrame, void());
34 };
35
36 class GamepadTest : public test::ExoTestBase {
37 public:
38 GamepadTest() {}
39
40 std::unique_ptr<device::GamepadDataFetcher> MockDataFetcherFactory() {
41 blink::WebGamepads initial_data;
42 std::unique_ptr<device::MockGamepadDataFetcher> fetcher(
43 new device::MockGamepadDataFetcher(initial_data));
44 mock_data_fetcher_ = fetcher.get();
45 return std::move(fetcher);
46 }
47
48 void InitializeGamepad(MockGamepadDelegate* delegate) {
reveman 2016/07/11 10:50:44 Would be nice to have one test that verifies that
denniskempin 2016/07/11 17:43:29 I think that would be a good thing to do when we a
49 polling_task_runner_ = new base::TestSimpleTaskRunner();
50 gamepad_.reset(new Gamepad(delegate,
51 make_scoped_refptr(polling_task_runner_.get()),
52 base::Bind(&GamepadTest::MockDataFetcherFactory,
53 base::Unretained(this))));
54 // Run the polling task runner to have it create the data fetcher.
55 polling_task_runner_->RunPendingTasks();
56 }
57
58 void DestroyGamepad(MockGamepadDelegate* delegate) {
59 EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1);
60 mock_data_fetcher_ = nullptr;
61 gamepad_.reset();
62 // Process tasks until polling is shut down.
reveman 2016/07/11 10:50:44 are the following lines really needed?
denniskempin 2016/07/11 17:43:29 Since polling is happening independently of the Ga
63 polling_task_runner_->RunUntilIdle();
64 polling_task_runner_ = nullptr;
65 }
66
67 void SetDataAndPostToDelegate(const blink::WebGamepads& new_data) {
68 ASSERT_TRUE(mock_data_fetcher_ != nullptr);
69 mock_data_fetcher_->SetTestData(new_data);
70 // Run one polling cycle, which will post a task to the origin task runner.
71 polling_task_runner_->RunPendingTasks();
72 // Run origin task runner to invoke delegate
73 base::MessageLoop::current()->RunUntilIdle();
74 }
75
76 protected:
77 std::unique_ptr<Gamepad> gamepad_;
78
79 // Task runner to simulate the polling thread.
80 scoped_refptr<base::TestSimpleTaskRunner> polling_task_runner_;
81
82 // Weak reference to the mock data fetcher provided by MockDataFetcherFactory.
83 // This instance is valid until both gamepad_ and polling_task_runner_ are
84 // shut down.
85 device::MockGamepadDataFetcher* mock_data_fetcher_;
86
87 DISALLOW_COPY_AND_ASSIGN(GamepadTest);
88 };
89
90 TEST_F(GamepadTest, OnStateChange) {
91 std::unique_ptr<Surface> surface(new Surface);
92 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
93 gfx::Size buffer_size(10, 10);
94 std::unique_ptr<Buffer> buffer(
95 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
96 surface->Attach(buffer.get());
97 surface->Commit();
98
99 testing::StrictMock<MockGamepadDelegate> delegate;
100 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_))
101 .WillOnce(testing::Return(true));
102
103 InitializeGamepad(&delegate);
104
105 // Gamepad connected.
106 EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
107 blink::WebGamepads gamepad_connected;
108 gamepad_connected.length = 1;
109 gamepad_connected.items[0].connected = true;
110 gamepad_connected.items[0].timestamp = 1;
111 SetDataAndPostToDelegate(gamepad_connected);
112
113 // Gamepad disconnected.
114 blink::WebGamepads all_disconnected;
115 EXPECT_CALL(delegate, OnStateChange(false)).Times(1);
116 SetDataAndPostToDelegate(all_disconnected);
117
118 DestroyGamepad(&delegate);
119 }
120
121 TEST_F(GamepadTest, OnAxis) {
122 std::unique_ptr<Surface> surface(new Surface);
123 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
124 gfx::Size buffer_size(10, 10);
125 std::unique_ptr<Buffer> buffer(
126 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
127 surface->Attach(buffer.get());
128 surface->Commit();
129
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 EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
144 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1);
145 EXPECT_CALL(delegate, OnFrame()).Times(1);
146 SetDataAndPostToDelegate(axis_moved);
147
148 DestroyGamepad(&delegate);
149 }
150
151 TEST_F(GamepadTest, OnButton) {
152 std::unique_ptr<Surface> surface(new Surface);
153 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
154 gfx::Size buffer_size(10, 10);
155 std::unique_ptr<Buffer> buffer(
156 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
157 surface->Attach(buffer.get());
158 surface->Commit();
159
160 testing::StrictMock<MockGamepadDelegate> delegate;
161 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_))
162 .WillOnce(testing::Return(true));
163
164 InitializeGamepad(&delegate);
165
166 blink::WebGamepads axis_moved;
167 axis_moved.length = 1;
168 axis_moved.items[0].connected = true;
169 axis_moved.items[0].timestamp = 1;
170 axis_moved.items[0].buttonsLength = 1;
171 axis_moved.items[0].buttons[0].pressed = true;
172 axis_moved.items[0].buttons[0].value = 1.0;
173
174 EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
175 EXPECT_CALL(delegate, OnButton(0, true, 1.0)).Times(1);
176 EXPECT_CALL(delegate, OnFrame()).Times(1);
177 SetDataAndPostToDelegate(axis_moved);
178
179 DestroyGamepad(&delegate);
180 }
181
182 TEST_F(GamepadTest, OnWindowFocused) {
183 // Create surface and focus on it
184 std::unique_ptr<Surface> surface(new Surface);
185 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
186 gfx::Size buffer_size(10, 10);
187 std::unique_ptr<Buffer> buffer(
188 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
189 surface->Attach(buffer.get());
190 surface->Commit();
191
192 testing::StrictMock<MockGamepadDelegate> delegate;
193 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_))
194 .WillOnce(testing::Return(true));
195
196 InitializeGamepad(&delegate);
197
198 // In focus. Should be polling.
199 polling_task_runner_->RunPendingTasks();
200 ASSERT_TRUE(polling_task_runner_->HasPendingTask());
201 polling_task_runner_->RunPendingTasks();
202 ASSERT_TRUE(polling_task_runner_->HasPendingTask());
203
204 // Remove focus from exo window.
205 aura::client::FocusClient* focus_client =
206 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
207 focus_client->FocusWindow(nullptr);
208
209 // Run EnablePolling and OnPoll task, no more polls should be scheduled.
210 polling_task_runner_->RunPendingTasks();
211 polling_task_runner_->RunPendingTasks();
212 ASSERT_FALSE(polling_task_runner_->HasPendingTask());
213
214 DestroyGamepad(&delegate);
215 }
216
217 } // namespace
218 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698