| 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/memory/weak_ptr.h" |
| 6 #include "base/process_util.h" | 7 #include "base/process_util.h" |
| 7 #include "base/synchronization/waitable_event.h" | 8 #include "content/browser/gamepad/gamepad_data_fetcher.h" |
| 8 #include "base/system_monitor/system_monitor.h" | |
| 9 #include "content/browser/gamepad/data_fetcher.h" | |
| 10 #include "content/browser/gamepad/gamepad_provider.h" | 9 #include "content/browser/gamepad/gamepad_provider.h" |
| 10 #include "content/browser/gamepad/gamepad_test_helpers.h" |
| 11 #include "content/common/gamepad_hardware_buffer.h" | 11 #include "content/common/gamepad_hardware_buffer.h" |
| 12 #include "content/common/gamepad_messages.h" | 12 #include "content/common/gamepad_messages.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGamepads.
h" | |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 using WebKit::WebGamepads; | 19 using WebKit::WebGamepads; |
| 21 | 20 |
| 22 class MockDataFetcher : public GamepadDataFetcher { | 21 // Helper class to generate and record user gesture callbacks. |
| 22 class UserGestureListener { |
| 23 public: | 23 public: |
| 24 explicit MockDataFetcher(const WebGamepads& test_data) | 24 UserGestureListener() |
| 25 : test_data_(test_data), | 25 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 26 read_data_(false, false) { | 26 has_user_gesture_(false) { |
| 27 } | |
| 28 virtual void GetGamepadData(WebGamepads* pads, | |
| 29 bool devices_changed_hint) OVERRIDE { | |
| 30 *pads = test_data_; | |
| 31 read_data_.Signal(); | |
| 32 } | 27 } |
| 33 | 28 |
| 34 void WaitForDataRead() { return read_data_.Wait(); } | 29 base::Closure GetClosure() { |
| 30 return base::Bind(&UserGestureListener::GotUserGesture, |
| 31 weak_factory_.GetWeakPtr()); |
| 32 } |
| 35 | 33 |
| 36 WebGamepads test_data_; | 34 bool has_user_gesture() const { return has_user_gesture_; } |
| 37 base::WaitableEvent read_data_; | 35 |
| 36 private: |
| 37 void GotUserGesture() { |
| 38 has_user_gesture_ = true; |
| 39 } |
| 40 |
| 41 base::WeakPtrFactory<UserGestureListener> weak_factory_; |
| 42 bool has_user_gesture_; |
| 38 }; | 43 }; |
| 39 | 44 |
| 40 // Main test fixture | 45 // Main test fixture |
| 41 class GamepadProviderTest : public testing::Test { | 46 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { |
| 42 public: | 47 public: |
| 43 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | 48 GamepadProvider* CreateProvider(const WebGamepads& test_data) { |
| 44 #if defined(OS_MACOSX) | 49 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); |
| 45 base::SystemMonitor::AllocateSystemIOPorts(); | 50 provider_.reset(new GamepadProvider( |
| 46 #endif | 51 scoped_ptr<GamepadDataFetcher>(mock_data_fetcher_))); |
| 47 system_monitor_.reset(new base::SystemMonitor); | |
| 48 mock_data_fetcher_ = new MockDataFetcher(test_data); | |
| 49 provider_.reset(new GamepadProvider); | |
| 50 provider_->SetDataFetcher(mock_data_fetcher_); | |
| 51 return provider_.get(); | 52 return provider_.get(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 protected: | 55 protected: |
| 55 GamepadProviderTest() { | 56 GamepadProviderTest() { |
| 56 } | 57 } |
| 57 | 58 |
| 58 MessageLoop main_message_loop_; | |
| 59 scoped_ptr<base::SystemMonitor> system_monitor_; | |
| 60 MockDataFetcher* mock_data_fetcher_; | |
| 61 scoped_ptr<GamepadProvider> provider_; | 59 scoped_ptr<GamepadProvider> provider_; |
| 60 |
| 61 // Pointer owned by the provider. |
| 62 MockGamepadDataFetcher* mock_data_fetcher_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 // Crashes. http://crbug.com/106163 | 67 // Crashes. http://crbug.com/106163 |
| 65 TEST_F(GamepadProviderTest, DISABLED_PollingAccess) { | 68 TEST_F(GamepadProviderTest, PollingAccess) { |
| 66 WebGamepads test_data; | 69 WebGamepads test_data; |
| 67 test_data.length = 1; | 70 test_data.length = 1; |
| 68 test_data.items[0].connected = true; | 71 test_data.items[0].connected = true; |
| 69 test_data.items[0].timestamp = 0; | 72 test_data.items[0].timestamp = 0; |
| 70 test_data.items[0].buttonsLength = 1; | 73 test_data.items[0].buttonsLength = 1; |
| 71 test_data.items[0].axesLength = 2; | 74 test_data.items[0].axesLength = 2; |
| 72 test_data.items[0].buttons[0] = 1.f; | 75 test_data.items[0].buttons[0] = 1.f; |
| 73 test_data.items[0].axes[0] = -1.f; | 76 test_data.items[0].axes[0] = -1.f; |
| 74 test_data.items[0].axes[1] = .5f; | 77 test_data.items[0].axes[1] = .5f; |
| 75 | 78 |
| 76 GamepadProvider* provider = CreateProvider(test_data); | 79 GamepadProvider* provider = CreateProvider(test_data); |
| 80 provider->Resume(); |
| 77 | 81 |
| 78 main_message_loop_.RunAllPending(); | 82 message_loop().RunAllPending(); |
| 79 | 83 |
| 80 mock_data_fetcher_->WaitForDataRead(); | 84 mock_data_fetcher_->WaitForDataRead(); |
| 81 | 85 |
| 82 // Renderer-side, pull data out of poll buffer. | 86 // Renderer-side, pull data out of poll buffer. |
| 83 base::SharedMemoryHandle handle = | 87 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 84 provider->GetRendererSharedMemoryHandle(base::GetCurrentProcessHandle()); | 88 base::GetCurrentProcessHandle()); |
| 85 scoped_ptr<base::SharedMemory> shared_memory( | 89 scoped_ptr<base::SharedMemory> shared_memory( |
| 86 new base::SharedMemory(handle, true)); | 90 new base::SharedMemory(handle, true)); |
| 87 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 91 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); |
| 88 void* mem = shared_memory->memory(); | 92 void* mem = shared_memory->memory(); |
| 89 | 93 |
| 90 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 94 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); |
| 91 // See gamepad_hardware_buffer.h for details on the read discipline. | 95 // See gamepad_hardware_buffer.h for details on the read discipline. |
| 92 WebGamepads output; | 96 WebGamepads output; |
| 93 | 97 |
| 94 base::subtle::Atomic32 version; | 98 base::subtle::Atomic32 version; |
| 95 do { | 99 do { |
| 96 version = hwbuf->sequence.ReadBegin(); | 100 version = hwbuf->sequence.ReadBegin(); |
| 97 memcpy(&output, &hwbuf->buffer, sizeof(output)); | 101 memcpy(&output, &hwbuf->buffer, sizeof(output)); |
| 98 } while (hwbuf->sequence.ReadRetry(version)); | 102 } while (hwbuf->sequence.ReadRetry(version)); |
| 99 | 103 |
| 100 EXPECT_EQ(1u, output.length); | 104 EXPECT_EQ(1u, output.length); |
| 101 EXPECT_EQ(1u, output.items[0].buttonsLength); | 105 EXPECT_EQ(1u, output.items[0].buttonsLength); |
| 102 EXPECT_EQ(1.f, output.items[0].buttons[0]); | 106 EXPECT_EQ(1.f, output.items[0].buttons[0]); |
| 103 EXPECT_EQ(2u, output.items[0].axesLength); | 107 EXPECT_EQ(2u, output.items[0].axesLength); |
| 104 EXPECT_EQ(-1.f, output.items[0].axes[0]); | 108 EXPECT_EQ(-1.f, output.items[0].axes[0]); |
| 105 EXPECT_EQ(0.5f, output.items[0].axes[1]); | 109 EXPECT_EQ(0.5f, output.items[0].axes[1]); |
| 106 } | 110 } |
| 107 | 111 |
| 112 // Tests that waiting for a user gesture works properly. |
| 113 TEST_F(GamepadProviderTest, UserGesture) { |
| 114 WebGamepads no_button_data; |
| 115 no_button_data.length = 1; |
| 116 no_button_data.items[0].connected = true; |
| 117 no_button_data.items[0].timestamp = 0; |
| 118 no_button_data.items[0].buttonsLength = 1; |
| 119 no_button_data.items[0].axesLength = 2; |
| 120 no_button_data.items[0].buttons[0] = 0.f; |
| 121 no_button_data.items[0].axes[0] = -1.f; |
| 122 no_button_data.items[0].axes[1] = .5f; |
| 123 |
| 124 WebGamepads button_down_data = no_button_data; |
| 125 button_down_data.items[0].buttons[0] = 1.f; |
| 126 |
| 127 UserGestureListener listener; |
| 128 GamepadProvider* provider = CreateProvider(no_button_data); |
| 129 provider->Resume(); |
| 130 |
| 131 // Register for a user gesture and make sure the provider reads it twice |
| 132 // see below for why). |
| 133 provider->RegisterForUserGesture(listener.GetClosure()); |
| 134 mock_data_fetcher_->WaitForDataRead(); |
| 135 mock_data_fetcher_->WaitForDataRead(); |
| 136 |
| 137 // It should not have issued our callback. |
| 138 message_loop().RunAllPending(); |
| 139 EXPECT_FALSE(listener.has_user_gesture()); |
| 140 |
| 141 // Set a button down and wait for it to be read twice. |
| 142 // |
| 143 // We wait for two reads before calling RunAllPending because the provider |
| 144 // will read the data on the background thread (setting the event) and *then* |
| 145 // will issue the callback on our thread. Waiting for it to read twice |
| 146 // ensures that it was able to issue callbacks for the first read (if it |
| 147 // issued one) before we try to check for it. |
| 148 mock_data_fetcher_->SetTestData(button_down_data); |
| 149 mock_data_fetcher_->WaitForDataRead(); |
| 150 mock_data_fetcher_->WaitForDataRead(); |
| 151 |
| 152 // It should have issued our callback. |
| 153 message_loop().RunAllPending(); |
| 154 EXPECT_TRUE(listener.has_user_gesture()); |
| 155 } |
| 156 |
| 108 } // namespace | 157 } // namespace |
| 109 | 158 |
| 110 } // namespace content | 159 } // namespace content |
| OLD | NEW |