2 * Copyright (C) 2016 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
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 General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17#include <QCoreApplication>
19#include <QImageReader>
21#include <QTemporaryDir>
23#include <private/qquickimage_p.h>
29Q_DECLARE_METATYPE(QQuickImage::Status)
31class ImageCacheTest : public QObject
37 void setUpImage(const QString &name, const QSize &sourceSize = QSize())
39 QString url = "image://unity8imagecache/file://" + sourceFile(name);
40 image->setProperty("sourceSize", sourceSize);
41 image->setProperty("source", url);
44 void waitForImage(QQuickImage::Status status = QQuickImage::Ready)
46 QTRY_COMPARE(image->property("status"), QVariant((int)status));
49 QSize cachedImageSize(const QString &filename)
51 QImageReader reader(filename);
55 QString sourceFile(const QString &name)
57 return testDataDir() + "/" TEST_DIR "/graphics/" + name;
60 QString cachedFile(bool isPath, const QString &name)
63 return home->path() + "/.cache/unity8/imagecache/paths" + sourceFile(name);
65 return home->path() + "/.cache/unity8/imagecache/names/" + name;
68 void createCachedImage(const QString &name, const QString &cachePath, const QSize &cacheSize, time_t mtime_in = 0)
70 QString origPath = sourceFile(name);
71 QImageReader origImage(origPath);
72 origImage.setScaledSize(cacheSize);
73 auto format = origImage.format(); // can't get this after reading
75 QImage cacheImage(origImage.read());
76 QVERIFY(!cacheImage.isNull());
78 QFileInfo(cachePath).dir().mkpath(QStringLiteral("."));
79 QVERIFY(cacheImage.save(cachePath, format, 100));
81 // Make file a few seconds old, so we can more easily tell if it gets updated
82 auto mtime = mtime_in == 0 ? QFileInfo(cachePath).lastModified().toTime_t() - 5 : mtime_in;
83 struct utimbuf timebuffer;
84 timebuffer.modtime = mtime;
85 timebuffer.actime = timebuffer.modtime;
86 QCOMPARE(utime(cachePath.toUtf8().data(), &timebuffer), 0);
93 home = new QTemporaryDir();
94 QVERIFY(home->isValid());
95 qputenv("HOME", home->path().toUtf8());
97 view = new QQuickView();
98 view->setSource(QUrl::fromLocalFile(testDataDir() + "/" TEST_DIR "/test.qml"));
99 image = view->rootObject();
102 QTest::qWaitForWindowExposed(view);
104 waitForImage(QQuickImage::Null);
113 void testFileNotFound()
115 setUpImage("NOTHERE");
116 waitForImage(QQuickImage::Error);
117 QVERIFY(!QFile::exists(cachedFile(true, "NOTHERE")));
120 void testNoSourceSize()
122 setUpImage("wide.jpg");
124 QVERIFY(!QFile::exists(cachedFile(true, "wide.jpg")));
127 void testNoScalingUp()
129 setUpImage("wide.jpg", QSize(1000, 1000));
131 QVERIFY(!QFile::exists(cachedFile(true, "wide.jpg")));
134 void testFullSourceSize()
136 setUpImage("wide.jpg", QSize(100, 100));
138 QCOMPARE(cachedImageSize(cachedFile(true, "wide.jpg")), QSize(100, 100));
141 void testWidthSourceSize()
143 setUpImage("wide.jpg", QSize(100, 0));
145 // wide.jpg is 500x200
146 QCOMPARE(cachedImageSize(cachedFile(true, "wide.jpg")), QSize(100, 40));
149 void testHeightSourceSize()
151 setUpImage("wide.jpg", QSize(0, 100));
153 // wide.jpg is 500x200
154 QCOMPARE(cachedImageSize(cachedFile(true, "wide.jpg")), QSize(250, 100));
159 setUpImage("wide.jpg?name=foo", QSize(0, 100));
161 QVERIFY(!QFile::exists(cachedFile(true, ""))); // check for dir itself
162 QVERIFY(QFile::exists(cachedFile(false, "foo")));
167 auto cacheName = cachedFile(false, "foo");
168 createCachedImage("wide.jpg", cacheName, QSize(250, 100));
169 auto mtime = QFileInfo(cacheName).lastModified();
171 auto now = time(NULL);
172 QVERIFY(QFileInfo(cacheName).lastModified().toTime_t() < now); // sanity check
174 setUpImage("wide.jpg?name=foo", QSize(0, 100));
176 QCOMPARE(QFileInfo(cacheName).lastModified(), mtime); // wasn't recreated
179 void testDifferentSize()
181 auto cacheName = cachedFile(false, "foo");
182 createCachedImage("wide.jpg", cacheName, QSize(250, 100));
184 auto now = time(NULL);
185 QVERIFY(QFileInfo(cacheName).lastModified().toTime_t() < now); // sanity check
187 setUpImage("wide.jpg?name=foo", QSize(100, 0));
189 QVERIFY(QFileInfo(cacheName).lastModified().toTime_t() >= now); // was recreated
192 void testStaleCache()
194 auto sourceName = sourceFile("wide.jpg");
195 auto mtime = QFileInfo(sourceName).lastModified().toTime_t() - 5;
197 auto cacheName = cachedFile(false, "foo");
198 createCachedImage("wide.jpg", cacheName, QSize(250, 100), mtime);
200 auto now = time(NULL);
201 QVERIFY(QFileInfo(cacheName).lastModified().toTime_t() < now); // sanity check
203 setUpImage("wide.jpg?name=foo", QSize(0, 100));
205 QVERIFY(QFileInfo(cacheName).lastModified().toTime_t() >= now); // was recreated
214QTEST_MAIN(ImageCacheTest)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list