on ec520e2 help sign in
unity8/tests/plugins/Dash/cardcreatortest.cpp
99 lines 3.7 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 <QTemporaryFile>
24class CardCreatorTest : public QObject
26 Q_OBJECT
28private Q_SLOTS:
30 void initTestCase()
31 {
32 }
34 void init()
35 {
36 view = new QQuickView();
37 view->engine()->addImportPath(BUILT_PLUGINS_DIR);
38 view->setSource(QUrl::fromLocalFile(DASHVIEWSTEST_FOLDER "/cardcreatortest.qml"));
39 view->show();
40 QTest::qWaitForWindowExposed(view);
41 }
43 void cleanup()
44 {
45 delete view;
46 }
48 void testKnownCases()
49 {
50 const QString templateString("template: ");
51 const QString componentsString("components: ");
52 const QString resultString("result: ");
54 const QString testDirPath = DASHVIEWSTEST_FOLDER "/cardcreator/";
55 QDir d(testDirPath);
56 const QStringList testFiles = d.entryList(QStringList() << "*.tst");
57 foreach(const QString &testFileName, testFiles) {
58 qDebug() << testFileName;
59 QFile testFile(testDirPath + testFileName);
60 QVERIFY(testFile.open(QIODevice::ReadOnly));
61 QTextStream ts(&testFile);
62 const QStringList lines = ts.readAll().split("\n");
64 QVERIFY(lines[0].startsWith(templateString));
65 QVERIFY(lines[1].startsWith(componentsString));
66 QVERIFY(lines[2].startsWith(resultString));
67 const QString templateJSON = lines[0].mid(templateString.length());
68 const QString componentsJSON = lines[1].mid(componentsString.length());
69 const QString resultFileName = lines[2].mid(resultString.length());
71 QVariant cardStringResult;
72 QMetaObject::invokeMethod(view->rootObject(), "cardString", Q_RETURN_ARG(QVariant, cardStringResult), Q_ARG(QVariant, templateJSON), Q_ARG(QVariant, componentsJSON));
74 QFile testResultFile(testDirPath + resultFileName);
75 QVERIFY(testResultFile.open(QIODevice::ReadOnly));
76 QTextStream ts2(&testResultFile);
77 const QString expectedResult = ts2.readAll();
78 const QString executedResult = cardStringResult.toString();
79 // Record failed results to /tmp
80 QTemporaryFile tmpFile(QDir::tempPath() + QDir::separator() + "testCardCreatorFailedResultXXXXXX");
81 tmpFile.open();
82 tmpFile.setAutoRemove(false);
83 tmpFile.write(executedResult.toUtf8().constData());
84 QCOMPARE(executedResult.simplified(), expectedResult.simplified());
85 tmpFile.setAutoRemove(true); // Remove the result if it passed
87 QVariant createCardComponentResult;
88 QMetaObject::invokeMethod(view->rootObject(), "createCardComponent", Q_RETURN_ARG(QVariant, createCardComponentResult), Q_ARG(QVariant, templateJSON), Q_ARG(QVariant, componentsJSON));
89 QVERIFY(createCardComponentResult.toBool());
90 }
91 }
93private:
94 QQuickView *view;
95};
97QTEST_MAIN(CardCreatorTest)
99#include "cardcreatortest.moc"