| 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 test suite 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 <QtTest> | 
| 30 | #include <private/qquickimage_p.h> | 
| 31 | #include <QQmlApplicationEngine> | 
| 32 |  | 
| 33 | class tst_sharedimage : public QObject | 
| 34 | { | 
| 35 |     Q_OBJECT | 
| 36 | public: | 
| 37 |     tst_sharedimage() | 
| 38 |     { | 
| 39 |     } | 
| 40 |  | 
| 41 | private slots: | 
| 42 |     void initTestCase(); | 
| 43 |     void compareToPlainLoad_data(); | 
| 44 |     void compareToPlainLoad(); | 
| 45 | }; | 
| 46 |  | 
| 47 | void tst_sharedimage::initTestCase() | 
| 48 | { | 
| 49 | #if !QT_CONFIG(systemsemaphore) | 
| 50 |     QSKIP("Shared image not supported" ); | 
| 51 | #endif | 
| 52 | } | 
| 53 |  | 
| 54 | void tst_sharedimage::compareToPlainLoad_data() | 
| 55 | { | 
| 56 |     QString imagePath = QFINDTESTDATA("data/yellow.png" ); | 
| 57 |     if (imagePath.startsWith(c: QLatin1Char('/'))) | 
| 58 |         imagePath.remove(i: 0, len: 1); | 
| 59 |     QString plainImage("Image { source: \"file:///%1\"; cache: false; %2 }" ); | 
| 60 |     QString sharedImage("Image { source: \"image://shared/%1\"; cache: false; %2 }" ); | 
| 61 |     QString script("import QtQuick 2.0\nimport Qt.labs.sharedimage 1.0\n%1\n" ); | 
| 62 |  | 
| 63 |     QString plain = script.arg(a: plainImage).arg(a: imagePath); | 
| 64 |     QString shared = script.arg(a: sharedImage).arg(a: imagePath); | 
| 65 |  | 
| 66 |     QTest::addColumn<QByteArray>(name: "plainScript" ); | 
| 67 |     QTest::addColumn<QByteArray>(name: "sharedScript" ); | 
| 68 |  | 
| 69 |     QString opts = QStringLiteral("asynchronous: false;" ); | 
| 70 |     QTest::newRow(dataTag: "sync" ) << plain.arg(a: opts).toLatin1() << shared.arg(a: opts).toLatin1(); | 
| 71 |  | 
| 72 |     opts = QStringLiteral("asynchronous: true" ); | 
| 73 |     QTest::newRow(dataTag: "async" ) << plain.arg(a: opts).toLatin1() << shared.arg(a: opts).toLatin1(); | 
| 74 |  | 
| 75 |     opts = QStringLiteral("sourceSize: Qt.size(50, 50)" ); | 
| 76 |     QTest::newRow(dataTag: "scaled, stretch" ) << plain.arg(a: opts).toLatin1() << shared.arg(a: opts).toLatin1(); | 
| 77 |  | 
| 78 |     opts = QStringLiteral("sourceSize: Qt.size(50, 50); fillMode: Image.PreserveAspectFit" ); | 
| 79 |     QTest::newRow(dataTag: "scaled, aspectfit" ) << plain.arg(a: opts).toLatin1() << shared.arg(a: opts).toLatin1(); | 
| 80 | } | 
| 81 |  | 
| 82 | void tst_sharedimage::compareToPlainLoad() | 
| 83 | { | 
| 84 |     QFETCH(QByteArray, plainScript); | 
| 85 |     QFETCH(QByteArray, sharedScript); | 
| 86 |  | 
| 87 |     QImage images[2]; | 
| 88 |     for (int i = 0; i < 2; i++) { | 
| 89 |         QQmlApplicationEngine engine; | 
| 90 |         engine.loadData(data: i ? sharedScript : plainScript); | 
| 91 |         QVERIFY(engine.rootObjects().size()); | 
| 92 |         QQuickImage *obj = qobject_cast<QQuickImage*>(object: engine.rootObjects().at(i: 0)); | 
| 93 |         QVERIFY(obj != nullptr); | 
| 94 |         QTRY_VERIFY(!obj->image().isNull()); | 
| 95 |         images[i] = obj->image(); | 
| 96 |     } | 
| 97 |  | 
| 98 |     QCOMPARE(images[1], images[0].convertToFormat(images[1].format())); | 
| 99 | } | 
| 100 |  | 
| 101 | QTEST_MAIN(tst_sharedimage) | 
| 102 |  | 
| 103 | #include "tst_sharedimage.moc" | 
| 104 |  |