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