OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/gamepad/gamepad_provider.h" | 5 #include "device/gamepad/gamepad_provider.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 test_data.items[0].connected = true; | 74 test_data.items[0].connected = true; |
75 test_data.items[0].timestamp = 0; | 75 test_data.items[0].timestamp = 0; |
76 test_data.items[0].buttonsLength = 1; | 76 test_data.items[0].buttonsLength = 1; |
77 test_data.items[0].axesLength = 2; | 77 test_data.items[0].axesLength = 2; |
78 test_data.items[0].buttons[0].value = 1.f; | 78 test_data.items[0].buttons[0].value = 1.f; |
79 test_data.items[0].buttons[0].pressed = true; | 79 test_data.items[0].buttons[0].pressed = true; |
80 test_data.items[0].axes[0] = -1.f; | 80 test_data.items[0].axes[0] = -1.f; |
81 test_data.items[0].axes[1] = .5f; | 81 test_data.items[0].axes[1] = .5f; |
82 | 82 |
83 GamepadProvider* provider = CreateProvider(test_data); | 83 GamepadProvider* provider = CreateProvider(test_data); |
84 provider->SetSanitizationEnabled(false); | |
84 provider->Resume(); | 85 provider->Resume(); |
85 | 86 |
86 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
87 | 88 |
88 mock_data_fetcher_->WaitForDataRead(); | 89 mock_data_fetcher_->WaitForDataRead(); |
89 | 90 |
90 // Renderer-side, pull data out of poll buffer. | 91 // Renderer-side, pull data out of poll buffer. |
91 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 92 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
92 base::GetCurrentProcessHandle()); | 93 base::GetCurrentProcessHandle()); |
93 std::unique_ptr<base::SharedMemory> shared_memory( | 94 std::unique_ptr<base::SharedMemory> shared_memory( |
(...skipping 24 matching lines...) Expand all Loading... | |
118 no_button_data.items[0].buttons[0].pressed = false; | 119 no_button_data.items[0].buttons[0].pressed = false; |
119 no_button_data.items[0].axes[0] = 0.f; | 120 no_button_data.items[0].axes[0] = 0.f; |
120 no_button_data.items[0].axes[1] = .4f; | 121 no_button_data.items[0].axes[1] = .4f; |
121 | 122 |
122 WebGamepads button_down_data = no_button_data; | 123 WebGamepads button_down_data = no_button_data; |
123 button_down_data.items[0].buttons[0].value = 1.f; | 124 button_down_data.items[0].buttons[0].value = 1.f; |
124 button_down_data.items[0].buttons[0].pressed = true; | 125 button_down_data.items[0].buttons[0].pressed = true; |
125 | 126 |
126 UserGestureListener listener; | 127 UserGestureListener listener; |
127 GamepadProvider* provider = CreateProvider(no_button_data); | 128 GamepadProvider* provider = CreateProvider(no_button_data); |
129 provider->SetSanitizationEnabled(false); | |
128 provider->Resume(); | 130 provider->Resume(); |
129 | 131 |
130 provider->RegisterForUserGesture(listener.GetClosure()); | 132 provider->RegisterForUserGesture(listener.GetClosure()); |
131 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 133 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
132 | 134 |
133 // It should not have issued our callback. | 135 // It should not have issued our callback. |
134 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
135 EXPECT_FALSE(listener.has_user_gesture()); | 137 EXPECT_FALSE(listener.has_user_gesture()); |
136 | 138 |
137 // Set a button down and wait for it to be read twice. | 139 // Set a button down and wait for it to be read twice. |
138 mock_data_fetcher_->SetTestData(button_down_data); | 140 mock_data_fetcher_->SetTestData(button_down_data); |
139 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 141 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
140 | 142 |
141 // It should have issued our callback. | 143 // It should have issued our callback. |
142 base::RunLoop().RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
143 EXPECT_TRUE(listener.has_user_gesture()); | 145 EXPECT_TRUE(listener.has_user_gesture()); |
144 } | 146 } |
145 | 147 |
148 // Tests that waiting for a user gesture works properly. | |
denniskempin
2016/07/11 22:44:50
update comment
| |
149 TEST_F(GamepadProviderTest, Sanitization) { | |
150 WebGamepads active_data; | |
151 active_data.length = 1; | |
152 active_data.items[0].connected = true; | |
153 active_data.items[0].timestamp = 0; | |
154 active_data.items[0].buttonsLength = 1; | |
155 active_data.items[0].axesLength = 1; | |
156 active_data.items[0].buttons[0].value = 1.f; | |
157 active_data.items[0].buttons[0].pressed = true; | |
158 active_data.items[0].axes[0] = -1.f; | |
159 | |
160 WebGamepads zero_data; | |
161 zero_data.length = 1; | |
162 zero_data.items[0].connected = true; | |
163 zero_data.items[0].timestamp = 0; | |
164 zero_data.items[0].buttonsLength = 1; | |
165 zero_data.items[0].axesLength = 1; | |
166 zero_data.items[0].buttons[0].value = 0.f; | |
167 zero_data.items[0].buttons[0].pressed = false; | |
168 zero_data.items[0].axes[0] = 0.f; | |
169 | |
170 UserGestureListener listener; | |
171 GamepadProvider* provider = CreateProvider(active_data); | |
172 provider->SetSanitizationEnabled(true); | |
173 provider->Resume(); | |
174 | |
175 message_loop().RunUntilIdle(); | |
176 | |
177 mock_data_fetcher_->WaitForDataRead(); | |
178 | |
179 // Renderer-side, pull data out of poll buffer. | |
180 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | |
181 base::GetCurrentProcessHandle()); | |
182 std::unique_ptr<base::SharedMemory> shared_memory( | |
183 new base::SharedMemory(handle, true)); | |
184 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); | |
185 void* mem = shared_memory->memory(); | |
186 | |
187 WebGamepads* output = static_cast<WebGamepads*>(mem); | |
188 | |
189 // Initial data should all be zeroed out due to sanitization, even though the | |
190 // gamepad reported input | |
191 EXPECT_EQ(1u, output->length); | |
192 EXPECT_EQ(1u, output->items[0].buttonsLength); | |
193 EXPECT_EQ(0.f, output->items[0].buttons[0].value); | |
194 EXPECT_FALSE(output->items[0].buttons[0].pressed); | |
195 EXPECT_EQ(1u, output->items[0].axesLength); | |
196 EXPECT_EQ(0.f, output->items[0].axes[0]); | |
197 | |
198 // Zero out the inputs | |
199 mock_data_fetcher_->SetTestData(zero_data); | |
200 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
201 | |
202 // Should still read zero, which is now an accurate reflection of the data | |
203 EXPECT_EQ(1u, output->length); | |
204 EXPECT_EQ(1u, output->items[0].buttonsLength); | |
205 EXPECT_EQ(0.f, output->items[0].buttons[0].value); | |
206 EXPECT_FALSE(output->items[0].buttons[0].pressed); | |
207 EXPECT_EQ(1u, output->items[0].axesLength); | |
208 EXPECT_EQ(0.f, output->items[0].axes[0]); | |
209 | |
210 // Re-set the active inputs | |
211 mock_data_fetcher_->SetTestData(active_data); | |
212 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
213 | |
214 // Should now accurately reflect the reported data. | |
215 EXPECT_EQ(1u, output->length); | |
216 EXPECT_EQ(1u, output->items[0].buttonsLength); | |
217 EXPECT_EQ(1.f, output->items[0].buttons[0].value); | |
218 EXPECT_TRUE(output->items[0].buttons[0].pressed); | |
219 EXPECT_EQ(1u, output->items[0].axesLength); | |
220 EXPECT_EQ(-1.f, output->items[0].axes[0]); | |
221 } | |
222 | |
146 } // namespace | 223 } // namespace |
147 | 224 |
148 } // namespace device | 225 } // namespace device |
OLD | NEW |