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

Unified Diff: device/gamepad/public/interfaces/webgamepad_struct_traits_unittest.cc

Issue 2492183002: Add struct_traits and typemap for blink::WebGamepad (Closed)
Patch Set: fixed "un-aligned warning" in windows x64 build Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: device/gamepad/public/interfaces/webgamepad_struct_traits_unittest.cc
diff --git a/device/gamepad/public/interfaces/webgamepad_struct_traits_unittest.cc b/device/gamepad/public/interfaces/webgamepad_struct_traits_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cdc021b85d93bff4f40fd4eec8434c3c93677498
--- /dev/null
+++ b/device/gamepad/public/interfaces/webgamepad_struct_traits_unittest.cc
@@ -0,0 +1,180 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "device/gamepad/public/interfaces/webgamepad_typemapping_test.mojom.h"
+#include "mojo/public/cpp/bindings/binding.h"
+#include "mojo/public/cpp/bindings/interface_request.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/public/platform/WebGamepad.h"
+
+namespace device {
+
+namespace {
+
+blink::WebGamepad GetWebGamepadInstance() {
+ blink::WebGamepadButton wgb(true, false, 1.0f);
+
+ blink::WebGamepadVector wgv;
+ wgv.notNull = true;
+ wgv.x = wgv.y = wgv.z = 1.0f;
+
+ blink::WebGamepadQuaternion wgq;
+ wgq.notNull = false;
+ wgq.x = wgq.y = wgq.z = wgq.w = 2.0f;
+
+ blink::WebGamepadPose wgp;
+ wgp.notNull = wgp.hasOrientation = wgp.hasPosition = true;
+ wgp.orientation = wgq;
+ wgp.position = wgv;
+ wgp.angularAcceleration = wgv;
+
+ blink::WebUChar wch[blink::WebGamepad::mappingLengthCap] = {
+ 1024, 1205, 1026, 1027, 1028, 1029, 1030, 1031,
+ 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039};
+
+ blink::WebGamepad send;
+ memset(&send, 0, sizeof(blink::WebGamepad));
+
+ send.connected = true;
+ for (size_t i = 0; i < blink::WebGamepad::mappingLengthCap; i++) {
+ send.id[i] = send.mapping[i] = wch[i];
+ }
+ send.timestamp = 1234567890123456789ULL;
+ send.axesLength = 0U;
+ for (size_t i = 0; i < blink::WebGamepad::axesLengthCap; i++) {
+ send.axesLength++;
+ send.axes[i] = 1.0;
+ }
+ send.buttonsLength = 0U;
+ for (unsigned i = 0; i < blink::WebGamepad::buttonsLengthCap; i++) {
+ send.buttonsLength++;
+ send.buttons[i] = wgb;
+ }
+ send.pose = wgp;
+ send.hand = blink::WebGamepadHand::GamepadHandRight;
+ send.displayId = static_cast<unsigned short>(16);
+
+ return send;
+}
+
+bool isWebGamepadButtonEqual(const blink::WebGamepadButton& lhs,
+ const blink::WebGamepadButton& rhs) {
+ if (lhs.pressed == rhs.pressed && lhs.touched == rhs.touched &&
+ lhs.value == rhs.value) {
+ return true;
+ }
+ return false;
+}
+
+bool isWebGamepadVectorEqual(const blink::WebGamepadVector& lhs,
+ const blink::WebGamepadVector& rhs) {
+ if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y &&
+ lhs.z == rhs.z) {
+ return true;
+ }
+ return false;
+}
+
+bool isWebGamepadQuaternionEqual(const blink::WebGamepadQuaternion& lhs,
+ const blink::WebGamepadQuaternion& rhs) {
+ if (lhs.notNull == rhs.notNull && lhs.x == rhs.x && lhs.y == rhs.y &&
+ lhs.z == rhs.z && lhs.w == rhs.w) {
+ return true;
+ }
+ return false;
+}
+
+bool isWebGamepadPoseEqual(const blink::WebGamepadPose& lhs,
+ const blink::WebGamepadPose& rhs) {
+ if (lhs.notNull == rhs.notNull && lhs.hasOrientation == rhs.hasOrientation &&
+ lhs.hasPosition == rhs.hasPosition &&
+ isWebGamepadQuaternionEqual(lhs.orientation, rhs.orientation) &&
+ isWebGamepadVectorEqual(lhs.position, rhs.position) &&
+ isWebGamepadVectorEqual(lhs.angularVelocity, rhs.angularVelocity) &&
+ isWebGamepadVectorEqual(lhs.linearVelocity, rhs.linearVelocity) &&
+ isWebGamepadVectorEqual(lhs.angularAcceleration,
+ rhs.angularAcceleration) &&
+ isWebGamepadVectorEqual(lhs.linearAcceleration, rhs.linearAcceleration)) {
+ return true;
+ }
+ return false;
+}
+
+bool isWebGamepadEqual(const blink::WebGamepad& send,
+ const blink::WebGamepad& echo) {
+ if (send.connected != echo.connected || send.timestamp != echo.timestamp ||
+ send.axesLength != echo.axesLength ||
+ send.buttonsLength != echo.buttonsLength ||
+ !isWebGamepadPoseEqual(send.pose, echo.pose) || send.hand != echo.hand ||
+ send.displayId != echo.displayId) {
+ return false;
+ }
+ for (size_t i = 0; i < blink::WebGamepad::idLengthCap; i++) {
+ if (send.id[i] != echo.id[i]) {
+ return false;
+ }
+ }
+ for (size_t i = 0; i < blink::WebGamepad::axesLengthCap; i++) {
+ if (send.axes[i] != echo.axes[i]) {
+ return false;
+ }
+ }
+ for (size_t i = 0; i < blink::WebGamepad::buttonsLengthCap; i++) {
+ if (!isWebGamepadButtonEqual(send.buttons[i], echo.buttons[i])) {
+ return false;
+ }
+ }
+ for (size_t i = 0; i < blink::WebGamepad::mappingLengthCap; i++) {
+ if (send.mapping[i] != echo.mapping[i]) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void ExpectWebGamepad(const blink::WebGamepad& send,
+ const base::Closure& closure,
+ const blink::WebGamepad& echo) {
+ EXPECT_EQ(true, isWebGamepadEqual(send, echo));
+ closure.Run();
+}
+
+} // namespace
+
+class WebGamepadTypeMappingTest
+ : public testing::Test,
+ public device::mojom::WebGamepadTypeMappingTest {
+ protected:
+ WebGamepadTypeMappingTest() : binding_(this) {}
+
+ void PassWebGamepad(const blink::WebGamepad& send,
+ const PassWebGamepadCallback& callback) override {
+ callback.Run(send);
+ }
+
+ device::mojom::WebGamepadTypeMappingTestPtr
+ GetWebGamepadTypeMappingTestProxy() {
+ return binding_.CreateInterfacePtrAndBind();
+ }
+
+ private:
+ base::MessageLoop message_loop_;
+ mojo::Binding<device::mojom::WebGamepadTypeMappingTest> binding_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebGamepadTypeMappingTest);
+};
+
+TEST_F(WebGamepadTypeMappingTest, WebGamepadTypeMapping) {
+ blink::WebGamepad send = GetWebGamepadInstance();
+ base::RunLoop loop;
+ device::mojom::WebGamepadTypeMappingTestPtr proxy =
+ GetWebGamepadTypeMappingTestProxy();
+ proxy->PassWebGamepad(
+ send, base::Bind(&ExpectWebGamepad, send, loop.QuitClosure()));
+ loop.Run();
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698