on 7d7cfae help sign in
unity8/tests/plugins/Wizard/tst_pagelist.cpp
183 lines 5.6 kB history raw
1/*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "PageList.h"
19#include <QDebug>
20#include <QObject>
21#include <QTemporaryDir>
22#include <QTest>
23#include <QSettings>
25#define PAGES_PATH "Wizard/Pages"
27class PageListTest: public QObject
29 Q_OBJECT
31public:
32 PageListTest() {}
34private Q_SLOTS:
35 void testCollect();
36 void testIterate();
37 void testIgnoreNonNumbered();
38 void testIgnoreNonQml();
39 void testIgnoreDuplicates();
40 void testDisabled();
41 void testSkipUntilLastPage();
43private:
44 void fillRoot(const QTemporaryDir &root);
45 void makeFile(const QTemporaryDir &root, const QString &dir, const QString &filename);
46};
48void PageListTest::fillRoot(const QTemporaryDir &root)
50 QVERIFY(root.isValid());
51 QDir rootDir = root.path();
52 QVERIFY(rootDir.mkpath(QString("a/") + PAGES_PATH));
53 QVERIFY(rootDir.mkpath(QString("b/") + PAGES_PATH));
54 QVERIFY(rootDir.mkpath(QString("c/") + PAGES_PATH));
55 qputenv("XDG_DATA_DIRS", QString(rootDir.path() + "/a:" +
56 rootDir.path() + "/b:" +
57 rootDir.path() + "/c").toLatin1());
60void PageListTest::makeFile(const QTemporaryDir &root, const QString &dir, const QString &filename)
62 QFile file(root.path() + "/" + dir + "/" + PAGES_PATH + "/" + filename);
63 QVERIFY(file.open(QIODevice::WriteOnly));
64 file.close();
65 QVERIFY(file.exists());
68void PageListTest::testCollect()
70 QTemporaryDir root;
71 fillRoot(root);
72 makeFile(root, "a", "3.qml");
73 makeFile(root, "b", "1.qml");
74 makeFile(root, "c", "2.qml");
76 PageList pageList;
77 QCOMPARE(pageList.entries(), QStringList() << "1.qml" << "2.qml" << "3.qml");
78 QCOMPARE(pageList.paths(), QStringList() << root.path() + "/b/" + PAGES_PATH + "/1.qml"
79 << root.path() + "/c/" + PAGES_PATH + "/2.qml"
80 << root.path() + "/a/" + PAGES_PATH + "/3.qml");
83void PageListTest::testIterate()
85 QTemporaryDir root;
86 fillRoot(root);
87 makeFile(root, "a", "1.qml");
88 makeFile(root, "a", "2.qml");
89 makeFile(root, "a", "3.qml");
91 PageList pageList;
92 QCOMPARE(pageList.index(), -1);
93 QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/1.qml"));
94 QCOMPARE(pageList.prev(), QString());
95 QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/2.qml"));
96 QCOMPARE(pageList.prev(), QString(root.path() + "/a/" + PAGES_PATH + "/1.qml"));
97 QCOMPARE(pageList.index(), 0);
98 QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/2.qml"));
99 QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/3.qml"));
100 QCOMPARE(pageList.index(), 2);
101 QCOMPARE(pageList.next(), QString());
102 QCOMPARE(pageList.index(), 2);
105void PageListTest::testIgnoreNonNumbered()
107 QTemporaryDir root;
108 fillRoot(root);
109 makeFile(root, "a", "1.qml");
110 makeFile(root, "a", "nope.qml");
112 PageList pageList;
113 QCOMPARE(pageList.entries(), QStringList() << "1.qml");
116void PageListTest::testIgnoreNonQml()
118 QTemporaryDir root;
119 fillRoot(root);
120 makeFile(root, "a", "1.qml");
121 makeFile(root, "a", "2");
122 makeFile(root, "a", "2.txt");
124 PageList pageList;
125 QCOMPARE(pageList.entries(), QStringList() << "1.qml");
128void PageListTest::testIgnoreDuplicates()
130 QTemporaryDir root;
131 fillRoot(root);
132 makeFile(root, "a", "1.qml");
133 makeFile(root, "b", "1.qml");
135 PageList pageList;
136 QCOMPARE(pageList.paths(), QStringList() << root.path() + "/a/" + PAGES_PATH + "/1.qml");
139void PageListTest::testDisabled()
141 QTemporaryDir root;
142 fillRoot(root);
143 makeFile(root, "a", "1.qml.disabled"); // before the fact
144 makeFile(root, "b", "1.qml");
145 makeFile(root, "b", "2.qml");
146 makeFile(root, "b", "2.qml.disabled"); // same dir
147 makeFile(root, "b", "3.qml");
148 makeFile(root, "b", "4.qml"); // only survivor
149 makeFile(root, "c", "3.qml.disabled"); // after the fact
151 PageList pageList;
152 QCOMPARE(pageList.entries(), QStringList() << "4.qml");
155void PageListTest::testSkipUntilLastPage() {
156 PageList * pageList = nullptr;
157 QTemporaryDir root;
158 fillRoot(root);
160 makeFile(root, "a", "1.qml");
161 makeFile(root, "a", "2.qml");
162 makeFile(root, "a", "3.qml");
164 // normal run
165 pageList = new PageList;
166 QCOMPARE(pageList->numPages(), 3);
167 delete pageList; pageList = nullptr;
169 // after system update had been installed, have the last page only
170 QSettings settings;
171 settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
172 pageList = new PageList;
173 QCOMPARE(pageList->entries(), {QStringLiteral("3.qml")}); // only the last page should be in the list
174 delete pageList; pageList = nullptr;
176 // normal run again
177 pageList = new PageList;
178 QCOMPARE(pageList->numPages(), 3);
179 delete pageList; pageList = nullptr;
182QTEST_MAIN(PageListTest)
183#include "tst_pagelist.moc"