| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Crashes. http://crbug.com/106163 | 73 // Crashes. http://crbug.com/106163 |
| 74 // crbug.com/147549 | 74 // crbug.com/147549 |
| 75 #if defined(OS_ANDROID) | 75 #if defined(OS_ANDROID) |
| 76 #define MAYBE_PollingAccess DISABLED_PollingAccess | 76 #define MAYBE_PollingAccess DISABLED_PollingAccess |
| 77 #else | 77 #else |
| 78 #define MAYBE_PollingAccess PollingAccess | 78 #define MAYBE_PollingAccess PollingAccess |
| 79 #endif | 79 #endif |
| 80 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { | 80 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { |
| 81 WebGamepads test_data; | 81 WebGamepads test_data; |
| 82 memset(&test_data, 0, sizeof(WebGamepads)); | 82 memset(&test_data, 0, sizeof(WebGamepads)); |
| 83 test_data.length = 1; | |
| 84 test_data.items[0].connected = true; | 83 test_data.items[0].connected = true; |
| 85 test_data.items[0].timestamp = 0; | 84 test_data.items[0].timestamp = 0; |
| 86 test_data.items[0].buttonsLength = 1; | 85 test_data.items[0].buttonsLength = 1; |
| 87 test_data.items[0].axesLength = 2; | 86 test_data.items[0].axesLength = 2; |
| 88 test_data.items[0].buttons[0].value = 1.f; | 87 test_data.items[0].buttons[0].value = 1.f; |
| 89 test_data.items[0].buttons[0].pressed = true; | 88 test_data.items[0].buttons[0].pressed = true; |
| 90 test_data.items[0].axes[0] = -1.f; | 89 test_data.items[0].axes[0] = -1.f; |
| 91 test_data.items[0].axes[1] = .5f; | 90 test_data.items[0].axes[1] = .5f; |
| 92 | 91 |
| 93 GamepadProvider* provider = CreateProvider(test_data); | 92 GamepadProvider* provider = CreateProvider(test_data); |
| 94 provider->SetSanitizationEnabled(false); | 93 provider->SetSanitizationEnabled(false); |
| 95 provider->Resume(); | 94 provider->Resume(); |
| 96 | 95 |
| 97 base::RunLoop().RunUntilIdle(); | 96 base::RunLoop().RunUntilIdle(); |
| 98 | 97 |
| 99 mock_data_fetcher_->WaitForDataRead(); | 98 mock_data_fetcher_->WaitForDataRead(); |
| 100 | 99 |
| 101 // Renderer-side, pull data out of poll buffer. | 100 // Renderer-side, pull data out of poll buffer. |
| 102 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 101 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 103 base::GetCurrentProcessHandle()); | 102 base::GetCurrentProcessHandle()); |
| 104 std::unique_ptr<base::SharedMemory> shared_memory( | 103 std::unique_ptr<base::SharedMemory> shared_memory( |
| 105 new base::SharedMemory(handle, true)); | 104 new base::SharedMemory(handle, true)); |
| 106 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 105 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 107 | 106 |
| 108 GamepadHardwareBuffer* buffer = | 107 GamepadHardwareBuffer* buffer = |
| 109 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); | 108 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); |
| 110 WebGamepads output; | 109 WebGamepads output; |
| 111 ReadGamepadHardwareBuffer(buffer, &output); | 110 ReadGamepadHardwareBuffer(buffer, &output); |
| 112 | 111 |
| 113 EXPECT_EQ(1u, output.length); | |
| 114 EXPECT_EQ(1u, output.items[0].buttonsLength); | 112 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 115 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | 113 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
| 116 EXPECT_EQ(true, output.items[0].buttons[0].pressed); | 114 EXPECT_EQ(true, output.items[0].buttons[0].pressed); |
| 117 EXPECT_EQ(2u, output.items[0].axesLength); | 115 EXPECT_EQ(2u, output.items[0].axesLength); |
| 118 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 116 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 119 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 117 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 120 } | 118 } |
| 121 | 119 |
| 120 // http://crbug.com/106163, crbug.com/147549 |
| 121 #if defined(OS_ANDROID) |
| 122 #define MAYBE_ConnectDisconnectMultiple DISABLED_ConnectDisconnectMultiple |
| 123 #else |
| 124 #define MAYBE_ConnectDisconnectMultiple ConnectDisconnectMultiple |
| 125 #endif |
| 126 TEST_F(GamepadProviderTest, MAYBE_ConnectDisconnectMultiple) { |
| 127 WebGamepads test_data; |
| 128 test_data.items[0].connected = true; |
| 129 test_data.items[0].timestamp = 0; |
| 130 test_data.items[0].axesLength = 2; |
| 131 test_data.items[0].axes[0] = -1.f; |
| 132 test_data.items[0].axes[1] = .5f; |
| 133 |
| 134 test_data.items[1].connected = true; |
| 135 test_data.items[1].timestamp = 0; |
| 136 test_data.items[1].axesLength = 2; |
| 137 test_data.items[1].axes[0] = 1.f; |
| 138 test_data.items[1].axes[1] = -.5f; |
| 139 |
| 140 WebGamepads test_data_onedisconnected; |
| 141 test_data_onedisconnected.items[1].connected = true; |
| 142 test_data_onedisconnected.items[1].timestamp = 0; |
| 143 test_data_onedisconnected.items[1].axesLength = 2; |
| 144 test_data_onedisconnected.items[1].axes[0] = 1.f; |
| 145 test_data_onedisconnected.items[1].axes[1] = -.5f; |
| 146 |
| 147 GamepadProvider* provider = CreateProvider(test_data); |
| 148 provider->SetSanitizationEnabled(false); |
| 149 provider->Resume(); |
| 150 |
| 151 base::RunLoop().RunUntilIdle(); |
| 152 |
| 153 mock_data_fetcher_->WaitForDataRead(); |
| 154 |
| 155 // Renderer-side, pull data out of poll buffer. |
| 156 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 157 base::GetCurrentProcessHandle()); |
| 158 std::unique_ptr<base::SharedMemory> shared_memory( |
| 159 new base::SharedMemory(handle, true)); |
| 160 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 161 |
| 162 GamepadHardwareBuffer* buffer = |
| 163 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); |
| 164 WebGamepads output; |
| 165 ReadGamepadHardwareBuffer(buffer, &output); |
| 166 |
| 167 EXPECT_EQ(2u, output.items[0].axesLength); |
| 168 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 169 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 170 EXPECT_EQ(2u, output.items[1].axesLength); |
| 171 EXPECT_EQ(1.f, output.items[1].axes[0]); |
| 172 EXPECT_EQ(-0.5f, output.items[1].axes[1]); |
| 173 |
| 174 mock_data_fetcher_->SetTestData(test_data_onedisconnected); |
| 175 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 176 ReadGamepadHardwareBuffer(buffer, &output); |
| 177 |
| 178 EXPECT_EQ(0u, output.items[0].axesLength); |
| 179 EXPECT_EQ(2u, output.items[1].axesLength); |
| 180 EXPECT_EQ(1.f, output.items[1].axes[0]); |
| 181 EXPECT_EQ(-0.5f, output.items[1].axes[1]); |
| 182 } |
| 183 |
| 122 // Tests that waiting for a user gesture works properly. | 184 // Tests that waiting for a user gesture works properly. |
| 123 TEST_F(GamepadProviderTest, UserGesture) { | 185 TEST_F(GamepadProviderTest, UserGesture) { |
| 124 WebGamepads no_button_data; | 186 WebGamepads no_button_data; |
| 125 no_button_data.length = 1; | |
| 126 no_button_data.items[0].connected = true; | 187 no_button_data.items[0].connected = true; |
| 127 no_button_data.items[0].timestamp = 0; | 188 no_button_data.items[0].timestamp = 0; |
| 128 no_button_data.items[0].buttonsLength = 1; | 189 no_button_data.items[0].buttonsLength = 1; |
| 129 no_button_data.items[0].axesLength = 2; | 190 no_button_data.items[0].axesLength = 2; |
| 130 no_button_data.items[0].buttons[0].value = 0.f; | 191 no_button_data.items[0].buttons[0].value = 0.f; |
| 131 no_button_data.items[0].buttons[0].pressed = false; | 192 no_button_data.items[0].buttons[0].pressed = false; |
| 132 no_button_data.items[0].axes[0] = 0.f; | 193 no_button_data.items[0].axes[0] = 0.f; |
| 133 no_button_data.items[0].axes[1] = .4f; | 194 no_button_data.items[0].axes[1] = .4f; |
| 134 | 195 |
| 135 WebGamepads button_down_data = no_button_data; | 196 WebGamepads button_down_data = no_button_data; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 163 #if defined(OS_ANDROID) | 224 #if defined(OS_ANDROID) |
| 164 #define MAYBE_Sanitization DISABLED_Sanitization | 225 #define MAYBE_Sanitization DISABLED_Sanitization |
| 165 #elif defined(MEMORY_SANITIZER) | 226 #elif defined(MEMORY_SANITIZER) |
| 166 #define MAYBE_Sanitization DISABLED_Sanitization | 227 #define MAYBE_Sanitization DISABLED_Sanitization |
| 167 #else | 228 #else |
| 168 #define MAYBE_Sanitization Sanitization | 229 #define MAYBE_Sanitization Sanitization |
| 169 #endif | 230 #endif |
| 170 // Tests that waiting for a user gesture works properly. | 231 // Tests that waiting for a user gesture works properly. |
| 171 TEST_F(GamepadProviderTest, MAYBE_Sanitization) { | 232 TEST_F(GamepadProviderTest, MAYBE_Sanitization) { |
| 172 WebGamepads active_data; | 233 WebGamepads active_data; |
| 173 active_data.length = 1; | |
| 174 active_data.items[0].connected = true; | 234 active_data.items[0].connected = true; |
| 175 active_data.items[0].timestamp = 0; | 235 active_data.items[0].timestamp = 0; |
| 176 active_data.items[0].buttonsLength = 1; | 236 active_data.items[0].buttonsLength = 1; |
| 177 active_data.items[0].axesLength = 1; | 237 active_data.items[0].axesLength = 1; |
| 178 active_data.items[0].buttons[0].value = 1.f; | 238 active_data.items[0].buttons[0].value = 1.f; |
| 179 active_data.items[0].buttons[0].pressed = true; | 239 active_data.items[0].buttons[0].pressed = true; |
| 180 active_data.items[0].axes[0] = -1.f; | 240 active_data.items[0].axes[0] = -1.f; |
| 181 | 241 |
| 182 WebGamepads zero_data; | 242 WebGamepads zero_data; |
| 183 zero_data.length = 1; | |
| 184 zero_data.items[0].connected = true; | 243 zero_data.items[0].connected = true; |
| 185 zero_data.items[0].timestamp = 0; | 244 zero_data.items[0].timestamp = 0; |
| 186 zero_data.items[0].buttonsLength = 1; | 245 zero_data.items[0].buttonsLength = 1; |
| 187 zero_data.items[0].axesLength = 1; | 246 zero_data.items[0].axesLength = 1; |
| 188 zero_data.items[0].buttons[0].value = 0.f; | 247 zero_data.items[0].buttons[0].value = 0.f; |
| 189 zero_data.items[0].buttons[0].pressed = false; | 248 zero_data.items[0].buttons[0].pressed = false; |
| 190 zero_data.items[0].axes[0] = 0.f; | 249 zero_data.items[0].axes[0] = 0.f; |
| 191 | 250 |
| 192 UserGestureListener listener; | 251 UserGestureListener listener; |
| 193 GamepadProvider* provider = CreateProvider(active_data); | 252 GamepadProvider* provider = CreateProvider(active_data); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 205 new base::SharedMemory(handle, true)); | 264 new base::SharedMemory(handle, true)); |
| 206 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 265 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 207 | 266 |
| 208 GamepadHardwareBuffer* buffer = | 267 GamepadHardwareBuffer* buffer = |
| 209 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); | 268 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); |
| 210 WebGamepads output; | 269 WebGamepads output; |
| 211 ReadGamepadHardwareBuffer(buffer, &output); | 270 ReadGamepadHardwareBuffer(buffer, &output); |
| 212 | 271 |
| 213 // Initial data should all be zeroed out due to sanitization, even though the | 272 // Initial data should all be zeroed out due to sanitization, even though the |
| 214 // gamepad reported input | 273 // gamepad reported input |
| 215 EXPECT_EQ(1u, output.length); | |
| 216 EXPECT_EQ(1u, output.items[0].buttonsLength); | 274 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 217 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | 275 EXPECT_EQ(0.f, output.items[0].buttons[0].value); |
| 218 EXPECT_FALSE(output.items[0].buttons[0].pressed); | 276 EXPECT_FALSE(output.items[0].buttons[0].pressed); |
| 219 EXPECT_EQ(1u, output.items[0].axesLength); | 277 EXPECT_EQ(1u, output.items[0].axesLength); |
| 220 EXPECT_EQ(0.f, output.items[0].axes[0]); | 278 EXPECT_EQ(0.f, output.items[0].axes[0]); |
| 221 | 279 |
| 222 // Zero out the inputs | 280 // Zero out the inputs |
| 223 mock_data_fetcher_->SetTestData(zero_data); | 281 mock_data_fetcher_->SetTestData(zero_data); |
| 224 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 282 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 225 | 283 |
| 226 // Read updated data from shared memory | 284 // Read updated data from shared memory |
| 227 ReadGamepadHardwareBuffer(buffer, &output); | 285 ReadGamepadHardwareBuffer(buffer, &output); |
| 228 | 286 |
| 229 // Should still read zero, which is now an accurate reflection of the data | 287 // Should still read zero, which is now an accurate reflection of the data |
| 230 EXPECT_EQ(1u, output.length); | |
| 231 EXPECT_EQ(1u, output.items[0].buttonsLength); | 288 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 232 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | 289 EXPECT_EQ(0.f, output.items[0].buttons[0].value); |
| 233 EXPECT_FALSE(output.items[0].buttons[0].pressed); | 290 EXPECT_FALSE(output.items[0].buttons[0].pressed); |
| 234 EXPECT_EQ(1u, output.items[0].axesLength); | 291 EXPECT_EQ(1u, output.items[0].axesLength); |
| 235 EXPECT_EQ(0.f, output.items[0].axes[0]); | 292 EXPECT_EQ(0.f, output.items[0].axes[0]); |
| 236 | 293 |
| 237 // Re-set the active inputs | 294 // Re-set the active inputs |
| 238 mock_data_fetcher_->SetTestData(active_data); | 295 mock_data_fetcher_->SetTestData(active_data); |
| 239 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 296 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 240 | 297 |
| 241 // Read updated data from shared memory | 298 // Read updated data from shared memory |
| 242 ReadGamepadHardwareBuffer(buffer, &output); | 299 ReadGamepadHardwareBuffer(buffer, &output); |
| 243 | 300 |
| 244 // Should now accurately reflect the reported data. | 301 // Should now accurately reflect the reported data. |
| 245 EXPECT_EQ(1u, output.length); | |
| 246 EXPECT_EQ(1u, output.items[0].buttonsLength); | 302 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 247 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | 303 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
| 248 EXPECT_TRUE(output.items[0].buttons[0].pressed); | 304 EXPECT_TRUE(output.items[0].buttons[0].pressed); |
| 249 EXPECT_EQ(1u, output.items[0].axesLength); | 305 EXPECT_EQ(1u, output.items[0].axesLength); |
| 250 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 306 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 251 } | 307 } |
| 252 | 308 |
| 253 } // namespace | 309 } // namespace |
| 254 | 310 |
| 255 } // namespace device | 311 } // namespace device |
| OLD | NEW |