1 // Copyright 2014 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.
5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
9 #import "remoting/ios/key_input.h"
10 #import "remoting/ios/key_map_us.h"
14 #import "base/compiler_specific.h"
15 #import "testing/gtest_mac.h"
17 @interface KeyInputDelegateTester : NSObject<KeyInputDelegate> {
19 std::vector<uint32_t> _keyList;
22 @property(nonatomic, assign) int numKeysDown;
23 @property(nonatomic, assign) BOOL wasDismissed;
25 - (std::vector<uint32_t>&)getKeyList;
29 @implementation KeyInputDelegateTester
31 - (std::vector<uint32_t>&)getKeyList {
35 - (void)keyboardDismissed {
36 // This can not be tested, because we can not set |keyInput_| as
37 // FirstResponder in this test harness
41 - (void)keyboardActionKeyCode:(uint32_t)keyPressed isKeyDown:(BOOL)keyDown {
43 _keyList.push_back(keyPressed);
54 class KeyInputTest : public ::testing::Test {
56 virtual void SetUp() OVERRIDE {
57 keyInput_ = [[KeyInput allocWithZone:nil] init];
58 delegateTester_ = [[KeyInputDelegateTester alloc] init];
59 keyInput_.delegate = delegateTester_;
63 KeyInputDelegateTester* delegateTester_;
66 TEST_F(KeyInputTest, SendKey) {
68 [keyInput_ insertText:@""];
69 ASSERT_EQ(0, delegateTester_.numKeysDown);
70 ASSERT_EQ(0, [delegateTester_ getKeyList].size());
72 // Value is out of bounds
73 [keyInput_ insertText:@"รณ"];
74 ASSERT_EQ(0, delegateTester_.numKeysDown);
75 ASSERT_EQ(0, [delegateTester_ getKeyList].size());
78 [keyInput_ insertText:@"a"];
79 ASSERT_EQ(0, delegateTester_.numKeysDown);
80 ASSERT_EQ(1, [delegateTester_ getKeyList].size());
81 ASSERT_EQ(kKeyCodeUS['a'], [delegateTester_ getKeyList][0]);
83 [delegateTester_ getKeyList].clear();
84 [keyInput_ insertText:@"A"];
85 ASSERT_EQ(0, delegateTester_.numKeysDown);
86 ASSERT_EQ(2, [delegateTester_ getKeyList].size());
87 ASSERT_EQ(kKeyCodeUS[kShiftIndex], [delegateTester_ getKeyList][0]);
88 ASSERT_EQ(kKeyCodeUS['A'], [delegateTester_ getKeyList][1]);
90 // Multiple characters and mixed case
91 [delegateTester_ getKeyList].clear();
92 [keyInput_ insertText:@"ABCabc"];
93 ASSERT_EQ(0, delegateTester_.numKeysDown);
94 ASSERT_EQ(9, [delegateTester_ getKeyList].size());
95 ASSERT_EQ(kKeyCodeUS[kShiftIndex], [delegateTester_ getKeyList][0]);
96 ASSERT_EQ(kKeyCodeUS['A'], [delegateTester_ getKeyList][1]);
97 ASSERT_EQ(kKeyCodeUS[kShiftIndex], [delegateTester_ getKeyList][2]);
98 ASSERT_EQ(kKeyCodeUS['B'], [delegateTester_ getKeyList][3]);
99 ASSERT_EQ(kKeyCodeUS[kShiftIndex], [delegateTester_ getKeyList][4]);
100 ASSERT_EQ(kKeyCodeUS['C'], [delegateTester_ getKeyList][5]);
101 ASSERT_EQ(kKeyCodeUS['a'], [delegateTester_ getKeyList][6]);
102 ASSERT_EQ(kKeyCodeUS['b'], [delegateTester_ getKeyList][7]);
103 ASSERT_EQ(kKeyCodeUS['c'], [delegateTester_ getKeyList][8]);
106 TEST_F(KeyInputTest, CtrlAltDel) {
107 [keyInput_ ctrlAltDel];
109 ASSERT_EQ(0, delegateTester_.numKeysDown);
110 ASSERT_EQ(3, [delegateTester_ getKeyList].size());
111 ASSERT_EQ(kKeyCodeUS[kCtrlIndex], [delegateTester_ getKeyList][0]);
112 ASSERT_EQ(kKeyCodeUS[kAltIndex], [delegateTester_ getKeyList][1]);
113 ASSERT_EQ(kKeyCodeUS[kDelIndex], [delegateTester_ getKeyList][2]);
116 TEST_F(KeyInputTest, Backspace) {
117 [keyInput_ deleteBackward];
119 ASSERT_EQ(0, delegateTester_.numKeysDown);
120 ASSERT_EQ(1, [delegateTester_ getKeyList].size());
121 ASSERT_EQ(kKeyCodeUS[kBackspaceIndex], [delegateTester_ getKeyList][0]);
124 } // namespace remoting