summaryrefslogtreecommitdiffstats
path: root/tests/testqPrefGeneral.cpp
blob: 4910647f1082c804c29d58fae6e7e81ea94fbced (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
// SPDX-License-Identifier: GPL-2.0
#include "testqPrefGeneral.h"

#include "core/pref.h"
#include "core/qthelper.h"
#include "core/settings/qPrefGeneral.h"
#include "core/settings/qPref.h"

#include <QTest>
#include <QSignalSpy>

void TestQPrefGeneral::initTestCase()
{
	QCoreApplication::setOrganizationName("Subsurface");
	QCoreApplication::setOrganizationDomain("subsurface.hohndel.org");
	QCoreApplication::setApplicationName("SubsurfaceTestQPrefGeneral");
	qPref::registerQML(NULL);
}

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

	auto tst = qPrefGeneral::instance();

	prefs.defaultsetpoint = 14;
	prefs.o2consumption = 17;
	prefs.pscr_ratio = 18;

	QCOMPARE(tst->defaultsetpoint(), prefs.defaultsetpoint);
	QCOMPARE(tst->o2consumption(), prefs.o2consumption);
	QCOMPARE(tst->pscr_ratio(), prefs.pscr_ratio);
}

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

	auto tst = qPrefGeneral::instance();

	tst->set_defaultsetpoint(24);
	tst->set_o2consumption(27);
	tst->set_pscr_ratio(28);
	tst->set_diveshareExport_uid("uid1");
	tst->set_diveshareExport_private(false);

	QCOMPARE(prefs.defaultsetpoint, 24);
	QCOMPARE(prefs.o2consumption, 27);
	QCOMPARE(prefs.pscr_ratio, 28);
	QCOMPARE(tst->diveshareExport_uid(), QString("uid1"));
	QCOMPARE(tst->diveshareExport_private(), false);
}

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

	auto tst = qPrefGeneral::instance();

	tst->set_defaultsetpoint(34);
	tst->set_o2consumption(37);
	tst->set_pscr_ratio(38);
	tst->set_diveshareExport_uid("uid2");
	tst->set_diveshareExport_private(true);

	prefs.defaultsetpoint = 14;
	prefs.o2consumption = 17;
	prefs.pscr_ratio = 18;

	tst->load();
	QCOMPARE(prefs.defaultsetpoint, 34);
	QCOMPARE(prefs.o2consumption, 37);
	QCOMPARE(prefs.pscr_ratio, 38);
	QCOMPARE(tst->diveshareExport_uid(), QString("uid2"));
	QCOMPARE(tst->diveshareExport_private(), true);
}

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

	auto tst = qPrefGeneral::instance();

	prefs.defaultsetpoint = 44;
	prefs.o2consumption = 47;
	prefs.pscr_ratio = 48;

	tst->sync();
	prefs.defaultsetpoint = 14;
	prefs.o2consumption = 17;
	prefs.pscr_ratio = 18;

	tst->load();
	QCOMPARE(prefs.defaultsetpoint, 44);
	QCOMPARE(prefs.o2consumption, 47);
	QCOMPARE(prefs.pscr_ratio, 48);
}

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

	prefs.o2consumption = 17;
	prefs.pscr_ratio = 18;
	auto tst = qPrefGeneral::instance();

	QCOMPARE(tst->o2consumption(), qPrefGeneral::o2consumption());
	QCOMPARE(tst->pscr_ratio(), qPrefGeneral::pscr_ratio());
	QCOMPARE(qPrefGeneral::o2consumption(), 17);
	QCOMPARE(qPrefGeneral::pscr_ratio(), 18);
}

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

void TestQPrefGeneral::test_oldPreferences()
{
	auto general = qPrefGeneral::instance();

	general->set_defaultsetpoint(0);
	general->set_o2consumption(0);
	general->set_pscr_ratio(0);

	TEST(general->defaultsetpoint(), 0);
	TEST(general->o2consumption(), 0);
	TEST(general->pscr_ratio(), 0);

	general->set_defaultsetpoint(1);
	general->set_o2consumption(1);
	general->set_pscr_ratio(1);

	TEST(general->defaultsetpoint(), 1);
	TEST(general->o2consumption(), 1);
	TEST(general->pscr_ratio(), 1);
}

void TestQPrefGeneral::test_signals()
{
	QSignalSpy spy5(qPrefGeneral::instance(), &qPrefGeneral::defaultsetpointChanged);
	QSignalSpy spy9(qPrefGeneral::instance(), &qPrefGeneral::o2consumptionChanged);
	QSignalSpy spy10(qPrefGeneral::instance(), &qPrefGeneral::pscr_ratioChanged);
	QSignalSpy spy12(qPrefGeneral::instance(), &qPrefGeneral::diveshareExport_uidChanged);
	QSignalSpy spy13(qPrefGeneral::instance(), &qPrefGeneral::diveshareExport_privateChanged);

	qPrefGeneral::set_defaultsetpoint(24);
	qPrefGeneral::set_o2consumption(27);
	qPrefGeneral::set_pscr_ratio(28);
	qPrefGeneral::set_diveshareExport_uid("uid1");
	qPrefGeneral::set_diveshareExport_private(false);

	qPrefGeneral::set_defaultsetpoint(24);
	qPrefGeneral::set_o2consumption(27);
	qPrefGeneral::set_pscr_ratio(28);
	qPrefGeneral::set_diveshareExport_uid("uid1");
	qPrefGeneral::set_diveshareExport_private(false);
}

QTEST_MAIN(TestQPrefGeneral)