OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/gamepad/gamepad_provider.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "build/build_config.h" | |
12 #include "content/browser/gamepad/gamepad_data_fetcher.h" | |
13 #include "content/browser/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" | |
17 | |
18 namespace content { | |
19 | |
20 namespace { | |
21 | |
22 using blink::WebGamepads; | |
23 | |
24 // Helper class to generate and record user gesture callbacks. | |
25 class UserGestureListener { | |
26 public: | |
27 UserGestureListener() | |
28 : has_user_gesture_(false), | |
29 weak_factory_(this) { | |
30 } | |
31 | |
32 base::Closure GetClosure() { | |
33 return base::Bind(&UserGestureListener::GotUserGesture, | |
34 weak_factory_.GetWeakPtr()); | |
35 } | |
36 | |
37 bool has_user_gesture() const { return has_user_gesture_; } | |
38 | |
39 private: | |
40 void GotUserGesture() { | |
41 has_user_gesture_ = true; | |
42 } | |
43 | |
44 bool has_user_gesture_; | |
45 base::WeakPtrFactory<UserGestureListener> weak_factory_; | |
46 }; | |
47 | |
48 // Main test fixture | |
49 class GamepadProviderTest : public testing::Test, public GamepadTestHelper { | |
50 public: | |
51 GamepadProvider* CreateProvider(const WebGamepads& test_data) { | |
52 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data); | |
53 provider_.reset(new GamepadProvider( | |
54 std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_))); | |
55 return provider_.get(); | |
56 } | |
57 | |
58 protected: | |
59 GamepadProviderTest() { | |
60 } | |
61 | |
62 std::unique_ptr<GamepadProvider> provider_; | |
63 | |
64 // Pointer owned by the provider. | |
65 MockGamepadDataFetcher* mock_data_fetcher_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest); | |
68 }; | |
69 | |
70 // Crashes. http://crbug.com/106163 | |
71 // crbug.com/147549 | |
72 #if defined(OS_ANDROID) | |
73 #define MAYBE_PollingAccess DISABLED_PollingAccess | |
74 #else | |
75 #define MAYBE_PollingAccess PollingAccess | |
76 #endif | |
77 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) { | |
78 WebGamepads test_data; | |
79 test_data.length = 1; | |
80 test_data.items[0].connected = true; | |
81 test_data.items[0].timestamp = 0; | |
82 test_data.items[0].buttonsLength = 1; | |
83 test_data.items[0].axesLength = 2; | |
84 test_data.items[0].buttons[0].value = 1.f; | |
85 test_data.items[0].buttons[0].pressed = true; | |
86 test_data.items[0].axes[0] = -1.f; | |
87 test_data.items[0].axes[1] = .5f; | |
88 | |
89 GamepadProvider* provider = CreateProvider(test_data); | |
90 provider->Resume(); | |
91 | |
92 message_loop().RunUntilIdle(); | |
93 | |
94 mock_data_fetcher_->WaitForDataRead(); | |
95 | |
96 // Renderer-side, pull data out of poll buffer. | |
97 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess( | |
98 base::GetCurrentProcessHandle()); | |
99 std::unique_ptr<base::SharedMemory> shared_memory( | |
100 new base::SharedMemory(handle, true)); | |
101 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer))); | |
102 void* mem = shared_memory->memory(); | |
103 | |
104 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem); | |
105 // See gamepad_hardware_buffer.h for details on the read discipline. | |
106 WebGamepads output; | |
107 | |
108 base::subtle::Atomic32 version; | |
109 do { | |
110 version = hwbuf->sequence.ReadBegin(); | |
111 memcpy(&output, &hwbuf->buffer, sizeof(output)); | |
112 } while (hwbuf->sequence.ReadRetry(version)); | |
113 | |
114 EXPECT_EQ(1u, output.length); | |
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 } | |
122 | |
123 // Tests that waiting for a user gesture works properly. | |
124 TEST_F(GamepadProviderTest, UserGesture) { | |
125 WebGamepads no_button_data; | |
126 no_button_data.length = 1; | |
127 no_button_data.items[0].connected = true; | |
128 no_button_data.items[0].timestamp = 0; | |
129 no_button_data.items[0].buttonsLength = 1; | |
130 no_button_data.items[0].axesLength = 2; | |
131 no_button_data.items[0].buttons[0].value = 0.f; | |
132 no_button_data.items[0].buttons[0].pressed = false; | |
133 no_button_data.items[0].axes[0] = 0.f; | |
134 no_button_data.items[0].axes[1] = .4f; | |
135 | |
136 WebGamepads button_down_data = no_button_data; | |
137 button_down_data.items[0].buttons[0].value = 1.f; | |
138 button_down_data.items[0].buttons[0].pressed = true; | |
139 | |
140 UserGestureListener listener; | |
141 GamepadProvider* provider = CreateProvider(no_button_data); | |
142 provider->Resume(); | |
143 | |
144 provider->RegisterForUserGesture(listener.GetClosure()); | |
145 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
146 | |
147 // It should not have issued our callback. | |
148 message_loop().RunUntilIdle(); | |
149 EXPECT_FALSE(listener.has_user_gesture()); | |
150 | |
151 // Set a button down and wait for it to be read twice. | |
152 mock_data_fetcher_->SetTestData(button_down_data); | |
153 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued(); | |
154 | |
155 // It should have issued our callback. | |
156 message_loop().RunUntilIdle(); | |
157 EXPECT_TRUE(listener.has_user_gesture()); | |
158 } | |
159 | |
160 } // namespace | |
161 | |
162 } // namespace content | |
OLD | NEW |