| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							|  |  |  | #include "testqPrefUpdateManager.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "core/pref.h"
 | 
					
						
							|  |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-08 19:46:11 +02:00
										 |  |  | #include "core/settings/qPrefUpdateManager.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QTest>
 | 
					
						
							| 
									
										
										
										
											2018-08-28 12:33:47 +02:00
										 |  |  | #include <QSignalSpy>
 | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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.last_version_used = copy_qstring("last_version"); | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 	prefs.update_manager.next_check = QDate::fromString("11/09/1957", "dd/MM/yyyy").toJulianDay(); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QCOMPARE(tst->dont_check_for_updates(), true); | 
					
						
							|  |  |  | 	QCOMPARE(tst->last_version_used(), QString("last_version")); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	QCOMPARE(tst->next_check(), QDate::fromString("11/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TestQPrefUpdateManager::test_set_struct() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Test set func -> struct pref
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto tst = qPrefUpdateManager::instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tst->set_dont_check_for_updates(false); | 
					
						
							|  |  |  | 	tst->set_last_version_used("last_version2"); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	tst->set_next_check(QDate::fromString("11/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-19 12:00:01 +02:00
										 |  |  | 	tst->set_uuidString("uuid"); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QCOMPARE(prefs.update_manager.dont_check_for_updates, false); | 
					
						
							|  |  |  | 	QCOMPARE(QString(prefs.update_manager.last_version_used), QString("last_version2")); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	QCOMPARE(QDate::fromJulianDay(prefs.update_manager.next_check), QDate::fromString("11/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-19 12:00:01 +02:00
										 |  |  | 	QCOMPARE(tst->uuidString(), QString("uuid")); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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; | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 	prefs.update_manager.next_check = 100; | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tst->set_dont_check_for_updates(false); | 
					
						
							|  |  |  | 	tst->set_last_version_used("last_version2"); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	tst->set_next_check(QDate::fromString("11/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-19 12:00:01 +02:00
										 |  |  | 	tst->set_uuidString("uuid2"); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	prefs.update_manager.dont_check_for_updates = true; | 
					
						
							|  |  |  | 	prefs.update_manager.last_version_used = copy_qstring("last_version"); | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 	prefs.update_manager.next_check = 1000; | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tst->load(); | 
					
						
							|  |  |  | 	QCOMPARE(prefs.update_manager.dont_check_for_updates, false); | 
					
						
							|  |  |  | 	QCOMPARE(QString(prefs.update_manager.last_version_used), QString("last_version2")); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	QCOMPARE(QDate::fromJulianDay(prefs.update_manager.next_check), QDate::fromString("11/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-19 12:00:01 +02:00
										 |  |  | 	QCOMPARE(tst->uuidString(), QString("uuid2")); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TestQPrefUpdateManager::test_struct_disk() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// test struct prefs -> disk
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto tst = qPrefUpdateManager::instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	prefs.update_manager.dont_check_for_updates = true; | 
					
						
							|  |  |  | 	prefs.update_manager.last_version_used = copy_qstring("last_version"); | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 	prefs.update_manager.next_check = QDate::fromString("11/09/1957", "dd/MM/yyyy").toJulianDay(); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tst->sync(); | 
					
						
							|  |  |  | 	prefs.update_manager.dont_check_for_updates = false; | 
					
						
							|  |  |  | 	prefs.update_manager.last_version_used = copy_qstring(""); | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 	prefs.update_manager.next_check = 1000; | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TestQPrefUpdateManager::test_multiple() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// test multiple instances have the same information
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	prefs.update_manager.dont_check_for_updates = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto tst = qPrefUpdateManager::instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-23 17:40:56 +01:00
										 |  |  | 	QCOMPARE(tst->dont_check_for_updates(), qPrefUpdateManager::dont_check_for_updates()); | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | 	QCOMPARE(tst->dont_check_for_updates(), false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | 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++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	QCOMPARE(tst->next_check(), QDate::fromString("12/09/1957", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-15 17:19:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-04 11:25:57 +02:00
										 |  |  | #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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 12:33:47 +02:00
										 |  |  | void TestQPrefUpdateManager::test_signals() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-31 15:02:03 +02:00
										 |  |  | 	QSignalSpy spy1(qPrefUpdateManager::instance(), &qPrefUpdateManager::dont_check_for_updatesChanged); | 
					
						
							|  |  |  | 	QSignalSpy spy3(qPrefUpdateManager::instance(), &qPrefUpdateManager::last_version_usedChanged); | 
					
						
							|  |  |  | 	QSignalSpy spy4(qPrefUpdateManager::instance(), &qPrefUpdateManager::next_checkChanged); | 
					
						
							|  |  |  | 	QSignalSpy spy5(qPrefUpdateManager::instance(), &qPrefUpdateManager::uuidStringChanged); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:33:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	prefs.update_manager.dont_check_for_updates = true; | 
					
						
							|  |  |  | 	qPrefUpdateManager::set_dont_check_for_updates(false); | 
					
						
							|  |  |  | 	qPrefUpdateManager::set_last_version_used("signal last_version2"); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	qPrefUpdateManager::set_next_check(QDate::fromString("11/09/1967", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:33:47 +02:00
										 |  |  | 	qPrefUpdateManager::set_uuidString("signal uuid"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QCOMPARE(spy1.count(), 1); | 
					
						
							|  |  |  | 	QCOMPARE(spy3.count(), 1); | 
					
						
							|  |  |  | 	QCOMPARE(spy4.count(), 1); | 
					
						
							|  |  |  | 	QCOMPARE(spy5.count(), 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QVERIFY(spy1.takeFirst().at(0).toBool() == false); | 
					
						
							|  |  |  | 	QVERIFY(spy3.takeFirst().at(0).toString() == "signal last_version2"); | 
					
						
							| 
									
										
										
										
											2021-01-05 12:08:09 -08:00
										 |  |  | 	QVERIFY(spy4.takeFirst().at(0).toDate() == QDate::fromString("11/09/1967", "dd/MM/yyyy")); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:33:47 +02:00
										 |  |  | 	QVERIFY(spy5.takeFirst().at(0).toString() == "signal uuid"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 21:07:44 +02:00
										 |  |  | QTEST_MAIN(TestQPrefUpdateManager) |