on daa5708 help sign in
unity8/tests/plugins/Dash/cardcreatortest.cpp
108 lines 4.0 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
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.
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 General Public License for more details.
12 *
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/>.
15 */
17#include <QDir>
18#include <QQmlEngine>
19#include <QQuickItem>
20#include <QQuickView>
21#include <QtTestGui>
22#include <QDebug>
23#include <QTemporaryFile>
25class CardCreatorTest : public QObject
27 Q_OBJECT
29private Q_SLOTS:
31 void initTestCase()
32 {
33 }
35 void init()
36 {
37 view = new QQuickView();
38 view->engine()->addImportPath(BUILT_PLUGINS_DIR);
39 view->setSource(QUrl::fromLocalFile(DASHVIEWSTEST_FOLDER "/cardcreatortest.qml"));
40 view->show();
41 QTest::qWaitForWindowExposed(view);
42 }
44 void cleanup()
45 {
46 delete view;
47 }
49 void testKnownCases()
50 {
51 const QString templateString("template: ");
52 const QString componentsString("components: ");
53 const QString resultString("result: ");
55 const QString testDirPath = DASHVIEWSTEST_FOLDER "/cardcreator/";
56 QDir d(testDirPath);
57 const QStringList testFiles = d.entryList(QStringList() << "*.tst");
58 foreach(const QString &testFileName, testFiles) {
59 qDebug() << testFileName;
60 QFile testFile(testDirPath + testFileName);
61 QVERIFY(testFile.open(QIODevice::ReadOnly));
62 QTextStream ts(&testFile);
63 const QStringList lines = ts.readAll().split("\n");
65 QVERIFY(lines[0].startsWith(templateString));
66 QVERIFY(lines[1].startsWith(componentsString));
67 QVERIFY(lines[2].startsWith(resultString));
68 const QString templateJSON = lines[0].mid(templateString.length());
69 const QString componentsJSON = lines[1].mid(componentsString.length());
70 const QString resultFileName = lines[2].mid(resultString.length());
72 QVariant cardStringResult;
73 QMetaObject::invokeMethod(view->rootObject(), "cardString", Q_RETURN_ARG(QVariant, cardStringResult), Q_ARG(QVariant, templateJSON), Q_ARG(QVariant, componentsJSON));
75 QFile testResultFile(testDirPath + resultFileName);
76 QVERIFY(testResultFile.open(QIODevice::ReadOnly));
77 QTextStream ts2(&testResultFile);
79 // Record failed results to /tmp
80 const QString executedResult = cardStringResult.toString();
81 QTemporaryFile tmpFile(QDir::tempPath() + QDir::separator() + "testCardCreatorFailedResultXXXXXX");
82 tmpFile.open();
83 tmpFile.setAutoRemove(false);
84 tmpFile.write(executedResult.toUtf8().constData());
86 // Line by line comparison
87 const QStringList expectedLines = ts2.readAll().trimmed().replace(QRegExp("\n\\s*\n"),"\n").split("\n");
88 const QStringList cardStringResultLines = cardStringResult.toString().trimmed().replace(QRegExp("\n\\s*\n"),"\n").split("\n");
89 for (int i = 0; i < expectedLines.size(); ++i) {
90 QCOMPARE(cardStringResultLines[i].simplified(), expectedLines[i].simplified());
91 }
93 // Remove the result if it passed
94 tmpFile.setAutoRemove(true);
96 QVariant createCardComponentResult;
97 QMetaObject::invokeMethod(view->rootObject(), "createCardComponent", Q_RETURN_ARG(QVariant, createCardComponentResult), Q_ARG(QVariant, templateJSON), Q_ARG(QVariant, componentsJSON));
98 QVERIFY(createCardComponentResult.toBool());
99 }
100 }
102private:
103 QQuickView *view;
104};
106QTEST_MAIN(CardCreatorTest)
108#include "cardcreatortest.moc"