Chromium Code Reviews| 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 { | |
| 36 public: | |
| 37 GamepadTest() {} | |
| 38 | |
| 39 void SetUp() override { | |
| 40 test::ExoTestBase::SetUp(); | |
| 41 surface_.reset(new Surface); | |
| 42 shell_surface_.reset(new ShellSurface(surface_.get())); | |
| 43 gfx::Size buffer_size(10, 10); | |
| 44 buffer_.reset( | |
| 45 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); | |
| 46 surface_->Attach(buffer_.get()); | |
| 47 surface_->Commit(); | |
| 48 } | |
| 49 | |
| 50 void TearDown() override { | |
| 51 buffer_.reset(); | |
| 52 shell_surface_.reset(); | |
| 53 surface_.reset(); | |
| 54 test::ExoTestBase::TearDown(); | |
| 55 } | |
| 56 | |
| 57 void InitializeGamepad(MockGamepadDelegate* delegate) { | |
| 58 blink::WebGamepads initial_data; | |
| 59 mock_data_fetcher_.reset(new device::MockGamepadDataFetcher(initial_data)); | |
| 60 | |
| 61 gamepad_.reset(new Gamepad(delegate, mock_data_fetcher_.get())); | |
| 62 } | |
| 63 | |
| 64 void DestroyGamepad(MockGamepadDelegate* delegate) { | |
| 65 EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1); | |
| 66 gamepad_.reset(); | |
| 67 } | |
| 68 | |
| 69 void SetDataAndWaitForDelegate(const blink::WebGamepads& new_data) { | |
| 70 mock_data_fetcher_->SetTestData(new_data); | |
| 71 // Wait for 2 polling cycles for data to be read and callbacks | |
| 72 // to be processed. | |
| 73 for (int i = 0; i < 1; ++i) { | |
|
reveman
2016/07/07 22:14:03
this loop doesn't seem to be doing anything as i <
| |
| 74 mock_data_fetcher_->WaitForDataRead(); | |
| 75 base::MessageLoop::current()->RunUntilIdle(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 std::unique_ptr<Gamepad> gamepad_; | |
| 81 | |
| 82 // Pointer owned by the gamepad instance. | |
|
reveman
2016/07/07 22:14:03
this doesn't seem correct anymore
| |
| 83 std::unique_ptr<device::MockGamepadDataFetcher> mock_data_fetcher_; | |
| 84 | |
| 85 std::unique_ptr<Surface> surface_; | |
| 86 std::unique_ptr<ShellSurface> shell_surface_; | |
| 87 std::unique_ptr<Buffer> buffer_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(GamepadTest); | |
| 90 }; | |
| 91 | |
| 92 TEST_F(GamepadTest, OnStateChange) { | |
| 93 testing::StrictMock<MockGamepadDelegate> delegate; | |
| 94 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
| 95 .WillOnce(testing::Return(true)); | |
| 96 | |
| 97 InitializeGamepad(&delegate); | |
| 98 | |
| 99 // Gamepad connected. | |
| 100 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
| 101 blink::WebGamepads gamepad_connected; | |
| 102 gamepad_connected.length = 1; | |
| 103 gamepad_connected.items[0].connected = true; | |
| 104 gamepad_connected.items[0].timestamp = 1; | |
| 105 SetDataAndWaitForDelegate(gamepad_connected); | |
| 106 | |
| 107 // Gamepad disconnected. | |
| 108 blink::WebGamepads all_disconnected; | |
| 109 EXPECT_CALL(delegate, OnStateChange(false)).Times(1); | |
| 110 SetDataAndWaitForDelegate(all_disconnected); | |
| 111 | |
| 112 DestroyGamepad(&delegate); | |
| 113 } | |
| 114 | |
| 115 TEST_F(GamepadTest, OnAxis) { | |
| 116 testing::StrictMock<MockGamepadDelegate> delegate; | |
| 117 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
| 118 .WillOnce(testing::Return(true)); | |
| 119 | |
| 120 InitializeGamepad(&delegate); | |
| 121 | |
| 122 blink::WebGamepads axis_moved; | |
| 123 axis_moved.length = 1; | |
| 124 axis_moved.items[0].connected = true; | |
| 125 axis_moved.items[0].timestamp = 1; | |
| 126 axis_moved.items[0].axesLength = 1; | |
| 127 axis_moved.items[0].axes[0] = 1.0; | |
| 128 | |
| 129 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
| 130 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1); | |
| 131 EXPECT_CALL(delegate, OnFrame()).Times(1); | |
| 132 SetDataAndWaitForDelegate(axis_moved); | |
| 133 | |
| 134 DestroyGamepad(&delegate); | |
| 135 } | |
| 136 | |
| 137 TEST_F(GamepadTest, OnButton) { | |
| 138 testing::StrictMock<MockGamepadDelegate> delegate; | |
| 139 EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) | |
| 140 .WillOnce(testing::Return(true)); | |
| 141 | |
| 142 InitializeGamepad(&delegate); | |
| 143 | |
| 144 blink::WebGamepads axis_moved; | |
| 145 axis_moved.length = 1; | |
| 146 axis_moved.items[0].connected = true; | |
| 147 axis_moved.items[0].timestamp = 1; | |
| 148 axis_moved.items[0].buttonsLength = 1; | |
| 149 axis_moved.items[0].buttons[0].pressed = true; | |
| 150 axis_moved.items[0].buttons[0].value = 1.0; | |
| 151 | |
| 152 EXPECT_CALL(delegate, OnStateChange(true)).Times(1); | |
| 153 EXPECT_CALL(delegate, OnButton(0, true, 1.0)).Times(1); | |
| 154 EXPECT_CALL(delegate, OnFrame()).Times(1); | |
| 155 SetDataAndWaitForDelegate(axis_moved); | |
| 156 | |
| 157 DestroyGamepad(&delegate); | |
| 158 } | |
| 159 | |
| 160 } // namespace | |
| 161 } // namespace exo | |
| OLD | NEW |