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