Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: chromeos_keyboard_unittest.cc

Issue 6735026: Remove chromeos_keyboard* (Closed) Base URL: http://git.chromium.org/git/cros.git@master
Patch Set: re-upload Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos_keyboard.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium OS 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 // DEPRECATED. TODO(satorux): Remove this file.
6
7 // How to run the test:
8 // $ FEATURES="test" emerge-x86-generic -a libcros
9
10 #include "chromeos_keyboard.h"
11
12 #include <algorithm>
13 #include <set>
14 #include <string>
15
16 #include <gtest/gtest.h>
17 #include <X11/Xlib.h>
18
19 #include "base/logging.h"
20
21 namespace chromeos {
22
23 namespace {
24
25 // Returns a ModifierMap object that contains the following mapping:
26 // - kSearchKey is mapped to |search|.
27 // - kLeftControl key is mapped to |control|.
28 // - kLeftAlt key is mapped to |alt|.
29 ModifierMap GetMap(ModifierKey search, ModifierKey control, ModifierKey alt) {
30 ModifierMap modifier_key;
31 // Use the Search key as |search|.
32 modifier_key.push_back(ModifierKeyPair(kSearchKey, search));
33 modifier_key.push_back(ModifierKeyPair(kLeftControlKey, control));
34 modifier_key.push_back(ModifierKeyPair(kLeftAltKey, alt));
35 return modifier_key;
36 }
37
38 // Checks |modifier_map| and returns true if the following conditions are met:
39 // - kSearchKey is mapped to |search|.
40 // - kLeftControl key is mapped to |control|.
41 // - kLeftAlt key is mapped to |alt|.
42 bool CheckMap(const ModifierMap& modifier_map,
43 ModifierKey search, ModifierKey control, ModifierKey alt) {
44 ModifierMap::const_iterator begin = modifier_map.begin();
45 ModifierMap::const_iterator end = modifier_map.end();
46 if ((std::count(begin, end, ModifierKeyPair(kSearchKey, search)) == 1) &&
47 (std::count(begin, end,
48 ModifierKeyPair(kLeftControlKey, control)) == 1) &&
49 (std::count(begin, end, ModifierKeyPair(kLeftAltKey, alt)) == 1)) {
50 return true;
51 }
52 return false;
53 }
54
55 // Returns true if X display is available.
56 bool DisplayAvailable() {
57 Display* display = XOpenDisplay(NULL);
58 if (!display) {
59 return false;
60 }
61 XCloseDisplay(display);
62 return true;
63 }
64
65 } // namespace
66
67 // Tests CreateFullXkbLayoutName() function.
68 TEST(ChromeOSKeyboardTest, TestCreateFullXkbLayoutNameBasic) {
69 // CreateFullXkbLayoutName should not accept an empty |layout_name|.
70 EXPECT_STREQ("", CreateFullXkbLayoutName(
71 "", GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
72
73 // CreateFullXkbLayoutName should not accept an empty ModifierMap.
74 EXPECT_STREQ("", CreateFullXkbLayoutName("us", ModifierMap()).c_str());
75
76 // CreateFullXkbLayoutName should not accept an incomplete ModifierMap.
77 ModifierMap tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
78 tmp_map.pop_back();
79 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
80
81 // CreateFullXkbLayoutName should not accept redundant ModifierMaps.
82 tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
83 tmp_map.push_back(ModifierKeyPair(kSearchKey, kVoidKey)); // two search maps
84 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
85 tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
86 tmp_map.push_back(ModifierKeyPair(kLeftControlKey, kVoidKey)); // two ctrls
87 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
88 tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
89 tmp_map.push_back(ModifierKeyPair(kLeftAltKey, kVoidKey)); // two alts.
90 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
91
92 // CreateFullXkbLayoutName should not accept invalid ModifierMaps.
93 tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
94 tmp_map.push_back(ModifierKeyPair(kVoidKey, kSearchKey)); // can't remap void
95 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
96 tmp_map = GetMap(kVoidKey, kVoidKey, kVoidKey);
97 tmp_map.push_back(ModifierKeyPair(kCapsLockKey, kSearchKey)); // ditto
98 EXPECT_STREQ("", CreateFullXkbLayoutName("us", tmp_map).c_str());
99
100 // CreateFullXkbLayoutName can remap Search/Ctrl/Alt to CapsLock.
101 EXPECT_STREQ("us+chromeos(capslock_disabled_disabled)",
102 CreateFullXkbLayoutName(
103 "us",
104 GetMap(kCapsLockKey, kVoidKey, kVoidKey)).c_str());
105 EXPECT_STREQ("us+chromeos(disabled_capslock_disabled)",
106 CreateFullXkbLayoutName(
107 "us",
108 GetMap(kVoidKey, kCapsLockKey, kVoidKey)).c_str());
109 EXPECT_STREQ("us+chromeos(disabled_disabled_capslock)",
110 CreateFullXkbLayoutName(
111 "us",
112 GetMap(kVoidKey, kVoidKey, kCapsLockKey)).c_str());
113
114 // CreateFullXkbLayoutName should not accept non-alphanumeric characters
115 // except "()-_".
116 EXPECT_STREQ("", CreateFullXkbLayoutName(
117 "us!", GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
118 EXPECT_STREQ("", CreateFullXkbLayoutName(
119 "us; /bin/sh", GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
120 EXPECT_STREQ("ab-c_12+chromeos(disabled_disabled_disabled),us",
121 CreateFullXkbLayoutName(
122 "ab-c_12",
123 GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
124
125 // CreateFullXkbLayoutName should not accept upper-case ascii characters.
126 EXPECT_STREQ("", CreateFullXkbLayoutName(
127 "US", GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
128
129 // CreateFullXkbLayoutName should accept lower-case ascii characters.
130 for (int c = 'a'; c <= 'z'; ++c) {
131 EXPECT_STRNE("", CreateFullXkbLayoutName(
132 std::string(3, c),
133 GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
134 }
135
136 // CreateFullXkbLayoutName should accept numbers.
137 for (int c = '0'; c <= '9'; ++c) {
138 EXPECT_STRNE("", CreateFullXkbLayoutName(
139 std::string(3, c),
140 GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
141 }
142
143 // CreateFullXkbLayoutName should accept a layout with a variant name.
144 EXPECT_STREQ("us(dvorak)+chromeos(disabled_disabled_disabled)",
145 CreateFullXkbLayoutName(
146 "us(dvorak)",
147 GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
148 EXPECT_STREQ("gb(extd)+chromeos(disabled_disabled_disabled),us",
149 CreateFullXkbLayoutName(
150 "gb(extd)",
151 GetMap(kVoidKey, kVoidKey, kVoidKey)).c_str());
152 EXPECT_STREQ("gb(extd)+", CreateFullXkbLayoutName(
153 "gb(extd)",
154 GetMap(kVoidKey, kVoidKey, kVoidKey)).substr(0, 9).c_str());
155 EXPECT_STREQ("jp+", CreateFullXkbLayoutName(
156 "jp", GetMap(kVoidKey, kVoidKey, kVoidKey)).substr(0, 3).c_str());
157
158 // When the layout name is not "us", the second layout should be added.
159 EXPECT_EQ(-1, CreateFullXkbLayoutName(
160 "us", GetMap(kVoidKey, kVoidKey, kVoidKey)).find(",us"));
161 EXPECT_EQ(-1, CreateFullXkbLayoutName(
162 "us(dvorak)", GetMap(kVoidKey, kVoidKey, kVoidKey)).find(",us"));
163 EXPECT_NE(-1, CreateFullXkbLayoutName(
164 "gb(extd)", GetMap(kVoidKey, kVoidKey, kVoidKey)).find(",us"));
165 EXPECT_NE(-1, CreateFullXkbLayoutName(
166 "jp", GetMap(kVoidKey, kVoidKey, kVoidKey)).find(",us"));
167 }
168
169 // Tests if CreateFullXkbLayoutName and ExtractLayoutNameFromFullXkbLayoutName
170 // functions could handle all combinations of modifier remapping.
171 TEST(ChromeOSKeyboardTest, TestCreateFullXkbLayoutNameModifierKeys) {
172 std::set<std::string> layouts;
173 for (int i = 0; i < static_cast<int>(kNumModifierKeys); ++i) {
174 for (int j = 0; j < static_cast<int>(kNumModifierKeys); ++j) {
175 for (int k = 0; k < static_cast<int>(kNumModifierKeys); ++k) {
176 const std::string layout = CreateFullXkbLayoutName(
177 "us", GetMap(ModifierKey(i), ModifierKey(j), ModifierKey(k)));
178 // CreateFullXkbLayoutName should succeed (i.e. should not return "".)
179 EXPECT_STREQ("us+", layout.substr(0, 3).c_str())
180 << "layout: " << layout;
181 // All 4*3*3 layouts should be different.
182 EXPECT_TRUE(layouts.insert(layout).second) << "layout: " << layout;
183 }
184 }
185 }
186 }
187
188 TEST(ChromeOSKeyboardTest, TestSetCapsLockIsEnabled) {
189 if (!DisplayAvailable()) {
190 return;
191 }
192 const bool initial_lock_state = CapsLockIsEnabled();
193 SetCapsLockEnabled(true);
194 EXPECT_TRUE(CapsLockIsEnabled());
195 SetCapsLockEnabled(false);
196 EXPECT_FALSE(CapsLockIsEnabled());
197 SetCapsLockEnabled(true);
198 EXPECT_TRUE(CapsLockIsEnabled());
199 SetCapsLockEnabled(false);
200 EXPECT_FALSE(CapsLockIsEnabled());
201 SetCapsLockEnabled(initial_lock_state);
202 }
203
204 TEST(ChromeOSKeyboardTest, TestContainsModifierKeyAsReplacement) {
205 EXPECT_FALSE(ContainsModifierKeyAsReplacement(
206 GetMap(kVoidKey, kVoidKey, kVoidKey), kCapsLockKey));
207 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
208 GetMap(kCapsLockKey, kVoidKey, kVoidKey), kCapsLockKey));
209 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
210 GetMap(kVoidKey, kCapsLockKey, kVoidKey), kCapsLockKey));
211 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
212 GetMap(kVoidKey, kVoidKey, kCapsLockKey), kCapsLockKey));
213 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
214 GetMap(kCapsLockKey, kCapsLockKey, kVoidKey), kCapsLockKey));
215 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
216 GetMap(kCapsLockKey, kCapsLockKey, kCapsLockKey), kCapsLockKey));
217 EXPECT_TRUE(ContainsModifierKeyAsReplacement(
218 GetMap(kSearchKey, kVoidKey, kVoidKey), kSearchKey));
219 }
220
221 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos_keyboard.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698