Chromium Code Reviews| 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_typemapping_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 1024, 1205, 1026, 1027, 1028, 1029, 1030, 1031, | |
| 60 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039}; | |
| 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 if (lhs.pressed == rhs.pressed && lhs.touched == rhs.touched && | |
|
yzshen1
2016/11/15 19:17:33
Please directly return what you currently use as t
ke.he
2016/11/16 09:51:41
Done.
| |
| 90 lhs.value == rhs.value) { | |
| 91 return true; | |
| 92 } | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 bool isWebGamepadVectorEqual(const blink::WebGamepadVector& lhs, | |
| 97 const blink::WebGamepadVector& rhs) { | |
| 98 if (lhs.notNull == false && rhs.notNull == false) { | |
| 99 return true; | |
| 100 } | |
| 101 if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y && | |
| 102 lhs.z == rhs.z) { | |
| 103 return true; | |
| 104 } | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 bool isWebGamepadQuaternionEqual(const blink::WebGamepadQuaternion& lhs, | |
| 109 const blink::WebGamepadQuaternion& rhs) { | |
| 110 if (lhs.notNull == false && rhs.notNull == false) { | |
| 111 return true; | |
| 112 } | |
| 113 if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y && | |
| 114 lhs.z == rhs.z && lhs.w == rhs.w) { | |
| 115 return true; | |
| 116 } | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 bool isWebGamepadPoseEqual(const blink::WebGamepadPose& lhs, | |
| 121 const blink::WebGamepadPose& rhs) { | |
| 122 if (lhs.notNull == false && rhs.notNull == false) { | |
| 123 return true; | |
| 124 } | |
| 125 if (lhs.notNull != rhs.notNull || lhs.hasOrientation != rhs.hasOrientation || | |
| 126 lhs.hasPosition != rhs.hasPosition || | |
| 127 !isWebGamepadVectorEqual(lhs.angularVelocity, rhs.angularVelocity) || | |
| 128 !isWebGamepadVectorEqual(lhs.linearVelocity, rhs.linearVelocity) || | |
| 129 !isWebGamepadVectorEqual(lhs.angularAcceleration, | |
| 130 rhs.angularAcceleration) || | |
| 131 !isWebGamepadVectorEqual(lhs.linearAcceleration, | |
| 132 rhs.linearAcceleration)) { | |
| 133 return false; | |
| 134 } | |
| 135 if (lhs.hasOrientation && | |
| 136 !isWebGamepadQuaternionEqual(lhs.orientation, rhs.orientation)) { | |
| 137 return false; | |
| 138 } | |
| 139 if (lhs.hasPosition && !isWebGamepadVectorEqual(lhs.position, rhs.position)) { | |
| 140 return false; | |
| 141 } | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 bool isWebGamepadEqual(const blink::WebGamepad& send, | |
| 146 const blink::WebGamepad& echo) { | |
| 147 if (send.connected != echo.connected || send.timestamp != echo.timestamp || | |
| 148 send.axesLength != echo.axesLength || | |
| 149 send.buttonsLength != echo.buttonsLength || | |
| 150 !isWebGamepadPoseEqual(send.pose, echo.pose) || send.hand != echo.hand || | |
| 151 send.displayId != echo.displayId) { | |
| 152 return false; | |
| 153 } | |
| 154 for (size_t i = 0; i < blink::WebGamepad::idLengthCap; i++) { | |
| 155 if (send.id[i] != echo.id[i]) { | |
| 156 return false; | |
| 157 } | |
| 158 } | |
| 159 for (size_t i = 0; i < blink::WebGamepad::axesLengthCap; i++) { | |
| 160 if (send.axes[i] != echo.axes[i]) { | |
| 161 return false; | |
| 162 } | |
| 163 } | |
| 164 for (size_t i = 0; i < blink::WebGamepad::buttonsLengthCap; i++) { | |
| 165 if (!isWebGamepadButtonEqual(send.buttons[i], echo.buttons[i])) { | |
| 166 return false; | |
| 167 } | |
| 168 } | |
| 169 for (size_t i = 0; i < blink::WebGamepad::mappingLengthCap; i++) { | |
| 170 if (send.mapping[i] != echo.mapping[i]) { | |
| 171 return false; | |
| 172 } | |
| 173 } | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 void ExpectWebGamepad(const blink::WebGamepad& send, | |
| 178 const base::Closure& closure, | |
| 179 const blink::WebGamepad& echo) { | |
| 180 EXPECT_EQ(true, isWebGamepadEqual(send, echo)); | |
| 181 closure.Run(); | |
| 182 } | |
| 183 | |
| 184 } // namespace | |
| 185 | |
| 186 class GamepadTypeMappingTest : public testing::Test, | |
| 187 public device::mojom::GamepadTypeMappingTest { | |
| 188 protected: | |
| 189 GamepadTypeMappingTest() : binding_(this) {} | |
| 190 | |
| 191 void PassGamepad(const blink::WebGamepad& send, | |
| 192 const PassGamepadCallback& callback) override { | |
| 193 callback.Run(send); | |
| 194 } | |
| 195 | |
| 196 device::mojom::GamepadTypeMappingTestPtr GetGamepadTypeMappingTestProxy() { | |
| 197 return binding_.CreateInterfacePtrAndBind(); | |
| 198 } | |
| 199 | |
| 200 private: | |
| 201 base::MessageLoop message_loop_; | |
| 202 mojo::Binding<device::mojom::GamepadTypeMappingTest> binding_; | |
| 203 | |
| 204 DISALLOW_COPY_AND_ASSIGN(GamepadTypeMappingTest); | |
| 205 }; | |
| 206 | |
| 207 TEST_F(GamepadTypeMappingTest, GamepadCommon) { | |
| 208 blink::WebGamepad send = GetWebGamepadInstance(GamepadCommon); | |
| 209 base::RunLoop loop; | |
| 210 device::mojom::GamepadTypeMappingTestPtr proxy = | |
| 211 GetGamepadTypeMappingTestProxy(); | |
| 212 proxy->PassGamepad(send, | |
| 213 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | |
| 214 loop.Run(); | |
| 215 } | |
| 216 | |
| 217 TEST_F(GamepadTypeMappingTest, GamepadPose_HasOrientation) { | |
| 218 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_HasOrientation); | |
| 219 base::RunLoop loop; | |
| 220 device::mojom::GamepadTypeMappingTestPtr proxy = | |
| 221 GetGamepadTypeMappingTestProxy(); | |
| 222 proxy->PassGamepad(send, | |
| 223 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | |
| 224 loop.Run(); | |
| 225 } | |
| 226 | |
| 227 TEST_F(GamepadTypeMappingTest, GamepadPose_HasPosition) { | |
| 228 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_HasPosition); | |
| 229 base::RunLoop loop; | |
| 230 device::mojom::GamepadTypeMappingTestPtr proxy = | |
| 231 GetGamepadTypeMappingTestProxy(); | |
| 232 proxy->PassGamepad(send, | |
| 233 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | |
| 234 loop.Run(); | |
| 235 } | |
| 236 | |
| 237 TEST_F(GamepadTypeMappingTest, GamepadPose_Null) { | |
| 238 blink::WebGamepad send = GetWebGamepadInstance(GamepadPose_Null); | |
| 239 base::RunLoop loop; | |
| 240 device::mojom::GamepadTypeMappingTestPtr proxy = | |
| 241 GetGamepadTypeMappingTestProxy(); | |
| 242 proxy->PassGamepad(send, | |
| 243 base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); | |
| 244 loop.Run(); | |
| 245 } | |
| 246 | |
| 247 } // namespace device | |
| OLD | NEW |