on 101c302 help sign in
unity8/tests/plugins/Wizard/tst_system.cpp
156 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 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 "System.h"
19#include <QDebug>
20#include <QObject>
21#include <QSignalSpy>
22#include <QTemporaryDir>
23#include <QTest>
25class SystemTest: public QObject
27 Q_OBJECT
29public:
30 SystemTest();
32private Q_SLOTS:
33 void testEnable();
34 void testDisable();
35 void testNoticeChanges();
36 void testUpdate();
38private:
39 void enable();
40 void enableUpdateOnly();
41 void disable();
42 bool isEnabled();
43 bool updateIsEnabled();
45 QTemporaryDir dir;
46 QDir enableDir;
47 QFile enableUpdateWizardFile;
48};
50SystemTest::SystemTest()
52 System system;
53 qputenv("HOME", dir.path().toUtf8());
54 enableDir.setPath(dir.path() + "/.config/ubuntu-system-settings/wizard-has-run/");
55 QString updateFileName = enableDir.filePath(QString::number(system.CURRENT_VERSION));
56 enableUpdateWizardFile.setFileName(updateFileName);
59void SystemTest::enable()
61 enableDir.removeRecursively();
62 QCOMPARE(isEnabled(), true);
65void SystemTest::disable()
67 enableDir.mkpath(".");
68 enableUpdateWizardFile.open(QIODevice::WriteOnly);
69 enableUpdateWizardFile.close();
70 QCOMPARE(isEnabled(), false);
71 QCOMPARE(updateIsEnabled(), false);
74bool SystemTest::isEnabled()
76 return !enableDir.exists();
79void SystemTest::enableUpdateOnly()
81 disable();
82 enableUpdateWizardFile.remove();
83 QCOMPARE(updateIsEnabled(), true);
86bool SystemTest::updateIsEnabled()
88 return !enableUpdateWizardFile.exists();
91void SystemTest::testEnable()
93 disable();
95 System system;
96 QVERIFY(!system.wizardEnabled());
98 system.setWizardEnabled(true);
99 QVERIFY(system.wizardEnabled());
100 QVERIFY(isEnabled());
103void SystemTest::testDisable()
105 enable();
107 System system;
108 QVERIFY(system.wizardEnabled());
110 system.setWizardEnabled(false);
111 QVERIFY(!system.wizardEnabled());
112 QVERIFY(!isEnabled());
115void SystemTest::testUpdate()
117 enableUpdateOnly();
119 System system;
120 QVERIFY(system.wizardEnabled());
121 QVERIFY(system.versionToShow() == system.CURRENT_VERSION);
123 system.setWizardEnabled(false);
124 QVERIFY(!system.wizardEnabled());
125 QVERIFY(!isEnabled());
126 QVERIFY(!updateIsEnabled());
129void SystemTest::testNoticeChanges()
131 enable();
133 System system;
134 QSignalSpy spy(&system, SIGNAL(wizardEnabledChanged()));
136 // System only guarantees its signals work correcty when using its own set
137 // methods (i.e. it won't necessarily notice if we modify the file behind
138 // the scenes). This is because watching all parent directories of the
139 // wizard-has-run file with QFileSystemWatcher is a nightmare and waste of
140 // resources for the corner case it is. So we'll just test the set method.
142 system.setWizardEnabled(false);
143 QTRY_COMPARE(spy.count(), 1);
145 system.setWizardEnabled(true);
146 QTRY_COMPARE(spy.count(), 2);
148 system.setWizardEnabled(false);
149 QTRY_COMPARE(spy.count(), 3);
151 system.setWizardEnabled(true);
152 QTRY_COMPARE(spy.count(), 4);
155QTEST_MAIN(SystemTest)
156#include "tst_system.moc"