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

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: fixed nits. only sends gamepad updates if an exo window is in focus. 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/gamepad.h"
6 #include "components/exo/gamepad_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 MockGamepadDelegate : public GamepadDelegate {
16 public:
17 MockGamepadDelegate() {}
18
19 // Overridden from GamepadDelegate:
20 MOCK_METHOD1(OnGamepadDestroying, void(Gamepad*));
21 MOCK_METHOD1(OnStateChange, void(bool));
22 MOCK_METHOD2(OnAxis, void(int, double));
23 MOCK_METHOD3(OnButton, void(int, bool, double));
24 MOCK_METHOD0(OnFrame, void());
25 };
26
27 class GamepadTest : public testing::Test, public device::GamepadTestHelper {
28 public:
29 GamepadTest() {}
30
31 void InitializeGamepad(MockGamepadDelegate* delegate) {
32 blink::WebGamepads initial_data;
33 mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data);
34
35 gamepad_.reset(new Gamepad(
36 delegate,
37 std::unique_ptr<device::GamepadDataFetcher>(mock_data_fetcher_)));
38 }
39
40 void DestroyGamepad(MockGamepadDelegate* delegate) {
41 EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1);
42 gamepad_.reset();
43 }
44
45 void SetDataAndWaitForDelegate(const blink::WebGamepads& new_data) {
46 mock_data_fetcher_->SetTestData(new_data);
47 // Wait for 2 polling cycles for data to be read and callbacks
48 // to be processed.
49 for (int i = 0; i < 1; ++i) {
50 mock_data_fetcher_->WaitForDataRead();
51 message_loop().RunUntilIdle();
52 }
53 }
54
55 protected:
56 std::unique_ptr<Gamepad> gamepad_;
57
58 // Pointer owned by the gamepad instance.
59 device::MockGamepadDataFetcher* mock_data_fetcher_;
60
61 DISALLOW_COPY_AND_ASSIGN(GamepadTest);
62 };
63
64 TEST_F(GamepadTest, ConnectDisconnect) {
65 MockGamepadDelegate delegate;
66 InitializeGamepad(&delegate);
67
68 // Gamepad connected.
69 EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
70 blink::WebGamepads gamepad_connected;
71 gamepad_connected.length = 1;
72 gamepad_connected.items[0].connected = true;
73 gamepad_connected.items[0].timestamp = 1;
74 SetDataAndWaitForDelegate(gamepad_connected);
75
76 // Gamepad disconnected.
77 blink::WebGamepads all_disconnected;
78 EXPECT_CALL(delegate, OnStateChange(false)).Times(1);
79 SetDataAndWaitForDelegate(all_disconnected);
80
81 DestroyGamepad(&delegate);
82 }
83
84 TEST_F(GamepadTest, AxisMove) {
85 MockGamepadDelegate delegate;
86 InitializeGamepad(&delegate);
87
88 blink::WebGamepads axis_moved;
89 axis_moved.length = 1;
90 axis_moved.items[0].connected = true;
91 axis_moved.items[0].timestamp = 1;
92 axis_moved.items[0].axesLength = 1;
93 axis_moved.items[0].axes[0] = 1.0;
94
95 // Gamepad connected.
96 EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
97 EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1);
98 EXPECT_CALL(delegate, OnFrame()).Times(1);
99 SetDataAndWaitForDelegate(axis_moved);
100
101 DestroyGamepad(&delegate);
102 }
103
104 } // namespace
105 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698