summaryrefslogtreecommitdiffstats
path: root/chrome/test/render_view_test.cc
diff options
context:
space:
mode:
authorhbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-11 04:41:21 +0000
committerhbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-11 04:41:21 +0000
commit8b048323819876b75e3875cd1de5ef01f1f54375 (patch)
tree1397552dd2d5c6d1461d253523e7e0118eb8b520 /chrome/test/render_view_test.cc
parentda4f93efcfa81849826a188b39c92fb3b94a87f6 (diff)
downloadchromium_src-8b048323819876b75e3875cd1de5ef01f1f54375.zip
chromium_src-8b048323819876b75e3875cd1de5ef01f1f54375.tar.gz
chromium_src-8b048323819876b75e3875cd1de5ef01f1f54375.tar.bz2
Implements keyboard events for RenderViewTest.This change implements a function RenderViewTest::SendKeyEvent() that sends a keyboard event to a RenderView object.To emulate not only US keyboards but also non-US keyboards, this change adds a new class "MockKeyboard", which implements a mapping function from a triple <keyboard type, key code, modifiers> to a Unicode character so that engineers can write RenderViewTest cases without taking care of keyboard types.As samples for this new function, I updated my test RenderViewTest.OnHandleKeyboardEvent and added another test RenderViewTest.InsertCharacters.I wish this class help engineers write more RenderViewTest cases.
Review URL: http://codereview.chromium.org/92122 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15748 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/render_view_test.cc')
-rw-r--r--chrome/test/render_view_test.cc54
1 files changed, 54 insertions, 0 deletions
diff --git a/chrome/test/render_view_test.cc b/chrome/test/render_view_test.cc
index 9885334..4096238 100644
--- a/chrome/test/render_view_test.cc
+++ b/chrome/test/render_view_test.cc
@@ -4,12 +4,14 @@
#include "chrome/test/render_view_test.h"
+#include "chrome/common/native_web_keyboard_event.h"
#include "chrome/common/render_messages.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_process_bindings.h"
#include "chrome/renderer/extensions/renderer_extension_bindings.h"
#include "chrome/renderer/js_only_v8_extensions.h"
+#include "webkit/api/public/WebInputEvent.h"
#include "webkit/api/public/WebKit.h"
#include "webkit/api/public/WebScriptSource.h"
#include "webkit/glue/weburlrequest.h"
@@ -74,6 +76,9 @@ void RenderViewTest::SetUp() {
view_ = RenderView::Create(&render_thread_, NULL, NULL, kOpenerId,
WebPreferences(),
new SharedRenderViewCounter(0), kRouteId);
+
+ // Attach a pseudo keyboard device to this object.
+ mock_keyboard_.reset(new MockKeyboard());
}
void RenderViewTest::TearDown() {
render_thread_.SendCloseMessage();
@@ -89,4 +94,53 @@ void RenderViewTest::TearDown() {
WebKit::shutdown();
msg_loop_.RunAllPending();
+
+ mock_keyboard_.reset();
+}
+
+int RenderViewTest::SendKeyEvent(MockKeyboard::Layout layout,
+ int key_code,
+ MockKeyboard::Modifiers modifiers,
+ std::wstring* output) {
+#if defined(OS_WIN)
+ // Retrieve the Unicode character for the given tuple (keyboard-layout,
+ // key-code, and modifiers).
+ // Exit when a keyboard-layout driver cannot assign a Unicode character to
+ // the tuple to prevent sending an invalid key code to the RenderView object.
+ CHECK(mock_keyboard_.get());
+ CHECK(output);
+ int length = mock_keyboard_->GetCharacters(layout, key_code, modifiers,
+ output);
+ if (length != 1)
+ return -1;
+
+ // Create IPC messages from Windows messages and send them to our
+ // back-end.
+ // A keyboard event of Windows consists of three Windows messages:
+ // WM_KEYDOWN, WM_CHAR, and WM_KEYUP.
+ // WM_KEYDOWN and WM_KEYUP sends virtual-key codes. On the other hand,
+ // WM_CHAR sends a composed Unicode character.
+ NativeWebKeyboardEvent keydown_event(NULL, WM_KEYDOWN, key_code, 0);
+ scoped_ptr<IPC::Message> keydown_message(new ViewMsg_HandleInputEvent(0));
+ keydown_message->WriteData(reinterpret_cast<const char*>(&keydown_event),
+ sizeof(WebKit::WebKeyboardEvent));
+ view_->OnHandleInputEvent(*keydown_message);
+
+ NativeWebKeyboardEvent char_event(NULL, WM_CHAR, (*output)[0], 0);
+ scoped_ptr<IPC::Message> char_message(new ViewMsg_HandleInputEvent(0));
+ char_message->WriteData(reinterpret_cast<const char*>(&char_event),
+ sizeof(WebKit::WebKeyboardEvent));
+ view_->OnHandleInputEvent(*char_message);
+
+ NativeWebKeyboardEvent keyup_event(NULL, WM_KEYUP, key_code, 0);
+ scoped_ptr<IPC::Message> keyup_message(new ViewMsg_HandleInputEvent(0));
+ keyup_message->WriteData(reinterpret_cast<const char*>(&keyup_event),
+ sizeof(WebKit::WebKeyboardEvent));
+ view_->OnHandleInputEvent(*keyup_message);
+
+ return length;
+#else
+ NOTIMPLEMENTED();
+ return L'\0';
+#endif
}