 Chromium Code Reviews
 Chromium Code Reviews Issue 6630001:
  Allow webdriver users to choose between sending the key events when...  (Closed) 
  Base URL: http://src.chromium.org/svn/trunk/src/
    
  
    Issue 6630001:
  Allow webdriver users to choose between sending the key events when...  (Closed) 
  Base URL: http://src.chromium.org/svn/trunk/src/| Index: chrome/browser/automation/testing_automation_provider.cc | 
| =================================================================== | 
| --- chrome/browser/automation/testing_automation_provider.cc (revision 76851) | 
| +++ chrome/browser/automation/testing_automation_provider.cc (working copy) | 
| @@ -2262,6 +2262,8 @@ | 
| browser_handler_map["SendKeyEventToActiveTab"] = | 
| &TestingAutomationProvider::SendKeyEventToActiveTab; | 
| + browser_handler_map["SendKeyEventToActiveBrowserWindow"] = | 
| + &TestingAutomationProvider::SendKeyEventToActiveBrowserWindow; | 
| browser_handler_map["GetNTPThumbnailMode"] = | 
| &TestingAutomationProvider::GetNTPThumbnailMode; | 
| @@ -4567,99 +4569,165 @@ | 
| base::CloseProcessHandle(process); | 
| } | 
| -void TestingAutomationProvider::SendKeyEventToActiveTab( | 
| - Browser* browser, | 
| +bool TestingAutomationProvider::BuildNativeWebKeyEventFromArgs( | 
| DictionaryValue* args, | 
| - IPC::Message* reply_message) { | 
| + IPC::Message* reply_message, | 
| + NativeWebKeyboardEvent* event) { | 
| int type, modifiers; | 
| bool is_system_key; | 
| string16 unmodified_text, text; | 
| std::string key_identifier; | 
| - NativeWebKeyboardEvent event; | 
| - if (!args->GetInteger("type", &type)) { | 
| + if (!args->GetInteger("type", &type)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'type' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| if (!args->GetBoolean("isSystemKey", &is_system_key)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'isSystemKey' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| if (!args->GetString("unmodifiedText", &unmodified_text)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'unmodifiedText' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| if (!args->GetString("text", &text)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'text' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| - if (!args->GetInteger("nativeKeyCode", &event.nativeKeyCode)) { | 
| + if (!args->GetInteger("nativeKeyCode", &event->nativeKeyCode)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'nativeKeyCode' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| - if (!args->GetInteger("windowsKeyCode", &event.windowsKeyCode)) { | 
| + if (!args->GetInteger("windowsKeyCode", &event->windowsKeyCode)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'windowsKeyCode' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| if (!args->GetInteger("modifiers", &modifiers)) { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'modifiers' missing or invalid."); | 
| - return; | 
| + return false; | 
| } | 
| if (args->GetString("keyIdentifier", &key_identifier)) { | 
| - base::strlcpy(event.keyIdentifier, | 
| + base::strlcpy(event->keyIdentifier, | 
| key_identifier.c_str(), | 
| WebKit::WebKeyboardEvent::keyIdentifierLengthCap); | 
| } else { | 
| - event.setKeyIdentifierFromWindowsKeyCode(); | 
| + event->setKeyIdentifierFromWindowsKeyCode(); | 
| } | 
| if (type == automation::kRawKeyDownType) { | 
| - event.type = WebKit::WebInputEvent::RawKeyDown; | 
| + event->type = WebKit::WebInputEvent::RawKeyDown; | 
| } else if (type == automation::kKeyDownType) { | 
| - event.type = WebKit::WebInputEvent::KeyDown; | 
| + event->type = WebKit::WebInputEvent::KeyDown; | 
| } else if (type == automation::kKeyUpType) { | 
| - event.type = WebKit::WebInputEvent::KeyUp; | 
| + event->type = WebKit::WebInputEvent::KeyUp; | 
| } else if (type == automation::kCharType) { | 
| - event.type = WebKit::WebInputEvent::Char; | 
| + event->type = WebKit::WebInputEvent::Char; | 
| } else { | 
| AutomationJSONReply reply(this, reply_message); | 
| reply.SendError("'type' refers to an unrecognized keyboard event type"); | 
| - return; | 
| + return false; | 
| } | 
| string16 unmodified_text_truncated = unmodified_text.substr( | 
| 0, WebKit::WebKeyboardEvent::textLengthCap - 1); | 
| - memcpy(event.unmodifiedText, | 
| + memcpy(event->unmodifiedText, | 
| unmodified_text_truncated.c_str(), | 
| unmodified_text_truncated.length() + 1); | 
| string16 text_truncated = text.substr( | 
| 0, WebKit::WebKeyboardEvent::textLengthCap - 1); | 
| - memcpy(event.text, text_truncated.c_str(), text_truncated.length() + 1); | 
| + memcpy(event->text, text_truncated.c_str(), text_truncated.length() + 1); | 
| - event.modifiers = 0; | 
| + event->modifiers = 0; | 
| if (modifiers & automation::kShiftKeyMask) | 
| - event.modifiers |= WebKit::WebInputEvent::ShiftKey; | 
| + event->modifiers |= WebKit::WebInputEvent::ShiftKey; | 
| if (modifiers & automation::kControlKeyMask) | 
| - event.modifiers |= WebKit::WebInputEvent::ControlKey; | 
| + event->modifiers |= WebKit::WebInputEvent::ControlKey; | 
| if (modifiers & automation::kAltKeyMask) | 
| - event.modifiers |= WebKit::WebInputEvent::AltKey; | 
| + event->modifiers |= WebKit::WebInputEvent::AltKey; | 
| if (modifiers & automation::kMetaKeyMask) | 
| - event.modifiers |= WebKit::WebInputEvent::MetaKey; | 
| + event->modifiers |= WebKit::WebInputEvent::MetaKey; | 
| - event.isSystemKey = is_system_key; | 
| - event.timeStampSeconds = base::Time::Now().ToDoubleT(); | 
| - event.skip_in_browser = true; | 
| + event->isSystemKey = is_system_key; | 
| + event->timeStampSeconds = base::Time::Now().ToDoubleT(); | 
| + event->skip_in_browser = true; | 
| + return true; | 
| +} | 
| + | 
| +void TestingAutomationProvider::SendKeyEventToActiveTab( | 
| + Browser* browser, | 
| + DictionaryValue* args, | 
| + IPC::Message* reply_message) { | 
| + NativeWebKeyboardEvent event; | 
| + // BuildNativeWebKeyEventFromArgs handles telling what when wrong and sending | 
| + // the reply message, if it failed we just have to stop here. | 
| + if (!BuildNativeWebKeyEventFromArgs(args, reply_message, &event)) { | 
| + return; | 
| + } | 
| 
Paweł Hajdan Jr.
2011/03/08 09:25:13
nit: Remove the braces {} for a 1-line "if" statem
 
timothe faudot
2011/03/08 09:34:23
Done, also changed the same call on SendKeyEventTo
 | 
| + | 
| new InputEventAckNotificationObserver(this, reply_message, event.type); | 
| - browser->GetSelectedTabContents()->render_view_host()-> | 
| + browser->GetSelectedTabContents()->render_view_host()-> | 
| ForwardKeyboardEvent(event); | 
| + return; | 
| } | 
| +void TestingAutomationProvider::SendKeyEventToActiveBrowserWindow( | 
| + Browser* browser, | 
| + DictionaryValue* args, | 
| + IPC::Message* reply_message) { | 
| + NativeWebKeyboardEvent event; | 
| + // BuildNativeWebKeyEventFromArgs handles telling what when wrong and sending | 
| + // the reply message, if it failed we just have to stop here. | 
| + if (!BuildNativeWebKeyEventFromArgs(args, reply_message, &event)) { | 
| + return; | 
| + } | 
| + | 
| + // The ui_controls::SendKeyPress function will generate up and down events | 
| + // from one keycode for us so we only need to send one event to it. | 
| + // Filtering is done via the RawKeyDown type because it is the one sent by | 
| + // the current webdriver implementation of WebElement.SendKeys(...). | 
| + DictionaryValue* was_processed = new DictionaryValue(); | 
| 
Paweł Hajdan Jr.
2011/03/08 09:25:13
nit: |was_processed| is not a good name for the en
 
timothe faudot
2011/03/08 09:34:23
Done.
 | 
| + if (event.type != WebKit::WebInputEvent::RawKeyDown) { | 
| + AutomationJSONReply reply(this, reply_message); | 
| + was_processed->SetBoolean("processed", false); | 
| + reply.SendSuccess(was_processed); | 
| + return; | 
| + } | 
| + BrowserWindow* browser_window = browser->window(); | 
| + if (!browser_window) { | 
| + AutomationJSONReply reply(this, reply_message); | 
| + reply.SendError("Could not get the browser window"); | 
| + return; | 
| + } | 
| + gfx::NativeWindow window = browser_window->GetNativeHandle(); | 
| + if (!window) { | 
| + AutomationJSONReply reply(this, reply_message); | 
| + reply.SendError("Could not get the browser window handle"); | 
| + return; | 
| + } | 
| + bool control = !!(event.modifiers & WebKit::WebInputEvent::ControlKey); | 
| + bool shift = !!(event.modifiers & WebKit::WebInputEvent::ShiftKey); | 
| + bool alt = !!(event.modifiers & WebKit::WebInputEvent::AltKey); | 
| + bool meta = !!(event.modifiers & WebKit::WebInputEvent::MetaKey); | 
| + if (!ui_controls::SendKeyPress( | 
| + window, static_cast<ui::KeyboardCode>(event.nativeKeyCode), | 
| + control, shift, alt, meta)) { | 
| + AutomationJSONReply reply(this, reply_message); | 
| + reply.SendError("Could not send the native key event"); | 
| + return; | 
| 
Paweł Hajdan Jr.
2011/03/08 09:25:13
nit: This "return" statement seems not needed now.
 
timothe faudot
2011/03/08 09:34:23
Done.
 | 
| + } else { | 
| + AutomationJSONReply reply(this, reply_message); | 
| + was_processed->SetBoolean("processed", true); | 
| + reply.SendSuccess(was_processed); | 
| + return; | 
| 
Paweł Hajdan Jr.
2011/03/08 09:25:13
nit: This "return" statement seems not needed now.
 
timothe faudot
2011/03/08 09:34:23
Done.
 | 
| + } | 
| +} | 
| + | 
| // Sample JSON input: { "command": "GetNTPThumbnailMode" } | 
| // For output, refer to GetNTPThumbnailMode() in | 
| // chrome/test/pyautolib/pyauto.py. | 
| @@ -4772,8 +4840,7 @@ | 
| section = MENU_THUMB; | 
| } else if (section_name.compare("recently_closed") == 0) { | 
| section = MENU_RECENT; | 
| - } | 
| - else { | 
| + } else { | 
| reply.SendError(StringPrintf("Unexpected section name: '%s'", | 
| section_name.c_str())); | 
| return; |