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/system_monitor/system_monitor.h" | |
8 #include "content/browser/gamepad/gamepad_provider.h" | |
9 #include "content/common/gamepad_messages.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace { | |
13 | |
14 class MockDataFetcher : public gamepad::DataFetcher { | |
15 public: | |
16 MockDataFetcher() : read_data_(false) { | |
17 memset(&test_data, 0, sizeof(test_data)); | |
18 } | |
19 virtual void GetGamepadData(WebKit::WebGamepads* pads, | |
20 bool devices_changed_hint) OVERRIDE { | |
21 *pads = test_data; | |
22 read_data_ = true; | |
23 } | |
24 | |
25 void SetData(WebKit::WebGamepads& data) { | |
26 test_data = data; | |
27 read_data_ = false; | |
28 } | |
29 | |
30 bool ReadData() const { return read_data_; } | |
31 | |
32 WebKit::WebGamepads test_data; | |
33 bool 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 while (!mock_data_fetcher_->ReadData()) { | |
86 base::PlatformThread::Sleep(10); | |
Paweł Hajdan Jr.
2011/11/18 11:59:07
Sorry, this is still bad. Please wait for en _even
| |
87 } | |
88 | |
89 // Renderer-side, pull data out of poll buffer. | |
90 base::SharedMemoryHandle handle = | |
91 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | |
92 base::SharedMemory* shared_memory = new base::SharedMemory(handle, true); | |
93 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
94 void* mem = shared_memory->memory(); | |
95 | |
96 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
97 // See gamepad_hardware_buffer.h for details on the read discipline. | |
98 base::subtle::Atomic32 start, end; | |
99 int contention_count = 0; | |
100 WebKit::WebGamepads output; | |
101 for (;;) { | |
102 end = base::subtle::Acquire_Load(&hwbuf->end_marker); | |
103 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
104 start = base::subtle::Acquire_Load(&hwbuf->start_marker); | |
105 if (start == end) break; | |
106 ++contention_count; | |
107 base::PlatformThread::YieldCurrentThread(); | |
108 EXPECT_LT(10, contention_count); | |
Paweł Hajdan Jr.
2011/11/18 11:59:07
This will still make the test hang if you're unluc
| |
109 } | |
110 | |
111 EXPECT_EQ(1u, output.length); | |
112 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
113 EXPECT_EQ(1.f, output.items[0].buttons[0]); | |
114 EXPECT_EQ(2u, output.items[0].axesLength); | |
115 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
116 EXPECT_EQ(0.5f, output.items[0].axes[1]); | |
117 | |
118 provider->Stop(); | |
119 } | |
120 | |
121 } // namespace | |
OLD | NEW |