| 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/webgamepad_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 blink::WebGamepad GetWebGamepadInstance() { |
| 18 blink::WebGamepadButton wgb(true, false, 1.0f); |
| 19 |
| 20 blink::WebGamepadVector wgv; |
| 21 wgv.notNull = true; |
| 22 wgv.x = wgv.y = wgv.z = 1.0f; |
| 23 |
| 24 blink::WebGamepadQuaternion wgq; |
| 25 wgq.notNull = false; |
| 26 wgq.x = wgq.y = wgq.z = wgq.w = 2.0f; |
| 27 |
| 28 blink::WebGamepadPose wgp; |
| 29 wgp.notNull = wgp.hasOrientation = wgp.hasPosition = true; |
| 30 wgp.orientation = wgq; |
| 31 wgp.position = wgv; |
| 32 wgp.angularAcceleration = wgv; |
| 33 |
| 34 blink::WebUChar wch[blink::WebGamepad::mappingLengthCap] = { |
| 35 1024, 1205, 1026, 1027, 1028, 1029, 1030, 1031, |
| 36 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039}; |
| 37 |
| 38 blink::WebGamepad send; |
| 39 memset(&send, 0, sizeof(blink::WebGamepad)); |
| 40 |
| 41 send.connected = true; |
| 42 for (size_t i = 0; i < blink::WebGamepad::mappingLengthCap; i++) { |
| 43 send.id[i] = send.mapping[i] = wch[i]; |
| 44 } |
| 45 send.timestamp = 1234567890123456789ULL; |
| 46 send.axesLength = 0U; |
| 47 for (size_t i = 0; i < blink::WebGamepad::axesLengthCap; i++) { |
| 48 send.axesLength++; |
| 49 send.axes[i] = 1.0; |
| 50 } |
| 51 send.buttonsLength = 0U; |
| 52 for (unsigned i = 0; i < blink::WebGamepad::buttonsLengthCap; i++) { |
| 53 send.buttonsLength++; |
| 54 send.buttons[i] = wgb; |
| 55 } |
| 56 send.pose = wgp; |
| 57 send.hand = blink::WebGamepadHand::GamepadHandRight; |
| 58 send.displayId = static_cast<unsigned short>(16); |
| 59 |
| 60 return send; |
| 61 } |
| 62 |
| 63 bool isWebGamepadButtonEqual(const blink::WebGamepadButton& lhs, |
| 64 const blink::WebGamepadButton& rhs) { |
| 65 if (lhs.pressed == rhs.pressed && lhs.touched == rhs.touched && |
| 66 lhs.value == rhs.value) { |
| 67 return true; |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 72 bool isWebGamepadVectorEqual(const blink::WebGamepadVector& lhs, |
| 73 const blink::WebGamepadVector& rhs) { |
| 74 if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y && |
| 75 lhs.z == rhs.z) { |
| 76 return true; |
| 77 } |
| 78 return false; |
| 79 } |
| 80 |
| 81 bool isWebGamepadQuaternionEqual(const blink::WebGamepadQuaternion& lhs, |
| 82 const blink::WebGamepadQuaternion& rhs) { |
| 83 if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y && |
| 84 lhs.z == rhs.z && lhs.w == rhs.w) { |
| 85 return true; |
| 86 } |
| 87 return false; |
| 88 } |
| 89 |
| 90 bool isWebGamepadPoseEqual(const blink::WebGamepadPose& lhs, |
| 91 const blink::WebGamepadPose& rhs) { |
| 92 if (lhs.notNull == rhs.notNull && lhs.hasOrientation == rhs.hasOrientation && |
| 93 lhs.hasPosition == rhs.hasPosition && |
| 94 isWebGamepadQuaternionEqual(lhs.orientation, rhs.orientation) && |
| 95 isWebGamepadVectorEqual(lhs.position, rhs.position) && |
| 96 isWebGamepadVectorEqual(lhs.angularVelocity, rhs.angularVelocity) && |
| 97 isWebGamepadVectorEqual(lhs.linearVelocity, rhs.linearVelocity) && |
| 98 isWebGamepadVectorEqual(lhs.angularAcceleration, |
| 99 rhs.angularAcceleration) && |
| 100 isWebGamepadVectorEqual(lhs.linearAcceleration, rhs.linearAcceleration)) { |
| 101 return true; |
| 102 } |
| 103 return false; |
| 104 } |
| 105 |
| 106 bool isWebGamepadEqual(const blink::WebGamepad& send, |
| 107 const blink::WebGamepad& echo) { |
| 108 if (send.connected != echo.connected || send.timestamp != echo.timestamp || |
| 109 send.axesLength != echo.axesLength || |
| 110 send.buttonsLength != echo.buttonsLength || |
| 111 !isWebGamepadPoseEqual(send.pose, echo.pose) || send.hand != echo.hand || |
| 112 send.displayId != echo.displayId) { |
| 113 return false; |
| 114 } |
| 115 for (size_t i = 0; i < blink::WebGamepad::idLengthCap; i++) { |
| 116 if (send.id[i] != echo.id[i]) { |
| 117 return false; |
| 118 } |
| 119 } |
| 120 for (size_t i = 0; i < blink::WebGamepad::axesLengthCap; i++) { |
| 121 if (send.axes[i] != echo.axes[i]) { |
| 122 return false; |
| 123 } |
| 124 } |
| 125 for (size_t i = 0; i < blink::WebGamepad::buttonsLengthCap; i++) { |
| 126 if (!isWebGamepadButtonEqual(send.buttons[i], echo.buttons[i])) { |
| 127 return false; |
| 128 } |
| 129 } |
| 130 for (size_t i = 0; i < blink::WebGamepad::mappingLengthCap; i++) { |
| 131 if (send.mapping[i] != echo.mapping[i]) { |
| 132 return false; |
| 133 } |
| 134 } |
| 135 return true; |
| 136 } |
| 137 |
| 138 void ExpectWebGamepad(const blink::WebGamepad& send, |
| 139 const base::Closure& closure, |
| 140 const blink::WebGamepad& echo) { |
| 141 EXPECT_EQ(true, isWebGamepadEqual(send, echo)); |
| 142 closure.Run(); |
| 143 } |
| 144 |
| 145 } // namespace |
| 146 |
| 147 class WebGamepadTypeMappingTest |
| 148 : public testing::Test, |
| 149 public device::mojom::WebGamepadTypeMappingTest { |
| 150 protected: |
| 151 WebGamepadTypeMappingTest() : binding_(this) {} |
| 152 |
| 153 void PassWebGamepad(const blink::WebGamepad& send, |
| 154 const PassWebGamepadCallback& callback) override { |
| 155 callback.Run(send); |
| 156 } |
| 157 |
| 158 device::mojom::WebGamepadTypeMappingTestPtr |
| 159 GetWebGamepadTypeMappingTestProxy() { |
| 160 return binding_.CreateInterfacePtrAndBind(); |
| 161 } |
| 162 |
| 163 private: |
| 164 base::MessageLoop message_loop_; |
| 165 mojo::Binding<device::mojom::WebGamepadTypeMappingTest> binding_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(WebGamepadTypeMappingTest); |
| 168 }; |
| 169 |
| 170 TEST_F(WebGamepadTypeMappingTest, WebGamepadTypeMapping) { |
| 171 blink::WebGamepad send = GetWebGamepadInstance(); |
| 172 base::RunLoop loop; |
| 173 device::mojom::WebGamepadTypeMappingTestPtr proxy = |
| 174 GetWebGamepadTypeMappingTestProxy(); |
| 175 proxy->PassWebGamepad( |
| 176 send, base::Bind(&ExpectWebGamepad, send, loop.QuitClosure())); |
| 177 loop.Run(); |
| 178 } |
| 179 |
| 180 } // namespace device |
| OLD | NEW |