on bionic help sign in
unity8/tests/plugins/Wizard/tst_system.cpp
124 lines 2.9 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();
37private:
38 void enable();
39 void disable();
40 bool isEnabled();
42 QTemporaryDir dir;
43 QDir enableDir;
44 QFile enableFile;
45};
47SystemTest::SystemTest()
49 qputenv("HOME", dir.path().toUtf8());
50 enableDir.setPath(dir.path() + "/.config/ubuntu-system-settings");
51 enableFile.setFileName(enableDir.filePath("wizard-has-run"));
54void SystemTest::enable()
56 enableFile.remove();
57 QCOMPARE(isEnabled(), true);
60void SystemTest::disable()
62 enableDir.mkpath(".");
63 enableFile.open(QIODevice::WriteOnly);
64 enableFile.close();
65 QCOMPARE(isEnabled(), false);
68bool SystemTest::isEnabled()
70 return !enableFile.exists();
73void SystemTest::testEnable()
75 disable();
77 System system;
78 QVERIFY(!system.wizardEnabled());
80 system.setWizardEnabled(true);
81 QVERIFY(system.wizardEnabled());
82 QVERIFY(isEnabled());
85void SystemTest::testDisable()
87 enable();
89 System system;
90 QVERIFY(system.wizardEnabled());
92 system.setWizardEnabled(false);
93 QVERIFY(!system.wizardEnabled());
94 QVERIFY(!isEnabled());
97void SystemTest::testNoticeChanges()
99 enable();
101 System system;
102 QSignalSpy spy(&system, SIGNAL(wizardEnabledChanged()));
104 // System only guarantees its signals work correcty when using its own set
105 // methods (i.e. it won't necessarily notice if we modify the file behind
106 // the scenes). This is because watching all parent directories of the
107 // wizard-has-run file with QFileSystemWatcher is a nightmare and waste of
108 // resources for the corner case it is. So we'll just test the set method.
110 system.setWizardEnabled(false);
111 QTRY_COMPARE(spy.count(), 1);
113 system.setWizardEnabled(true);
114 QTRY_COMPARE(spy.count(), 2);
116 system.setWizardEnabled(false);
117 QTRY_COMPARE(spy.count(), 3);
119 system.setWizardEnabled(true);
120 QTRY_COMPARE(spy.count(), 4);
123QTEST_MAIN(SystemTest)
124#include "tst_system.moc"