OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/scoped_ptr.h" | |
6 #include "base/process_util.h" | |
7 #include "base/synchronization/waitable_event.h" | |
8 #include "base/system_monitor/system_monitor.h" | |
9 #include "content/browser/gamepad/gamepad_provider.h" | |
10 #include "content/common/gamepad_messages.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace { | |
14 | |
15 class MockDataFetcher : public gamepad::DataFetcher { | |
16 public: | |
17 MockDataFetcher() : read_data_(false, false) { | |
18 memset(&test_data, 0, sizeof(test_data)); | |
19 } | |
20 virtual void GetGamepadData(WebKit::WebGamepads* pads, | |
21 bool devices_changed_hint) OVERRIDE { | |
22 *pads = test_data; | |
23 read_data_.Signal(); | |
24 } | |
25 | |
26 void SetData(WebKit::WebGamepads& data) { | |
27 test_data = data; | |
28 } | |
29 | |
30 void WaitForDataRead() { return read_data_.Wait(); } | |
31 | |
32 WebKit::WebGamepads test_data; | |
33 base::WaitableEvent read_data_; | |
34 }; | |
35 | |
36 // Main test fixture | |
37 class GamepadProviderTest : public testing::Test { | |
38 public: | |
39 gamepad::Provider* CreateProvider() { | |
40 #if defined(OS_MACOSX) | |
41 base::SystemMonitor::AllocateSystemIOPorts(); | |
42 #endif | |
43 system_monitor_.reset(new base::SystemMonitor); | |
44 mock_data_fetcher_ = new MockDataFetcher; | |
45 provider_ = new gamepad::Provider(mock_data_fetcher_); | |
46 return provider_.get(); | |
47 } | |
48 | |
49 protected: | |
50 GamepadProviderTest() { | |
51 } | |
52 | |
53 MessageLoop main_message_loop_; | |
54 scoped_ptr<base::SystemMonitor> system_monitor_; | |
55 MockDataFetcher* mock_data_fetcher_; | |
56 scoped_refptr<gamepad::Provider> provider_; | |
57 }; | |
58 | |
59 TEST_F(GamepadProviderTest, BasicStartStop) { | |
60 gamepad::Provider* provider = CreateProvider(); | |
61 provider->Start(); | |
62 provider->Stop(); | |
63 // Just ensure that there's no asserts on startup, shutdown, or destroy. | |
64 } | |
65 | |
66 TEST_F(GamepadProviderTest, PollingAccess) { | |
67 using namespace gamepad; | |
68 | |
69 Provider* provider = CreateProvider(); | |
70 provider->Start(); | |
71 | |
72 WebKit::WebGamepads test_data; | |
73 test_data.length = 1; | |
74 test_data.items[0].connected = true; | |
75 test_data.items[0].timestamp = 0; | |
76 test_data.items[0].buttonsLength = 1; | |
77 test_data.items[0].axesLength = 2; | |
78 test_data.items[0].buttons[0] = 1.f; | |
79 test_data.items[0].axes[0] = -1.f; | |
80 test_data.items[0].axes[1] = .5f; | |
81 mock_data_fetcher_->SetData(test_data); | |
82 | |
83 main_message_loop_.RunAllPending(); | |
84 | |
85 mock_data_fetcher_->WaitForDataRead(); | |
86 | |
87 // Renderer-side, pull data out of poll buffer. | |
88 base::SharedMemoryHandle handle = | |
89 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | |
90 base::SharedMemory* shared_memory = new base::SharedMemory(handle, true); | |
91 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
92 void* mem = shared_memory->memory(); | |
93 | |
94 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
95 // See gamepad_hardware_buffer.h for details on the read discipline. | |
96 base::subtle::Atomic32 start, end; | |
97 WebKit::WebGamepads output; | |
98 int contention_count; | |
99 for (contention_count = 0; contention_count < 10; ++contention_count) { | |
Paweł Hajdan Jr.
2011/11/21 12:09:37
Please add a comment here, summarizing main points
| |
100 end = base::subtle::Acquire_Load(&hwbuf->end_marker); | |
101 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
102 start = base::subtle::Acquire_Load(&hwbuf->start_marker); | |
103 if (start == end) break; | |
Paweł Hajdan Jr.
2011/11/21 12:09:37
nit: Don't cram two statements in one line.
| |
104 base::PlatformThread::YieldCurrentThread(); | |
105 } | |
106 EXPECT_GT(10, contention_count); | |
107 EXPECT_EQ(1u, output.length); | |
108 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
109 EXPECT_EQ(1.f, output.items[0].buttons[0]); | |
110 EXPECT_EQ(2u, output.items[0].axesLength); | |
111 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
112 EXPECT_EQ(0.5f, output.items[0].axes[1]); | |
113 | |
114 provider->Stop(); | |
115 } | |
116 | |
117 } // namespace | |
OLD | NEW |