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 "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 27 matching lines...) Expand all Loading... | |
| 38 bool has_user_gesture_; | 38 bool has_user_gesture_; |
| 39 base::WeakPtrFactory<UserGestureListener> weak_factory_; | 39 base::WeakPtrFactory<UserGestureListener> weak_factory_; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 // Main test fixture | 42 // Main test fixture |
| 43 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { | 43 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { |
| 44 public: | 44 public: |
| 45 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | 45 GamepadProvider* CreateProvider(const WebGamepads& test_data) { |
| 46 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); | 46 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); |
| 47 provider_.reset(new GamepadProvider( | 47 provider_.reset(new GamepadProvider( |
| 48 std::unique_ptr<GamepadSharedBuffer>(new MockGamepadSharedBuffer()), | |
| 49 nullptr, std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_))); | 48 nullptr, std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_))); |
| 50 return provider_.get(); | 49 return provider_.get(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 protected: | 52 protected: |
| 54 GamepadProviderTest() {} | 53 GamepadProviderTest() {} |
| 55 | 54 |
| 56 std::unique_ptr<GamepadProvider> provider_; | 55 std::unique_ptr<GamepadProvider> provider_; |
| 57 | 56 |
| 58 // Pointer owned by the provider. | 57 // Pointer owned by the provider. |
| 59 MockGamepadDataFetcher* mock_data_fetcher_; | 58 MockGamepadDataFetcher* mock_data_fetcher_; |
| 60 | 59 |
| 61 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); | 60 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 // Crashes. http://crbug.com/106163 | 63 // Crashes. http://crbug.com/106163 |
| 65 // crbug.com/147549 | 64 // crbug.com/147549 |
| 66 #if defined(OS_ANDROID) | 65 #if defined(OS_ANDROID) |
| 67 #define MAYBE_PollingAccess DISABLED_PollingAccess | 66 #define MAYBE_PollingAccess DISABLED_PollingAccess |
| 68 #else | 67 #else |
| 69 #define MAYBE_PollingAccess PollingAccess | 68 #define MAYBE_PollingAccess PollingAccess |
| 70 #endif | 69 #endif |
| 71 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { | 70 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { |
| 72 WebGamepads test_data; | 71 WebGamepads test_data; |
| 72 memset(&test_data, 0, sizeof(WebGamepads)); | |
| 73 test_data.length = 1; | 73 test_data.length = 1; |
| 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->SetSanitizationEnabled(false); |
| 85 provider->Resume(); | 85 provider->Resume(); |
| 86 | 86 |
| 87 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
| 88 | 88 |
| 89 mock_data_fetcher_->WaitForDataRead(); | 89 mock_data_fetcher_->WaitForDataRead(); |
| 90 | 90 |
| 91 // Renderer-side, pull data out of poll buffer. | 91 // Renderer-side, pull data out of poll buffer. |
| 92 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 92 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 93 base::GetCurrentProcessHandle()); | 93 base::GetCurrentProcessHandle()); |
| 94 std::unique_ptr<base::SharedMemory> shared_memory( | 94 std::unique_ptr<base::SharedMemory> shared_memory( |
| 95 new base::SharedMemory(handle, true)); | 95 new base::SharedMemory(handle, true)); |
| 96 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); | 96 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 97 void* mem = shared_memory->memory(); | |
| 98 | 97 |
| 99 WebGamepads* output = static_cast<WebGamepads*>(mem); | 98 WebGamepads output; |
| 99 memset(&output, 0, sizeof(WebGamepads)); | |
| 100 GamepadHardwareBuffer* buffer = | |
| 101 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); | |
| 102 base::subtle::Atomic32 version; | |
| 103 do { | |
|
ke.he
2016/12/09 07:44:55
Before this patch, it is the "MockGamepadSharedBuf
blundell
2016/12/09 13:35:22
Seems like it would be worthwhile to make a helper
ke.he
2016/12/09 15:15:22
Done.
| |
| 104 version = buffer->seqlock.ReadBegin(); | |
| 105 memcpy(&output, &buffer->data, sizeof(WebGamepads)); | |
| 106 } while (buffer->seqlock.ReadRetry(version)); | |
| 100 | 107 |
| 101 EXPECT_EQ(1u, output->length); | 108 EXPECT_EQ(1u, output.length); |
| 102 EXPECT_EQ(1u, output->items[0].buttonsLength); | 109 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 103 EXPECT_EQ(1.f, output->items[0].buttons[0].value); | 110 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
| 104 EXPECT_EQ(true, output->items[0].buttons[0].pressed); | 111 EXPECT_EQ(true, output.items[0].buttons[0].pressed); |
| 105 EXPECT_EQ(2u, output->items[0].axesLength); | 112 EXPECT_EQ(2u, output.items[0].axesLength); |
| 106 EXPECT_EQ(-1.f, output->items[0].axes[0]); | 113 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 107 EXPECT_EQ(0.5f, output->items[0].axes[1]); | 114 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 108 } | 115 } |
| 109 | 116 |
| 110 // Tests that waiting for a user gesture works properly. | 117 // Tests that waiting for a user gesture works properly. |
| 111 TEST_F(GamepadProviderTest, UserGesture) { | 118 TEST_F(GamepadProviderTest, UserGesture) { |
| 112 WebGamepads no_button_data; | 119 WebGamepads no_button_data; |
| 113 no_button_data.length = 1; | 120 no_button_data.length = 1; |
| 114 no_button_data.items[0].connected = true; | 121 no_button_data.items[0].connected = true; |
| 115 no_button_data.items[0].timestamp = 0; | 122 no_button_data.items[0].timestamp = 0; |
| 116 no_button_data.items[0].buttonsLength = 1; | 123 no_button_data.items[0].buttonsLength = 1; |
| 117 no_button_data.items[0].axesLength = 2; | 124 no_button_data.items[0].axesLength = 2; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 | 191 |
| 185 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
| 186 | 193 |
| 187 mock_data_fetcher_->WaitForDataRead(); | 194 mock_data_fetcher_->WaitForDataRead(); |
| 188 | 195 |
| 189 // Renderer-side, pull data out of poll buffer. | 196 // Renderer-side, pull data out of poll buffer. |
| 190 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 197 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 191 base::GetCurrentProcessHandle()); | 198 base::GetCurrentProcessHandle()); |
| 192 std::unique_ptr<base::SharedMemory> shared_memory( | 199 std::unique_ptr<base::SharedMemory> shared_memory( |
| 193 new base::SharedMemory(handle, true)); | 200 new base::SharedMemory(handle, true)); |
| 194 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); | 201 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 195 void* mem = shared_memory->memory(); | |
| 196 | 202 |
| 197 WebGamepads* output = static_cast<WebGamepads*>(mem); | 203 WebGamepads output; |
| 204 memset(&output, 0, sizeof(WebGamepads)); | |
| 205 GamepadHardwareBuffer* buffer = | |
| 206 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); | |
| 207 base::subtle::Atomic32 version; | |
| 208 do { | |
| 209 version = buffer->seqlock.ReadBegin(); | |
| 210 memcpy(&output, &buffer->data, sizeof(WebGamepads)); | |
| 211 } while (buffer->seqlock.ReadRetry(version)); | |
| 198 | 212 |
| 199 // Initial data should all be zeroed out due to sanitization, even though the | 213 // Initial data should all be zeroed out due to sanitization, even though the |
| 200 // gamepad reported input | 214 // gamepad reported input |
| 201 EXPECT_EQ(1u, output->length); | 215 EXPECT_EQ(1u, output.length); |
| 202 EXPECT_EQ(1u, output->items[0].buttonsLength); | 216 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 203 EXPECT_EQ(0.f, output->items[0].buttons[0].value); | 217 EXPECT_EQ(0.f, output.items[0].buttons[0].value); |
| 204 EXPECT_FALSE(output->items[0].buttons[0].pressed); | 218 EXPECT_FALSE(output.items[0].buttons[0].pressed); |
| 205 EXPECT_EQ(1u, output->items[0].axesLength); | 219 EXPECT_EQ(1u, output.items[0].axesLength); |
| 206 EXPECT_EQ(0.f, output->items[0].axes[0]); | 220 EXPECT_EQ(0.f, output.items[0].axes[0]); |
| 207 | 221 |
| 208 // Zero out the inputs | 222 // Zero out the inputs |
| 209 mock_data_fetcher_->SetTestData(zero_data); | 223 mock_data_fetcher_->SetTestData(zero_data); |
| 210 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 224 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 211 | 225 |
| 226 // Read updated data from shared memory | |
| 227 memset(&output, 0, sizeof(WebGamepads)); | |
| 228 do { | |
| 229 version = buffer->seqlock.ReadBegin(); | |
| 230 memcpy(&output, &buffer->data, sizeof(WebGamepads)); | |
| 231 } while (buffer->seqlock.ReadRetry(version)); | |
| 232 | |
| 212 // Should still read zero, which is now an accurate reflection of the data | 233 // Should still read zero, which is now an accurate reflection of the data |
| 213 EXPECT_EQ(1u, output->length); | 234 EXPECT_EQ(1u, output.length); |
| 214 EXPECT_EQ(1u, output->items[0].buttonsLength); | 235 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 215 EXPECT_EQ(0.f, output->items[0].buttons[0].value); | 236 EXPECT_EQ(0.f, output.items[0].buttons[0].value); |
| 216 EXPECT_FALSE(output->items[0].buttons[0].pressed); | 237 EXPECT_FALSE(output.items[0].buttons[0].pressed); |
| 217 EXPECT_EQ(1u, output->items[0].axesLength); | 238 EXPECT_EQ(1u, output.items[0].axesLength); |
| 218 EXPECT_EQ(0.f, output->items[0].axes[0]); | 239 EXPECT_EQ(0.f, output.items[0].axes[0]); |
| 219 | 240 |
| 220 // Re-set the active inputs | 241 // Re-set the active inputs |
| 221 mock_data_fetcher_->SetTestData(active_data); | 242 mock_data_fetcher_->SetTestData(active_data); |
| 222 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 243 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 223 | 244 |
| 245 // Read updated data from shared memory | |
| 246 memset(&output, 0, sizeof(WebGamepads)); | |
| 247 do { | |
| 248 version = buffer->seqlock.ReadBegin(); | |
| 249 memcpy(&output, &buffer->data, sizeof(WebGamepads)); | |
| 250 } while (buffer->seqlock.ReadRetry(version)); | |
| 251 | |
| 224 // Should now accurately reflect the reported data. | 252 // Should now accurately reflect the reported data. |
| 225 EXPECT_EQ(1u, output->length); | 253 EXPECT_EQ(1u, output.length); |
| 226 EXPECT_EQ(1u, output->items[0].buttonsLength); | 254 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 227 EXPECT_EQ(1.f, output->items[0].buttons[0].value); | 255 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
| 228 EXPECT_TRUE(output->items[0].buttons[0].pressed); | 256 EXPECT_TRUE(output.items[0].buttons[0].pressed); |
| 229 EXPECT_EQ(1u, output->items[0].axesLength); | 257 EXPECT_EQ(1u, output.items[0].axesLength); |
| 230 EXPECT_EQ(-1.f, output->items[0].axes[0]); | 258 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 231 } | 259 } |
| 232 | 260 |
| 233 } // namespace | 261 } // namespace |
| 234 | 262 |
| 235 } // namespace device | 263 } // namespace device |
| OLD | NEW |