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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "base/system_monitor/system_monitor.h" | 8 #include "base/system_monitor/system_monitor.h" |
9 #include "base/threading/platform_thread.h" | |
10 #include "content/browser/gamepad/data_fetcher.h" | 9 #include "content/browser/gamepad/data_fetcher.h" |
11 #include "content/browser/gamepad/gamepad_provider.h" | 10 #include "content/browser/gamepad/gamepad_provider.h" |
12 #include "content/common/gamepad_hardware_buffer.h" | 11 #include "content/common/gamepad_hardware_buffer.h" |
13 #include "content/common/gamepad_messages.h" | 12 #include "content/common/gamepad_messages.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" |
16 | 15 |
17 namespace content { | 16 namespace content { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 using WebKit::WebGamepads; | 20 using WebKit::WebGamepads; |
22 | 21 |
23 class MockDataFetcher : public GamepadDataFetcher { | 22 class MockDataFetcher : public GamepadDataFetcher { |
24 public: | 23 public: |
25 explicit MockDataFetcher(const WebGamepads& test_data) | 24 explicit MockDataFetcher(const WebGamepads& test_data) |
26 : test_data_(test_data) { | 25 : test_data_(test_data), |
| 26 read_data_(false, false) { |
27 } | 27 } |
28 virtual void GetGamepadData(WebGamepads* pads, | 28 virtual void GetGamepadData(WebGamepads* pads, |
29 bool devices_changed_hint) OVERRIDE { | 29 bool devices_changed_hint) OVERRIDE { |
30 *pads = test_data_; | 30 *pads = test_data_; |
| 31 read_data_.Signal(); |
31 } | 32 } |
32 | 33 |
| 34 void WaitForDataRead() { return read_data_.Wait(); } |
| 35 |
33 WebGamepads test_data_; | 36 WebGamepads test_data_; |
| 37 base::WaitableEvent read_data_; |
34 }; | 38 }; |
35 | 39 |
36 // Main test fixture | 40 // Main test fixture |
37 class GamepadProviderTest : public testing::Test { | 41 class GamepadProviderTest : public testing::Test { |
38 public: | 42 public: |
39 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | 43 GamepadProvider* CreateProvider(const WebGamepads& test_data) { |
40 #if defined(OS_MACOSX) | 44 #if defined(OS_MACOSX) |
41 base::SystemMonitor::AllocateSystemIOPorts(); | 45 base::SystemMonitor::AllocateSystemIOPorts(); |
42 #endif | 46 #endif |
43 system_monitor_.reset(new base::SystemMonitor); | 47 system_monitor_.reset(new base::SystemMonitor); |
(...skipping 22 matching lines...) Expand all Loading... |
66 test_data.items[0].buttonsLength = 1; | 70 test_data.items[0].buttonsLength = 1; |
67 test_data.items[0].axesLength = 2; | 71 test_data.items[0].axesLength = 2; |
68 test_data.items[0].buttons[0] = 1.f; | 72 test_data.items[0].buttons[0] = 1.f; |
69 test_data.items[0].axes[0] = -1.f; | 73 test_data.items[0].axes[0] = -1.f; |
70 test_data.items[0].axes[1] = .5f; | 74 test_data.items[0].axes[1] = .5f; |
71 | 75 |
72 GamepadProvider* provider = CreateProvider(test_data); | 76 GamepadProvider* provider = CreateProvider(test_data); |
73 | 77 |
74 main_message_loop_.RunAllPending(); | 78 main_message_loop_.RunAllPending(); |
75 | 79 |
| 80 mock_data_fetcher_->WaitForDataRead(); |
| 81 |
76 // Renderer-side, pull data out of poll buffer. | 82 // Renderer-side, pull data out of poll buffer. |
77 base::SharedMemoryHandle handle = | 83 base::SharedMemoryHandle handle = |
78 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | 84 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); |
79 scoped_ptr<base::SharedMemory> shared_memory( | 85 scoped_ptr<base::SharedMemory> shared_memory( |
80 new base::SharedMemory(handle, true)); | 86 new base::SharedMemory(handle, true)); |
81 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 87 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
82 void* mem = shared_memory->memory(); | 88 void* mem = shared_memory->memory(); |
83 | 89 |
84 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 90 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
| 91 // See gamepad_hardware_buffer.h for details on the read discipline. |
85 WebGamepads output; | 92 WebGamepads output; |
86 output.length = 0; | 93 |
87 for (;;) { | 94 base::subtle::Atomic32 version; |
88 hwbuf->gamepads.ReadTo(&output); | 95 do { |
89 if (output.length == 1) | 96 version = hwbuf->sequence.ReadBegin(); |
90 break; | 97 memcpy(&output, &hwbuf->buffer, sizeof(output)); |
91 base::PlatformThread::YieldCurrentThread(); | 98 } while (hwbuf->sequence.ReadRetry(version)); |
92 } | 99 |
93 EXPECT_EQ(1u, output.length); | 100 EXPECT_EQ(1u, output.length); |
94 EXPECT_EQ(1u, output.items[0].buttonsLength); | 101 EXPECT_EQ(1u, output.items[0].buttonsLength); |
95 EXPECT_EQ(1.f, output.items[0].buttons[0]); | 102 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
96 EXPECT_EQ(2u, output.items[0].axesLength); | 103 EXPECT_EQ(2u, output.items[0].axesLength); |
97 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 104 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
98 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 105 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
99 } | 106 } |
100 | 107 |
101 } // namespace | 108 } // namespace |
102 | 109 |
103 } // namespace content | 110 } // namespace content |
OLD | NEW |