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

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

Powered by Google App Engine
This is Rietveld 408576698