| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <qtest.h> |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <QtQuick/QQuickTextDocument> |
| 32 | #include <QtQuick/QQuickItem> |
| 33 | #include <QtQuick/private/qquicktextedit_p.h> |
| 34 | #include <QtQuick/private/qquicktextdocument_p.h> |
| 35 | #include <QtGui/QTextDocument> |
| 36 | #include <QtGui/QTextDocumentWriter> |
| 37 | #include <QtQml/QQmlEngine> |
| 38 | #include <QtQml/QQmlComponent> |
| 39 | #include "../../shared/util.h" |
| 40 | |
| 41 | class tst_qquicktextdocument : public QQmlDataTest |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | private slots: |
| 45 | void textDocumentWriter(); |
| 46 | void textDocumentWithImage(); |
| 47 | }; |
| 48 | |
| 49 | QString text = QStringLiteral("foo bar" ); |
| 50 | |
| 51 | void tst_qquicktextdocument::textDocumentWriter() |
| 52 | { |
| 53 | QQmlEngine e; |
| 54 | QQmlComponent c(&e, testFileUrl(fileName: "text.qml" )); |
| 55 | QObject* o = c.create(); |
| 56 | QVERIFY(o); |
| 57 | QQuickTextEdit *edit = qobject_cast<QQuickTextEdit*>(object: o); |
| 58 | QVERIFY(edit); |
| 59 | |
| 60 | QQuickTextDocument* quickDocument = qobject_cast<QQuickTextDocument*>(object: edit->property(name: "textDocument" ).value<QObject*>()); |
| 61 | QVERIFY(quickDocument->textDocument() != nullptr); |
| 62 | |
| 63 | QBuffer output; |
| 64 | output.open(openMode: QBuffer::ReadWrite); |
| 65 | QVERIFY(output.buffer().isEmpty()); |
| 66 | |
| 67 | edit->setProperty(name: "text" , value: QVariant(text)); |
| 68 | QTextDocumentWriter writer(&output, "plaintext" ); |
| 69 | QVERIFY(writer.write(quickDocument->textDocument())); |
| 70 | QCOMPARE(output.buffer(), text.toLatin1()); |
| 71 | delete o; |
| 72 | } |
| 73 | |
| 74 | void tst_qquicktextdocument::textDocumentWithImage() |
| 75 | { |
| 76 | QQuickTextDocumentWithImageResources document(nullptr); |
| 77 | QImage image(1, 1, QImage::Format_Mono); |
| 78 | image.fill(pixel: 1); |
| 79 | |
| 80 | QString name = "image" ; |
| 81 | document.addResource(type: QTextDocument::ImageResource, name, resource: image); |
| 82 | QTextImageFormat format; |
| 83 | format.setName(name); |
| 84 | QCOMPARE(image, document.image(format)); |
| 85 | QCOMPARE(image, document.resource(QTextDocument::ImageResource, name).value<QImage>()); |
| 86 | } |
| 87 | |
| 88 | QTEST_MAIN(tst_qquicktextdocument) |
| 89 | |
| 90 | #include "tst_qquicktextdocument.moc" |
| 91 | |