| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/threading/platform_thread.h" |
| 9 #include "content/browser/gamepad/data_fetcher.h" | 10 #include "content/browser/gamepad/data_fetcher.h" |
| 10 #include "content/browser/gamepad/gamepad_provider.h" | 11 #include "content/browser/gamepad/gamepad_provider.h" |
| 11 #include "content/common/gamepad_hardware_buffer.h" | 12 #include "content/common/gamepad_hardware_buffer.h" |
| 12 #include "content/common/gamepad_messages.h" | 13 #include "content/common/gamepad_messages.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 using WebKit::WebGamepads; | 21 using WebKit::WebGamepads; |
| 21 | 22 |
| 22 class MockDataFetcher : public GamepadDataFetcher { | 23 class MockDataFetcher : public GamepadDataFetcher { |
| 23 public: | 24 public: |
| 24 explicit MockDataFetcher(const WebGamepads& test_data) | 25 explicit MockDataFetcher(const WebGamepads& test_data) |
| 25 : test_data_(test_data), | 26 : test_data_(test_data) { |
| 26 read_data_(false, false) { | |
| 27 } | 27 } |
| 28 virtual void GetGamepadData(WebGamepads* pads, | 28 virtual void GetGamepadData(WebGamepads* pads, |
| 29 bool devices_changed_hint) OVERRIDE { | 29 bool devices_changed_hint) OVERRIDE { |
| 30 *pads = test_data_; | 30 *pads = test_data_; |
| 31 read_data_.Signal(); | |
| 32 } | 31 } |
| 33 | 32 |
| 34 void WaitForDataRead() { return read_data_.Wait(); } | |
| 35 | |
| 36 WebGamepads test_data_; | 33 WebGamepads test_data_; |
| 37 base::WaitableEvent read_data_; | |
| 38 }; | 34 }; |
| 39 | 35 |
| 40 // Main test fixture | 36 // Main test fixture |
| 41 class GamepadProviderTest : public testing::Test { | 37 class GamepadProviderTest : public testing::Test { |
| 42 public: | 38 public: |
| 43 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | 39 GamepadProvider* CreateProvider(const WebGamepads& test_data) { |
| 44 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
| 45 base::SystemMonitor::AllocateSystemIOPorts(); | 41 base::SystemMonitor::AllocateSystemIOPorts(); |
| 46 #endif | 42 #endif |
| 47 system_monitor_.reset(new base::SystemMonitor); | 43 system_monitor_.reset(new base::SystemMonitor); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 70 test_data.items[0].buttonsLength = 1; | 66 test_data.items[0].buttonsLength = 1; |
| 71 test_data.items[0].axesLength = 2; | 67 test_data.items[0].axesLength = 2; |
| 72 test_data.items[0].buttons[0] = 1.f; | 68 test_data.items[0].buttons[0] = 1.f; |
| 73 test_data.items[0].axes[0] = -1.f; | 69 test_data.items[0].axes[0] = -1.f; |
| 74 test_data.items[0].axes[1] = .5f; | 70 test_data.items[0].axes[1] = .5f; |
| 75 | 71 |
| 76 GamepadProvider* provider = CreateProvider(test_data); | 72 GamepadProvider* provider = CreateProvider(test_data); |
| 77 | 73 |
| 78 main_message_loop_.RunAllPending(); | 74 main_message_loop_.RunAllPending(); |
| 79 | 75 |
| 80 mock_data_fetcher_->WaitForDataRead(); | |
| 81 | |
| 82 // Renderer-side, pull data out of poll buffer. | 76 // Renderer-side, pull data out of poll buffer. |
| 83 base::SharedMemoryHandle handle = | 77 base::SharedMemoryHandle handle = |
| 84 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | 78 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); |
| 85 scoped_ptr<base::SharedMemory> shared_memory( | 79 scoped_ptr<base::SharedMemory> shared_memory( |
| 86 new base::SharedMemory(handle, true)); | 80 new base::SharedMemory(handle, true)); |
| 87 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 81 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 88 void* mem = shared_memory->memory(); | 82 void* mem = shared_memory->memory(); |
| 89 | 83 |
| 90 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 84 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
| 91 // See gamepad_hardware_buffer.h for details on the read discipline. | |
| 92 WebGamepads output; | 85 WebGamepads output; |
| 93 | 86 output.length = 0; |
| 94 base::subtle::Atomic32 version; | 87 for (;;) { |
| 95 do { | 88 hwbuf->gamepads.ReadTo(&output); |
| 96 version = hwbuf->sequence.ReadBegin(); | 89 if (output.length == 1) |
| 97 memcpy(&output, &hwbuf->buffer, sizeof(output)); | 90 break; |
| 98 } while (hwbuf->sequence.ReadRetry(version)); | 91 base::PlatformThread::YieldCurrentThread(); |
| 99 | 92 } |
| 100 EXPECT_EQ(1u, output.length); | 93 EXPECT_EQ(1u, output.length); |
| 101 EXPECT_EQ(1u, output.items[0].buttonsLength); | 94 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 102 EXPECT_EQ(1.f, output.items[0].buttons[0]); | 95 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
| 103 EXPECT_EQ(2u, output.items[0].axesLength); | 96 EXPECT_EQ(2u, output.items[0].axesLength); |
| 104 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 97 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 105 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 98 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 106 } | 99 } |
| 107 | 100 |
| 108 } // namespace | 101 } // namespace |
| 109 | 102 |
| 110 } // namespace content | 103 } // namespace content |
| OLD | NEW |