diff options
Diffstat (limited to 'tests/auto/quick/scenegraph/tst_scenegraph.cpp')
-rw-r--r-- | tests/auto/quick/scenegraph/tst_scenegraph.cpp | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/tests/auto/quick/scenegraph/tst_scenegraph.cpp b/tests/auto/quick/scenegraph/tst_scenegraph.cpp new file mode 100644 index 0000000000..cfa491ce97 --- /dev/null +++ b/tests/auto/quick/scenegraph/tst_scenegraph.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qtest.h> + +#include <QtQuick> +#include <private/qquickanimator_p.h> + + + +#include <QtQml> + +class tst_SceneGraph : public QObject +{ + Q_OBJECT + +private slots: + void manyWindows_data(); + void manyWindows(); + +}; + +template <typename T> class ScopedList : public QList<T> { +public: + ~ScopedList() { qDeleteAll(*this); } +}; + +QQuickView *createView(const QString &file, QWindow *parent = 0, int x = -1, int y = -1, int w = -1, int h = -1) +{ + QQuickView *view = new QQuickView(parent); + view->setSource(QUrl::fromLocalFile("data/" + file)); + if (x >= 0 && y >= 0) view->setPosition(x, y); + if (w >= 0 && h >= 0) view->resize(w, h); + view->show(); + return view; +} + +QImage showAndGrab(const QString &file, int w, int h) +{ + QQuickView view; + view.setSource(QUrl::fromLocalFile(QStringLiteral("data/") + file)); + if (w >= 0 && h >= 0) + view.resize(w, h); + view.create(); + return view.grabWindow(); +} + +// Assumes the images are opaque white... +bool containsSomethingOtherThanWhite(const QImage &image) +{ + Q_ASSERT(image.format() == QImage::Format_ARGB32_Premultiplied + || image.format() == QImage::Format_RGB32); + int w = image.width(); + int h = image.height(); + for (int y=0; y<h; ++y) { + const uint *pixels = (const uint *) image.constScanLine(y); + for (int x=0; x<w; ++x) + if (pixels[x] != 0xffffffff) + return true; + } + return false; +} + +// To compare images, we need a special function with a bit of error +// as glyphs has an invisible, but measurable variance depending +// on where in the glyph cache they end up. +bool compareImages(const QImage &ia, const QImage &ib) +{ + if (ia.size() != ib.size()) + qDebug() << "images are of different size" << ia.size() << ib.size(); + Q_ASSERT(ia.size() == ib.size()); + Q_ASSERT(ia.format() == ib.format()); + + int w = ia.width(); + int h = ia.height(); + const int tolerance = 1; + for (int y=0; y<h; ++y) { + const uint *as= (const uint *) ia.constScanLine(y); + const uint *bs= (const uint *) ib.constScanLine(y); + for (int x=0; x<w; ++x) { + uint a = as[x]; + uint b = bs[x]; + + // No tolerance for error in the alpha. + if ((a & 0xff000000) != (b & 0xff000000)) + return false; + if (qAbs(qRed(a) - qRed(b)) > tolerance) + return false; + if (qAbs(qRed(a) - qRed(b)) > tolerance) + return false; + if (qAbs(qRed(a) - qRed(b)) > tolerance) + return false; + } + } + return true; +} + +void tst_SceneGraph::manyWindows_data() +{ + QTest::addColumn<QString>("file"); + QTest::addColumn<bool>("toplevel"); + + QTest::newRow("image,toplevel") << QStringLiteral("manyWindows_image.qml") << true; + QTest::newRow("image,subwindow") << QStringLiteral("manyWindows_image.qml") << false; + QTest::newRow("dftext,toplevel") << QStringLiteral("manyWindows_dftext.qml") << true; + QTest::newRow("dftext,subwindow") << QStringLiteral("manyWindows_dftext.qml") << false; + QTest::newRow("ntext,toplevel") << QStringLiteral("manyWindows_ntext.qml") << true; + QTest::newRow("ntext,subwindow") << QStringLiteral("manyWindows_ntext.qml") << false; + QTest::newRow("rects,toplevel") << QStringLiteral("manyWindows_rects.qml") << true; + QTest::newRow("rects,subwindow") << QStringLiteral("manyWindows_rects.qml") << false; +} + +void tst_SceneGraph::manyWindows() +{ + QFETCH(QString, file); + QFETCH(bool, toplevel); + + QScopedPointer<QWindow> parent; + if (!toplevel) { + parent.reset(new QWindow()); + parent->resize(200, 200); + parent->show(); + } + + ScopedList <QQuickView *> views; + + const int COUNT = 4; + + QImage baseLine; + for (int i=0; i<COUNT; ++i) { + views << createView(file, parent.data(), (i % 2) * 100, (i / 2) * 100, 100, 100); + } + for (int i=0; i<COUNT; ++i) { + QQuickView *view = views.at(i); + QTest::qWaitForWindowExposed(view); + QImage content = view->grabWindow(); + if (i == 0) { + baseLine = content; + QVERIFY(containsSomethingOtherThanWhite(baseLine)); + } else { + QVERIFY(compareImages(content, baseLine)); + } + } + + // Wipe and recreate one (scope pointer delets it...) + delete views.takeLast(); + QQuickView *last = createView(file, parent.data(), 100, 100, 100, 100); + QTest::qWaitForWindowExposed(last); + views << last; + QVERIFY(compareImages(baseLine, last->grabWindow())); + + // Wipe and recreate all + qDeleteAll(views); + views.clear(); + + for (int i=0; i<COUNT; ++i) { + views << createView(file, parent.data(), (i % 2) * 100, (i / 2) * 100, 100, 100); + } + for (int i=0; i<COUNT; ++i) { + QQuickView *view = views.at(i); + QTest::qWaitForWindowExposed(view); + QImage content = view->grabWindow(); + QVERIFY(compareImages(content, baseLine)); + } +} + + +#include "tst_scenegraph.moc" + +QTEST_MAIN(tst_SceneGraph) + |