OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "remoting/host/linux/x11_key_mapper.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
| 11 #include "remoting/host/linux/mock_x11_keyboard.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using namespace testing; |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 class XServerKeyMapperTest : public Test { |
| 19 public: |
| 20 void SetUp() override; |
| 21 void TearDown() override; |
| 22 |
| 23 protected: |
| 24 // Expect that |mapper_| returns |keycode| when trying to map |code_point|. |
| 25 void ExpectReturnKeyMapping(uint32_t code_point, uint32_t keycode); |
| 26 |
| 27 void ExpectNeedToRetryMappingLater(uint32_t code_point); |
| 28 |
| 29 std::vector<uint32_t> available_keycodes_; |
| 30 MockX11Keyboard mock_keyboard_; |
| 31 |
| 32 std::unique_ptr<X11KeyMapper> mapper_; |
| 33 base::SimpleTestTickClock* mock_clock_; // owned by |mapper_|. |
| 34 }; |
| 35 |
| 36 void XServerKeyMapperTest::SetUp() { |
| 37 available_keycodes_.push_back(55); |
| 38 available_keycodes_.push_back(54); |
| 39 available_keycodes_.push_back(53); |
| 40 available_keycodes_.push_back(52); |
| 41 available_keycodes_.push_back(51); |
| 42 |
| 43 EXPECT_CALL(mock_keyboard_, GetUnusedKeycodes()) |
| 44 .WillOnce(Return(available_keycodes_)); |
| 45 |
| 46 mapper_.reset(new X11KeyMapper(&mock_keyboard_)); |
| 47 mock_clock_ = new base::SimpleTestTickClock(); |
| 48 mapper_->SetClockForTesting(base::WrapUnique((mock_clock_))); |
| 49 } |
| 50 |
| 51 void XServerKeyMapperTest::TearDown() { |
| 52 mapper_.reset(); |
| 53 } |
| 54 |
| 55 void XServerKeyMapperTest::ExpectReturnKeyMapping(uint32_t code_point, |
| 56 uint32_t keycode) { |
| 57 X11KeyMapper::MapResult result = mapper_->MapCharacter(code_point); |
| 58 EXPECT_TRUE(result.success); |
| 59 EXPECT_EQ(keycode, result.keycode); |
| 60 EXPECT_EQ(base::TimeDelta(), result.retry_after); |
| 61 } |
| 62 |
| 63 void XServerKeyMapperTest::ExpectNeedToRetryMappingLater(uint32_t code_point) { |
| 64 X11KeyMapper::MapResult result = mapper_->MapCharacter(code_point); |
| 65 |
| 66 EXPECT_FALSE(result.success); |
| 67 EXPECT_GT(result.retry_after, base::TimeDelta()); |
| 68 } |
| 69 |
| 70 TEST_F(XServerKeyMapperTest, TestNoResetIfNoUse) { |
| 71 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)).Times(0); |
| 72 } |
| 73 |
| 74 TEST_F(XServerKeyMapperTest, TestAddOneCharacter) { |
| 75 EXPECT_CALL(mock_keyboard_, FindKeycode(123, _, _)).WillOnce(Return(false)); |
| 76 |
| 77 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 123)).WillOnce(Return(true)); |
| 78 |
| 79 ExpectReturnKeyMapping(123, 55); |
| 80 |
| 81 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 0)).WillOnce(Return(true)); |
| 82 } |
| 83 |
| 84 TEST_F(XServerKeyMapperTest, TestAddCharactersUntilFull) { |
| 85 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 86 .Times(5) |
| 87 .WillRepeatedly(Return(false)); |
| 88 |
| 89 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
| 90 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(54, 2)).WillOnce(Return(true)); |
| 91 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(53, 3)).WillOnce(Return(true)); |
| 92 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(52, 4)).WillOnce(Return(true)); |
| 93 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(51, 5)).WillOnce(Return(true)); |
| 94 |
| 95 ExpectReturnKeyMapping(1, 55); |
| 96 ExpectReturnKeyMapping(2, 54); |
| 97 ExpectReturnKeyMapping(3, 53); |
| 98 ExpectReturnKeyMapping(4, 52); |
| 99 ExpectReturnKeyMapping(5, 51); |
| 100 |
| 101 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)).Times(5); |
| 102 } |
| 103 |
| 104 TEST_F(XServerKeyMapperTest, TestAddOneCharacterWhenFull) { |
| 105 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 106 .Times(7) |
| 107 .WillRepeatedly(Return(false)); |
| 108 |
| 109 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
| 110 .Times(5) |
| 111 .WillRepeatedly(Return(true)); |
| 112 |
| 113 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 6)).WillOnce(Return(true)); |
| 114 |
| 115 mapper_->MapCharacter(1); |
| 116 mapper_->MapCharacter(2); |
| 117 mapper_->MapCharacter(3); |
| 118 mapper_->MapCharacter(4); |
| 119 mapper_->MapCharacter(5); |
| 120 |
| 121 ExpectNeedToRetryMappingLater(6); |
| 122 |
| 123 mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
| 124 ExpectReturnKeyMapping(6, 55); |
| 125 |
| 126 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
| 127 .Times(5) |
| 128 .WillRepeatedly(Return(true)); |
| 129 } |
| 130 |
| 131 ACTION_P(ReturnKeycode, keycode) { |
| 132 *arg1 = keycode; |
| 133 *arg2 = 0; |
| 134 return true; |
| 135 } |
| 136 |
| 137 TEST_F(XServerKeyMapperTest, TestReuseMappedCharacter) { |
| 138 InSequence sequence; |
| 139 EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(Return(false)); |
| 140 |
| 141 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
| 142 |
| 143 EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(ReturnKeycode(55)); |
| 144 |
| 145 ExpectReturnKeyMapping(1, 55); |
| 146 ExpectReturnKeyMapping(1, 55); |
| 147 |
| 148 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 0)).WillOnce(Return(true)); |
| 149 } |
| 150 |
| 151 TEST_F(XServerKeyMapperTest, TestReuseingCharacterResetsExpiration) { |
| 152 { |
| 153 InSequence sequence; |
| 154 EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(Return(false)); |
| 155 |
| 156 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
| 157 |
| 158 EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)) |
| 159 .WillOnce(ReturnKeycode(55)); |
| 160 |
| 161 ExpectReturnKeyMapping(1, 55); |
| 162 mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
| 163 ExpectReturnKeyMapping(1, 55); |
| 164 } |
| 165 |
| 166 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 167 .Times(6) |
| 168 .WillRepeatedly(Return(false)); |
| 169 |
| 170 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
| 171 .Times(5) |
| 172 .WillRepeatedly(Return(true)); |
| 173 |
| 174 mapper_->MapCharacter(2); |
| 175 mapper_->MapCharacter(3); |
| 176 mapper_->MapCharacter(4); |
| 177 mapper_->MapCharacter(5); |
| 178 |
| 179 ExpectNeedToRetryMappingLater(6); |
| 180 |
| 181 mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
| 182 ExpectReturnKeyMapping(6, 55); |
| 183 |
| 184 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
| 185 .Times(5) |
| 186 .WillRepeatedly(Return(true)); |
| 187 } |
| 188 |
| 189 TEST_F(XServerKeyMapperTest, TestOrderAfterReusingCharacters) { |
| 190 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 191 .Times(5) |
| 192 .WillRepeatedly(Return(false)); |
| 193 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
| 194 .Times(5) |
| 195 .WillRepeatedly(Return(true)); |
| 196 |
| 197 mapper_->MapCharacter(1); |
| 198 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 199 mapper_->MapCharacter(2); |
| 200 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 201 mapper_->MapCharacter(3); |
| 202 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 203 mapper_->MapCharacter(4); |
| 204 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 205 mapper_->MapCharacter(5); |
| 206 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 207 |
| 208 // keycode = 56 - code_point |
| 209 |
| 210 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 211 .Times(5) |
| 212 .WillOnce(ReturnKeycode(54)) |
| 213 .WillOnce(ReturnKeycode(52)) |
| 214 .WillOnce(ReturnKeycode(51)) |
| 215 .WillOnce(ReturnKeycode(55)) |
| 216 .WillOnce(ReturnKeycode(53)); |
| 217 |
| 218 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)).Times(0); |
| 219 |
| 220 ExpectReturnKeyMapping(2, 54); |
| 221 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 222 ExpectReturnKeyMapping(4, 52); |
| 223 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 224 ExpectReturnKeyMapping(5, 51); |
| 225 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 226 ExpectReturnKeyMapping(1, 55); |
| 227 mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 228 ExpectReturnKeyMapping(3, 53); |
| 229 |
| 230 mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
| 231 |
| 232 EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
| 233 .Times(5) |
| 234 .WillRepeatedly(Return(false)); |
| 235 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
| 236 .Times(5) |
| 237 .WillRepeatedly(Return(true)); |
| 238 |
| 239 ExpectReturnKeyMapping(31, 54); |
| 240 ExpectReturnKeyMapping(32, 52); |
| 241 ExpectReturnKeyMapping(33, 51); |
| 242 ExpectReturnKeyMapping(34, 55); |
| 243 ExpectReturnKeyMapping(35, 53); |
| 244 |
| 245 EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
| 246 .Times(5) |
| 247 .WillRepeatedly(Return(true)); |
| 248 } |
| 249 |
| 250 } // namespace remoting |
OLD | NEW |