OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "device/gamepad/public/cpp/gamepad.h" |
7 #include "device/gamepad/public/interfaces/gamepad_struct_traits_test.mojom.h" | 8 #include "device/gamepad/public/interfaces/gamepad_struct_traits_test.mojom.h" |
8 #include "mojo/public/cpp/bindings/binding.h" | 9 #include "mojo/public/cpp/bindings/binding.h" |
9 #include "mojo/public/cpp/bindings/interface_request.h" | 10 #include "mojo/public/cpp/bindings/interface_request.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/WebKit/public/platform/WebGamepad.h" | |
12 | 12 |
13 namespace device { | 13 namespace device { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 enum GamepadTestDataType { | 17 enum GamepadTestDataType { |
18 GamepadCommon = 0, | 18 GamepadCommon = 0, |
19 GamepadPose_HasOrientation = 1, | 19 GamepadPose_HasOrientation = 1, |
20 GamepadPose_HasPosition = 2, | 20 GamepadPose_HasPosition = 2, |
21 GamepadPose_Null = 3, | 21 GamepadPose_Null = 3, |
22 }; | 22 }; |
23 | 23 |
24 blink::WebGamepad GetWebGamepadInstance(GamepadTestDataType type) { | 24 Gamepad GetWebGamepadInstance(GamepadTestDataType type) { |
25 blink::WebGamepadButton wgb(true, false, 1.0f); | 25 GamepadButton wgb(true, false, 1.0f); |
26 | 26 |
27 blink::WebGamepadVector wgv; | 27 GamepadVector wgv; |
28 memset(&wgv, 0, sizeof(blink::WebGamepadVector)); | 28 memset(&wgv, 0, sizeof(GamepadVector)); |
29 wgv.not_null = true; | 29 wgv.not_null = true; |
30 wgv.x = wgv.y = wgv.z = 1.0f; | 30 wgv.x = wgv.y = wgv.z = 1.0f; |
31 | 31 |
32 blink::WebGamepadQuaternion wgq; | 32 GamepadQuaternion wgq; |
33 memset(&wgq, 0, sizeof(blink::WebGamepadQuaternion)); | 33 memset(&wgq, 0, sizeof(GamepadQuaternion)); |
34 wgq.not_null = true; | 34 wgq.not_null = true; |
35 wgq.x = wgq.y = wgq.z = wgq.w = 2.0f; | 35 wgq.x = wgq.y = wgq.z = wgq.w = 2.0f; |
36 | 36 |
37 blink::WebGamepadPose wgp; | 37 GamepadPose wgp; |
38 memset(&wgp, 0, sizeof(blink::WebGamepadPose)); | 38 memset(&wgp, 0, sizeof(GamepadPose)); |
39 if (type == GamepadPose_Null) { | 39 if (type == GamepadPose_Null) { |
40 wgp.not_null = false; | 40 wgp.not_null = false; |
41 } else if (type == GamepadCommon) { | 41 } else if (type == GamepadCommon) { |
42 wgp.not_null = wgp.has_orientation = wgp.has_position = true; | 42 wgp.not_null = wgp.has_orientation = wgp.has_position = true; |
43 wgp.orientation = wgq; | 43 wgp.orientation = wgq; |
44 wgp.position = wgv; | 44 wgp.position = wgv; |
45 wgp.angular_acceleration = wgv; | 45 wgp.angular_acceleration = wgv; |
46 } else if (type == GamepadPose_HasOrientation) { | 46 } else if (type == GamepadPose_HasOrientation) { |
47 wgp.not_null = wgp.has_orientation = true; | 47 wgp.not_null = wgp.has_orientation = true; |
48 wgp.has_position = false; | 48 wgp.has_position = false; |
49 wgp.orientation = wgq; | 49 wgp.orientation = wgq; |
50 wgp.angular_acceleration = wgv; | 50 wgp.angular_acceleration = wgv; |
51 } else if (type == GamepadPose_HasPosition) { | 51 } else if (type == GamepadPose_HasPosition) { |
52 wgp.not_null = wgp.has_position = true; | 52 wgp.not_null = wgp.has_position = true; |
53 wgp.has_orientation = false; | 53 wgp.has_orientation = false; |
54 wgp.position = wgv; | 54 wgp.position = wgv; |
55 wgp.angular_acceleration = wgv; | 55 wgp.angular_acceleration = wgv; |
56 } | 56 } |
57 | 57 |
58 blink::WebUChar wch[blink::WebGamepad::kMappingLengthCap] = { | 58 UChar wch[Gamepad::kMappingLengthCap] = { |
59 1, 8, 9, 127, 128, 1024, 1025, 1949, | 59 1, 8, 9, 127, 128, 1024, 1025, 1949, |
60 2047, 2048, 16383, 16384, 20000, 32767, 32768, 65535}; | 60 2047, 2048, 16383, 16384, 20000, 32767, 32768, 65535}; |
61 | 61 |
62 blink::WebGamepad send; | 62 Gamepad send; |
63 memset(&send, 0, sizeof(blink::WebGamepad)); | 63 memset(&send, 0, sizeof(Gamepad)); |
64 | 64 |
65 send.connected = true; | 65 send.connected = true; |
66 for (size_t i = 0; i < blink::WebGamepad::kMappingLengthCap; i++) { | 66 for (size_t i = 0; i < Gamepad::kMappingLengthCap; i++) { |
67 send.id[i] = send.mapping[i] = wch[i]; | 67 send.id[i] = send.mapping[i] = wch[i]; |
68 } | 68 } |
69 send.timestamp = 1234567890123456789ULL; | 69 send.timestamp = 1234567890123456789ULL; |
70 send.axes_length = 0U; | 70 send.axes_length = 0U; |
71 for (size_t i = 0; i < blink::WebGamepad::kAxesLengthCap; i++) { | 71 for (size_t i = 0; i < Gamepad::kAxesLengthCap; i++) { |
72 send.axes_length++; | 72 send.axes_length++; |
73 send.axes[i] = 1.0; | 73 send.axes[i] = 1.0; |
74 } | 74 } |
75 send.buttons_length = 0U; | 75 send.buttons_length = 0U; |
76 for (size_t i = 0; i < blink::WebGamepad::kButtonsLengthCap; i++) { | 76 for (size_t i = 0; i < Gamepad::kButtonsLengthCap; i++) { |
77 send.buttons_length++; | 77 send.buttons_length++; |
78 send.buttons[i] = wgb; | 78 send.buttons[i] = wgb; |
79 } | 79 } |
80 send.pose = wgp; | 80 send.pose = wgp; |
81 send.hand = blink::WebGamepadHand::kGamepadHandRight; | 81 send.hand = GamepadHand::kRight; |
82 send.display_id = static_cast<unsigned short>(16); | 82 send.display_id = static_cast<unsigned short>(16); |
83 | 83 |
84 return send; | 84 return send; |
85 } | 85 } |
86 | 86 |
87 bool isWebGamepadButtonEqual(const blink::WebGamepadButton& lhs, | 87 bool isWebGamepadButtonEqual(const GamepadButton& lhs, |
88 const blink::WebGamepadButton& rhs) { | 88 const GamepadButton& rhs) { |
89 return (lhs.pressed == rhs.pressed && lhs.touched == rhs.touched && | 89 return (lhs.pressed == rhs.pressed && lhs.touched == rhs.touched && |
90 lhs.value == rhs.value); | 90 lhs.value == rhs.value); |
91 } | 91 } |
92 | 92 |
93 bool isWebGamepadVectorEqual(const blink::WebGamepadVector& lhs, | 93 bool isWebGamepadVectorEqual(const GamepadVector& lhs, |
94 const blink::WebGamepadVector& rhs) { | 94 const GamepadVector& rhs) { |
95 return ((lhs.not_null == false && rhs.not_null == false) || | 95 return ((lhs.not_null == false && rhs.not_null == false) || |
96 (lhs.not_null == rhs.not_null && lhs.x == rhs.x && lhs.y == rhs.y && | 96 (lhs.not_null == rhs.not_null && lhs.x == rhs.x && lhs.y == rhs.y && |
97 lhs.z == rhs.z)); | 97 lhs.z == rhs.z)); |
98 } | 98 } |
99 | 99 |
100 bool isWebGamepadQuaternionEqual(const blink::WebGamepadQuaternion& lhs, | 100 bool isWebGamepadQuaternionEqual(const GamepadQuaternion& lhs, |
101 const blink::WebGamepadQuaternion& rhs) { | 101 const GamepadQuaternion& rhs) { |
102 return ((lhs.not_null == false && rhs.not_null == false) || | 102 return ((lhs.not_null == false && rhs.not_null == false) || |
103 (lhs.not_null == rhs.not_null && lhs.x == rhs.x && lhs.y == rhs.y && | 103 (lhs.not_null == rhs.not_null && lhs.x == rhs.x && lhs.y == rhs.y && |
104 lhs.z == rhs.z && lhs.w == rhs.w)); | 104 lhs.z == rhs.z && lhs.w == rhs.w)); |
105 } | 105 } |
106 | 106 |
107 bool isWebGamepadPoseEqual(const blink::WebGamepadPose& lhs, | 107 bool isWebGamepadPoseEqual(const GamepadPose& lhs, const GamepadPose& rhs) { |
108 const blink::WebGamepadPose& rhs) { | |
109 if (lhs.not_null == false && rhs.not_null == false) { | 108 if (lhs.not_null == false && rhs.not_null == false) { |
110 return true; | 109 return true; |
111 } | 110 } |
112 if (lhs.not_null != rhs.not_null || | 111 if (lhs.not_null != rhs.not_null || |
113 lhs.has_orientation != rhs.has_orientation || | 112 lhs.has_orientation != rhs.has_orientation || |
114 lhs.has_position != rhs.has_position || | 113 lhs.has_position != rhs.has_position || |
115 !isWebGamepadVectorEqual(lhs.angular_velocity, rhs.angular_velocity) || | 114 !isWebGamepadVectorEqual(lhs.angular_velocity, rhs.angular_velocity) || |
116 !isWebGamepadVectorEqual(lhs.linear_velocity, rhs.linear_velocity) || | 115 !isWebGamepadVectorEqual(lhs.linear_velocity, rhs.linear_velocity) || |
117 !isWebGamepadVectorEqual(lhs.angular_acceleration, | 116 !isWebGamepadVectorEqual(lhs.angular_acceleration, |
118 rhs.angular_acceleration) || | 117 rhs.angular_acceleration) || |
119 !isWebGamepadVectorEqual(lhs.linear_acceleration, | 118 !isWebGamepadVectorEqual(lhs.linear_acceleration, |
120 rhs.linear_acceleration)) { | 119 rhs.linear_acceleration)) { |
121 return false; | 120 return false; |
122 } | 121 } |
123 if (lhs.has_orientation && | 122 if (lhs.has_orientation && |
124 !isWebGamepadQuaternionEqual(lhs.orientation, rhs.orientation)) { | 123 !isWebGamepadQuaternionEqual(lhs.orientation, rhs.orientation)) { |
125 return false; | 124 return false; |
126 } | 125 } |
127 if (lhs.has_position && | 126 if (lhs.has_position && |
128 !isWebGamepadVectorEqual(lhs.position, rhs.position)) { | 127 !isWebGamepadVectorEqual(lhs.position, rhs.position)) { |
129 return false; | 128 return false; |
130 } | 129 } |
131 return true; | 130 return true; |
132 } | 131 } |
133 | 132 |
134 bool isWebGamepadEqual(const blink::WebGamepad& send, | 133 bool isWebGamepadEqual(const Gamepad& send, const Gamepad& echo) { |
135 const blink::WebGamepad& echo) { | |
136 if (send.connected != echo.connected || send.timestamp != echo.timestamp || | 134 if (send.connected != echo.connected || send.timestamp != echo.timestamp || |
137 send.axes_length != echo.axes_length || | 135 send.axes_length != echo.axes_length || |
138 send.buttons_length != echo.buttons_length || | 136 send.buttons_length != echo.buttons_length || |
139 !isWebGamepadPoseEqual(send.pose, echo.pose) || send.hand != echo.hand || | 137 !isWebGamepadPoseEqual(send.pose, echo.pose) || send.hand != echo.hand || |
140 send.display_id != echo.display_id) { | 138 send.display_id != echo.display_id) { |
141 return false; | 139 return false; |
142 } | 140 } |
143 for (size_t i = 0; i < blink::WebGamepad::kIdLengthCap; i++) { | 141 for (size_t i = 0; i < Gamepad::kIdLengthCap; i++) { |
144 if (send.id[i] != echo.id[i]) { | 142 if (send.id[i] != echo.id[i]) { |
145 return false; | 143 return false; |
146 } | 144 } |
147 } | 145 } |
148 for (size_t i = 0; i < blink::WebGamepad::kAxesLengthCap; i++) { | 146 for (size_t i = 0; i < Gamepad::kAxesLengthCap; i++) { |
149 if (send.axes[i] != echo.axes[i]) { | 147 if (send.axes[i] != echo.axes[i]) { |
150 return false; | 148 return false; |
151 } | 149 } |
152 } | 150 } |
153 for (size_t i = 0; i < blink::WebGamepad::kButtonsLengthCap; i++) { | 151 for (size_t i = 0; i < Gamepad::kButtonsLengthCap; i++) { |
154 if (!isWebGamepadButtonEqual(send.buttons[i], echo.buttons[i])) { | 152 if (!isWebGamepadButtonEqual(send.buttons[i], echo.buttons[i])) { |
155 return false; | 153 return false; |
156 } | 154 } |
157 } | 155 } |
158 for (size_t i = 0; i < blink::WebGamepad::kMappingLengthCap; i++) { | 156 for (size_t i = 0; i < Gamepad::kMappingLengthCap; i++) { |
159 if (send.mapping[i] != echo.mapping[i]) { | 157 if (send.mapping[i] != echo.mapping[i]) { |
160 return false; | 158 return false; |
161 } | 159 } |
162 } | 160 } |
163 return true; | 161 return true; |
164 } | 162 } |
165 | 163 |
166 void ExpectWebGamepad(const blink::WebGamepad& send, | 164 void ExpectWebGamepad(const Gamepad& send, |
167 const base::Closure& closure, | 165 const base::Closure& closure, |
168 const blink::WebGamepad& echo) { | 166 const Gamepad& echo) { |
169 EXPECT_EQ(true, isWebGamepadEqual(send, echo)); | 167 EXPECT_EQ(true, isWebGamepadEqual(send, echo)); |
170 closure.Run(); | 168 closure.Run(); |
171 } | 169 } |
172 | 170 |
173 } // namespace | 171 } // namespace |
174 | 172 |
175 class GamepadStructTraitsTest : public testing::Test, | 173 class GamepadStructTraitsTest : public testing::Test, |
176 public device::mojom::GamepadStructTraitsTest { | 174 public mojom::GamepadStructTraitsTest { |
177 protected: | 175 protected: |
178 GamepadStructTraitsTest() : binding_(this) {} | 176 GamepadStructTraitsTest() : binding_(this) {} |
179 | 177 |
180 void PassGamepad(const blink::WebGamepad& send, | 178 void PassGamepad(const Gamepad& send, |
181 const PassGamepadCallback& callback) override { | 179 const PassGamepadCallback& callback) override { |
182 callback.Run(send); | 180 callback.Run(send); |
183 } | 181 } |
184 | 182 |
185 device::mojom::GamepadStructTraitsTestPtr GetGamepadStructTraitsTestProxy() { | 183 mojom::GamepadStructTraitsTestPtr GetGamepadStructTraitsTestProxy() { |
186 return binding_.CreateInterfacePtrAndBind(); | 184 return binding_.CreateInterfacePtrAndBind(); |
187 } | 185 } |
188 | 186 |
189 private: | 187 private: |
190 base::MessageLoop message_loop_; | 188 base::MessageLoop message_loop_; |
191 mojo::Binding<device::mojom::GamepadStructTraitsTest> binding_; | 189 mojo::Binding<mojom::GamepadStructTraitsTest> binding_; |
192 | 190 |
193 DISALLOW_COPY_AND_ASSIGN(GamepadStructTraitsTest); | 191 DISALLOW_COPY_AND_ASSIGN(GamepadStructTraitsTest); |
194 }; | 192 }; |
195 | 193 |
196 TEST_F(GamepadStructTraitsTest, GamepadCommon) { | 194 TEST_F(GamepadStructTraitsTest, GamepadCommon) { |
197 blink::WebGamepad send = GetWebGamepadInstance(GamepadCommon); | 195 Gamepad send = GetWebGamepadInstance(GamepadCommon); |
198 base::RunLoop loop; | 196 base::RunLoop loop; |
199 device::mojom::GamepadStructTraitsTestPtr proxy = | 197 mojom::GamepadStructTraitsTestPtr proxy = GetGamepadStructTraitsTestProxy(); |
200 GetGamepadStructTraitsTestProxy(); | |
201 proxy->PassGamepad(send, | 198 proxy->PassGamepad(send, |
202 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | 199 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); |
203 loop.Run(); | 200 loop.Run(); |
204 } | 201 } |
205 | 202 |
206 TEST_F(GamepadStructTraitsTest, GamepadPose_HasOrientation) { | 203 TEST_F(GamepadStructTraitsTest, GamepadPose_HasOrientation) { |
207 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_HasOrientation); | 204 Gamepad send = GetWebGamepadInstance(GamepadPose_HasOrientation); |
208 base::RunLoop loop; | 205 base::RunLoop loop; |
209 device::mojom::GamepadStructTraitsTestPtr proxy = | 206 mojom::GamepadStructTraitsTestPtr proxy = GetGamepadStructTraitsTestProxy(); |
210 GetGamepadStructTraitsTestProxy(); | |
211 proxy->PassGamepad(send, | 207 proxy->PassGamepad(send, |
212 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | 208 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); |
213 loop.Run(); | 209 loop.Run(); |
214 } | 210 } |
215 | 211 |
216 TEST_F(GamepadStructTraitsTest, GamepadPose_HasPosition) { | 212 TEST_F(GamepadStructTraitsTest, GamepadPose_HasPosition) { |
217 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_HasPosition); | 213 Gamepad send = GetWebGamepadInstance(GamepadPose_HasPosition); |
218 base::RunLoop loop; | 214 base::RunLoop loop; |
219 device::mojom::GamepadStructTraitsTestPtr proxy = | 215 mojom::GamepadStructTraitsTestPtr proxy = GetGamepadStructTraitsTestProxy(); |
220 GetGamepadStructTraitsTestProxy(); | |
221 proxy->PassGamepad(send, | 216 proxy->PassGamepad(send, |
222 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | 217 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); |
223 loop.Run(); | 218 loop.Run(); |
224 } | 219 } |
225 | 220 |
226 TEST_F(GamepadStructTraitsTest, GamepadPose_Null) { | 221 TEST_F(GamepadStructTraitsTest, GamepadPose_Null) { |
227 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_Null); | 222 Gamepad send = GetWebGamepadInstance(GamepadPose_Null); |
228 base::RunLoop loop; | 223 base::RunLoop loop; |
229 device::mojom::GamepadStructTraitsTestPtr proxy = | 224 mojom::GamepadStructTraitsTestPtr proxy = GetGamepadStructTraitsTestProxy(); |
230 GetGamepadStructTraitsTestProxy(); | |
231 proxy->PassGamepad(send, | 225 proxy->PassGamepad(send, |
232 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | 226 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); |
233 loop.Run(); | 227 loop.Run(); |
234 } | 228 } |
235 | 229 |
236 } // namespace device | 230 } // namespace device |
OLD | NEW |