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

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

Issue 1214823002: Revert of [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: 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
« no previous file with comments | « no previous file | content/browser/renderer_host/web_input_event_aura.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
10 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
12 #include "components/test_runner/mock_spell_check.h" 11 #include "components/test_runner/mock_spell_check.h"
13 #include "components/test_runner/test_interfaces.h" 12 #include "components/test_runner/test_interfaces.h"
14 #include "components/test_runner/web_test_delegate.h" 13 #include "components/test_runner/web_test_delegate.h"
15 #include "components/test_runner/web_test_proxy.h" 14 #include "components/test_runner/web_test_proxy.h"
16 #include "gin/handle.h" 15 #include "gin/handle.h"
17 #include "gin/object_template_builder.h" 16 #include "gin/object_template_builder.h"
18 #include "gin/wrappable.h" 17 #include "gin/wrappable.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 18 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebVector.h" 19 #include "third_party/WebKit/public/platform/WebVector.h"
21 #include "third_party/WebKit/public/web/WebContextMenuData.h" 20 #include "third_party/WebKit/public/web/WebContextMenuData.h"
22 #include "third_party/WebKit/public/web/WebFrame.h" 21 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebKit.h" 22 #include "third_party/WebKit/public/web/WebKit.h"
24 #include "third_party/WebKit/public/web/WebPagePopup.h" 23 #include "third_party/WebKit/public/web/WebPagePopup.h"
25 #include "third_party/WebKit/public/web/WebView.h" 24 #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"
29 #include "ui/events/keycodes/dom/keycode_converter.h" 25 #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"
32 #include "ui/events/keycodes/keyboard_codes.h" 26 #include "ui/events/keycodes/keyboard_codes.h"
33 #include "v8/include/v8.h" 27 #include "v8/include/v8.h"
34 28
35 using blink::WebContextMenuData; 29 using blink::WebContextMenuData;
36 using blink::WebDragData; 30 using blink::WebDragData;
37 using blink::WebDragOperationsMask; 31 using blink::WebDragOperationsMask;
38 using blink::WebFloatPoint; 32 using blink::WebFloatPoint;
39 using blink::WebFrame; 33 using blink::WebFrame;
40 using blink::WebGestureEvent; 34 using blink::WebGestureEvent;
41 using blink::WebInputEvent; 35 using blink::WebInputEvent;
(...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 button_type, 1234 button_type,
1241 last_mouse_pos_, 1235 last_mouse_pos_,
1242 GetCurrentEventTimeSec(), 1236 GetCurrentEventTimeSec(),
1243 click_count_, 1237 click_count_,
1244 modifiers, 1238 modifiers,
1245 &event); 1239 &event);
1246 DoMouseUp(event); 1240 DoMouseUp(event);
1247 } 1241 }
1248 } 1242 }
1249 1243
1250 ui::DomCode convertKeyCodeStrToDomCode(const std::string key_code_str, 1244 void EventSender::KeyDown(const std::string& code_str,
1251 bool &need_modifier)
1252 {
1253 // Convert key_code_str to DomCode passed from the layout tests.
1254 const struct DomCodeToKeyCodeStr {
1255 ui::DomCode dom_code;
1256 std::string key_code_str;
1257 } kDomCodeToKeyCodeStrMap[] = {
1258 {ui::DomCode::ENTER, "\n"},
1259 {ui::DomCode::ENTER, "\r"},
1260 {ui::DomCode::ARROW_RIGHT, "rightArrow"},
1261 {ui::DomCode::ARROW_LEFT, "leftArrow"},
1262 {ui::DomCode::ARROW_DOWN, "downArrow"},
1263 {ui::DomCode::ARROW_UP, "upArrow"},
1264 {ui::DomCode::INSERT, "insert"},
1265 {ui::DomCode::DEL, "delete"},
1266 {ui::DomCode::PAGE_UP, "pageUp"},
1267 {ui::DomCode::PAGE_DOWN, "pageDown"},
1268 {ui::DomCode::HOME, "home"},
1269 {ui::DomCode::END, "end"},
1270 {ui::DomCode::PRINT_SCREEN, "printScreen"},
1271 {ui::DomCode::CONTEXT_MENU, "menu"},
1272 {ui::DomCode::CONTROL_LEFT, "leftControl"},
1273 {ui::DomCode::CONTROL_RIGHT, "rightControl"},
1274 {ui::DomCode::SHIFT_LEFT, "leftShift"},
1275 {ui::DomCode::SHIFT_RIGHT, "rightShift"},
1276 {ui::DomCode::ALT_LEFT, "leftAlt"},
1277 {ui::DomCode::ALT_RIGHT, "rightAlt"},
1278 {ui::DomCode::NUM_LOCK, "numLock"},
1279 {ui::DomCode::BACKSPACE, "backspace"},
1280 {ui::DomCode::ESCAPE, "escape"},
1281 };
1282
1283 for (const auto& it : kDomCodeToKeyCodeStrMap) {
1284 if (it.key_code_str == key_code_str)
1285 return it.dom_code;
1286 }
1287
1288 // F1..24 DomCode Value.
1289 for (int i = 1; i <= 24; ++i) {
1290 std::string function_key_name = base::StringPrintf("F%d", i);
1291 if (function_key_name == key_code_str) {
1292 int num = static_cast<int>(ui::DomCode::F1) + ( i -1);
1293 return static_cast<ui::DomCode>(num);
1294 }
1295 }
1296
1297 // Printable Value
1298 for (const auto& it : ui::kPrintableCodeMap) {
1299 base::char16 str = static_cast<base::char16>(key_code_str.at(0));
1300 if (it.character[0] == str || it.character[1] == str) {
1301 need_modifier = NeedsShiftModifier(str);
1302 return it.dom_code;
1303 }
1304 }
1305
1306 // Fallback to get corresponding DomCode value.
1307 return ui::UsLayoutKeyboardCodeToDomCode(
1308 static_cast<ui::KeyboardCode>(key_code_str.at(0)));
1309 }
1310
1311 void EventSender::KeyDown(const std::string& key_code_str,
1312 int modifiers, 1245 int modifiers,
1313 KeyLocationCode location) { 1246 KeyLocationCode location) {
1314 // FIXME: I'm not exactly sure how we should convert the string to a key 1247 // FIXME: I'm not exactly sure how we should convert the string to a key
1315 // event. This seems to work in the cases I tested. 1248 // event. This seems to work in the cases I tested.
1316 // FIXME: Should we also generate a KEY_UP? 1249 // FIXME: Should we also generate a KEY_UP?
1317 1250
1318 bool generate_char = false; 1251 bool generate_char = false;
1319 bool need_modifier = false;
1320 1252
1321 ui::DomCode dom_code = convertKeyCodeStrToDomCode(key_code_str, 1253 // Convert \n -> VK_RETURN. Some layout tests use \n to mean "Enter", when
1322 need_modifier); 1254 // Windows uses \r for "Enter".
1323 ui::KeyboardCode key_code; 1255 int code = 0;
1324 ui::DomKey dom_key; 1256 int text = 0;
1325 base::char16 dom_key_char = 0; 1257 bool needs_shift_key_modifier = false;
1258 std::string domString;
1326 1259
1327 // Modifier helps in getting correct dom_key_char. 1260 if ("\n" == code_str) {
1328 if (need_modifier) 1261 generate_char = true;
1329 modifier |= ui::EF_SHIFT_DOWN; 1262 text = code = ui::VKEY_RETURN;
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 }
1330 1365
1331 if (!DomCodeToUsLayoutMeaning(dom_code, modifier, &dom_key, &dom_key_char, 1366 if ("(" == code_str) {
1332 &key_code)) { 1367 code = '9';
1333 dom_key = ui::DomKey::NONE; 1368 needs_shift_key_modifier = true;
1369 }
1334 } 1370 }
1335 1371
1336 // For one generated keyboard event, we need to generate a keyDown/keyUp 1372 // For one generated keyboard event, we need to generate a keyDown/keyUp
1337 // pair; 1373 // pair;
1338 // On Windows, we might also need to generate a char event to mimic the 1374 // On Windows, we might also need to generate a char event to mimic the
1339 // Windows event flow; on other platforms we create a merged event and test 1375 // Windows event flow; on other platforms we create a merged event and test
1340 // the event flow that that platform provides. 1376 // the event flow that that platform provides.
1341 WebKeyboardEvent event_down; 1377 WebKeyboardEvent event_down;
1342 event_down.type = WebInputEvent::RawKeyDown; 1378 event_down.type = WebInputEvent::RawKeyDown;
1343 event_down.modifiers = modifiers; 1379 event_down.modifiers = modifiers;
1344 event_down.windowsKeyCode = key_code; 1380 event_down.windowsKeyCode = code;
1345 event_down.domCode = static_cast<int>(dom_code); 1381 event_down.domCode = static_cast<int>(
1382 ui::KeycodeConverter::CodeStringToDomCode(domString.c_str()));
1346 1383
1347 if (dom_key == ui::DomKey::CHARACTER || dom_code == ui::DomCode::ENTER) { 1384 if (generate_char) {
1348 event_down.domKey = static_cast<int>(dom_key_char); 1385 event_down.text[0] = text;
1349 event_down.unmodifiedText[0] = event_down.text[0] = dom_key_char; 1386 event_down.unmodifiedText[0] = text;
1350 generate_char = true;
1351 } else {
1352 event_down.domKey = DOM_KEY_PRINT_NON_DIFF + static_cast<int>(dom_key);
1353 } 1387 }
1354 1388
1355 event_down.setKeyIdentifierFromWindowsKeyCode(); 1389 event_down.setKeyIdentifierFromWindowsKeyCode();
1356 1390
1357 if (event_down.modifiers != 0) 1391 if (event_down.modifiers != 0)
1358 event_down.isSystemKey = IsSystemKeyEvent(event_down); 1392 event_down.isSystemKey = IsSystemKeyEvent(event_down);
1359 1393
1394 if (needs_shift_key_modifier)
1395 event_down.modifiers |= WebInputEvent::ShiftKey;
1396
1360 // See if KeyLocation argument is given. 1397 // See if KeyLocation argument is given.
1361 if (location == DOMKeyLocationNumpad) 1398 if (location == DOMKeyLocationNumpad)
1362 event_down.modifiers |= WebInputEvent::IsKeyPad; 1399 event_down.modifiers |= WebInputEvent::IsKeyPad;
1363 1400
1364 WebKeyboardEvent event_up; 1401 WebKeyboardEvent event_up;
1365 event_up = event_down; 1402 event_up = event_down;
1366 event_up.type = WebInputEvent::KeyUp; 1403 event_up.type = WebInputEvent::KeyUp;
1367 // EventSender.m forces a layout here, with at least one 1404 // EventSender.m forces a layout here, with at least one
1368 // test (fast/forms/focus-control-to-page.html) relying on this. 1405 // test (fast/forms/focus-control-to-page.html) relying on this.
1369 if (force_layout_on_events_) 1406 if (force_layout_on_events_)
1370 view_->layout(); 1407 view_->layout();
1371 1408
1372 // In the browser, if a keyboard event corresponds to an editor command, 1409 // In the browser, if a keyboard event corresponds to an editor command,
1373 // the command will be dispatched to the renderer just before dispatching 1410 // the command will be dispatched to the renderer just before dispatching
1374 // the keyboard event, and then it will be executed in the 1411 // the keyboard event, and then it will be executed in the
1375 // RenderView::handleCurrentKeyboardEvent() method. 1412 // RenderView::handleCurrentKeyboardEvent() method.
1376 // We just simulate the same behavior here. 1413 // We just simulate the same behavior here.
1377 std::string edit_command; 1414 std::string edit_command;
1378 if (GetEditCommand(event_down, &edit_command)) 1415 if (GetEditCommand(event_down, &edit_command))
1379 delegate_->SetEditCommand(edit_command, ""); 1416 delegate_->SetEditCommand(edit_command, "");
1380 1417
1381 HandleInputEventOnViewOrPopup(event_down); 1418 HandleInputEventOnViewOrPopup(event_down);
1382 1419
1383 if (dom_code == ui::DomCode::ESCAPE && !current_drag_data_.isNull()) { 1420 if (code == ui::VKEY_ESCAPE && !current_drag_data_.isNull()) {
1384 WebMouseEvent event; 1421 WebMouseEvent event;
1385 InitMouseEvent(WebInputEvent::MouseDown, 1422 InitMouseEvent(WebInputEvent::MouseDown,
1386 pressed_button_, 1423 pressed_button_,
1387 last_mouse_pos_, 1424 last_mouse_pos_,
1388 GetCurrentEventTimeSec(), 1425 GetCurrentEventTimeSec(),
1389 click_count_, 1426 click_count_,
1390 0, 1427 0,
1391 &event); 1428 &event);
1392 FinishDragAndDrop(event, blink::WebDragOperationNone); 1429 FinishDragAndDrop(event, blink::WebDragOperationNone);
1393 } 1430 }
1394 1431
1395 delegate_->ClearEditCommand(); 1432 delegate_->ClearEditCommand();
1396 1433
1397 if (generate_char) { 1434 if (generate_char) {
1398 WebKeyboardEvent event_char = event_up; 1435 WebKeyboardEvent event_char = event_up;
1399 event_char.type = WebInputEvent::Char; 1436 event_char.type = WebInputEvent::Char;
1400 // keyIdentifier is an empty string, unless the Enter key was pressed. 1437 // keyIdentifier is an empty string, unless the Enter key was pressed.
1401 // This behavior is not standard (keyIdentifier itself is not even a 1438 // This behavior is not standard (keyIdentifier itself is not even a
1402 // standard any more), but it matches the actual behavior in Blink. 1439 // standard any more), but it matches the actual behavior in Blink.
1403 if (dom_code != ui::DomCode::ENTER) 1440 if (code != ui::VKEY_RETURN)
1404 event_char.keyIdentifier[0] = '\0'; 1441 event_char.keyIdentifier[0] = '\0';
1405 HandleInputEventOnViewOrPopup(event_char); 1442 HandleInputEventOnViewOrPopup(event_char);
1406 } 1443 }
1407 1444
1408 HandleInputEventOnViewOrPopup(event_up); 1445 HandleInputEventOnViewOrPopup(event_up);
1409 } 1446 }
1410 1447
1411 void EventSender::EnableDOMUIEventLogging() {} 1448 void EventSender::EnableDOMUIEventLogging() {}
1412 1449
1413 void EventSender::FireKeyboardEventsToElement() {} 1450 void EventSender::FireKeyboardEventsToElement() {}
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after
2437 2474
2438 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) { 2475 bool EventSender::HandleInputEventOnViewOrPopup(const WebInputEvent& event) {
2439 if (WebPagePopup* popup = view_->pagePopup()) { 2476 if (WebPagePopup* popup = view_->pagePopup()) {
2440 if (!WebInputEvent::isKeyboardEventType(event.type)) 2477 if (!WebInputEvent::isKeyboardEventType(event.type))
2441 return popup->handleInputEvent(event); 2478 return popup->handleInputEvent(event);
2442 } 2479 }
2443 return view_->handleInputEvent(event); 2480 return view_->handleInputEvent(event);
2444 } 2481 }
2445 2482
2446 } // namespace test_runner 2483 } // namespace test_runner
OLDNEW
« no previous file with comments | « no previous file | content/browser/renderer_host/web_input_event_aura.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698