| 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "content/browser/gamepad/gamepad_data_fetcher.h" | 9 #include "content/browser/gamepad/gamepad_data_fetcher.h" |
| 10 #include "content/browser/gamepad/gamepad_provider.h" | 10 #include "content/browser/gamepad/gamepad_provider.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 test_data.items[0].connected = true; | 78 test_data.items[0].connected = true; |
| 79 test_data.items[0].timestamp = 0; | 79 test_data.items[0].timestamp = 0; |
| 80 test_data.items[0].buttonsLength = 1; | 80 test_data.items[0].buttonsLength = 1; |
| 81 test_data.items[0].axesLength = 2; | 81 test_data.items[0].axesLength = 2; |
| 82 test_data.items[0].buttons[0].value = 1.f; | 82 test_data.items[0].buttons[0].value = 1.f; |
| 83 test_data.items[0].buttons[0].pressed = true; | 83 test_data.items[0].buttons[0].pressed = true; |
| 84 test_data.items[0].axes[0] = -1.f; | 84 test_data.items[0].axes[0] = -1.f; |
| 85 test_data.items[0].axes[1] = .5f; | 85 test_data.items[0].axes[1] = .5f; |
| 86 | 86 |
| 87 GamepadProvider* provider = CreateProvider(test_data); | 87 GamepadProvider* provider = CreateProvider(test_data); |
| 88 provider->SetSanitizationEnabled(false); | |
| 89 provider->Resume(); | 88 provider->Resume(); |
| 90 | 89 |
| 91 message_loop().RunUntilIdle(); | 90 message_loop().RunUntilIdle(); |
| 92 | 91 |
| 93 mock_data_fetcher_->WaitForDataRead(); | 92 mock_data_fetcher_->WaitForDataRead(); |
| 94 | 93 |
| 95 // Renderer-side, pull data out of poll buffer. | 94 // Renderer-side, pull data out of poll buffer. |
| 96 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 95 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 97 base::GetCurrentProcessHandle()); | 96 base::GetCurrentProcessHandle()); |
| 98 scoped_ptr<base::SharedMemory> shared_memory( | 97 scoped_ptr<base::SharedMemory> shared_memory( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 no_button_data.items[0].buttons[0].pressed = false; | 130 no_button_data.items[0].buttons[0].pressed = false; |
| 132 no_button_data.items[0].axes[0] = 0.f; | 131 no_button_data.items[0].axes[0] = 0.f; |
| 133 no_button_data.items[0].axes[1] = .4f; | 132 no_button_data.items[0].axes[1] = .4f; |
| 134 | 133 |
| 135 WebGamepads button_down_data = no_button_data; | 134 WebGamepads button_down_data = no_button_data; |
| 136 button_down_data.items[0].buttons[0].value = 1.f; | 135 button_down_data.items[0].buttons[0].value = 1.f; |
| 137 button_down_data.items[0].buttons[0].pressed = true; | 136 button_down_data.items[0].buttons[0].pressed = true; |
| 138 | 137 |
| 139 UserGestureListener listener; | 138 UserGestureListener listener; |
| 140 GamepadProvider* provider = CreateProvider(no_button_data); | 139 GamepadProvider* provider = CreateProvider(no_button_data); |
| 141 provider->SetSanitizationEnabled(false); | |
| 142 provider->Resume(); | 140 provider->Resume(); |
| 143 | 141 |
| 144 provider->RegisterForUserGesture(listener.GetClosure()); | 142 provider->RegisterForUserGesture(listener.GetClosure()); |
| 145 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 143 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 146 | 144 |
| 147 // It should not have issued our callback. | 145 // It should not have issued our callback. |
| 148 message_loop().RunUntilIdle(); | 146 message_loop().RunUntilIdle(); |
| 149 EXPECT_FALSE(listener.has_user_gesture()); | 147 EXPECT_FALSE(listener.has_user_gesture()); |
| 150 | 148 |
| 151 // Set a button down and wait for it to be read twice. | 149 // Set a button down and wait for it to be read twice. |
| 152 mock_data_fetcher_->SetTestData(button_down_data); | 150 mock_data_fetcher_->SetTestData(button_down_data); |
| 153 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 151 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 154 | 152 |
| 155 // It should have issued our callback. | 153 // It should have issued our callback. |
| 156 message_loop().RunUntilIdle(); | 154 message_loop().RunUntilIdle(); |
| 157 EXPECT_TRUE(listener.has_user_gesture()); | 155 EXPECT_TRUE(listener.has_user_gesture()); |
| 158 } | 156 } |
| 159 | 157 |
| 160 // Tests that waiting for a user gesture works properly. | |
| 161 // Crashes on android. Seems to be same issue as the PollingAccess test | |
| 162 // crbug.com/147549 | |
| 163 #if defined(OS_ANDROID) | |
| 164 #define MAYBE_Sanitization DISABLED_Sanitization | |
| 165 #else | |
| 166 #define MAYBE_Sanitization Sanitization | |
| 167 #endif | |
| 168 TEST_F(GamepadProviderTest, MAYBE_Sanitization) { | |
| 169 WebGamepads active_data; | |
| 170 active_data.length = 1; | |
| 171 active_data.items[0].connected = true; | |
| 172 active_data.items[0].timestamp = 0; | |
| 173 active_data.items[0].buttonsLength = 1; | |
| 174 active_data.items[0].axesLength = 1; | |
| 175 active_data.items[0].buttons[0].value = 1.f; | |
| 176 active_data.items[0].buttons[0].pressed = true; | |
| 177 active_data.items[0].axes[0] = -1.f; | |
| 178 | |
| 179 WebGamepads zero_data; | |
| 180 zero_data.length = 1; | |
| 181 zero_data.items[0].connected = true; | |
| 182 zero_data.items[0].timestamp = 0; | |
| 183 zero_data.items[0].buttonsLength = 1; | |
| 184 zero_data.items[0].axesLength = 1; | |
| 185 zero_data.items[0].buttons[0].value = 0.f; | |
| 186 zero_data.items[0].buttons[0].pressed = false; | |
| 187 zero_data.items[0].axes[0] = 0.f; | |
| 188 | |
| 189 UserGestureListener listener; | |
| 190 GamepadProvider* provider = CreateProvider(active_data); | |
| 191 provider->SetSanitizationEnabled(true); | |
| 192 provider->Resume(); | |
| 193 | |
| 194 message_loop().RunUntilIdle(); | |
| 195 | |
| 196 mock_data_fetcher_->WaitForDataRead(); | |
| 197 | |
| 198 // Renderer-side, pull data out of poll buffer. | |
| 199 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | |
| 200 base::GetCurrentProcessHandle()); | |
| 201 scoped_ptr<base::SharedMemory> shared_memory( | |
| 202 new base::SharedMemory(handle, true)); | |
| 203 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
| 204 void* mem = shared_memory->memory(); | |
| 205 | |
| 206 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
| 207 // See gamepad_hardware_buffer.h for details on the read discipline. | |
| 208 WebGamepads output; | |
| 209 | |
| 210 base::subtle::Atomic32 version; | |
| 211 do { | |
| 212 version = hwbuf->sequence.ReadBegin(); | |
| 213 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 214 } while (hwbuf->sequence.ReadRetry(version)); | |
| 215 | |
| 216 // Initial data should all be zeroed out due to sanitization, even though the | |
| 217 // gamepad reported input. | |
| 218 EXPECT_EQ(1u, output.length); | |
| 219 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 220 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | |
| 221 EXPECT_FALSE(output.items[0].buttons[0].pressed); | |
| 222 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 223 EXPECT_EQ(0.f, output.items[0].axes[0]); | |
| 224 | |
| 225 // Zero out the inputs. | |
| 226 mock_data_fetcher_->SetTestData(zero_data); | |
| 227 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
| 228 | |
| 229 do { | |
| 230 version = hwbuf->sequence.ReadBegin(); | |
| 231 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 232 } while (hwbuf->sequence.ReadRetry(version)); | |
| 233 | |
| 234 // Should still read zero, which is now an accurate reflection of the data. | |
| 235 EXPECT_EQ(1u, output.length); | |
| 236 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 237 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | |
| 238 EXPECT_FALSE(output.items[0].buttons[0].pressed); | |
| 239 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 240 EXPECT_EQ(0.f, output.items[0].axes[0]); | |
| 241 | |
| 242 // Re-set the active inputs. | |
| 243 mock_data_fetcher_->SetTestData(active_data); | |
| 244 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
| 245 | |
| 246 do { | |
| 247 version = hwbuf->sequence.ReadBegin(); | |
| 248 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 249 } while (hwbuf->sequence.ReadRetry(version)); | |
| 250 | |
| 251 // Should now accurately reflect the reported data. | |
| 252 EXPECT_EQ(1u, output.length); | |
| 253 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 254 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | |
| 255 EXPECT_TRUE(output.items[0].buttons[0].pressed); | |
| 256 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 257 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
| 258 } | |
| 259 | |
| 260 // Tests that waiting for a user gesture works properly. | |
| 261 // Crashes on android. Seems to be same issue as the PollingAccess test | |
| 262 // crbug.com/147549 | |
| 263 #if defined(OS_ANDROID) | |
| 264 #define MAYBE_DynamicallyAddedDataFetcher DISABLED_DynamicallyAddedDataFetcher | |
| 265 #else | |
| 266 #define MAYBE_DynamicallyAddedDataFetcher DynamicallyAddedDataFetcher | |
| 267 #endif | |
| 268 TEST_F(GamepadProviderTest, MAYBE_DynamicallyAddedDataFetcher) { | |
| 269 WebGamepads active_data; | |
| 270 active_data.length = 1; | |
| 271 active_data.items[0].connected = true; | |
| 272 active_data.items[0].timestamp = 0; | |
| 273 active_data.items[0].buttonsLength = 1; | |
| 274 active_data.items[0].axesLength = 1; | |
| 275 active_data.items[0].buttons[0].value = 1.f; | |
| 276 active_data.items[0].buttons[0].pressed = true; | |
| 277 active_data.items[0].axes[0] = -1.f; | |
| 278 | |
| 279 UserGestureListener listener; | |
| 280 GamepadProvider* provider = CreateProvider(active_data); | |
| 281 provider->SetSanitizationEnabled(false); | |
| 282 provider->Resume(); | |
| 283 | |
| 284 message_loop().RunUntilIdle(); | |
| 285 | |
| 286 mock_data_fetcher_->WaitForDataRead(); | |
| 287 | |
| 288 // Renderer-side, pull data out of poll buffer. | |
| 289 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | |
| 290 base::GetCurrentProcessHandle()); | |
| 291 scoped_ptr<base::SharedMemory> shared_memory( | |
| 292 new base::SharedMemory(handle, true)); | |
| 293 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
| 294 void* mem = shared_memory->memory(); | |
| 295 | |
| 296 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
| 297 // See gamepad_hardware_buffer.h for details on the read discipline. | |
| 298 WebGamepads output; | |
| 299 | |
| 300 base::subtle::Atomic32 version; | |
| 301 do { | |
| 302 version = hwbuf->sequence.ReadBegin(); | |
| 303 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 304 } while (hwbuf->sequence.ReadRetry(version)); | |
| 305 | |
| 306 // Should still accurately report the test data. | |
| 307 EXPECT_EQ(1u, output.length); | |
| 308 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 309 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | |
| 310 EXPECT_TRUE(output.items[0].buttons[0].pressed); | |
| 311 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 312 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
| 313 | |
| 314 WebGamepads active_data_2; | |
| 315 active_data_2.length = 1; | |
| 316 active_data_2.items[1].connected = true; | |
| 317 active_data_2.items[1].timestamp = 0; | |
| 318 active_data_2.items[1].buttonsLength = 1; | |
| 319 active_data_2.items[1].axesLength = 1; | |
| 320 active_data_2.items[1].buttons[0].value = 0.5f; | |
| 321 active_data_2.items[1].buttons[0].pressed = true; | |
| 322 active_data_2.items[1].axes[0] = 0.5f; | |
| 323 | |
| 324 // Add a new data fetcher. | |
| 325 provider->AddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher>( | |
| 326 new MockGamepadDataFetcher(active_data_2))); | |
| 327 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
| 328 | |
| 329 do { | |
| 330 version = hwbuf->sequence.ReadBegin(); | |
| 331 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 332 } while (hwbuf->sequence.ReadRetry(version)); | |
| 333 | |
| 334 // Should now report test data from both data fetchers. | |
| 335 EXPECT_EQ(2u, output.length); | |
| 336 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 337 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | |
| 338 EXPECT_TRUE(output.items[0].buttons[0].pressed); | |
| 339 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 340 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
| 341 | |
| 342 EXPECT_EQ(1u, output.items[1].buttonsLength); | |
| 343 EXPECT_EQ(0.5f, output.items[1].buttons[0].value); | |
| 344 EXPECT_TRUE(output.items[1].buttons[0].pressed); | |
| 345 EXPECT_EQ(1u, output.items[1].axesLength); | |
| 346 EXPECT_EQ(0.5f, output.items[1].axes[0]); | |
| 347 } | |
| 348 | |
| 349 } // namespace | 158 } // namespace |
| 350 | 159 |
| 351 } // namespace content | 160 } // namespace content |
| OLD | NEW |