1 // Copyright (c) 2012 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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/weak_ptr.h"
9 #include "content/browser/gamepad/gamepad_test_helpers.h"
10 #include "content/browser/renderer_host/pepper/browser_ppapi_host_test.h"
11 #include "content/browser/renderer_host/pepper/pepper_gamepad_host.h"
12 #include "content/common/gamepad_hardware_buffer.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/proxy/gamepad_resource.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/proxy/resource_message_params.h"
18 #include "ppapi/shared_impl/ppb_gamepad_shared.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 class PepperGamepadHostTest
: public testing::Test
,
26 public BrowserPpapiHostTest
{
28 PepperGamepadHostTest() {}
29 ~PepperGamepadHostTest() override
{}
31 void ConstructService(const blink::WebGamepads
& test_data
) {
32 service_
.reset(new GamepadServiceTestConstructor(test_data
));
35 GamepadService
* gamepad_service() { return service_
->gamepad_service(); }
38 scoped_ptr
<GamepadServiceTestConstructor
> service_
;
40 DISALLOW_COPY_AND_ASSIGN(PepperGamepadHostTest
);
43 inline ptrdiff_t AddressDiff(const void* a
, const void* b
) {
44 return static_cast<const char*>(a
) - static_cast<const char*>(b
);
49 // Validate the memory layout of the Pepper proxy struct matches the content
50 // one. The proxy can't depend on content so has a duplicate definition. This
51 // code can see both definitions so we do the validation here.
52 TEST_F(PepperGamepadHostTest
, ValidateHardwareBuffersMatch
) {
54 static_assert(sizeof(ppapi::ContentGamepadHardwareBuffer
) ==
55 sizeof(GamepadHardwareBuffer
),
56 "gamepad hardware buffers must match");
57 ppapi::ContentGamepadHardwareBuffer ppapi_buf
;
58 GamepadHardwareBuffer content_buf
;
59 EXPECT_EQ(AddressDiff(&content_buf
.sequence
, &content_buf
),
60 AddressDiff(&ppapi_buf
.sequence
, &ppapi_buf
));
61 EXPECT_EQ(AddressDiff(&content_buf
.buffer
, &content_buf
),
62 AddressDiff(&ppapi_buf
.buffer
, &ppapi_buf
));
65 TEST_F(PepperGamepadHostTest
, ValidateGamepadsMatch
) {
67 static_assert(sizeof(ppapi::WebKitGamepads
) == sizeof(blink::WebGamepads
),
68 "gamepads data must match");
69 ppapi::WebKitGamepads ppapi_gamepads
;
70 blink::WebGamepads web_gamepads
;
71 EXPECT_EQ(AddressDiff(&web_gamepads
.length
, &web_gamepads
),
72 AddressDiff(&ppapi_gamepads
.length
, &ppapi_gamepads
));
74 // See comment below on storage & the EXPECT macro.
75 size_t webkit_items_length_cap
= blink::WebGamepads::itemsLengthCap
;
76 size_t ppapi_items_length_cap
= ppapi::WebKitGamepads::kItemsLengthCap
;
77 EXPECT_EQ(webkit_items_length_cap
, ppapi_items_length_cap
);
79 for (size_t i
= 0; i
< web_gamepads
.itemsLengthCap
; i
++) {
80 EXPECT_EQ(AddressDiff(&web_gamepads
.items
[0], &web_gamepads
),
81 AddressDiff(&ppapi_gamepads
.items
[0], &ppapi_gamepads
));
85 TEST_F(PepperGamepadHostTest
, ValidateGamepadMatch
) {
87 static_assert(sizeof(ppapi::WebKitGamepad
) == sizeof(blink::WebGamepad
),
88 "gamepad data must match");
89 ppapi::WebKitGamepad ppapi_gamepad
;
90 blink::WebGamepad web_gamepad
;
92 // Using EXPECT seems to force storage for the parameter, which the constants
93 // in the WebKit/PPAPI headers don't have. So we have to use temporaries
94 // before comparing them.
95 size_t webkit_id_length_cap
= blink::WebGamepad::idLengthCap
;
96 size_t ppapi_id_length_cap
= ppapi::WebKitGamepad::kIdLengthCap
;
97 EXPECT_EQ(webkit_id_length_cap
, ppapi_id_length_cap
);
99 size_t webkit_axes_length_cap
= blink::WebGamepad::axesLengthCap
;
100 size_t ppapi_axes_length_cap
= ppapi::WebKitGamepad::kAxesLengthCap
;
101 EXPECT_EQ(webkit_axes_length_cap
, ppapi_axes_length_cap
);
103 size_t webkit_buttons_length_cap
= blink::WebGamepad::buttonsLengthCap
;
104 size_t ppapi_buttons_length_cap
= ppapi::WebKitGamepad::kButtonsLengthCap
;
105 EXPECT_EQ(webkit_buttons_length_cap
, ppapi_buttons_length_cap
);
107 EXPECT_EQ(AddressDiff(&web_gamepad
.connected
, &web_gamepad
),
108 AddressDiff(&ppapi_gamepad
.connected
, &ppapi_gamepad
));
109 EXPECT_EQ(AddressDiff(&web_gamepad
.id
, &web_gamepad
),
110 AddressDiff(&ppapi_gamepad
.id
, &ppapi_gamepad
));
111 EXPECT_EQ(AddressDiff(&web_gamepad
.timestamp
, &web_gamepad
),
112 AddressDiff(&ppapi_gamepad
.timestamp
, &ppapi_gamepad
));
113 EXPECT_EQ(AddressDiff(&web_gamepad
.axesLength
, &web_gamepad
),
114 AddressDiff(&ppapi_gamepad
.axes_length
, &ppapi_gamepad
));
115 EXPECT_EQ(AddressDiff(&web_gamepad
.axes
, &web_gamepad
),
116 AddressDiff(&ppapi_gamepad
.axes
, &ppapi_gamepad
));
117 EXPECT_EQ(AddressDiff(&web_gamepad
.buttonsLength
, &web_gamepad
),
118 AddressDiff(&ppapi_gamepad
.buttons_length
, &ppapi_gamepad
));
119 EXPECT_EQ(AddressDiff(&web_gamepad
.buttons
, &web_gamepad
),
120 AddressDiff(&ppapi_gamepad
.buttons
, &ppapi_gamepad
));
124 #if defined(OS_ANDROID)
125 #define MAYBE_WaitForReply DISABLED_WaitForReply
127 #define MAYBE_WaitForReply WaitForReply
129 TEST_F(PepperGamepadHostTest
, MAYBE_WaitForReply
) {
130 blink::WebGamepads default_data
;
131 memset(&default_data
, 0, sizeof(blink::WebGamepads
));
132 default_data
.length
= 1;
133 default_data
.items
[0].connected
= true;
134 default_data
.items
[0].buttonsLength
= 1;
135 ConstructService(default_data
);
137 PP_Instance pp_instance
= 12345;
138 PP_Resource pp_resource
= 67890;
139 PepperGamepadHost
gamepad_host(
140 gamepad_service(), GetBrowserPpapiHost(), pp_instance
, pp_resource
);
142 // Synthesize a request for gamepad data.
143 ppapi::host::HostMessageContext
context(
144 ppapi::proxy::ResourceMessageCallParams(pp_resource
, 1));
145 EXPECT_EQ(PP_OK_COMPLETIONPENDING
,
146 gamepad_host
.OnResourceMessageReceived(
147 PpapiHostMsg_Gamepad_RequestMemory(), &context
));
149 MockGamepadDataFetcher
* fetcher
= service_
->data_fetcher();
150 fetcher
->WaitForDataReadAndCallbacksIssued();
152 // It should not have sent the callback message.
153 service_
->message_loop().RunUntilIdle();
154 EXPECT_EQ(0u, sink().message_count());
156 // Set a button down and wait for it to be read twice.
157 blink::WebGamepads button_down_data
= default_data
;
158 button_down_data
.items
[0].buttons
[0].value
= 1.f
;
159 button_down_data
.items
[0].buttons
[0].pressed
= true;
160 fetcher
->SetTestData(button_down_data
);
161 fetcher
->WaitForDataReadAndCallbacksIssued();
163 // It should have sent a callback.
164 service_
->message_loop().RunUntilIdle();
165 ppapi::proxy::ResourceMessageReplyParams reply_params
;
166 IPC::Message reply_msg
;
167 ASSERT_TRUE(sink().GetFirstResourceReplyMatching(
168 PpapiPluginMsg_Gamepad_SendMemory::ID
, &reply_params
, &reply_msg
));
170 // Extract the shared memory handle.
171 base::SharedMemoryHandle reply_handle
;
172 EXPECT_TRUE(reply_params
.TakeSharedMemoryHandleAtIndex(0, &reply_handle
));
174 // Validate the shared memory.
175 base::SharedMemory
shared_memory(reply_handle
, true);
176 EXPECT_TRUE(shared_memory
.Map(sizeof(ppapi::ContentGamepadHardwareBuffer
)));
177 const ppapi::ContentGamepadHardwareBuffer
* buffer
=
178 static_cast<const ppapi::ContentGamepadHardwareBuffer
*>(
179 shared_memory
.memory());
180 EXPECT_EQ(button_down_data
.length
, buffer
->buffer
.length
);
181 EXPECT_EQ(button_down_data
.items
[0].buttonsLength
,
182 buffer
->buffer
.items
[0].buttons_length
);
183 for (size_t i
= 0; i
< ppapi::WebKitGamepad::kButtonsLengthCap
; i
++) {
184 EXPECT_EQ(button_down_data
.items
[0].buttons
[i
].value
,
185 buffer
->buffer
.items
[0].buttons
[i
].value
);
186 EXPECT_EQ(button_down_data
.items
[0].buttons
[i
].pressed
,
187 buffer
->buffer
.items
[0].buttons
[i
].pressed
);
190 // Duplicate requests should be denied.
191 EXPECT_EQ(PP_ERROR_FAILED
,
192 gamepad_host
.OnResourceMessageReceived(
193 PpapiHostMsg_Gamepad_RequestMemory(), &context
));
196 } // namespace content