Index: remoting/host/linux/x11_key_mapper_unittest.cc |
diff --git a/remoting/host/linux/x11_key_mapper_unittest.cc b/remoting/host/linux/x11_key_mapper_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f76284859685f168298aa145b9979db072f84dc5 |
--- /dev/null |
+++ b/remoting/host/linux/x11_key_mapper_unittest.cc |
@@ -0,0 +1,250 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/host/linux/x11_key_mapper.h" |
+ |
+#include <memory> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/test/simple_test_tick_clock.h" |
+#include "remoting/host/linux/mock_x11_keyboard.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using namespace testing; |
+ |
+namespace remoting { |
+ |
+class XServerKeyMapperTest : public Test { |
+ public: |
+ void SetUp() override; |
+ void TearDown() override; |
+ |
+ protected: |
+ // Expect that |mapper_| returns |keycode| when trying to map |code_point|. |
+ void ExpectReturnKeyMapping(uint32_t code_point, uint32_t keycode); |
+ |
+ void ExpectNeedToRetryMappingLater(uint32_t code_point); |
+ |
+ std::vector<uint32_t> available_keycodes_; |
+ MockX11Keyboard mock_keyboard_; |
+ |
+ std::unique_ptr<X11KeyMapper> mapper_; |
+ base::SimpleTestTickClock* mock_clock_; // owned by |mapper_|. |
+}; |
+ |
+void XServerKeyMapperTest::SetUp() { |
+ available_keycodes_.push_back(55); |
+ available_keycodes_.push_back(54); |
+ available_keycodes_.push_back(53); |
+ available_keycodes_.push_back(52); |
+ available_keycodes_.push_back(51); |
+ |
+ EXPECT_CALL(mock_keyboard_, GetUnusedKeycodes()) |
+ .WillOnce(Return(available_keycodes_)); |
+ |
+ mapper_.reset(new X11KeyMapper(&mock_keyboard_)); |
+ mock_clock_ = new base::SimpleTestTickClock(); |
+ mapper_->SetClockForTesting(base::WrapUnique((mock_clock_))); |
+} |
+ |
+void XServerKeyMapperTest::TearDown() { |
+ mapper_.reset(); |
+} |
+ |
+void XServerKeyMapperTest::ExpectReturnKeyMapping(uint32_t code_point, |
+ uint32_t keycode) { |
+ X11KeyMapper::MapResult result = mapper_->MapCharacter(code_point); |
+ EXPECT_TRUE(result.success); |
+ EXPECT_EQ(keycode, result.keycode); |
+ EXPECT_EQ(base::TimeDelta(), result.retry_after); |
+} |
+ |
+void XServerKeyMapperTest::ExpectNeedToRetryMappingLater(uint32_t code_point) { |
+ X11KeyMapper::MapResult result = mapper_->MapCharacter(code_point); |
+ |
+ EXPECT_FALSE(result.success); |
+ EXPECT_GT(result.retry_after, base::TimeDelta()); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestNoResetIfNoUse) { |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)).Times(0); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestAddOneCharacter) { |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(123, _, _)).WillOnce(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 123)).WillOnce(Return(true)); |
+ |
+ ExpectReturnKeyMapping(123, 55); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 0)).WillOnce(Return(true)); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestAddCharactersUntilFull) { |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(54, 2)).WillOnce(Return(true)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(53, 3)).WillOnce(Return(true)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(52, 4)).WillOnce(Return(true)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(51, 5)).WillOnce(Return(true)); |
+ |
+ ExpectReturnKeyMapping(1, 55); |
+ ExpectReturnKeyMapping(2, 54); |
+ ExpectReturnKeyMapping(3, 53); |
+ ExpectReturnKeyMapping(4, 52); |
+ ExpectReturnKeyMapping(5, 51); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)).Times(5); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestAddOneCharacterWhenFull) { |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(7) |
+ .WillRepeatedly(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 6)).WillOnce(Return(true)); |
+ |
+ mapper_->MapCharacter(1); |
+ mapper_->MapCharacter(2); |
+ mapper_->MapCharacter(3); |
+ mapper_->MapCharacter(4); |
+ mapper_->MapCharacter(5); |
+ |
+ ExpectNeedToRetryMappingLater(6); |
+ |
+ mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
+ ExpectReturnKeyMapping(6, 55); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+} |
+ |
+ACTION_P(ReturnKeycode, keycode) { |
+ *arg1 = keycode; |
+ *arg2 = 0; |
+ return true; |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestReuseMappedCharacter) { |
+ InSequence sequence; |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
+ |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(ReturnKeycode(55)); |
+ |
+ ExpectReturnKeyMapping(1, 55); |
+ ExpectReturnKeyMapping(1, 55); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 0)).WillOnce(Return(true)); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestReuseingCharacterResetsExpiration) { |
+ { |
+ InSequence sequence; |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)).WillOnce(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(55, 1)).WillOnce(Return(true)); |
+ |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(1, _, _)) |
+ .WillOnce(ReturnKeycode(55)); |
+ |
+ ExpectReturnKeyMapping(1, 55); |
+ mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
+ ExpectReturnKeyMapping(1, 55); |
+ } |
+ |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(6) |
+ .WillRepeatedly(Return(false)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+ |
+ mapper_->MapCharacter(2); |
+ mapper_->MapCharacter(3); |
+ mapper_->MapCharacter(4); |
+ mapper_->MapCharacter(5); |
+ |
+ ExpectNeedToRetryMappingLater(6); |
+ |
+ mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
+ ExpectReturnKeyMapping(6, 55); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+} |
+ |
+TEST_F(XServerKeyMapperTest, TestOrderAfterReusingCharacters) { |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(false)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+ |
+ mapper_->MapCharacter(1); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ mapper_->MapCharacter(2); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ mapper_->MapCharacter(3); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ mapper_->MapCharacter(4); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ mapper_->MapCharacter(5); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ |
+ // keycode = 56 - code_point |
+ |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(5) |
+ .WillOnce(ReturnKeycode(54)) |
+ .WillOnce(ReturnKeycode(52)) |
+ .WillOnce(ReturnKeycode(51)) |
+ .WillOnce(ReturnKeycode(55)) |
+ .WillOnce(ReturnKeycode(53)); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)).Times(0); |
+ |
+ ExpectReturnKeyMapping(2, 54); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ ExpectReturnKeyMapping(4, 52); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ ExpectReturnKeyMapping(5, 51); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ ExpectReturnKeyMapping(1, 55); |
+ mock_clock_->Advance(base::TimeDelta::FromMilliseconds(1)); |
+ ExpectReturnKeyMapping(3, 53); |
+ |
+ mock_clock_->Advance(base::TimeDelta::FromSeconds(10)); |
+ |
+ EXPECT_CALL(mock_keyboard_, FindKeycode(_, _, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(false)); |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, _)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+ |
+ ExpectReturnKeyMapping(31, 54); |
+ ExpectReturnKeyMapping(32, 52); |
+ ExpectReturnKeyMapping(33, 51); |
+ ExpectReturnKeyMapping(34, 55); |
+ ExpectReturnKeyMapping(35, 53); |
+ |
+ EXPECT_CALL(mock_keyboard_, ChangeKeyMapping(_, 0)) |
+ .Times(5) |
+ .WillRepeatedly(Return(true)); |
+} |
+ |
+} // namespace remoting |