1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtTest/QSignalSpy>
43 #include "../../shared/testhttpserver.h"
46 #include <QTextDocument>
47 #include <QtQml/qqmlengine.h>
48 #include <QtQml/qqmlcontext.h>
49 #include <QtQml/qqmlexpression.h>
50 #include <QtQml/qqmlcomponent.h>
51 #include <QtGui/qguiapplication.h>
52 #include <private/qquicktextedit_p.h>
53 #include <private/qquicktextedit_p_p.h>
54 #include <private/qquicktext_p_p.h>
55 #include <QFontMetrics>
56 #include <QtQuick/QQuickView>
58 #include <QInputMethod>
61 #include <private/qquicktextcontrol_p.h>
62 #include "../../shared/util.h"
63 #include "../../shared/platformquirks.h"
64 #include "../../shared/platforminputcontext.h"
65 #include <private/qinputmethod_p.h>
66 #include <QtGui/qstylehints.h>
69 #include <Carbon/Carbon.h>
72 #define SERVER_PORT 42332
73 #define SERVER_ADDR "http://localhost:42332"
75 Q_DECLARE_METATYPE(QQuickTextEdit::SelectionMode)
76 Q_DECLARE_METATYPE(Qt::Key)
77 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
79 QString createExpectedFileIfNotFound(const QString& filebasename, const QImage& actual)
81 // XXX This will be replaced by some clever persistent platform image store.
82 QString persistent_dir = QQmlDataTest::instance()->dataDirectory();
83 QString arch = "unknown-architecture"; // QTest needs to help with this.
85 QString expectfile = persistent_dir + QDir::separator() + filebasename + "-" + arch + ".png";
87 if (!QFile::exists(expectfile)) {
88 actual.save(expectfile);
89 qWarning() << "created" << expectfile;
95 typedef QPair<int, QChar> Key;
97 class tst_qquicktextedit : public QQmlDataTest
102 tst_qquicktextedit();
111 // ### these tests may be trivial
113 void hAlign_RightToLeft();
119 void persistentSelection();
122 void isRightToLeft_data();
123 void isRightToLeft();
125 void moveCursorSelection_data();
126 void moveCursorSelection();
127 void moveCursorSelectionSequence_data();
128 void moveCursorSelectionSequence();
129 void mouseSelection_data();
130 void mouseSelection();
131 void mouseSelectionMode_data();
132 void mouseSelectionMode();
133 void dragMouseSelection();
134 void mouseSelectionMode_accessors();
135 void selectByMouse();
137 void inputMethodHints();
139 void positionAt_data();
142 void linkActivated();
144 void cursorDelegate_data();
145 void cursorDelegate();
146 void remoteCursorDelegate();
147 void cursorVisible();
148 void delegateLoading_data();
149 void delegateLoading();
152 #ifndef QT_NO_CLIPBOARD
155 void canPasteEmpty();
156 void middleClickPaste();
159 void inputMethodUpdate();
160 void openInputPanel();
161 void geometrySignals();
162 #ifndef QT_NO_CLIPBOARD
163 void pastingRichText_QTBUG_14003();
165 void implicitSize_data();
170 void implicitSizeBinding_data();
171 void implicitSizeBinding();
173 void preeditCursorRectangle();
174 void inputMethodComposing();
175 void cursorRectangleSize();
179 void getFormattedText_data();
180 void getFormattedText();
186 void keySequence_data();
193 void undo_keypressevents_data();
194 void undo_keypressevents();
197 void embeddedImages();
198 void embeddedImages_data();
200 void emptytags_QTBUG_22058();
203 void simulateKeys(QWindow *window, const QList<Key> &keys);
204 void simulateKeys(QWindow *window, const QKeySequence &sequence);
206 void simulateKey(QWindow *, int key, Qt::KeyboardModifiers modifiers = 0);
208 QStringList standard;
209 QStringList richText;
211 QStringList hAlignmentStrings;
212 QStringList vAlignmentStrings;
214 QList<Qt::Alignment> vAlignments;
215 QList<Qt::Alignment> hAlignments;
217 QStringList colorStrings;
222 typedef QList<int> IntList;
223 Q_DECLARE_METATYPE(IntList)
225 typedef QList<Key> KeyList;
226 Q_DECLARE_METATYPE(KeyList)
228 Q_DECLARE_METATYPE(QQuickTextEdit::HAlignment)
229 Q_DECLARE_METATYPE(QQuickTextEdit::VAlignment)
230 Q_DECLARE_METATYPE(QQuickTextEdit::TextFormat)
232 void tst_qquicktextedit::simulateKeys(QWindow *window, const QList<Key> &keys)
234 for (int i = 0; i < keys.count(); ++i) {
235 const int key = keys.at(i).first;
236 const int modifiers = key & Qt::KeyboardModifierMask;
237 const QString text = !keys.at(i).second.isNull() ? QString(keys.at(i).second) : QString();
239 QKeyEvent press(QEvent::KeyPress, Qt::Key(key), Qt::KeyboardModifiers(modifiers), text);
240 QKeyEvent release(QEvent::KeyRelease, Qt::Key(key), Qt::KeyboardModifiers(modifiers), text);
242 QGuiApplication::sendEvent(window, &press);
243 QGuiApplication::sendEvent(window, &release);
247 void tst_qquicktextedit::simulateKeys(QWindow *window, const QKeySequence &sequence)
249 for (int i = 0; i < sequence.count(); ++i) {
250 const int key = sequence[i];
251 const int modifiers = key & Qt::KeyboardModifierMask;
253 QTest::keyClick(window, Qt::Key(key & ~modifiers), Qt::KeyboardModifiers(modifiers));
257 QList<Key> &operator <<(QList<Key> &keys, const QKeySequence &sequence)
259 for (int i = 0; i < sequence.count(); ++i)
260 keys << Key(sequence[i], QChar());
264 template <int N> QList<Key> &operator <<(QList<Key> &keys, const char (&characters)[N])
266 for (int i = 0; i < N - 1; ++i) {
267 int key = QTest::asciiToKey(characters[i]);
268 QChar character = QLatin1Char(characters[i]);
269 keys << Key(key, character);
274 QList<Key> &operator <<(QList<Key> &keys, Qt::Key key)
276 keys << Key(key, QChar());
280 tst_qquicktextedit::tst_qquicktextedit()
282 standard << "the quick brown fox jumped over the lazy dog"
283 << "the quick brown fox\n jumped over the lazy dog"
287 richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
288 << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
290 hAlignmentStrings << "AlignLeft"
294 vAlignmentStrings << "AlignTop"
298 hAlignments << Qt::AlignLeft
302 vAlignments << Qt::AlignTop
306 colorStrings << "aliceblue"
319 // need a different test to do alpha channel test
325 void tst_qquicktextedit::cleanup()
327 // ensure not even skipped tests with custom input context leave it dangling
328 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
329 inputMethodPrivate->testContext = 0;
332 void tst_qquicktextedit::text()
335 QQmlComponent texteditComponent(&engine);
336 texteditComponent.setData("import QtQuick 2.0\nTextEdit { text: \"\" }", QUrl());
337 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
339 QVERIFY(textEditObject != 0);
340 QCOMPARE(textEditObject->text(), QString(""));
341 QCOMPARE(textEditObject->length(), 0);
344 for (int i = 0; i < standard.size(); i++)
346 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + standard.at(i) + "\" }";
347 QQmlComponent texteditComponent(&engine);
348 texteditComponent.setData(componentStr.toLatin1(), QUrl());
349 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
351 QVERIFY(textEditObject != 0);
352 QCOMPARE(textEditObject->text(), standard.at(i));
353 QCOMPARE(textEditObject->length(), standard.at(i).length());
356 for (int i = 0; i < richText.size(); i++)
358 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + richText.at(i) + "\" }";
359 QQmlComponent texteditComponent(&engine);
360 texteditComponent.setData(componentStr.toLatin1(), QUrl());
362 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
364 QVERIFY(textEditObject != 0);
366 QString expected = richText.at(i);
367 expected.replace(QRegExp("\\\\(.)"),"\\1");
368 QCOMPARE(textEditObject->text(), expected);
369 QCOMPARE(textEditObject->length(), expected.length());
372 for (int i = 0; i < standard.size(); i++)
374 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + standard.at(i) + "\" }";
375 QQmlComponent texteditComponent(&engine);
376 texteditComponent.setData(componentStr.toLatin1(), QUrl());
377 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
379 QVERIFY(textEditObject != 0);
381 QString actual = textEditObject->text();
382 QString expected = standard.at(i);
383 actual.remove(QRegExp(".*<body[^>]*>"));
384 actual.remove(QRegExp("(<[^>]*>)+"));
385 expected.remove("\n");
386 QCOMPARE(actual.simplified(), expected);
387 QCOMPARE(textEditObject->length(), expected.length());
390 for (int i = 0; i < richText.size(); i++)
392 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
393 QQmlComponent texteditComponent(&engine);
394 texteditComponent.setData(componentStr.toLatin1(), QUrl());
395 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
397 QVERIFY(textEditObject != 0);
398 QString actual = textEditObject->text();
399 QString expected = richText.at(i);
400 actual.replace(QRegExp(".*<body[^>]*>"),"");
401 actual.replace(QRegExp("(<[^>]*>)+"),"<>");
402 expected.replace(QRegExp("(<[^>]*>)+"),"<>");
403 QCOMPARE(actual.simplified(),expected.simplified());
405 expected.replace("<>", " ");
406 QCOMPARE(textEditObject->length(), expected.simplified().length());
409 for (int i = 0; i < standard.size(); i++)
411 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + standard.at(i) + "\" }";
412 QQmlComponent texteditComponent(&engine);
413 texteditComponent.setData(componentStr.toLatin1(), QUrl());
414 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
416 QVERIFY(textEditObject != 0);
417 QCOMPARE(textEditObject->text(), standard.at(i));
418 QCOMPARE(textEditObject->length(), standard.at(i).length());
421 for (int i = 0; i < richText.size(); i++)
423 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + richText.at(i) + "\" }";
424 QQmlComponent texteditComponent(&engine);
425 texteditComponent.setData(componentStr.toLatin1(), QUrl());
426 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
428 QVERIFY(textEditObject != 0);
429 QString actual = textEditObject->text();
430 QString expected = richText.at(i);
431 actual.replace(QRegExp(".*<body[^>]*>"),"");
432 actual.replace(QRegExp("(<[^>]*>)+"),"<>");
433 expected.replace(QRegExp("(<[^>]*>)+"),"<>");
434 QCOMPARE(actual.simplified(),expected.simplified());
436 expected.replace("<>", " ");
437 QCOMPARE(textEditObject->length(), expected.simplified().length());
441 void tst_qquicktextedit::width()
443 // uses Font metrics to find the width for standard and document to find the width for rich
445 QQmlComponent texteditComponent(&engine);
446 texteditComponent.setData("import QtQuick 2.0\nTextEdit { text: \"\" }", QUrl());
447 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
449 QVERIFY(textEditObject != 0);
450 QCOMPARE(textEditObject->width(), 0.0);
453 bool requiresUnhintedMetrics = !qmlDisableDistanceField();
455 for (int i = 0; i < standard.size(); i++)
457 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + standard.at(i) + "\" }";
458 QQmlComponent texteditComponent(&engine);
459 texteditComponent.setData(componentStr.toLatin1(), QUrl());
460 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
462 QString s = standard.at(i);
463 s.replace(QLatin1Char('\n'), QChar::LineSeparator);
465 QTextLayout layout(s);
466 layout.setFont(textEditObject->font());
467 layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic);
468 if (requiresUnhintedMetrics) {
470 option.setUseDesignMetrics(true);
471 layout.setTextOption(option);
474 layout.beginLayout();
476 QTextLine line = layout.createLine();
483 qreal metricWidth = layout.boundingRect().width();
485 QVERIFY(textEditObject != 0);
486 QCOMPARE(textEditObject->width(), metricWidth);
489 for (int i = 0; i < richText.size(); i++)
491 QTextDocument document;
492 document.setHtml(richText.at(i));
493 document.setDocumentMargin(0);
494 if (requiresUnhintedMetrics)
495 document.setUseDesignMetrics(true);
497 qreal documentWidth = document.idealWidth();
499 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
500 QQmlComponent texteditComponent(&engine);
501 texteditComponent.setData(componentStr.toLatin1(), QUrl());
502 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
504 QVERIFY(textEditObject != 0);
505 QCOMPARE(textEditObject->width(), documentWidth);
509 void tst_qquicktextedit::wrap()
511 // for specified width and wrap set true
513 QQmlComponent texteditComponent(&engine);
514 texteditComponent.setData("import QtQuick 2.0\nTextEdit { text: \"\"; wrapMode: TextEdit.WordWrap; width: 300 }", QUrl());
515 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
517 QVERIFY(textEditObject != 0);
518 QCOMPARE(textEditObject->width(), 300.);
521 for (int i = 0; i < standard.size(); i++)
523 QString componentStr = "import QtQuick 2.0\nTextEdit { wrapMode: TextEdit.WordWrap; width: 300; text: \"" + standard.at(i) + "\" }";
524 QQmlComponent texteditComponent(&engine);
525 texteditComponent.setData(componentStr.toLatin1(), QUrl());
526 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
528 QVERIFY(textEditObject != 0);
529 QCOMPARE(textEditObject->width(), 300.);
532 for (int i = 0; i < richText.size(); i++)
534 QString componentStr = "import QtQuick 2.0\nTextEdit { wrapMode: TextEdit.WordWrap; width: 300; text: \"" + richText.at(i) + "\" }";
535 QQmlComponent texteditComponent(&engine);
536 texteditComponent.setData(componentStr.toLatin1(), QUrl());
537 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
539 QVERIFY(textEditObject != 0);
540 QCOMPARE(textEditObject->width(), 300.);
543 QQmlComponent component(&engine);
544 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
545 QScopedPointer<QObject> object(component.create());
546 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
549 QSignalSpy spy(edit, SIGNAL(wrapModeChanged()));
551 QCOMPARE(edit->wrapMode(), QQuickTextEdit::NoWrap);
553 edit->setWrapMode(QQuickTextEdit::Wrap);
554 QCOMPARE(edit->wrapMode(), QQuickTextEdit::Wrap);
555 QCOMPARE(spy.count(), 1);
557 edit->setWrapMode(QQuickTextEdit::Wrap);
558 QCOMPARE(spy.count(), 1);
560 edit->setWrapMode(QQuickTextEdit::NoWrap);
561 QCOMPARE(edit->wrapMode(), QQuickTextEdit::NoWrap);
562 QCOMPARE(spy.count(), 2);
567 void tst_qquicktextedit::textFormat()
570 QQmlComponent textComponent(&engine);
571 textComponent.setData("import QtQuick 2.0\nTextEdit { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile(""));
572 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit*>(textComponent.create());
574 QVERIFY(textObject != 0);
575 QVERIFY(textObject->textFormat() == QQuickTextEdit::RichText);
578 QQmlComponent textComponent(&engine);
579 textComponent.setData("import QtQuick 2.0\nTextEdit { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile(""));
580 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit*>(textComponent.create());
582 QVERIFY(textObject != 0);
583 QVERIFY(textObject->textFormat() == QQuickTextEdit::PlainText);
586 QQmlComponent component(&engine);
587 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
588 QScopedPointer<QObject> object(component.create());
589 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
592 QSignalSpy spy(edit, SIGNAL(textFormatChanged(TextFormat)));
594 QCOMPARE(edit->textFormat(), QQuickTextEdit::PlainText);
596 edit->setTextFormat(QQuickTextEdit::RichText);
597 QCOMPARE(edit->textFormat(), QQuickTextEdit::RichText);
598 QCOMPARE(spy.count(), 1);
600 edit->setTextFormat(QQuickTextEdit::RichText);
601 QCOMPARE(spy.count(), 1);
603 edit->setTextFormat(QQuickTextEdit::PlainText);
604 QCOMPARE(edit->textFormat(), QQuickTextEdit::PlainText);
605 QCOMPARE(spy.count(), 2);
609 //the alignment tests may be trivial o.oa
610 void tst_qquicktextedit::hAlign()
612 //test one align each, and then test if two align fails.
614 for (int i = 0; i < standard.size(); i++)
616 for (int j=0; j < hAlignmentStrings.size(); j++)
618 QString componentStr = "import QtQuick 2.0\nTextEdit { horizontalAlignment: \"" + hAlignmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
619 QQmlComponent texteditComponent(&engine);
620 texteditComponent.setData(componentStr.toLatin1(), QUrl());
621 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
623 QVERIFY(textEditObject != 0);
624 QCOMPARE((int)textEditObject->hAlign(), (int)hAlignments.at(j));
628 for (int i = 0; i < richText.size(); i++)
630 for (int j=0; j < hAlignmentStrings.size(); j++)
632 QString componentStr = "import QtQuick 2.0\nTextEdit { horizontalAlignment: \"" + hAlignmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
633 QQmlComponent texteditComponent(&engine);
634 texteditComponent.setData(componentStr.toLatin1(), QUrl());
635 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
637 QVERIFY(textEditObject != 0);
638 QCOMPARE((int)textEditObject->hAlign(), (int)hAlignments.at(j));
644 void tst_qquicktextedit::hAlign_RightToLeft()
646 PlatformInputContext platformInputContext;
647 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
648 inputMethodPrivate->testContext = &platformInputContext;
650 QQuickView window(testFileUrl("horizontalAlignment_RightToLeft.qml"));
651 QQuickTextEdit *textEdit = window.rootObject()->findChild<QQuickTextEdit*>("text");
652 QVERIFY(textEdit != 0);
655 const QString rtlText = textEdit->text();
657 // implicit alignment should follow the reading direction of text
658 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
659 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
661 // explicitly left aligned
662 textEdit->setHAlign(QQuickTextEdit::AlignLeft);
663 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
664 QVERIFY(textEdit->positionToRectangle(0).x() < window.width()/2);
666 // explicitly right aligned
667 textEdit->setHAlign(QQuickTextEdit::AlignRight);
668 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
669 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
671 QString textString = textEdit->text();
672 textEdit->setText(QString("<i>") + textString + QString("</i>"));
673 textEdit->resetHAlign();
675 // implicitly aligned rich text should follow the reading direction of RTL text
676 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
677 QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign());
678 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
680 // explicitly left aligned rich text
681 textEdit->setHAlign(QQuickTextEdit::AlignLeft);
682 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
683 QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign());
684 QVERIFY(textEdit->positionToRectangle(0).x() < window.width()/2);
686 // explicitly right aligned rich text
687 textEdit->setHAlign(QQuickTextEdit::AlignRight);
688 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
689 QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign());
690 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
692 textEdit->setText(textString);
694 // explicitly center aligned
695 textEdit->setHAlign(QQuickTextEdit::AlignHCenter);
696 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignHCenter);
697 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
699 // reseted alignment should go back to following the text reading direction
700 textEdit->resetHAlign();
701 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
702 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
704 // mirror the text item
705 QQuickItemPrivate::get(textEdit)->setLayoutMirror(true);
707 // mirrored implicit alignment should continue to follow the reading direction of the text
708 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
709 QCOMPARE(textEdit->effectiveHAlign(), QQuickTextEdit::AlignRight);
710 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
712 // mirrored explicitly right aligned behaves as left aligned
713 textEdit->setHAlign(QQuickTextEdit::AlignRight);
714 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
715 QCOMPARE(textEdit->effectiveHAlign(), QQuickTextEdit::AlignLeft);
716 QVERIFY(textEdit->positionToRectangle(0).x() < window.width()/2);
718 // mirrored explicitly left aligned behaves as right aligned
719 textEdit->setHAlign(QQuickTextEdit::AlignLeft);
720 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
721 QCOMPARE(textEdit->effectiveHAlign(), QQuickTextEdit::AlignRight);
722 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
725 QQuickItemPrivate::get(textEdit)->setLayoutMirror(false);
726 textEdit->resetHAlign();
728 // English text should be implicitly left aligned
729 textEdit->setText("Hello world!");
730 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
731 QVERIFY(textEdit->positionToRectangle(0).x() < window.width()/2);
733 window.requestActivateWindow();
734 QTest::qWaitForWindowActive(&window);
735 QVERIFY(textEdit->hasActiveFocus());
737 textEdit->setText(QString());
738 { QInputMethodEvent ev(rtlText, QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(textEdit, &ev); }
739 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
740 { QInputMethodEvent ev("Hello world!", QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(textEdit, &ev); }
741 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
743 // Clear pre-edit text. TextEdit should maybe do this itself on setText, but that may be
744 // redundant as an actual input method may take care of it.
745 { QInputMethodEvent ev; QGuiApplication::sendEvent(textEdit, &ev); }
747 // empty text with implicit alignment follows the system locale-based
748 // keyboard input direction from qApp->inputMethod()->inputDirection
749 textEdit->setText("");
750 platformInputContext.setInputDirection(Qt::LeftToRight);
751 QVERIFY(qApp->inputMethod()->inputDirection() == Qt::LeftToRight);
752 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);
753 QVERIFY(textEdit->positionToRectangle(0).x() < window.width()/2);
755 QSignalSpy cursorRectangleSpy(textEdit, SIGNAL(cursorRectangleChanged()));
757 platformInputContext.setInputDirection(Qt::RightToLeft);
758 QCOMPARE(cursorRectangleSpy.count(), 1);
759 QVERIFY(qApp->inputMethod()->inputDirection() == Qt::RightToLeft);
760 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
761 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
763 // neutral text follows also input method direction
764 textEdit->resetHAlign();
765 textEdit->setText(" ()((=<>");
766 platformInputContext.setInputDirection(Qt::LeftToRight);
767 QCOMPARE(textEdit->effectiveHAlign(), QQuickTextEdit::AlignLeft);
768 QVERIFY(textEdit->cursorRectangle().left() < window.width()/2);
769 platformInputContext.setInputDirection(Qt::RightToLeft);
770 QCOMPARE(textEdit->effectiveHAlign(), QQuickTextEdit::AlignRight);
771 QVERIFY(textEdit->cursorRectangle().left() > window.width()/2);
773 // set input direction while having content
774 platformInputContext.setInputDirection(Qt::LeftToRight);
775 textEdit->setText("a");
776 textEdit->setCursorPosition(1);
777 platformInputContext.setInputDirection(Qt::RightToLeft);
778 QTest::keyClick(&window, Qt::Key_Backspace);
779 QVERIFY(textEdit->text().isEmpty());
780 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
781 QVERIFY(textEdit->cursorRectangle().left() > window.width()/2);
783 // input direction changed while not having focus
784 platformInputContext.setInputDirection(Qt::LeftToRight);
785 textEdit->setFocus(false);
786 platformInputContext.setInputDirection(Qt::RightToLeft);
787 textEdit->setFocus(true);
788 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
789 QVERIFY(textEdit->cursorRectangle().left() > window.width()/2);
791 textEdit->setHAlign(QQuickTextEdit::AlignRight);
792 QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
793 QVERIFY(textEdit->positionToRectangle(0).x() > window.width()/2);
795 // make sure editor doesn't rely on input for updating size
796 QQuickTextEdit *emptyEdit = window.rootObject()->findChild<QQuickTextEdit*>("emptyTextEdit");
797 QVERIFY(emptyEdit != 0);
798 platformInputContext.setInputDirection(Qt::RightToLeft);
799 emptyEdit->setFocus(true);
800 QCOMPARE(emptyEdit->hAlign(), QQuickTextEdit::AlignRight);
801 QVERIFY(emptyEdit->cursorRectangle().left() > window.width()/2);
805 static int numberOfNonWhitePixels(int fromX, int toX, const QImage &image)
808 for (int x = fromX; x < toX; ++x) {
809 for (int y = 0; y < image.height(); ++y) {
810 if (image.pixel(x, y) != qRgb(255, 255, 255))
817 void tst_qquicktextedit::hAlignVisual()
819 QQuickView view(testFileUrl("hAlignVisual.qml"));
821 QVERIFY(QTest::qWaitForWindowExposed(&view));
823 QQuickText *text = view.rootObject()->findChild<QQuickText*>("textItem");
827 QImage image = view.grabWindow();
828 int left = numberOfNonWhitePixels(0, image.width() / 3, image);
829 int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
830 int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
832 QVERIFY(mid > right);
836 text->setHAlign(QQuickText::AlignHCenter);
837 QImage image = view.grabWindow();
838 int left = numberOfNonWhitePixels(0, image.width() / 3, image);
839 int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
840 int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
842 QVERIFY(mid > right);
846 text->setHAlign(QQuickText::AlignRight);
847 QImage image = view.grabWindow();
848 int left = numberOfNonWhitePixels(0, image.width() / 3, image);
849 int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
850 int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
852 QVERIFY(mid < right);
859 QImage image = view.grabWindow();
860 int x = qCeil(text->implicitWidth());
861 int left = numberOfNonWhitePixels(0, x, image);
862 int right = numberOfNonWhitePixels(x, image.width() - x, image);
868 text->setHAlign(QQuickText::AlignHCenter);
869 QImage image = view.grabWindow();
870 int x1 = qFloor(image.width() - text->implicitWidth()) / 2;
871 int x2 = image.width() - x1;
872 int left = numberOfNonWhitePixels(0, x1, image);
873 int mid = numberOfNonWhitePixels(x1, x2 - x1, image);
874 int right = numberOfNonWhitePixels(x2, image.width() - x2, image);
881 text->setHAlign(QQuickText::AlignRight);
882 QImage image = view.grabWindow();
883 int x = image.width() - qCeil(text->implicitWidth());
884 int left = numberOfNonWhitePixels(0, x, image);
885 int right = numberOfNonWhitePixels(x, image.width() - x, image);
891 void tst_qquicktextedit::vAlign()
893 //test one align each, and then test if two align fails.
895 for (int i = 0; i < standard.size(); i++)
897 for (int j=0; j < vAlignmentStrings.size(); j++)
899 QString componentStr = "import QtQuick 2.0\nTextEdit { verticalAlignment: \"" + vAlignmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
900 QQmlComponent texteditComponent(&engine);
901 texteditComponent.setData(componentStr.toLatin1(), QUrl());
902 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
904 QVERIFY(textEditObject != 0);
905 QCOMPARE((int)textEditObject->vAlign(), (int)vAlignments.at(j));
909 for (int i = 0; i < richText.size(); i++)
911 for (int j=0; j < vAlignmentStrings.size(); j++)
913 QString componentStr = "import QtQuick 2.0\nTextEdit { verticalAlignment: \"" + vAlignmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
914 QQmlComponent texteditComponent(&engine);
915 texteditComponent.setData(componentStr.toLatin1(), QUrl());
916 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
918 QVERIFY(textEditObject != 0);
919 QCOMPARE((int)textEditObject->vAlign(), (int)vAlignments.at(j));
923 QQmlComponent texteditComponent(&engine);
924 texteditComponent.setData(
925 "import QtQuick 2.0\n"
926 "TextEdit { width: 100; height: 100; text: \"Hello World\" }", QUrl());
927 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
929 QVERIFY(textEditObject != 0);
931 QCOMPARE(textEditObject->vAlign(), QQuickTextEdit::AlignTop);
932 QVERIFY(textEditObject->cursorRectangle().bottom() < 50);
933 QVERIFY(textEditObject->positionToRectangle(0).bottom() < 50);
936 textEditObject->setVAlign(QQuickTextEdit::AlignBottom);
937 QCOMPARE(textEditObject->vAlign(), QQuickTextEdit::AlignBottom);
938 QVERIFY(textEditObject->cursorRectangle().top() > 50);
939 QVERIFY(textEditObject->positionToRectangle(0).top() > 50);
941 // explicitly center aligned
942 textEditObject->setVAlign(QQuickTextEdit::AlignVCenter);
943 QCOMPARE(textEditObject->vAlign(), QQuickTextEdit::AlignVCenter);
944 QVERIFY(textEditObject->cursorRectangle().top() < 50);
945 QVERIFY(textEditObject->cursorRectangle().bottom() > 50);
946 QVERIFY(textEditObject->positionToRectangle(0).top() < 50);
947 QVERIFY(textEditObject->positionToRectangle(0).bottom() > 50);
950 void tst_qquicktextedit::font()
952 //test size, then bold, then italic, then family
954 QString componentStr = "import QtQuick 2.0\nTextEdit { font.pointSize: 40; text: \"Hello World\" }";
955 QQmlComponent texteditComponent(&engine);
956 texteditComponent.setData(componentStr.toLatin1(), QUrl());
957 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
959 QVERIFY(textEditObject != 0);
960 QCOMPARE(textEditObject->font().pointSize(), 40);
961 QCOMPARE(textEditObject->font().bold(), false);
962 QCOMPARE(textEditObject->font().italic(), false);
966 QString componentStr = "import QtQuick 2.0\nTextEdit { font.bold: true; text: \"Hello World\" }";
967 QQmlComponent texteditComponent(&engine);
968 texteditComponent.setData(componentStr.toLatin1(), QUrl());
969 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
971 QVERIFY(textEditObject != 0);
972 QCOMPARE(textEditObject->font().bold(), true);
973 QCOMPARE(textEditObject->font().italic(), false);
977 QString componentStr = "import QtQuick 2.0\nTextEdit { font.italic: true; text: \"Hello World\" }";
978 QQmlComponent texteditComponent(&engine);
979 texteditComponent.setData(componentStr.toLatin1(), QUrl());
980 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
982 QVERIFY(textEditObject != 0);
983 QCOMPARE(textEditObject->font().italic(), true);
984 QCOMPARE(textEditObject->font().bold(), false);
988 QString componentStr = "import QtQuick 2.0\nTextEdit { font.family: \"Helvetica\"; text: \"Hello World\" }";
989 QQmlComponent texteditComponent(&engine);
990 texteditComponent.setData(componentStr.toLatin1(), QUrl());
991 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
993 QVERIFY(textEditObject != 0);
994 QCOMPARE(textEditObject->font().family(), QString("Helvetica"));
995 QCOMPARE(textEditObject->font().bold(), false);
996 QCOMPARE(textEditObject->font().italic(), false);
1000 QString componentStr = "import QtQuick 2.0\nTextEdit { font.family: \"\"; text: \"Hello World\" }";
1001 QQmlComponent texteditComponent(&engine);
1002 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1003 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1005 QVERIFY(textEditObject != 0);
1006 QCOMPARE(textEditObject->font().family(), QString(""));
1010 void tst_qquicktextedit::color()
1012 //test initial color
1014 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"Hello World\" }";
1015 QQmlComponent texteditComponent(&engine);
1016 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1017 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1019 QVERIFY(textEditObject);
1020 QCOMPARE(textEditObject->color(), QColor("black"));
1021 QCOMPARE(textEditObject->selectionColor(), QColor::fromRgba(0xFF000080));
1022 QCOMPARE(textEditObject->selectedTextColor(), QColor("white"));
1024 QSignalSpy colorSpy(textEditObject, SIGNAL(colorChanged(QColor)));
1025 QSignalSpy selectionColorSpy(textEditObject, SIGNAL(selectionColorChanged(QColor)));
1026 QSignalSpy selectedTextColorSpy(textEditObject, SIGNAL(selectedTextColorChanged(QColor)));
1028 textEditObject->setColor(QColor("white"));
1029 QCOMPARE(textEditObject->color(), QColor("white"));
1030 QCOMPARE(colorSpy.count(), 1);
1032 textEditObject->setSelectionColor(QColor("black"));
1033 QCOMPARE(textEditObject->selectionColor(), QColor("black"));
1034 QCOMPARE(selectionColorSpy.count(), 1);
1036 textEditObject->setSelectedTextColor(QColor("blue"));
1037 QCOMPARE(textEditObject->selectedTextColor(), QColor("blue"));
1038 QCOMPARE(selectedTextColorSpy.count(), 1);
1040 textEditObject->setColor(QColor("white"));
1041 QCOMPARE(colorSpy.count(), 1);
1043 textEditObject->setSelectionColor(QColor("black"));
1044 QCOMPARE(selectionColorSpy.count(), 1);
1046 textEditObject->setSelectedTextColor(QColor("blue"));
1047 QCOMPARE(selectedTextColorSpy.count(), 1);
1049 textEditObject->setColor(QColor("black"));
1050 QCOMPARE(textEditObject->color(), QColor("black"));
1051 QCOMPARE(colorSpy.count(), 2);
1053 textEditObject->setSelectionColor(QColor("blue"));
1054 QCOMPARE(textEditObject->selectionColor(), QColor("blue"));
1055 QCOMPARE(selectionColorSpy.count(), 2);
1057 textEditObject->setSelectedTextColor(QColor("white"));
1058 QCOMPARE(textEditObject->selectedTextColor(), QColor("white"));
1059 QCOMPARE(selectedTextColorSpy.count(), 2);
1063 for (int i = 0; i < colorStrings.size(); i++)
1065 QString componentStr = "import QtQuick 2.0\nTextEdit { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
1066 QQmlComponent texteditComponent(&engine);
1067 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1068 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1069 //qDebug() << "textEditObject: " << textEditObject->color() << "vs. " << QColor(colorStrings.at(i));
1070 QVERIFY(textEditObject != 0);
1071 QCOMPARE(textEditObject->color(), QColor(colorStrings.at(i)));
1075 for (int i = 0; i < colorStrings.size(); i++)
1077 QString componentStr = "import QtQuick 2.0\nTextEdit { selectionColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
1078 QQmlComponent texteditComponent(&engine);
1079 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1080 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1081 QVERIFY(textEditObject != 0);
1082 QCOMPARE(textEditObject->selectionColor(), QColor(colorStrings.at(i)));
1085 //test selected text
1086 for (int i = 0; i < colorStrings.size(); i++)
1088 QString componentStr = "import QtQuick 2.0\nTextEdit { selectedTextColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
1089 QQmlComponent texteditComponent(&engine);
1090 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1091 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1092 QVERIFY(textEditObject != 0);
1093 QCOMPARE(textEditObject->selectedTextColor(), QColor(colorStrings.at(i)));
1097 QString colorStr = "#AA001234";
1098 QColor testColor("#001234");
1099 testColor.setAlpha(170);
1101 QString componentStr = "import QtQuick 2.0\nTextEdit { color: \"" + colorStr + "\"; text: \"Hello World\" }";
1102 QQmlComponent texteditComponent(&engine);
1103 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1104 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1106 QVERIFY(textEditObject != 0);
1107 QCOMPARE(textEditObject->color(), testColor);
1111 void tst_qquicktextedit::textMargin()
1113 for (qreal i=0; i<=10; i+=0.3) {
1114 QString componentStr = "import QtQuick 2.0\nTextEdit { textMargin: " + QString::number(i) + "; text: \"Hello World\" }";
1115 QQmlComponent texteditComponent(&engine);
1116 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1117 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1118 QVERIFY(textEditObject != 0);
1119 QCOMPARE(textEditObject->textMargin(), i);
1123 void tst_qquicktextedit::persistentSelection()
1125 QQuickView window(testFileUrl("persistentSelection.qml"));
1127 window.requestActivateWindow();
1128 QTest::qWaitForWindowActive(&window);
1130 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(window.rootObject());
1132 QVERIFY(edit->hasActiveFocus());
1134 QSignalSpy spy(edit, SIGNAL(persistentSelectionChanged(bool)));
1136 QCOMPARE(edit->persistentSelection(), false);
1138 edit->setPersistentSelection(false);
1139 QCOMPARE(edit->persistentSelection(), false);
1140 QCOMPARE(spy.count(), 0);
1143 QCOMPARE(edit->property("selected").toString(), QLatin1String("ell"));
1145 edit->setFocus(false);
1146 QCOMPARE(edit->property("selected").toString(), QString());
1148 edit->setFocus(true);
1149 QCOMPARE(edit->property("selected").toString(), QString());
1151 edit->setPersistentSelection(true);
1152 QCOMPARE(edit->persistentSelection(), true);
1153 QCOMPARE(spy.count(), 1);
1156 QCOMPARE(edit->property("selected").toString(), QLatin1String("ell"));
1158 edit->setFocus(false);
1159 QCOMPARE(edit->property("selected").toString(), QLatin1String("ell"));
1161 edit->setFocus(true);
1162 QCOMPARE(edit->property("selected").toString(), QLatin1String("ell"));
1166 void tst_qquicktextedit::focusOnPress()
1168 QString componentStr =
1169 "import QtQuick 2.0\n"
1171 "property bool selectOnFocus: false\n"
1172 "width: 100; height: 50\n"
1173 "activeFocusOnPress: true\n"
1174 "text: \"Hello World\"\n"
1175 "onFocusChanged: { if (focus && selectOnFocus) selectAll() }"
1177 QQmlComponent texteditComponent(&engine);
1178 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1179 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1180 QVERIFY(textEditObject != 0);
1181 QCOMPARE(textEditObject->focusOnPress(), true);
1182 QCOMPARE(textEditObject->hasFocus(), false);
1184 QSignalSpy focusSpy(textEditObject, SIGNAL(focusChanged(bool)));
1185 QSignalSpy activeFocusSpy(textEditObject, SIGNAL(focusChanged(bool)));
1186 QSignalSpy activeFocusOnPressSpy(textEditObject, SIGNAL(activeFocusOnPressChanged(bool)));
1188 textEditObject->setFocusOnPress(true);
1189 QCOMPARE(textEditObject->focusOnPress(), true);
1190 QCOMPARE(activeFocusOnPressSpy.count(), 0);
1192 QQuickWindow window;
1193 window.resize(100, 50);
1194 textEditObject->setParentItem(window.rootItem());
1196 window.requestActivateWindow();
1197 QTest::qWaitForWindowActive(&window);
1199 QCOMPARE(textEditObject->hasFocus(), false);
1200 QCOMPARE(textEditObject->hasActiveFocus(), false);
1202 QPoint centerPoint(window.width()/2, window.height()/2);
1203 Qt::KeyboardModifiers noModifiers = 0;
1204 QTest::mousePress(&window, Qt::LeftButton, noModifiers, centerPoint);
1205 QGuiApplication::processEvents();
1206 QCOMPARE(textEditObject->hasFocus(), true);
1207 QCOMPARE(textEditObject->hasActiveFocus(), true);
1208 QCOMPARE(focusSpy.count(), 1);
1209 QCOMPARE(activeFocusSpy.count(), 1);
1210 QCOMPARE(textEditObject->selectedText(), QString());
1211 QTest::mouseRelease(&window, Qt::LeftButton, noModifiers, centerPoint);
1213 textEditObject->setFocusOnPress(false);
1214 QCOMPARE(textEditObject->focusOnPress(), false);
1215 QCOMPARE(activeFocusOnPressSpy.count(), 1);
1217 textEditObject->setFocus(false);
1218 QCOMPARE(textEditObject->hasFocus(), false);
1219 QCOMPARE(textEditObject->hasActiveFocus(), false);
1220 QCOMPARE(focusSpy.count(), 2);
1221 QCOMPARE(activeFocusSpy.count(), 2);
1223 // Wait for double click timeout to expire before clicking again.
1225 QTest::mousePress(&window, Qt::LeftButton, noModifiers, centerPoint);
1226 QGuiApplication::processEvents();
1227 QCOMPARE(textEditObject->hasFocus(), false);
1228 QCOMPARE(textEditObject->hasActiveFocus(), false);
1229 QCOMPARE(focusSpy.count(), 2);
1230 QCOMPARE(activeFocusSpy.count(), 2);
1231 QTest::mouseRelease(&window, Qt::LeftButton, noModifiers, centerPoint);
1233 textEditObject->setFocusOnPress(true);
1234 QCOMPARE(textEditObject->focusOnPress(), true);
1235 QCOMPARE(activeFocusOnPressSpy.count(), 2);
1237 // Test a selection made in the on(Active)FocusChanged handler isn't overwritten.
1238 textEditObject->setProperty("selectOnFocus", true);
1241 QTest::mousePress(&window, Qt::LeftButton, noModifiers, centerPoint);
1242 QGuiApplication::processEvents();
1243 QCOMPARE(textEditObject->hasFocus(), true);
1244 QCOMPARE(textEditObject->hasActiveFocus(), true);
1245 QCOMPARE(focusSpy.count(), 3);
1246 QCOMPARE(activeFocusSpy.count(), 3);
1247 QCOMPARE(textEditObject->selectedText(), textEditObject->text());
1248 QTest::mouseRelease(&window, Qt::LeftButton, noModifiers, centerPoint);
1251 void tst_qquicktextedit::selection()
1253 QString testStr = standard[0];//TODO: What should happen for multiline/rich text?
1254 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \""+ testStr +"\"; }";
1255 QQmlComponent texteditComponent(&engine);
1256 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1257 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1258 QVERIFY(textEditObject != 0);
1261 //Test selection follows cursor
1262 for (int i=0; i<= testStr.size(); i++) {
1263 textEditObject->setCursorPosition(i);
1264 QCOMPARE(textEditObject->cursorPosition(), i);
1265 QCOMPARE(textEditObject->selectionStart(), i);
1266 QCOMPARE(textEditObject->selectionEnd(), i);
1267 QVERIFY(textEditObject->selectedText().isNull());
1270 textEditObject->setCursorPosition(0);
1271 QVERIFY(textEditObject->cursorPosition() == 0);
1272 QVERIFY(textEditObject->selectionStart() == 0);
1273 QVERIFY(textEditObject->selectionEnd() == 0);
1274 QVERIFY(textEditObject->selectedText().isNull());
1276 // Verify invalid positions are ignored.
1277 textEditObject->setCursorPosition(-1);
1278 QVERIFY(textEditObject->cursorPosition() == 0);
1279 QVERIFY(textEditObject->selectionStart() == 0);
1280 QVERIFY(textEditObject->selectionEnd() == 0);
1281 QVERIFY(textEditObject->selectedText().isNull());
1283 textEditObject->setCursorPosition(textEditObject->text().count()+1);
1284 QVERIFY(textEditObject->cursorPosition() == 0);
1285 QVERIFY(textEditObject->selectionStart() == 0);
1286 QVERIFY(textEditObject->selectionEnd() == 0);
1287 QVERIFY(textEditObject->selectedText().isNull());
1290 for (int i=0; i<= testStr.size(); i++) {
1291 textEditObject->select(0,i);
1292 QCOMPARE(testStr.mid(0,i), textEditObject->selectedText());
1294 for (int i=0; i<= testStr.size(); i++) {
1295 textEditObject->select(i,testStr.size());
1296 QCOMPARE(testStr.mid(i,testStr.size()-i), textEditObject->selectedText());
1299 textEditObject->setCursorPosition(0);
1300 QVERIFY(textEditObject->cursorPosition() == 0);
1301 QVERIFY(textEditObject->selectionStart() == 0);
1302 QVERIFY(textEditObject->selectionEnd() == 0);
1303 QVERIFY(textEditObject->selectedText().isNull());
1305 //Test Error Ignoring behaviour
1306 textEditObject->setCursorPosition(0);
1307 QVERIFY(textEditObject->selectedText().isNull());
1308 textEditObject->select(-10,0);
1309 QVERIFY(textEditObject->selectedText().isNull());
1310 textEditObject->select(100,101);
1311 QVERIFY(textEditObject->selectedText().isNull());
1312 textEditObject->select(0,-10);
1313 QVERIFY(textEditObject->selectedText().isNull());
1314 textEditObject->select(0,100);
1315 QVERIFY(textEditObject->selectedText().isNull());
1316 textEditObject->select(0,10);
1317 QVERIFY(textEditObject->selectedText().size() == 10);
1318 textEditObject->select(-10,0);
1319 QVERIFY(textEditObject->selectedText().size() == 10);
1320 textEditObject->select(100,101);
1321 QVERIFY(textEditObject->selectedText().size() == 10);
1322 textEditObject->select(0,-10);
1323 QVERIFY(textEditObject->selectedText().size() == 10);
1324 textEditObject->select(0,100);
1325 QVERIFY(textEditObject->selectedText().size() == 10);
1327 textEditObject->deselect();
1328 QVERIFY(textEditObject->selectedText().isNull());
1329 textEditObject->select(0,10);
1330 QVERIFY(textEditObject->selectedText().size() == 10);
1331 textEditObject->deselect();
1332 QVERIFY(textEditObject->selectedText().isNull());
1335 void tst_qquicktextedit::isRightToLeft_data()
1337 QTest::addColumn<QString>("text");
1338 QTest::addColumn<bool>("emptyString");
1339 QTest::addColumn<bool>("firstCharacter");
1340 QTest::addColumn<bool>("lastCharacter");
1341 QTest::addColumn<bool>("middleCharacter");
1342 QTest::addColumn<bool>("startString");
1343 QTest::addColumn<bool>("midString");
1344 QTest::addColumn<bool>("endString");
1346 const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647};
1347 QTest::newRow("Empty") << "" << false << false << false << false << false << false << false;
1348 QTest::newRow("Neutral") << "23244242" << false << false << false << false << false << false << false;
1349 QTest::newRow("LTR") << "Hello world" << false << false << false << false << false << false << false;
1350 QTest::newRow("RTL") << QString::fromUtf16(arabic_str, 11) << false << true << true << true << true << true << true;
1351 QTest::newRow("Bidi RTL + LTR + RTL") << QString::fromUtf16(arabic_str, 11) + QString("Hello world") + QString::fromUtf16(arabic_str, 11) << false << true << true << false << true << true << true;
1352 QTest::newRow("Bidi LTR + RTL + LTR") << QString("Hello world") + QString::fromUtf16(arabic_str, 11) + QString("Hello world") << false << false << false << true << false << false << false;
1355 void tst_qquicktextedit::isRightToLeft()
1357 QFETCH(QString, text);
1358 QFETCH(bool, emptyString);
1359 QFETCH(bool, firstCharacter);
1360 QFETCH(bool, lastCharacter);
1361 QFETCH(bool, middleCharacter);
1362 QFETCH(bool, startString);
1363 QFETCH(bool, midString);
1364 QFETCH(bool, endString);
1366 QQuickTextEdit textEdit;
1367 textEdit.setText(text);
1369 // first test that the right string is delivered to the QString::isRightToLeft()
1370 QCOMPARE(textEdit.isRightToLeft(0,0), text.mid(0,0).isRightToLeft());
1371 QCOMPARE(textEdit.isRightToLeft(0,1), text.mid(0,1).isRightToLeft());
1372 QCOMPARE(textEdit.isRightToLeft(text.count()-2, text.count()-1), text.mid(text.count()-2, text.count()-1).isRightToLeft());
1373 QCOMPARE(textEdit.isRightToLeft(text.count()/2, text.count()/2 + 1), text.mid(text.count()/2, text.count()/2 + 1).isRightToLeft());
1374 QCOMPARE(textEdit.isRightToLeft(0,text.count()/4), text.mid(0,text.count()/4).isRightToLeft());
1375 QCOMPARE(textEdit.isRightToLeft(text.count()/4,3*text.count()/4), text.mid(text.count()/4,3*text.count()/4).isRightToLeft());
1377 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML TextEdit: isRightToLeft(start, end) called with the end property being smaller than the start.");
1378 QCOMPARE(textEdit.isRightToLeft(3*text.count()/4,text.count()-1), text.mid(3*text.count()/4,text.count()-1).isRightToLeft());
1380 // then test that the feature actually works
1381 QCOMPARE(textEdit.isRightToLeft(0,0), emptyString);
1382 QCOMPARE(textEdit.isRightToLeft(0,1), firstCharacter);
1383 QCOMPARE(textEdit.isRightToLeft(text.count()-2, text.count()-1), lastCharacter);
1384 QCOMPARE(textEdit.isRightToLeft(text.count()/2, text.count()/2 + 1), middleCharacter);
1385 QCOMPARE(textEdit.isRightToLeft(0,text.count()/4), startString);
1386 QCOMPARE(textEdit.isRightToLeft(text.count()/4,3*text.count()/4), midString);
1388 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML TextEdit: isRightToLeft(start, end) called with the end property being smaller than the start.");
1389 QCOMPARE(textEdit.isRightToLeft(3*text.count()/4,text.count()-1), endString);
1392 void tst_qquicktextedit::keySelection()
1394 QQuickView window(testFileUrl("navigation.qml"));
1396 window.requestActivateWindow();
1397 QTest::qWaitForWindowActive(&window);
1399 QVERIFY(window.rootObject() != 0);
1401 QQuickTextEdit *input = qobject_cast<QQuickTextEdit *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
1403 QVERIFY(input != 0);
1404 QVERIFY(input->hasActiveFocus());
1406 QSignalSpy spy(input, SIGNAL(selectedTextChanged()));
1408 simulateKey(&window, Qt::Key_Right, Qt::ShiftModifier);
1409 QVERIFY(input->hasActiveFocus() == true);
1410 QCOMPARE(input->selectedText(), QString("a"));
1411 QCOMPARE(spy.count(), 1);
1412 simulateKey(&window, Qt::Key_Right);
1413 QVERIFY(input->hasActiveFocus() == true);
1414 QCOMPARE(input->selectedText(), QString());
1415 QCOMPARE(spy.count(), 2);
1416 simulateKey(&window, Qt::Key_Right);
1417 QVERIFY(input->hasActiveFocus() == false);
1418 QCOMPARE(input->selectedText(), QString());
1419 QCOMPARE(spy.count(), 2);
1421 simulateKey(&window, Qt::Key_Left);
1422 QVERIFY(input->hasActiveFocus() == true);
1423 QCOMPARE(spy.count(), 2);
1424 simulateKey(&window, Qt::Key_Left, Qt::ShiftModifier);
1425 QVERIFY(input->hasActiveFocus() == true);
1426 QCOMPARE(input->selectedText(), QString("a"));
1427 QCOMPARE(spy.count(), 3);
1428 simulateKey(&window, Qt::Key_Left);
1429 QVERIFY(input->hasActiveFocus() == true);
1430 QCOMPARE(input->selectedText(), QString());
1431 QCOMPARE(spy.count(), 4);
1432 simulateKey(&window, Qt::Key_Left);
1433 QVERIFY(input->hasActiveFocus() == false);
1434 QCOMPARE(input->selectedText(), QString());
1435 QCOMPARE(spy.count(), 4);
1438 void tst_qquicktextedit::moveCursorSelection_data()
1440 QTest::addColumn<QString>("testStr");
1441 QTest::addColumn<int>("cursorPosition");
1442 QTest::addColumn<int>("movePosition");
1443 QTest::addColumn<QQuickTextEdit::SelectionMode>("mode");
1444 QTest::addColumn<int>("selectionStart");
1445 QTest::addColumn<int>("selectionEnd");
1446 QTest::addColumn<bool>("reversible");
1448 QTest::newRow("(t)he|characters")
1449 << standard[0] << 0 << 1 << QQuickTextEdit::SelectCharacters << 0 << 1 << true;
1450 QTest::newRow("do(g)|characters")
1451 << standard[0] << 43 << 44 << QQuickTextEdit::SelectCharacters << 43 << 44 << true;
1452 QTest::newRow("jum(p)ed|characters")
1453 << standard[0] << 23 << 24 << QQuickTextEdit::SelectCharacters << 23 << 24 << true;
1454 QTest::newRow("jumped( )over|characters")
1455 << standard[0] << 26 << 27 << QQuickTextEdit::SelectCharacters << 26 << 27 << true;
1456 QTest::newRow("(the )|characters")
1457 << standard[0] << 0 << 4 << QQuickTextEdit::SelectCharacters << 0 << 4 << true;
1458 QTest::newRow("( dog)|characters")
1459 << standard[0] << 40 << 44 << QQuickTextEdit::SelectCharacters << 40 << 44 << true;
1460 QTest::newRow("( jumped )|characters")
1461 << standard[0] << 19 << 27 << QQuickTextEdit::SelectCharacters << 19 << 27 << true;
1462 QTest::newRow("th(e qu)ick|characters")
1463 << standard[0] << 2 << 6 << QQuickTextEdit::SelectCharacters << 2 << 6 << true;
1464 QTest::newRow("la(zy d)og|characters")
1465 << standard[0] << 38 << 42 << QQuickTextEdit::SelectCharacters << 38 << 42 << true;
1466 QTest::newRow("jum(ped ov)er|characters")
1467 << standard[0] << 23 << 29 << QQuickTextEdit::SelectCharacters << 23 << 29 << true;
1468 QTest::newRow("()the|characters")
1469 << standard[0] << 0 << 0 << QQuickTextEdit::SelectCharacters << 0 << 0 << true;
1470 QTest::newRow("dog()|characters")
1471 << standard[0] << 44 << 44 << QQuickTextEdit::SelectCharacters << 44 << 44 << true;
1472 QTest::newRow("jum()ped|characters")
1473 << standard[0] << 23 << 23 << QQuickTextEdit::SelectCharacters << 23 << 23 << true;
1475 QTest::newRow("<(t)he>|words")
1476 << standard[0] << 0 << 1 << QQuickTextEdit::SelectWords << 0 << 3 << true;
1477 QTest::newRow("<do(g)>|words")
1478 << standard[0] << 43 << 44 << QQuickTextEdit::SelectWords << 41 << 44 << true;
1479 QTest::newRow("<jum(p)ed>|words")
1480 << standard[0] << 23 << 24 << QQuickTextEdit::SelectWords << 20 << 26 << true;
1481 QTest::newRow("<jumped( )>over|words")
1482 << standard[0] << 26 << 27 << QQuickTextEdit::SelectWords << 20 << 27 << false;
1483 QTest::newRow("jumped<( )over>|words,reversed")
1484 << standard[0] << 27 << 26 << QQuickTextEdit::SelectWords << 26 << 31 << false;
1485 QTest::newRow("<(the )>quick|words")
1486 << standard[0] << 0 << 4 << QQuickTextEdit::SelectWords << 0 << 4 << false;
1487 QTest::newRow("<(the )quick>|words,reversed")
1488 << standard[0] << 4 << 0 << QQuickTextEdit::SelectWords << 0 << 9 << false;
1489 QTest::newRow("<lazy( dog)>|words")
1490 << standard[0] << 40 << 44 << QQuickTextEdit::SelectWords << 36 << 44 << false;
1491 QTest::newRow("lazy<( dog)>|words,reversed")
1492 << standard[0] << 44 << 40 << QQuickTextEdit::SelectWords << 40 << 44 << false;
1493 QTest::newRow("<fox( jumped )>over|words")
1494 << standard[0] << 19 << 27 << QQuickTextEdit::SelectWords << 16 << 27 << false;
1495 QTest::newRow("fox<( jumped )over>|words,reversed")
1496 << standard[0] << 27 << 19 << QQuickTextEdit::SelectWords << 19 << 31 << false;
1497 QTest::newRow("<th(e qu)ick>|words")
1498 << standard[0] << 2 << 6 << QQuickTextEdit::SelectWords << 0 << 9 << true;
1499 QTest::newRow("<la(zy d)og|words>")
1500 << standard[0] << 38 << 42 << QQuickTextEdit::SelectWords << 36 << 44 << true;
1501 QTest::newRow("<jum(ped ov)er>|words")
1502 << standard[0] << 23 << 29 << QQuickTextEdit::SelectWords << 20 << 31 << true;
1503 QTest::newRow("<()>the|words")
1504 << standard[0] << 0 << 0 << QQuickTextEdit::SelectWords << 0 << 0 << true;
1505 QTest::newRow("dog<()>|words")
1506 << standard[0] << 44 << 44 << QQuickTextEdit::SelectWords << 44 << 44 << true;
1507 QTest::newRow("jum<()>ped|words")
1508 << standard[0] << 23 << 23 << QQuickTextEdit::SelectWords << 23 << 23 << true;
1510 QTest::newRow("Hello<(,)> |words")
1511 << standard[2] << 5 << 6 << QQuickTextEdit::SelectWords << 5 << 6 << true;
1512 QTest::newRow("Hello<(, )>world|words")
1513 << standard[2] << 5 << 7 << QQuickTextEdit::SelectWords << 5 << 7 << false;
1514 QTest::newRow("Hello<(, )world>|words,reversed")
1515 << standard[2] << 7 << 5 << QQuickTextEdit::SelectWords << 5 << 12 << false;
1516 QTest::newRow("<Hel(lo, )>world|words")
1517 << standard[2] << 3 << 7 << QQuickTextEdit::SelectWords << 0 << 7 << false;
1518 QTest::newRow("<Hel(lo, )world>|words,reversed")
1519 << standard[2] << 7 << 3 << QQuickTextEdit::SelectWords << 0 << 12 << false;
1520 QTest::newRow("<Hel(lo)>,|words")
1521 << standard[2] << 3 << 5 << QQuickTextEdit::SelectWords << 0 << 5 << true;
1522 QTest::newRow("Hello<()>,|words")
1523 << standard[2] << 5 << 5 << QQuickTextEdit::SelectWords << 5 << 5 << true;
1524 QTest::newRow("Hello,<()>|words")
1525 << standard[2] << 6 << 6 << QQuickTextEdit::SelectWords << 6 << 6 << true;
1526 QTest::newRow("Hello<,( )>world|words")
1527 << standard[2] << 6 << 7 << QQuickTextEdit::SelectWords << 5 << 7 << false;
1528 QTest::newRow("Hello,<( )world>|words,reversed")
1529 << standard[2] << 7 << 6 << QQuickTextEdit::SelectWords << 6 << 12 << false;
1530 QTest::newRow("Hello<,( world)>|words")
1531 << standard[2] << 6 << 12 << QQuickTextEdit::SelectWords << 5 << 12 << false;
1532 QTest::newRow("Hello,<( world)>|words,reversed")
1533 << standard[2] << 12 << 6 << QQuickTextEdit::SelectWords << 6 << 12 << false;
1534 QTest::newRow("Hello<,( world!)>|words")
1535 << standard[2] << 6 << 13 << QQuickTextEdit::SelectWords << 5 << 13 << false;
1536 QTest::newRow("Hello,<( world!)>|words,reversed")
1537 << standard[2] << 13 << 6 << QQuickTextEdit::SelectWords << 6 << 13 << false;
1538 QTest::newRow("Hello<(, world!)>|words")
1539 << standard[2] << 5 << 13 << QQuickTextEdit::SelectWords << 5 << 13 << true;
1540 QTest::newRow("world<(!)>|words")
1541 << standard[2] << 12 << 13 << QQuickTextEdit::SelectWords << 12 << 13 << true;
1542 QTest::newRow("world!<()>)|words")
1543 << standard[2] << 13 << 13 << QQuickTextEdit::SelectWords << 13 << 13 << true;
1544 QTest::newRow("world<()>!)|words")
1545 << standard[2] << 12 << 12 << QQuickTextEdit::SelectWords << 12 << 12 << true;
1547 QTest::newRow("<(,)>olleH |words")
1548 << standard[3] << 7 << 8 << QQuickTextEdit::SelectWords << 7 << 8 << true;
1549 QTest::newRow("<dlrow( ,)>olleH|words")
1550 << standard[3] << 6 << 8 << QQuickTextEdit::SelectWords << 1 << 8 << false;
1551 QTest::newRow("dlrow<( ,)>olleH|words,reversed")
1552 << standard[3] << 8 << 6 << QQuickTextEdit::SelectWords << 6 << 8 << false;
1553 QTest::newRow("<dlrow( ,ol)leH>|words")
1554 << standard[3] << 6 << 10 << QQuickTextEdit::SelectWords << 1 << 13 << false;
1555 QTest::newRow("dlrow<( ,ol)leH>|words,reversed")
1556 << standard[3] << 10 << 6 << QQuickTextEdit::SelectWords << 6 << 13 << false;
1557 QTest::newRow(",<(ol)leH>,|words")
1558 << standard[3] << 8 << 10 << QQuickTextEdit::SelectWords << 8 << 13 << true;
1559 QTest::newRow(",<()>olleH|words")
1560 << standard[3] << 8 << 8 << QQuickTextEdit::SelectWords << 8 << 8 << true;
1561 QTest::newRow("<()>,olleH|words")
1562 << standard[3] << 7 << 7 << QQuickTextEdit::SelectWords << 7 << 7 << true;
1563 QTest::newRow("<dlrow( )>,olleH|words")
1564 << standard[3] << 6 << 7 << QQuickTextEdit::SelectWords << 1 << 7 << false;
1565 QTest::newRow("dlrow<( ),>olleH|words,reversed")
1566 << standard[3] << 7 << 6 << QQuickTextEdit::SelectWords << 6 << 8 << false;
1567 QTest::newRow("<(dlrow )>,olleH|words")
1568 << standard[3] << 1 << 7 << QQuickTextEdit::SelectWords << 1 << 7 << false;
1569 QTest::newRow("<(dlrow ),>olleH|words,reversed")
1570 << standard[3] << 7 << 1 << QQuickTextEdit::SelectWords << 1 << 8 << false;
1571 QTest::newRow("<(!dlrow )>,olleH|words")
1572 << standard[3] << 0 << 7 << QQuickTextEdit::SelectWords << 0 << 7 << false;
1573 QTest::newRow("<(!dlrow ),>olleH|words,reversed")
1574 << standard[3] << 7 << 0 << QQuickTextEdit::SelectWords << 0 << 8 << false;
1575 QTest::newRow("(!dlrow ,)olleH|words")
1576 << standard[3] << 0 << 8 << QQuickTextEdit::SelectWords << 0 << 8 << true;
1577 QTest::newRow("<(!)>dlrow|words")
1578 << standard[3] << 0 << 1 << QQuickTextEdit::SelectWords << 0 << 1 << true;
1579 QTest::newRow("<()>!dlrow|words")
1580 << standard[3] << 0 << 0 << QQuickTextEdit::SelectWords << 0 << 0 << true;
1581 QTest::newRow("!<()>dlrow|words")
1582 << standard[3] << 1 << 1 << QQuickTextEdit::SelectWords << 1 << 1 << true;
1585 void tst_qquicktextedit::moveCursorSelection()
1587 QFETCH(QString, testStr);
1588 QFETCH(int, cursorPosition);
1589 QFETCH(int, movePosition);
1590 QFETCH(QQuickTextEdit::SelectionMode, mode);
1591 QFETCH(int, selectionStart);
1592 QFETCH(int, selectionEnd);
1593 QFETCH(bool, reversible);
1595 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \""+ testStr +"\"; }";
1596 QQmlComponent textinputComponent(&engine);
1597 textinputComponent.setData(componentStr.toLatin1(), QUrl());
1598 QQuickTextEdit *texteditObject = qobject_cast<QQuickTextEdit*>(textinputComponent.create());
1599 QVERIFY(texteditObject != 0);
1601 texteditObject->setCursorPosition(cursorPosition);
1602 texteditObject->moveCursorSelection(movePosition, mode);
1604 QCOMPARE(texteditObject->selectedText(), testStr.mid(selectionStart, selectionEnd - selectionStart));
1605 QCOMPARE(texteditObject->selectionStart(), selectionStart);
1606 QCOMPARE(texteditObject->selectionEnd(), selectionEnd);
1609 texteditObject->setCursorPosition(movePosition);
1610 texteditObject->moveCursorSelection(cursorPosition, mode);
1612 QCOMPARE(texteditObject->selectedText(), testStr.mid(selectionStart, selectionEnd - selectionStart));
1613 QCOMPARE(texteditObject->selectionStart(), selectionStart);
1614 QCOMPARE(texteditObject->selectionEnd(), selectionEnd);
1618 void tst_qquicktextedit::moveCursorSelectionSequence_data()
1620 QTest::addColumn<QString>("testStr");
1621 QTest::addColumn<int>("cursorPosition");
1622 QTest::addColumn<int>("movePosition1");
1623 QTest::addColumn<int>("movePosition2");
1624 QTest::addColumn<int>("selection1Start");
1625 QTest::addColumn<int>("selection1End");
1626 QTest::addColumn<int>("selection2Start");
1627 QTest::addColumn<int>("selection2End");
1629 QTest::newRow("the {<quick( bro)wn> f^ox} jumped|ltr")
1634 QTest::newRow("the quick<( {bro)wn> f^ox} jumped|rtl")
1639 QTest::newRow("the {<quick( bro)wn> ^}fox jumped|ltr")
1644 QTest::newRow("the quick<( {bro)wn> ^}fox jumped|rtl")
1649 QTest::newRow("the {<quick( bro)wn^>} fox jumped|ltr")
1654 QTest::newRow("the quick<( {bro)wn^>} f^ox jumped|rtl")
1659 QTest::newRow("the {<quick() ^}bro)wn> fox|ltr")
1664 QTest::newRow("the quick<(^ {^bro)wn>} fox|rtl")
1669 QTest::newRow("the {<quick^}( bro)wn> fox|ltr")
1674 QTest::newRow("the quick{<(^ bro)wn>} fox|rtl")
1679 QTest::newRow("the {<qui^ck}( bro)wn> fox|ltr")
1684 QTest::newRow("the {<qui^ck}( bro)wn> fox|rtl")
1689 QTest::newRow("the {<^quick}( bro)wn> fox|ltr")
1694 QTest::newRow("the {<^quick}( bro)wn> fox|rtl")
1699 QTest::newRow("the{^ <quick}( bro)wn> fox|ltr")
1704 QTest::newRow("the{^ <quick}( bro)wn> fox|rtl")
1709 QTest::newRow("{t^he <quick}( bro)wn> fox|ltr")
1714 QTest::newRow("{t^he <quick}( bro)wn> fox|rtl")
1720 QTest::newRow("{<He(ll)o>, w^orld}!|ltr")
1725 QTest::newRow("{<He(ll)o>, w^orld}!|rtl")
1731 QTest::newRow("!{dlro^w ,<o(ll)eH>}|ltr")
1736 QTest::newRow("!{dlro^w ,<o(ll)eH>}|rtl")
1743 void tst_qquicktextedit::moveCursorSelectionSequence()
1745 QFETCH(QString, testStr);
1746 QFETCH(int, cursorPosition);
1747 QFETCH(int, movePosition1);
1748 QFETCH(int, movePosition2);
1749 QFETCH(int, selection1Start);
1750 QFETCH(int, selection1End);
1751 QFETCH(int, selection2Start);
1752 QFETCH(int, selection2End);
1754 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \""+ testStr +"\"; }";
1755 QQmlComponent texteditComponent(&engine);
1756 texteditComponent.setData(componentStr.toLatin1(), QUrl());
1757 QQuickTextEdit *texteditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
1758 QVERIFY(texteditObject != 0);
1760 texteditObject->setCursorPosition(cursorPosition);
1762 texteditObject->moveCursorSelection(movePosition1, QQuickTextEdit::SelectWords);
1763 QCOMPARE(texteditObject->selectedText(), testStr.mid(selection1Start, selection1End - selection1Start));
1764 QCOMPARE(texteditObject->selectionStart(), selection1Start);
1765 QCOMPARE(texteditObject->selectionEnd(), selection1End);
1767 texteditObject->moveCursorSelection(movePosition2, QQuickTextEdit::SelectWords);
1768 QCOMPARE(texteditObject->selectedText(), testStr.mid(selection2Start, selection2End - selection2Start));
1769 QCOMPARE(texteditObject->selectionStart(), selection2Start);
1770 QCOMPARE(texteditObject->selectionEnd(), selection2End);
1774 void tst_qquicktextedit::mouseSelection_data()
1776 QTest::addColumn<QString>("qmlfile");
1777 QTest::addColumn<int>("from");
1778 QTest::addColumn<int>("to");
1779 QTest::addColumn<QString>("selectedText");
1780 QTest::addColumn<bool>("focus");
1781 QTest::addColumn<bool>("focusOnPress");
1782 QTest::addColumn<int>("clicks");
1785 QTest::newRow("on") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << true << true << 1;
1786 QTest::newRow("off") << testFile("mouseselection_false.qml") << 4 << 9 << QString() << true << true << 1;
1787 QTest::newRow("default") << testFile("mouseselection_default.qml") << 4 << 9 << QString() << true << true << 1;
1788 QTest::newRow("off word selection") << testFile("mouseselection_false_words.qml") << 4 << 9 << QString() << true << true << 1;
1789 QTest::newRow("on word selection (4,9)") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << true << true << 1;
1791 QTest::newRow("on unfocused") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << false << false << 1;
1792 QTest::newRow("on word selection (4,9) unfocused") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << false << false << 1;
1794 QTest::newRow("on focus on press") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << false << true << 1;
1795 QTest::newRow("on word selection (4,9) focus on press") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << false << true << 1;
1797 QTest::newRow("on word selection (2,13)") << testFile("mouseselection_true_words.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1798 QTest::newRow("on word selection (2,30)") << testFile("mouseselection_true_words.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1799 QTest::newRow("on word selection (9,13)") << testFile("mouseselection_true_words.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1800 QTest::newRow("on word selection (9,30)") << testFile("mouseselection_true_words.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1801 QTest::newRow("on word selection (13,2)") << testFile("mouseselection_true_words.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1802 QTest::newRow("on word selection (20,2)") << testFile("mouseselection_true_words.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1803 QTest::newRow("on word selection (12,9)") << testFile("mouseselection_true_words.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1804 QTest::newRow("on word selection (30,9)") << testFile("mouseselection_true_words.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 1;
1806 QTest::newRow("on double click (4,9)") << testFile("mouseselection_true.qml") << 4 << 9 << "0123456789" << true << true << 2;
1807 QTest::newRow("on double click (2,13)") << testFile("mouseselection_true.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1808 QTest::newRow("on double click (2,30)") << testFile("mouseselection_true.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1809 QTest::newRow("on double click (9,13)") << testFile("mouseselection_true.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1810 QTest::newRow("on double click (9,30)") << testFile("mouseselection_true.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1811 QTest::newRow("on double click (13,2)") << testFile("mouseselection_true.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1812 QTest::newRow("on double click (20,2)") << testFile("mouseselection_true.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1813 QTest::newRow("on double click (12,9)") << testFile("mouseselection_true.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1814 QTest::newRow("on double click (30,9)") << testFile("mouseselection_true.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << 2;
1816 QTest::newRow("on triple click (4,9)") << testFile("mouseselection_true.qml") << 4 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1817 QTest::newRow("on triple click (2,13)") << testFile("mouseselection_true.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1818 QTest::newRow("on triple click (2,30)") << testFile("mouseselection_true.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1819 QTest::newRow("on triple click (9,13)") << testFile("mouseselection_true.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1820 QTest::newRow("on triple click (9,30)") << testFile("mouseselection_true.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1821 QTest::newRow("on triple click (13,2)") << testFile("mouseselection_true.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1822 QTest::newRow("on triple click (20,2)") << testFile("mouseselection_true.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1823 QTest::newRow("on triple click (12,9)") << testFile("mouseselection_true.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1824 QTest::newRow("on triple click (30,9)") << testFile("mouseselection_true.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" << true << true << 3;
1826 QTest::newRow("on triple click (2,40)") << testFile("mouseselection_true.qml") << 2 << 40 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n" << true << true << 3;
1827 QTest::newRow("on triple click (2,50)") << testFile("mouseselection_true.qml") << 2 << 50 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n\nZXYWVUTSRQPON MLKJIHGFEDCBA" << true << true << 3;
1828 QTest::newRow("on triple click (25,40)") << testFile("mouseselection_true.qml") << 25 << 40 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n" << true << true << 3;
1829 QTest::newRow("on triple click (25,50)") << testFile("mouseselection_true.qml") << 25 << 50 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n\nZXYWVUTSRQPON MLKJIHGFEDCBA" << true << true << 3;
1830 QTest::newRow("on triple click (40,25)") << testFile("mouseselection_true.qml") << 40 << 25 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n" << true << true << 3;
1831 QTest::newRow("on triple click (40,50)") << testFile("mouseselection_true.qml") << 40 << 50 << "9876543210\n\nZXYWVUTSRQPON MLKJIHGFEDCBA" << true << true << 3;
1832 QTest::newRow("on triple click (50,25)") << testFile("mouseselection_true.qml") << 50 << 25 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\n9876543210\n\nZXYWVUTSRQPON MLKJIHGFEDCBA" << true << true << 3;
1833 QTest::newRow("on triple click (50,40)") << testFile("mouseselection_true.qml") << 50 << 40 << "9876543210\n\nZXYWVUTSRQPON MLKJIHGFEDCBA" << true << true << 3;
1835 QTest::newRow("on tr align") << testFile("mouseselection_align_tr.qml") << 4 << 9 << "45678" << true << true << 1;
1836 QTest::newRow("on center align") << testFile("mouseselection_align_center.qml") << 4 << 9 << "45678" << true << true << 1;
1837 QTest::newRow("on bl align") << testFile("mouseselection_align_bl.qml") << 4 << 9 << "45678" << true << true << 1;
1840 void tst_qquicktextedit::mouseSelection()
1842 QFETCH(QString, qmlfile);
1845 QFETCH(QString, selectedText);
1846 QFETCH(bool, focus);
1847 QFETCH(bool, focusOnPress);
1848 QFETCH(int, clicks);
1850 QQuickView window(QUrl::fromLocalFile(qmlfile));
1853 window.requestActivateWindow();
1854 QTest::qWaitForWindowActive(&window);
1856 QVERIFY(window.rootObject() != 0);
1857 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
1858 QVERIFY(textEditObject != 0);
1860 textEditObject->setFocus(focus);
1861 textEditObject->setFocusOnPress(focusOnPress);
1863 // press-and-drag-and-release from x1 to x2
1864 QPoint p1 = textEditObject->positionToRectangle(from).center().toPoint();
1865 QPoint p2 = textEditObject->positionToRectangle(to).center().toPoint();
1867 QTest::mouseClick(&window, Qt::LeftButton, Qt::NoModifier, p1);
1868 else if (clicks == 3)
1869 QTest::mouseDClick(&window, Qt::LeftButton, Qt::NoModifier, p1);
1870 QTest::mousePress(&window, Qt::LeftButton, Qt::NoModifier, p1);
1871 QTest::mouseMove(&window, p2);
1872 QTest::mouseRelease(&window, Qt::LeftButton, Qt::NoModifier, p2);
1873 QTRY_COMPARE(textEditObject->selectedText(), selectedText);
1875 // Clicking and shift to clicking between the same points should select the same text.
1876 textEditObject->setCursorPosition(0);
1878 QTest::mouseDClick(&window, Qt::LeftButton, Qt::NoModifier, p1);
1880 QTest::mouseClick(&window, Qt::LeftButton, Qt::NoModifier, p1);
1881 QTest::mouseClick(&window, Qt::LeftButton, Qt::ShiftModifier, p2);
1882 QTRY_COMPARE(textEditObject->selectedText(), selectedText);
1884 // ### This is to prevent double click detection from carrying over to the next test.
1885 QTest::qWait(QGuiApplication::styleHints()->mouseDoubleClickInterval() + 10);
1888 void tst_qquicktextedit::dragMouseSelection()
1890 QString qmlfile = testFile("mouseselection_true.qml");
1892 QQuickView window(QUrl::fromLocalFile(qmlfile));
1895 window.requestActivateWindow();
1896 QTest::qWaitForWindowActive(&window);
1898 QVERIFY(window.rootObject() != 0);
1899 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
1900 QVERIFY(textEditObject != 0);
1902 // press-and-drag-and-release from x1 to x2
1905 int y = QFontMetrics(textEditObject->font()).height() / 2;
1906 QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
1907 QTest::mouseMove(&window, QPoint(x2, y));
1908 QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
1911 QTRY_VERIFY((str1 = textEditObject->selectedText()).length() > 3);
1913 // press and drag the current selection.
1916 QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
1917 QTest::mouseMove(&window, QPoint(x2, y));
1918 QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
1921 QTRY_VERIFY((str2 = textEditObject->selectedText()).length() > 3);
1923 QVERIFY(str1 != str2); // Verify the second press and drag is a new selection and not the first moved.
1927 void tst_qquicktextedit::mouseSelectionMode_data()
1929 QTest::addColumn<QString>("qmlfile");
1930 QTest::addColumn<bool>("selectWords");
1933 QTest::newRow("SelectWords") << testFile("mouseselectionmode_words.qml") << true;
1934 QTest::newRow("SelectCharacters") << testFile("mouseselectionmode_characters.qml") << false;
1935 QTest::newRow("default") << testFile("mouseselectionmode_default.qml") << false;
1938 void tst_qquicktextedit::mouseSelectionMode()
1940 QFETCH(QString, qmlfile);
1941 QFETCH(bool, selectWords);
1943 QString text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1945 QQuickView window(QUrl::fromLocalFile(qmlfile));
1948 window.requestActivateWindow();
1949 QTest::qWaitForWindowActive(&window);
1951 QVERIFY(window.rootObject() != 0);
1952 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
1953 QVERIFY(textEditObject != 0);
1955 // press-and-drag-and-release from x1 to x2
1958 int y = textEditObject->height()/2;
1959 QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
1960 QTest::mouseMove(&window, QPoint(x2, y));
1961 QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
1962 QString str = textEditObject->selectedText();
1964 QTRY_COMPARE(textEditObject->selectedText(), text);
1966 QTRY_VERIFY(textEditObject->selectedText().length() > 3);
1967 QVERIFY(str != text);
1971 void tst_qquicktextedit::mouseSelectionMode_accessors()
1973 QQmlComponent component(&engine);
1974 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
1975 QScopedPointer<QObject> object(component.create());
1976 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
1979 QSignalSpy spy(edit, SIGNAL(mouseSelectionModeChanged(SelectionMode)));
1981 QCOMPARE(edit->mouseSelectionMode(), QQuickTextEdit::SelectCharacters);
1983 edit->setMouseSelectionMode(QQuickTextEdit::SelectWords);
1984 QCOMPARE(edit->mouseSelectionMode(), QQuickTextEdit::SelectWords);
1985 QCOMPARE(spy.count(), 1);
1987 edit->setMouseSelectionMode(QQuickTextEdit::SelectWords);
1988 QCOMPARE(spy.count(), 1);
1990 edit->setMouseSelectionMode(QQuickTextEdit::SelectCharacters);
1991 QCOMPARE(edit->mouseSelectionMode(), QQuickTextEdit::SelectCharacters);
1992 QCOMPARE(spy.count(), 2);
1995 void tst_qquicktextedit::selectByMouse()
1997 QQmlComponent component(&engine);
1998 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
1999 QScopedPointer<QObject> object(component.create());
2000 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
2003 QSignalSpy spy(edit, SIGNAL(selectByMouseChanged(bool)));
2005 QCOMPARE(edit->selectByMouse(), false);
2007 edit->setSelectByMouse(true);
2008 QCOMPARE(edit->selectByMouse(), true);
2009 QCOMPARE(spy.count(), 1);
2010 QCOMPARE(spy.at(0).at(0).toBool(), true);
2012 edit->setSelectByMouse(true);
2013 QCOMPARE(spy.count(), 1);
2015 edit->setSelectByMouse(false);
2016 QCOMPARE(edit->selectByMouse(), false);
2017 QCOMPARE(spy.count(), 2);
2018 QCOMPARE(spy.at(1).at(0).toBool(), false);
2021 void tst_qquicktextedit::renderType()
2023 QQmlComponent component(&engine);
2024 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
2025 QScopedPointer<QObject> object(component.create());
2026 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
2029 QSignalSpy spy(edit, SIGNAL(renderTypeChanged()));
2031 QCOMPARE(edit->renderType(), QQuickTextEdit::QtRendering);
2033 edit->setRenderType(QQuickTextEdit::NativeRendering);
2034 QCOMPARE(edit->renderType(), QQuickTextEdit::NativeRendering);
2035 QCOMPARE(spy.count(), 1);
2037 edit->setRenderType(QQuickTextEdit::NativeRendering);
2038 QCOMPARE(spy.count(), 1);
2040 edit->setRenderType(QQuickTextEdit::QtRendering);
2041 QCOMPARE(edit->renderType(), QQuickTextEdit::QtRendering);
2042 QCOMPARE(spy.count(), 2);
2045 void tst_qquicktextedit::inputMethodHints()
2047 QQuickView window(testFileUrl("inputmethodhints.qml"));
2049 window.requestActivateWindow();
2051 QVERIFY(window.rootObject() != 0);
2052 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
2053 QVERIFY(textEditObject != 0);
2054 QVERIFY(textEditObject->inputMethodHints() & Qt::ImhNoPredictiveText);
2055 QSignalSpy inputMethodHintSpy(textEditObject, SIGNAL(inputMethodHintsChanged()));
2056 textEditObject->setInputMethodHints(Qt::ImhUppercaseOnly);
2057 QVERIFY(textEditObject->inputMethodHints() & Qt::ImhUppercaseOnly);
2058 QCOMPARE(inputMethodHintSpy.count(), 1);
2059 textEditObject->setInputMethodHints(Qt::ImhUppercaseOnly);
2060 QCOMPARE(inputMethodHintSpy.count(), 1);
2062 QQuickTextEdit plainTextEdit;
2063 QCOMPARE(plainTextEdit.inputMethodHints(), Qt::ImhNone);
2066 void tst_qquicktextedit::positionAt_data()
2068 QTest::addColumn<QQuickTextEdit::HAlignment>("horizontalAlignment");
2069 QTest::addColumn<QQuickTextEdit::VAlignment>("verticalAlignment");
2071 QTest::newRow("top-left") << QQuickTextEdit::AlignLeft << QQuickTextEdit::AlignTop;
2072 QTest::newRow("bottom-left") << QQuickTextEdit::AlignLeft << QQuickTextEdit::AlignBottom;
2073 QTest::newRow("center-left") << QQuickTextEdit::AlignLeft << QQuickTextEdit::AlignVCenter;
2075 QTest::newRow("top-right") << QQuickTextEdit::AlignRight << QQuickTextEdit::AlignTop;
2076 QTest::newRow("top-center") << QQuickTextEdit::AlignHCenter << QQuickTextEdit::AlignTop;
2078 QTest::newRow("center") << QQuickTextEdit::AlignHCenter << QQuickTextEdit::AlignVCenter;
2081 void tst_qquicktextedit::positionAt()
2083 QFETCH(QQuickTextEdit::HAlignment, horizontalAlignment);
2084 QFETCH(QQuickTextEdit::VAlignment, verticalAlignment);
2086 QQuickView window(testFileUrl("positionAt.qml"));
2087 QVERIFY(window.rootObject() != 0);
2089 window.requestActivateWindow();
2090 QTest::qWaitForWindowActive(&window);
2092 QQuickTextEdit *texteditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
2093 QVERIFY(texteditObject != 0);
2094 texteditObject->setHAlign(horizontalAlignment);
2095 texteditObject->setVAlign(verticalAlignment);
2097 QTextLayout layout(texteditObject->text().replace(QLatin1Char('\n'), QChar::LineSeparator));
2098 layout.setFont(texteditObject->font());
2100 if (!qmlDisableDistanceField()) {
2102 option.setUseDesignMetrics(true);
2103 layout.setTextOption(option);
2106 layout.beginLayout();
2107 QTextLine line = layout.createLine();
2108 line.setLineWidth(texteditObject->width());
2109 QTextLine secondLine = layout.createLine();
2110 secondLine.setLineWidth(texteditObject->width());
2116 switch (verticalAlignment) {
2117 case QQuickTextEdit::AlignTop:
2118 y0 = line.height() / 2;
2119 y1 = line.height() * 3 / 2;
2121 case QQuickTextEdit::AlignVCenter:
2122 y0 = (texteditObject->height() - line.height()) / 2;
2123 y1 = (texteditObject->height() + line.height()) / 2;
2125 case QQuickTextEdit::AlignBottom:
2126 y0 = texteditObject->height() - line.height() * 3 / 2;
2127 y1 = texteditObject->height() - line.height() / 2;
2132 switch (horizontalAlignment) {
2133 case QQuickTextEdit::AlignLeft:
2136 case QQuickTextEdit::AlignHCenter:
2137 xoff = (texteditObject->width() - secondLine.naturalTextWidth()) / 2;
2139 case QQuickTextEdit::AlignRight:
2140 xoff = texteditObject->width() - secondLine.naturalTextWidth();
2143 int pos = texteditObject->positionAt(texteditObject->width()/2, y0);
2145 int widthBegin = floor(xoff + line.cursorToX(pos - 1));
2146 int widthEnd = ceil(xoff + line.cursorToX(pos + 1));
2148 QVERIFY(widthBegin <= texteditObject->width() / 2);
2149 QVERIFY(widthEnd >= texteditObject->width() / 2);
2151 const qreal x0 = texteditObject->positionToRectangle(pos).x();
2152 const qreal x1 = texteditObject->positionToRectangle(pos + 1).x();
2154 QString preeditText = texteditObject->text().mid(0, pos);
2155 texteditObject->setText(texteditObject->text().mid(pos));
2156 texteditObject->setCursorPosition(0);
2158 QInputMethodEvent inputEvent(preeditText, QList<QInputMethodEvent::Attribute>());
2159 QGuiApplication::sendEvent(texteditObject, &inputEvent);
2161 // Check all points within the preedit text return the same position.
2162 QCOMPARE(texteditObject->positionAt(0, y0), 0);
2163 QCOMPARE(texteditObject->positionAt(x0 / 2, y0), 0);
2164 QCOMPARE(texteditObject->positionAt(x0, y0), 0);
2166 // Verify positioning returns to normal after the preedit text.
2167 QCOMPARE(texteditObject->positionAt(x1, y0), 1);
2168 QCOMPARE(texteditObject->positionToRectangle(1).x(), x1);
2170 QVERIFY(texteditObject->positionAt(x0 / 2, y1) > 0);
2173 void tst_qquicktextedit::linkActivated()
2175 QQuickView window(testFileUrl("linkActivated.qml"));
2176 QVERIFY(window.rootObject() != 0);
2178 window.requestActivateWindow();
2179 QTest::qWaitForWindowActive(&window);
2181 QQuickTextEdit *texteditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
2182 QVERIFY(texteditObject != 0);
2184 QSignalSpy spy(texteditObject, SIGNAL(linkActivated(QString)));
2186 const QString link("http://example.com/");
2188 const QPointF linkPos = texteditObject->positionToRectangle(7).center();
2189 const QPointF textPos = texteditObject->positionToRectangle(2).center();
2191 QTest::mouseClick(&window, Qt::LeftButton, 0, linkPos.toPoint());
2192 QTRY_COMPARE(spy.count(), 1);
2193 QCOMPARE(spy.last()[0].toString(), link);
2195 QTest::mouseClick(&window, Qt::LeftButton, 0, textPos.toPoint());
2197 QCOMPARE(spy.count(), 1);
2199 texteditObject->setReadOnly(true);
2201 QTest::mouseClick(&window, Qt::LeftButton, 0, linkPos.toPoint());
2202 QTRY_COMPARE(spy.count(), 2);
2203 QCOMPARE(spy.last()[0].toString(), link);
2205 QTest::mouseClick(&window, Qt::LeftButton, 0, textPos.toPoint());
2207 QCOMPARE(spy.count(), 2);
2210 void tst_qquicktextedit::cursorDelegate_data()
2212 QTest::addColumn<QUrl>("source");
2213 QTest::newRow("out of line") << testFileUrl("cursorTest.qml");
2214 QTest::newRow("in line") << testFileUrl("cursorTestInline.qml");
2215 QTest::newRow("external") << testFileUrl("cursorTestExternal.qml");
2218 void tst_qquicktextedit::cursorDelegate()
2220 QFETCH(QUrl, source);
2221 QQuickView view(source);
2223 view.requestActivateWindow();
2224 QQuickTextEdit *textEditObject = view.rootObject()->findChild<QQuickTextEdit*>("textEditObject");
2225 QVERIFY(textEditObject != 0);
2226 // Delegate creation is deferred until focus in or cursor visibility is forced.
2227 QVERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
2228 QVERIFY(!textEditObject->isCursorVisible());
2229 //Test Delegate gets created
2230 textEditObject->setFocus(true);
2231 QVERIFY(textEditObject->isCursorVisible());
2232 QQuickItem* delegateObject = textEditObject->findChild<QQuickItem*>("cursorInstance");
2233 QVERIFY(delegateObject);
2234 QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
2235 //Test Delegate gets moved
2236 for (int i=0; i<= textEditObject->text().length(); i++) {
2237 textEditObject->setCursorPosition(i);
2238 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2239 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2242 // Test delegate gets moved on mouse press.
2243 textEditObject->setSelectByMouse(true);
2244 textEditObject->setCursorPosition(0);
2245 const QPoint point1 = textEditObject->positionToRectangle(5).center().toPoint();
2246 QTest::qWait(400); //ensure this isn't treated as a double-click
2247 QTest::mouseClick(&view, Qt::LeftButton, 0, point1);
2249 QTRY_VERIFY(textEditObject->cursorPosition() != 0);
2250 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2251 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2253 // Test delegate gets moved on mouse drag
2254 textEditObject->setCursorPosition(0);
2255 const QPoint point2 = textEditObject->positionToRectangle(10).center().toPoint();
2256 QTest::qWait(400); //ensure this isn't treated as a double-click
2257 QTest::mousePress(&view, Qt::LeftButton, 0, point1);
2258 QMouseEvent mv(QEvent::MouseMove, point2, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
2259 QGuiApplication::sendEvent(&view, &mv);
2260 QTest::mouseRelease(&view, Qt::LeftButton, 0, point2);
2262 QTRY_COMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2263 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2265 textEditObject->setReadOnly(true);
2266 textEditObject->setCursorPosition(0);
2267 QTest::qWait(400); //ensure this isn't treated as a double-click
2268 QTest::mouseClick(&view, Qt::LeftButton, 0, textEditObject->positionToRectangle(5).center().toPoint());
2270 QTRY_VERIFY(textEditObject->cursorPosition() != 0);
2271 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2272 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2274 textEditObject->setCursorPosition(0);
2275 QTest::qWait(400); //ensure this isn't treated as a double-click
2276 QTest::mouseClick(&view, Qt::LeftButton, 0, textEditObject->positionToRectangle(5).center().toPoint());
2278 QTRY_VERIFY(textEditObject->cursorPosition() != 0);
2279 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2280 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2282 textEditObject->setCursorPosition(0);
2283 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2284 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2286 textEditObject->setReadOnly(false);
2288 // Delegate moved when text is entered
2289 textEditObject->setText(QString());
2290 for (int i = 0; i < 20; ++i) {
2291 QTest::keyClick(&view, Qt::Key_A);
2292 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2293 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2296 // Delegate moved when text is entered by im.
2297 textEditObject->setText(QString());
2298 for (int i = 0; i < 20; ++i) {
2299 QInputMethodEvent event;
2300 event.setCommitString("a");
2301 QGuiApplication::sendEvent(textEditObject, &event);
2302 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2303 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2305 // Delegate moved when text is removed by im.
2306 for (int i = 19; i > 1; --i) {
2307 QInputMethodEvent event;
2308 event.setCommitString(QString(), -1, 1);
2309 QGuiApplication::sendEvent(textEditObject, &event);
2310 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2311 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2313 { // Delegate moved the text is changed in place by im.
2314 QInputMethodEvent event;
2315 event.setCommitString("i", -1, 1);
2316 QGuiApplication::sendEvent(textEditObject, &event);
2317 QCOMPARE(textEditObject->cursorRectangle().x(), delegateObject->x());
2318 QCOMPARE(textEditObject->cursorRectangle().y(), delegateObject->y());
2321 //Test Delegate gets deleted
2322 textEditObject->setCursorDelegate(0);
2323 QVERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
2326 void tst_qquicktextedit::remoteCursorDelegate()
2328 TestHTTPServer server(SERVER_PORT);
2329 server.serveDirectory(dataDirectory());
2333 QQmlComponent component(view.engine(), QUrl(SERVER_ADDR "/RemoteCursor.qml"));
2335 view.rootContext()->setContextProperty("contextDelegate", &component);
2336 view.setSource(testFileUrl("cursorTestRemote.qml"));
2338 view.requestActivateWindow();
2339 QQuickTextEdit *textEditObject = view.rootObject()->findChild<QQuickTextEdit*>("textEditObject");
2340 QVERIFY(textEditObject != 0);
2342 // Delegate is created on demand, and so won't be available immediately. Focus in or
2343 // setCursorVisible(true) will trigger creation.
2344 QTRY_VERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
2345 QVERIFY(!textEditObject->isCursorVisible());
2347 textEditObject->setFocus(true);
2348 QVERIFY(textEditObject->isCursorVisible());
2350 QCOMPARE(component.status(), QQmlComponent::Loading);
2351 QVERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
2353 // Wait for component to load.
2354 QTRY_COMPARE(component.status(), QQmlComponent::Ready);
2355 QVERIFY(textEditObject->findChild<QQuickItem*>("cursorInstance"));
2358 void tst_qquicktextedit::cursorVisible()
2360 QQuickTextEdit edit;
2361 edit.componentComplete();
2362 QSignalSpy spy(&edit, SIGNAL(cursorVisibleChanged(bool)));
2364 QQuickView view(testFileUrl("cursorVisible.qml"));
2366 view.requestActivateWindow();
2367 QTest::qWaitForWindowActive(&view);
2368 QCOMPARE(&view, qGuiApp->focusWindow());
2370 QCOMPARE(edit.isCursorVisible(), false);
2372 edit.setCursorVisible(true);
2373 QCOMPARE(edit.isCursorVisible(), true);
2374 QCOMPARE(spy.count(), 1);
2376 edit.setCursorVisible(false);
2377 QCOMPARE(edit.isCursorVisible(), false);
2378 QCOMPARE(spy.count(), 2);
2380 edit.setFocus(true);
2381 QCOMPARE(edit.isCursorVisible(), false);
2382 QCOMPARE(spy.count(), 2);
2384 edit.setParentItem(view.rootObject());
2385 QCOMPARE(edit.isCursorVisible(), true);
2386 QCOMPARE(spy.count(), 3);
2388 edit.setFocus(false);
2389 QCOMPARE(edit.isCursorVisible(), false);
2390 QCOMPARE(spy.count(), 4);
2392 edit.setFocus(true);
2393 QCOMPARE(edit.isCursorVisible(), true);
2394 QCOMPARE(spy.count(), 5);
2396 QWindow alternateView;
2397 alternateView.show();
2398 alternateView.requestActivateWindow();
2399 QTest::qWaitForWindowActive(&alternateView);
2401 QCOMPARE(edit.isCursorVisible(), false);
2402 QCOMPARE(spy.count(), 6);
2404 view.requestActivateWindow();
2405 QTest::qWaitForWindowActive(&view);
2406 QCOMPARE(edit.isCursorVisible(), true);
2407 QCOMPARE(spy.count(), 7);
2409 { // Cursor attribute with 0 length hides cursor.
2410 QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
2411 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
2412 QCoreApplication::sendEvent(&edit, &ev);
2414 QCOMPARE(edit.isCursorVisible(), false);
2415 QCOMPARE(spy.count(), 8);
2417 { // Cursor attribute with non zero length shows cursor.
2418 QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
2419 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 1, QVariant()));
2420 QCoreApplication::sendEvent(&edit, &ev);
2422 QCOMPARE(edit.isCursorVisible(), true);
2423 QCOMPARE(spy.count(), 9);
2426 { // If the cursor is hidden by the input method and the text is changed it should be visible again.
2427 QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
2428 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
2429 QCoreApplication::sendEvent(&edit, &ev);
2431 QCOMPARE(edit.isCursorVisible(), false);
2432 QCOMPARE(spy.count(), 10);
2434 edit.setText("something");
2435 QCOMPARE(edit.isCursorVisible(), true);
2436 QCOMPARE(spy.count(), 11);
2438 { // If the cursor is hidden by the input method and the cursor position is changed it should be visible again.
2439 QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
2440 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
2441 QCoreApplication::sendEvent(&edit, &ev);
2443 QCOMPARE(edit.isCursorVisible(), false);
2444 QCOMPARE(spy.count(), 12);
2446 edit.setCursorPosition(5);
2447 QCOMPARE(edit.isCursorVisible(), true);
2448 QCOMPARE(spy.count(), 13);
2451 void tst_qquicktextedit::delegateLoading_data()
2453 QTest::addColumn<QString>("qmlfile");
2454 QTest::addColumn<QString>("error");
2457 QTest::newRow("pass") << "cursorHttpTestPass.qml" << "";
2458 QTest::newRow("fail1") << "cursorHttpTestFail1.qml" << "http://localhost:42332/FailItem.qml: Remote host closed the connection ";
2459 QTest::newRow("fail2") << "cursorHttpTestFail2.qml" << "http://localhost:42332/ErrItem.qml:4:5: Fungus is not a type ";
2462 void tst_qquicktextedit::delegateLoading()
2464 QFETCH(QString, qmlfile);
2465 QFETCH(QString, error);
2467 TestHTTPServer server(SERVER_PORT);
2468 server.serveDirectory(testFile("httpfail"), TestHTTPServer::Disconnect);
2469 server.serveDirectory(testFile("httpslow"), TestHTTPServer::Delay);
2470 server.serveDirectory(testFile("http"));
2472 QQuickView view(QUrl(QLatin1String(SERVER_ADDR "/") + qmlfile));
2474 view.requestActivateWindow();
2476 if (!error.isEmpty()) {
2477 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
2478 QTRY_VERIFY(view.status()==QQuickView::Error);
2479 QTRY_VERIFY(!view.rootObject()); // there is fail item inside this test
2481 QTRY_VERIFY(view.rootObject());//Wait for loading to finish.
2482 QQuickTextEdit *textEditObject = view.rootObject()->findChild<QQuickTextEdit*>("textEditObject");
2483 // view.rootObject()->dumpObjectTree();
2484 QVERIFY(textEditObject != 0);
2485 textEditObject->setFocus(true);
2486 QQuickItem *delegate;
2487 delegate = view.rootObject()->findChild<QQuickItem*>("delegateOkay");
2489 delegate = view.rootObject()->findChild<QQuickItem*>("delegateSlow");
2496 //A test should be added here with a component which is ready but component.create() returns null
2497 //Not sure how to accomplish this with QQuickTextEdits cursor delegate
2498 //###This was only needed for code coverage, and could be a case of overzealous defensive programming
2499 //delegate = view.rootObject()->findChild<QQuickItem*>("delegateErrorB");
2500 //QVERIFY(!delegate);
2504 TextEdit element should only handle left/right keys until the cursor reaches
2505 the extent of the text, then they should ignore the keys.
2507 void tst_qquicktextedit::navigation()
2509 QQuickView window(testFileUrl("navigation.qml"));
2511 window.requestActivateWindow();
2513 QVERIFY(window.rootObject() != 0);
2515 QQuickTextEdit *input = qobject_cast<QQuickTextEdit *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
2517 QVERIFY(input != 0);
2518 QTRY_VERIFY(input->hasActiveFocus() == true);
2519 simulateKey(&window, Qt::Key_Left);
2520 QVERIFY(input->hasActiveFocus() == false);
2521 simulateKey(&window, Qt::Key_Right);
2522 QVERIFY(input->hasActiveFocus() == true);
2523 simulateKey(&window, Qt::Key_Right);
2524 QVERIFY(input->hasActiveFocus() == true);
2525 simulateKey(&window, Qt::Key_Right);
2526 QVERIFY(input->hasActiveFocus() == false);
2527 simulateKey(&window, Qt::Key_Left);
2528 QVERIFY(input->hasActiveFocus() == true);
2530 // Test left and right navigation works if the TextEdit is empty (QTBUG-25447).
2531 input->setText(QString());
2532 QCOMPARE(input->cursorPosition(), 0);
2533 simulateKey(&window, Qt::Key_Right);
2534 QCOMPARE(input->hasActiveFocus(), false);
2535 simulateKey(&window, Qt::Key_Left);
2536 QCOMPARE(input->hasActiveFocus(), true);
2537 simulateKey(&window, Qt::Key_Left);
2538 QCOMPARE(input->hasActiveFocus(), false);
2541 #ifndef QT_NO_CLIPBOARD
2542 void tst_qquicktextedit::copyAndPaste()
2544 if (!PlatformQuirks::isClipboardAvailable())
2545 QSKIP("This machine doesn't support the clipboard");
2547 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"Hello world!\" }";
2548 QQmlComponent textEditComponent(&engine);
2549 textEditComponent.setData(componentStr.toLatin1(), QUrl());
2550 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
2551 QVERIFY(textEdit != 0);
2554 QCOMPARE(textEdit->text().length(), 12);
2555 textEdit->select(0, textEdit->text().length());;
2557 QCOMPARE(textEdit->selectedText(), QString("Hello world!"));
2558 QCOMPARE(textEdit->selectedText().length(), 12);
2559 textEdit->setCursorPosition(0);
2560 QVERIFY(textEdit->canPaste());
2562 QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
2563 QCOMPARE(textEdit->text().length(), 24);
2566 QVERIFY(textEdit->canPaste());
2567 textEdit->setReadOnly(true);
2568 QVERIFY(!textEdit->canPaste());
2570 QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
2571 QCOMPARE(textEdit->text().length(), 24);
2572 textEdit->setReadOnly(false);
2573 QVERIFY(textEdit->canPaste());
2576 // test that document and internal text attribute are in sync
2577 QQuickItemPrivate* pri = QQuickItemPrivate::get(textEdit);
2578 QQuickTextEditPrivate *editPrivate = static_cast<QQuickTextEditPrivate*>(pri);
2579 QCOMPARE(textEdit->text(), editPrivate->text);
2581 // cut: no selection
2583 QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
2586 textEdit->setCursorPosition(0);
2587 textEdit->selectWord();
2588 QCOMPARE(textEdit->selectedText(), QString("Hello"));
2591 textEdit->setReadOnly(true);
2593 QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
2594 textEdit->setReadOnly(false);
2596 // select all and cut
2597 textEdit->selectAll();
2599 QCOMPARE(textEdit->text().length(), 0);
2601 QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
2602 QCOMPARE(textEdit->text().length(), 24);
2605 textEdit->setCursorPosition(0);
2606 textEdit->selectWord();
2608 // copy: no selection, previous copy retained;
2609 textEdit->setCursorPosition(0);
2610 QCOMPARE(textEdit->selectedText(), QString());
2612 textEdit->setText(QString());
2614 QCOMPARE(textEdit->text(), QString("Hello"));
2618 #ifndef QT_NO_CLIPBOARD
2619 void tst_qquicktextedit::canPaste()
2621 QGuiApplication::clipboard()->setText("Some text");
2623 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"Hello world!\" }";
2624 QQmlComponent textEditComponent(&engine);
2625 textEditComponent.setData(componentStr.toLatin1(), QUrl());
2626 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
2627 QVERIFY(textEdit != 0);
2629 // check initial value - QTBUG-17765
2630 QTextDocument document;
2631 QQuickTextControl tc(&document);
2632 QCOMPARE(textEdit->canPaste(), tc.canPaste());
2636 #ifndef QT_NO_CLIPBOARD
2637 void tst_qquicktextedit::canPasteEmpty()
2639 QGuiApplication::clipboard()->clear();
2641 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"Hello world!\" }";
2642 QQmlComponent textEditComponent(&engine);
2643 textEditComponent.setData(componentStr.toLatin1(), QUrl());
2644 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
2645 QVERIFY(textEdit != 0);
2647 // check initial value - QTBUG-17765
2648 QTextDocument document;
2649 QQuickTextControl tc(&document);
2650 QCOMPARE(textEdit->canPaste(), tc.canPaste());
2654 #ifndef QT_NO_CLIPBOARD
2655 void tst_qquicktextedit::middleClickPaste()
2657 if (!PlatformQuirks::isClipboardAvailable())
2658 QSKIP("This machine doesn't support the clipboard");
2660 QQuickView window(testFileUrl("mouseselection_true.qml"));
2663 window.requestActivateWindow();
2664 QTest::qWaitForWindowActive(&window);
2666 QVERIFY(window.rootObject() != 0);
2667 QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
2668 QVERIFY(textEditObject != 0);
2670 textEditObject->setFocus(true);
2672 QString originalText = textEditObject->text();
2673 QString selectedText = "234567";
2675 // press-and-drag-and-release from x1 to x2
2676 const QPoint p1 = textEditObject->positionToRectangle(2).center().toPoint();
2677 const QPoint p2 = textEditObject->positionToRectangle(8).center().toPoint();
2678 const QPoint p3 = textEditObject->positionToRectangle(1).center().toPoint();
2679 QTest::mousePress(&window, Qt::LeftButton, Qt::NoModifier, p1);
2680 QTest::mouseMove(&window, p2);
2681 QTest::mouseRelease(&window, Qt::LeftButton, Qt::NoModifier, p2);
2682 QTRY_COMPARE(textEditObject->selectedText(), selectedText);
2684 // Middle click pastes the selected text, assuming the platform supports it.
2685 QTest::mouseClick(&window, Qt::MiddleButton, Qt::NoModifier, p3);
2687 // ### This is to prevent double click detection from carrying over to the next test.
2688 QTest::qWait(QGuiApplication::styleHints()->mouseDoubleClickInterval() + 10);
2690 if (QGuiApplication::clipboard()->supportsSelection())
2691 QCOMPARE(textEditObject->text().mid(1, selectedText.length()), selectedText);
2693 QCOMPARE(textEditObject->text(), originalText);
2697 void tst_qquicktextedit::readOnly()
2699 QQuickView window(testFileUrl("readOnly.qml"));
2701 window.requestActivateWindow();
2703 QVERIFY(window.rootObject() != 0);
2705 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
2708 QTRY_VERIFY(edit->hasActiveFocus() == true);
2709 QVERIFY(edit->isReadOnly() == true);
2710 QString initial = edit->text();
2711 for (int k=Qt::Key_0; k<=Qt::Key_Z; k++)
2712 simulateKey(&window, k);
2713 simulateKey(&window, Qt::Key_Return);
2714 simulateKey(&window, Qt::Key_Space);
2715 simulateKey(&window, Qt::Key_Escape);
2716 QCOMPARE(edit->text(), initial);
2718 edit->setCursorPosition(3);
2719 edit->setReadOnly(false);
2720 QCOMPARE(edit->isReadOnly(), false);
2721 QCOMPARE(edit->cursorPosition(), edit->text().length());
2724 void tst_qquicktextedit::simulateKey(QWindow *view, int key, Qt::KeyboardModifiers modifiers)
2726 QKeyEvent press(QKeyEvent::KeyPress, key, modifiers);
2727 QKeyEvent release(QKeyEvent::KeyRelease, key, modifiers);
2729 QGuiApplication::sendEvent(view, &press);
2730 QGuiApplication::sendEvent(view, &release);
2733 void tst_qquicktextedit::textInput()
2735 QQuickView view(testFileUrl("inputMethodEvent.qml"));
2737 view.requestActivateWindow();
2738 QTest::qWaitForWindowActive(&view);
2739 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
2741 QVERIFY(edit->hasActiveFocus() == true);
2743 // test that input method event is committed and change signal is emitted
2744 QSignalSpy spy(edit, SIGNAL(textChanged()));
2745 QInputMethodEvent event;
2746 event.setCommitString( "Hello world!", 0, 0);
2747 QGuiApplication::sendEvent(edit, &event);
2748 QCOMPARE(edit->text(), QString("Hello world!"));
2749 QCOMPARE(spy.count(), 1);
2752 // test that document and internal text attribute are in sync
2753 QQuickTextEditPrivate *editPrivate = static_cast<QQuickTextEditPrivate*>(QQuickItemPrivate::get(edit));
2754 QCOMPARE(editPrivate->text, QString("Hello world!"));
2756 QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
2757 QGuiApplication::sendEvent(edit, &queryEvent);
2758 QCOMPARE(queryEvent.value(Qt::ImEnabled).toBool(), true);
2760 edit->setReadOnly(true);
2761 QGuiApplication::sendEvent(edit, &queryEvent);
2762 QCOMPARE(queryEvent.value(Qt::ImEnabled).toBool(), false);
2765 void tst_qquicktextedit::inputMethodUpdate()
2767 PlatformInputContext platformInputContext;
2768 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
2769 inputMethodPrivate->testContext = &platformInputContext;
2771 QQuickView view(testFileUrl("inputMethodEvent.qml"));
2773 view.requestActivateWindow();
2774 QTest::qWaitForWindowActive(&view);
2775 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
2777 QVERIFY(edit->hasActiveFocus() == true);
2779 // text change even without cursor position change needs to trigger update
2780 edit->setText("test");
2781 platformInputContext.clear();
2782 edit->setText("xxxx");
2783 QVERIFY(platformInputContext.m_updateCallCount > 0);
2785 // input method event replacing text
2786 platformInputContext.clear();
2788 QInputMethodEvent inputMethodEvent;
2789 inputMethodEvent.setCommitString("y", -1, 1);
2790 QGuiApplication::sendEvent(edit, &inputMethodEvent);
2792 QVERIFY(platformInputContext.m_updateCallCount > 0);
2794 // input method changing selection
2795 platformInputContext.clear();
2797 QList<QInputMethodEvent::Attribute> attributes;
2798 attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 0, 2, QVariant());
2799 QInputMethodEvent inputMethodEvent("", attributes);
2800 QGuiApplication::sendEvent(edit, &inputMethodEvent);
2802 QVERIFY(edit->selectionStart() != edit->selectionEnd());
2803 QVERIFY(platformInputContext.m_updateCallCount > 0);
2805 // programmatical selections trigger update
2806 platformInputContext.clear();
2808 QCOMPARE(platformInputContext.m_updateCallCount, 1);
2811 platformInputContext.clear();
2812 QFont font = edit->font();
2813 font.setBold(!font.bold());
2814 edit->setFont(font);
2815 QVERIFY(platformInputContext.m_updateCallCount > 0);
2818 platformInputContext.clear();
2820 QInputMethodEvent inputMethodEvent;
2821 inputMethodEvent.setCommitString("y");
2822 QGuiApplication::sendEvent(edit, &inputMethodEvent);
2824 QVERIFY(platformInputContext.m_updateCallCount > 0);
2826 // changing cursor position
2827 platformInputContext.clear();
2828 edit->setCursorPosition(0);
2829 QVERIFY(platformInputContext.m_updateCallCount > 0);
2831 // continuing with selection
2832 platformInputContext.clear();
2833 edit->moveCursorSelection(1);
2834 QVERIFY(platformInputContext.m_updateCallCount > 0);
2836 // read only disabled input method
2837 platformInputContext.clear();
2838 edit->setReadOnly(true);
2839 QVERIFY(platformInputContext.m_updateCallCount > 0);
2840 edit->setReadOnly(false);
2842 // no updates while no focus
2843 edit->setFocus(false);
2844 platformInputContext.clear();
2845 edit->setText("Foo");
2846 QCOMPARE(platformInputContext.m_updateCallCount, 0);
2847 edit->setCursorPosition(1);
2848 QCOMPARE(platformInputContext.m_updateCallCount, 0);
2850 QCOMPARE(platformInputContext.m_updateCallCount, 0);
2851 edit->setReadOnly(true);
2852 QCOMPARE(platformInputContext.m_updateCallCount, 0);
2855 void tst_qquicktextedit::openInputPanel()
2857 PlatformInputContext platformInputContext;
2858 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
2859 inputMethodPrivate->testContext = &platformInputContext;
2861 QQuickView view(testFileUrl("openInputPanel.qml"));
2863 view.requestActivateWindow();
2864 QTest::qWaitForWindowActive(&view);
2866 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
2869 // check default values
2870 QVERIFY(edit->focusOnPress());
2871 QVERIFY(!edit->hasActiveFocus());
2872 QVERIFY(qApp->focusObject() != edit);
2874 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2876 // input panel should open on focus
2877 QPoint centerPoint(view.width()/2, view.height()/2);
2878 Qt::KeyboardModifiers noModifiers = 0;
2879 QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
2880 QGuiApplication::processEvents();
2881 QVERIFY(edit->hasActiveFocus());
2882 QCOMPARE(qApp->focusObject(), edit);
2883 QCOMPARE(qApp->inputMethod()->isVisible(), true);
2884 QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
2886 // input panel should be re-opened when pressing already focused TextEdit
2887 qApp->inputMethod()->hide();
2888 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2889 QVERIFY(edit->hasActiveFocus());
2890 QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
2891 QGuiApplication::processEvents();
2892 QCOMPARE(qApp->inputMethod()->isVisible(), true);
2893 QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
2895 // input panel should stay visible if focus is lost to another text editor
2896 QSignalSpy inputPanelVisibilitySpy(qApp->inputMethod(), SIGNAL(visibleChanged()));
2897 QQuickTextEdit anotherEdit;
2898 anotherEdit.setParentItem(view.rootObject());
2899 anotherEdit.setFocus(true);
2900 QCOMPARE(qApp->inputMethod()->isVisible(), true);
2901 QCOMPARE(qApp->focusObject(), qobject_cast<QObject*>(&anotherEdit));
2902 QCOMPARE(inputPanelVisibilitySpy.count(), 0);
2904 anotherEdit.setFocus(false);
2905 QVERIFY(qApp->focusObject() != &anotherEdit);
2906 QCOMPARE(view.activeFocusItem(), view.rootItem());
2907 anotherEdit.setFocus(true);
2909 qApp->inputMethod()->hide();
2911 // input panel should not be opened if TextEdit is read only
2912 edit->setReadOnly(true);
2913 edit->setFocus(true);
2914 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2915 QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
2916 QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
2917 QGuiApplication::processEvents();
2918 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2920 // input panel should not be opened if focusOnPress is set to false
2921 edit->setFocusOnPress(false);
2922 edit->setFocus(false);
2923 edit->setFocus(true);
2924 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2925 QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
2926 QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
2927 QCOMPARE(qApp->inputMethod()->isVisible(), false);
2929 inputMethodPrivate->testContext = 0;
2932 void tst_qquicktextedit::geometrySignals()
2934 QQmlComponent component(&engine, testFileUrl("geometrySignals.qml"));
2935 QObject *o = component.create();
2937 QCOMPARE(o->property("bindingWidth").toInt(), 400);
2938 QCOMPARE(o->property("bindingHeight").toInt(), 500);
2942 #ifndef QT_NO_CLIPBOARD
2943 void tst_qquicktextedit::pastingRichText_QTBUG_14003()
2945 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.PlainText }";
2946 QQmlComponent component(&engine);
2947 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
2948 QQuickTextEdit *obj = qobject_cast<QQuickTextEdit*>(component.create());
2950 QTRY_VERIFY(obj != 0);
2951 QTRY_VERIFY(obj->textFormat() == QQuickTextEdit::PlainText);
2953 QMimeData *mData = new QMimeData;
2954 mData->setHtml("<font color=\"red\">Hello</font>");
2955 QGuiApplication::clipboard()->setMimeData(mData);
2958 QTRY_VERIFY(obj->text() == "");
2959 QTRY_VERIFY(obj->textFormat() == QQuickTextEdit::PlainText);
2963 void tst_qquicktextedit::implicitSize_data()
2965 QTest::addColumn<QString>("text");
2966 QTest::addColumn<QString>("wrap");
2967 QTest::addColumn<QString>("format");
2968 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "TextEdit.NoWrap" << "TextEdit.PlainText";
2969 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "TextEdit.NoWrap" << "TextEdit.RichText";
2970 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "TextEdit.Wrap" << "TextEdit.PlainText";
2971 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "TextEdit.Wrap" << "TextEdit.RichText";
2974 void tst_qquicktextedit::implicitSize()
2976 QFETCH(QString, text);
2977 QFETCH(QString, wrap);
2978 QFETCH(QString, format);
2979 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + "; textFormat: " + format + " }";
2980 QQmlComponent textComponent(&engine);
2981 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
2982 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit*>(textComponent.create());
2984 QVERIFY(textObject->width() < textObject->implicitWidth());
2985 QVERIFY(textObject->height() == textObject->implicitHeight());
2987 textObject->resetWidth();
2988 QVERIFY(textObject->width() == textObject->implicitWidth());
2989 QVERIFY(textObject->height() == textObject->implicitHeight());
2992 void tst_qquicktextedit::contentSize()
2994 QString componentStr = "import QtQuick 2.0\nTextEdit { width: 75; height: 16; font.pixelSize: 10 }";
2995 QQmlComponent textComponent(&engine);
2996 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
2997 QScopedPointer<QObject> object(textComponent.create());
2998 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit *>(object.data());
3000 QSignalSpy spy(textObject, SIGNAL(contentSizeChanged()));
3002 textObject->setText("The quick red fox jumped over the lazy brown dog");
3004 QVERIFY(textObject->contentWidth() > textObject->width());
3005 QVERIFY(textObject->contentHeight() < textObject->height());
3006 QCOMPARE(spy.count(), 1);
3008 textObject->setWrapMode(QQuickTextEdit::WordWrap);
3009 QVERIFY(textObject->contentWidth() <= textObject->width());
3010 QVERIFY(textObject->contentHeight() > textObject->height());
3011 QCOMPARE(spy.count(), 2);
3013 textObject->setText("The quickredfoxjumpedoverthe lazy brown dog");
3015 QVERIFY(textObject->contentWidth() > textObject->width());
3016 QVERIFY(textObject->contentHeight() > textObject->height());
3017 QCOMPARE(spy.count(), 3);
3020 void tst_qquicktextedit::implicitSizeBinding_data()
3022 implicitSize_data();
3025 void tst_qquicktextedit::implicitSizeBinding()
3027 QFETCH(QString, text);
3028 QFETCH(QString, wrap);
3029 QFETCH(QString, format);
3030 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\"; width: implicitWidth; height: implicitHeight; wrapMode: " + wrap + "; textFormat: " + format + " }";
3031 QQmlComponent textComponent(&engine);
3032 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
3033 QScopedPointer<QObject> object(textComponent.create());
3034 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit *>(object.data());
3036 QCOMPARE(textObject->width(), textObject->implicitWidth());
3037 QCOMPARE(textObject->height(), textObject->implicitHeight());
3039 textObject->resetWidth();
3040 QCOMPARE(textObject->width(), textObject->implicitWidth());
3041 QCOMPARE(textObject->height(), textObject->implicitHeight());
3043 textObject->resetHeight();
3044 QCOMPARE(textObject->width(), textObject->implicitWidth());
3045 QCOMPARE(textObject->height(), textObject->implicitHeight());
3048 void tst_qquicktextedit::clipRect()
3050 QQmlComponent component(&engine);
3051 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
3052 QScopedPointer<QObject> object(component.create());
3053 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
3056 QCOMPARE(edit->clipRect().x(), qreal(0));
3057 QCOMPARE(edit->clipRect().y(), qreal(0));
3059 QCOMPARE(edit->clipRect().width(), edit->width() + edit->cursorRectangle().width());
3060 QCOMPARE(edit->clipRect().height(), edit->height());
3062 edit->setText("Hello World");
3063 QCOMPARE(edit->clipRect().x(), qreal(0));
3064 QCOMPARE(edit->clipRect().y(), qreal(0));
3065 // XXX: TextEdit allows an extra 3 pixels boundary for the cursor beyond it's width for non
3066 // empty text. TextInput doesn't.
3067 QCOMPARE(edit->clipRect().width(), edit->width() + edit->cursorRectangle().width() + 3);
3068 QCOMPARE(edit->clipRect().height(), edit->height());
3070 // clip rect shouldn't exceed the size of the item, expect for the cursor width;
3071 edit->setWidth(edit->width() / 2);
3072 QCOMPARE(edit->clipRect().x(), qreal(0));
3073 QCOMPARE(edit->clipRect().y(), qreal(0));
3074 QCOMPARE(edit->clipRect().width(), edit->width() + edit->cursorRectangle().width() + 3);
3075 QCOMPARE(edit->clipRect().height(), edit->height());
3077 edit->setHeight(edit->height() * 2);
3078 QCOMPARE(edit->clipRect().x(), qreal(0));
3079 QCOMPARE(edit->clipRect().y(), qreal(0));
3080 QCOMPARE(edit->clipRect().width(), edit->width() + edit->cursorRectangle().width() + 3);
3081 QCOMPARE(edit->clipRect().height(), edit->height());
3083 QQmlComponent cursorComponent(&engine);
3084 cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
3086 edit->setCursorDelegate(&cursorComponent);
3087 edit->setCursorVisible(true);
3089 // If a cursor delegate is used it's size should determine the excess width.
3090 QCOMPARE(edit->clipRect().x(), qreal(0));
3091 QCOMPARE(edit->clipRect().y(), qreal(0));
3092 QCOMPARE(edit->clipRect().width(), edit->width() + 8 + 3);
3093 QCOMPARE(edit->clipRect().height(), edit->height());
3095 // Alignment and wrapping don't affect the clip rect.
3096 edit->setHAlign(QQuickTextEdit::AlignRight);
3097 QCOMPARE(edit->clipRect().x(), qreal(0));
3098 QCOMPARE(edit->clipRect().y(), qreal(0));
3099 QCOMPARE(edit->clipRect().width(), edit->width() + 8 + 3);
3100 QCOMPARE(edit->clipRect().height(), edit->height());
3102 edit->setWrapMode(QQuickTextEdit::Wrap);
3103 QCOMPARE(edit->clipRect().x(), qreal(0));
3104 QCOMPARE(edit->clipRect().y(), qreal(0));
3105 QCOMPARE(edit->clipRect().width(), edit->width() + 8 + 3);
3106 QCOMPARE(edit->clipRect().height(), edit->height());
3108 edit->setVAlign(QQuickTextEdit::AlignBottom);
3109 QCOMPARE(edit->clipRect().x(), qreal(0));
3110 QCOMPARE(edit->clipRect().y(), qreal(0));
3111 QCOMPARE(edit->clipRect().width(), edit->width() + 8 + 3);
3112 QCOMPARE(edit->clipRect().height(), edit->height());
3115 void tst_qquicktextedit::boundingRect()
3117 QQmlComponent component(&engine);
3118 component.setData("import QtQuick 2.0\n TextEdit {}", QUrl());
3119 QScopedPointer<QObject> object(component.create());
3120 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(object.data());
3124 layout.setFont(edit->font());
3126 if (!qmlDisableDistanceField()) {
3128 option.setUseDesignMetrics(true);
3129 layout.setTextOption(option);
3131 layout.beginLayout();
3132 QTextLine line = layout.createLine();
3135 QCOMPARE(edit->boundingRect().x(), qreal(0));
3136 QCOMPARE(edit->boundingRect().y(), qreal(0));
3137 QCOMPARE(edit->boundingRect().width(), edit->cursorRectangle().width());
3138 QCOMPARE(edit->boundingRect().height(), line.height());
3140 edit->setText("Hello World");
3142 layout.setText(edit->text());
3143 layout.beginLayout();
3144 line = layout.createLine();
3147 QCOMPARE(edit->boundingRect().x(), qreal(0));
3148 QCOMPARE(edit->boundingRect().y(), qreal(0));
3149 QCOMPARE(edit->boundingRect().width(), line.naturalTextWidth() + edit->cursorRectangle().width() + 3);
3150 QCOMPARE(edit->boundingRect().height(), line.height());
3152 // the size of the bounding rect shouldn't be bounded by the size of item.
3153 edit->setWidth(edit->width() / 2);
3154 QCOMPARE(edit->boundingRect().x(), qreal(0));
3155 QCOMPARE(edit->boundingRect().y(), qreal(0));
3156 QCOMPARE(edit->boundingRect().width(), line.naturalTextWidth() + edit->cursorRectangle().width() + 3);
3157 QCOMPARE(edit->boundingRect().height(), line.height());
3159 edit->setHeight(edit->height() * 2);
3160 QCOMPARE(edit->boundingRect().x(), qreal(0));
3161 QCOMPARE(edit->boundingRect().y(), qreal(0));
3162 QCOMPARE(edit->boundingRect().width(), line.naturalTextWidth() + edit->cursorRectangle().width() + 3);
3163 QCOMPARE(edit->boundingRect().height(), line.height());
3165 QQmlComponent cursorComponent(&engine);
3166 cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
3168 edit->setCursorDelegate(&cursorComponent);
3169 edit->setCursorVisible(true);
3171 // Don't include the size of a cursor delegate as it has its own bounding rect.
3172 QCOMPARE(edit->boundingRect().x(), qreal(0));
3173 QCOMPARE(edit->boundingRect().y(), qreal(0));
3174 QCOMPARE(edit->boundingRect().width(), line.naturalTextWidth());
3175 QCOMPARE(edit->boundingRect().height(), line.height());
3177 edit->setHAlign(QQuickTextEdit::AlignRight);
3178 QCOMPARE(edit->boundingRect().x(), edit->width() - line.naturalTextWidth());
3179 QCOMPARE(edit->boundingRect().y(), qreal(0));
3180 QCOMPARE(edit->boundingRect().width(), line.naturalTextWidth());
3181 QCOMPARE(edit->boundingRect().height(), line.height());
3183 edit->setWrapMode(QQuickTextEdit::Wrap);
3184 QCOMPARE(edit->boundingRect().right(), edit->width());
3185 QCOMPARE(edit->boundingRect().y(), qreal(0));
3186 QVERIFY(edit->boundingRect().width() < line.naturalTextWidth());
3187 QVERIFY(edit->boundingRect().height() > line.height());
3189 edit->setVAlign(QQuickTextEdit::AlignBottom);
3190 QCOMPARE(edit->boundingRect().right(), edit->width());
3191 QCOMPARE(edit->boundingRect().bottom(), edit->height());
3192 QVERIFY(edit->boundingRect().width() < line.naturalTextWidth());
3193 QVERIFY(edit->boundingRect().height() > line.height());
3196 void tst_qquicktextedit::preeditCursorRectangle()
3198 QString preeditText = "super";
3200 QQuickView view(testFileUrl("inputMethodEvent.qml"));
3202 view.requestActivateWindow();
3203 QTest::qWaitForWindowActive(&view);
3205 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
3208 QQuickItem *cursor = edit->findChild<QQuickItem *>("cursor");
3211 QSignalSpy editSpy(edit, SIGNAL(cursorRectangleChanged()));
3212 QSignalSpy panelSpy(qGuiApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
3216 QCOMPARE(QGuiApplication::focusObject(), static_cast<QObject *>(edit));
3217 QInputMethodQueryEvent query(Qt::ImCursorRectangle);
3218 QCoreApplication::sendEvent(edit, &query);
3219 QRectF previousRect = query.value(Qt::ImCursorRectangle).toRectF();
3221 // Verify that the micro focus rect is positioned the same for position 0 as
3222 // it would be if there was no preedit text.
3223 QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>()
3224 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, preeditText.length(), QVariant()));
3225 QCoreApplication::sendEvent(edit, &imEvent);
3226 QCoreApplication::sendEvent(edit, &query);
3227 currentRect = query.value(Qt::ImCursorRectangle).toRectF();
3228 QCOMPARE(edit->cursorRectangle(), currentRect);
3229 QCOMPARE(cursor->pos(), currentRect.topLeft());
3230 QCOMPARE(currentRect, previousRect);
3232 // Verify that the micro focus rect moves to the left as the cursor position
3236 for (int i = 1; i <= 5; ++i) {
3237 QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>()
3238 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, i, preeditText.length(), QVariant()));
3239 QCoreApplication::sendEvent(edit, &imEvent);
3240 QCoreApplication::sendEvent(edit, &query);
3241 currentRect = query.value(Qt::ImCursorRectangle).toRectF();
3242 QCOMPARE(edit->cursorRectangle(), currentRect);
3243 QCOMPARE(cursor->pos(), currentRect.topLeft());
3244 QVERIFY(previousRect.left() < currentRect.left());
3245 QCOMPARE(editSpy.count(), 1); editSpy.clear();
3246 QCOMPARE(panelSpy.count(), 1); panelSpy.clear();
3247 previousRect = currentRect;
3250 // Verify that if the cursor rectangle is updated if the pre-edit text changes
3251 // but the (non-zero) cursor position is the same.
3254 { QInputMethodEvent imEvent("wwwww", QList<QInputMethodEvent::Attribute>()
3255 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 5, 1, QVariant()));
3256 QCoreApplication::sendEvent(edit, &imEvent); }
3257 QCoreApplication::sendEvent(edit, &query);
3258 currentRect = query.value(Qt::ImCursorRectangle).toRectF();
3259 QCOMPARE(edit->cursorRectangle(), currentRect);
3260 QCOMPARE(cursor->pos(), currentRect.topLeft());
3261 QCOMPARE(editSpy.count(), 1);
3262 QCOMPARE(panelSpy.count(), 1);
3264 // Verify that if there is no preedit cursor then the micro focus rect is the
3265 // same as it would be if it were positioned at the end of the preedit text.
3268 { QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
3269 QCoreApplication::sendEvent(edit, &imEvent); }
3270 QCoreApplication::sendEvent(edit, &query);
3271 currentRect = query.value(Qt::ImCursorRectangle).toRectF();
3272 QCOMPARE(edit->cursorRectangle(), currentRect);
3273 QCOMPARE(cursor->pos(), currentRect.topLeft());
3274 QCOMPARE(currentRect, previousRect);
3275 QCOMPARE(editSpy.count(), 1);
3276 QCOMPARE(panelSpy.count(), 1);
3279 void tst_qquicktextedit::inputMethodComposing()
3281 QString text = "supercalifragisiticexpialidocious!";
3283 QQuickView view(testFileUrl("inputContext.qml"));
3285 view.requestActivateWindow();
3286 QTest::qWaitForWindowActive(&view);
3288 QQuickTextEdit *edit = qobject_cast<QQuickTextEdit *>(view.rootObject());
3290 QCOMPARE(QGuiApplication::focusObject(), static_cast<QObject *>(edit));
3292 QSignalSpy spy(edit, SIGNAL(inputMethodComposingChanged()));
3293 edit->setCursorPosition(12);
3295 QCOMPARE(edit->isInputMethodComposing(), false);
3298 QInputMethodEvent event(text.mid(3), QList<QInputMethodEvent::Attribute>());
3299 QGuiApplication::sendEvent(edit, &event);
3302 QCOMPARE(edit->isInputMethodComposing(), true);
3303 QCOMPARE(spy.count(), 1);
3306 QInputMethodEvent event(text.mid(12), QList<QInputMethodEvent::Attribute>());
3307 QGuiApplication::sendEvent(edit, &event);
3309 QCOMPARE(spy.count(), 1);
3312 QInputMethodEvent event;
3313 QGuiApplication::sendEvent(edit, &event);
3315 QCOMPARE(edit->isInputMethodComposing(), false);
3316 QCOMPARE(spy.count(), 2);
3318 // Changing the text while not composing doesn't alter the composing state.
3319 edit->setText(text.mid(0, 16));
3320 QCOMPARE(edit->isInputMethodComposing(), false);
3321 QCOMPARE(spy.count(), 2);
3324 QInputMethodEvent event(text.mid(16), QList<QInputMethodEvent::Attribute>());
3325 QGuiApplication::sendEvent(edit, &event);
3327 QCOMPARE(edit->isInputMethodComposing(), true);
3328 QCOMPARE(spy.count(), 3);
3330 // Changing the text while composing cancels composition.
3331 edit->setText(text.mid(0, 12));
3332 QCOMPARE(edit->isInputMethodComposing(), false);
3333 QCOMPARE(spy.count(), 4);
3335 { // Preedit cursor positioned outside (empty) preedit; composing.
3336 QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
3337 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, -2, 1, QVariant()));
3338 QGuiApplication::sendEvent(edit, &event);
3340 QCOMPARE(edit->isInputMethodComposing(), true);
3341 QCOMPARE(spy.count(), 5);
3343 { // Cursor hidden; composing
3344 QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
3345 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
3346 QGuiApplication::sendEvent(edit, &event);
3348 QCOMPARE(edit->isInputMethodComposing(), true);
3349 QCOMPARE(spy.count(), 5);
3351 { // Default cursor attributes; composing.
3352 QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
3353 << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 1, QVariant()));
3354 QGuiApplication::sendEvent(edit, &event);
3356 QCOMPARE(edit->isInputMethodComposing(), true);
3357 QCOMPARE(spy.count(), 5);
3359 { // Selections are persisted: not composing
3360 QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
3361 << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 2, 4, QVariant()));
3362 QGuiApplication::sendEvent(edit, &event);
3364 QCOMPARE(edit->isInputMethodComposing(), false);
3365 QCOMPARE(spy.count(), 6);
3367 edit->setCursorPosition(0);
3369 { // Formatting applied; composing.
3370 QTextCharFormat format;
3371 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
3372 QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
3373 << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 2, 4, format));
3374 QGuiApplication::sendEvent(edit, &event);
3376 QCOMPARE(edit->isInputMethodComposing(), true);
3377 QCOMPARE(spy.count(), 7);
3380 QInputMethodEvent event;
3381 QGuiApplication::sendEvent(edit, &event);
3383 QCOMPARE(edit->isInputMethodComposing(), false);
3384 QCOMPARE(spy.count(), 8);
3387 void tst_qquicktextedit::cursorRectangleSize()
3389 QQuickView *window = new QQuickView(testFileUrl("positionAt.qml"));
3390 QVERIFY(window->rootObject() != 0);
3391 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit *>(window->rootObject());
3393 // make sure cursor rectangle is not at (0,0)
3396 textEdit->setCursorPosition(3);
3397 QVERIFY(textEdit != 0);
3398 textEdit->setFocus(true);
3400 window->requestActivateWindow();
3401 QTest::qWaitForWindowActive(window);
3403 QInputMethodQueryEvent event(Qt::ImCursorRectangle);
3404 qApp->sendEvent(textEdit, &event);
3405 QRectF cursorRectFromQuery = event.value(Qt::ImCursorRectangle).toRectF();
3407 QRectF cursorRectFromItem = textEdit->cursorRectangle();
3408 QRectF cursorRectFromPositionToRectangle = textEdit->positionToRectangle(textEdit->cursorPosition());
3410 // item and input query cursor rectangles match
3411 QCOMPARE(cursorRectFromItem, cursorRectFromQuery);
3413 // item cursor rectangle and positionToRectangle calculations match
3414 QCOMPARE(cursorRectFromItem, cursorRectFromPositionToRectangle);
3416 // item-window transform and input item transform match
3417 QCOMPARE(QQuickItemPrivate::get(textEdit)->itemToWindowTransform(), qApp->inputMethod()->inputItemTransform());
3419 // input panel cursorRectangle property and tranformed item cursor rectangle match
3420 QRectF sceneCursorRect = QQuickItemPrivate::get(textEdit)->itemToWindowTransform().mapRect(cursorRectFromItem);
3421 QCOMPARE(sceneCursorRect, qApp->inputMethod()->cursorRectangle());
3426 void tst_qquicktextedit::getText_data()
3428 QTest::addColumn<QString>("text");
3429 QTest::addColumn<int>("start");
3430 QTest::addColumn<int>("end");
3431 QTest::addColumn<QString>("expectedText");
3433 const QString richBoldText = QStringLiteral("This is some <b>bold</b> text");
3434 const QString plainBoldText = QStringLiteral("This is some bold text");
3435 const QString richBoldTextLB = QStringLiteral("This is some<br/><b>bold</b> text");
3436 const QString plainBoldTextLB = QString(QStringLiteral("This is some\nbold text")).replace(QLatin1Char('\n'), QChar(QChar::LineSeparator));
3438 QTest::newRow("all plain text")
3440 << 0 << standard.at(0).length()
3443 QTest::newRow("plain text sub string")
3446 << standard.at(0).mid(0, 12);
3448 QTest::newRow("plain text sub string reversed")
3451 << standard.at(0).mid(0, 12);
3453 QTest::newRow("plain text cropped beginning")
3456 << standard.at(0).mid(0, 4);
3458 QTest::newRow("plain text cropped end")
3460 << 23 << standard.at(0).length() + 8
3461 << standard.at(0).mid(23);
3463 QTest::newRow("plain text cropped beginning and end")
3465 << -9 << standard.at(0).length() + 4
3468 QTest::newRow("all rich text")
3470 << 0 << plainBoldText.length()
3473 QTest::newRow("rich text sub string")
3476 << plainBoldText.mid(14, 7);
3479 QTest::newRow("all plain text (line break)")
3481 << 0 << standard.at(1).length()
3484 QTest::newRow("plain text sub string (line break)")
3487 << standard.at(1).mid(0, 12);
3489 QTest::newRow("plain text sub string reversed (line break)")
3492 << standard.at(1).mid(0, 12);
3494 QTest::newRow("plain text cropped beginning (line break)")
3497 << standard.at(1).mid(0, 4);
3499 QTest::newRow("plain text cropped end (line break)")
3501 << 23 << standard.at(1).length() + 8
3502 << standard.at(1).mid(23);
3504 QTest::newRow("plain text cropped beginning and end (line break)")
3506 << -9 << standard.at(1).length() + 4
3509 QTest::newRow("all rich text (line break)")
3511 << 0 << plainBoldTextLB.length()
3514 QTest::newRow("rich text sub string (line break)")
3517 << plainBoldTextLB.mid(14, 7);
3520 void tst_qquicktextedit::getText()
3522 QFETCH(QString, text);
3525 QFETCH(QString, expectedText);
3527 QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + text + "\" }";
3528 QQmlComponent textEditComponent(&engine);
3529 textEditComponent.setData(componentStr.toLatin1(), QUrl());
3530 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
3531 QVERIFY(textEdit != 0);
3533 QCOMPARE(textEdit->getText(start, end), expectedText);
3536 void tst_qquicktextedit::getFormattedText_data()
3538 QTest::addColumn<QString>("text");
3539 QTest::addColumn<QQuickTextEdit::TextFormat>("textFormat");
3540 QTest::addColumn<int>("start");
3541 QTest::addColumn<int>("end");
3542 QTest::addColumn<QString>("expectedText");
3544 const QString richBoldText = QStringLiteral("This is some <b>bold</b> text");
3545 const QString plainBoldText = QStringLiteral("This is some bold text");
3547 QTest::newRow("all plain text")
3549 << QQuickTextEdit::PlainText
3550 << 0 << standard.at(0).length()
3553 QTest::newRow("plain text sub string")
3555 << QQuickTextEdit::PlainText
3557 << standard.at(0).mid(0, 12);
3559 QTest::newRow("plain text sub string reversed")
3561 << QQuickTextEdit::PlainText
3563 << standard.at(0).mid(0, 12);
3565 QTest::newRow("plain text cropped beginning")
3567 << QQuickTextEdit::PlainText
3569 << standard.at(0).mid(0, 4);
3571 QTest::newRow("plain text cropped end")
3573 << QQuickTextEdit::PlainText
3574 << 23 << standard.at(0).length() + 8
3575 << standard.at(0).mid(23);
3577 QTest::newRow("plain text cropped beginning and end")
3579 << QQuickTextEdit::PlainText
3580 << -9 << standard.at(0).length() + 4
3583 QTest::newRow("all rich (Auto) text")
3585 << QQuickTextEdit::AutoText
3586 << 0 << plainBoldText.length()
3587 << QString("This is some \\<.*\\>bold\\</.*\\> text");
3589 QTest::newRow("all rich (Rich) text")
3591 << QQuickTextEdit::RichText
3592 << 0 << plainBoldText.length()
3593 << QString("This is some \\<.*\\>bold\\</.*\\> text");
3595 QTest::newRow("all rich (Plain) text")
3597 << QQuickTextEdit::PlainText
3598 << 0 << richBoldText.length()
3601 QTest::newRow("rich (Auto) text sub string")
3603 << QQuickTextEdit::AutoText
3605 << QString("\\<.*\\>old\\</.*\\> tex");
3607 QTest::newRow("rich (Rich) text sub string")
3609 << QQuickTextEdit::RichText
3611 << QString("\\<.*\\>old\\</.*\\> tex");
3613 QTest::newRow("rich (Plain) text sub string")
3615 << QQuickTextEdit::PlainText
3617 << richBoldText.mid(17, 10);
3620 void tst_qquicktextedit::getFormattedText()
3622 QFETCH(QString, text);
3623 QFETCH(QQuickTextEdit::TextFormat, textFormat);
3626 QFETCH(QString, expectedText);
3628 QString componentStr = "import QtQuick 2.0\nTextEdit {}";
3629 QQmlComponent textEditComponent(&engine);
3630 textEditComponent.setData(componentStr.toLatin1(), QUrl());
3631 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
3632 QVERIFY(textEdit != 0);
3634 textEdit->setTextFormat(textFormat);
3635 textEdit->setText(text);
3637 if (textFormat == QQuickTextEdit::RichText
3638 || (textFormat == QQuickTextEdit::AutoText && Qt::mightBeRichText(text))) {
3639 QVERIFY(textEdit->getFormattedText(start, end).contains(QRegExp(expectedText)));
3641 QCOMPARE(textEdit->getFormattedText(start, end), expectedText);
3645 void tst_qquicktextedit::insert_data()
3647 QTest::addColumn<QString>("text");
3648 QTest::addColumn<QQuickTextEdit::TextFormat>("textFormat");
3649 QTest::addColumn<int>("selectionStart");
3650 QTest::addColumn<int>("selectionEnd");
3651 QTest::addColumn<int>("insertPosition");
3652 QTest::addColumn<QString>("insertText");
3653 QTest::addColumn<QString>("expectedText");
3654 QTest::addColumn<int>("expectedSelectionStart");
3655 QTest::addColumn<int>("expectedSelectionEnd");
3656 QTest::addColumn<int>("expectedCursorPosition");
3657 QTest::addColumn<bool>("selectionChanged");
3658 QTest::addColumn<bool>("cursorPositionChanged");
3660 QTest::newRow("at cursor position (beginning)")
3661 << standard.at(0) << QQuickTextEdit::PlainText
3664 << QString("Hello") + standard.at(0)
3668 QTest::newRow("at cursor position (end)")
3669 << standard.at(0) << QQuickTextEdit::PlainText
3670 << standard.at(0).length() << standard.at(0).length() << standard.at(0).length()
3672 << standard.at(0) + QString("Hello")
3673 << standard.at(0).length() + 5 << standard.at(0).length() + 5 << standard.at(0).length() + 5
3676 QTest::newRow("at cursor position (middle)")
3677 << standard.at(0) << QQuickTextEdit::PlainText
3680 << standard.at(0).mid(0, 18) + QString("Hello") + standard.at(0).mid(18)
3684 QTest::newRow("after cursor position (beginning)")
3685 << standard.at(0) << QQuickTextEdit::PlainText
3688 << standard.at(0).mid(0, 18) + QString("Hello") + standard.at(0).mid(18)
3692 QTest::newRow("before cursor position (end)")
3693 << standard.at(0) << QQuickTextEdit::PlainText
3694 << standard.at(0).length() << standard.at(0).length() << 18
3696 << standard.at(0).mid(0, 18) + QString("Hello") + standard.at(0).mid(18)
3697 << standard.at(0).length() + 5 << standard.at(0).length() + 5 << standard.at(0).length() + 5
3700 QTest::newRow("before cursor position (middle)")
3701 << standard.at(0) << QQuickTextEdit::PlainText
3704 << QString("Hello") + standard.at(0)
3708 QTest::newRow("after cursor position (middle)")
3709 << standard.at(0) << QQuickTextEdit::PlainText
3710 << 18 << 18 << standard.at(0).length()
3712 << standard.at(0) + QString("Hello")
3716 QTest::newRow("before selection")
3717 << standard.at(0) << QQuickTextEdit::PlainText
3720 << QString("Hello") + standard.at(0)
3724 QTest::newRow("before reversed selection")
3725 << standard.at(0) << QQuickTextEdit::PlainText
3728 << QString("Hello") + standard.at(0)
3732 QTest::newRow("after selection")
3733 << standard.at(0) << QQuickTextEdit::PlainText
3734 << 14 << 19 << standard.at(0).length()
3736 << standard.at(0) + QString("Hello")
3740 QTest::newRow("after reversed selection")
3741 << standard.at(0) << QQuickTextEdit::PlainText
3742 << 19 << 14 << standard.at(0).length()
3744 << standard.at(0) + QString("Hello")
3748 QTest::newRow("into selection")
3749 << standard.at(0) << QQuickTextEdit::PlainText
3752 << standard.at(0).mid(0, 18) + QString("Hello") + standard.at(0).mid(18)
3756 QTest::newRow("into reversed selection")
3757 << standard.at(0) << QQuickTextEdit::PlainText
3760 << standard.at(0).mid(0, 18) + QString("Hello") + standard.at(0).mid(18)
3764 QTest::newRow("rich text into plain text")
3765 << standard.at(0) << QQuickTextEdit::PlainText
3767 << QString("<b>Hello</b>")
3768 << QString("<b>Hello</b>") + standard.at(0)
3772 QTest::newRow("rich text into rich text")
3773 << standard.at(0) << QQuickTextEdit::RichText
3775 << QString("<b>Hello</b>")
3776 << QString("Hello") + standard.at(0)
3780 QTest::newRow("rich text into auto text")
3781 << standard.at(0) << QQuickTextEdit::AutoText
3783 << QString("<b>Hello</b>")
3784 << QString("Hello") + standard.at(0)
3788 QTest::newRow("before start")
3789 << standard.at(0) << QQuickTextEdit::PlainText
3796 QTest::newRow("past end")
3797 << standard.at(0) << QQuickTextEdit::PlainText
3798 << 0 << 0 << standard.at(0).length() + 3
3805 void tst_qquicktextedit::insert()
3807 QFETCH(QString, text);
3808 QFETCH(QQuickTextEdit::TextFormat, textFormat);
3809 QFETCH(int, selectionStart);
3810 QFETCH(int, selectionEnd);
3811 QFETCH(int, insertPosition);
3812 QFETCH(QString, insertText);
3813 QFETCH(QString, expectedText);
3814 QFETCH(int, expectedSelectionStart);
3815 QFETCH(int, expectedSelectionEnd);
3816 QFETCH(int, expectedCursorPosition);
3817 QFETCH(bool, selectionChanged);
3818 QFETCH(bool, cursorPositionChanged);
3820 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\" }";
3821 QQmlComponent textEditComponent(&engine);
3822 textEditComponent.setData(componentStr.toLatin1(), QUrl());
3823 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
3824 QVERIFY(textEdit != 0);
3826 textEdit->setTextFormat(textFormat);
3827 textEdit->select(selectionStart, selectionEnd);
3829 QSignalSpy selectionSpy(textEdit, SIGNAL(selectedTextChanged()));
3830 QSignalSpy selectionStartSpy(textEdit, SIGNAL(selectionStartChanged()));
3831 QSignalSpy selectionEndSpy(textEdit, SIGNAL(selectionEndChanged()));
3832 QSignalSpy textSpy(textEdit, SIGNAL(textChanged()));
3833 QSignalSpy cursorPositionSpy(textEdit, SIGNAL(cursorPositionChanged()));
3835 textEdit->insert(insertPosition, insertText);
3837 if (textFormat == QQuickTextEdit::RichText || (textFormat == QQuickTextEdit::AutoText && (
3838 Qt::mightBeRichText(text) || Qt::mightBeRichText(insertText)))) {
3839 QCOMPARE(textEdit->getText(0, expectedText.length()), expectedText);
3841 QCOMPARE(textEdit->text(), expectedText);
3844 QCOMPARE(textEdit->length(), expectedText.length());
3846 QCOMPARE(textEdit->selectionStart(), expectedSelectionStart);
3847 QCOMPARE(textEdit->selectionEnd(), expectedSelectionEnd);
3848 QCOMPARE(textEdit->cursorPosition(), expectedCursorPosition);
3850 if (selectionStart > selectionEnd)
3851 qSwap(selectionStart, selectionEnd);
3853 QEXPECT_FAIL("into selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue);
3854 QEXPECT_FAIL("into reversed selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue);
3855 QCOMPARE(selectionSpy.count() > 0, selectionChanged);
3856 QCOMPARE(selectionStartSpy.count() > 0, selectionStart != expectedSelectionStart);
3857 QEXPECT_FAIL("into reversed selection", "selectionEndChanged signal not emitted", Continue);
3858 QCOMPARE(selectionEndSpy.count() > 0, selectionEnd != expectedSelectionEnd);
3859 QCOMPARE(textSpy.count() > 0, text != expectedText);
3860 QCOMPARE(cursorPositionSpy.count() > 0, cursorPositionChanged);
3863 void tst_qquicktextedit::remove_data()
3865 QTest::addColumn<QString>("text");
3866 QTest::addColumn<QQuickTextEdit::TextFormat>("textFormat");
3867 QTest::addColumn<int>("selectionStart");
3868 QTest::addColumn<int>("selectionEnd");
3869 QTest::addColumn<int>("removeStart");
3870 QTest::addColumn<int>("removeEnd");
3871 QTest::addColumn<QString>("expectedText");
3872 QTest::addColumn<int>("expectedSelectionStart");
3873 QTest::addColumn<int>("expectedSelectionEnd");
3874 QTest::addColumn<int>("expectedCursorPosition");
3875 QTest::addColumn<bool>("selectionChanged");
3876 QTest::addColumn<bool>("cursorPositionChanged");
3878 const QString richBoldText = QStringLiteral("This is some <b>bold</b> text");
3879 const QString plainBoldText = QStringLiteral("This is some bold text");
3881 QTest::newRow("from cursor position (beginning)")
3882 << standard.at(0) << QQuickTextEdit::PlainText
3885 << standard.at(0).mid(5)
3889 QTest::newRow("to cursor position (beginning)")
3890 << standard.at(0) << QQuickTextEdit::PlainText
3893 << standard.at(0).mid(5)
3897 QTest::newRow("to cursor position (end)")
3898 << standard.at(0) << QQuickTextEdit::PlainText
3899 << standard.at(0).length() << standard.at(0).length()
3900 << standard.at(0).length() << standard.at(0).length() - 5
3901 << standard.at(0).mid(0, standard.at(0).length() - 5)
3902 << standard.at(0).length() - 5 << standard.at(0).length() - 5 << standard.at(0).length() - 5
3905 QTest::newRow("to cursor position (end)")
3906 << standard.at(0) << QQuickTextEdit::PlainText
3907 << standard.at(0).length() << standard.at(0).length()
3908 << standard.at(0).length() - 5 << standard.at(0).length()
3909 << standard.at(0).mid(0, standard.at(0).length() - 5)
3910 << standard.at(0).length() - 5 << standard.at(0).length() - 5 << standard.at(0).length() - 5
3913 QTest::newRow("from cursor position (middle)")
3914 << standard.at(0) << QQuickTextEdit::PlainText
3917 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
3921 QTest::newRow("to cursor position (middle)")
3922 << standard.at(0) << QQuickTextEdit::PlainText
3925 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
3929 QTest::newRow("after cursor position (beginning)")
3930 << standard.at(0) << QQuickTextEdit::PlainText
3933 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
3937 QTest::newRow("before cursor position (end)")
3938 << standard.at(0) << QQuickTextEdit::PlainText
3939 << standard.at(0).length() << standard.at(0).length()
3941 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
3942 << standard.at(0).length() - 5 << standard.at(0).length() - 5 << standard.at(0).length() - 5
3945 QTest::newRow("before cursor position (middle)")
3946 << standard.at(0) << QQuickTextEdit::PlainText
3949 << standard.at(0).mid(5)
3953 QTest::newRow("after cursor position (middle)")
3954 << standard.at(0) << QQuickTextEdit::PlainText
3957 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
3961 QTest::newRow("before selection")
3962 << standard.at(0) << QQuickTextEdit::PlainText
3965 << standard.at(0).mid(5)
3969 QTest::newRow("before reversed selection")
3970 << standard.at(0) << QQuickTextEdit::PlainText
3973 << standard.at(0).mid(5)
3977 QTest::newRow("after selection")
3978 << standard.at(0) << QQuickTextEdit::PlainText
3980 << standard.at(0).length() - 5 << standard.at(0).length()
3981 << standard.at(0).mid(0, standard.at(0).length() - 5)
3985 QTest::newRow("after reversed selection")
3986 << standard.at(0) << QQuickTextEdit::PlainText
3988 << standard.at(0).length() - 5 << standard.at(0).length()
3989 << standard.at(0).mid(0, standard.at(0).length() - 5)
3993 QTest::newRow("from selection")
3994 << standard.at(0) << QQuickTextEdit::PlainText
3997 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
4001 QTest::newRow("from reversed selection")
4002 << standard.at(0) << QQuickTextEdit::PlainText
4005 << standard.at(0).mid(0, 18) + standard.at(0).mid(23)
4009 QTest::newRow("plain text cropped beginning")
4010 << standard.at(0) << QQuickTextEdit::PlainText
4013 << standard.at(0).mid(4)
4017 QTest::newRow("plain text cropped end")
4018 << standard.at(0) << QQuickTextEdit::PlainText
4020 << 23 << standard.at(0).length() + 8
4021 << standard.at(0).mid(0, 23)
4025 QTest::newRow("plain text cropped beginning and end")
4026 << standard.at(0) << QQuickTextEdit::PlainText
4028 << -9 << standard.at(0).length() + 4
4033 QTest::newRow("all rich text")
4034 << richBoldText << QQuickTextEdit::RichText
4036 << 0 << plainBoldText.length()
4041 QTest::newRow("rick text sub string")
4042 << richBoldText << QQuickTextEdit::RichText
4045 << plainBoldText.mid(0, 14) + plainBoldText.mid(21)
4050 void tst_qquicktextedit::remove()
4052 QFETCH(QString, text);
4053 QFETCH(QQuickTextEdit::TextFormat, textFormat);
4054 QFETCH(int, selectionStart);
4055 QFETCH(int, selectionEnd);
4056 QFETCH(int, removeStart);
4057 QFETCH(int, removeEnd);
4058 QFETCH(QString, expectedText);
4059 QFETCH(int, expectedSelectionStart);
4060 QFETCH(int, expectedSelectionEnd);
4061 QFETCH(int, expectedCursorPosition);
4062 QFETCH(bool, selectionChanged);
4063 QFETCH(bool, cursorPositionChanged);
4065 QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\" }";
4066 QQmlComponent textEditComponent(&engine);
4067 textEditComponent.setData(componentStr.toLatin1(), QUrl());
4068 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
4069 QVERIFY(textEdit != 0);
4071 textEdit->setTextFormat(textFormat);
4072 textEdit->select(selectionStart, selectionEnd);
4074 QSignalSpy selectionSpy(textEdit, SIGNAL(selectedTextChanged()));
4075 QSignalSpy selectionStartSpy(textEdit, SIGNAL(selectionStartChanged()));
4076 QSignalSpy selectionEndSpy(textEdit, SIGNAL(selectionEndChanged()));
4077 QSignalSpy textSpy(textEdit, SIGNAL(textChanged()));
4078 QSignalSpy cursorPositionSpy(textEdit, SIGNAL(cursorPositionChanged()));
4080 textEdit->remove(removeStart, removeEnd);
4082 if (textFormat == QQuickTextEdit::RichText
4083 || (textFormat == QQuickTextEdit::AutoText && Qt::mightBeRichText(text))) {
4084 QCOMPARE(textEdit->getText(0, expectedText.length()), expectedText);
4086 QCOMPARE(textEdit->text(), expectedText);
4088 QCOMPARE(textEdit->length(), expectedText.length());
4090 if (selectionStart > selectionEnd) //
4091 qSwap(selectionStart, selectionEnd);
4093 QCOMPARE(textEdit->selectionStart(), expectedSelectionStart);
4094 QCOMPARE(textEdit->selectionEnd(), expectedSelectionEnd);
4095 QCOMPARE(textEdit->cursorPosition(), expectedCursorPosition);
4097 QEXPECT_FAIL("from selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue);
4098 QEXPECT_FAIL("from reversed selection", "selectedTextChanged signal isn't emitted on edits within selection", Continue);
4099 QCOMPARE(selectionSpy.count() > 0, selectionChanged);
4100 QCOMPARE(selectionStartSpy.count() > 0, selectionStart != expectedSelectionStart);
4101 QEXPECT_FAIL("from reversed selection", "selectionEndChanged signal not emitted", Continue);
4102 QCOMPARE(selectionEndSpy.count() > 0, selectionEnd != expectedSelectionEnd);
4103 QCOMPARE(textSpy.count() > 0, text != expectedText);
4106 if (cursorPositionChanged) //
4107 QVERIFY(cursorPositionSpy.count() > 0);
4111 void tst_qquicktextedit::keySequence_data()
4113 QTest::addColumn<QString>("text");
4114 QTest::addColumn<QKeySequence>("sequence");
4115 QTest::addColumn<int>("selectionStart");
4116 QTest::addColumn<int>("selectionEnd");
4117 QTest::addColumn<int>("cursorPosition");
4118 QTest::addColumn<QString>("expectedText");
4119 QTest::addColumn<QString>("selectedText");
4120 QTest::addColumn<Qt::Key>("layoutDirection");
4122 // standard[0] == "the [4]quick [10]brown [16]fox [20]jumped [27]over [32]the [36]lazy [41]dog"
4124 QTest::newRow("select all")
4125 << standard.at(0) << QKeySequence(QKeySequence::SelectAll) << 0 << 0
4126 << 44 << standard.at(0) << standard.at(0)
4127 << Qt::Key_Direction_L;
4128 QTest::newRow("select start of line")
4129 << standard.at(0) << QKeySequence(QKeySequence::SelectStartOfLine) << 5 << 5
4130 << 0 << standard.at(0) << standard.at(0).mid(0, 5)
4131 << Qt::Key_Direction_L;
4132 QTest::newRow("select start of block")
4133 << standard.at(0) << QKeySequence(QKeySequence::SelectStartOfBlock) << 5 << 5
4134 << 0 << standard.at(0) << standard.at(0).mid(0, 5)
4135 << Qt::Key_Direction_L;
4136 QTest::newRow("select end of line")
4137 << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfLine) << 5 << 5
4138 << 44 << standard.at(0) << standard.at(0).mid(5)
4139 << Qt::Key_Direction_L;
4140 QTest::newRow("select end of document")
4141 << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfDocument) << 3 << 3
4142 << 44 << standard.at(0) << standard.at(0).mid(3)
4143 << Qt::Key_Direction_L;
4144 QTest::newRow("select end of block")
4145 << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfBlock) << 18 << 18
4146 << 44 << standard.at(0) << standard.at(0).mid(18)
4147 << Qt::Key_Direction_L;
4148 QTest::newRow("delete end of line")
4149 << standard.at(0) << QKeySequence(QKeySequence::DeleteEndOfLine) << 24 << 24
4150 << 24 << standard.at(0).mid(0, 24) << QString()
4151 << Qt::Key_Direction_L;
4152 QTest::newRow("move to start of line")
4153 << standard.at(0) << QKeySequence(QKeySequence::MoveToStartOfLine) << 31 << 31
4154 << 0 << standard.at(0) << QString()
4155 << Qt::Key_Direction_L;
4156 QTest::newRow("move to start of block")
4157 << standard.at(0) << QKeySequence(QKeySequence::MoveToStartOfBlock) << 25 << 25
4158 << 0 << standard.at(0) << QString()
4159 << Qt::Key_Direction_L;
4160 QTest::newRow("move to next char")
4161 << standard.at(0) << QKeySequence(QKeySequence::MoveToNextChar) << 12 << 12
4162 << 13 << standard.at(0) << QString()
4163 << Qt::Key_Direction_L;
4164 QTest::newRow("move to previous char (ltr)")
4165 << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 3
4166 << 2 << standard.at(0) << QString()
4167 << Qt::Key_Direction_L;
4168 QTest::newRow("move to previous char (rtl)")
4169 << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 3
4170 << 4 << standard.at(0) << QString()
4171 << Qt::Key_Direction_R;
4172 QTest::newRow("move to previous char with selection")
4173 << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 7
4174 << 3 << standard.at(0) << QString()
4175 << Qt::Key_Direction_L;
4176 QTest::newRow("select next char (ltr)")
4177 << standard.at(0) << QKeySequence(QKeySequence::SelectNextChar) << 23 << 23
4178 << 24 << standard.at(0) << standard.at(0).mid(23, 1)
4179 << Qt::Key_Direction_L;
4180 QTest::newRow("select next char (rtl)")
4181 << standard.at(0) << QKeySequence(QKeySequence::SelectNextChar) << 23 << 23
4182 << 22 << standard.at(0) << standard.at(0).mid(22, 1)
4183 << Qt::Key_Direction_R;
4184 QTest::newRow("select previous char (ltr)")
4185 << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousChar) << 19 << 19
4186 << 18 << standard.at(0) << standard.at(0).mid(18, 1)
4187 << Qt::Key_Direction_L;
4188 QTest::newRow("select previous char (rtl)")
4189 << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousChar) << 19 << 19
4190 << 20 << standard.at(0) << standard.at(0).mid(19, 1)
4191 << Qt::Key_Direction_R;
4192 QTest::newRow("move to next word (ltr)")
4193 << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
4194 << 10 << standard.at(0) << QString()
4195 << Qt::Key_Direction_L;
4196 QTest::newRow("move to next word (rtl)")
4197 << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
4198 << 4 << standard.at(0) << QString()
4199 << Qt::Key_Direction_R;
4200 QTest::newRow("move to previous word (ltr)")
4201 << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
4202 << 4 << standard.at(0) << QString()
4203 << Qt::Key_Direction_L;
4204 QTest::newRow("move to previous word (rlt)")
4205 << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
4206 << 10 << standard.at(0) << QString()
4207 << Qt::Key_Direction_R;
4208 QTest::newRow("select next word")
4209 << standard.at(0) << QKeySequence(QKeySequence::SelectNextWord) << 11 << 11
4210 << 16 << standard.at(0) << standard.at(0).mid(11, 5)
4211 << Qt::Key_Direction_L;
4212 QTest::newRow("select previous word")
4213 << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousWord) << 11 << 11
4214 << 10 << standard.at(0) << standard.at(0).mid(10, 1)
4215 << Qt::Key_Direction_L;
4216 QTest::newRow("delete (selection)")
4217 << standard.at(0) << QKeySequence(QKeySequence::Delete) << 12 << 15
4218 << 12 << (standard.at(0).mid(0, 12) + standard.at(0).mid(15)) << QString()
4219 << Qt::Key_Direction_L;
4220 QTest::newRow("delete (no selection)")
4221 << standard.at(0) << QKeySequence(QKeySequence::Delete) << 15 << 15
4222 << 15 << (standard.at(0).mid(0, 15) + standard.at(0).mid(16)) << QString()
4223 << Qt::Key_Direction_L;
4224 QTest::newRow("delete end of word")
4225 << standard.at(0) << QKeySequence(QKeySequence::DeleteEndOfWord) << 24 << 24
4226 << 24 << (standard.at(0).mid(0, 24) + standard.at(0).mid(27)) << QString()
4227 << Qt::Key_Direction_L;
4228 QTest::newRow("delete start of word")
4229 << standard.at(0) << QKeySequence(QKeySequence::DeleteStartOfWord) << 7 << 7
4230 << 4 << (standard.at(0).mid(0, 4) + standard.at(0).mid(7)) << QString()
4231 << Qt::Key_Direction_L;
4234 void tst_qquicktextedit::keySequence()
4236 QFETCH(QString, text);
4237 QFETCH(QKeySequence, sequence);
4238 QFETCH(int, selectionStart);
4239 QFETCH(int, selectionEnd);
4240 QFETCH(int, cursorPosition);
4241 QFETCH(QString, expectedText);
4242 QFETCH(QString, selectedText);
4243 QFETCH(Qt::Key, layoutDirection);
4245 if (sequence.isEmpty()) {
4246 QSKIP("Key sequence is undefined");
4249 QString componentStr = "import QtQuick 2.0\nTextEdit { focus: true; text: \"" + text + "\" }";
4250 QQmlComponent textEditComponent(&engine);
4251 textEditComponent.setData(componentStr.toLatin1(), QUrl());
4252 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
4253 QVERIFY(textEdit != 0);
4255 QQuickWindow window;
4256 textEdit->setParentItem(window.rootItem());
4258 window.requestActivateWindow();
4259 QTest::qWaitForWindowActive(&window);
4261 QVERIFY(textEdit->hasActiveFocus());
4263 simulateKey(&window, layoutDirection);
4265 textEdit->select(selectionStart, selectionEnd);
4267 simulateKeys(&window, sequence);
4269 QCOMPARE(textEdit->cursorPosition(), cursorPosition);
4270 QCOMPARE(textEdit->text(), expectedText);
4271 QCOMPARE(textEdit->selectedText(), selectedText);
4275 #define REPLACE_UNTIL_END 1
4277 void tst_qquicktextedit::undo_data()
4279 QTest::addColumn<QStringList>("insertString");
4280 QTest::addColumn<IntList>("insertIndex");
4281 QTest::addColumn<IntList>("insertMode");
4282 QTest::addColumn<QStringList>("expectedString");
4283 QTest::addColumn<bool>("use_keys");
4285 for (int i=0; i<2; i++) {
4286 QString keys_str = "keyboard";
4287 bool use_keys = true;
4289 keys_str = "insert";
4294 IntList insertIndex;
4296 QStringList insertString;
4297 QStringList expectedString;
4300 insertMode << NORMAL;
4301 insertString << "1";
4304 insertMode << NORMAL;
4305 insertString << "5";
4308 insertMode << NORMAL;
4309 insertString << "3";
4312 insertMode << NORMAL;
4313 insertString << "2";
4316 insertMode << NORMAL;
4317 insertString << "4";
4319 expectedString << "12345";
4320 expectedString << "1235";
4321 expectedString << "135";
4322 expectedString << "15";
4323 expectedString << "";
4325 QTest::newRow(QString(keys_str + "_numbers").toLatin1()) <<
4333 IntList insertIndex;
4335 QStringList insertString;
4336 QStringList expectedString;
4339 insertMode << NORMAL;
4340 insertString << "World"; // World
4343 insertMode << NORMAL;
4344 insertString << "Hello"; // HelloWorld
4347 insertMode << NORMAL;
4348 insertString << "Well"; // WellHelloWorld
4351 insertMode << NORMAL;
4352 insertString << "There"; // WellHelloThereWorld;
4354 expectedString << "WellHelloThereWorld";
4355 expectedString << "WellHelloWorld";
4356 expectedString << "HelloWorld";
4357 expectedString << "World";
4358 expectedString << "";
4360 QTest::newRow(QString(keys_str + "_helloworld").toLatin1()) <<
4368 IntList insertIndex;
4370 QStringList insertString;
4371 QStringList expectedString;
4374 insertMode << NORMAL;
4375 insertString << "Ensuring";
4378 insertMode << NORMAL;
4379 insertString << " instan";
4382 insertMode << NORMAL;
4383 insertString << "an ";
4386 insertMode << REPLACE_UNTIL_END;
4387 insertString << " unique instance.";
4389 expectedString << "Ensuring a unique instance.";
4390 expectedString << "Ensuring a "; // ### Not present in TextEdit.
4391 expectedString << "Ensuring an instan";
4392 expectedString << "Ensuring instan";
4393 expectedString << "";
4395 QTest::newRow(QString(keys_str + "_patterns").toLatin1()) <<
4405 void tst_qquicktextedit::undo()
4407 QFETCH(QStringList, insertString);
4408 QFETCH(IntList, insertIndex);
4409 QFETCH(IntList, insertMode);
4410 QFETCH(QStringList, expectedString);
4412 QString componentStr = "import QtQuick 2.0\nTextEdit { focus: true }";
4413 QQmlComponent textEditComponent(&engine);
4414 textEditComponent.setData(componentStr.toLatin1(), QUrl());
4415 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
4416 QVERIFY(textEdit != 0);
4418 QQuickWindow window;
4419 textEdit->setParentItem(window.rootItem());
4421 window.requestActivateWindow();
4422 QTest::qWaitForWindowActive(&window);
4424 QVERIFY(textEdit->hasActiveFocus());
4425 QVERIFY(!textEdit->canUndo());
4427 QSignalSpy spy(textEdit, SIGNAL(canUndoChanged()));
4431 // STEP 1: First build up an undo history by inserting or typing some strings...
4432 for (i = 0; i < insertString.size(); ++i) {
4433 if (insertIndex[i] > -1)
4434 textEdit->setCursorPosition(insertIndex[i]);
4436 // experimental stuff
4437 if (insertMode[i] == REPLACE_UNTIL_END) {
4438 textEdit->select(insertIndex[i], insertIndex[i] + 8);
4440 // This is what I actually want...
4441 // QTest::keyClick(testWidget, Qt::Key_End, Qt::ShiftModifier);
4444 for (int j = 0; j < insertString.at(i).length(); j++)
4445 QTest::keyClick(&window, insertString.at(i).at(j).toLatin1());
4448 QCOMPARE(spy.count(), 1);
4450 // STEP 2: Next call undo several times and see if we can restore to the previous state
4451 for (i = 0; i < expectedString.size() - 1; ++i) {
4452 QCOMPARE(textEdit->text(), expectedString[i]);
4453 QVERIFY(textEdit->canUndo());
4457 // STEP 3: Verify that we have undone everything
4458 QVERIFY(textEdit->text().isEmpty());
4459 QVERIFY(!textEdit->canUndo());
4460 QCOMPARE(spy.count(), 2);
4463 void tst_qquicktextedit::redo_data()
4465 QTest::addColumn<QStringList>("insertString");
4466 QTest::addColumn<IntList>("insertIndex");
4467 QTest::addColumn<QStringList>("expectedString");
4470 IntList insertIndex;
4471 QStringList insertString;
4472 QStringList expectedString;
4475 insertString << "World"; // World
4477 insertString << "Hello"; // HelloWorld
4479 insertString << "Well"; // WellHelloWorld
4481 insertString << "There"; // WellHelloThereWorld;
4483 expectedString << "World";
4484 expectedString << "HelloWorld";
4485 expectedString << "WellHelloWorld";
4486 expectedString << "WellHelloThereWorld";
4488 QTest::newRow("Inserts and setting cursor") << insertString << insertIndex << expectedString;
4492 void tst_qquicktextedit::redo()
4494 QFETCH(QStringList, insertString);
4495 QFETCH(IntList, insertIndex);
4496 QFETCH(QStringList, expectedString);
4498 QString componentStr = "import QtQuick 2.0\nTextEdit { focus: true }";
4499 QQmlComponent textEditComponent(&engine);
4500 textEditComponent.setData(componentStr.toLatin1(), QUrl());
4501 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
4502 QVERIFY(textEdit != 0);
4504 QQuickWindow window;
4505 textEdit->setParentItem(window.rootItem());
4507 window.requestActivateWindow();
4508 QTest::qWaitForWindowActive(&window);
4509 QVERIFY(textEdit->hasActiveFocus());
4511 QVERIFY(!textEdit->canUndo());
4512 QVERIFY(!textEdit->canRedo());
4514 QSignalSpy spy(textEdit, SIGNAL(canRedoChanged()));
4517 // inserts the diff strings at diff positions
4518 for (i = 0; i < insertString.size(); ++i) {
4519 if (insertIndex[i] > -1)
4520 textEdit->setCursorPosition(insertIndex[i]);
4521 for (int j = 0; j < insertString.at(i).length(); j++)
4522 QTest::keyClick(&window, insertString.at(i).at(j).toLatin1());
4523 QVERIFY(textEdit->canUndo());
4524 QVERIFY(!textEdit->canRedo());
4527 QCOMPARE(spy.count(), 0);
4530 while (!textEdit->text().isEmpty()) {
4531 QVERIFY(textEdit->canUndo());
4533 QVERIFY(textEdit->canRedo());
4536 QCOMPARE(spy.count(), 1);
4538 for (i = 0; i < expectedString.size(); ++i) {
4539 QVERIFY(textEdit->canRedo());
4541 QCOMPARE(textEdit->text() , expectedString[i]);
4542 QVERIFY(textEdit->canUndo());
4544 QVERIFY(!textEdit->canRedo());
4545 QCOMPARE(spy.count(), 2);
4548 void tst_qquicktextedit::undo_keypressevents_data()
4550 QTest::addColumn<KeyList>("keys");
4551 QTest::addColumn<QStringList>("expectedString");
4555 QStringList expectedString;
4568 expectedString << "BEVERYAFRAID!";
4569 expectedString << "BEVERYAFRAID";
4570 expectedString << "VERYAFRAID";
4571 expectedString << "AFRAID";
4573 QTest::newRow("Inserts and moving cursor") << keys << expectedString;
4576 QStringList expectedString;
4579 keys << "1234" << Qt::Key_Home
4581 << Qt::Key_Right << Qt::Key_Right
4583 << (Qt::Key_Right | Qt::ShiftModifier) << (Qt::Key_Right | Qt::ShiftModifier)
4587 expectedString << "12";
4588 expectedString << "1234";
4590 QTest::newRow("Inserts,moving,selection and delete") << keys << expectedString;
4593 QStringList expectedString;
4599 << (Qt::Key_Right | Qt::ShiftModifier) << (Qt::Key_Right | Qt::ShiftModifier)
4601 << QKeySequence::Undo
4602 // ### Text is selected in text input
4604 << (Qt::Key_Right | Qt::ShiftModifier) << (Qt::Key_Right | Qt::ShiftModifier)
4607 expectedString << "AB";
4608 expectedString << "AB12";
4610 QTest::newRow("Inserts,moving,selection, delete and undo") << keys << expectedString;
4613 QStringList expectedString;
4618 << Qt::Key_Left << Qt::Key_Left
4622 << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier)
4623 // overwriting '1234' with '5'
4625 // undoing deletion of 'AB'
4626 << QKeySequence::Undo
4627 // ### Text is selected in text input
4628 << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier) << (Qt::Key_Left | Qt::ShiftModifier)
4629 // overwriting '1234' with '6'
4632 expectedString << "ab6cd";
4633 // for versions previous to 3.2 we overwrite needed two undo operations
4634 expectedString << "ab1234cd";
4635 expectedString << "abcd";
4637 QTest::newRow("Inserts,moving,selection and undo, removing selection") << keys << expectedString;
4640 QStringList expectedString;
4645 << Qt::Key_Backspace;
4647 expectedString << "AB";
4648 expectedString << "ABC";
4650 QTest::newRow("Inserts,backspace") << keys << expectedString;
4653 QStringList expectedString;
4657 << Qt::Key_Backspace
4661 expectedString << "ABZ";
4662 expectedString << "AB";
4663 expectedString << "ABC";
4665 QTest::newRow("Inserts,backspace,inserts") << keys << expectedString;
4668 QStringList expectedString;
4671 keys << "123" << Qt::Key_Home
4673 << (Qt::Key_End | Qt::ShiftModifier)
4674 // overwriting '123' with 'ABC'
4677 expectedString << "ABC";
4678 // ### One operation in TextEdit.
4679 expectedString << "A";
4680 expectedString << "123";
4682 QTest::newRow("Inserts,moving,selection and overwriting") << keys << expectedString;
4685 bool canCopyPaste = PlatformQuirks::isClipboardAvailable();
4690 << QKeySequence(QKeySequence::SelectStartOfLine)
4691 << QKeySequence(QKeySequence::Cut)
4693 << QKeySequence(QKeySequence::Paste);
4694 QStringList expectedString = QStringList()
4699 QTest::newRow("Cut,paste") << keys << expectedString;
4704 << QKeySequence(QKeySequence::SelectStartOfLine)
4705 << QKeySequence(QKeySequence::Copy)
4707 << QKeySequence(QKeySequence::Paste);
4708 QStringList expectedString = QStringList()
4713 QTest::newRow("Copy,paste") << keys << expectedString;
4717 void tst_qquicktextedit::undo_keypressevents()
4719 QFETCH(KeyList, keys);
4720 QFETCH(QStringList, expectedString);
4722 QString componentStr = "import QtQuick 2.0\nTextEdit { focus: true }";
4723 QQmlComponent textEditComponent(&engine);
4724 textEditComponent.setData(componentStr.toLatin1(), QUrl());
4725 QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
4726 QVERIFY(textEdit != 0);
4728 QQuickWindow window;
4729 textEdit->setParentItem(window.rootItem());
4731 window.requestActivateWindow();
4732 QTest::qWaitForWindowActive(&window);
4733 QVERIFY(textEdit->hasActiveFocus());
4735 simulateKeys(&window, keys);
4737 for (int i = 0; i < expectedString.size(); ++i) {
4738 QCOMPARE(textEdit->text() , expectedString[i]);
4741 QVERIFY(textEdit->text().isEmpty());
4744 void tst_qquicktextedit::baseUrl()
4746 QUrl localUrl("file:///tests/text.qml");
4747 QUrl remoteUrl("http://qt.nokia.com/test.qml");
4749 QQmlComponent textComponent(&engine);
4750 textComponent.setData("import QtQuick 2.0\n TextEdit {}", localUrl);
4751 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit *>(textComponent.create());
4753 QCOMPARE(textObject->baseUrl(), localUrl);
4755 QSignalSpy spy(textObject, SIGNAL(baseUrlChanged()));
4757 textObject->setBaseUrl(localUrl);
4758 QCOMPARE(textObject->baseUrl(), localUrl);
4759 QCOMPARE(spy.count(), 0);
4761 textObject->setBaseUrl(remoteUrl);
4762 QCOMPARE(textObject->baseUrl(), remoteUrl);
4763 QCOMPARE(spy.count(), 1);
4765 textObject->resetBaseUrl();
4766 QCOMPARE(textObject->baseUrl(), localUrl);
4767 QCOMPARE(spy.count(), 2);
4770 void tst_qquicktextedit::embeddedImages_data()
4772 QTest::addColumn<QUrl>("qmlfile");
4773 QTest::addColumn<QString>("error");
4774 QTest::newRow("local") << testFileUrl("embeddedImagesLocal.qml") << "";
4775 QTest::newRow("local-error") << testFileUrl("embeddedImagesLocalError.qml")
4776 << testFileUrl("embeddedImagesLocalError.qml").toString()+":3:1: QML TextEdit: Cannot open: " + testFileUrl("http/notexists.png").toString();
4777 QTest::newRow("local") << testFileUrl("embeddedImagesLocalRelative.qml") << "";
4778 QTest::newRow("remote") << testFileUrl("embeddedImagesRemote.qml") << "";
4779 QTest::newRow("remote-error") << testFileUrl("embeddedImagesRemoteError.qml")
4780 << testFileUrl("embeddedImagesRemoteError.qml").toString()+":3:1: QML TextEdit: Error downloading http://127.0.0.1:42332/notexists.png - server replied: Not found";
4781 QTest::newRow("remote") << testFileUrl("embeddedImagesRemoteRelative.qml") << "";
4784 void tst_qquicktextedit::embeddedImages()
4786 QFETCH(QUrl, qmlfile);
4787 QFETCH(QString, error);
4789 TestHTTPServer server(SERVER_PORT);
4790 server.serveDirectory(testFile("http"));
4792 if (!error.isEmpty())
4793 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
4795 QQmlComponent textComponent(&engine, qmlfile);
4796 QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit*>(textComponent.create());
4798 QVERIFY(textObject != 0);
4799 QTRY_COMPARE(QQuickTextEditPrivate::get(textObject)->document->resourcesLoading(), 0);
4801 QPixmap pm(testFile("http/exists.png"));
4802 if (error.isEmpty()) {
4803 QCOMPARE(textObject->width(), double(pm.width()));
4804 QCOMPARE(textObject->height(), double(pm.height()));
4806 QVERIFY(16 != pm.width()); // check test is effective
4807 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
4808 QCOMPARE(textObject->height(), 16.0);
4814 void tst_qquicktextedit::emptytags_QTBUG_22058()
4816 QQuickView window(testFileUrl("qtbug-22058.qml"));
4817 QVERIFY(window.rootObject() != 0);
4820 window.requestActivateWindow();
4821 QTest::qWaitForWindowActive(&window);
4822 QQuickTextEdit *input = qobject_cast<QQuickTextEdit *>(qvariant_cast<QObject *>(window.rootObject()->property("inputField")));
4823 QVERIFY(input->hasActiveFocus());
4825 QInputMethodEvent event("", QList<QInputMethodEvent::Attribute>());
4826 event.setCommitString("<b>Bold<");
4827 QGuiApplication::sendEvent(input, &event);
4828 QCOMPARE(input->text(), QString("<b>Bold<"));
4829 event.setCommitString(">");
4830 QGuiApplication::sendEvent(input, &event);
4831 QCOMPARE(input->text(), QString("<b>Bold<>"));
4834 QTEST_MAIN(tst_qquicktextedit)
4836 #include "tst_qquicktextedit.moc"