diff options
-rw-r--r-- | src/imports/testlib/TestCase.qml | 27 | ||||
-rw-r--r-- | src/imports/testlib/testcase.qdoc | 4 | ||||
-rw-r--r-- | src/qmltest/quicktestevent.cpp | 30 | ||||
-rw-r--r-- | src/qmltest/quicktestevent_p.h | 4 | ||||
-rw-r--r-- | 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 3df85b415a..f9a1a4918c 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 64fa42d579..56fcb24beb 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 16ba80083f..1f72bc65c1 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 de33ff736b..595ea9e1f7 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 dc9affdad8..edfa127c59 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); |