aboutsummaryrefslogtreecommitdiffstats
path: root/tests/testqPrefUpdateManager.cpp
blob: 8e3047fdb7d44afba8a54a384289a2c3452458fd (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: GPL-2.0
#include "testqPrefUpdateManager.h"

#include "core/pref.h"
#include "core/qthelper.h"
#include "core/settings/qPrefUpdateManager.h"

#include <QTest>
#include <QSignalSpy>

void TestQPrefUpdateManager::initTestCase()
{
	QCoreApplication::setOrganizationName("Subsurface");
	QCoreApplication::setOrganizationDomain("subsurface.hohndel.org");
	QCoreApplication::setApplicationName("SubsurfaceTestQPrefUpdateManager");
}

void TestQPrefUpdateManager::test_struct_get()
{
	// Test struct pref -> get func.

	auto tst = qPrefUpdateManager::instance();

	prefs.update_manager.dont_check_for_updates = true;
	prefs.update_manager.dont_check_exists = true;
	prefs.update_manager.last_version_used = copy_qstring("last_version");
	prefs.update_manager.next_check = QDate::fromString("11/09/1957", "dd/MM/yyyy").toJulianDay();

	QCOMPARE(tst->dont_check_for_updates(), true);
	QCOMPARE(tst->dont_check_exists(), true);
	QCOMPARE(tst->last_version_used(), QString("last_version"));
	QCOMPARE(tst->next_check(), QDate::fromString("11/09/1957", "dd/MM/yyyy")); 
}

void TestQPrefUpdateManager::test_set_struct()
{
	// Test set func -> struct pref

	auto tst = qPrefUpdateManager::instance();

	tst->set_dont_check_for_updates(false);
	tst->set_dont_check_exists(false);
	tst->set_last_version_used("last_version2");
	tst->set_next_check(QDate::fromString("11/09/1957", "dd/MM/yyyy")); 
	tst->set_uuidString("uuid");

	QCOMPARE(prefs.update_manager.dont_check_for_updates, false);
	QCOMPARE(prefs.update_manager.dont_check_exists, false);
	QCOMPARE(QString(prefs.update_manager.last_version_used), QString("last_version2"));
	QCOMPARE(QDate::fromJulianDay(prefs.update_manager.next_check), QDate::fromString("11/09/1957", "dd/MM/yyyy")); 
	QCOMPARE(tst->uuidString(), QString("uuid"));
}

void TestQPrefUpdateManager::test_set_load_struct()
{
	// test set func -> load -> struct pref

	auto tst = qPrefUpdateManager::instance();

	// secure set_ stores on disk
	prefs.update_manager.dont_check_for_updates = true;
	prefs.update_manager.dont_check_exists = true;
	prefs.update_manager.next_check = 100;

	tst->set_dont_check_for_updates(false);
	tst->set_dont_check_exists(false);
	tst->set_last_version_used("last_version2");
	tst->set_next_check(QDate::fromString("11/09/1957", "dd/MM/yyyy")); 
	tst->set_uuidString("uuid2");

	prefs.update_manager.dont_check_for_updates = true;
	prefs.update_manager.dont_check_exists = true;
	prefs.update_manager.last_version_used = copy_qstring("last_version");
	prefs.update_manager.next_check = 1000;

	tst->load();
	QCOMPARE(prefs.update_manager.dont_check_for_updates, false);
	QCOMPARE(QString(prefs.update_manager.last_version_used), QString("last_version2"));
	QCOMPARE(QDate::fromJulianDay(prefs.update_manager.next_check), QDate::fromString("11/09/1957", "dd/MM/yyyy")); 
	QCOMPARE(tst->uuidString(), QString("uuid2"));

	// dont_check_exists is NOT stored on disk
	QCOMPARE(prefs.update_manager.dont_check_exists, true);
}

void TestQPrefUpdateManager::test_struct_disk()
{
	// test struct prefs -> disk

	auto tst = qPrefUpdateManager::instance();

	prefs.update_manager.dont_check_for_updates = true;
	prefs.update_manager.dont_check_exists = true;
	prefs.update_manager.last_version_used = copy_qstring("last_version");
	prefs.update_manager.next_check = QDate::fromString("11/09/1957", "dd/MM/yyyy").toJulianDay();

	tst->sync();
	prefs.update_manager.dont_check_for_updates = false;
	prefs.update_manager.dont_check_exists = false;
	prefs.update_manager.last_version_used = copy_qstring("");
	prefs.update_manager.next_check = 1000;

	tst->load();
	QCOMPARE(tst->dont_check_for_updates(), true);
	QCOMPARE(tst->last_version_used(), QString("last_version"));
	QCOMPARE(tst->next_check(), QDate::fromString("11/09/1957", "dd/MM/yyyy"));

	// dont_check_exists is NOT stored on disk
	QCOMPARE(tst->dont_check_exists(), false);
}

void TestQPrefUpdateManager::test_multiple()
{
	// test multiple instances have the same information

	prefs.update_manager.dont_check_for_updates = false;

	prefs.update_manager.dont_check_exists = false;
	auto tst = qPrefUpdateManager::instance();

	QCOMPARE(tst->dont_check_for_updates(), qPrefUpdateManager::dont_check_for_updates());
	QCOMPARE(tst->dont_check_for_updates(), false);
	QCOMPARE(tst->dont_check_exists(), qPrefUpdateManager::dont_check_exists());
	QCOMPARE(qPrefUpdateManager::dont_check_exists(), false);
}

void TestQPrefUpdateManager::test_next_check()
{
	auto tst = qPrefUpdateManager::instance();

	prefs.update_manager.next_check = QDate::fromString("11/09/1957", "dd/MM/yyyy").toJulianDay();
	prefs.update_manager.next_check++;

	QCOMPARE(tst->next_check(), QDate::fromString("12/09/1957", "dd/MM/yyyy")); 
}

#define TEST(METHOD, VALUE)      \
	QCOMPARE(METHOD, VALUE); \
	update->sync();           \
	update->load();           \
	QCOMPARE(METHOD, VALUE);

void TestQPrefUpdateManager::test_oldPreferences()
{
	auto update = qPrefUpdateManager::instance();
	QDate date = QDate::currentDate();

	update->set_dont_check_for_updates(true);
	update->set_last_version_used("tomaz-1");
	update->set_next_check(date);

	TEST(update->dont_check_for_updates(), true);
	TEST(update->last_version_used(), QStringLiteral("tomaz-1"));
	TEST(update->next_check(), date);

	date = date.addDays(3);
	update->set_dont_check_for_updates(false);
	update->set_last_version_used("tomaz-2");
	update->set_next_check(date);

	TEST(update->dont_check_for_updates(), false);
	TEST(update->last_version_used(), QStringLiteral("tomaz-2"));
	TEST(update->next_check(), date);
}

void TestQPrefUpdateManager::test_signals()
{
	QSignalSpy spy1(qPrefUpdateManager::instance(), &qPrefUpdateManager::dont_check_for_updatesChanged);
	QSignalSpy spy2(qPrefUpdateManager::instance(), &qPrefUpdateManager::dont_check_existsChanged);
	QSignalSpy spy3(qPrefUpdateManager::instance(), &qPrefUpdateManager::last_version_usedChanged);
	QSignalSpy spy4(qPrefUpdateManager::instance(), &qPrefUpdateManager::next_checkChanged);
	QSignalSpy spy5(qPrefUpdateManager::instance(), &qPrefUpdateManager::uuidStringChanged);

	prefs.update_manager.dont_check_for_updates = true;
	qPrefUpdateManager::set_dont_check_for_updates(false);
	prefs.update_manager.dont_check_exists = true;
	qPrefUpdateManager::set_dont_check_exists(false);
	qPrefUpdateManager::set_last_version_used("signal last_version2");
	qPrefUpdateManager::set_next_check(QDate::fromString("11/09/1967", "dd/MM/yyyy")); 
	qPrefUpdateManager::set_uuidString("signal uuid");

	QCOMPARE(spy1.count(), 1);
	QCOMPARE(spy2.count(), 1);
	QCOMPARE(spy3.count(), 1);
	QCOMPARE(spy4.count(), 1);
	QCOMPARE(spy5.count(), 1);

	QVERIFY(spy1.takeFirst().at(0).toBool() == false);
	QVERIFY(spy2.takeFirst().at(0).toBool() == false);
	QVERIFY(spy3.takeFirst().at(0).toString() == "signal last_version2");
	QVERIFY(spy4.takeFirst().at(0).toDate() == QDate::fromString("11/09/1967", "dd/MM/yyyy")); 
	QVERIFY(spy5.takeFirst().at(0).toString() == "signal uuid");
}


QTEST_MAIN(TestQPrefUpdateManager)