Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: content/browser/gamepad/gamepad_provider_unittest.cc

Issue 2081583002: Migrating majority of gamepad from content/browser/ to device/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final tweaks Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/gamepad/gamepad_provider.cc ('k') | content/browser/gamepad/gamepad_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/run_loop.h"
12 #include "build/build_config.h"
13 #include "content/browser/gamepad/gamepad_data_fetcher.h"
14 #include "content/browser/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"
18
19 namespace content {
20
21 namespace {
22
23 using blink::WebGamepads;
24
25 // Helper class to generate and record user gesture callbacks.
26 class UserGestureListener {
27 public:
28 UserGestureListener()
29 : has_user_gesture_(false),
30 weak_factory_(this) {
31 }
32
33 base::Closure GetClosure() {
34 return base::Bind(&UserGestureListener::GotUserGesture,
35 weak_factory_.GetWeakPtr());
36 }
37
38 bool has_user_gesture() const { return has_user_gesture_; }
39
40 private:
41 void GotUserGesture() {
42 has_user_gesture_ = true;
43 }
44
45 bool has_user_gesture_;
46 base::WeakPtrFactory<UserGestureListener> weak_factory_;
47 };
48
49 // Main test fixture
50 class GamepadProviderTest : public testing::Test, public GamepadTestHelper {
51 public:
52 GamepadProvider* CreateProvider(const WebGamepads& test_data) {
53 mock_data_fetcher_ = new MockGamepadDataFetcher(test_data);
54 provider_.reset(new GamepadProvider(
55 std::unique_ptr<GamepadDataFetcher>(mock_data_fetcher_)));
56 return provider_.get();
57 }
58
59 protected:
60 GamepadProviderTest() {
61 }
62
63 std::unique_ptr<GamepadProvider> provider_;
64
65 // Pointer owned by the provider.
66 MockGamepadDataFetcher* mock_data_fetcher_;
67
68 DISALLOW_COPY_AND_ASSIGN(GamepadProviderTest);
69 };
70
71 // Crashes. http://crbug.com/106163
72 // crbug.com/147549
73 #if defined(OS_ANDROID)
74 #define MAYBE_PollingAccess DISABLED_PollingAccess
75 #else
76 #define MAYBE_PollingAccess PollingAccess
77 #endif
78 TEST_F(GamepadProviderTest, MAYBE_PollingAccess) {
79 WebGamepads test_data;
80 test_data.length = 1;
81 test_data.items[0].connected = true;
82 test_data.items[0].timestamp = 0;
83 test_data.items[0].buttonsLength = 1;
84 test_data.items[0].axesLength = 2;
85 test_data.items[0].buttons[0].value = 1.f;
86 test_data.items[0].buttons[0].pressed = true;
87 test_data.items[0].axes[0] = -1.f;
88 test_data.items[0].axes[1] = .5f;
89
90 GamepadProvider* provider = CreateProvider(test_data);
91 provider->Resume();
92
93 base::RunLoop().RunUntilIdle();
94
95 mock_data_fetcher_->WaitForDataRead();
96
97 // Renderer-side, pull data out of poll buffer.
98 base::SharedMemoryHandle handle = provider->GetSharedMemoryHandleForProcess(
99 base::GetCurrentProcessHandle());
100 std::unique_ptr<base::SharedMemory> shared_memory(
101 new base::SharedMemory(handle, true));
102 EXPECT_TRUE(shared_memory->Map(sizeof(GamepadHardwareBuffer)));
103 void* mem = shared_memory->memory();
104
105 GamepadHardwareBuffer* hwbuf = static_cast<GamepadHardwareBuffer*>(mem);
106 // See gamepad_hardware_buffer.h for details on the read discipline.
107 WebGamepads output;
108
109 base::subtle::Atomic32 version;
110 do {
111 version = hwbuf->sequence.ReadBegin();
112 memcpy(&output, &hwbuf->buffer, sizeof(output));
113 } while (hwbuf->sequence.ReadRetry(version));
114
115 EXPECT_EQ(1u, output.length);
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 }
123
124 // Tests that waiting for a user gesture works properly.
125 TEST_F(GamepadProviderTest, UserGesture) {
126 WebGamepads no_button_data;
127 no_button_data.length = 1;
128 no_button_data.items[0].connected = true;
129 no_button_data.items[0].timestamp = 0;
130 no_button_data.items[0].buttonsLength = 1;
131 no_button_data.items[0].axesLength = 2;
132 no_button_data.items[0].buttons[0].value = 0.f;
133 no_button_data.items[0].buttons[0].pressed = false;
134 no_button_data.items[0].axes[0] = 0.f;
135 no_button_data.items[0].axes[1] = .4f;
136
137 WebGamepads button_down_data = no_button_data;
138 button_down_data.items[0].buttons[0].value = 1.f;
139 button_down_data.items[0].buttons[0].pressed = true;
140
141 UserGestureListener listener;
142 GamepadProvider* provider = CreateProvider(no_button_data);
143 provider->Resume();
144
145 provider->RegisterForUserGesture(listener.GetClosure());
146 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued();
147
148 // It should not have issued our callback.
149 base::RunLoop().RunUntilIdle();
150 EXPECT_FALSE(listener.has_user_gesture());
151
152 // Set a button down and wait for it to be read twice.
153 mock_data_fetcher_->SetTestData(button_down_data);
154 mock_data_fetcher_->WaitForDataReadAndCallbacksIssued();
155
156 // It should have issued our callback.
157 base::RunLoop().RunUntilIdle();
158 EXPECT_TRUE(listener.has_user_gesture());
159 }
160
161 } // namespace
162
163 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/gamepad/gamepad_provider.cc ('k') | content/browser/gamepad/gamepad_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698