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 "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) { |
| 49 polling_task_runner_ = new base::TestSimpleTaskRunner(); |
| 50 gamepad_.reset(new Gamepad(delegate, polling_task_runner_.get(), |
| 51 base::Bind(&GamepadTest::MockDataFetcherFactory, |
| 52 base::Unretained(this)))); |
| 53 // Run the polling task runner to have it create the data fetcher. |
| 54 polling_task_runner_->RunPendingTasks(); |
| 55 } |
| 56 |
| 57 void DestroyGamepad(MockGamepadDelegate* delegate) { |
| 58 EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1); |
| 59 mock_data_fetcher_ = nullptr; |
| 60 gamepad_.reset(); |
| 61 // Process tasks until polling is shut down. |
| 62 polling_task_runner_->RunPendingTasks(); |
| 63 polling_task_runner_ = nullptr; |
| 64 } |
| 65 |
| 66 void SetDataAndPostToDelegate(const blink::WebGamepads& new_data) { |
| 67 ASSERT_TRUE(mock_data_fetcher_ != nullptr); |
| 68 mock_data_fetcher_->SetTestData(new_data); |
| 69 // Run one polling cycle, which will post a task to the origin task runner. |
| 70 polling_task_runner_->RunPendingTasks(); |
| 71 // Run origin task runner to invoke delegate. |
| 72 base::MessageLoop::current()->RunUntilIdle(); |
| 73 } |
| 74 |
| 75 protected: |
| 76 std::unique_ptr<Gamepad> gamepad_; |
| 77 |
| 78 // Task runner to simulate the polling thread. |
| 79 scoped_refptr<base::TestSimpleTaskRunner> polling_task_runner_; |
| 80 |
| 81 // Weak reference to the mock data fetcher provided by MockDataFetcherFactory. |
| 82 // This instance is valid until both gamepad_ and polling_task_runner_ are |
| 83 // shut down. |
| 84 device::MockGamepadDataFetcher* mock_data_fetcher_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(GamepadTest); |
| 87 }; |
| 88 |
| 89 TEST_F(GamepadTest, OnStateChange) { |
| 90 std::unique_ptr<Surface> surface(new Surface); |
| 91 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get())); |
| 92 gfx::Size buffer_size(10, 10); |
| 93 std::unique_ptr<Buffer> buffer( |
| 94 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); |
| 95 surface->Attach(buffer.get()); |
| 96 surface->Commit(); |
| 97 |
| 98 testing::StrictMock<MockGamepadDelegate> delegate; |
| 99 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
| 100 .WillOnce(testing::Return(true)); |
| 101 |
| 102 InitializeGamepad(&delegate); |
| 103 |
| 104 // Gamepad connected. |
| 105 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); |
| 106 blink::WebGamepads gamepad_connected; |
| 107 gamepad_connected.length = 1; |
| 108 gamepad_connected.items[0].connected = true; |
| 109 gamepad_connected.items[0].timestamp = 1; |
| 110 SetDataAndPostToDelegate(gamepad_connected); |
| 111 |
| 112 // Gamepad disconnected. |
| 113 blink::WebGamepads all_disconnected; |
| 114 EXPECT_CALL(delegate, OnStateChange(false)).Times(1); |
| 115 SetDataAndPostToDelegate(all_disconnected); |
| 116 |
| 117 DestroyGamepad(&delegate); |
| 118 } |
| 119 |
| 120 TEST_F(GamepadTest, OnAxis) { |
| 121 std::unique_ptr<Surface> surface(new Surface); |
| 122 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get())); |
| 123 gfx::Size buffer_size(10, 10); |
| 124 std::unique_ptr<Buffer> buffer( |
| 125 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); |
| 126 surface->Attach(buffer.get()); |
| 127 surface->Commit(); |
| 128 |
| 129 testing::StrictMock<MockGamepadDelegate> delegate; |
| 130 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
| 131 .WillOnce(testing::Return(true)); |
| 132 |
| 133 InitializeGamepad(&delegate); |
| 134 |
| 135 blink::WebGamepads axis_moved; |
| 136 axis_moved.length = 1; |
| 137 axis_moved.items[0].connected = true; |
| 138 axis_moved.items[0].timestamp = 1; |
| 139 axis_moved.items[0].axesLength = 1; |
| 140 axis_moved.items[0].axes[0] = 1.0; |
| 141 |
| 142 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); |
| 143 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1); |
| 144 EXPECT_CALL(delegate, OnFrame()).Times(1); |
| 145 SetDataAndPostToDelegate(axis_moved); |
| 146 |
| 147 DestroyGamepad(&delegate); |
| 148 } |
| 149 |
| 150 TEST_F(GamepadTest, OnButton) { |
| 151 std::unique_ptr<Surface> surface(new Surface); |
| 152 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get())); |
| 153 gfx::Size buffer_size(10, 10); |
| 154 std::unique_ptr<Buffer> buffer( |
| 155 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); |
| 156 surface->Attach(buffer.get()); |
| 157 surface->Commit(); |
| 158 |
| 159 testing::StrictMock<MockGamepadDelegate> delegate; |
| 160 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
| 161 .WillOnce(testing::Return(true)); |
| 162 |
| 163 InitializeGamepad(&delegate); |
| 164 |
| 165 blink::WebGamepads axis_moved; |
| 166 axis_moved.length = 1; |
| 167 axis_moved.items[0].connected = true; |
| 168 axis_moved.items[0].timestamp = 1; |
| 169 axis_moved.items[0].buttonsLength = 1; |
| 170 axis_moved.items[0].buttons[0].pressed = true; |
| 171 axis_moved.items[0].buttons[0].value = 1.0; |
| 172 |
| 173 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); |
| 174 EXPECT_CALL(delegate, OnButton(0, true, 1.0)).Times(1); |
| 175 EXPECT_CALL(delegate, OnFrame()).Times(1); |
| 176 SetDataAndPostToDelegate(axis_moved); |
| 177 |
| 178 DestroyGamepad(&delegate); |
| 179 } |
| 180 |
| 181 TEST_F(GamepadTest, OnWindowFocused) { |
| 182 // Create surface and move focus to it. |
| 183 std::unique_ptr<Surface> surface(new Surface); |
| 184 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get())); |
| 185 gfx::Size buffer_size(10, 10); |
| 186 std::unique_ptr<Buffer> buffer( |
| 187 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); |
| 188 surface->Attach(buffer.get()); |
| 189 surface->Commit(); |
| 190 |
| 191 testing::StrictMock<MockGamepadDelegate> delegate; |
| 192 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
| 193 .WillOnce(testing::Return(true)); |
| 194 |
| 195 InitializeGamepad(&delegate); |
| 196 |
| 197 // In focus. Should be polling indefinitely, check a couple of time that the |
| 198 // poll task is re-posted. |
| 199 for (size_t i = 0; i < 5; ++i) { |
| 200 polling_task_runner_->RunPendingTasks(); |
| 201 ASSERT_TRUE(polling_task_runner_->HasPendingTask()); |
| 202 } |
| 203 |
| 204 // Remove focus from 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 // In the first round of RunPendingTasks we will execute |
| 211 // EnablePollingOnPollingThread, which will cause the polling to stop being |
| 212 // scheduled in the next round. |
| 213 polling_task_runner_->RunPendingTasks(); |
| 214 polling_task_runner_->RunPendingTasks(); |
| 215 ASSERT_FALSE(polling_task_runner_->HasPendingTask()); |
| 216 |
| 217 DestroyGamepad(&delegate); |
| 218 } |
| 219 |
| 220 } // namespace |
| 221 } // namespace exo |
OLD | NEW |