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 "base/process_util.h"
8 #include "content/browser/gamepad/gamepad_data_fetcher.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"
12 #include "content/common/gamepad_messages.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 using WebKit::WebGamepads
;
21 // Helper class to generate and record user gesture callbacks.
22 class UserGestureListener
{
25 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
26 has_user_gesture_(false) {
29 base::Closure
GetClosure() {
30 return base::Bind(&UserGestureListener::GotUserGesture
,
31 weak_factory_
.GetWeakPtr());
34 bool has_user_gesture() const { return has_user_gesture_
; }
37 void GotUserGesture() {
38 has_user_gesture_
= true;
41 base::WeakPtrFactory
<UserGestureListener
> weak_factory_
;
42 bool has_user_gesture_
;
46 class GamepadProviderTest
: public testing::Test
, public GamepadTestHelper
{
48 GamepadProvider
* CreateProvider(const WebGamepads
& test_data
) {
49 mock_data_fetcher_
= new MockGamepadDataFetcher(test_data
);
50 provider_
.reset(new GamepadProvider(
51 scoped_ptr
<GamepadDataFetcher
>(mock_data_fetcher_
)));
52 return provider_
.get();
56 GamepadProviderTest() {
59 scoped_ptr
<GamepadProvider
> provider_
;
61 // Pointer owned by the provider.
62 MockGamepadDataFetcher
* mock_data_fetcher_
;
64 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest
);
67 // Crashes. http://crbug.com/106163
68 TEST_F(GamepadProviderTest
, PollingAccess
) {
69 WebGamepads test_data
;
71 test_data
.items
[0].connected
= true;
72 test_data
.items
[0].timestamp
= 0;
73 test_data
.items
[0].buttonsLength
= 1;
74 test_data
.items
[0].axesLength
= 2;
75 test_data
.items
[0].buttons
[0] = 1.f
;
76 test_data
.items
[0].axes
[0] = -1.f
;
77 test_data
.items
[0].axes
[1] = .5f
;
79 GamepadProvider
* provider
= CreateProvider(test_data
);
82 message_loop().RunUntilIdle();
84 mock_data_fetcher_
->WaitForDataRead();
86 // Renderer-side, pull data out of poll buffer.
87 base::SharedMemoryHandle handle
= provider
->GetSharedMemoryHandleForProcess(
88 base::GetCurrentProcessHandle());
89 scoped_ptr
<base::SharedMemory
> shared_memory(
90 new base::SharedMemory(handle
, true));
91 EXPECT_TRUE(shared_memory
->Map(sizeof(GamepadHardwareBuffer
)));
92 void* mem
= shared_memory
->memory();
94 GamepadHardwareBuffer
* hwbuf
= static_cast<GamepadHardwareBuffer
*>(mem
);
95 // See gamepad_hardware_buffer.h for details on the read discipline.
98 base::subtle::Atomic32 version
;
100 version
= hwbuf
->sequence
.ReadBegin();
101 memcpy(&output
, &hwbuf
->buffer
, sizeof(output
));
102 } while (hwbuf
->sequence
.ReadRetry(version
));
104 EXPECT_EQ(1u, output
.length
);
105 EXPECT_EQ(1u, output
.items
[0].buttonsLength
);
106 EXPECT_EQ(1.f
, output
.items
[0].buttons
[0]);
107 EXPECT_EQ(2u, output
.items
[0].axesLength
);
108 EXPECT_EQ(-1.f
, output
.items
[0].axes
[0]);
109 EXPECT_EQ(0.5f
, output
.items
[0].axes
[1]);
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
;
124 WebGamepads button_down_data
= no_button_data
;
125 button_down_data
.items
[0].buttons
[0] = 1.f
;
127 UserGestureListener listener
;
128 GamepadProvider
* provider
= CreateProvider(no_button_data
);
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();
137 // It should not have issued our callback.
138 message_loop().RunUntilIdle();
139 EXPECT_FALSE(listener
.has_user_gesture());
141 // Set a button down and wait for it to be read twice.
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();
152 // It should have issued our callback.
153 message_loop().RunUntilIdle();
154 EXPECT_TRUE(listener
.has_user_gesture());
159 } // namespace content