on c07aa7e help sign in
unity8/tests/wizard/tst_pagelist.cpp
156 lines 4.8 kB history raw
1/*
2 * This file is part of system-settings
3 *
4 * Copyright (C) 2014 Canonical Ltd.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 3, as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranties of
12 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "PageList.h"
21#include <QDebug>
22#include <QObject>
23#include <QTemporaryDir>
24#include <QTest>
26#define PAGES_PATH "ubuntu/settings/wizard/qml/Pages"
28class PageListTest: public QObject
30 Q_OBJECT
32public:
33 PageListTest() {};
35private Q_SLOTS:
36 void testCollect();
37 void testIterate();
38 void testIgnoreNonNumbered();
39 void testIgnoreNonQml();
40 void testIgnoreDuplicates();
41 void testDisabled();
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(), root.path() + "/a/" + PAGES_PATH + "/1.qml");
94 QCOMPARE(pageList.prev(), QString());
95 QCOMPARE(pageList.next(), root.path() + "/a/" + PAGES_PATH + "/2.qml");
96 QCOMPARE(pageList.prev(), root.path() + "/a/" + PAGES_PATH + "/1.qml");
97 QCOMPARE(pageList.index(), 0);
98 QCOMPARE(pageList.next(), root.path() + "/a/" + PAGES_PATH + "/2.qml");
99 QCOMPARE(pageList.next(), 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");
155QTEST_MAIN(PageListTest)
156#include "tst_pagelist.moc"