| 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 "content/browser/gamepad/gamepad_provider.h" | 5 #include "device/gamepad/gamepad_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "content/browser/gamepad/gamepad_data_fetcher.h" | 13 #include "device/gamepad/gamepad_data_fetcher.h" |
| 14 #include "content/browser/gamepad/gamepad_test_helpers.h" | 14 #include "device/gamepad/gamepad_test_helpers.h" |
| 15 #include "content/common/gamepad_hardware_buffer.h" | |
| 16 #include "content/common/gamepad_messages.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 16 |
| 19 namespace content { | 17 namespace device { |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 using blink::WebGamepads; | 21 using blink::WebGamepads; |
| 24 | 22 |
| 25 // Helper class to generate and record user gesture callbacks. | 23 // Helper class to generate and record user gesture callbacks. |
| 26 class UserGestureListener { | 24 class UserGestureListener { |
| 27 public: | 25 public: |
| 28 UserGestureListener() | 26 UserGestureListener() : has_user_gesture_(false), weak_factory_(this) {} |
| 29 : has_user_gesture_(false), | |
| 30 weak_factory_(this) { | |
| 31 } | |
| 32 | 27 |
| 33 base::Closure GetClosure() { | 28 base::Closure GetClosure() { |
| 34 return base::Bind(&UserGestureListener::GotUserGesture, | 29 return base::Bind(&UserGestureListener::GotUserGesture, |
| 35 weak_factory_.GetWeakPtr()); | 30 weak_factory_.GetWeakPtr()); |
| 36 } | 31 } |
| 37 | 32 |
| 38 bool has_user_gesture() const { return has_user_gesture_; } | 33 bool has_user_gesture() const { return has_user_gesture_; } |
| 39 | 34 |
| 40 private: | 35 private: |
| 41 void GotUserGesture() { | 36 void GotUserGesture() { has_user_gesture_ = true; } |
| 42 has_user_gesture_ = true; | |
| 43 } | |
| 44 | 37 |
| 45 bool has_user_gesture_; | 38 bool has_user_gesture_; |
| 46 base::WeakPtrFactory<UserGestureListener> weak_factory_; | 39 base::WeakPtrFactory<UserGestureListener> weak_factory_; |
| 47 }; | 40 }; |
| 48 | 41 |
| 49 // Main test fixture | 42 // Main test fixture |
| 50 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { | 43 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { |
| 51 public: | 44 public: |
| 52 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | 45 GamepadProvider* CreateProvider(const WebGamepads& test_data) { |
| 53 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); | 46 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); |
| 54 provider_.reset(new GamepadProvider( | 47 provider_.reset(new GamepadProvider( |
| 55 std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_))); | 48 std::unique_ptr<GamepadSharedBuffer>(new MockGamepadSharedBuffer()), |
| 49 nullptr, std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_))); |
| 56 return provider_.get(); | 50 return provider_.get(); |
| 57 } | 51 } |
| 58 | 52 |
| 59 protected: | 53 protected: |
| 60 GamepadProviderTest() { | 54 GamepadProviderTest() {} |
| 61 } | |
| 62 | 55 |
| 63 std::unique_ptr<GamepadProvider> provider_; | 56 std::unique_ptr<GamepadProvider> provider_; |
| 64 | 57 |
| 65 // Pointer owned by the provider. | 58 // Pointer owned by the provider. |
| 66 MockGamepadDataFetcher* mock_data_fetcher_; | 59 MockGamepadDataFetcher* mock_data_fetcher_; |
| 67 | 60 |
| 68 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); | 61 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); |
| 69 }; | 62 }; |
| 70 | 63 |
| 71 // Crashes. http://crbug.com/106163 | 64 // Crashes. http://crbug.com/106163 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 92 | 85 |
| 93 base::RunLoop().RunUntilIdle(); | 86 base::RunLoop().RunUntilIdle(); |
| 94 | 87 |
| 95 mock_data_fetcher_->WaitForDataRead(); | 88 mock_data_fetcher_->WaitForDataRead(); |
| 96 | 89 |
| 97 // Renderer-side, pull data out of poll buffer. | 90 // Renderer-side, pull data out of poll buffer. |
| 98 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | 91 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( |
| 99 base::GetCurrentProcessHandle()); | 92 base::GetCurrentProcessHandle()); |
| 100 std::unique_ptr<base::SharedMemory> shared_memory( | 93 std::unique_ptr<base::SharedMemory> shared_memory( |
| 101 new base::SharedMemory(handle, true)); | 94 new base::SharedMemory(handle, true)); |
| 102 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | 95 EXPECT_TRUE(shared_memory->Map(sizeof(WebGamepads))); |
| 103 void* mem = shared_memory->memory(); | 96 void* mem = shared_memory->memory(); |
| 104 | 97 |
| 105 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | 98 WebGamepads* output = static_cast<WebGamepads*>(mem); |
| 106 // See gamepad_hardware_buffer.h for details on the read discipline. | |
| 107 WebGamepads output; | |
| 108 | 99 |
| 109 base::subtle::Atomic32 version; | 100 EXPECT_EQ(1u, output->length); |
| 110 do { | 101 EXPECT_EQ(1u, output->items[0].buttonsLength); |
| 111 version = hwbuf->sequence.ReadBegin(); | 102 EXPECT_EQ(1.f, output->items[0].buttons[0].value); |
| 112 memcpy(&output, &hwbuf->buffer, sizeof(output)); | 103 EXPECT_EQ(true, output->items[0].buttons[0].pressed); |
| 113 } while (hwbuf->sequence.ReadRetry(version)); | 104 EXPECT_EQ(2u, output->items[0].axesLength); |
| 114 | 105 EXPECT_EQ(-1.f, output->items[0].axes[0]); |
| 115 EXPECT_EQ(1u, output.length); | 106 EXPECT_EQ(0.5f, output->items[0].axes[1]); |
| 116 EXPECT_EQ(1u, output.items[0].buttonsLength); | |
| 117 EXPECT_EQ(1.f, output.items[0].buttons[0].value); | |
| 118 EXPECT_EQ(true, output.items[0].buttons[0].pressed); | |
| 119 EXPECT_EQ(2u, output.items[0].axesLength); | |
| 120 EXPECT_EQ(-1.f, output.items[0].axes[0]); | |
| 121 EXPECT_EQ(0.5f, output.items[0].axes[1]); | |
| 122 } | 107 } |
| 123 | 108 |
| 124 // Tests that waiting for a user gesture works properly. | 109 // Tests that waiting for a user gesture works properly. |
| 125 TEST_F(GamepadProviderTest, UserGesture) { | 110 TEST_F(GamepadProviderTest, UserGesture) { |
| 126 WebGamepads no_button_data; | 111 WebGamepads no_button_data; |
| 127 no_button_data.length = 1; | 112 no_button_data.length = 1; |
| 128 no_button_data.items[0].connected = true; | 113 no_button_data.items[0].connected = true; |
| 129 no_button_data.items[0].timestamp = 0; | 114 no_button_data.items[0].timestamp = 0; |
| 130 no_button_data.items[0].buttonsLength = 1; | 115 no_button_data.items[0].buttonsLength = 1; |
| 131 no_button_data.items[0].axesLength = 2; | 116 no_button_data.items[0].axesLength = 2; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 153 mock_data_fetcher_->SetTestData(button_down_data); | 138 mock_data_fetcher_->SetTestData(button_down_data); |
| 154 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | 139 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); |
| 155 | 140 |
| 156 // It should have issued our callback. | 141 // It should have issued our callback. |
| 157 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_TRUE(listener.has_user_gesture()); | 143 EXPECT_TRUE(listener.has_user_gesture()); |
| 159 } | 144 } |
| 160 | 145 |
| 161 } // namespace | 146 } // namespace |
| 162 | 147 |
| 163 } // namespace content | 148 } // namespace device |
| OLD | NEW |