File: tst_pagelist.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (183 lines) | stat: -rw-r--r-- 5,641 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PageList.h"

#include <QDebug>
#include <QObject>
#include <QTemporaryDir>
#include <QTest>
#include <QSettings>

#define PAGES_PATH "Wizard/Pages"

class PageListTest: public QObject
{
    Q_OBJECT

public:
    PageListTest() {}

private Q_SLOTS:
    void testCollect();
    void testIterate();
    void testIgnoreNonNumbered();
    void testIgnoreNonQml();
    void testIgnoreDuplicates();
    void testDisabled();
    void testSkipUntilLastPage();

private:
    void fillRoot(const QTemporaryDir &root);
    void makeFile(const QTemporaryDir &root, const QString &dir, const QString &filename);
};

void PageListTest::fillRoot(const QTemporaryDir &root)
{
    QVERIFY(root.isValid());
    QDir rootDir = root.path();
    QVERIFY(rootDir.mkpath(QString("a/") + PAGES_PATH));
    QVERIFY(rootDir.mkpath(QString("b/") + PAGES_PATH));
    QVERIFY(rootDir.mkpath(QString("c/") + PAGES_PATH));
    qputenv("XDG_DATA_DIRS", QString(rootDir.path() + "/a:" +
                                     rootDir.path() + "/b:" +
                                     rootDir.path() + "/c").toLatin1());
}

void PageListTest::makeFile(const QTemporaryDir &root, const QString &dir, const QString &filename)
{
    QFile file(root.path() + "/" + dir + "/" + PAGES_PATH + "/" + filename);
    QVERIFY(file.open(QIODevice::WriteOnly));
    file.close();
    QVERIFY(file.exists());
}

void PageListTest::testCollect()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "3.qml");
    makeFile(root, "b", "1.qml");
    makeFile(root, "c", "2.qml");

    PageList pageList;
    QCOMPARE(pageList.entries(), QStringList() << "1.qml" << "2.qml" << "3.qml");
    QCOMPARE(pageList.paths(), QStringList() << root.path() + "/b/" + PAGES_PATH + "/1.qml"
                                             << root.path() + "/c/" + PAGES_PATH + "/2.qml"
                                             << root.path() + "/a/" + PAGES_PATH + "/3.qml");
}

void PageListTest::testIterate()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "1.qml");
    makeFile(root, "a", "2.qml");
    makeFile(root, "a", "3.qml");

    PageList pageList;
    QCOMPARE(pageList.index(), -1);
    QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/1.qml"));
    QCOMPARE(pageList.prev(), QString());
    QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/2.qml"));
    QCOMPARE(pageList.prev(), QString(root.path() + "/a/" + PAGES_PATH + "/1.qml"));
    QCOMPARE(pageList.index(), 0);
    QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/2.qml"));
    QCOMPARE(pageList.next(), QString(root.path() + "/a/" + PAGES_PATH + "/3.qml"));
    QCOMPARE(pageList.index(), 2);
    QCOMPARE(pageList.next(), QString());
    QCOMPARE(pageList.index(), 2);
}

void PageListTest::testIgnoreNonNumbered()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "1.qml");
    makeFile(root, "a", "nope.qml");

    PageList pageList;
    QCOMPARE(pageList.entries(), QStringList() << "1.qml");
}

void PageListTest::testIgnoreNonQml()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "1.qml");
    makeFile(root, "a", "2");
    makeFile(root, "a", "2.txt");

    PageList pageList;
    QCOMPARE(pageList.entries(), QStringList() << "1.qml");
}

void PageListTest::testIgnoreDuplicates()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "1.qml");
    makeFile(root, "b", "1.qml");

    PageList pageList;
    QCOMPARE(pageList.paths(), QStringList() << root.path() + "/a/" + PAGES_PATH + "/1.qml");
}

void PageListTest::testDisabled()
{
    QTemporaryDir root;
    fillRoot(root);
    makeFile(root, "a", "1.qml.disabled"); // before the fact
    makeFile(root, "b", "1.qml");
    makeFile(root, "b", "2.qml");
    makeFile(root, "b", "2.qml.disabled"); // same dir
    makeFile(root, "b", "3.qml");
    makeFile(root, "b", "4.qml"); // only survivor
    makeFile(root, "c", "3.qml.disabled"); // after the fact

    PageList pageList;
    QCOMPARE(pageList.entries(), QStringList() << "4.qml");
}

void PageListTest::testSkipUntilLastPage() {
    PageList * pageList = nullptr;
    QTemporaryDir root;
    fillRoot(root);

    makeFile(root, "a", "1.qml");
    makeFile(root, "a", "2.qml");
    makeFile(root, "a", "3.qml");

    // normal run
    pageList = new PageList;
    QCOMPARE(pageList->numPages(), 3);
    delete pageList; pageList = nullptr;

    // after system update had been installed, have the last page only
    QSettings settings;
    settings.setValue(QStringLiteral("Wizard/SkipUntilFinishedPage"), true);
    pageList = new PageList;
    QCOMPARE(pageList->entries(), {QStringLiteral("3.qml")}); // only the last page should be in the list
    delete pageList; pageList = nullptr;

    // normal run again
    pageList = new PageList;
    QCOMPARE(pageList->numPages(), 3);
    delete pageList; pageList = nullptr;
}

QTEST_MAIN(PageListTest)
#include "tst_pagelist.moc"