gamepad_provider_unittest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. #include "device/gamepad/gamepad_provider.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/command_line.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/run_loop.h"
  11. #include "base/threading/platform_thread.h"
  12. #include "base/threading/thread.h"
  13. #include "build/build_config.h"
  14. #include "device/gamepad/gamepad_data_fetcher.h"
  15. #include "device/gamepad/gamepad_test_helpers.h"
  16. #include "device/gamepad/public/cpp/gamepad_switches.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace device {
  19. namespace {
  20. // Helper class to generate and record user gesture callbacks.
  21. class UserGestureListener {
  22. public:
  23. UserGestureListener() : has_user_gesture_(false) {}
  24. base::OnceClosure GetClosure() {
  25. return base::BindOnce(&UserGestureListener::GotUserGesture,
  26. weak_factory_.GetWeakPtr());
  27. }
  28. bool has_user_gesture() const { return has_user_gesture_; }
  29. private:
  30. void GotUserGesture() { has_user_gesture_ = true; }
  31. bool has_user_gesture_;
  32. base::WeakPtrFactory<UserGestureListener> weak_factory_{this};
  33. };
  34. class TestChangeClient : public GamepadChangeClient {
  35. public:
  36. TestChangeClient() = default;
  37. TestChangeClient(const TestChangeClient&) = delete;
  38. TestChangeClient& operator=(const TestChangeClient&) = delete;
  39. ~TestChangeClient() = default;
  40. void OnGamepadConnectionChange(bool connected,
  41. uint32_t index,
  42. const Gamepad& pad) override {}
  43. void OnGamepadChange(mojom::GamepadChangesPtr changes) override {
  44. all_changes_.push_back(std::move(changes));
  45. EXPECT_GT(num_changes_left_, 0);
  46. if (--num_changes_left_ == 0)
  47. run_loop_.Quit();
  48. }
  49. void RunUntilChangeEvents(int num_changes) {
  50. // If we are explicitly not expecting any changes wait 20 milliseconds
  51. // to ensure no changes come in.
  52. if (num_changes == 0) {
  53. num_changes_left_ = 0;
  54. base::PlatformThread::Sleep(base::Milliseconds(20));
  55. base::RunLoop().RunUntilIdle();
  56. return;
  57. }
  58. num_changes_left_ = num_changes;
  59. run_loop_.Run();
  60. }
  61. const std::vector<mojom::GamepadChangesPtr>& all_changes() const {
  62. return all_changes_;
  63. }
  64. private:
  65. int num_changes_left_ = 0;
  66. base::RunLoop run_loop_;
  67. std::vector<mojom::GamepadChangesPtr> all_changes_;
  68. };
  69. // Main test fixture
  70. class GamepadProviderTest : public testing::Test, public GamepadTestHelper {
  71. public:
  72. GamepadProviderTest(const GamepadProviderTest&) = delete;
  73. GamepadProviderTest& operator=(const GamepadProviderTest&) = delete;
  74. GamepadProvider* CreateProvider(const Gamepads& test_data) {
  75. auto fetcher = std::make_unique<MockGamepadDataFetcher>(test_data);
  76. mock_data_fetcher_ = fetcher.get();
  77. provider_ =
  78. std::make_unique<GamepadProvider>(&change_client_, std::move(fetcher),
  79. /*polling_thread=*/nullptr);
  80. return provider_.get();
  81. }
  82. // Sleep until the shared memory buffer's seqlock advances the buffer version,
  83. // indicating that the gamepad provider has written to it after polling the
  84. // gamepad fetchers. The buffer will report an odd value for the version if
  85. // the buffer is not in a consistent state, so we also require that the value
  86. // is even before continuing.
  87. void WaitForData(const GamepadHardwareBuffer* buffer) {
  88. const base::subtle::Atomic32 initial_version = buffer->seqlock.ReadBegin();
  89. base::subtle::Atomic32 current_version;
  90. do {
  91. base::PlatformThread::Sleep(base::Milliseconds(10));
  92. current_version = buffer->seqlock.ReadBegin();
  93. } while (current_version % 2 || current_version == initial_version);
  94. }
  95. // The provider polls the data on the background thread and then issues
  96. // the callback on the client thread. Waiting for it to poll twice ensures
  97. // that it was able to issue callbacks for the first poll.
  98. void WaitForDataAndCallbacksIssued(const GamepadHardwareBuffer* buffer) {
  99. WaitForData(buffer);
  100. WaitForData(buffer);
  101. }
  102. void ReadGamepadHardwareBuffer(const GamepadHardwareBuffer* buffer,
  103. Gamepads* output) {
  104. memset(output, 0, sizeof(Gamepads));
  105. base::subtle::Atomic32 version;
  106. do {
  107. version = buffer->seqlock.ReadBegin();
  108. memcpy(output, &buffer->data, sizeof(Gamepads));
  109. } while (buffer->seqlock.ReadRetry(version));
  110. }
  111. protected:
  112. GamepadProviderTest() = default;
  113. std::unique_ptr<GamepadProvider> provider_;
  114. // Pointer owned by the provider.
  115. raw_ptr<MockGamepadDataFetcher> mock_data_fetcher_;
  116. TestChangeClient change_client_;
  117. };
  118. TEST_F(GamepadProviderTest, PollingAccess) {
  119. Gamepads test_data;
  120. memset(&test_data, 0, sizeof(Gamepads));
  121. test_data.items[0].connected = true;
  122. test_data.items[0].timestamp = 0;
  123. test_data.items[0].buttons_length = 1;
  124. test_data.items[0].axes_length = 2;
  125. test_data.items[0].buttons[0].value = 1.0f;
  126. test_data.items[0].buttons[0].pressed = true;
  127. test_data.items[0].axes[0] = -1.0f;
  128. test_data.items[0].axes[1] = 0.5f;
  129. GamepadProvider* provider = CreateProvider(test_data);
  130. provider->SetSanitizationEnabled(false);
  131. provider->Resume();
  132. base::RunLoop().RunUntilIdle();
  133. // Renderer-side, pull data out of poll buffer.
  134. base::ReadOnlySharedMemoryRegion region =
  135. provider->DuplicateSharedMemoryRegion();
  136. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  137. EXPECT_TRUE(mapping.IsValid());
  138. const GamepadHardwareBuffer* buffer =
  139. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  140. // Wait until the shared memory buffer has been written at least once.
  141. WaitForData(buffer);
  142. Gamepads output;
  143. ReadGamepadHardwareBuffer(buffer, &output);
  144. ASSERT_EQ(1u, output.items[0].buttons_length);
  145. EXPECT_EQ(1.0f, output.items[0].buttons[0].value);
  146. EXPECT_EQ(true, output.items[0].buttons[0].pressed);
  147. ASSERT_EQ(2u, output.items[0].axes_length);
  148. EXPECT_EQ(-1.0f, output.items[0].axes[0]);
  149. EXPECT_EQ(0.5f, output.items[0].axes[1]);
  150. }
  151. TEST_F(GamepadProviderTest, ConnectDisconnectMultiple) {
  152. Gamepads test_data;
  153. test_data.items[0].connected = true;
  154. test_data.items[0].timestamp = 0;
  155. test_data.items[0].axes_length = 2;
  156. test_data.items[0].axes[0] = -1.0f;
  157. test_data.items[0].axes[1] = 0.5f;
  158. test_data.items[1].connected = true;
  159. test_data.items[1].timestamp = 0;
  160. test_data.items[1].axes_length = 2;
  161. test_data.items[1].axes[0] = 1.0f;
  162. test_data.items[1].axes[1] = -0.5f;
  163. Gamepads test_data_onedisconnected;
  164. test_data_onedisconnected.items[1].connected = true;
  165. test_data_onedisconnected.items[1].timestamp = 0;
  166. test_data_onedisconnected.items[1].axes_length = 2;
  167. test_data_onedisconnected.items[1].axes[0] = 1.0f;
  168. test_data_onedisconnected.items[1].axes[1] = -0.5f;
  169. GamepadProvider* provider = CreateProvider(test_data);
  170. provider->SetSanitizationEnabled(false);
  171. provider->Resume();
  172. base::RunLoop().RunUntilIdle();
  173. // Renderer-side, pull data out of poll buffer.
  174. base::ReadOnlySharedMemoryRegion region =
  175. provider->DuplicateSharedMemoryRegion();
  176. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  177. EXPECT_TRUE(mapping.IsValid());
  178. const GamepadHardwareBuffer* buffer =
  179. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  180. // Wait until the shared memory buffer has been written at least once.
  181. WaitForData(buffer);
  182. Gamepads output;
  183. ReadGamepadHardwareBuffer(buffer, &output);
  184. ASSERT_EQ(2u, output.items[0].axes_length);
  185. EXPECT_EQ(-1.0f, output.items[0].axes[0]);
  186. EXPECT_EQ(0.5f, output.items[0].axes[1]);
  187. ASSERT_EQ(2u, output.items[1].axes_length);
  188. EXPECT_EQ(1.0f, output.items[1].axes[0]);
  189. EXPECT_EQ(-0.5f, output.items[1].axes[1]);
  190. mock_data_fetcher_->SetTestData(test_data_onedisconnected);
  191. WaitForDataAndCallbacksIssued(buffer);
  192. ReadGamepadHardwareBuffer(buffer, &output);
  193. EXPECT_EQ(0u, output.items[0].axes_length);
  194. ASSERT_EQ(2u, output.items[1].axes_length);
  195. EXPECT_EQ(1.0f, output.items[1].axes[0]);
  196. EXPECT_EQ(-0.5f, output.items[1].axes[1]);
  197. }
  198. // Tests that waiting for a user gesture works properly.
  199. TEST_F(GamepadProviderTest, UserGesture) {
  200. Gamepads no_button_data;
  201. no_button_data.items[0].connected = true;
  202. no_button_data.items[0].timestamp = 0;
  203. no_button_data.items[0].buttons_length = 1;
  204. no_button_data.items[0].axes_length = 2;
  205. no_button_data.items[0].buttons[0].value = 0.0f;
  206. no_button_data.items[0].buttons[0].pressed = false;
  207. no_button_data.items[0].axes[0] = 0.0f;
  208. no_button_data.items[0].axes[1] = 0.4f;
  209. Gamepads button_down_data = no_button_data;
  210. button_down_data.items[0].buttons[0].value = 1.0f;
  211. button_down_data.items[0].buttons[0].pressed = true;
  212. UserGestureListener listener;
  213. GamepadProvider* provider = CreateProvider(no_button_data);
  214. provider->SetSanitizationEnabled(false);
  215. provider->Resume();
  216. provider->RegisterForUserGesture(listener.GetClosure());
  217. base::RunLoop().RunUntilIdle();
  218. // Renderer-side, pull data out of poll buffer.
  219. base::ReadOnlySharedMemoryRegion region =
  220. provider->DuplicateSharedMemoryRegion();
  221. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  222. EXPECT_TRUE(mapping.IsValid());
  223. const GamepadHardwareBuffer* buffer =
  224. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  225. // Wait until the shared memory buffer has been written at least once.
  226. WaitForData(buffer);
  227. // It should not have issued our callback.
  228. EXPECT_FALSE(listener.has_user_gesture());
  229. // Set a button down.
  230. mock_data_fetcher_->SetTestData(button_down_data);
  231. // The user gesture listener callback is not called until after the buffer has
  232. // been updated. Wait for the second update to ensure callbacks have fired.
  233. WaitForDataAndCallbacksIssued(buffer);
  234. // It should have issued our callback.
  235. base::RunLoop().RunUntilIdle();
  236. EXPECT_TRUE(listener.has_user_gesture());
  237. }
  238. // Tests that waiting for a user gesture works properly.
  239. TEST_F(GamepadProviderTest, Sanitization) {
  240. Gamepads active_data;
  241. active_data.items[0].connected = true;
  242. active_data.items[0].timestamp = 0;
  243. active_data.items[0].buttons_length = 1;
  244. active_data.items[0].axes_length = 1;
  245. active_data.items[0].buttons[0].value = 1.0f;
  246. active_data.items[0].buttons[0].pressed = true;
  247. active_data.items[0].axes[0] = -1.0f;
  248. Gamepads zero_data;
  249. zero_data.items[0].connected = true;
  250. zero_data.items[0].timestamp = 0;
  251. zero_data.items[0].buttons_length = 1;
  252. zero_data.items[0].axes_length = 1;
  253. zero_data.items[0].buttons[0].value = 0.0f;
  254. zero_data.items[0].buttons[0].pressed = false;
  255. zero_data.items[0].axes[0] = 0.0f;
  256. UserGestureListener listener;
  257. GamepadProvider* provider = CreateProvider(active_data);
  258. provider->SetSanitizationEnabled(true);
  259. provider->Resume();
  260. base::RunLoop().RunUntilIdle();
  261. // Renderer-side, pull data out of poll buffer.
  262. base::ReadOnlySharedMemoryRegion region =
  263. provider->DuplicateSharedMemoryRegion();
  264. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  265. ASSERT_TRUE(mapping.IsValid());
  266. const GamepadHardwareBuffer* buffer =
  267. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  268. // Wait until the shared memory buffer has been written at least once.
  269. WaitForData(buffer);
  270. Gamepads output;
  271. ReadGamepadHardwareBuffer(buffer, &output);
  272. // Initial data should all be zeroed out due to sanitization, even though the
  273. // gamepad reported input
  274. ASSERT_EQ(1u, output.items[0].buttons_length);
  275. EXPECT_EQ(0.0f, output.items[0].buttons[0].value);
  276. EXPECT_FALSE(output.items[0].buttons[0].pressed);
  277. ASSERT_EQ(1u, output.items[0].axes_length);
  278. EXPECT_EQ(0.0f, output.items[0].axes[0]);
  279. // Zero out the inputs
  280. mock_data_fetcher_->SetTestData(zero_data);
  281. WaitForDataAndCallbacksIssued(buffer);
  282. // Read updated data from shared memory
  283. ReadGamepadHardwareBuffer(buffer, &output);
  284. // Should still read zero, which is now an accurate reflection of the data
  285. ASSERT_EQ(1u, output.items[0].buttons_length);
  286. EXPECT_EQ(0.0f, output.items[0].buttons[0].value);
  287. EXPECT_FALSE(output.items[0].buttons[0].pressed);
  288. ASSERT_EQ(1u, output.items[0].axes_length);
  289. EXPECT_EQ(0.0f, output.items[0].axes[0]);
  290. // Re-set the active inputs
  291. mock_data_fetcher_->SetTestData(active_data);
  292. WaitForDataAndCallbacksIssued(buffer);
  293. // Read updated data from shared memory
  294. ReadGamepadHardwareBuffer(buffer, &output);
  295. // Should now accurately reflect the reported data.
  296. ASSERT_EQ(1u, output.items[0].buttons_length);
  297. EXPECT_EQ(1.0f, output.items[0].buttons[0].value);
  298. EXPECT_TRUE(output.items[0].buttons[0].pressed);
  299. ASSERT_EQ(1u, output.items[0].axes_length);
  300. EXPECT_EQ(-1.0f, output.items[0].axes[0]);
  301. }
  302. TEST_F(GamepadProviderTest, SendEvents) {
  303. // This is a test for the logic that is currently behind this flag.
  304. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  305. switches::kEnableGamepadButtonAxisEvents);
  306. Gamepads test_data;
  307. test_data.items[0].connected = true;
  308. test_data.items[0].timestamp = 0;
  309. test_data.items[0].axes_length = 2;
  310. test_data.items[0].axes[0] = -1.0f;
  311. test_data.items[0].axes[1] = 0.5f;
  312. test_data.items[0].buttons[0].value = 0.0f;
  313. test_data.items[0].buttons[0].pressed = false;
  314. test_data.items[0].buttons[1].value = 0.7f;
  315. test_data.items[0].buttons[1].pressed = true;
  316. test_data.items[0].buttons[2].value = 0.0f;
  317. test_data.items[0].buttons[2].pressed = false;
  318. test_data.items[0].buttons[3].value = 0.0f;
  319. test_data.items[0].buttons[3].pressed = false;
  320. test_data.items[0].buttons_length = 4;
  321. test_data.items[0].axes_length = 2;
  322. test_data.items[1].connected = true;
  323. test_data.items[1].timestamp = 0;
  324. test_data.items[1].axes_length = 2;
  325. test_data.items[1].axes[0] = 1.0f;
  326. test_data.items[1].axes[1] = -0.5f;
  327. test_data.items[1].buttons[0].value = 0.0f;
  328. test_data.items[1].buttons[0].pressed = false;
  329. test_data.items[1].buttons[1].value = 1.0f;
  330. test_data.items[1].buttons[1].pressed = true;
  331. test_data.items[1].buttons[2].value = 1.0f;
  332. test_data.items[1].buttons[2].pressed = true;
  333. test_data.items[1].buttons_length = 3;
  334. test_data.items[1].axes_length = 2;
  335. Gamepads test_data_changed = test_data;
  336. test_data_changed.items[0].axes[1] = -0.5f;
  337. test_data_changed.items[0].buttons[0].value = 0.4f;
  338. test_data_changed.items[0].buttons[1].value = 0.2f;
  339. test_data_changed.items[0].buttons[1].pressed = false;
  340. test_data_changed.items[0].buttons[3].value = 0.2f;
  341. test_data_changed.items[1].axes[0] = 0.5f;
  342. test_data_changed.items[1].buttons[0].value = 1.0f;
  343. test_data_changed.items[1].buttons[0].pressed = true;
  344. test_data_changed.items[1].buttons[2].value = 0.9f;
  345. GamepadProvider* provider = CreateProvider(test_data);
  346. provider->SetSanitizationEnabled(false);
  347. provider->Resume();
  348. base::RunLoop().RunUntilIdle();
  349. // Renderer-side, pull data out of poll buffer.
  350. base::ReadOnlySharedMemoryRegion region =
  351. provider->DuplicateSharedMemoryRegion();
  352. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  353. EXPECT_TRUE(mapping.IsValid());
  354. const GamepadHardwareBuffer* buffer =
  355. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  356. // Wait until the shared memory buffer has been written at least once.
  357. WaitForData(buffer);
  358. mock_data_fetcher_->SetTestData(test_data_changed);
  359. // Wait for changes to take place and events to fire.
  360. WaitForDataAndCallbacksIssued(buffer);
  361. change_client_.RunUntilChangeEvents(2);
  362. const auto& changes = change_client_.all_changes();
  363. // Ensure the |button_changes| and |axis_changes| objects have all the
  364. // expected values.
  365. ASSERT_EQ(2u, changes.size());
  366. ASSERT_EQ(1u, changes[1]->axis_changes.size());
  367. ASSERT_EQ(1u, changes[0]->axis_changes.size());
  368. ASSERT_EQ(2u, changes[1]->button_changes.size());
  369. ASSERT_EQ(3u, changes[0]->button_changes.size());
  370. EXPECT_EQ(1u, changes[0]->axis_changes[0]->axis_index);
  371. EXPECT_EQ(-0.5f, changes[0]->axis_changes[0]->axis_snapshot);
  372. EXPECT_EQ(0u, changes[0]->button_changes[0]->button_index);
  373. EXPECT_FALSE(changes[0]->button_changes[0]->button_up);
  374. EXPECT_FALSE(changes[0]->button_changes[0]->button_down);
  375. EXPECT_TRUE(changes[0]->button_changes[0]->value_changed);
  376. EXPECT_EQ(changes[0]->button_changes[0]->button_snapshot,
  377. test_data_changed.items[0].buttons[0]);
  378. EXPECT_EQ(1u, changes[0]->button_changes[1]->button_index);
  379. EXPECT_TRUE(changes[0]->button_changes[1]->button_up);
  380. EXPECT_FALSE(changes[0]->button_changes[1]->button_down);
  381. EXPECT_TRUE(changes[0]->button_changes[1]->value_changed);
  382. EXPECT_EQ(changes[0]->button_changes[1]->button_snapshot,
  383. test_data_changed.items[0].buttons[1]);
  384. EXPECT_EQ(3u, changes[0]->button_changes[2]->button_index);
  385. EXPECT_FALSE(changes[0]->button_changes[2]->button_up);
  386. EXPECT_FALSE(changes[0]->button_changes[2]->button_down);
  387. EXPECT_TRUE(changes[0]->button_changes[2]->value_changed);
  388. EXPECT_EQ(changes[0]->button_changes[2]->button_snapshot,
  389. test_data_changed.items[0].buttons[3]);
  390. EXPECT_EQ(0u, changes[1]->axis_changes[0]->axis_index);
  391. EXPECT_EQ(0.5f, changes[1]->axis_changes[0]->axis_snapshot);
  392. EXPECT_EQ(0u, changes[1]->button_changes[0]->button_index);
  393. EXPECT_FALSE(changes[1]->button_changes[0]->button_up);
  394. EXPECT_TRUE(changes[1]->button_changes[0]->button_down);
  395. EXPECT_TRUE(changes[1]->button_changes[0]->value_changed);
  396. EXPECT_EQ(changes[1]->button_changes[0]->button_snapshot,
  397. test_data_changed.items[1].buttons[0]);
  398. EXPECT_EQ(2u, changes[1]->button_changes[1]->button_index);
  399. EXPECT_FALSE(changes[1]->button_changes[1]->button_up);
  400. EXPECT_FALSE(changes[1]->button_changes[1]->button_down);
  401. EXPECT_TRUE(changes[1]->button_changes[1]->value_changed);
  402. EXPECT_EQ(changes[1]->button_changes[1]->button_snapshot,
  403. test_data_changed.items[1].buttons[2]);
  404. }
  405. TEST_F(GamepadProviderTest, DontSendEventsBeforeUserGesture) {
  406. // This is a test for the logic that is currently behind this flag.
  407. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  408. switches::kEnableGamepadButtonAxisEvents);
  409. Gamepads test_data;
  410. test_data.items[0].connected = true;
  411. test_data.items[0].timestamp = 0;
  412. test_data.items[0].axes_length = 2;
  413. test_data.items[0].axes[0] = 0.0f;
  414. test_data.items[0].buttons[0].value = 0.0f;
  415. test_data.items[0].buttons[0].pressed = false;
  416. test_data.items[0].buttons_length = 1;
  417. test_data.items[0].axes_length = 1;
  418. Gamepads test_data_changed = test_data;
  419. test_data_changed.items[0].axes[1] = 0.4f;
  420. test_data_changed.items[0].buttons[0].value = 0.3f;
  421. GamepadProvider* provider = CreateProvider(test_data);
  422. provider->SetSanitizationEnabled(false);
  423. provider->Resume();
  424. base::RunLoop().RunUntilIdle();
  425. // Renderer-side, pull data out of poll buffer.
  426. base::ReadOnlySharedMemoryRegion region =
  427. provider->DuplicateSharedMemoryRegion();
  428. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  429. EXPECT_TRUE(mapping.IsValid());
  430. const GamepadHardwareBuffer* buffer =
  431. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  432. // Wait until the shared memory buffer has been written at least once.
  433. WaitForData(buffer);
  434. mock_data_fetcher_->SetTestData(test_data_changed);
  435. // Wait for changes to take place and allow potential events to fire.
  436. WaitForDataAndCallbacksIssued(buffer);
  437. change_client_.RunUntilChangeEvents(0);
  438. EXPECT_TRUE(change_client_.all_changes().empty());
  439. }
  440. TEST_F(GamepadProviderTest, DontSendEventsWhenDisconnected) {
  441. // This is a test for the logic that is currently behind this flag.
  442. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  443. switches::kEnableGamepadButtonAxisEvents);
  444. Gamepads test_data;
  445. test_data.items[0].connected = false;
  446. test_data.items[0].timestamp = 0;
  447. test_data.items[0].axes[0] = 0.0f;
  448. test_data.items[0].buttons[0].value = 1.0f;
  449. test_data.items[0].buttons[0].pressed = true;
  450. test_data.items[0].buttons_length = 1;
  451. test_data.items[0].axes_length = 1;
  452. test_data.items[1].connected = true;
  453. test_data.items[1].timestamp = 0;
  454. test_data.items[1].axes[0] = 1.0f;
  455. test_data.items[1].buttons[0].value = 0.0f;
  456. test_data.items[1].buttons[0].pressed = false;
  457. test_data.items[1].buttons_length = 1;
  458. test_data.items[1].axes_length = 1;
  459. Gamepads test_data_changed = test_data;
  460. test_data_changed.items[0].axes[0] = 1.0f;
  461. test_data_changed.items[0].buttons[0].value = 0.0f;
  462. test_data_changed.items[0].buttons[0].pressed = false;
  463. test_data_changed.items[1].connected = false;
  464. test_data_changed.items[1].axes[0] = 0.0f;
  465. test_data_changed.items[1].buttons[0].value = 1.0f;
  466. test_data_changed.items[1].buttons[0].pressed = true;
  467. GamepadProvider* provider = CreateProvider(test_data);
  468. provider->SetSanitizationEnabled(false);
  469. provider->Resume();
  470. base::RunLoop().RunUntilIdle();
  471. // Renderer-side, pull data out of poll buffer.
  472. base::ReadOnlySharedMemoryRegion region =
  473. provider->DuplicateSharedMemoryRegion();
  474. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  475. EXPECT_TRUE(mapping.IsValid());
  476. const GamepadHardwareBuffer* buffer =
  477. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  478. // Wait until the shared memory buffer has been written at least once.
  479. WaitForData(buffer);
  480. mock_data_fetcher_->SetTestData(test_data_changed);
  481. // Wait for changes to take place and allow potential events to fire.
  482. WaitForDataAndCallbacksIssued(buffer);
  483. change_client_.RunUntilChangeEvents(0);
  484. EXPECT_TRUE(change_client_.all_changes().empty());
  485. }
  486. TEST_F(GamepadProviderTest, DontSendEventsOnConnection) {
  487. // This is a test for the logic that is currently behind this flag.
  488. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  489. switches::kEnableGamepadButtonAxisEvents);
  490. Gamepads test_data;
  491. test_data.items[0].connected = true;
  492. test_data.items[0].timestamp = 0;
  493. test_data.items[0].axes[0] = 0.0f;
  494. test_data.items[0].buttons[0].value = 1.0f;
  495. test_data.items[0].buttons[0].pressed = true;
  496. test_data.items[0].buttons_length = 1;
  497. test_data.items[0].axes_length = 1;
  498. test_data.items[1].connected = false;
  499. test_data.items[1].timestamp = 0;
  500. test_data.items[1].axes[0] = 0.0f;
  501. test_data.items[1].buttons[0].value = 1.0f;
  502. test_data.items[1].buttons[0].pressed = true;
  503. test_data.items[1].buttons_length = 1;
  504. test_data.items[1].axes_length = 1;
  505. Gamepads test_data_changed = test_data;
  506. test_data_changed.items[1].connected = true;
  507. test_data_changed.items[1].axes[0] = 1.0f;
  508. test_data_changed.items[1].buttons[0].value = 0.0f;
  509. test_data_changed.items[1].buttons[0].pressed = false;
  510. GamepadProvider* provider = CreateProvider(test_data);
  511. provider->SetSanitizationEnabled(false);
  512. provider->Resume();
  513. base::RunLoop().RunUntilIdle();
  514. // Renderer-side, pull data out of poll buffer.
  515. base::ReadOnlySharedMemoryRegion region =
  516. provider->DuplicateSharedMemoryRegion();
  517. base::ReadOnlySharedMemoryMapping mapping = region.Map();
  518. EXPECT_TRUE(mapping.IsValid());
  519. const GamepadHardwareBuffer* buffer =
  520. static_cast<const GamepadHardwareBuffer*>(mapping.memory());
  521. // Wait until the shared memory buffer has been written at least once.
  522. WaitForData(buffer);
  523. mock_data_fetcher_->SetTestData(test_data_changed);
  524. // Wait for changes to take place and allow potential events to fire.
  525. WaitForDataAndCallbacksIssued(buffer);
  526. change_client_.RunUntilChangeEvents(0);
  527. EXPECT_TRUE(change_client_.all_changes().empty());
  528. }
  529. } // namespace
  530. } // namespace device