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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
Index: components/exo/gamepads_unittest.cc
diff --git a/components/exo/gamepads_unittest.cc b/components/exo/gamepads_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..60e78203c32266e8e8185b753cb17810ae6e838e
--- /dev/null
+++ b/components/exo/gamepads_unittest.cc
@@ -0,0 +1,114 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/exo/gamepads.h"
+#include "components/exo/gamepads_delegate.h"
+#include "components/exo/test/exo_test_base.h"
+#include "device/gamepad/gamepad_test_helpers.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace exo {
+namespace {
+
+class MockGamepadsDelegate : public GamepadsDelegate {
+ public:
+ MockGamepadsDelegate() {}
+
+ // Overridden from GamepadsDelegate:
+ MOCK_METHOD1(OnGamepadsDestroying, void(Gamepads*));
+
+ MOCK_METHOD1(OnConnected, void(int));
+ MOCK_METHOD1(OnDisconnected, void(int));
+
+ MOCK_METHOD3(OnAxis, void(int, int, double));
+ MOCK_METHOD4(OnButton, void(int, int, bool, double));
+ MOCK_METHOD0(OnFrame, void());
+};
+
+class GamepadsTests : public testing::Test, public device::GamepadTestHelper {
+ public:
+ GamepadsTests() {}
+
+ void InitializeGamepads(MockGamepadsDelegate* delegate) {
+ blink::WebGamepads initial_data;
+ mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data);
+
+ gamepads_.reset(new Gamepads(
+ delegate,
+ std::unique_ptr<device::GamepadDataFetcher>(mock_data_fetcher_)));
+ }
+
+ void DestroyGamepads(MockGamepadsDelegate* delegate) {
+ EXPECT_CALL(*delegate, OnGamepadsDestroying(testing::_)).Times(1);
+ gamepads_.reset();
+ }
+
+ void SetDataAndWaitForDelegate(const blink::WebGamepads& new_data) {
+ mock_data_fetcher_->SetTestData(new_data);
+ // Wait for 2 polling cycles for data to be read and callbacks
+ // to be processed.
+ for (int i = 0; i < 2; ++i) {
+ mock_data_fetcher_->WaitForDataRead();
+ message_loop().RunUntilIdle();
+ }
+ }
+
+ protected:
+ std::unique_ptr<Gamepads> gamepads_;
+
+ // Pointer owned by the gamepads instance.
+ device::MockGamepadDataFetcher* mock_data_fetcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(GamepadsTests);
+};
+
+TEST_F(GamepadsTests, ConnectDisconnect) {
+ MockGamepadsDelegate delegate;
+ InitializeGamepads(&delegate);
+
+ blink::WebGamepads no_gamepads;
+ blink::WebGamepads one_gamepad;
+ one_gamepad.length = 1;
+ one_gamepad.items[0].connected = true;
+ one_gamepad.items[0].timestamp = 1;
+ one_gamepad.items[0].buttonsLength = 1;
+ one_gamepad.items[0].buttons[0].pressed = true;
+ one_gamepad.items[0].buttons[0].value = 1.0f;
+
+ // Gamepad connected
+ EXPECT_CALL(delegate, OnConnected(0)).Times(1);
+ EXPECT_CALL(delegate, OnButton(0, 0, true, 1.0f)).Times(1);
+ EXPECT_CALL(delegate, OnFrame()).Times(1);
+ SetDataAndWaitForDelegate(one_gamepad);
+
+ // Gamepad disconnected
+ EXPECT_CALL(delegate, OnDisconnected(0)).Times(1);
+ SetDataAndWaitForDelegate(no_gamepads);
+
+ DestroyGamepads(&delegate);
+}
+
+TEST_F(GamepadsTests, AxisMove) {
+ MockGamepadsDelegate delegate;
+ InitializeGamepads(&delegate);
+
+ blink::WebGamepads axis_moved;
+ axis_moved.length = 1;
+ axis_moved.items[0].connected = true;
+ axis_moved.items[0].timestamp = 1;
+ axis_moved.items[0].axesLength = 1;
+ axis_moved.items[0].axes[0] = 1.0;
+
+ // Gamepad connected
+ EXPECT_CALL(delegate, OnConnected(0)).Times(1);
+ EXPECT_CALL(delegate, OnAxis(0, 0, 1.0)).Times(1);
+ EXPECT_CALL(delegate, OnFrame()).Times(1);
+ SetDataAndWaitForDelegate(axis_moved);
+
+ DestroyGamepads(&delegate);
+}
+
+} // namespace
+} // namespace exo

Powered by Google App Engine
This is Rietveld 408576698