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

Side by Side Diff: components/exo/gaming_seat_ozone_unittest.cc

Issue 2900773003: Allow gaming_seat to use ozone gamepad as back-end (Closed)
Patch Set: Add gaming_seat_ozone to exo Created 3 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
reveman 2017/06/05 22:48:04 Can we just make this the default unit tests and k
jkwang 2017/06/06 20:03:09 Done.
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 "ash/shell.h"
6 #include "base/command_line.h"
7 #include "base/run_loop.h"
8 #include "components/exo/buffer.h"
9 #include "components/exo/gamepad_delegate.h"
10 #include "components/exo/gaming_seat.h"
11 #include "components/exo/gaming_seat_delegate.h"
12 #include "components/exo/shell_surface.h"
13 #include "components/exo/surface.h"
14 #include "components/exo/test/exo_test_base.h"
15 #include "components/exo/test/exo_test_helper.h"
16 #include "device/gamepad/gamepad_test_helpers.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/aura/client/focus_client.h"
20 #include "ui/events/ozone/gamepad/gamepad_provider_ozone.h"
21
22 namespace exo {
23 namespace {
24
25 class MockGamingSeatDelegate : public GamingSeatDelegate {
26 public:
27 MOCK_CONST_METHOD1(CanAcceptGamepadEventsForSurface, bool(Surface*));
28 MOCK_METHOD0(GamepadAdded, GamepadDelegate*());
29 MOCK_METHOD0(Die, void());
30 void OnGamingSeatDestroying(GamingSeat*) override { delete this; };
31 ~MockGamingSeatDelegate() { Die(); };
32 };
33
34 class MockGamepadDelegate : public GamepadDelegate {
35 public:
36 MockGamepadDelegate() {}
37
38 // Overridden from GamepadDelegate:
39 MOCK_METHOD0(OnRemoved, void());
40 MOCK_METHOD2(OnAxis, void(int, double));
41 MOCK_METHOD3(OnButton, void(int, bool, double));
42 MOCK_METHOD0(OnFrame, void());
43 };
44
45 class GamingSeatOzoneTest : public test::ExoTestBase {
46 public:
47 GamingSeatOzoneTest() {
48 gamepad_provider_ = ui::GamepadProviderOzone::GetInstance();
49 }
50
51 void InitializeGamingSeat(MockGamingSeatDelegate* delegate) {
52 gaming_seat_.reset(new GamingSeat(delegate, nullptr));
53 }
54
55 void DestroyGamingSeat(MockGamingSeatDelegate* delegate) {
56 EXPECT_CALL(*delegate, Die()).Times(1);
57 gaming_seat_.reset();
58 }
59
60 void UpdateGamepadDevice(const std::vector<int>& gamepad_device_ids) {
61 std::vector<ui::InputDevice> gamepad_devices;
62 for (auto& id : gamepad_device_ids) {
63 gamepad_devices.push_back(ui::InputDevice(
64 id, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "gamepad"));
65 }
66 gamepad_provider_->DispatchGamepadDevicesUpdated(gamepad_devices);
67 }
68
69 void SendFrameToGamepads(const std::vector<int>& gamepad_device_ids) {
70 for (auto& id : gamepad_device_ids) {
71 ui::GamepadEvent event(id, ui::GamepadEventType::FRAME, 0, 0,
72 base::TimeTicks());
73 gamepad_provider_->DispatchGamepadEvent(event);
74 }
75 }
76
77 protected:
78 std::unique_ptr<GamingSeat> gaming_seat_;
79
80 ui::GamepadProviderOzone* gamepad_provider_;
81
82 DISALLOW_COPY_AND_ASSIGN(GamingSeatOzoneTest);
83 };
84
85 TEST_F(GamingSeatOzoneTest, ConnectionChange) {
86 std::unique_ptr<Surface> surface(new Surface);
87 std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
88 gfx::Size buffer_size(10, 10);
89 std::unique_ptr<Buffer> buffer(
90 new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
91 surface->Attach(buffer.get());
92 surface->Commit();
93
94 testing::StrictMock<MockGamingSeatDelegate>* gaming_seat_delegate =
95 new testing::StrictMock<MockGamingSeatDelegate>();
96 EXPECT_CALL(*gaming_seat_delegate,
97 CanAcceptGamepadEventsForSurface(testing::_))
98 .WillOnce(testing::Return(true));
99
100 InitializeGamingSeat(gaming_seat_delegate);
101 testing::StrictMock<MockGamepadDelegate> gamepad_delegate[5];
102
103 { // Test sequence
104 testing::InSequence s;
105 // Connect 2 gamepads.
106 EXPECT_CALL(*gaming_seat_delegate, GamepadAdded())
107 .WillOnce(testing::Return(&gamepad_delegate[0]))
108 .WillOnce(testing::Return(&gamepad_delegate[1]));
109 // Send frame to connected gamepad.
110 EXPECT_CALL(gamepad_delegate[0], OnFrame()).Times(1);
111 EXPECT_CALL(gamepad_delegate[1], OnFrame()).Times(1);
112 // Connect 3 more.
113 EXPECT_CALL(*gaming_seat_delegate, GamepadAdded())
114 .WillOnce(testing::Return(&gamepad_delegate[2]))
115 .WillOnce(testing::Return(&gamepad_delegate[3]));
116 // Send frame to all gamepads.
117 EXPECT_CALL(gamepad_delegate[0], OnFrame()).Times(1);
118 EXPECT_CALL(gamepad_delegate[1], OnFrame()).Times(1);
119 EXPECT_CALL(gamepad_delegate[2], OnFrame()).Times(1);
120 EXPECT_CALL(gamepad_delegate[3], OnFrame()).Times(1);
121 // Disconnect gamepad 0 and gamepad 2 and connect a new gamepad.
122 EXPECT_CALL(gamepad_delegate[0], OnRemoved()).Times(1);
123 EXPECT_CALL(gamepad_delegate[2], OnRemoved()).Times(1);
124 EXPECT_CALL(*gaming_seat_delegate, GamepadAdded())
125 .WillOnce(testing::Return(&gamepad_delegate[4]));
126 // Send frame to all gamepads.
127 EXPECT_CALL(gamepad_delegate[1], OnFrame()).Times(1);
128 EXPECT_CALL(gamepad_delegate[3], OnFrame()).Times(1);
129 EXPECT_CALL(gamepad_delegate[4], OnFrame()).Times(1);
130
131 // disconnect other gamepads
132 EXPECT_CALL(gamepad_delegate[1], OnRemoved()).Times(1);
133 EXPECT_CALL(gamepad_delegate[3], OnRemoved()).Times(1);
134 EXPECT_CALL(gamepad_delegate[4], OnRemoved()).Times(1);
135 }
136 // Gamepad connected.
137 UpdateGamepadDevice({0, 1});
138 SendFrameToGamepads({0, 1});
139 UpdateGamepadDevice({0, 1, 2, 3, 4});
140 SendFrameToGamepads({0, 1, 2, 3, 4});
141 UpdateGamepadDevice({1, 3, 5});
142 SendFrameToGamepads({1, 2, 3, 4, 5});
143 DestroyGamingSeat(gaming_seat_delegate);
144 }
145
146 } // namespace
147 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698