OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/chromedriver/key_converter.h" | 5 #include "chrome/test/chromedriver/key_converter.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/test/chromedriver/chrome/status.h" | 10 #include "chrome/test/chromedriver/chrome/status.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 ui::VKEY_F10, | 90 ui::VKEY_F10, |
91 ui::VKEY_F11, | 91 ui::VKEY_F11, |
92 ui::VKEY_F12}; | 92 ui::VKEY_F12}; |
93 | 93 |
94 const char16 kWebDriverNullKey = 0xE000U; | 94 const char16 kWebDriverNullKey = 0xE000U; |
95 const char16 kWebDriverShiftKey = 0xE008U; | 95 const char16 kWebDriverShiftKey = 0xE008U; |
96 const char16 kWebDriverControlKey = 0xE009U; | 96 const char16 kWebDriverControlKey = 0xE009U; |
97 const char16 kWebDriverAltKey = 0xE00AU; | 97 const char16 kWebDriverAltKey = 0xE00AU; |
98 const char16 kWebDriverCommandKey = 0xE03DU; | 98 const char16 kWebDriverCommandKey = 0xE03DU; |
99 | 99 |
100 // Returns whether the given key code has a corresponding printable char. | |
101 // Notice: The given key code should be a special WebDriver key code. | |
102 bool IsPrintable(ui::KeyboardCode key_code) { | |
kkania
2013/04/15 21:35:35
How about rename this IsSpecialKeyPrintable?
chrisgao (Use stgao instead)
2013/04/15 23:57:01
Done.
| |
103 return key_code == ui::VKEY_TAB || key_code == ui::VKEY_SPACE || | |
104 key_code == ui::VKEY_OEM_1 || key_code == ui::VKEY_OEM_PLUS || | |
105 key_code == ui::VKEY_OEM_COMMA || | |
106 (key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_DIVIDE); | |
107 } | |
108 | |
100 // Returns whether the given key is a WebDriver key modifier. | 109 // Returns whether the given key is a WebDriver key modifier. |
101 bool IsModifierKey(char16 key) { | 110 bool IsModifierKey(char16 key) { |
102 switch (key) { | 111 switch (key) { |
103 case kWebDriverShiftKey: | 112 case kWebDriverShiftKey: |
104 case kWebDriverControlKey: | 113 case kWebDriverControlKey: |
105 case kWebDriverAltKey: | 114 case kWebDriverAltKey: |
106 case kWebDriverCommandKey: | 115 case kWebDriverCommandKey: |
107 return true; | 116 return true; |
108 default: | 117 default: |
109 return false; | 118 return false; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 key_events.push_back(CreateKeyUpEvent(key_code, sticky_modifiers)); | 244 key_events.push_back(CreateKeyUpEvent(key_code, sticky_modifiers)); |
236 continue; | 245 continue; |
237 } | 246 } |
238 | 247 |
239 ui::KeyboardCode key_code = ui::VKEY_UNKNOWN; | 248 ui::KeyboardCode key_code = ui::VKEY_UNKNOWN; |
240 std::string unmodified_text, modified_text; | 249 std::string unmodified_text, modified_text; |
241 int all_modifiers = sticky_modifiers; | 250 int all_modifiers = sticky_modifiers; |
242 | 251 |
243 // Get the key code, text, and modifiers for the given key. | 252 // Get the key code, text, and modifiers for the given key. |
244 bool should_skip = false; | 253 bool should_skip = false; |
245 if (KeyCodeFromSpecialWebDriverKey(key, &key_code) || | 254 bool is_special_key = KeyCodeFromSpecialWebDriverKey(key, &key_code); |
255 if (is_special_key || | |
246 KeyCodeFromShorthandKey(key, &key_code, &should_skip)) { | 256 KeyCodeFromShorthandKey(key, &key_code, &should_skip)) { |
247 if (should_skip) | 257 if (should_skip) |
248 continue; | 258 continue; |
249 if (key_code == ui::VKEY_UNKNOWN) { | 259 if (key_code == ui::VKEY_UNKNOWN) { |
250 return Status(kUnknownError, base::StringPrintf( | 260 return Status(kUnknownError, base::StringPrintf( |
251 "unknown WebDriver key(%d) at string index (%" PRIuS ")", | 261 "unknown WebDriver key(%d) at string index (%" PRIuS ")", |
252 static_cast<int>(key), | 262 static_cast<int>(key), |
253 i)); | 263 i)); |
254 } | 264 } |
255 if (key_code == ui::VKEY_RETURN) { | 265 if (key_code == ui::VKEY_RETURN) { |
256 // For some reason Chrome expects a carriage return for the return key. | 266 // For some reason Chrome expects a carriage return for the return key. |
257 modified_text = unmodified_text = "\r"; | 267 modified_text = unmodified_text = "\r"; |
268 } else if (is_special_key && !IsPrintable(key_code)) { | |
269 modified_text = unmodified_text = ""; | |
kkania
2013/04/15 21:35:35
Explain why this is needed, perhaps give an exampl
chrisgao (Use stgao instead)
2013/04/15 23:57:01
Done.
| |
258 } else { | 270 } else { |
259 // WebDriver assumes a numpad key should translate to the number, | 271 // WebDriver assumes a numpad key should translate to the number, |
260 // which requires NumLock to be on with some platforms. This isn't | 272 // which requires NumLock to be on with some platforms. This isn't |
261 // formally in the spec, but is expected by their tests. | 273 // formally in the spec, but is expected by their tests. |
262 int webdriver_modifiers = 0; | 274 int webdriver_modifiers = 0; |
263 if (key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_NUMPAD9) | 275 if (key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_NUMPAD9) |
264 webdriver_modifiers = kNumLockKeyModifierMask; | 276 webdriver_modifiers = kNumLockKeyModifierMask; |
265 unmodified_text = ConvertKeyCodeToText(key_code, webdriver_modifiers); | 277 unmodified_text = ConvertKeyCodeToText(key_code, webdriver_modifiers); |
266 modified_text = ConvertKeyCodeToText( | 278 modified_text = ConvertKeyCodeToText( |
267 key_code, | 279 key_code, |
268 all_modifiers | webdriver_modifiers); | 280 all_modifiers | webdriver_modifiers); |
269 } | 281 } |
270 } else { | 282 } else { |
271 int necessary_modifiers = 0; | 283 int necessary_modifiers = 0; |
272 ConvertCharToKeyCode(key, &key_code, &necessary_modifiers); | 284 ConvertCharToKeyCode(key, &key_code, &necessary_modifiers); |
273 all_modifiers |= necessary_modifiers; | 285 all_modifiers |= necessary_modifiers; |
274 if (key_code != ui::VKEY_UNKNOWN) { | 286 if (key_code != ui::VKEY_UNKNOWN) { |
275 unmodified_text = ConvertKeyCodeToText(key_code, 0); | 287 unmodified_text = ConvertKeyCodeToText(key_code, 0); |
276 modified_text = ConvertKeyCodeToText(key_code, all_modifiers); | 288 modified_text = ConvertKeyCodeToText(key_code, all_modifiers); |
277 } | 289 if (unmodified_text.empty() || modified_text.empty()) { |
278 | 290 unmodified_text.clear(); |
kkania
2013/04/15 21:35:35
explain why this is needed, perhaps give an exampl
chrisgao (Use stgao instead)
2013/04/15 23:57:01
Done.
| |
279 if (unmodified_text.empty() || modified_text.empty()) { | 291 modified_text.clear(); |
292 } | |
293 } else { | |
280 // Do a best effort and use the raw key we were given. | 294 // Do a best effort and use the raw key we were given. |
281 if (unmodified_text.empty()) | 295 unmodified_text = UTF16ToUTF8(keys.substr(i, 1)); |
282 unmodified_text = UTF16ToUTF8(keys.substr(i, 1)); | 296 modified_text = UTF16ToUTF8(keys.substr(i, 1)); |
283 if (modified_text.empty()) | |
284 modified_text = UTF16ToUTF8(keys.substr(i, 1)); | |
285 } | 297 } |
286 } | 298 } |
287 | 299 |
288 // Create the key events. | 300 // Create the key events. |
289 bool necessary_modifiers[3]; | 301 bool necessary_modifiers[3]; |
290 for (int i = 0; i < 3; ++i) { | 302 for (int i = 0; i < 3; ++i) { |
291 necessary_modifiers[i] = | 303 necessary_modifiers[i] = |
292 all_modifiers & kModifiers[i].mask && | 304 all_modifiers & kModifiers[i].mask && |
293 !(sticky_modifiers & kModifiers[i].mask); | 305 !(sticky_modifiers & kModifiers[i].mask); |
294 if (necessary_modifiers[i]) { | 306 if (necessary_modifiers[i]) { |
(...skipping 13 matching lines...) Expand all Loading... | |
308 if (necessary_modifiers[i]) { | 320 if (necessary_modifiers[i]) { |
309 key_events.push_back( | 321 key_events.push_back( |
310 CreateKeyUpEvent(kModifiers[i].key_code, sticky_modifiers)); | 322 CreateKeyUpEvent(kModifiers[i].key_code, sticky_modifiers)); |
311 } | 323 } |
312 } | 324 } |
313 } | 325 } |
314 client_key_events->swap(key_events); | 326 client_key_events->swap(key_events); |
315 *modifiers = sticky_modifiers; | 327 *modifiers = sticky_modifiers; |
316 return Status(kOk); | 328 return Status(kOk); |
317 } | 329 } |
OLD | NEW |