x11_character_injector_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2016 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 "remoting/host/linux/x11_character_injector.h"
  5. #include <unordered_map>
  6. #include "base/memory/ptr_util.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "base/time/time.h"
  12. #include "remoting/host/linux/x11_keyboard.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace {
  15. constexpr base::TimeDelta kKeycodeReuseDuration = base::Milliseconds(100);
  16. }
  17. namespace remoting {
  18. // This class acts as a simulated keyboard interface for testing that
  19. // * Maintains a changeable simulated keyboard layout.
  20. // * Verifies the correct sequence of characters are being typed.
  21. // * Ensures that a key mapping can't be changed if the time elapsed since
  22. // last used is less than |kKeycodeReuseDuration|.
  23. class FakeX11Keyboard : public X11Keyboard {
  24. public:
  25. struct MappingInfo {
  26. uint32_t code_point;
  27. base::TimeTicks reusable_at;
  28. };
  29. explicit FakeX11Keyboard(const std::vector<uint32_t>& available_keycodes);
  30. ~FakeX11Keyboard() override;
  31. // X11Keyboard overrides.
  32. std::vector<uint32_t> GetUnusedKeycodes() override;
  33. void PressKey(uint32_t keycode, uint32_t modifiers) override;
  34. bool FindKeycode(uint32_t code_point,
  35. uint32_t* keycode,
  36. uint32_t* modifiers) override;
  37. bool ChangeKeyMapping(uint32_t keycode, uint32_t code_point) override;
  38. void Flush() override;
  39. void Sync() override;
  40. void ExpectEnterCodePoints(const std::vector<uint32_t>& sequence);
  41. // Sets a callback to be called when the keypress expectation queue becomes
  42. // empty.
  43. void SetKeyPressFinishedCallback(const base::RepeatingClosure& callback) {
  44. keypress_finished_callback_ = callback;
  45. }
  46. private:
  47. std::unordered_map<uint32_t, MappingInfo> keycode_mapping_;
  48. base::circular_deque<uint32_t> expected_code_point_sequence_;
  49. base::RepeatingClosure keypress_finished_callback_;
  50. };
  51. FakeX11Keyboard::FakeX11Keyboard(
  52. const std::vector<uint32_t>& available_keycodes) {
  53. for (uint32_t keycode : available_keycodes) {
  54. keycode_mapping_.insert({keycode, {0, base::TimeTicks()}});
  55. }
  56. }
  57. FakeX11Keyboard::~FakeX11Keyboard() {
  58. EXPECT_TRUE(expected_code_point_sequence_.empty());
  59. for (const auto& pair : keycode_mapping_) {
  60. EXPECT_EQ(0u, pair.second.code_point);
  61. }
  62. }
  63. std::vector<uint32_t> FakeX11Keyboard::GetUnusedKeycodes() {
  64. std::vector<uint32_t> keycodes;
  65. for (const auto& pair : keycode_mapping_) {
  66. if (!pair.second.code_point) {
  67. keycodes.push_back(pair.first);
  68. }
  69. }
  70. return keycodes;
  71. }
  72. void FakeX11Keyboard::PressKey(uint32_t keycode, uint32_t modifiers) {
  73. ASSERT_FALSE(expected_code_point_sequence_.empty());
  74. uint32_t expected_code_point = expected_code_point_sequence_.front();
  75. auto position = keycode_mapping_.find(keycode);
  76. ASSERT_NE(position, keycode_mapping_.end());
  77. MappingInfo& info = position->second;
  78. EXPECT_EQ(expected_code_point, info.code_point);
  79. info.reusable_at = base::TimeTicks::Now() + kKeycodeReuseDuration;
  80. expected_code_point_sequence_.pop_front();
  81. if (expected_code_point_sequence_.empty() && keypress_finished_callback_) {
  82. keypress_finished_callback_.Run();
  83. }
  84. }
  85. bool FakeX11Keyboard::FindKeycode(uint32_t code_point,
  86. uint32_t* keycode,
  87. uint32_t* modifiers) {
  88. auto position = std::find_if(keycode_mapping_.begin(), keycode_mapping_.end(),
  89. [code_point](const std::pair<uint32_t, MappingInfo>& pair) {
  90. return pair.second.code_point == code_point;
  91. });
  92. if (position == keycode_mapping_.end()) {
  93. return false;
  94. }
  95. *keycode = position->first;
  96. *modifiers = 0;
  97. return true;
  98. }
  99. bool FakeX11Keyboard::ChangeKeyMapping(uint32_t keycode, uint32_t code_point) {
  100. MappingInfo& info = keycode_mapping_[keycode];
  101. info.code_point = code_point;
  102. if (code_point) {
  103. base::TimeTicks now = base::TimeTicks::Now();
  104. EXPECT_LE(info.reusable_at, now)
  105. << "Attempted to reuse a keycode in less than "
  106. << kKeycodeReuseDuration;
  107. info.reusable_at = now + kKeycodeReuseDuration;
  108. } else {
  109. info.reusable_at = base::TimeTicks();
  110. }
  111. return true;
  112. }
  113. void FakeX11Keyboard::Flush() {}
  114. void FakeX11Keyboard::Sync() {}
  115. void FakeX11Keyboard::ExpectEnterCodePoints(
  116. const std::vector<uint32_t>& sequence) {
  117. expected_code_point_sequence_.insert(expected_code_point_sequence_.end(),
  118. sequence.begin(), sequence.end());
  119. }
  120. class X11CharacterInjectorTest : public testing::Test {
  121. public:
  122. void SetUp() override;
  123. void TearDown() override;
  124. protected:
  125. // Injects the characters sequentially, verifies the sequence of characters
  126. // being injected are correct, and runs the message loop until all
  127. // characters are injected.
  128. void InjectAndRun(const std::vector<uint32_t>& code_points);
  129. std::unique_ptr<X11CharacterInjector> injector_;
  130. raw_ptr<FakeX11Keyboard> keyboard_; // Owned by |injector_|.
  131. base::test::SingleThreadTaskEnvironment task_environment_;
  132. };
  133. void X11CharacterInjectorTest::SetUp() {
  134. keyboard_ = new FakeX11Keyboard({55, 54, 53, 52, 51});
  135. injector_.reset(new X11CharacterInjector(base::WrapUnique(keyboard_.get())));
  136. }
  137. void X11CharacterInjectorTest::TearDown() {
  138. injector_.reset();
  139. }
  140. void X11CharacterInjectorTest::InjectAndRun(
  141. const std::vector<uint32_t>& code_points) {
  142. base::RunLoop run_loop;
  143. keyboard_->SetKeyPressFinishedCallback(run_loop.QuitClosure());
  144. for (uint32_t code_point : code_points)
  145. injector_->Inject(code_point);
  146. keyboard_->ExpectEnterCodePoints(code_points);
  147. run_loop.Run();
  148. }
  149. TEST_F(X11CharacterInjectorTest, TestNoMappingNoExpectation) {
  150. }
  151. TEST_F(X11CharacterInjectorTest, TestTypeOneCharacter) {
  152. InjectAndRun({123});
  153. }
  154. TEST_F(X11CharacterInjectorTest, TestMapCharactersUntilFull) {
  155. InjectAndRun({1, 2, 3, 4, 5});
  156. }
  157. TEST_F(X11CharacterInjectorTest, TestMapOneCharacterWhenFull) {
  158. InjectAndRun({1, 2, 3, 4, 5, 6});
  159. }
  160. TEST_F(X11CharacterInjectorTest, TestReuseMappedCharacterOnce) {
  161. InjectAndRun({1, 2, 3, 4, 5});
  162. InjectAndRun({1, 6});
  163. }
  164. TEST_F(X11CharacterInjectorTest, TestReuseAllMappedCharactersInChangedOrder) {
  165. InjectAndRun({1, 2, 3, 4, 5});
  166. InjectAndRun({2, 4, 5, 1, 3});
  167. InjectAndRun({31, 32, 33, 34, 35});
  168. }
  169. } // namespace remoting