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() { | |
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 } | |
23 | |
24 void SetData(WebKit::WebGamepads& data) { | |
25 test_data = data; | |
26 } | |
27 | |
28 WebKit::WebGamepads test_data; | |
29 }; | |
30 | |
31 // Main test fixture | |
32 class GamepadProviderTest : public testing::Test { | |
33 public: | |
34 gamepad::Provider* CreateProvider() { | |
35 system_monitor_.reset(new base::SystemMonitor); | |
36 mock_data_fetcher_ = new MockDataFetcher; | |
37 provider_ = new gamepad::Provider(mock_data_fetcher_); | |
38 return provider_.get(); | |
39 } | |
40 | |
41 protected: | |
42 GamepadProviderTest() { | |
43 } | |
44 | |
45 MessageLoop main_message_loop_; | |
46 scoped_ptr<base::SystemMonitor> system_monitor_; | |
47 MockDataFetcher* mock_data_fetcher_; | |
48 scoped_refptr<gamepad::Provider> provider_; | |
49 }; | |
50 | |
51 TEST_F(GamepadProviderTest, BasicStartStop) { | |
52 gamepad::Provider* provider = CreateProvider(); | |
53 provider->Start(); | |
54 provider->Stop(); | |
55 // Just ensure that there's no asserts on startup, shutdown, or destroy. | |
56 } | |
57 | |
58 TEST_F(GamepadProviderTest, PollingAccess) { | |
59 using namespace gamepad; | |
60 | |
61 Provider* provider = CreateProvider(); | |
62 provider->Start(); | |
63 | |
64 WebKit::WebGamepads test_data; | |
65 test_data.length = 1; | |
66 test_data.items[0].connected = true; | |
67 test_data.items[0].timestamp = 0; | |
68 test_data.items[0].buttonsLength = 1; | |
69 test_data.items[0].axesLength = 2; | |
70 test_data.items[0].buttons[0] = 1.f; | |
71 test_data.items[0].axes[0] = -1.f; | |
72 test_data.items[0].axes[1] = .5f; | |
73 mock_data_fetcher_->SetData(test_data); | |
74 | |
75 main_message_loop_.RunAllPending(); | |
76 base::PlatformThread::Sleep(300); | |
Paweł Hajdan Jr.
2011/11/16 08:36:35
Why? Is it inherently timing-dependent, or should
scottmg
2011/11/16 18:04:35
The background thread polls hardware every 16ms. T
Paweł Hajdan Jr.
2011/11/17 09:40:59
If it's background thread polling every 16ms, it's
| |
77 | |
jam
2011/11/16 00:24:58
extra blank line
scottmg
2011/11/16 18:04:35
Done.
| |
78 | |
79 // Renderer-side, pull data out of poll buffer. | |
80 base::SharedMemoryHandle handle = | |
81 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | |
jam
2011/11/16 00:24:58
nit: 4 spaces
scottmg
2011/11/16 18:04:35
Done.
| |
82 base::SharedMemory* shared_memory = new base::SharedMemory(handle, true); | |
83 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
84 void* mem = shared_memory->memory(); | |
85 | |
86 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
87 // See gamepad_hardware_buffer.h for details on the read discipline. | |
88 base::subtle::Atomic32 start, end; | |
89 int contention_count = 0; | |
90 WebKit::WebGamepads output; | |
91 for (;;) | |
92 { | |
jam
2011/11/16 00:24:58
nit: brace bracket on previous line
scottmg
2011/11/16 18:04:35
Done.
| |
93 end = base::subtle::Acquire_Load(&hwbuf->end_marker); | |
94 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
95 start = base::subtle::Acquire_Load(&hwbuf->start_marker); | |
96 if (start == end) break; | |
97 ++contention_count; | |
98 base::PlatformThread::YieldCurrentThread(); | |
Paweł Hajdan Jr.
2011/11/16 08:36:35
What's the purpose of this? Tests generally run si
scottmg
2011/11/16 18:04:35
That's right, the code under test creates a backgr
Paweł Hajdan Jr.
2011/11/17 09:40:59
Still, what's the purpose of this? Threads should
| |
99 } | |
100 | |
101 EXPECT_EQ(1, output.length); | |
102 EXPECT_EQ(1, output.items[0].buttonsLength); | |
103 EXPECT_EQ(1.f, output.items[0].buttons[0]); | |
104 EXPECT_EQ(2, output.items[0].axesLength); | |
105 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
106 EXPECT_EQ(0.5f, output.items[0].axes[1]); | |
107 | |
108 provider->Stop(); | |
109 } | |
110 | |
111 } // namespace | |
OLD | NEW |