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..244e33ce77199efbd5f9c6324a0b168c664119c1 |
--- /dev/null |
+++ b/components/exo/gamepad_unittest.cc |
@@ -0,0 +1,190 @@ |
+// 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 "ash/shell.h" |
+#include "base/command_line.h" |
+#include "components/exo/buffer.h" |
+#include "components/exo/gamepad.h" |
+#include "components/exo/gamepad_delegate.h" |
+#include "components/exo/shell_surface.h" |
+#include "components/exo/surface.h" |
+#include "components/exo/test/exo_test_base.h" |
+#include "components/exo/test/exo_test_helper.h" |
+#include "device/gamepad/gamepad_test_helpers.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/aura/client/focus_client.h" |
+ |
+namespace exo { |
+namespace { |
+ |
+class MockGamepadDelegate : public GamepadDelegate { |
+ public: |
+ MockGamepadDelegate() {} |
+ |
+ // Overridden from GamepadDelegate: |
+ MOCK_METHOD1(OnGamepadDestroying, void(Gamepad*)); |
+ MOCK_CONST_METHOD1(CanAcceptGamepadEventsForSurface, bool(Surface*)); |
+ MOCK_METHOD1(OnStateChange, void(bool)); |
+ MOCK_METHOD2(OnAxis, void(int, double)); |
+ MOCK_METHOD3(OnButton, void(int, bool, double)); |
+ MOCK_METHOD0(OnFrame, void()); |
+}; |
+ |
+class GamepadTest : public test::ExoTestBase { |
reveman
2016/06/30 18:06:12
Consider avoiding this test fixture to be consiste
denniskempin
2016/07/01 02:34:36
I am not quite sure I understand your reasoning to
reveman
2016/07/07 22:14:02
Consistency would be the benefit and imo a suffici
|
+ public: |
+ GamepadTest() {} |
+ |
+ void FocusNonExo() { |
reveman
2016/06/30 18:06:12
nit: FocusNonClientSurface. However, I think it's
denniskempin
2016/07/01 02:34:35
no longer needed.
|
+ aura::client::FocusClient* focus_client = |
+ aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
+ focus_client->FocusWindow(nullptr); |
+ } |
+ |
+ void FocusExo() { |
reveman
2016/06/30 18:06:12
ditto
denniskempin
2016/07/01 02:34:36
no longer needed.
|
+ aura::client::FocusClient* focus_client = |
+ aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
+ focus_client->FocusWindow(surface_->window()); |
+ } |
+ |
+ void SetUp() override { |
+ test::ExoTestBase::SetUp(); |
+ surface_.reset(new Surface); |
+ shell_surface_.reset(new ShellSurface(surface_.get())); |
+ gfx::Size buffer_size(10, 10); |
+ buffer_.reset( |
+ new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size))); |
+ surface_->Attach(buffer_.get()); |
+ surface_->Commit(); |
+ } |
+ |
+ void TearDown() override { |
+ buffer_.reset(); |
+ shell_surface_.reset(); |
+ surface_.reset(); |
+ test::ExoTestBase::TearDown(); |
+ } |
+ |
+ void InitializeGamepad(MockGamepadDelegate* delegate) { |
+ blink::WebGamepads initial_data; |
+ mock_data_fetcher_ = new device::MockGamepadDataFetcher(initial_data); |
reveman
2016/06/30 18:06:11
nit: can you refactor the Gamepad code so it doesn
denniskempin
2016/07/01 02:34:36
Done.
|
+ |
+ gamepad_.reset(new Gamepad( |
+ 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. |
reveman
2016/06/30 18:06:11
It's strongly discouraged to have tests that depen
denniskempin
2016/07/01 02:34:36
This is not based on timers, but based on the poll
denniskempin
2016/07/01 17:16:50
I've been looking into just providing a TaskRunner
reveman
2016/07/07 22:14:02
Does changing the polling frequency to 16 seconds
|
+ for (int i = 0; i < 2; ++i) { |
+ mock_data_fetcher_->WaitForDataRead(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ } |
+ } |
+ |
+ protected: |
+ std::unique_ptr<Gamepad> gamepad_; |
+ |
+ // Pointer owned by the gamepad instance. |
+ device::MockGamepadDataFetcher* mock_data_fetcher_; |
+ |
+ std::unique_ptr<Surface> surface_; |
+ std::unique_ptr<ShellSurface> shell_surface_; |
+ std::unique_ptr<Buffer> buffer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(GamepadTest); |
+}; |
+ |
+TEST_F(GamepadTest, ConnectDisconnect) { |
reveman
2016/06/30 18:06:12
Can you name the tests based on the delegate funct
denniskempin
2016/07/01 02:34:35
Done.
|
+ testing::StrictMock<MockGamepadDelegate> delegate; |
+ EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
+ .WillOnce(testing::Return(true)); |
+ |
+ 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(GamepadTest, AxisMove) { |
reveman
2016/06/30 18:06:12
s/AxisMove/OnAxis/?
denniskempin
2016/07/01 02:34:35
Done.
|
+ testing::StrictMock<MockGamepadDelegate> delegate; |
+ EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
+ .WillOnce(testing::Return(true)); |
+ |
+ 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); |
+} |
+ |
+TEST_F(GamepadTest, FocusHandling) { |
reveman
2016/06/30 18:06:12
s/FocusHandling/CanAcceptGamepadEventsForSurface/
denniskempin
2016/07/01 02:34:35
I had to remove the focus handling test since I am
|
+ testing::StrictMock<MockGamepadDelegate> delegate; |
+ EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
+ .WillOnce(testing::Return(true)); |
+ |
+ InitializeGamepad(&delegate); |
+ FocusNonExo(); |
+ |
+ 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 = 2; |
+ axis_moved.items[0].axes[0] = 1.0; |
+ axis_moved.items[0].axes[1] = 0; |
+ |
+ // Exo is not in focus.. should not cause any delegate calls. |
+ SetDataAndWaitForDelegate(axis_moved); |
+ testing::Mock::VerifyAndClearExpectations(&delegate); |
+ |
+ EXPECT_CALL(delegate, CanAcceptGamepadEventsForSurface(testing::_)) |
+ .WillOnce(testing::Return(true)); |
+ |
+ // Exo becomes focused, update delegate with full state on next gamepad state. |
+ FocusExo(); |
+ blink::WebGamepads axis_moved_2 = axis_moved; |
+ axis_moved_2.items[0].timestamp = 2; |
+ axis_moved_2.items[0].axes[1] = 1.0; |
+ EXPECT_CALL(delegate, OnStateChange(true)).Times(1); |
+ EXPECT_CALL(delegate, OnAxis(0, 1.0)).Times(1); |
+ EXPECT_CALL(delegate, OnAxis(1, 1.0)).Times(1); |
+ EXPECT_CALL(delegate, OnFrame()).Times(1); |
+ SetDataAndWaitForDelegate(axis_moved_2); |
+ |
+ DestroyGamepad(&delegate); |
+} |
+ |
+} // namespace |
reveman
2016/06/30 18:06:12
Can you add a simple test for OnButton too?
denniskempin
2016/07/01 02:34:35
Done.
|
+} // namespace exo |