From fedd27e1faa65d38a1a5b11f750b5ae2fb5c4006 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Wed, 3 Apr 2013 11:07:18 +0200 Subject: [PATCH] Make keyClick/Press/Release work with a char Change-Id: Id821bddd993d4da4458464713a69edfea4aaecaa Reviewed-by: Oswald Buddenhagen Reviewed-by: Alan Alpert --- src/imports/testlib/TestCase.qml | 27 ++++++++++++++++++----- src/imports/testlib/testcase.qdoc | 4 ++- src/qmltest/quicktestevent.cpp | 30 ++++++++++++++++++++++++++ src/qmltest/quicktestevent_p.h | 4 +++ tests/auto/qmltest/textedit/tst_textedit.qml | 24 ++++++++++++++++++++ 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 3df85b4..f9a1a49 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -372,8 +372,13 @@ Item { modifiers = Qt.NoModifier if (delay == undefined) delay = -1 - if (!qtest_events.keyPress(key, modifiers, delay)) - qtest_fail("window not shown", 2) + if (typeof(key) == "string" && key.length == 1) { + if (!qtest_events.keyPressChar(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } else { + if (!qtest_events.keyPress(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } } function keyRelease(key, modifiers, delay) { @@ -381,8 +386,13 @@ Item { modifiers = Qt.NoModifier if (delay == undefined) delay = -1 - if (!qtest_events.keyRelease(key, modifiers, delay)) - qtest_fail("window not shown", 2) + if (typeof(key) == "string" && key.length == 1) { + if (!qtest_events.keyReleaseChar(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } else { + if (!qtest_events.keyRelease(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } } function keyClick(key, modifiers, delay) { @@ -390,8 +400,13 @@ Item { modifiers = Qt.NoModifier if (delay == undefined) delay = -1 - if (!qtest_events.keyClick(key, modifiers, delay)) - qtest_fail("window not shown", 2) + if (typeof(key) == "string" && key.length == 1) { + if (!qtest_events.keyClickChar(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } else { + if (!qtest_events.keyClick(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } } function mousePress(item, x, y, button, modifiers, delay) { diff --git a/src/imports/testlib/testcase.qdoc b/src/imports/testlib/testcase.qdoc index 64fa42d..56fcb24 100644 --- a/src/imports/testlib/testcase.qdoc +++ b/src/imports/testlib/testcase.qdoc @@ -174,7 +174,8 @@ The keyPress(), keyRelease(), and keyClick() methods can be used to simulate keyboard events within unit tests. The events are - delivered to the currently focused QML item. + delivered to the currently focused QML item. You can pass either + a Qt.Key enum value or a latin1 char (string of length one) \code Rectangle { @@ -187,6 +188,7 @@ function test_key_click() { keyClick(Qt.Key_Left) + keyClick("a") ... } } diff --git a/src/qmltest/quicktestevent.cpp b/src/qmltest/quicktestevent.cpp index 16ba800..1f72bc6 100644 --- a/src/qmltest/quicktestevent.cpp +++ b/src/qmltest/quicktestevent.cpp @@ -83,6 +83,36 @@ bool QuickTestEvent::keyClick(int key, int modifiers, int delay) return true; } +bool QuickTestEvent::keyPressChar(const QString &character, int modifiers, int delay) +{ + QTEST_ASSERT(character.length() == 1); + QWindow *window = eventWindow(); + if (!window) + return false; + QTest::keyPress(window, character[0].toLatin1(), Qt::KeyboardModifiers(modifiers), delay); + return true; +} + +bool QuickTestEvent::keyReleaseChar(const QString &character, int modifiers, int delay) +{ + QTEST_ASSERT(character.length() == 1); + QWindow *window = eventWindow(); + if (!window) + return false; + QTest::keyRelease(window, character[0].toLatin1(), Qt::KeyboardModifiers(modifiers), delay); + return true; +} + +bool QuickTestEvent::keyClickChar(const QString &character, int modifiers, int delay) +{ + QTEST_ASSERT(character.length() == 1); + QWindow *window = eventWindow(); + if (!window) + return false; + QTest::keyClick(window, character[0].toLatin1(), Qt::KeyboardModifiers(modifiers), delay); + return true; +} + namespace QTest { extern int Q_TESTLIB_EXPORT defaultMouseDelay(); }; diff --git a/src/qmltest/quicktestevent_p.h b/src/qmltest/quicktestevent_p.h index de33ff7..595ea9e 100644 --- a/src/qmltest/quicktestevent_p.h +++ b/src/qmltest/quicktestevent_p.h @@ -59,6 +59,10 @@ public Q_SLOTS: bool keyRelease(int key, int modifiers, int delay); bool keyClick(int key, int modifiers, int delay); + bool keyPressChar(const QString &character, int modifiers, int delay); + bool keyReleaseChar(const QString &character, int modifiers, int delay); + bool keyClickChar(const QString &character, int modifiers, int delay); + bool mousePress(QObject *item, qreal x, qreal y, int button, int modifiers, int delay); bool mouseRelease(QObject *item, qreal x, qreal y, int button, diff --git a/tests/auto/qmltest/textedit/tst_textedit.qml b/tests/auto/qmltest/textedit/tst_textedit.qml index dc9affd..edfa127 100644 --- a/tests/auto/qmltest/textedit/tst_textedit.qml +++ b/tests/auto/qmltest/textedit/tst_textedit.qml @@ -74,6 +74,13 @@ Item { } TextEdit { + id: txtentry2 + text: "" + height: 20 + width: 50 + } + + TextEdit { id: txtfunctions text: "The quick brown fox jumped over the lazy dog" height: 20 @@ -124,6 +131,23 @@ Item { compare(txtentry.text, "hello world") } + function test_textentry_char() { + txtentry2.focus = true; + compare(txtentry2.text, "") + keyClick("h") + keyClick("e") + keyClick("l") + keyClick("l") + keyClick("o") + keyClick(" ") + keyClick("W") + keyClick("o") + keyClick("r") + keyClick("l") + keyClick("d") + compare(txtentry2.text, "hello World") + } + function test_functions() { compare(txtfunctions.getText(4,9), "quick") txtfunctions.select(4,9); -- 1.7.2.5