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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/weak_ptr.h"
7 #include "content/browser/gamepad/gamepad_data_fetcher.h"
8 #include "content/browser/gamepad/gamepad_provider.h"
9 #include "content/browser/gamepad/gamepad_test_helpers.h"
10 #include "content/common/gamepad_hardware_buffer.h"
11 #include "content/common/gamepad_messages.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 using blink::WebGamepads
;
20 // Helper class to generate and record user gesture callbacks.
21 class UserGestureListener
{
24 : has_user_gesture_(false),
28 base::Closure
GetClosure() {
29 return base::Bind(&UserGestureListener::GotUserGesture
,
30 weak_factory_
.GetWeakPtr());
33 bool has_user_gesture() const { return has_user_gesture_
; }
36 void GotUserGesture() {
37 has_user_gesture_
= true;
40 bool has_user_gesture_
;
41 base::WeakPtrFactory
<UserGestureListener
> weak_factory_
;
45 class GamepadProviderTest
: public testing::Test
, public GamepadTestHelper
{
47 GamepadProvider
* CreateProvider(const WebGamepads
& test_data
) {
48 mock_data_fetcher_
= new MockGamepadDataFetcher(test_data
);
49 provider_
.reset(new GamepadProvider(
50 scoped_ptr
<GamepadDataFetcher
>(mock_data_fetcher_
)));
51 return provider_
.get();
55 GamepadProviderTest() {
58 scoped_ptr
<GamepadProvider
> provider_
;
60 // Pointer owned by the provider.
61 MockGamepadDataFetcher
* mock_data_fetcher_
;
63 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest
);
66 // Crashes. http://crbug.com/106163
68 #if defined(OS_ANDROID)
69 #define MAYBE_PollingAccess DISABLED_PollingAccess
71 #define MAYBE_PollingAccess PollingAccess
73 TEST_F(GamepadProviderTest
, MAYBE_PollingAccess
) {
74 WebGamepads test_data
;
76 test_data
.items
[0].connected
= true;
77 test_data
.items
[0].timestamp
= 0;
78 test_data
.items
[0].buttonsLength
= 1;
79 test_data
.items
[0].axesLength
= 2;
80 test_data
.items
[0].buttons
[0].value
= 1.f
;
81 test_data
.items
[0].buttons
[0].pressed
= true;
82 test_data
.items
[0].axes
[0] = -1.f
;
83 test_data
.items
[0].axes
[1] = .5f
;
85 GamepadProvider
* provider
= CreateProvider(test_data
);
88 message_loop().RunUntilIdle();
90 mock_data_fetcher_
->WaitForDataRead();
92 // Renderer-side, pull data out of poll buffer.
93 base::SharedMemoryHandle handle
= provider
->GetSharedMemoryHandleForProcess(
94 base::GetCurrentProcessHandle());
95 scoped_ptr
<base::SharedMemory
> shared_memory(
96 new base::SharedMemory(handle
, true));
97 EXPECT_TRUE(shared_memory
->Map(sizeof(GamepadHardwareBuffer
)));
98 void* mem
= shared_memory
->memory();
100 GamepadHardwareBuffer
* hwbuf
= static_cast<GamepadHardwareBuffer
*>(mem
);
101 // See gamepad_hardware_buffer.h for details on the read discipline.
104 base::subtle::Atomic32 version
;
106 version
= hwbuf
->sequence
.ReadBegin();
107 memcpy(&output
, &hwbuf
->buffer
, sizeof(output
));
108 } while (hwbuf
->sequence
.ReadRetry(version
));
110 EXPECT_EQ(1u, output
.length
);
111 EXPECT_EQ(1u, output
.items
[0].buttonsLength
);
112 EXPECT_EQ(1.f
, output
.items
[0].buttons
[0].value
);
113 EXPECT_EQ(true, output
.items
[0].buttons
[0].pressed
);
114 EXPECT_EQ(2u, output
.items
[0].axesLength
);
115 EXPECT_EQ(-1.f
, output
.items
[0].axes
[0]);
116 EXPECT_EQ(0.5f
, output
.items
[0].axes
[1]);
119 // Tests that waiting for a user gesture works properly.
120 TEST_F(GamepadProviderTest
, UserGesture
) {
121 WebGamepads no_button_data
;
122 no_button_data
.length
= 1;
123 no_button_data
.items
[0].connected
= true;
124 no_button_data
.items
[0].timestamp
= 0;
125 no_button_data
.items
[0].buttonsLength
= 1;
126 no_button_data
.items
[0].axesLength
= 2;
127 no_button_data
.items
[0].buttons
[0].value
= 0.f
;
128 no_button_data
.items
[0].buttons
[0].pressed
= false;
129 no_button_data
.items
[0].axes
[0] = -1.f
;
130 no_button_data
.items
[0].axes
[1] = .5f
;
132 WebGamepads button_down_data
= no_button_data
;
133 button_down_data
.items
[0].buttons
[0].value
= 1.f
;
134 button_down_data
.items
[0].buttons
[0].pressed
= true;
136 UserGestureListener listener
;
137 GamepadProvider
* provider
= CreateProvider(no_button_data
);
140 provider
->RegisterForUserGesture(listener
.GetClosure());
141 mock_data_fetcher_
->WaitForDataReadAndCallbacksIssued();
143 // It should not have issued our callback.
144 message_loop().RunUntilIdle();
145 EXPECT_FALSE(listener
.has_user_gesture());
147 // Set a button down and wait for it to be read twice.
148 mock_data_fetcher_
->SetTestData(button_down_data
);
149 mock_data_fetcher_
->WaitForDataReadAndCallbacksIssued();
151 // It should have issued our callback.
152 message_loop().RunUntilIdle();
153 EXPECT_TRUE(listener
.has_user_gesture());
158 } // namespace content