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

Unified 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, 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/gamepad_unittest.cc
diff --git a/components/exo/gamepad_unittest.cc b/components/exo/gamepad_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fc89621fc76b818f90fd77a8b217c065a124c404
--- /dev/null
+++ b/components/exo/gamepad_unittest.cc
@@ -0,0 +1,107 @@
+// 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/gamepad.h"
+#include "components/exo/gamepad_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 MockGamepadDelegate : public GamepadDelegate {
+ public:
+ MockGamepadDelegate() {}
+
+ // Overridden from GamepadDelegate:
+ MOCK_METHOD1(OnGamepadDestroying, void(Gamepad*));
+
+ MOCK_METHOD1(OnStateChange, void(bool));
+
+ MOCK_METHOD2(OnAxis, void(int, double));
+ MOCK_METHOD3(OnButton, void(int, bool, double));
+ MOCK_METHOD0(OnFrame, void());
+};
+
+class GamepadTests : public testing::Test, public device::GamepadTestHelper {
+ public:
+ GamepadTests() {}
+
+ void InitializeGamepad(MockGamepadDelegate* delegate) {
+ blink::WebGamepads initial_data;
+ mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data);
+
+ gamepad_.reset(new Gamepad(
+ 0, delegate,
+ std::unique_ptr<device::GamepadDataFetcher>(mock_data_fetcher_)));
+ }
+
+ void DestroyGamepad(MockGamepadDelegate* delegate) {
+ EXPECT_CALL(*delegate, OnGamepadDestroying(testing::_)).Times(1);
+ gamepad_.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 < 1; ++i) {
+ mock_data_fetcher_->WaitForDataRead();
+ message_loop().RunUntilIdle();
+ }
+ }
+
+ protected:
+ std::unique_ptr<Gamepad> gamepad_;
+
+ // Pointer owned by the gamepad instance.
+ device::MockGamepadDataFetcher* mock_data_fetcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(GamepadTests);
+};
+
+TEST_F(GamepadTests, ConnectDisconnect) {
+ MockGamepadDelegate delegate;
+ InitializeGamepad(&delegate);
+
+ // Gamepad connected
+ EXPECT_CALL(delegate, OnStateChange(true)).Times(1);
+ blink::WebGamepads gamepad_connected;
+ gamepad_connected.length = 1;
+ gamepad_connected.items[0].connected = true;
+ gamepad_connected.items[0].timestamp = 1;
+ SetDataAndWaitForDelegate(gamepad_connected);
+
+ // Gamepad disconnected
+ blink::WebGamepads all_disconnected;
+ EXPECT_CALL(delegate, OnStateChange(false)).Times(1);
+ SetDataAndWaitForDelegate(all_disconnected);
+
+ DestroyGamepad(&delegate);
+}
+
+TEST_F(GamepadTests, AxisMove) {
+ MockGamepadDelegate delegate;
+ InitializeGamepad(&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, OnStateChange(true)).Times(1);
+ EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1);
+ EXPECT_CALL(delegate, OnFrame()).Times(1);
+ SetDataAndWaitForDelegate(axis_moved);
+
+ DestroyGamepad(&delegate);
+}
+
+} // namespace
+} // namespace exo

Powered by Google App Engine
This is Rietveld 408576698