diff options
author | Justin McPherson <justin.mcpherson@nokia.com> | 2010-02-14 23:37:12 (GMT) |
---|---|---|
committer | Justin McPherson <justin.mcpherson@nokia.com> | 2010-02-14 23:37:12 (GMT) |
commit | 0010cbdd14ce167857ae9eb8f539ad7b1205542a (patch) | |
tree | 92d8b31cece70ad76abc867e3a34567a1e2c116c /tests/auto/declarative/qmlgraphicstextedit | |
parent | 9bca64fc5eee598f97eba1052f1db250c5c7e43b (diff) | |
parent | 42dda19d82ceea48f19d356cfaf2b26acb763df8 (diff) | |
download | Qt-0010cbdd14ce167857ae9eb8f539ad7b1205542a.zip Qt-0010cbdd14ce167857ae9eb8f539ad7b1205542a.tar.gz Qt-0010cbdd14ce167857ae9eb8f539ad7b1205542a.tar.bz2 |
Merge branch 'master' of ../../qt/master
Diffstat (limited to 'tests/auto/declarative/qmlgraphicstextedit')
14 files changed, 964 insertions, 0 deletions
diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/cursorTest.qml b/tests/auto/declarative/qmlgraphicstextedit/data/cursorTest.qml new file mode 100644 index 0000000..e5df8f1 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/cursorTest.qml @@ -0,0 +1,8 @@ +import Qt 4.6 + +Rectangle { width: 300; height: 300; color: "white" + TextEdit { text: "Hello world!"; id: textEditObject; objectName: "textEditObject" + resources: [ Component { id:cursor; Item { id:cursorInstance; objectName: "cursorInstance" } } ] + cursorDelegate: cursor + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/ErrItem.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/ErrItem.qml new file mode 100644 index 0000000..34b3883 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/ErrItem.qml @@ -0,0 +1,7 @@ +import Qt 4.6 + +Item{ + Fungus{ + palatable: false; + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/NormItem.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/NormItem.qml new file mode 100644 index 0000000..718cb71 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/NormItem.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +Item { + objectName: "delegateOkay" + Rectangle { } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTest.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTest.qml new file mode 100644 index 0000000..3c31e11 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTest.qml @@ -0,0 +1,22 @@ +import Qt 4.6 + +Rectangle { width: 300; height: 300; color: "white" + resources: [ + Component { id:cursorFail; FailItem { objectName: "delegateFail" } }, + Component { id:cursorWait; WaitItem { objectName: "delegateSlow" } }, + Component { id:cursorNorm; NormItem { objectName: "delegateOkay" } }, + Component { id:cursorErr; ErrItem { objectName: "delegateErrorA" } } + ] + TextEdit { + cursorDelegate: cursorFail + } + TextEdit { + cursorDelegate: cursorWait + } + TextEdit { + cursorDelegate: cursorNorm + } + TextEdit { + cursorDelegate: cursorErr + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail1.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail1.qml new file mode 100644 index 0000000..a44aec2 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail1.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Rectangle { width: 300; height: 300; color: "white" + resources: [ + Component { id:cursorFail; FailItem { objectName: "delegateFail" } }, + Component { id:cursorWait; WaitItem { objectName: "delegateSlow" } }, + Component { id:cursorNorm; NormItem { objectName: "delegateOkay" } } + ] + TextEdit { + cursorDelegate: cursorFail + } + TextEdit { + cursorDelegate: cursorWait + } + TextEdit { + cursorDelegate: cursorNorm + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail2.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail2.qml new file mode 100644 index 0000000..57d3e47 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestFail2.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Rectangle { width: 300; height: 300; color: "white" + resources: [ + Component { id:cursorWait; WaitItem { objectName: "delegateSlow" } }, + Component { id:cursorNorm; NormItem { objectName: "delegateOkay" } }, + Component { id:cursorErr; ErrItem { objectName: "delegateErrorA" } } + ] + TextEdit { + cursorDelegate: cursorWait + } + TextEdit { + cursorDelegate: cursorNorm + } + TextEdit { + cursorDelegate: cursorErr + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestPass.qml b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestPass.qml new file mode 100644 index 0000000..a44e867 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/http/cursorHttpTestPass.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Rectangle { width: 300; height: 300; color: "white" + resources: [ + Component { id:cursorWait; WaitItem { objectName: "delegateSlow" } }, + Component { id:cursorNorm; NormItem { objectName: "delegateOkay" } } + ] + TextEdit { + cursorDelegate: cursorWait + text: "Hello" + } + TextEdit { + objectName: "textEditObject" + cursorDelegate: cursorNorm + focus: true; + text: "Hello" + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/httpfail/FailItem.qml b/tests/auto/declarative/qmlgraphicstextedit/data/httpfail/FailItem.qml new file mode 100644 index 0000000..ddbf526 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/httpfail/FailItem.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +Item { + Rectangle { } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/httpslow/WaitItem.qml b/tests/auto/declarative/qmlgraphicstextedit/data/httpslow/WaitItem.qml new file mode 100644 index 0000000..ddbf526 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/httpslow/WaitItem.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +Item { + Rectangle { } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/inputmethodhints.qml b/tests/auto/declarative/qmlgraphicstextedit/data/inputmethodhints.qml new file mode 100644 index 0000000..c3d4c16 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/inputmethodhints.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +TextEdit { + text: "Hello world!" + inputMethodHints: Qt.ImhNoPredictiveText +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/navigation.qml b/tests/auto/declarative/qmlgraphicstextedit/data/navigation.qml new file mode 100644 index 0000000..8d7dbbc --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/navigation.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Rectangle { + property var myInput: input + + width: 800; height: 600; color: "blue" + + Item { + id: firstItem; + KeyNavigation.right: input + } + + TextEdit { id: input; focus: true + KeyNavigation.left: firstItem + KeyNavigation.right: lastItem + KeyNavigation.up: firstItem + KeyNavigation.down: lastItem + } + Item { + id: lastItem + KeyNavigation.left: input + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/data/readOnly.qml b/tests/auto/declarative/qmlgraphicstextedit/data/readOnly.qml new file mode 100644 index 0000000..103a627 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/data/readOnly.qml @@ -0,0 +1,12 @@ +import Qt 4.6 + +Rectangle { + property var myInput: input + + width: 800; height: 600; color: "blue" + + TextEdit { id: input; focus: true + readOnly: true + text: "I am the very model of a modern major general.\n" + } +} diff --git a/tests/auto/declarative/qmlgraphicstextedit/qmlgraphicstextedit.pro b/tests/auto/declarative/qmlgraphicstextedit/qmlgraphicstextedit.pro new file mode 100644 index 0000000..a129120 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/qmlgraphicstextedit.pro @@ -0,0 +1,9 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative gui network +macx:CONFIG -= app_bundle + +SOURCES += tst_qmlgraphicstextedit.cpp ../shared/testhttpserver.cpp +HEADERS += ../shared/testhttpserver.h + +# Define SRCDIR equal to test's source directory +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qmlgraphicstextedit/tst_qmlgraphicstextedit.cpp b/tests/auto/declarative/qmlgraphicstextedit/tst_qmlgraphicstextedit.cpp new file mode 100644 index 0000000..a197ced --- /dev/null +++ b/tests/auto/declarative/qmlgraphicstextedit/tst_qmlgraphicstextedit.cpp @@ -0,0 +1,807 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <qtest.h> +#include "../../../shared/util.h" +#include "../shared/testhttpserver.h" +#include <math.h> +#include <QFile> +#include <QTextDocument> +#include <QtDeclarative/qmlengine.h> +#include <QtDeclarative/qmlcontext.h> +#include <QtDeclarative/qmlexpression.h> +#include <QtDeclarative/qmlcomponent.h> +#include <private/qmlgraphicstextedit_p.h> +#include <QFontMetrics> +#include <QmlView> +#include <QStyle> +#include <QInputContext> + +class tst_qmlgraphicstextedit : public QObject + +{ + Q_OBJECT +public: + tst_qmlgraphicstextedit(); + +private slots: + void text(); + void width(); + void wrap(); + void textFormat(); + + // ### these tests may be trivial + void hAlign(); + void vAlign(); + void font(); + void color(); + void textMargin(); + void persistentSelection(); + void focusOnPress(); + void selection(); + void inputMethodHints(); + + void cursorDelegate(); + void delegateLoading(); + void navigation(); + void readOnly(); + void sendRequestSoftwareInputPanelEvent(); + +private: + void simulateKey(QmlView *, int key); + QmlView *createView(const QString &filename); + + QStringList standard; + QStringList richText; + + QStringList hAlignmentStrings; + QStringList vAlignmentStrings; + + QList<Qt::Alignment> vAlignments; + QList<Qt::Alignment> hAlignments; + + QStringList colorStrings; + + QmlEngine engine; +}; + +tst_qmlgraphicstextedit::tst_qmlgraphicstextedit() +{ + standard << "the quick brown fox jumped over the lazy dog" + << "the quick brown fox\n jumped over the lazy dog"; + + richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>" + << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>"; + + hAlignmentStrings << "AlignLeft" + << "AlignRight" + << "AlignHCenter"; + + vAlignmentStrings << "AlignTop" + << "AlignBottom" + << "AlignVCenter"; + + hAlignments << Qt::AlignLeft + << Qt::AlignRight + << Qt::AlignHCenter; + + vAlignments << Qt::AlignTop + << Qt::AlignBottom + << Qt::AlignVCenter; + + colorStrings << "aliceblue" + << "antiquewhite" + << "aqua" + << "darkkhaki" + << "darkolivegreen" + << "dimgray" + << "palevioletred" + << "lightsteelblue" + << "#000000" + << "#AAAAAA" + << "#FFFFFF" + << "#2AC05F"; + // + // need a different test to do alpha channel test + // << "#AA0011DD" + // << "#00F16B11"; + // +} + +void tst_qmlgraphicstextedit::text() +{ + { + QmlComponent texteditComponent(&engine); + texteditComponent.setData("import Qt 4.6\nTextEdit { text: \"\" }", QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->text(), QString("")); + } + + for (int i = 0; i < standard.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { text: \"" + standard.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->text(), standard.at(i)); + } + + for (int i = 0; i < richText.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { text: \"" + richText.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QString actual = textEditObject->text(); + QString expected = richText.at(i); + actual.replace(QRegExp(".*<body[^>]*>"),""); + actual.replace(QRegExp("(<[^>]*>)+"),"<>"); + expected.replace(QRegExp("(<[^>]*>)+"),"<>"); + QCOMPARE(actual.simplified(),expected.simplified()); + } +} + +void tst_qmlgraphicstextedit::width() +{ + // uses Font metrics to find the width for standard and document to find the width for rich + { + QmlComponent texteditComponent(&engine); + texteditComponent.setData("import Qt 4.6\nTextEdit { text: \"\" }", QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), 1.);//+1 for cursor + } + + for (int i = 0; i < standard.size(); i++) + { + QFont f; + QFontMetricsF fm(f); + qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width(); + metricWidth = floor(metricWidth); + + QString componentStr = "import Qt 4.6\nTextEdit { text: \"" + standard.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), qreal(metricWidth + 1 + 3));//+3 is the current way of accounting for space between cursor and last character. + } + + for (int i = 0; i < richText.size(); i++) + { + QTextDocument document; + document.setHtml(richText.at(i)); + document.setDocumentMargin(0); + + int documentWidth = document.idealWidth(); + + QString componentStr = "import Qt 4.6\nTextEdit { text: \"" + richText.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), qreal(documentWidth + 1 + 3)); + } +} + +void tst_qmlgraphicstextedit::wrap() +{ + // for specified width and wrap set true + { + QmlComponent texteditComponent(&engine); + texteditComponent.setData("import Qt 4.6\nTextEdit { text: \"\"; wrap: true; width: 300 }", QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), 300.); + } + + for (int i = 0; i < standard.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { wrap: true; width: 300; text: \"" + standard.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), 300.); + } + + for (int i = 0; i < richText.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { wrap: true; width: 300; text: \"" + richText.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->width(), 300.); + } + +} + +void tst_qmlgraphicstextedit::textFormat() +{ + { + QmlComponent textComponent(&engine); + textComponent.setData("import Qt 4.6\nTextEdit { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile("")); + QmlGraphicsTextEdit *textObject = qobject_cast<QmlGraphicsTextEdit*>(textComponent.create()); + + QVERIFY(textObject != 0); + QVERIFY(textObject->textFormat() == QmlGraphicsTextEdit::RichText); + } + { + QmlComponent textComponent(&engine); + textComponent.setData("import Qt 4.6\nTextEdit { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile("")); + QmlGraphicsTextEdit *textObject = qobject_cast<QmlGraphicsTextEdit*>(textComponent.create()); + + QVERIFY(textObject != 0); + QVERIFY(textObject->textFormat() == QmlGraphicsTextEdit::PlainText); + } +} + +//the alignment tests may be trivial o.oa +void tst_qmlgraphicstextedit::hAlign() +{ + //test one align each, and then test if two align fails. + + for (int i = 0; i < standard.size(); i++) + { + for (int j=0; j < hAlignmentStrings.size(); j++) + { + QString componentStr = "import Qt 4.6\nTextEdit { horizontalAlignment: \"" + hAlignmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE((int)textEditObject->hAlign(), (int)hAlignments.at(j)); + } + } + + for (int i = 0; i < richText.size(); i++) + { + for (int j=0; j < hAlignmentStrings.size(); j++) + { + QString componentStr = "import Qt 4.6\nTextEdit { horizontalAlignment: \"" + hAlignmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE((int)textEditObject->hAlign(), (int)hAlignments.at(j)); + } + } + +} + +void tst_qmlgraphicstextedit::vAlign() +{ + //test one align each, and then test if two align fails. + + for (int i = 0; i < standard.size(); i++) + { + for (int j=0; j < vAlignmentStrings.size(); j++) + { + QString componentStr = "import Qt 4.6\nTextEdit { verticalAlignment: \"" + vAlignmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE((int)textEditObject->vAlign(), (int)vAlignments.at(j)); + } + } + + for (int i = 0; i < richText.size(); i++) + { + for (int j=0; j < vAlignmentStrings.size(); j++) + { + QString componentStr = "import Qt 4.6\nTextEdit { verticalAlignment: \"" + vAlignmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE((int)textEditObject->vAlign(), (int)vAlignments.at(j)); + } + } + +} + +void tst_qmlgraphicstextedit::font() +{ + //test size, then bold, then italic, then family + { + QString componentStr = "import Qt 4.6\nTextEdit { font.pointSize: 40; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->font().pointSize(), 40); + QCOMPARE(textEditObject->font().bold(), false); + QCOMPARE(textEditObject->font().italic(), false); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { font.bold: true; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->font().bold(), true); + QCOMPARE(textEditObject->font().italic(), false); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { font.italic: true; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->font().italic(), true); + QCOMPARE(textEditObject->font().bold(), false); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { font.family: \"Helvetica\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->font().family(), QString("Helvetica")); + QCOMPARE(textEditObject->font().bold(), false); + QCOMPARE(textEditObject->font().italic(), false); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { font.family: \"\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->font().family(), QString("")); + } +} + +void tst_qmlgraphicstextedit::color() +{ + //test normal + for (int i = 0; i < colorStrings.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + //qDebug() << "textEditObject: " << textEditObject->color() << "vs. " << QColor(colorStrings.at(i)); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->color(), QColor(colorStrings.at(i))); + } + + //test selection + for (int i = 0; i < colorStrings.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { selectionColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->selectionColor(), QColor(colorStrings.at(i))); + } + + //test selected text + for (int i = 0; i < colorStrings.size(); i++) + { + QString componentStr = "import Qt 4.6\nTextEdit { selectedTextColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->selectedTextColor(), QColor(colorStrings.at(i))); + } + + { + QString colorStr = "#AA001234"; + QColor testColor("#001234"); + testColor.setAlpha(170); + + QString componentStr = "import Qt 4.6\nTextEdit { color: \"" + colorStr + "\"; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->color(), testColor); + } +} + +void tst_qmlgraphicstextedit::textMargin() +{ + for(qreal i=0; i<=10; i+=0.3){ + QString componentStr = "import Qt 4.6\nTextEdit { textMargin: " + QString::number(i) + "; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->textMargin(), i); + } +} + +void tst_qmlgraphicstextedit::persistentSelection() +{ + { + QString componentStr = "import Qt 4.6\nTextEdit { persistentSelection: true; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->persistentSelection(), true); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { persistentSelection: false; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->persistentSelection(), false); + } +} + +void tst_qmlgraphicstextedit::focusOnPress() +{ + { + QString componentStr = "import Qt 4.6\nTextEdit { focusOnPress: true; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->focusOnPress(), true); + } + + { + QString componentStr = "import Qt 4.6\nTextEdit { focusOnPress: false; text: \"Hello World\" }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + QCOMPARE(textEditObject->focusOnPress(), false); + } +} + +void tst_qmlgraphicstextedit::selection() +{ + QString testStr = standard[0];//TODO: What should happen for multiline/rich text? + QString componentStr = "import Qt 4.6\nTextEdit { text: \""+ testStr +"\"; }"; + QmlComponent texteditComponent(&engine); + texteditComponent.setData(componentStr.toLatin1(), QUrl()); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit*>(texteditComponent.create()); + QVERIFY(textEditObject != 0); + + + //Test selection follows cursor + for(int i=0; i<= testStr.size(); i++) { + textEditObject->setCursorPosition(i); + QCOMPARE(textEditObject->cursorPosition(), i); + QCOMPARE(textEditObject->selectionStart(), i); + QCOMPARE(textEditObject->selectionEnd(), i); + QVERIFY(textEditObject->selectedText().isNull()); + } + + textEditObject->setCursorPosition(0); + QVERIFY(textEditObject->cursorPosition() == 0); + QVERIFY(textEditObject->selectionStart() == 0); + QVERIFY(textEditObject->selectionEnd() == 0); + QVERIFY(textEditObject->selectedText().isNull()); + + //Test selection + for(int i=0; i<= testStr.size(); i++) { + textEditObject->setSelectionEnd(i); + QCOMPARE(testStr.mid(0,i), textEditObject->selectedText()); + } + for(int i=0; i<= testStr.size(); i++) { + textEditObject->setSelectionStart(i); + QCOMPARE(testStr.mid(i,testStr.size()-i), textEditObject->selectedText()); + } + + textEditObject->setCursorPosition(0); + QVERIFY(textEditObject->cursorPosition() == 0); + QVERIFY(textEditObject->selectionStart() == 0); + QVERIFY(textEditObject->selectionEnd() == 0); + QVERIFY(textEditObject->selectedText().isNull()); + + for(int i=0; i< testStr.size(); i++) { + textEditObject->setSelectionStart(i); + QCOMPARE(textEditObject->selectionEnd(), i); + QCOMPARE(testStr.mid(i,0), textEditObject->selectedText()); + textEditObject->setSelectionEnd(i+1); + QCOMPARE(textEditObject->selectionStart(), i); + QCOMPARE(testStr.mid(i,1), textEditObject->selectedText()); + } + + for(int i= testStr.size() - 1; i>0; i--) { + textEditObject->setSelectionEnd(i); + QCOMPARE(testStr.mid(i,0), textEditObject->selectedText()); + textEditObject->setSelectionStart(i-1); + QCOMPARE(testStr.mid(i-1,1), textEditObject->selectedText()); + } + + //Test Error Ignoring behaviour + textEditObject->setCursorPosition(0); + QVERIFY(textEditObject->selectedText().isNull()); + textEditObject->setSelectionStart(-10); + QVERIFY(textEditObject->selectedText().isNull()); + textEditObject->setSelectionStart(100); + QVERIFY(textEditObject->selectedText().isNull()); + textEditObject->setSelectionEnd(-10); + QVERIFY(textEditObject->selectedText().isNull()); + textEditObject->setSelectionEnd(100); + QVERIFY(textEditObject->selectedText().isNull()); + textEditObject->setSelectionStart(0); + textEditObject->setSelectionEnd(10); + QVERIFY(textEditObject->selectedText().size() == 10); + textEditObject->setSelectionStart(-10); + QVERIFY(textEditObject->selectedText().size() == 10); + textEditObject->setSelectionStart(100); + QVERIFY(textEditObject->selectedText().size() == 10); + textEditObject->setSelectionEnd(-10); + QVERIFY(textEditObject->selectedText().size() == 10); + textEditObject->setSelectionEnd(100); + QVERIFY(textEditObject->selectedText().size() == 10); +} + +void tst_qmlgraphicstextedit::inputMethodHints() +{ + QmlView *canvas = createView(SRCDIR "/data/inputmethodhints.qml"); + canvas->execute(); + canvas->show(); + canvas->setFocus(); + + QVERIFY(canvas->root() != 0); + QmlGraphicsTextEdit *textEditObject = qobject_cast<QmlGraphicsTextEdit *>(canvas->root()); + QVERIFY(textEditObject != 0); + QVERIFY(textEditObject->inputMethodHints() & Qt::ImhNoPredictiveText); + textEditObject->setInputMethodHints(Qt::ImhUppercaseOnly); + QVERIFY(textEditObject->inputMethodHints() & Qt::ImhUppercaseOnly); +} + +void tst_qmlgraphicstextedit::cursorDelegate() +{ + QmlView* view = createView(SRCDIR "/data/cursorTest.qml"); + view->execute(); + view->show(); + view->setFocus(); + QmlGraphicsTextEdit *textEditObject = view->root()->findChild<QmlGraphicsTextEdit*>("textEditObject"); + QVERIFY(textEditObject != 0); + QVERIFY(textEditObject->findChild<QmlGraphicsItem*>("cursorInstance")); + //Test Delegate gets created + textEditObject->setFocus(true); + QmlGraphicsItem* delegateObject = textEditObject->findChild<QmlGraphicsItem*>("cursorInstance"); + QVERIFY(delegateObject); + //Test Delegate gets moved + for(int i=0; i<= textEditObject->text().length(); i++){ + textEditObject->setCursorPosition(i); + QCOMPARE(textEditObject->cursorRect().x(), qRound(delegateObject->x())); + QCOMPARE(textEditObject->cursorRect().y(), qRound(delegateObject->y())); + } + textEditObject->setCursorPosition(0); + QCOMPARE(textEditObject->cursorRect().x(), qRound(delegateObject->x())); + QCOMPARE(textEditObject->cursorRect().y(), qRound(delegateObject->y())); + //Test Delegate gets deleted + textEditObject->setCursorDelegate(0); + QVERIFY(!textEditObject->findChild<QmlGraphicsItem*>("cursorInstance")); +} + +void tst_qmlgraphicstextedit::delegateLoading() +{ + TestHTTPServer server(42332); + server.serveDirectory(SRCDIR "/data/httpfail", TestHTTPServer::Disconnect); + server.serveDirectory(SRCDIR "/data/httpslow", TestHTTPServer::Delay); + server.serveDirectory(SRCDIR "/data/http"); + QmlView* view = new QmlView(0); + view->setUrl(QUrl("http://localhost:42332/cursorHttpTestPass.qml")); + view->execute(); + view->show(); + view->setFocus(); + QTRY_VERIFY(view->root());//Wait for loading to finish. + QmlGraphicsTextEdit *textEditObject = view->root()->findChild<QmlGraphicsTextEdit*>("textEditObject"); + // view->root()->dumpObjectTree(); + QVERIFY(textEditObject != 0); + textEditObject->setFocus(true); + QmlGraphicsItem *delegate; + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateOkay"); + QVERIFY(delegate); + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateSlow"); + QVERIFY(delegate); + view->setUrl(QUrl("http://localhost:42332/cursorHttpTestFail1.qml")); + view->execute(); + view->show(); + view->setFocus(); + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateOkay"); + QVERIFY(delegate); + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateFail"); + QVERIFY(!delegate); + view->setUrl(QUrl("http://localhost:42332/cursorHttpTestFail2.qml")); + view->execute(); + view->show(); + view->setFocus(); + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateOkay"); + QVERIFY(delegate); + delegate = view->root()->findChild<QmlGraphicsItem*>("delegateErrorA"); + QVERIFY(!delegate); + //ErrorB should get a component which is ready but component.create() returns null + //Not sure how to accomplish this with QmlGraphicsTextEdits cursor delegate + //###This could be a case of overzealous defensive programming + //delegate = view->root()->findChild<QmlGraphicsItem*>("delegateErrorB"); + //QVERIFY(!delegate); +} + +/* +TextEdit element should only handle left/right keys until the cursor reaches +the extent of the text, then they should ignore the keys. +*/ +void tst_qmlgraphicstextedit::navigation() +{ + QmlView *canvas = createView(SRCDIR "/data/navigation.qml"); + canvas->execute(); + canvas->show(); + canvas->setFocus(); + + QVERIFY(canvas->root() != 0); + + QmlGraphicsItem *input = qobject_cast<QmlGraphicsItem *>(qvariant_cast<QObject *>(canvas->root()->property("myInput"))); + + QVERIFY(input != 0); + QTRY_VERIFY(input->hasFocus() == true); + simulateKey(canvas, Qt::Key_Left); + QVERIFY(input->hasFocus() == false); + simulateKey(canvas, Qt::Key_Right); + QVERIFY(input->hasFocus() == true); + simulateKey(canvas, Qt::Key_Right); + QVERIFY(input->hasFocus() == false); + simulateKey(canvas, Qt::Key_Left); + QVERIFY(input->hasFocus() == true); +} + +void tst_qmlgraphicstextedit::readOnly() +{ + QmlView *canvas = createView(SRCDIR "/data/readOnly.qml"); + canvas->execute(); + canvas->show(); + canvas->setFocus(); + + QVERIFY(canvas->root() != 0); + + QmlGraphicsTextEdit *edit = qobject_cast<QmlGraphicsTextEdit *>(qvariant_cast<QObject *>(canvas->root()->property("myInput"))); + + QVERIFY(edit != 0); + QTRY_VERIFY(edit->hasFocus() == true); + QVERIFY(edit->isReadOnly() == true); + QString initial = edit->text(); + for(int k=Qt::Key_0; k<=Qt::Key_Z; k++) + simulateKey(canvas, k); + simulateKey(canvas, Qt::Key_Return); + simulateKey(canvas, Qt::Key_Space); + simulateKey(canvas, Qt::Key_Escape); + QCOMPARE(edit->text(), initial); +} + +void tst_qmlgraphicstextedit::simulateKey(QmlView *view, int key) +{ + QKeyEvent press(QKeyEvent::KeyPress, key, 0); + QKeyEvent release(QKeyEvent::KeyRelease, key, 0); + + QApplication::sendEvent(view, &press); + QApplication::sendEvent(view, &release); +} + +QmlView *tst_qmlgraphicstextedit::createView(const QString &filename) +{ + QmlView *canvas = new QmlView(0); + + QFile file(filename); + file.open(QFile::ReadOnly); + QString xml = file.readAll(); + canvas->setQml(xml, filename); + return canvas; +} + +class MyInputContext : public QInputContext +{ +public: + MyInputContext() : softwareInputPanelEventReceived(false) {} + ~MyInputContext() {} + + QString identifierName() { return QString(); } + QString language() { return QString(); } + + void reset() {} + + bool isComposing() const { return false; } + + bool filterEvent( const QEvent *event ) + { + if (event->type() == QEvent::RequestSoftwareInputPanel) + softwareInputPanelEventReceived = true; + return QInputContext::filterEvent(event); + } + bool softwareInputPanelEventReceived; +}; + +void tst_qmlgraphicstextedit::sendRequestSoftwareInputPanelEvent() +{ + QGraphicsScene scene; + QGraphicsView view(&scene); + MyInputContext ic; + view.viewport()->setInputContext(&ic); + QStyle::RequestSoftwareInputPanel behavior = QStyle::RequestSoftwareInputPanel( + view.style()->styleHint(QStyle::SH_RequestSoftwareInputPanel)); + if ((behavior != QStyle::RSIP_OnMouseClick)) + QSKIP("This test need to have a style with RSIP_OnMouseClick", SkipSingle); + QmlGraphicsTextEdit edit; + edit.setText("Hello world"); + edit.setPos(0, 0); + scene.addItem(&edit); + view.show(); + qApp->setAutoSipEnabled(true); + QApplication::setActiveWindow(&view); + QTest::qWaitForWindowShown(&view); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view)); + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); + QApplication::processEvents(); + QCOMPARE(ic.softwareInputPanelEventReceived, true); +} +QTEST_MAIN(tst_qmlgraphicstextedit) + +#include "tst_qmlgraphicstextedit.moc" |