Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/webdriver/webdriver_key_converter.h" | 5 #include "chrome/test/webdriver/webdriver_key_converter.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/common/automation_constants.h" | 8 #include "chrome/common/automation_constants.h" |
| 9 #include "chrome/test/automation/automation_json_requests.h" | 9 #include "chrome/test/automation/automation_json_requests.h" |
| 10 #include "chrome/test/webdriver/keycode_text_conversion.h" | 10 #include "chrome/test/webdriver/keycode_text_conversion.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 // be set. | 102 // be set. |
| 103 bool KeyCodeFromSpecialWebDriverKey(char16 key, ui::KeyboardCode* key_code) { | 103 bool KeyCodeFromSpecialWebDriverKey(char16 key, ui::KeyboardCode* key_code) { |
| 104 int index = static_cast<int>(key) - 0xE000U; | 104 int index = static_cast<int>(key) - 0xE000U; |
| 105 bool is_special_key = index >= 0 && | 105 bool is_special_key = index >= 0 && |
| 106 index < static_cast<int>(arraysize(kSpecialWebDriverKeys)); | 106 index < static_cast<int>(arraysize(kSpecialWebDriverKeys)); |
| 107 if (is_special_key) | 107 if (is_special_key) |
| 108 *key_code = kSpecialWebDriverKeys[index]; | 108 *key_code = kSpecialWebDriverKeys[index]; |
| 109 return is_special_key; | 109 return is_special_key; |
| 110 } | 110 } |
| 111 | 111 |
| 112 // Gets the key code associated with |key|, if it is a special shorthand key, | |
| 113 // Shorthand keys are common text equivalents for keys, such as the newline | |
| 114 // character, which is shorthand for the return key. Returns whether |key| is | |
| 115 // a shorthand key. If true, |key_code| will be set and |should_skip| will be | |
| 116 // set to whether the key should be skipped. | |
| 117 bool KeyCodeFromShorthandKey(char16 key_utf16, | |
| 118 ui::KeyboardCode* key_code, | |
| 119 bool* client_should_skip) { | |
| 120 string16 key_str_utf16; | |
| 121 key_str_utf16.push_back(key_utf16); | |
| 122 std::string key_str_utf8 = UTF16ToUTF8(key_str_utf16); | |
| 123 if (key_str_utf8.length() != 1) | |
| 124 return false; | |
| 125 bool should_skip = false; | |
| 126 char key = key_str_utf8[0]; | |
| 127 if (key == '\n') { | |
|
Joe
2011/03/15 21:22:17
While \n is a newline for unix isn't \r\n a newlin
kkania
2011/03/18 21:45:12
Yes, I support \r\n and \n. I do this by skipping
| |
| 128 *key_code = ui::VKEY_RETURN; | |
| 129 } else if (key == '\t') { | |
| 130 *key_code = ui::VKEY_TAB; | |
| 131 } else if (key == '\b') { | |
| 132 *key_code = ui::VKEY_BACK; | |
| 133 } else if (key == ' ') { | |
| 134 *key_code = ui::VKEY_SPACE; | |
| 135 } else if (key == '\r') { | |
| 136 *key_code = ui::VKEY_UNKNOWN; | |
| 137 should_skip = true; | |
| 138 } else { | |
| 139 return false; | |
| 140 } | |
| 141 *client_should_skip = should_skip; | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 112 } // namespace | 145 } // namespace |
| 113 | 146 |
| 114 namespace webdriver { | 147 namespace webdriver { |
| 115 | 148 |
| 116 WebKeyEvent CreateKeyDownEvent(ui::KeyboardCode key_code, int modifiers) { | 149 WebKeyEvent CreateKeyDownEvent(ui::KeyboardCode key_code, int modifiers) { |
| 117 return WebKeyEvent(automation::kRawKeyDownType, key_code, "", "", modifiers); | 150 return WebKeyEvent(automation::kRawKeyDownType, key_code, "", "", modifiers); |
| 118 } | 151 } |
| 119 | 152 |
| 120 WebKeyEvent CreateKeyUpEvent(ui::KeyboardCode key_code, int modifiers) { | 153 WebKeyEvent CreateKeyUpEvent(ui::KeyboardCode key_code, int modifiers) { |
| 121 return WebKeyEvent(automation::kKeyUpType, key_code, "", "", modifiers); | 154 return WebKeyEvent(automation::kKeyUpType, key_code, "", "", modifiers); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 key_events->push_back(CreateKeyDownEvent(key_code, sticky_modifiers)); | 215 key_events->push_back(CreateKeyDownEvent(key_code, sticky_modifiers)); |
| 183 else | 216 else |
| 184 key_events->push_back(CreateKeyUpEvent(key_code, sticky_modifiers)); | 217 key_events->push_back(CreateKeyUpEvent(key_code, sticky_modifiers)); |
| 185 continue; | 218 continue; |
| 186 } | 219 } |
| 187 | 220 |
| 188 ui::KeyboardCode key_code = ui::VKEY_UNKNOWN; | 221 ui::KeyboardCode key_code = ui::VKEY_UNKNOWN; |
| 189 std::string unmodified_text, modified_text; | 222 std::string unmodified_text, modified_text; |
| 190 int all_modifiers = sticky_modifiers; | 223 int all_modifiers = sticky_modifiers; |
| 191 | 224 |
| 192 bool is_special_key = KeyCodeFromSpecialWebDriverKey(key, &key_code); | 225 // Get the key code, text, and modifiers for the given key. |
| 193 if (is_special_key && key_code == ui::VKEY_UNKNOWN) { | 226 bool should_skip = false; |
| 194 LOG(ERROR) << "Unknown WebDriver key: " << static_cast<int>(key); | 227 if (KeyCodeFromSpecialWebDriverKey(key, &key_code) || |
| 195 continue; | 228 KeyCodeFromShorthandKey(key, &key_code, &should_skip)) { |
| 196 } | 229 if (should_skip) |
| 197 if (!is_special_key) { | 230 continue; |
| 231 if (key_code == ui::VKEY_UNKNOWN) { | |
| 232 LOG(ERROR) << "Unknown WebDriver key: " << static_cast<int>(key); | |
|
Joe
2011/03/15 21:22:17
If an unknown key is requested shouldn't it break
kkania
2011/03/18 21:45:12
Done.
| |
| 233 continue; | |
| 234 } | |
| 235 if (key_code == ui::VKEY_RETURN) { | |
| 236 modified_text = unmodified_text = "\r"; | |
| 237 } else { | |
| 238 unmodified_text = ConvertKeyCodeToText(key_code, 0); | |
| 239 modified_text = ConvertKeyCodeToText(key_code, all_modifiers); | |
| 240 } | |
| 241 } else { | |
| 198 int necessary_modifiers = 0; | 242 int necessary_modifiers = 0; |
| 199 ConvertCharToKeyCode(key, &key_code, &necessary_modifiers); | 243 ConvertCharToKeyCode(key, &key_code, &necessary_modifiers); |
| 200 all_modifiers |= necessary_modifiers; | 244 all_modifiers |= necessary_modifiers; |
| 201 } | 245 if (key_code != ui::VKEY_UNKNOWN) { |
| 202 if (key_code != ui::VKEY_UNKNOWN) { | 246 unmodified_text = ConvertKeyCodeToText(key_code, 0); |
| 203 unmodified_text = ConvertKeyCodeToText(key_code, 0); | 247 modified_text = ConvertKeyCodeToText(key_code, all_modifiers); |
| 204 modified_text = ConvertKeyCodeToText(key_code, all_modifiers); | 248 } |
| 205 } | 249 if (unmodified_text.empty() || modified_text.empty()) { |
| 206 if (!is_special_key && (unmodified_text.empty() || modified_text.empty())) { | 250 // Do a best effort and use the raw key we were given. |
| 207 // Do a best effort and use the raw key we were given. | 251 LOG(WARNING) << "No translation for key code. Code point: " |
| 208 LOG(WARNING) << "No translation for key code. Code point: " | 252 << static_cast<int>(key); |
| 209 << static_cast<int>(key); | 253 if (unmodified_text.empty()) |
| 210 if (unmodified_text.empty()) | 254 unmodified_text = UTF16ToUTF8(keys.substr(i, 1)); |
| 211 unmodified_text = UTF16ToUTF8(keys.substr(i, 1)); | 255 if (modified_text.empty()) |
| 212 if (modified_text.empty()) | 256 modified_text = UTF16ToUTF8(keys.substr(i, 1)); |
| 213 modified_text = UTF16ToUTF8(keys.substr(i, 1)); | 257 } |
| 214 } | 258 } |
| 215 | 259 |
| 216 // Create the key events. | 260 // Create the key events. |
| 217 bool need_shift_key = | 261 bool need_shift_key = |
| 218 all_modifiers & automation::kShiftKeyMask && | 262 all_modifiers & automation::kShiftKeyMask && |
| 219 !(sticky_modifiers & automation::kShiftKeyMask); | 263 !(sticky_modifiers & automation::kShiftKeyMask); |
| 220 if (need_shift_key) { | 264 if (need_shift_key) { |
| 221 key_events->push_back( | 265 key_events->push_back( |
| 222 CreateKeyDownEvent(ui::VKEY_SHIFT, sticky_modifiers)); | 266 CreateKeyDownEvent(ui::VKEY_SHIFT, sticky_modifiers)); |
| 223 } | 267 } |
| 224 | 268 |
| 225 key_events->push_back(CreateKeyDownEvent(key_code, all_modifiers)); | 269 key_events->push_back(CreateKeyDownEvent(key_code, all_modifiers)); |
| 226 if (unmodified_text.length() || modified_text.length()) { | 270 if (unmodified_text.length() || modified_text.length()) { |
| 227 key_events->push_back( | 271 key_events->push_back( |
| 228 CreateCharEvent(unmodified_text, modified_text, all_modifiers)); | 272 CreateCharEvent(unmodified_text, modified_text, all_modifiers)); |
| 229 } | 273 } |
| 230 key_events->push_back(CreateKeyUpEvent(key_code, all_modifiers)); | 274 key_events->push_back(CreateKeyUpEvent(key_code, all_modifiers)); |
| 231 | 275 |
| 232 if (need_shift_key) { | 276 if (need_shift_key) { |
| 233 key_events->push_back( | 277 key_events->push_back( |
| 234 CreateKeyUpEvent(ui::VKEY_SHIFT, sticky_modifiers)); | 278 CreateKeyUpEvent(ui::VKEY_SHIFT, sticky_modifiers)); |
| 235 } | 279 } |
| 236 } | 280 } |
| 237 } | 281 } |
| 238 | 282 |
| 239 } // namespace webdriver | 283 } // namespace webdriver |
| OLD | NEW |