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

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

Issue 2076013002: exo: Implement wayland gamepad support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serv
Patch Set: minor adjustments to protocol 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 "components/exo/gamepads.h"
6 #include "components/exo/gamepads_delegate.h"
7 #include "components/exo/test/exo_test_base.h"
8 #include "device/gamepad/gamepad_test_helpers.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace exo {
13 namespace {
14
15 class MockGamepadsDelegate : public GamepadsDelegate {
16 public:
17 MockGamepadsDelegate() {}
18
19 // Overridden from GamepadsDelegate:
20 MOCK_METHOD1(OnGamepadsDestroying, void(Gamepads*));
21
22 MOCK_METHOD1(OnConnected, void(int));
23 MOCK_METHOD1(OnDisconnected, void(int));
24
25 MOCK_METHOD3(OnAxis, void(int, int, double));
26 MOCK_METHOD4(OnButton, void(int, int, bool, double));
27 MOCK_METHOD0(OnFrame, void());
28 };
29
30 class GamepadsTests : public testing::Test, public device::GamepadTestHelper {
31 public:
32 GamepadsTests() {}
33
34 void InitializeGamepads(MockGamepadsDelegate* delegate) {
35 blink::WebGamepads initial_data;
36 mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data);
37
38 gamepads_.reset(new Gamepads(
39 delegate,
40 std::unique_ptr<device::GamepadDataFetcher>(mock_data_fetcher_)));
41 }
42
43 void DestroyGamepads(MockGamepadsDelegate* delegate) {
44 EXPECT_CALL(*delegate, OnGamepadsDestroying(testing::_)).Times(1);
45 gamepads_.reset();
46 }
47
48 void SetDataAndWaitForDelegate(const blink::WebGamepads& new_data) {
49 mock_data_fetcher_->SetTestData(new_data);
50 // Wait for 2 polling cycles for data to be read and callbacks
51 // to be processed.
52 for (int i = 0; i < 2; ++i) {
53 mock_data_fetcher_->WaitForDataRead();
54 message_loop().RunUntilIdle();
55 }
56 }
57
58 protected:
59 std::unique_ptr<Gamepads> gamepads_;
60
61 // Pointer owned by the gamepads instance.
62 device::MockGamepadDataFetcher* mock_data_fetcher_;
63
64 DISALLOW_COPY_AND_ASSIGN(GamepadsTests);
65 };
66
67 TEST_F(GamepadsTests, ConnectDisconnect) {
68 MockGamepadsDelegate delegate;
69 InitializeGamepads(&delegate);
70
71 blink::WebGamepads no_gamepads;
72 blink::WebGamepads one_gamepad;
73 one_gamepad.length = 1;
74 one_gamepad.items[0].connected = true;
75 one_gamepad.items[0].timestamp = 1;
76 one_gamepad.items[0].buttonsLength = 1;
77 one_gamepad.items[0].buttons[0].pressed = true;
78 one_gamepad.items[0].buttons[0].value = 1.0f;
79
80 // Gamepad connected
81 EXPECT_CALL(delegate, OnConnected(0)).Times(1);
82 EXPECT_CALL(delegate, OnButton(0, 0, true, 1.0f)).Times(1);
83 EXPECT_CALL(delegate, OnFrame()).Times(1);
84 SetDataAndWaitForDelegate(one_gamepad);
85
86 // Gamepad disconnected
87 EXPECT_CALL(delegate, OnDisconnected(0)).Times(1);
88 SetDataAndWaitForDelegate(no_gamepads);
89
90 DestroyGamepads(&delegate);
91 }
92
93 TEST_F(GamepadsTests, AxisMove) {
94 MockGamepadsDelegate delegate;
95 InitializeGamepads(&delegate);
96
97 blink::WebGamepads axis_moved;
98 axis_moved.length = 1;
99 axis_moved.items[0].connected = true;
100 axis_moved.items[0].timestamp = 1;
101 axis_moved.items[0].axesLength = 1;
102 axis_moved.items[0].axes[0] = 1.0;
103
104 // Gamepad connected
105 EXPECT_CALL(delegate, OnConnected(0)).Times(1);
106 EXPECT_CALL(delegate, OnAxis(0, 0, 1.0)).Times(1);
107 EXPECT_CALL(delegate, OnFrame()).Times(1);
108 SetDataAndWaitForDelegate(axis_moved);
109
110 DestroyGamepads(&delegate);
111 }
112
113 } // namespace
114 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698