OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <string.h> | |
6 | |
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" | |
20 | |
21 namespace content { | |
22 | |
23 namespace { | |
24 | |
25 class PepperGamepadHostTest | |
26 : public testing::Test, | |
27 public BrowserPpapiHostTest { | |
28 public: | |
29 PepperGamepadHostTest() { | |
30 } | |
31 ~PepperGamepadHostTest() { | |
32 } | |
33 | |
34 void ConstructService(const WebKit::WebGamepads& test_data) { | |
35 service_.reset(new GamepadServiceTestConstructor(test_data)); | |
36 } | |
37 | |
38 GamepadService* gamepad_service() { return service_->gamepad_service(); } | |
39 | |
40 protected: | |
41 scoped_ptr<GamepadServiceTestConstructor> service_; | |
42 | |
43 DISALLOW_COPY_AND_ASSIGN(PepperGamepadHostTest); | |
44 }; | |
45 | |
46 inline ptrdiff_t AddressDiff(const void* a, const void* b) { | |
47 return static_cast<const char*>(a) - static_cast<const char*>(b); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 // Validate the memory layout of the Pepper proxy struct matches the content | |
53 // one. The proxy can't depend on content so has a duplicate definition. This | |
54 // code can see both definitions so we do the validation here. | |
55 TEST_F(PepperGamepadHostTest, ValidateHardwareBuffersMatch) { | |
56 // Hardware buffer. | |
57 COMPILE_ASSERT(sizeof(ppapi::ContentGamepadHardwareBuffer) == | |
58 sizeof(content::GamepadHardwareBuffer), | |
59 gamepad_hardware_buffers_must_match); | |
60 ppapi::ContentGamepadHardwareBuffer ppapi_buf; | |
61 GamepadHardwareBuffer content_buf; | |
62 EXPECT_EQ(AddressDiff(&content_buf.sequence, &content_buf), | |
63 AddressDiff(&ppapi_buf.sequence, &ppapi_buf)); | |
64 EXPECT_EQ(AddressDiff(&content_buf.buffer, &content_buf), | |
65 AddressDiff(&ppapi_buf.buffer, &ppapi_buf)); | |
66 } | |
67 | |
68 TEST_F(PepperGamepadHostTest, ValidateGamepadsMatch) { | |
69 // Gamepads. | |
70 COMPILE_ASSERT(sizeof(ppapi::WebKitGamepads) == | |
71 sizeof(WebKit::WebGamepads), | |
72 gamepads_data_must_match); | |
73 ppapi::WebKitGamepads ppapi_gamepads; | |
74 WebKit::WebGamepads web_gamepads; | |
75 EXPECT_EQ(AddressDiff(&web_gamepads.length, &web_gamepads), | |
76 AddressDiff(&ppapi_gamepads.length, &ppapi_gamepads)); | |
77 EXPECT_EQ(web_gamepads.itemsLengthCap, ppapi_gamepads.kItemsLengthCap); | |
78 for (size_t i = 0; i < web_gamepads.itemsLengthCap; i++) { | |
79 EXPECT_EQ(AddressDiff(&web_gamepads.items[0], &web_gamepads), | |
80 AddressDiff(&ppapi_gamepads.items[0], &ppapi_gamepads)); | |
81 } | |
82 } | |
83 | |
84 TEST_F(PepperGamepadHostTest, ValidateGamepadMatch) { | |
85 // Gamepad. | |
86 COMPILE_ASSERT(sizeof(ppapi::WebKitGamepad) == | |
87 sizeof(WebKit::WebGamepad), | |
88 gamepad_data_must_match); | |
89 ppapi::WebKitGamepad ppapi_gamepad; | |
90 WebKit::WebGamepad web_gamepad; | |
91 EXPECT_EQ(web_gamepad.idLengthCap, ppapi_gamepad.kIdLengthCap); | |
92 EXPECT_EQ(web_gamepad.axesLengthCap, ppapi_gamepad.kAxesLengthCap); | |
93 EXPECT_EQ(web_gamepad.buttonsLengthCap, ppapi_gamepad.kButtonsLengthCap); | |
94 EXPECT_EQ(AddressDiff(&web_gamepad.connected, &web_gamepad), | |
95 AddressDiff(&ppapi_gamepad.connected, &ppapi_gamepad)); | |
96 EXPECT_EQ(AddressDiff(&web_gamepad.id, &web_gamepad), | |
97 AddressDiff(&ppapi_gamepad.id, &ppapi_gamepad)); | |
98 EXPECT_EQ(AddressDiff(&web_gamepad.timestamp, &web_gamepad), | |
99 AddressDiff(&ppapi_gamepad.timestamp, &ppapi_gamepad)); | |
100 EXPECT_EQ(AddressDiff(&web_gamepad.axesLength, &web_gamepad), | |
101 AddressDiff(&ppapi_gamepad.axes_length, &ppapi_gamepad)); | |
102 EXPECT_EQ(AddressDiff(&web_gamepad.axes, &web_gamepad), | |
103 AddressDiff(&ppapi_gamepad.axes, &ppapi_gamepad)); | |
104 EXPECT_EQ(AddressDiff(&web_gamepad.buttonsLength, &web_gamepad), | |
105 AddressDiff(&ppapi_gamepad.buttons_length, &ppapi_gamepad)); | |
106 EXPECT_EQ(AddressDiff(&web_gamepad.buttons, &web_gamepad), | |
107 AddressDiff(&ppapi_gamepad.buttons, &ppapi_gamepad)); | |
108 } | |
109 | |
110 TEST_F(PepperGamepadHostTest, WaitForReply) { | |
111 WebKit::WebGamepads default_data; | |
112 memset(&default_data, 0, sizeof(WebKit::WebGamepads)); | |
113 default_data.length = 1; | |
114 default_data.items[0].connected = true; | |
115 default_data.items[0].buttonsLength = 1; | |
116 ConstructService(default_data); | |
117 | |
118 PP_Instance pp_instance = 12345; | |
119 PP_Resource pp_resource = 67890; | |
120 PepperGamepadHost gamepad_host(gamepad_service(), GetPpapiHost(), | |
121 pp_instance, pp_resource); | |
122 | |
123 // Synthesize a request for gamepad data. | |
124 ppapi::host::HostMessageContext context( | |
125 ppapi::proxy::ResourceMessageCallParams(pp_resource, 1)); | |
126 EXPECT_EQ(PP_OK_COMPLETIONPENDING, | |
127 gamepad_host.OnResourceMessageReceived( | |
128 PpapiHostMsg_Gamepad_RequestMemory(), | |
129 &context)); | |
130 | |
131 // Wait for the gamepad background thread to read twice to make sure we | |
132 // don't get a message yet (see below for why). | |
133 MockGamepadDataFetcher* fetcher = service_->data_fetcher(); | |
134 fetcher->WaitForDataRead(); | |
135 fetcher->WaitForDataRead(); | |
136 | |
137 // It should not have sent the callback message. | |
138 service_->message_loop().RunAllPending(); | |
139 EXPECT_EQ(0u, sink().message_count()); | |
140 | |
141 // Set a button down and wait for it to be read twice. | |
142 // | |
143 // We wait for two reads before calling RunAllPending because the provider | |
144 // will read the data on the background thread (setting the event) and *then* | |
145 // will issue the callback on our thread. Waiting for it to read twice | |
146 // ensures that it was able to issue callbacks for the first read (if it | |
147 // issued one) before we try to check for it. | |
148 WebKit::WebGamepads button_down_data = default_data; | |
149 button_down_data.items[0].buttons[0] = 1.f; | |
150 fetcher->SetTestData(button_down_data); | |
151 fetcher->WaitForDataRead(); | |
152 fetcher->WaitForDataRead(); | |
153 | |
154 // It should have send a callback. | |
raymes
2012/09/05 21:05:57
*sent
| |
155 service_->message_loop().RunAllPending(); | |
156 ppapi::proxy::ResourceMessageReplyParams reply_params; | |
157 IPC::Message reply_msg; | |
158 ASSERT_TRUE(sink().GetFirstResourceReplyMatching( | |
159 PpapiPluginMsg_Gamepad_SendMemory::ID, &reply_params, &reply_msg)); | |
160 | |
161 // Validate the callback params. | |
162 PpapiPluginMsg_Gamepad_SendMemory::Schema::Param reply_msg_param; | |
163 ASSERT_TRUE(PpapiPluginMsg_Gamepad_SendMemory::Read(&reply_msg, | |
raymes
2012/09/05 21:05:57
Do you actually want to check values here?
brettw
2012/09/05 21:25:51
Done.
| |
164 &reply_msg_param)); | |
165 | |
166 // Duplicate requests should be denied. | |
167 EXPECT_EQ(PP_ERROR_FAILED, | |
168 gamepad_host.OnResourceMessageReceived( | |
169 PpapiHostMsg_Gamepad_RequestMemory(), | |
170 &context)); | |
171 } | |
172 | |
173 } // namespace content | |
OLD | NEW |