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

Side by Side Diff: components/test_runner/event_sender.cc

Issue 929053004: [KeyboardEvent] Add embedder APIs to translate between Dom |key| enum and strings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: UnitTest updated as MakeKeyboardEvent now adds a value to differentiate Created 5 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/test_runner/event_sender.h" 5 #include "components/test_runner/event_sender.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
11 #include "components/test_runner/mock_spell_check.h" 12 #include "components/test_runner/mock_spell_check.h"
12 #include "components/test_runner/test_interfaces.h" 13 #include "components/test_runner/test_interfaces.h"
13 #include "components/test_runner/web_test_delegate.h" 14 #include "components/test_runner/web_test_delegate.h"
14 #include "components/test_runner/web_test_proxy.h" 15 #include "components/test_runner/web_test_proxy.h"
15 #include "gin/handle.h" 16 #include "gin/handle.h"
16 #include "gin/object_template_builder.h" 17 #include "gin/object_template_builder.h"
17 #include "gin/wrappable.h" 18 #include "gin/wrappable.h"
18 #include "third_party/WebKit/public/platform/WebString.h" 19 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebVector.h" 20 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "third_party/WebKit/public/web/WebContextMenuData.h" 21 #include "third_party/WebKit/public/web/WebContextMenuData.h"
21 #include "third_party/WebKit/public/web/WebFrame.h" 22 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebKit.h" 23 #include "third_party/WebKit/public/web/WebKit.h"
23 #include "third_party/WebKit/public/web/WebPagePopup.h" 24 #include "third_party/WebKit/public/web/WebPagePopup.h"
24 #include "third_party/WebKit/public/web/WebView.h" 25 #include "third_party/WebKit/public/web/WebView.h"
26 #include "ui/events/event_constants.h"
27 #include "ui/events/keycodes/dom/dom_code.h"
28 #include "ui/events/keycodes/dom/dom_key.h"
25 #include "ui/events/keycodes/dom/keycode_converter.h" 29 #include "ui/events/keycodes/dom/keycode_converter.h"
30 #include "ui/events/keycodes/dom_us_layout_data.h"
31 #include "ui/events/keycodes/keyboard_code_conversion.h"
26 #include "ui/events/keycodes/keyboard_codes.h" 32 #include "ui/events/keycodes/keyboard_codes.h"
27 #include "v8/include/v8.h" 33 #include "v8/include/v8.h"
28 34
29 using blink::WebContextMenuData; 35 using blink::WebContextMenuData;
30 using blink::WebDragData; 36 using blink::WebDragData;
31 using blink::WebDragOperationsMask; 37 using blink::WebDragOperationsMask;
32 using blink::WebFloatPoint; 38 using blink::WebFloatPoint;
33 using blink::WebFrame; 39 using blink::WebFrame;
34 using blink::WebGestureEvent; 40 using blink::WebGestureEvent;
35 using blink::WebInputEvent; 41 using blink::WebInputEvent;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 void RunIfValid() override { 291 void RunIfValid() override {
286 object_->KeyDown(code_str_, modifiers_, location_); 292 object_->KeyDown(code_str_, modifiers_, location_);
287 } 293 }
288 294
289 private: 295 private:
290 std::string code_str_; 296 std::string code_str_;
291 int modifiers_; 297 int modifiers_;
292 KeyLocationCode location_; 298 KeyLocationCode location_;
293 }; 299 };
294 300
295 bool NeedsShiftModifier(int keyCode) {
296 // If code is an uppercase letter, assign a SHIFT key to eventDown.modifier.
297 return (keyCode & 0xFF) >= 'A' && (keyCode & 0xFF) <= 'Z';
298 }
299
300 // Get the edit command corresponding to a keyboard event. 301 // Get the edit command corresponding to a keyboard event.
301 // Returns true if the specified event corresponds to an edit command, the name 302 // Returns true if the specified event corresponds to an edit command, the name
302 // of the edit command will be stored in |*name|. 303 // of the edit command will be stored in |*name|.
303 bool GetEditCommand(const WebKeyboardEvent& event, std::string* name) { 304 bool GetEditCommand(const WebKeyboardEvent& event, std::string* name) {
304 #if defined(OS_MACOSX) 305 #if defined(OS_MACOSX)
305 // We only cares about Left,Right,Up,Down keys with Command or Command+Shift 306 // We only cares about Left,Right,Up,Down keys with Command or Command+Shift
306 // modifiers. These key events correspond to some special movement and 307 // modifiers. These key events correspond to some special movement and
307 // selection editor commands. These keys will be marked as system key, which 308 // selection editor commands. These keys will be marked as system key, which
308 // prevents them from being handled. Thus they must be handled specially. 309 // prevents them from being handled. Thus they must be handled specially.
309 if ((event.modifiers & ~WebKeyboardEvent::ShiftKey) != 310 if ((event.modifiers & ~WebKeyboardEvent::ShiftKey) !=
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 button_type, 1235 button_type,
1235 last_mouse_pos_, 1236 last_mouse_pos_,
1236 GetCurrentEventTimeSec(), 1237 GetCurrentEventTimeSec(),
1237 click_count_, 1238 click_count_,
1238 modifiers, 1239 modifiers,
1239 &event); 1240 &event);
1240 DoMouseUp(event); 1241 DoMouseUp(event);
1241 } 1242 }
1242 } 1243 }
1243 1244
1244 void EventSender::KeyDown(const std::string& code_str, 1245 ui::DomCode convertKeyCodeStrToDomCode(const std::string key_code_str,
1246 bool &need_modifier)
1247 {
1248 // Convert key_code_str to DomCode passed from the layout tests.
1249 const struct DomCodeToKeyCodeStr {
1250 ui::DomCode dom_code;
1251 std::string key_code_str;
1252 } kDomCodeToKeyCodeStrMap[] = {
1253 {ui::DomCode::ENTER, "\n"},
1254 {ui::DomCode::ENTER, "\r"},
1255 {ui::DomCode::ARROW_RIGHT, "rightArrow"},
1256 {ui::DomCode::ARROW_LEFT, "leftArrow"},
1257 {ui::DomCode::ARROW_DOWN, "downArrow"},
1258 {ui::DomCode::ARROW_UP, "upArrow"},
1259 {ui::DomCode::INSERT, "insert"},
1260 {ui::DomCode::DEL, "delete"},
1261 {ui::DomCode::PAGE_UP, "pageUp"},
1262 {ui::DomCode::PAGE_DOWN, "pageDown"},
1263 {ui::DomCode::HOME, "home"},
1264 {ui::DomCode::END, "end"},
1265 {ui::DomCode::PRINT_SCREEN, "printScreen"},
1266 {ui::DomCode::CONTEXT_MENU, "menu"},
1267 {ui::DomCode::CONTROL_LEFT, "leftControl"},
1268 {ui::DomCode::CONTROL_RIGHT, "rightControl"},
1269 {ui::DomCode::SHIFT_LEFT, "leftShift"},
1270 {ui::DomCode::SHIFT_RIGHT, "rightShift"},
1271 {ui::DomCode::ALT_LEFT, "leftAlt"},
1272 {ui::DomCode::ALT_RIGHT, "rightAlt"},
1273 {ui::DomCode::NUM_LOCK, "numLock"},
1274 {ui::DomCode::BACKSPACE, "backspace"},
1275 {ui::DomCode::ESCAPE, "escape"},
1276 };
1277
1278 for (const auto& it : kDomCodeToKeyCodeStrMap) {
1279 if (it.key_code_str == key_code_str)
1280 return it.dom_code;
1281 }
1282
1283 // F1..24 DomCode Value.
1284 for (int i = 1; i <= 24; ++i) {
1285 std::string function_key_name = base::StringPrintf("F%d", i);
1286 if (function_key_name == key_code_str) {
1287 int num = static_cast<int>(ui::DomCode::F1) + ( i -1);
1288 return static_cast<ui::DomCode>(num);
1289 }
1290 }
1291
1292 // Printable Value
1293 for (const auto& it : ui::kPrintableCodeMap) {
1294 base::char16 str = static_cast<base::char16>(key_code_str.at(0));
1295 if (it.character[0] == str || it.character[1] == str) {
1296 if (it.character[1] == str && it.character[0] != it.character[1])
1297 need_modifier = true;
1298 return it.dom_code;
1299 }
1300 }
1301
1302 // Fallback to get corresponding DomCode value.
1303 return ui::UsLayoutKeyboardCodeToDomCode(
1304 static_cast<ui::KeyboardCode>(key_code_str.at(0)));
1305 }
1306
1307 void EventSender::KeyDown(const std::string& key_code_str,
1245 int modifiers, 1308 int modifiers,
1246 KeyLocationCode location) { 1309 KeyLocationCode location) {
1247 // FIXME: I'm not exactly sure how we should convert the string to a key 1310 // FIXME: I'm not exactly sure how we should convert the string to a key
1248 // event. This seems to work in the cases I tested. 1311 // event. This seems to work in the cases I tested.
1249 // FIXME: Should we also generate a KEY_UP? 1312 // FIXME: Should we also generate a KEY_UP?
1250 1313
1251 bool generate_char = false; 1314 bool generate_char = false;
1315 bool need_modifier = false;
1316 int flag = ui::EF_NONE;
1252 1317
1253 // Convert \n -> VK_RETURN. Some layout tests use \n to mean "Enter", when 1318 ui::DomCode dom_code = convertKeyCodeStrToDomCode(key_code_str,
1254 // Windows uses \r for "Enter". 1319 need_modifier);
1255 int code = 0; 1320 ui::KeyboardCode key_code;
1256 int text = 0; 1321 ui::DomKey dom_key;
1257 bool needs_shift_key_modifier = false; 1322 base::char16 dom_key_char = 0;
1258 std::string domString;
1259 1323
1260 if ("\n" == code_str) { 1324 // Modifier helps in getting correct dom_key_char.
1261 generate_char = true; 1325 if (need_modifier)
1262 text = code = ui::VKEY_RETURN; 1326 flag |= ui::EF_SHIFT_DOWN;
1263 domString.assign("Enter");
1264 } else if ("rightArrow" == code_str) {
1265 code = ui::VKEY_RIGHT;
1266 domString.assign("ArrowRight");
1267 } else if ("downArrow" == code_str) {
1268 code = ui::VKEY_DOWN;
1269 domString.assign("ArrowDown");
1270 } else if ("leftArrow" == code_str) {
1271 code = ui::VKEY_LEFT;
1272 domString.assign("ArrowLeft");
1273 } else if ("upArrow" == code_str) {
1274 code = ui::VKEY_UP;
1275 domString.assign("ArrowUp");
1276 } else if ("insert" == code_str) {
1277 code = ui::VKEY_INSERT;
1278 domString.assign("Insert");
1279 } else if ("delete" == code_str) {
1280 code = ui::VKEY_DELETE;
1281 domString.assign("Delete");
1282 } else if ("pageUp" == code_str) {
1283 code = ui::VKEY_PRIOR;
1284 domString.assign("PageUp");
1285 } else if ("pageDown" == code_str) {
1286 code = ui::VKEY_NEXT;
1287 domString.assign("PageDown");
1288 } else if ("home" == code_str) {
1289 code = ui::VKEY_HOME;
1290 domString.assign("Home");
1291 } else if ("end" == code_str) {
1292 code = ui::VKEY_END;
1293 domString.assign("End");
1294 } else if ("printScreen" == code_str) {
1295 code = ui::VKEY_SNAPSHOT;
1296 domString.assign("PrintScreen");
1297 } else if ("menu" == code_str) {
1298 code = ui::VKEY_APPS;
1299 domString.assign("ContextMenu");
1300 } else if ("leftControl" == code_str) {
1301 code = ui::VKEY_LCONTROL;
1302 domString.assign("ControlLeft");
1303 } else if ("rightControl" == code_str) {
1304 code = ui::VKEY_RCONTROL;
1305 domString.assign("ControlRight");
1306 } else if ("leftShift" == code_str) {
1307 code = ui::VKEY_LSHIFT;
1308 domString.assign("ShiftLeft");
1309 } else if ("rightShift" == code_str) {
1310 code = ui::VKEY_RSHIFT;
1311 domString.assign("ShiftRight");
1312 } else if ("leftAlt" == code_str) {
1313 code = ui::VKEY_LMENU;
1314 domString.assign("AltLeft");
1315 } else if ("rightAlt" == code_str) {
1316 code = ui::VKEY_RMENU;
1317 domString.assign("AltRight");
1318 } else if ("numLock" == code_str) {
1319 code = ui::VKEY_NUMLOCK;
1320 domString.assign("NumLock");
1321 } else if ("backspace" == code_str) {
1322 code = ui::VKEY_BACK;
1323 domString.assign("Backspace");
1324 } else if ("escape" == code_str) {
1325 code = ui::VKEY_ESCAPE;
1326 domString.assign("Escape");
1327 } else {
1328 // Compare the input string with the function-key names defined by the
1329 // DOM spec (i.e. "F1",...,"F24"). If the input string is a function-key
1330 // name, set its key code.
1331 for (int i = 1; i <= 24; ++i) {
1332 std::string function_key_name = base::StringPrintf("F%d", i);
1333 if (function_key_name == code_str) {
1334 code = ui::VKEY_F1 + (i - 1);
1335 domString = function_key_name;
1336 break;
1337 }
1338 }
1339 if (!code) {
1340 WebString web_code_str =
1341 WebString::fromUTF8(code_str.data(), code_str.size());
1342 if (web_code_str.length() != 1u) {
1343 v8::Isolate* isolate = blink::mainThreadIsolate();
1344 isolate->ThrowException(v8::Exception::TypeError(
1345 gin::StringToV8(isolate, "Invalid web code.")));
1346 return;
1347 }
1348 text = code = web_code_str.at(0);
1349 needs_shift_key_modifier = NeedsShiftModifier(code);
1350 if ((code & 0xFF) >= 'a' && (code & 0xFF) <= 'z')
1351 code -= 'a' - 'A';
1352 if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) {
1353 domString.assign("Key");
1354 domString.push_back(base::ToUpperASCII(code));
1355 } else if (code >= '0' && code <= '9') {
1356 domString.assign("Digit");
1357 domString.push_back(code);
1358 } else if (code == ' ') {
1359 domString.assign("Space");
1360 } else if (code == 9) {
1361 domString.assign("Tab");
1362 }
1363 generate_char = true;
1364 }
1365 1327
1366 if ("(" == code_str) { 1328 if (!DomCodeToUsLayoutMeaning(dom_code, flag, &dom_key, &dom_key_char,
1367 code = '9'; 1329 &key_code)) {
1368 needs_shift_key_modifier = true; 1330 dom_key = ui::DomKey::NONE;
1369 }
1370 } 1331 }
1371 1332
1333 key_code = NonLocatedToLocatedKeyboardCode(key_code, dom_code);
1334
1372 // For one generated keyboard event, we need to generate a keyDown/keyUp 1335 // For one generated keyboard event, we need to generate a keyDown/keyUp
1373 // pair; 1336 // pair;
1374 // On Windows, we might also need to generate a char event to mimic the 1337 // On Windows, we might also need to generate a char event to mimic the
1375 // Windows event flow; on other platforms we create a merged event and test 1338 // Windows event flow; on other platforms we create a merged event and test
1376 // the event flow that that platform provides. 1339 // the event flow that that platform provides.
1377 WebKeyboardEvent event_down; 1340 WebKeyboardEvent event_down;
1378 event_down.type = WebInputEvent::RawKeyDown; 1341 event_down.type = WebInputEvent::RawKeyDown;
1379 event_down.modifiers = modifiers; 1342 event_down.modifiers = modifiers;
1380 event_down.windowsKeyCode = code; 1343 event_down.windowsKeyCode = key_code;
1381 event_down.domCode = static_cast<int>( 1344 event_down.domCode = static_cast<int>(dom_code);
1382 ui::KeycodeConverter::CodeStringToDomCode(domString.c_str()));
1383 1345
1384 if (generate_char) { 1346 if (dom_key == ui::DomKey::CHARACTER || dom_key_char != 0) {
1385 event_down.text[0] = text; 1347 event_down.domKey = static_cast<int>(dom_key_char);
1386 event_down.unmodifiedText[0] = text; 1348 event_down.unmodifiedText[0] = event_down.text[0] = dom_key_char;
1349 generate_char = true;
1350 } else {
1351 event_down.domKey = DOM_KEY_PRINT_NON_DIFF + static_cast<int>(dom_key);
1387 } 1352 }
1388 1353
1389 event_down.setKeyIdentifierFromWindowsKeyCode(); 1354 event_down.setKeyIdentifierFromWindowsKeyCode();
1390 1355
1391 if (event_down.modifiers != 0) 1356 if (event_down.modifiers != 0)
1392 event_down.isSystemKey = IsSystemKeyEvent(event_down); 1357 event_down.isSystemKey = IsSystemKeyEvent(event_down);
1393 1358
1394 if (needs_shift_key_modifier) 1359 if (need_modifier)
1395 event_down.modifiers |= WebInputEvent::ShiftKey; 1360 event_down.modifiers |= WebInputEvent::ShiftKey;
1396 1361
1397 // See if KeyLocation argument is given. 1362 // See if KeyLocation argument is given.
1398 if (location == DOMKeyLocationNumpad) 1363 if (location == DOMKeyLocationNumpad)
1399 event_down.modifiers |= WebInputEvent::IsKeyPad; 1364 event_down.modifiers |= WebInputEvent::IsKeyPad;
1400 1365
1401 WebKeyboardEvent event_up; 1366 WebKeyboardEvent event_up;
1402 event_up = event_down; 1367 event_up = event_down;
1403 event_up.type = WebInputEvent::KeyUp; 1368 event_up.type = WebInputEvent::KeyUp;
1404 // EventSender.m forces a layout here, with at least one 1369 // EventSender.m forces a layout here, with at least one
1405 // test (fast/forms/focus-control-to-page.html) relying on this. 1370 // test (fast/forms/focus-control-to-page.html) relying on this.
1406 if (force_layout_on_events_) 1371 if (force_layout_on_events_)
1407 view_->layout(); 1372 view_->layout();
1408 1373
1409 // In the browser, if a keyboard event corresponds to an editor command, 1374 // In the browser, if a keyboard event corresponds to an editor command,
1410 // the command will be dispatched to the renderer just before dispatching 1375 // the command will be dispatched to the renderer just before dispatching
1411 // the keyboard event, and then it will be executed in the 1376 // the keyboard event, and then it will be executed in the
1412 // RenderView::handleCurrentKeyboardEvent() method. 1377 // RenderView::handleCurrentKeyboardEvent() method.
1413 // We just simulate the same behavior here. 1378 // We just simulate the same behavior here.
1414 std::string edit_command; 1379 std::string edit_command;
1415 if (GetEditCommand(event_down, &edit_command)) 1380 if (GetEditCommand(event_down, &edit_command))
1416 delegate_->SetEditCommand(edit_command, ""); 1381 delegate_->SetEditCommand(edit_command, "");
1417 1382
1418 HandleInputEventOnViewOrPopup(event_down); 1383 HandleInputEventOnViewOrPopup(event_down);
1419 1384
1420 if (code == ui::VKEY_ESCAPE && !current_drag_data_.isNull()) { 1385 if (dom_code == ui::DomCode::ESCAPE && !current_drag_data_.isNull()) {
1421 WebMouseEvent event; 1386 WebMouseEvent event;
1422 InitMouseEvent(WebInputEvent::MouseDown, 1387 InitMouseEvent(WebInputEvent::MouseDown,
1423 pressed_button_, 1388 pressed_button_,
1424 last_mouse_pos_, 1389 last_mouse_pos_,
1425 GetCurrentEventTimeSec(), 1390 GetCurrentEventTimeSec(),
1426 click_count_, 1391 click_count_,
1427 0, 1392 0,
1428 &event); 1393 &event);
1429 FinishDragAndDrop(event, blink::WebDragOperationNone); 1394 FinishDragAndDrop(event, blink::WebDragOperationNone);
1430 } 1395 }
1431 1396
1432 delegate_->ClearEditCommand(); 1397 delegate_->ClearEditCommand();
1433 1398
1434 if (generate_char) { 1399 if (generate_char) {
1435 WebKeyboardEvent event_char = event_up; 1400 WebKeyboardEvent event_char = event_up;
1436 event_char.type = WebInputEvent::Char; 1401 event_char.type = WebInputEvent::Char;
1437 // keyIdentifier is an empty string, unless the Enter key was pressed. 1402 // keyIdentifier is an empty string, unless the Enter key was pressed.
1438 // This behavior is not standard (keyIdentifier itself is not even a 1403 // This behavior is not standard (keyIdentifier itself is not even a
1439 // standard any more), but it matches the actual behavior in Blink. 1404 // standard any more), but it matches the actual behavior in Blink.
1440 if (code != ui::VKEY_RETURN) 1405 if (dom_code != ui::DomCode::ENTER)
1441 event_char.keyIdentifier[0] = '\0'; 1406 event_char.keyIdentifier[0] = '\0';
1442 HandleInputEventOnViewOrPopup(event_char); 1407 HandleInputEventOnViewOrPopup(event_char);
1443 } 1408 }
1444 1409
1445 HandleInputEventOnViewOrPopup(event_up); 1410 HandleInputEventOnViewOrPopup(event_up);
1446 } 1411 }
1447 1412
1448 void EventSender::EnableDOMUIEventLogging() {} 1413 void EventSender::EnableDOMUIEventLogging() {}
1449 1414
1450 void EventSender::FireKeyboardEventsToElement() {} 1415 void EventSender::FireKeyboardEventsToElement() {}
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after
2474 2439
2475 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) { 2440 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) {
2476 if (WebPagePopup* popup = view_->pagePopup()) { 2441 if (WebPagePopup* popup = view_->pagePopup()) {
2477 if (!WebInputEvent::isKeyboardEventType(event.type)) 2442 if (!WebInputEvent::isKeyboardEventType(event.type))
2478 return popup->handleInputEvent(event); 2443 return popup->handleInputEvent(event);
2479 } 2444 }
2480 return view_->handleInputEvent(event); 2445 return view_->handleInputEvent(event);
2481 } 2446 }
2482 2447
2483 } // namespace test_runner 2448 } // namespace test_runner
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698