| 1 |
/* |
| 2 |
* Copyright 2012 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <QtQml/QQmlEngine> |
| 20 |
#include <QtQuick/private/qquickimagebase_p.h> |
| 21 |
#include <QtTest/QtTest> |
| 22 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 23 |
#include <LomiriToolkit-Qt6/lomiritoolkitmodule.h> |
| 24 |
#else |
| 25 |
#include <LomiriToolkit/lomiritoolkitmodule.h> |
| 26 |
#endif |
| 27 |
#define protected public |
| 28 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 29 |
#include <LomiriToolkit-Qt6/private/ucqquickimageextension_p.h> |
| 30 |
#else |
| 31 |
#include <LomiriToolkit/private/ucqquickimageextension_p.h> |
| 32 |
#endif |
| 33 |
#undef protected |
| 34 |
#include <QtCore/private/qabstractfileengine_p.h> |
| 35 |
#include <QQuickView> |
| 36 |
|
| 37 |
UT_USE_NAMESPACE |
| 38 |
|
| 39 |
unsigned int numberOfTemporarySciFiles() { |
| 40 |
QStringList nameFilters; |
| 41 |
nameFilters << "*.sci"; |
| 42 |
return QDir::temp().entryList(nameFilters, QDir::Files).count(); |
| 43 |
} |
| 44 |
|
| 45 |
int nFaces = 0; |
| 46 |
|
| 47 |
class DummyFileEngineHandler : public QAbstractFileEngineHandler |
| 48 |
{ |
| 49 |
public: |
| 50 |
QAbstractFileEngine *create(const QString &fileName) const |
| 51 |
{ |
| 52 |
if (fileName.endsWith("/face.png")) |
| 53 |
nFaces++; |
| 54 |
|
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
class tst_UCQQuickImageExtension : public QObject |
| 60 |
{ |
| 61 |
Q_OBJECT |
| 62 |
|
| 63 |
private: |
| 64 |
QQmlEngine *engine = Q_NULLPTR; |
| 65 |
|
| 66 |
private Q_SLOTS: |
| 67 |
|
| 68 |
void init() |
| 69 |
{ |
| 70 |
engine = new QQmlEngine; |
| 71 |
LomiriToolkitModule::initializeContextProperties(engine); |
| 72 |
} |
| 73 |
|
| 74 |
void cleanup() |
| 75 |
{ |
| 76 |
delete engine; |
| 77 |
} |
| 78 |
|
| 79 |
void scaledBorderIdentity() { |
| 80 |
UCQQuickImageExtension image; |
| 81 |
QString border = "border: 13"; |
| 82 |
QString expected = "border: 13"; |
| 83 |
QString result = image.scaledBorder(border, "1"); |
| 84 |
QCOMPARE(result, expected); |
| 85 |
} |
| 86 |
|
| 87 |
void scaledBorderHalf() { |
| 88 |
UCQQuickImageExtension image; |
| 89 |
QString border = "border: 13"; |
| 90 |
QString expected = "border: 7"; |
| 91 |
QString result = image.scaledBorder(border, "0.5"); |
| 92 |
QCOMPARE(result, expected); |
| 93 |
} |
| 94 |
|
| 95 |
void scaledBorderDouble() { |
| 96 |
UCQQuickImageExtension image; |
| 97 |
QString border = "border: 13"; |
| 98 |
QString expected = "border: 26"; |
| 99 |
QString result = image.scaledBorder(border, "2"); |
| 100 |
QCOMPARE(result, expected); |
| 101 |
} |
| 102 |
|
| 103 |
void rewriteContainsBorderInName() { |
| 104 |
UCQQuickImageExtension image; |
| 105 |
QString sciFilePath = "data/borderInName.sci"; |
| 106 |
QString result; |
| 107 |
|
| 108 |
QTextStream resultStream(&result); |
| 109 |
image.rewriteSciFile(sciFilePath, "1", resultStream); |
| 110 |
|
| 111 |
QString expected; |
| 112 |
QTextStream expectedStream(&expected); |
| 113 |
expectedStream << "source: \"image://scaling/1/data/borderInName.png\"" |
| 114 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 115 |
<< endl; |
| 116 |
#else |
| 117 |
<< Qt::endl; |
| 118 |
#endif |
| 119 |
expectedStream << "border.left: 9" |
| 120 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 121 |
<< endl; |
| 122 |
#else |
| 123 |
<< Qt::endl; |
| 124 |
#endif |
| 125 |
expectedStream << "border.right: 2" |
| 126 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 127 |
<< endl; |
| 128 |
#else |
| 129 |
<< Qt::endl; |
| 130 |
#endif |
| 131 |
expectedStream << "border.top: 9" |
| 132 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 133 |
<< endl; |
| 134 |
#else |
| 135 |
<< Qt::endl; |
| 136 |
#endif |
| 137 |
expectedStream << "border.bottom: 0" |
| 138 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 139 |
<< endl; |
| 140 |
#else |
| 141 |
<< Qt::endl; |
| 142 |
#endif |
| 143 |
expectedStream << "horizontalTileMode: Stretch" |
| 144 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 145 |
<< endl; |
| 146 |
#else |
| 147 |
<< Qt::endl; |
| 148 |
#endif |
| 149 |
expectedStream << "verticalTileMode: Stretch" |
| 150 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 151 |
<< endl; |
| 152 |
#else |
| 153 |
<< Qt::endl; |
| 154 |
#endif |
| 155 |
|
| 156 |
QCOMPARE(result, expected); |
| 157 |
} |
| 158 |
|
| 159 |
void cachingOfRewrittenSciFiles() { |
| 160 |
/* This tests an internal implementation detail of UCQQuickImageExtension, |
| 161 |
namely making sure that only one temporary rewritten .sci file is |
| 162 |
created for each source .sci file. |
| 163 |
*/ |
| 164 |
|
| 165 |
unsigned int initialNumberOfSciFiles = numberOfTemporarySciFiles(); |
| 166 |
|
| 167 |
{ |
| 168 |
DummyFileEngineHandler handler; |
| 169 |
|
| 170 |
QQuickView view(QUrl::fromLocalFile("./data/single_cache_file.qml")); |
| 171 |
|
| 172 |
// Wait until the app is idle |
| 173 |
bool idle = false; |
| 174 |
while (!idle) { |
| 175 |
QEventLoop l; |
| 176 |
idle = !l.processEvents(); |
| 177 |
} |
| 178 |
|
| 179 |
QCOMPARE(numberOfTemporarySciFiles(), initialNumberOfSciFiles + 1); |
| 180 |
} |
| 181 |
|
| 182 |
/* The temporary files will be deleted when the cache is destroyed when |
| 183 |
the application exits. |
| 184 |
*/ |
| 185 |
QCOMPARE(numberOfTemporarySciFiles(), initialNumberOfSciFiles + 1); |
| 186 |
} |
| 187 |
|
| 188 |
void onlyOneStatRepeatedImage() { |
| 189 |
DummyFileEngineHandler handler; |
| 190 |
|
| 191 |
QQuickView view(QUrl::fromLocalFile("./data/hundred_faces.qml")); |
| 192 |
|
| 193 |
// Wait until the app is idle |
| 194 |
bool idle = false; |
| 195 |
while (!idle) { |
| 196 |
QEventLoop l; |
| 197 |
idle = !l.processEvents(); |
| 198 |
} |
| 199 |
// We have actually loaded the image |
| 200 |
QVERIFY(nFaces > 0); |
| 201 |
// The number at the moment of writing this test is 4, but we |
| 202 |
// can't know what Qt does internally, so i'm going for a relatively safe <10 |
| 203 |
// the important part is that is not 100 times (i.e. one for every Image item) |
| 204 |
QVERIFY(nFaces < 10); |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
QTEST_MAIN(tst_UCQQuickImageExtension) |
| 209 |
|
| 210 |
#include "tst_qquick_image_extension.moc" |