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