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 scoped_ptr<base::SharedMemory> shared_memory( |
| 91 new base::SharedMemory(handle, true)); |
| 92 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 93 void* mem = shared_memory->memory(); |
| 94 |
| 95 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
| 96 // See gamepad_hardware_buffer.h for details on the read discipline. |
| 97 base::subtle::Atomic32 start, end; |
| 98 WebKit::WebGamepads output; |
| 99 int contention_count; |
| 100 |
| 101 // Here we're attempting to test the read discipline during contention. If |
| 102 // we fail to read this many times, then the read thread is starving, and we |
| 103 // should fail the test. |
| 104 for (contention_count = 0; contention_count < 10; ++contention_count) { |
| 105 end = base::subtle::Acquire_Load(&hwbuf->end_marker); |
| 106 memcpy(&output, &hwbuf->buffer, sizeof(output)); |
| 107 start = base::subtle::Acquire_Load(&hwbuf->start_marker); |
| 108 if (start == end) |
| 109 break; |
| 110 base::PlatformThread::YieldCurrentThread(); |
| 111 } |
| 112 EXPECT_GT(10, contention_count); |
| 113 EXPECT_EQ(1u, output.length); |
| 114 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 115 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
| 116 EXPECT_EQ(2u, output.items[0].axesLength); |
| 117 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 118 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 119 |
| 120 provider->Stop(); |
| 121 } |
| 122 |
| 123 } // namespace |
OLD | NEW |