From 3af18224190d2c008a4ed6fe39b07e60db5dc683 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 3 May 2017 14:58:27 +0200 Subject: tst_snippets: take screenshots only when requested The component loading code has been adjusted so that it can load snippets that use either a Window root element too. This speeds up the test a lot in the CI, and allows us to flatten the snippet-structure. Set SCREENSHOTS=1 environment variable to take screenshots. Change-Id: Ibd9e76befe62044dd1374899f18ea3d8c7ad454b Reviewed-by: Mitch Curtis --- tests/auto/snippets/tst_snippets.cpp | 104 +++++++++++++++-------------------- 1 file changed, 43 insertions(+), 61 deletions(-) (limited to 'tests/auto/snippets') diff --git a/tests/auto/snippets/tst_snippets.cpp b/tests/auto/snippets/tst_snippets.cpp index ab3a94bc..97b10fe1 100644 --- a/tests/auto/snippets/tst_snippets.cpp +++ b/tests/auto/snippets/tst_snippets.cpp @@ -49,13 +49,11 @@ private slots: void verify(); void verify_data(); - void screenshots(); - void screenshots_data(); - private: - QQuickView view; + void loadSnippet(const QString &source); + + bool takeScreenshots; QMap snippetPaths; - QMap screenshotSnippetPaths; }; static QMap findSnippets(const QDir &inputDir, const QDir &outputDir = QDir()) @@ -77,86 +75,70 @@ void tst_Snippets::initTestCase() QDir snippetsDir(QQC2_SNIPPETS_PATH); QVERIFY(!snippetsDir.path().isEmpty()); - snippetPaths = findSnippets(snippetsDir); - QVERIFY(!snippetPaths.isEmpty()); + QDir screenshotsDir(QDir::current().filePath("screenshots")); - QDir screenshotOutputDir(QDir::current().filePath("screenshots")); - QVERIFY(screenshotOutputDir.exists() || QDir::current().mkpath("screenshots")); + takeScreenshots = qgetenv("SCREENSHOTS").toInt(); + if (takeScreenshots) + QVERIFY(screenshotsDir.exists() || QDir::current().mkpath("screenshots")); - QDir screenshotSnippetsDir(QQC2_SNIPPETS_PATH "/screenshots"); - QVERIFY(!screenshotSnippetsDir.path().isEmpty()); - - screenshotSnippetPaths = findSnippets(screenshotSnippetsDir, screenshotOutputDir); - QVERIFY(!screenshotSnippetPaths.isEmpty()); + snippetPaths = findSnippets(snippetsDir, screenshotsDir); + QVERIFY(!snippetPaths.isEmpty()); } Q_DECLARE_METATYPE(QList) -static void loadSnippet(QQuickView *view, const QString &source) +void tst_Snippets::verify() { + QFETCH(QString, input); + QFETCH(QString, output); + + QQmlEngine engine; + QQmlComponent component(&engine); + qRegisterMetaType >(); - QSignalSpy warnings(view->engine(), SIGNAL(warnings(QList))); + QSignalSpy warnings(&engine, SIGNAL(warnings(QList))); QVERIFY(warnings.isValid()); - QUrl url = QUrl::fromLocalFile(source); - QQmlComponent *component = new QQmlComponent(view->engine(), view); - component->loadUrl(url); + QUrl url = QUrl::fromLocalFile(input); + component.loadUrl(url); - QObject *root = component->beginCreate(view->rootContext()); + QObject *root = component.create(); QVERIFY(root); - view->setContent(url, component, root); - component->completeCreate(); - QCOMPARE(view->status(), QQuickView::Ready); - QVERIFY(view->errors().isEmpty()); - QVERIFY(view->rootObject()); + QCOMPARE(component.status(), QQmlComponent::Ready); + QVERIFY(component.errors().isEmpty()); QVERIFY(warnings.isEmpty()); -} - -void tst_Snippets::verify() -{ - QFETCH(QString, input); - loadSnippet(&view, input); + if (takeScreenshots) { + QQuickWindow *window = qobject_cast(root); + if (!window) { + QQuickView *view = new QQuickView; + view->setContent(url, &component, root); + window = view; + } + + window->show(); + window->requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QSharedPointer result = window->contentItem()->grabToImage(); + QSignalSpy spy(result.data(), SIGNAL(ready())); + QVERIFY(spy.isValid()); + QVERIFY(spy.wait()); + QVERIFY(result->saveToFile(output)); + + window->close(); + } } void tst_Snippets::verify_data() -{ - QTest::addColumn("input"); - - QMap::const_iterator it; - for (it = snippetPaths.constBegin(); it != snippetPaths.constEnd(); ++it) - QTest::newRow(qPrintable(it.key())) << it.value().first; -} - -void tst_Snippets::screenshots() -{ - QFETCH(QString, input); - QFETCH(QString, output); - - loadSnippet(&view, input); - - view.show(); - view.requestActivate(); - QVERIFY(QTest::qWaitForWindowActive(&view)); - - QSharedPointer result = view.contentItem()->grabToImage(); - QSignalSpy spy(result.data(), SIGNAL(ready())); - QVERIFY(spy.isValid()); - QVERIFY(spy.wait()); - QVERIFY(result->saveToFile(output)); - - view.close(); -} - -void tst_Snippets::screenshots_data() { QTest::addColumn("input"); QTest::addColumn("output"); QMap::const_iterator it; - for (it = screenshotSnippetPaths.constBegin(); it != screenshotSnippetPaths.constEnd(); ++it) + for (it = snippetPaths.constBegin(); it != snippetPaths.constEnd(); ++it) QTest::newRow(qPrintable(it.key())) << it.value().first << it.value().second; } -- cgit v1.2.3