OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/gamepad/gamepad_provider.h" | 9 #include "content/browser/gamepad/gamepad_provider.h" |
10 #include "content/common/gamepad_messages.h" | 10 #include "content/common/gamepad_messages.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using WebKit::WebGamepads; | |
14 using content::GamepadProvider; | |
15 using content::GamepadHardwareBuffer; | |
16 | |
13 namespace { | 17 namespace { |
14 | 18 |
15 class MockDataFetcher : public gamepad::DataFetcher { | 19 class MockDataFetcher : public content::GamepadDataFetcher { |
darin (slow to review)
2011/11/30 21:06:22
I think it is also OK for all of the stuff in this
scottmg
2011/11/30 21:22:39
Done.
| |
16 public: | 20 public: |
17 MockDataFetcher() : read_data_(false, false) { | 21 MockDataFetcher(WebGamepads& test_data) : read_data_(false, false) { |
18 memset(&test_data, 0, sizeof(test_data)); | 22 test_data_ = test_data; |
19 } | 23 } |
20 virtual void GetGamepadData(WebKit::WebGamepads* pads, | 24 virtual void GetGamepadData(WebGamepads* pads, |
21 bool devices_changed_hint) OVERRIDE { | 25 bool devices_changed_hint) OVERRIDE { |
22 *pads = test_data; | 26 *pads = test_data_; |
23 read_data_.Signal(); | 27 read_data_.Signal(); |
24 } | 28 } |
25 | 29 |
26 void SetData(WebKit::WebGamepads& data) { | |
27 test_data = data; | |
28 } | |
29 | |
30 void WaitForDataRead() { return read_data_.Wait(); } | 30 void WaitForDataRead() { return read_data_.Wait(); } |
31 | 31 |
32 WebKit::WebGamepads test_data; | 32 WebGamepads test_data_; |
33 base::WaitableEvent read_data_; | 33 base::WaitableEvent read_data_; |
34 }; | 34 }; |
35 | 35 |
36 // Main test fixture | 36 // Main test fixture |
37 class GamepadProviderTest : public testing::Test { | 37 class GamepadProviderTest : public testing::Test { |
38 public: | 38 public: |
39 gamepad::Provider* CreateProvider() { | 39 GamepadProvider* CreateProvider(WebGamepads& test_data) { |
40 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
41 base::SystemMonitor::AllocateSystemIOPorts(); | 41 base::SystemMonitor::AllocateSystemIOPorts(); |
42 #endif | 42 #endif |
43 system_monitor_.reset(new base::SystemMonitor); | 43 system_monitor_.reset(new base::SystemMonitor); |
44 mock_data_fetcher_ = new MockDataFetcher; | 44 mock_data_fetcher_ = new MockDataFetcher(test_data); |
45 provider_ = new gamepad::Provider(mock_data_fetcher_); | 45 provider_ = new GamepadProvider(mock_data_fetcher_); |
46 return provider_.get(); | 46 return provider_.get(); |
47 } | 47 } |
48 | 48 |
49 protected: | 49 protected: |
50 GamepadProviderTest() { | 50 GamepadProviderTest() { |
51 } | 51 } |
52 | 52 |
53 MessageLoop main_message_loop_; | 53 MessageLoop main_message_loop_; |
54 scoped_ptr<base::SystemMonitor> system_monitor_; | 54 scoped_ptr<base::SystemMonitor> system_monitor_; |
55 MockDataFetcher* mock_data_fetcher_; | 55 MockDataFetcher* mock_data_fetcher_; |
56 scoped_refptr<gamepad::Provider> provider_; | 56 scoped_refptr<GamepadProvider> provider_; |
57 }; | 57 }; |
58 | 58 |
59 TEST_F(GamepadProviderTest, BasicStartStop) { | 59 TEST_F(GamepadProviderTest, BasicStartStop) { |
60 gamepad::Provider* provider = CreateProvider(); | 60 WebGamepads test_data; |
61 memset(&test_data, 0, sizeof(test_data)); | |
62 GamepadProvider* provider = CreateProvider(test_data); | |
61 provider->Start(); | 63 provider->Start(); |
62 provider->Stop(); | 64 provider->Stop(); |
63 // Just ensure that there's no asserts on startup, shutdown, or destroy. | 65 // Just ensure that there's no asserts on startup, shutdown, or destroy. |
64 } | 66 } |
65 | 67 |
66 // http://crbug.com/105348 | 68 TEST_F(GamepadProviderTest, PollingAccess) { |
67 TEST_F(GamepadProviderTest, FLAKY_PollingAccess) { | 69 WebGamepads test_data; |
68 using namespace gamepad; | |
69 | |
70 Provider* provider = CreateProvider(); | |
71 provider->Start(); | |
72 | |
73 WebKit::WebGamepads test_data; | |
74 test_data.length = 1; | 70 test_data.length = 1; |
75 test_data.items[0].connected = true; | 71 test_data.items[0].connected = true; |
76 test_data.items[0].timestamp = 0; | 72 test_data.items[0].timestamp = 0; |
77 test_data.items[0].buttonsLength = 1; | 73 test_data.items[0].buttonsLength = 1; |
78 test_data.items[0].axesLength = 2; | 74 test_data.items[0].axesLength = 2; |
79 test_data.items[0].buttons[0] = 1.f; | 75 test_data.items[0].buttons[0] = 1.f; |
80 test_data.items[0].axes[0] = -1.f; | 76 test_data.items[0].axes[0] = -1.f; |
81 test_data.items[0].axes[1] = .5f; | 77 test_data.items[0].axes[1] = .5f; |
82 mock_data_fetcher_->SetData(test_data); | 78 |
79 GamepadProvider* provider = CreateProvider(test_data); | |
80 provider->Start(); | |
83 | 81 |
84 main_message_loop_.RunAllPending(); | 82 main_message_loop_.RunAllPending(); |
85 | 83 |
86 mock_data_fetcher_->WaitForDataRead(); | 84 mock_data_fetcher_->WaitForDataRead(); |
87 | 85 |
88 // Renderer-side, pull data out of poll buffer. | 86 // Renderer-side, pull data out of poll buffer. |
89 base::SharedMemoryHandle handle = | 87 base::SharedMemoryHandle handle = |
90 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | 88 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); |
91 scoped_ptr<base::SharedMemory> shared_memory( | 89 scoped_ptr<base::SharedMemory> shared_memory( |
92 new base::SharedMemory(handle, true)); | 90 new base::SharedMemory(handle, true)); |
93 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 91 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
94 void* mem = shared_memory->memory(); | 92 void* mem = shared_memory->memory(); |
95 | 93 |
96 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 94 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
97 // See gamepad_hardware_buffer.h for details on the read discipline. | 95 // See gamepad_hardware_buffer.h for details on the read discipline. |
98 base::subtle::Atomic32 start, end; | 96 WebGamepads output; |
99 WebKit::WebGamepads output; | |
100 int contention_count; | |
101 | 97 |
102 // Here we're attempting to test the read discipline during contention. If | 98 base::subtle::Atomic32 version; |
103 // we fail to read this many times, then the read thread is starving, and we | 99 do { |
104 // should fail the test. | 100 version = hwbuf->sequence.ReadBegin(); |
105 for (contention_count = 0; contention_count < 1000; ++contention_count) { | |
106 end = base::subtle::Acquire_Load(&hwbuf->end_marker); | |
107 memcpy(&output, &hwbuf->buffer, sizeof(output)); | 101 memcpy(&output, &hwbuf->buffer, sizeof(output)); |
108 start = base::subtle::Acquire_Load(&hwbuf->start_marker); | 102 } while (hwbuf->sequence.ReadRetry(version)); |
109 if (start == end) | 103 |
110 break; | |
111 base::PlatformThread::YieldCurrentThread(); | |
112 } | |
113 EXPECT_GT(1000, contention_count); | |
114 EXPECT_EQ(1u, output.length); | 104 EXPECT_EQ(1u, output.length); |
115 EXPECT_EQ(1u, output.items[0].buttonsLength); | 105 EXPECT_EQ(1u, output.items[0].buttonsLength); |
116 EXPECT_EQ(1.f, output.items[0].buttons[0]); | 106 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
117 EXPECT_EQ(2u, output.items[0].axesLength); | 107 EXPECT_EQ(2u, output.items[0].axesLength); |
118 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 108 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
119 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 109 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
120 | 110 |
121 provider->Stop(); | 111 provider->Stop(); |
122 } | 112 } |
123 | 113 |
124 } // namespace | 114 } // namespace |
OLD | NEW |