diff options
author | suzhe@chromium.org <suzhe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-23 02:52:06 +0000 |
---|---|---|
committer | suzhe@chromium.org <suzhe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-23 02:52:06 +0000 |
commit | 21abcc74837a6cb9537a8026a1b12efc9da402f0 (patch) | |
tree | 9860885689c7af40e05a14513b09a98dc8642542 /chrome/test | |
parent | 44c8966e5995e827d0d0b3fd2d4fcee0f56bbdff (diff) | |
download | chromium_src-21abcc74837a6cb9537a8026a1b12efc9da402f0.zip chromium_src-21abcc74837a6cb9537a8026a1b12efc9da402f0.tar.gz chromium_src-21abcc74837a6cb9537a8026a1b12efc9da402f0.tar.bz2 |
Implements tests for testing browser's overall key events handling behavior.
This CL implements some basic tests for testing browser's overall key events handling behavior. This CL depends on http://codereview.chromium.org/235039 and http://codereview.chromium.org/195062. Currently, only Linux and Windows are supported.
The tests assume US keyboard layout is used and no IME is activated. We still need to investigate how to write tests that involving different keyboard layout and input methods.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/268035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29866 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/data/keyevents_test.html | 165 | ||||
-rw-r--r-- | chrome/test/interactive_ui/interactive_ui_tests.gypi | 2 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 7 | ||||
-rw-r--r-- | chrome/test/ui_test_utils_linux.cc | 72 | ||||
-rw-r--r-- | chrome/test/ui_test_utils_mac.cc | 20 | ||||
-rw-r--r-- | chrome/test/ui_test_utils_win.cc | 42 |
6 files changed, 308 insertions, 0 deletions
diff --git a/chrome/test/data/keyevents_test.html b/chrome/test/data/keyevents_test.html new file mode 100644 index 0000000..140fad0 --- /dev/null +++ b/chrome/test/data/keyevents_test.html @@ -0,0 +1,165 @@ +<html><head> +<meta http-equiv="content-type" content="text/html; charset=utf-8"> +<script type="text/javascript"> +var defaultActions = { + 'keydown': true, + 'keypress': true, + 'keyup': true, + 'textInput': true, +}; +var keyEventResult = []; +var focusedElement = ""; +var lastFocusedElement = ""; +var testStarted = false; +var keyEventCount = 0; + +function init() { + document.addEventListener("keydown", handleEvent, false); + document.addEventListener("keypress", handleEvent, false); + document.addEventListener("keyup", handleEvent, false); + document.addEventListener("textInput", handleEvent, false); + window.addEventListener("blur", handleWindowBlur, false); +} + +function log(text) { + document.getElementById('log').innerHTML += text + '<br/>'; +} + +function setDefaultAction(type, value) { + defaultActions[type] = value; + document.getElementById(type).checked = !value; + return defaultActions[type]; +} + +function startTest() { + if (!testStarted) { + clearResult(); + testStarted = true; + log("Start test."); + return true; + } + return false; +} + +function finishTest() { + testStarted = false; + window.domAutomationController.setAutomationId(0); + window.domAutomationController.send("FINISHED"); + log("Finish test."); +} + +function handleEvent(e) { + var prefixes = { + 'keydown': 'D', + 'keypress': 'P', + 'keyup': 'U', + 'textInput': 'T', + }; + + var evt = e || window.event; + var result = prefixes[evt.type] + ' '; + if (evt.type == 'textInput') { + result += evt.data; + } else { + // On Linux, the keydown event of a modifier key doesn't have the + // corresponding modifier attribute set, while the keyup event does have, + // eg. pressing and releasing ctrl may generate a keydown event with + // ctrlKey=false and a keyup event with ctrlKey=true. + // But Windows and Mac have opposite behavior than Linux. + // To make the C++ testing code simpler, if it's a modifier key event, + // then ignores the corresponding modifier attribute by setting it to true. + var keyId = evt.keyIdentifier; + result += (evt.keyCode + ' ' + evt.charCode + ' ' + + (keyId == 'Control' ? true : evt.ctrlKey) + ' ' + + (keyId == 'Shift' ? true : evt.shiftKey) + ' ' + + (keyId == 'Alt' ? true : evt.altKey)); + } + keyEventResult.push(result); + log(result); + + if (testStarted) { + if (evt.type == "keydown") { + ++keyEventCount; + } else if (evt.type == "keyup") { + --keyEventCount; + if (keyEventCount == 0) + finishTest(); + } + } + + if (!defaultActions[evt.type]) { + if (evt.preventDefault) evt.preventDefault(); + if (evt.stopPropagation) evt.stopPropagation(); + } + return defaultActions[evt.type]; +} + +function handleWindowBlur() { + if (testStarted) + finishTest(); +} + +function clearResult() { + keyEventResult = []; + testStarted = false; + keyEventCount = 0; + document.getElementById('log').innerHTML = ""; + return true; +} + +function setFocusedElement(id) { + if (id == "" && focusedElement != "") { + var elem = document.getElementById(focusedElement); + if (elem) { + elem.blur(); + return true; + } + } else { + var elem = document.getElementById(id); + if (elem) { + elem.focus(); + return true; + } + } + return false; +} + +function onFocus(element) { + focusedElement = element.id; + log("Focus: " + focusedElement); +} + +function onBlur(element) { + focusedElement = ""; + lastFocusedElement = element.id; + log("Blur: " + element.id); +} + +function onClick(element) { + if (defaultActions[element.id] != undefined) + defaultActions[element.id] = !element.checked; +} +</script> +</head> +<body onload="init()"> + <input type="checkbox" id="keydown" onclick="onClick(this)">keydown</input> + <input type="checkbox" id="keypress" onclick="onClick(this)">keypress</input> + <input type="checkbox" id="keyup" onclick="onClick(this)">keyup</input> + <input type="checkbox" id="textInput" onclick="onClick(this)">textInput</input> + <br/> + <input type="checkbox" id="1" accesskey='1' + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <input type="checkbox" id="2" accesskey='2' + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <input type="checkbox" id="3" accesskey='3' + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <input type="checkbox" id="D" accesskey='D' + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <input type="text" id="A" accesskey="A" + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <input type="text" id="B" accesskey="B" + onfocus="onFocus(this)" onblur="onBlur(this)"/> + <button id="clear" accesskey='C' onclick="clearResult()">Clear</button> + <p id="log"></p> +</body> +</html> diff --git a/chrome/test/interactive_ui/interactive_ui_tests.gypi b/chrome/test/interactive_ui/interactive_ui_tests.gypi index fa1f846..3e0ed2f 100644 --- a/chrome/test/interactive_ui/interactive_ui_tests.gypi +++ b/chrome/test/interactive_ui/interactive_ui_tests.gypi @@ -30,6 +30,7 @@ 'sources': [ '<(DEPTH)/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc', '<(DEPTH)/chrome/browser/browser_focus_uitest.cc', + '<(DEPTH)/chrome/browser/browser_keyevents_browsertest.cc', '<(DEPTH)/chrome/browser/debugger/devtools_sanity_unittest.cc', '<(DEPTH)/chrome/browser/views/bookmark_bar_view_test.cc', '<(DEPTH)/chrome/browser/blocked_popup_container_interactive_uitest.cc', @@ -73,6 +74,7 @@ # TODO(port) '<(DEPTH)/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc', '<(DEPTH)/chrome/browser/browser_focus_uitest.cc', + '<(DEPTH)/chrome/browser/browser_keyevents_browsertest.cc', '<(DEPTH)/chrome/browser/debugger/devtools_sanity_unittest.cc', '<(DEPTH)/chrome/browser/views/bookmark_bar_view_test.cc', '<(DEPTH)/chrome/browser/blocked_popup_container_interactive_uitest.cc', diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 59f32b1b..ae0d44e 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -9,6 +9,7 @@ #include "base/basictypes.h" #include "base/string16.h" +#include "chrome/browser/view_ids.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_type.h" @@ -122,6 +123,12 @@ int FindInPage(TabContents* tab, bool case_sensitive, int* ordinal); +// Returns true if the View is focused. +bool IsViewFocused(const Browser* browser, ViewID vid); + +// Simulates a mouse click on a View in the browser. +void ClickOnView(const Browser* browser, ViewID vid); + // Register |observer| for the given |type| and run the message loop until // either the observer posts a quit task or we timeout. void RegisterAndWait(NotificationType::Type type, diff --git a/chrome/test/ui_test_utils_linux.cc b/chrome/test/ui_test_utils_linux.cc new file mode 100644 index 0000000..95f757e --- /dev/null +++ b/chrome/test/ui_test_utils_linux.cc @@ -0,0 +1,72 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/ui_test_utils.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_window.h" +#include "chrome/browser/automation/ui_controls.h" +#if defined(TOOLKIT_VIEWS) +#include "chrome/browser/views/frame/browser_view.h" +#endif + +#include "chrome/browser/gtk/view_id_util.h" + +namespace ui_test_utils { + +namespace { + +// Check if the focused widget for |root| is |target| or a child of |target|. +static bool IsWidgetInFocusChain(GtkWidget* root, GtkWidget* target) { + GtkWidget* iter = root; + + while (iter) { + if (iter == target) + return true; + + if (!GTK_IS_CONTAINER(iter)) + return false; + + iter = GTK_CONTAINER(iter)->focus_child; + } + + return false; +} + +} // namespace + +bool IsViewFocused(const Browser* browser, ViewID vid) { + BrowserWindow* browser_window = browser->window(); + DCHECK(browser_window); + gfx::NativeWindow window = browser_window->GetNativeHandle(); + DCHECK(window); + GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); + DCHECK(widget); + return IsWidgetInFocusChain(GTK_WIDGET(window), widget); +} + +void ClickOnView(const Browser* browser, ViewID vid) { + BrowserWindow* browser_window = browser->window(); + DCHECK(browser_window); +#if defined(TOOLKIT_VIEWS) + views::View* view = + reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid); +#else + gfx::NativeWindow window = browser_window->GetNativeHandle(); + DCHECK(window); + GtkWidget* view = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid); +#endif + + DCHECK(view); + ui_controls::MoveMouseToCenterAndPress( + view, + ui_controls::LEFT, + ui_controls::DOWN | ui_controls::UP, + new MessageLoop::QuitTask()); + RunMessageLoop(); +} + +} // namespace ui_test_utils diff --git a/chrome/test/ui_test_utils_mac.cc b/chrome/test/ui_test_utils_mac.cc new file mode 100644 index 0000000..1e40666 --- /dev/null +++ b/chrome/test/ui_test_utils_mac.cc @@ -0,0 +1,20 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/ui_test_utils.h" + +#include "base/logging.h" + +namespace ui_test_utils { + +bool IsViewFocused(const Browser* browser, ViewID vid) { + NOTIMPLEMENTED(); + return false; +} + +void ClickOnView(const Browser* browser, ViewID vid) { + NOTIMPLEMENTED(); +} + +} // namespace ui_test_utils diff --git a/chrome/test/ui_test_utils_win.cc b/chrome/test/ui_test_utils_win.cc new file mode 100644 index 0000000..22fa003 --- /dev/null +++ b/chrome/test/ui_test_utils_win.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/ui_test_utils.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_window.h" +#include "chrome/browser/automation/ui_controls.h" +#include "chrome/browser/views/frame/browser_view.h" +#include "views/focus/focus_manager.h" + +namespace ui_test_utils { + +bool IsViewFocused(const Browser* browser, ViewID vid) { + BrowserWindow* browser_window = browser->window(); + DCHECK(browser_window); + gfx::NativeWindow window = browser_window->GetNativeHandle(); + DCHECK(window); + views::FocusManager* focus_manager = + views::FocusManager::GetFocusManagerForNativeView(window); + DCHECK(focus_manager); + return focus_manager->GetFocusedView()->GetID() == vid; +} + +void ClickOnView(const Browser* browser, ViewID vid) { + BrowserWindow* browser_window = browser->window(); + DCHECK(browser_window); + views::View* view = + reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid); + DCHECK(view); + ui_controls::MoveMouseToCenterAndPress( + view, + ui_controls::LEFT, + ui_controls::DOWN | ui_controls::UP, + new MessageLoop::QuitTask()); + RunMessageLoop(); +} + +} // namespace ui_test_utils |