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