Chromium Code Reviews| 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); | |
| 88 provider->Resume(); | 89 provider->Resume(); |
| 89 | 90 |
| 90 message_loop().RunUntilIdle(); | 91 message_loop().RunUntilIdle(); |
| 91 | 92 |
| 92 mock_data_fetcher_->WaitForDataRead(); | 93 mock_data_fetcher_->WaitForDataRead(); |
| 93 | 94 |
| 94 // Renderer-side, pull data out of poll buffer. | 95 // Renderer-side, pull data out of poll buffer. |
| 95 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 96 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 96 base::GetCurrentProcessHandle()); | 97 base::GetCurrentProcessHandle()); |
| 97 scoped_ptr<base::SharedMemory> shared_memory( | 98 scoped_ptr<base::SharedMemory> shared_memory( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 no_button_data.items[0].buttons[0].pressed = false; | 131 no_button_data.items[0].buttons[0].pressed = false; |
| 131 no_button_data.items[0].axes[0] = 0.f; | 132 no_button_data.items[0].axes[0] = 0.f; |
| 132 no_button_data.items[0].axes[1] = .4f; | 133 no_button_data.items[0].axes[1] = .4f; |
| 133 | 134 |
| 134 WebGamepads button_down_data = no_button_data; | 135 WebGamepads button_down_data = no_button_data; |
| 135 button_down_data.items[0].buttons[0].value = 1.f; | 136 button_down_data.items[0].buttons[0].value = 1.f; |
| 136 button_down_data.items[0].buttons[0].pressed = true; | 137 button_down_data.items[0].buttons[0].pressed = true; |
| 137 | 138 |
| 138 UserGestureListener listener; | 139 UserGestureListener listener; |
| 139 GamepadProvider* provider = CreateProvider(no_button_data); | 140 GamepadProvider* provider = CreateProvider(no_button_data); |
| 141 provider->SetSanitizationEnabled(false); | |
| 140 provider->Resume(); | 142 provider->Resume(); |
| 141 | 143 |
| 142 provider->RegisterForUserGesture(listener.GetClosure()); | 144 provider->RegisterForUserGesture(listener.GetClosure()); |
| 143 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 145 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 144 | 146 |
| 145 // It should not have issued our callback. | 147 // It should not have issued our callback. |
| 146 message_loop().RunUntilIdle(); | 148 message_loop().RunUntilIdle(); |
| 147 EXPECT_FALSE(listener.has_user_gesture()); | 149 EXPECT_FALSE(listener.has_user_gesture()); |
| 148 | 150 |
| 149 // Set a button down and wait for it to be read twice. | 151 // Set a button down and wait for it to be read twice. |
| 150 mock_data_fetcher_->SetTestData(button_down_data); | 152 mock_data_fetcher_->SetTestData(button_down_data); |
| 151 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 153 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 152 | 154 |
| 153 // It should have issued our callback. | 155 // It should have issued our callback. |
| 154 message_loop().RunUntilIdle(); | 156 message_loop().RunUntilIdle(); |
| 155 EXPECT_TRUE(listener.has_user_gesture()); | 157 EXPECT_TRUE(listener.has_user_gesture()); |
| 156 } | 158 } |
| 157 | 159 |
| 160 // Tests that waiting for a user gesture works properly. | |
| 161 TEST_F(GamepadProviderTest, Sanitization) { | |
| 162 WebGamepads active_data; | |
| 163 active_data.length = 1; | |
| 164 active_data.items[0].connected = true; | |
| 165 active_data.items[0].timestamp = 0; | |
| 166 active_data.items[0].buttonsLength = 1; | |
| 167 active_data.items[0].axesLength = 1; | |
| 168 active_data.items[0].buttons[0].value = 1.f; | |
| 169 active_data.items[0].buttons[0].pressed = true; | |
| 170 active_data.items[0].axes[0] = -1.f; | |
| 171 | |
| 172 WebGamepads zero_data; | |
| 173 zero_data.length = 1; | |
| 174 zero_data.items[0].connected = true; | |
| 175 zero_data.items[0].timestamp = 0; | |
| 176 zero_data.items[0].buttonsLength = 1; | |
| 177 zero_data.items[0].axesLength = 1; | |
| 178 zero_data.items[0].buttons[0].value = 0.f; | |
| 179 zero_data.items[0].buttons[0].pressed = false; | |
| 180 zero_data.items[0].axes[0] = 0.f; | |
| 181 | |
| 182 UserGestureListener listener; | |
| 183 GamepadProvider* provider = CreateProvider(active_data); | |
| 184 provider->SetSanitizationEnabled(true); | |
| 185 provider->Resume(); | |
| 186 | |
| 187 message_loop().RunUntilIdle(); | |
| 188 | |
| 189 mock_data_fetcher_->WaitForDataRead(); | |
| 190 | |
| 191 // Renderer-side, pull data out of poll buffer. | |
| 192 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | |
| 193 base::GetCurrentProcessHandle()); | |
| 194 scoped_ptr<base::SharedMemory> shared_memory( | |
| 195 new base::SharedMemory(handle, true)); | |
| 196 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
| 197 void* mem = shared_memory->memory(); | |
| 198 | |
| 199 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
| 200 // See gamepad_hardware_buffer.h for details on the read discipline. | |
| 201 WebGamepads output; | |
| 202 | |
| 203 base::subtle::Atomic32 version; | |
| 204 do { | |
| 205 version = hwbuf->sequence.ReadBegin(); | |
| 206 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 207 } while (hwbuf->sequence.ReadRetry(version)); | |
| 208 | |
| 209 // Initial data should all be zeroed out due to sanitization, even though the | |
| 210 // gamepad reported input | |
|
scottmg
2016/01/18 22:54:51
'.'s to end comments in this file.
| |
| 211 EXPECT_EQ(1u, output.length); | |
| 212 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 213 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | |
| 214 EXPECT_EQ(false, output.items[0].buttons[0].pressed); | |
| 215 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 216 EXPECT_EQ(0.f, output.items[0].axes[0]); | |
| 217 | |
| 218 // Zero out the inputs | |
| 219 mock_data_fetcher_->SetTestData(zero_data); | |
| 220 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
| 221 | |
| 222 do { | |
| 223 version = hwbuf->sequence.ReadBegin(); | |
| 224 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 225 } while (hwbuf->sequence.ReadRetry(version)); | |
| 226 | |
| 227 // Should still read zero, which is now an accurate reflection of the data | |
| 228 EXPECT_EQ(1u, output.length); | |
| 229 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 230 EXPECT_EQ(0.f, output.items[0].buttons[0].value); | |
| 231 EXPECT_EQ(false, output.items[0].buttons[0].pressed); | |
| 232 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 233 EXPECT_EQ(0.f, output.items[0].axes[0]); | |
| 234 | |
| 235 // Re-set the active inputs | |
| 236 mock_data_fetcher_->SetTestData(active_data); | |
| 237 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
| 238 | |
| 239 do { | |
| 240 version = hwbuf->sequence.ReadBegin(); | |
| 241 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
| 242 } while (hwbuf->sequence.ReadRetry(version)); | |
| 243 | |
| 244 // Should now accurately reflect the reported data. | |
| 245 EXPECT_EQ(1u, output.length); | |
| 246 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 247 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | |
| 248 EXPECT_EQ(true, output.items[0].buttons[0].pressed); | |
| 249 EXPECT_EQ(1u, output.items[0].axesLength); | |
| 250 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
| 251 } | |
| 252 | |
| 158 } // namespace | 253 } // namespace |
| 159 | 254 |
| 160 } // namespace content | 255 } // namespace content |
| OLD | NEW |