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

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: Passing Dom KEY printable and non-printable to pass value under domKey Created 5 years, 6 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_key.h"
25 #include "ui/events/keycodes/dom/keycode_converter.h" 28 #include "ui/events/keycodes/dom/keycode_converter.h"
29 #include "ui/events/keycodes/keyboard_code_conversion.h"
26 #include "ui/events/keycodes/keyboard_codes.h" 30 #include "ui/events/keycodes/keyboard_codes.h"
27 #include "v8/include/v8.h" 31 #include "v8/include/v8.h"
28 32
29 using blink::WebContextMenuData; 33 using blink::WebContextMenuData;
30 using blink::WebDragData; 34 using blink::WebDragData;
31 using blink::WebDragOperationsMask; 35 using blink::WebDragOperationsMask;
32 using blink::WebFloatPoint; 36 using blink::WebFloatPoint;
33 using blink::WebFrame; 37 using blink::WebFrame;
34 using blink::WebGestureEvent; 38 using blink::WebGestureEvent;
35 using blink::WebInputEvent; 39 using blink::WebInputEvent;
(...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 button_type, 1238 button_type,
1235 last_mouse_pos_, 1239 last_mouse_pos_,
1236 GetCurrentEventTimeSec(), 1240 GetCurrentEventTimeSec(),
1237 click_count_, 1241 click_count_,
1238 modifiers, 1242 modifiers,
1239 &event); 1243 &event);
1240 DoMouseUp(event); 1244 DoMouseUp(event);
1241 } 1245 }
1242 } 1246 }
1243 1247
1244 void EventSender::KeyDown(const std::string& code_str, 1248 void EventSender::KeyDown(const std::string& key_code_str,
1245 int modifiers, 1249 int modifiers,
1246 KeyLocationCode location) { 1250 KeyLocationCode location) {
1247 // FIXME: I'm not exactly sure how we should convert the string to a key 1251 // 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. 1252 // event. This seems to work in the cases I tested.
1249 // FIXME: Should we also generate a KEY_UP? 1253 // FIXME: Should we also generate a KEY_UP?
1250 1254
1251 bool generate_char = false; 1255 bool generate_char = false;
1252 1256
1253 // Convert \n -> VK_RETURN. Some layout tests use \n to mean "Enter", when 1257 // Convert \n -> VK_RETURN. Some layout tests use \n to mean "Enter", when
1254 // Windows uses \r for "Enter". 1258 // Windows uses \r for "Enter".
1255 int code = 0; 1259 int key_code = 0;
1256 int text = 0; 1260 int text = 0;
1257 bool needs_shift_key_modifier = false; 1261 bool needs_shift_key_modifier = false;
1258 std::string domString;
1259 1262
1260 if ("\n" == code_str) { 1263 if ("\n" == key_code_str) {
1261 generate_char = true; 1264 generate_char = true;
1262 text = code = ui::VKEY_RETURN; 1265 text = key_code = ui::VKEY_RETURN;
1263 domString.assign("Enter"); 1266 } else if ("rightArrow" == key_code_str) {
1264 } else if ("rightArrow" == code_str) { 1267 key_code = ui::VKEY_RIGHT;
1265 code = ui::VKEY_RIGHT; 1268 } else if ("downArrow" == key_code_str) {
1266 domString.assign("ArrowRight"); 1269 key_code = ui::VKEY_DOWN;
1267 } else if ("downArrow" == code_str) { 1270 } else if ("leftArrow" == key_code_str) {
1268 code = ui::VKEY_DOWN; 1271 key_code = ui::VKEY_LEFT;
1269 domString.assign("ArrowDown"); 1272 } else if ("upArrow" == key_code_str) {
1270 } else if ("leftArrow" == code_str) { 1273 key_code = ui::VKEY_UP;
1271 code = ui::VKEY_LEFT; 1274 } else if ("insert" == key_code_str) {
1272 domString.assign("ArrowLeft"); 1275 key_code = ui::VKEY_INSERT;
1273 } else if ("upArrow" == code_str) { 1276 } else if ("delete" == key_code_str) {
1274 code = ui::VKEY_UP; 1277 key_code = ui::VKEY_DELETE;
1275 domString.assign("ArrowUp"); 1278 } else if ("pageUp" == key_code_str) {
1276 } else if ("insert" == code_str) { 1279 key_code = ui::VKEY_PRIOR;
1277 code = ui::VKEY_INSERT; 1280 } else if ("pageDown" == key_code_str) {
1278 domString.assign("Insert"); 1281 key_code = ui::VKEY_NEXT;
1279 } else if ("delete" == code_str) { 1282 } else if ("home" == key_code_str) {
1280 code = ui::VKEY_DELETE; 1283 key_code = ui::VKEY_HOME;
1281 domString.assign("Delete"); 1284 } else if ("end" == key_code_str) {
1282 } else if ("pageUp" == code_str) { 1285 key_code = ui::VKEY_END;
1283 code = ui::VKEY_PRIOR; 1286 } else if ("printScreen" == key_code_str) {
1284 domString.assign("PageUp"); 1287 key_code = ui::VKEY_SNAPSHOT;
1285 } else if ("pageDown" == code_str) { 1288 } else if ("menu" == key_code_str) {
1286 code = ui::VKEY_NEXT; 1289 key_code = ui::VKEY_APPS;
1287 domString.assign("PageDown"); 1290 } else if ("leftControl" == key_code_str) {
1288 } else if ("home" == code_str) { 1291 key_code = ui::VKEY_LCONTROL;
1289 code = ui::VKEY_HOME; 1292 } else if ("rightControl" == key_code_str) {
1290 domString.assign("Home"); 1293 key_code = ui::VKEY_RCONTROL;
1291 } else if ("end" == code_str) { 1294 } else if ("leftShift" == key_code_str) {
1292 code = ui::VKEY_END; 1295 key_code = ui::VKEY_LSHIFT;
1293 domString.assign("End"); 1296 } else if ("rightShift" == key_code_str) {
1294 } else if ("printScreen" == code_str) { 1297 key_code = ui::VKEY_RSHIFT;
1295 code = ui::VKEY_SNAPSHOT; 1298 } else if ("leftAlt" == key_code_str) {
1296 domString.assign("PrintScreen"); 1299 key_code = ui::VKEY_LMENU;
1297 } else if ("menu" == code_str) { 1300 } else if ("rightAlt" == key_code_str) {
1298 code = ui::VKEY_APPS; 1301 key_code = ui::VKEY_RMENU;
1299 domString.assign("ContextMenu"); 1302 } else if ("numLock" == key_code_str) {
1300 } else if ("leftControl" == code_str) { 1303 key_code = ui::VKEY_NUMLOCK;
1301 code = ui::VKEY_LCONTROL; 1304 } else if ("backspace" == key_code_str) {
1302 domString.assign("ControlLeft"); 1305 key_code = ui::VKEY_BACK;
1303 } else if ("rightControl" == code_str) { 1306 } else if ("escape" == key_code_str) {
1304 code = ui::VKEY_RCONTROL; 1307 key_code = ui::VKEY_ESCAPE;
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 { 1308 } else {
1328 // Compare the input string with the function-key names defined by the 1309 // 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 1310 // DOM spec (i.e. "F1",...,"F24"). If the input string is a function-key
1330 // name, set its key code. 1311 // name, set its key code.
1331 for (int i = 1; i <= 24; ++i) { 1312 for (int i = 1; i <= 24; ++i) {
1332 std::string function_key_name = base::StringPrintf("F%d", i); 1313 std::string function_key_name = base::StringPrintf("F%d", i);
1333 if (function_key_name == code_str) { 1314 if (function_key_name == key_code_str) {
1334 code = ui::VKEY_F1 + (i - 1); 1315 key_code = ui::VKEY_F1 + (i - 1);
1335 domString = function_key_name;
1336 break; 1316 break;
1337 } 1317 }
1338 } 1318 }
1339 if (!code) { 1319 if (!key_code) {
1340 WebString web_code_str = 1320 WebString web_code_str =
1341 WebString::fromUTF8(code_str.data(), code_str.size()); 1321 WebString::fromUTF8(key_code_str.data(), key_code_str.size());
1342 if (web_code_str.length() != 1u) { 1322 if (web_code_str.length() != 1u) {
1343 v8::Isolate* isolate = blink::mainThreadIsolate(); 1323 v8::Isolate* isolate = blink::mainThreadIsolate();
1344 isolate->ThrowException(v8::Exception::TypeError( 1324 isolate->ThrowException(v8::Exception::TypeError(
1345 gin::StringToV8(isolate, "Invalid web code."))); 1325 gin::StringToV8(isolate, "Invalid web code.")));
1346 return; 1326 return;
1347 } 1327 }
1348 text = code = web_code_str.at(0); 1328 text = key_code = web_code_str.at(0);
Habib Virji 2015/06/24 14:32:12 WebString is used to facilitate getting int value
1349 needs_shift_key_modifier = NeedsShiftModifier(code); 1329 needs_shift_key_modifier = NeedsShiftModifier(key_code);
1350 if ((code & 0xFF) >= 'a' && (code & 0xFF) <= 'z') 1330 if ((key_code & 0xFF) >= 'a' && (key_code & 0xFF) <= 'z')
1351 code -= 'a' - 'A'; 1331 key_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; 1332 generate_char = true;
1364 } 1333 }
1365 1334
1366 if ("(" == code_str) { 1335 if ("(" == key_code_str) {
1367 code = '9'; 1336 key_code = '9';
1368 needs_shift_key_modifier = true; 1337 needs_shift_key_modifier = true;
1369 } 1338 }
1370 } 1339 }
1371 1340
1341 ui::KeyboardCode codex = ui::VKEY_UNKNOWN;
1342 ui::DomCode dom_code = ui::UsLayoutKeyboardCodeToDomCode(
1343 static_cast<ui::KeyboardCode>(key_code));
1344 ui::DomKey dom_key;
1345 base::char16 dom_key_char = 0;
1346 if (!DomCodeToUsLayoutMeaning(dom_code, ui::EF_NONE, &dom_key, &dom_key_char,
1347 &codex)) {
1348 dom_key = ui::DomKey::NONE;
1349 }
1350
1351 int dom_key_int;
1352 if (dom_key == ui::DomKey::CHARACTER)
1353 dom_key_int = static_cast<int>(dom_key_char);
1354 else
1355 dom_key_int = DOM_KEY_PRINT_NON_DIFF + static_cast<int>(dom_key);
1356
1372 // For one generated keyboard event, we need to generate a keyDown/keyUp 1357 // For one generated keyboard event, we need to generate a keyDown/keyUp
1373 // pair; 1358 // pair;
1374 // On Windows, we might also need to generate a char event to mimic the 1359 // 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 1360 // Windows event flow; on other platforms we create a merged event and test
1376 // the event flow that that platform provides. 1361 // the event flow that that platform provides.
1377 WebKeyboardEvent event_down; 1362 WebKeyboardEvent event_down;
1378 event_down.type = WebInputEvent::RawKeyDown; 1363 event_down.type = WebInputEvent::RawKeyDown;
1379 event_down.modifiers = modifiers; 1364 event_down.modifiers = modifiers;
1380 event_down.windowsKeyCode = code; 1365 event_down.windowsKeyCode = key_code;
1381 event_down.domCode = static_cast<int>( 1366 event_down.domCode = static_cast<int>(dom_code);
1382 ui::KeycodeConverter::CodeStringToDomCode(domString.c_str())); 1367 event_down.domKey = dom_key_int;
1383 1368
1384 if (generate_char) { 1369 if (generate_char) {
1385 event_down.text[0] = text; 1370 event_down.text[0] = text;
1386 event_down.unmodifiedText[0] = text; 1371 event_down.unmodifiedText[0] = text;
1387 } 1372 }
1388 1373
1389 event_down.setKeyIdentifierFromWindowsKeyCode(); 1374 event_down.setKeyIdentifierFromWindowsKeyCode();
1390 1375
1391 if (event_down.modifiers != 0) 1376 if (event_down.modifiers != 0)
1392 event_down.isSystemKey = IsSystemKeyEvent(event_down); 1377 event_down.isSystemKey = IsSystemKeyEvent(event_down);
(...skipping 17 matching lines...) Expand all
1410 // the command will be dispatched to the renderer just before dispatching 1395 // the command will be dispatched to the renderer just before dispatching
1411 // the keyboard event, and then it will be executed in the 1396 // the keyboard event, and then it will be executed in the
1412 // RenderView::handleCurrentKeyboardEvent() method. 1397 // RenderView::handleCurrentKeyboardEvent() method.
1413 // We just simulate the same behavior here. 1398 // We just simulate the same behavior here.
1414 std::string edit_command; 1399 std::string edit_command;
1415 if (GetEditCommand(event_down, &edit_command)) 1400 if (GetEditCommand(event_down, &edit_command))
1416 delegate_->SetEditCommand(edit_command, ""); 1401 delegate_->SetEditCommand(edit_command, "");
1417 1402
1418 HandleInputEventOnViewOrPopup(event_down); 1403 HandleInputEventOnViewOrPopup(event_down);
1419 1404
1420 if (code == ui::VKEY_ESCAPE && !current_drag_data_.isNull()) { 1405 if (key_code == ui::VKEY_ESCAPE && !current_drag_data_.isNull()) {
1421 WebMouseEvent event; 1406 WebMouseEvent event;
1422 InitMouseEvent(WebInputEvent::MouseDown, 1407 InitMouseEvent(WebInputEvent::MouseDown,
1423 pressed_button_, 1408 pressed_button_,
1424 last_mouse_pos_, 1409 last_mouse_pos_,
1425 GetCurrentEventTimeSec(), 1410 GetCurrentEventTimeSec(),
1426 click_count_, 1411 click_count_,
1427 0, 1412 0,
1428 &event); 1413 &event);
1429 FinishDragAndDrop(event, blink::WebDragOperationNone); 1414 FinishDragAndDrop(event, blink::WebDragOperationNone);
1430 } 1415 }
1431 1416
1432 delegate_->ClearEditCommand(); 1417 delegate_->ClearEditCommand();
1433 1418
1434 if (generate_char) { 1419 if (generate_char) {
1435 WebKeyboardEvent event_char = event_up; 1420 WebKeyboardEvent event_char = event_up;
1436 event_char.type = WebInputEvent::Char; 1421 event_char.type = WebInputEvent::Char;
1437 // keyIdentifier is an empty string, unless the Enter key was pressed. 1422 // keyIdentifier is an empty string, unless the Enter key was pressed.
1438 // This behavior is not standard (keyIdentifier itself is not even a 1423 // This behavior is not standard (keyIdentifier itself is not even a
1439 // standard any more), but it matches the actual behavior in Blink. 1424 // standard any more), but it matches the actual behavior in Blink.
1440 if (code != ui::VKEY_RETURN) 1425 if (key_code != ui::VKEY_RETURN)
1441 event_char.keyIdentifier[0] = '\0'; 1426 event_char.keyIdentifier[0] = '\0';
1442 HandleInputEventOnViewOrPopup(event_char); 1427 HandleInputEventOnViewOrPopup(event_char);
1443 } 1428 }
1444 1429
1445 HandleInputEventOnViewOrPopup(event_up); 1430 HandleInputEventOnViewOrPopup(event_up);
1446 } 1431 }
1447 1432
1448 void EventSender::EnableDOMUIEventLogging() {} 1433 void EventSender::EnableDOMUIEventLogging() {}
1449 1434
1450 void EventSender::FireKeyboardEventsToElement() {} 1435 void EventSender::FireKeyboardEventsToElement() {}
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after
2474 2459
2475 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) { 2460 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) {
2476 if (WebPagePopup* popup = view_->pagePopup()) { 2461 if (WebPagePopup* popup = view_->pagePopup()) {
2477 if (!WebInputEvent::isKeyboardEventType(event.type)) 2462 if (!WebInputEvent::isKeyboardEventType(event.type))
2478 return popup->handleInputEvent(event); 2463 return popup->handleInputEvent(event);
2479 } 2464 }
2480 return view_->handleInputEvent(event); 2465 return view_->handleInputEvent(event);
2481 } 2466 }
2482 2467
2483 } // namespace test_runner 2468 } // namespace test_runner
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698