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 |
| 52 void ReadGamepadHardwareBuffer(GamepadHardwareBuffer* buffer, |
| 53 WebGamepads* output) { |
| 54 memset(output, 0, sizeof(WebGamepads)); |
| 55 base::subtle::Atomic32 version; |
| 56 do { |
| 57 version = buffer->seqlock.ReadBegin(); |
| 58 memcpy(output, &buffer->data, sizeof(WebGamepads)); |
| 59 } while (buffer->seqlock.ReadRetry(version)); |
| 60 } |
| 61 |
53 protected: | 62 protected: |
54 GamepadProviderTest() {} | 63 GamepadProviderTest() {} |
55 | 64 |
56 std::unique_ptr<GamepadProvider> provider_; | 65 std::unique_ptr<GamepadProvider> provider_; |
57 | 66 |
58 // Pointer owned by the provider. | 67 // Pointer owned by the provider. |
59 MockGamepadDataFetcher* mock_data_fetcher_; | 68 MockGamepadDataFetcher* mock_data_fetcher_; |
60 | 69 |
61 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); | 70 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); |
62 }; | 71 }; |
63 | 72 |
64 // Crashes. http://crbug.com/106163 | 73 // Crashes. http://crbug.com/106163 |
65 // crbug.com/147549 | 74 // crbug.com/147549 |
66 #if defined(OS_ANDROID) | 75 #if defined(OS_ANDROID) |
67 #define MAYBE_PollingAccess DISABLED_PollingAccess | 76 #define MAYBE_PollingAccess DISABLED_PollingAccess |
68 #else | 77 #else |
69 #define MAYBE_PollingAccess PollingAccess | 78 #define MAYBE_PollingAccess PollingAccess |
70 #endif | 79 #endif |
71 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { | 80 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { |
72 WebGamepads test_data; | 81 WebGamepads test_data; |
| 82 memset(&test_data, 0, sizeof(WebGamepads)); |
73 test_data.length = 1; | 83 test_data.length = 1; |
74 test_data.items[0].connected = true; | 84 test_data.items[0].connected = true; |
75 test_data.items[0].timestamp = 0; | 85 test_data.items[0].timestamp = 0; |
76 test_data.items[0].buttonsLength = 1; | 86 test_data.items[0].buttonsLength = 1; |
77 test_data.items[0].axesLength = 2; | 87 test_data.items[0].axesLength = 2; |
78 test_data.items[0].buttons[0].value = 1.f; | 88 test_data.items[0].buttons[0].value = 1.f; |
79 test_data.items[0].buttons[0].pressed = true; | 89 test_data.items[0].buttons[0].pressed = true; |
80 test_data.items[0].axes[0] = -1.f; | 90 test_data.items[0].axes[0] = -1.f; |
81 test_data.items[0].axes[1] = .5f; | 91 test_data.items[0].axes[1] = .5f; |
82 | 92 |
83 GamepadProvider* provider = CreateProvider(test_data); | 93 GamepadProvider* provider = CreateProvider(test_data); |
84 provider->SetSanitizationEnabled(false); | 94 provider->SetSanitizationEnabled(false); |
85 provider->Resume(); | 95 provider->Resume(); |
86 | 96 |
87 base::RunLoop().RunUntilIdle(); | 97 base::RunLoop().RunUntilIdle(); |
88 | 98 |
89 mock_data_fetcher_->WaitForDataRead(); | 99 mock_data_fetcher_->WaitForDataRead(); |
90 | 100 |
91 // Renderer-side, pull data out of poll buffer. | 101 // Renderer-side, pull data out of poll buffer. |
92 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 102 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
93 base::GetCurrentProcessHandle()); | 103 base::GetCurrentProcessHandle()); |
94 std::unique_ptr<base::SharedMemory> shared_memory( | 104 std::unique_ptr<base::SharedMemory> shared_memory( |
95 new base::SharedMemory(handle, true)); | 105 new base::SharedMemory(handle, true)); |
96 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); | 106 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
97 void* mem = shared_memory->memory(); | |
98 | 107 |
99 WebGamepads* output = static_cast<WebGamepads*>(mem); | 108 GamepadHardwareBuffer* buffer = |
| 109 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); |
| 110 WebGamepads output; |
| 111 ReadGamepadHardwareBuffer(buffer, &output); |
100 | 112 |
101 EXPECT_EQ(1u, output->length); | 113 EXPECT_EQ(1u, output.length); |
102 EXPECT_EQ(1u, output->items[0].buttonsLength); | 114 EXPECT_EQ(1u, output.items[0].buttonsLength); |
103 EXPECT_EQ(1.f, output->items[0].buttons[0].value); | 115 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
104 EXPECT_EQ(true, output->items[0].buttons[0].pressed); | 116 EXPECT_EQ(true, output.items[0].buttons[0].pressed); |
105 EXPECT_EQ(2u, output->items[0].axesLength); | 117 EXPECT_EQ(2u, output.items[0].axesLength); |
106 EXPECT_EQ(-1.f, output->items[0].axes[0]); | 118 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
107 EXPECT_EQ(0.5f, output->items[0].axes[1]); | 119 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
108 } | 120 } |
109 | 121 |
110 // Tests that waiting for a user gesture works properly. | 122 // Tests that waiting for a user gesture works properly. |
111 TEST_F(GamepadProviderTest, UserGesture) { | 123 TEST_F(GamepadProviderTest, UserGesture) { |
112 WebGamepads no_button_data; | 124 WebGamepads no_button_data; |
113 no_button_data.length = 1; | 125 no_button_data.length = 1; |
114 no_button_data.items[0].connected = true; | 126 no_button_data.items[0].connected = true; |
115 no_button_data.items[0].timestamp = 0; | 127 no_button_data.items[0].timestamp = 0; |
116 no_button_data.items[0].buttonsLength = 1; | 128 no_button_data.items[0].buttonsLength = 1; |
117 no_button_data.items[0].axesLength = 2; | 129 no_button_data.items[0].axesLength = 2; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 196 |
185 base::RunLoop().RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
186 | 198 |
187 mock_data_fetcher_->WaitForDataRead(); | 199 mock_data_fetcher_->WaitForDataRead(); |
188 | 200 |
189 // Renderer-side, pull data out of poll buffer. | 201 // Renderer-side, pull data out of poll buffer. |
190 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 202 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
191 base::GetCurrentProcessHandle()); | 203 base::GetCurrentProcessHandle()); |
192 std::unique_ptr<base::SharedMemory> shared_memory( | 204 std::unique_ptr<base::SharedMemory> shared_memory( |
193 new base::SharedMemory(handle, true)); | 205 new base::SharedMemory(handle, true)); |
194 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); | 206 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
195 void* mem = shared_memory->memory(); | |
196 | 207 |
197 WebGamepads* output = static_cast<WebGamepads*>(mem); | 208 GamepadHardwareBuffer* buffer = |
| 209 static_cast<GamepadHardwareBuffer*>(shared_memory->memory()); |
| 210 WebGamepads output; |
| 211 ReadGamepadHardwareBuffer(buffer, &output); |
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 ReadGamepadHardwareBuffer(buffer, &output); |
| 228 |
212 // Should still read zero, which is now an accurate reflection of the data | 229 // Should still read zero, which is now an accurate reflection of the data |
213 EXPECT_EQ(1u, output->length); | 230 EXPECT_EQ(1u, output.length); |
214 EXPECT_EQ(1u, output->items[0].buttonsLength); | 231 EXPECT_EQ(1u, output.items[0].buttonsLength); |
215 EXPECT_EQ(0.f, output->items[0].buttons[0].value); | 232 EXPECT_EQ(0.f, output.items[0].buttons[0].value); |
216 EXPECT_FALSE(output->items[0].buttons[0].pressed); | 233 EXPECT_FALSE(output.items[0].buttons[0].pressed); |
217 EXPECT_EQ(1u, output->items[0].axesLength); | 234 EXPECT_EQ(1u, output.items[0].axesLength); |
218 EXPECT_EQ(0.f, output->items[0].axes[0]); | 235 EXPECT_EQ(0.f, output.items[0].axes[0]); |
219 | 236 |
220 // Re-set the active inputs | 237 // Re-set the active inputs |
221 mock_data_fetcher_->SetTestData(active_data); | 238 mock_data_fetcher_->SetTestData(active_data); |
222 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 239 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
223 | 240 |
| 241 // Read updated data from shared memory |
| 242 ReadGamepadHardwareBuffer(buffer, &output); |
| 243 |
224 // Should now accurately reflect the reported data. | 244 // Should now accurately reflect the reported data. |
225 EXPECT_EQ(1u, output->length); | 245 EXPECT_EQ(1u, output.length); |
226 EXPECT_EQ(1u, output->items[0].buttonsLength); | 246 EXPECT_EQ(1u, output.items[0].buttonsLength); |
227 EXPECT_EQ(1.f, output->items[0].buttons[0].value); | 247 EXPECT_EQ(1.f, output.items[0].buttons[0].value); |
228 EXPECT_TRUE(output->items[0].buttons[0].pressed); | 248 EXPECT_TRUE(output.items[0].buttons[0].pressed); |
229 EXPECT_EQ(1u, output->items[0].axesLength); | 249 EXPECT_EQ(1u, output.items[0].axesLength); |
230 EXPECT_EQ(-1.f, output->items[0].axes[0]); | 250 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
231 } | 251 } |
232 | 252 |
233 } // namespace | 253 } // namespace |
234 | 254 |
235 } // namespace device | 255 } // namespace device |
OLD | NEW |