File: autosave_t.cpp

package info (click to toggle)
openorienteering-mapper 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 67,132 kB
  • sloc: cpp: 114,710; ansic: 1,455; sh: 430; java: 240; xml: 140; sed: 64; makefile: 28
file content (254 lines) | stat: -rw-r--r-- 7,585 bytes parent folder | download | duplicates (4)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 *    Copyright 2014-2019 Kai Pastor
 *
 *    This file is part of OpenOrienteering.
 *
 *    OpenOrienteering is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OpenOrienteering is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "autosave_t.h"

#include <QtGlobal>
#include <QtTest>
#include <QCoreApplication>
#include <QMetaObject>
#include <QString>

#include "settings.h"


namespace {

/// Converts from autosave interval unit (minutes) to QTest::qWait unit (milliseconds)
int msecs(double minutes)
{
	return qRound(minutes * 60000);
}

}  // namespace



//### AutosaveTestDocument ###

AutosaveTestDocument::AutosaveTestDocument(Autosave::AutosaveResult expected_result)
: expected_result(expected_result),
  autosave_count(0)
{
	// nothing
}

Autosave::AutosaveResult AutosaveTestDocument::autosave()
{
	++autosave_count;
	
	Autosave::AutosaveResult result = expected_result;
	if (result == Autosave::TemporaryFailure)
		expected_result = Autosave::Success; // success on next call
	
	return result;
}

int AutosaveTestDocument::autosaveCount() const
{
	return autosave_count;
}


//### AutosaveTest ###

AutosaveTest::AutosaveTest(QObject* parent)
: QObject(parent)
, autosave_interval(0.02) // 0.02 min = 1.2 s
{
	// Use distinct QSettings
	QCoreApplication::setOrganizationName(QString::fromLatin1("OpenOrienteering.org"));
	QCoreApplication::setApplicationName(QString::fromLatin1(metaObject()->className()));
	QVERIFY2(QDir::home().exists(), "The home dir must be writable in order to use QSettings.");
}

void AutosaveTest::initTestCase()
{
	Settings::getInstance().setSetting(Settings::General_AutosaveInterval, autosave_interval);
	QCOMPARE(QSettings().status(), QSettings::NoError);
	QCOMPARE(Settings::getInstance().getSetting(Settings::General_AutosaveInterval).toDouble(), autosave_interval);
}

void AutosaveTest::autosaveTestDocumentTest()
{
	{
		AutosaveTestDocument doc(Autosave::Success);
		QCOMPARE(doc.autosaveCount(), 0);
		QCOMPARE(doc.autosave(), Autosave::Success);
		QCOMPARE(doc.autosaveCount(), 1);
	}
	
	{
		AutosaveTestDocument doc(Autosave::TemporaryFailure);
		QCOMPARE(doc.autosave(), Autosave::TemporaryFailure);
		QCOMPARE(doc.autosave(), Autosave::Success);
		QCOMPARE(doc.autosave(), Autosave::Success);
		QCOMPARE(doc.autosaveCount(), 3);
	}
	
	{
		AutosaveTestDocument doc(Autosave::PermanentFailure);
		QCOMPARE(doc.autosave(), Autosave::PermanentFailure);
		QCOMPARE(doc.autosave(), Autosave::PermanentFailure);
		QCOMPARE(doc.autosaveCount(), 2);
	}
}

void AutosaveTest::successTest()
{
	AutosaveTestDocument doc(Autosave::Success);
	
	// Enable and trigger Autosave
	doc.setAutosaveNeeded(true);
	
	// Verify that Autosave does not trigger too early
	QTest::qWait(msecs(0.8 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 0);
	
	// Verify that Autosave does trigger within 2 seconds after timeout
	QTest::qWait(msecs(0.2 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 1, 2000);
	
	// Verify that Autosave does not trigger again too early
	QTest::qWait(msecs(0.8 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 1);
	
	// Verify that Autosave does trigger once again within 2 seconds
	QTest::qWait(msecs(0.2 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 2, 2000);
}

void AutosaveTest::temporaryFailureTest()
{
	AutosaveTestDocument doc(Autosave::TemporaryFailure);
	
	// Enable and trigger Autosave
	doc.setAutosaveNeeded(true);
	
	// Verify that Autosave does not trigger too early
	QTest::qWait(msecs(0.8 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 0);
	
	// Verify that Autosave does trigger within 2 seconds after timeout
	QTest::qWait(msecs(0.2 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 1, 2000);
	
	// Verify that Autosave does not trigger once more too early
	QTest::qWait(4000);
	QCOMPARE(doc.autosaveCount(), 1);
	
	// Verify that Autosave does trigger once more quickly
	QTest::qWait(1000);
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 2, 2000);
	
	// NOTE: The following check used to fail frequently on macOS with our
	//       standard timing of 0.8 * autosave_interval. Given our extremely
	//       short autosave_interval in this test, this is probably a timer
	//       precision issue, not a bug in the Autosave unit.
	//       The less strict timing of 0.7 * autosave_interval seems to reduce
	//       the rate of failing runs to an acceptable level. The extra time
	//       is moved to the subsequent step of waiting.
	
	// Verify that Autosave does not trigger again too early
	QTest::qWait(msecs(0.7 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 2);
	
	// Verify that Autosave does trigger again once before too late
	QTest::qWait(msecs(0.3 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 3, 2000);
}

void AutosaveTest::permanentFailureTest()
{
	AutosaveTestDocument doc(Autosave::PermanentFailure);
	
	// Enable and trigger Autosave
	doc.setAutosaveNeeded(true);
	
	// Verify that Autosave does not trigger too early
	QTest::qWait(msecs(0.8 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 0);
	
	// Verify that Autosave does trigger exactly once before too late
	QTest::qWait(msecs(0.2 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 1, 2000);

	// Verify that Autosave does not trigger again too early
	QTest::qWait(msecs(0.8 * autosave_interval));
	QCOMPARE(doc.autosaveCount(), 1);
	
	// Verify that Autosave does trigger again once before too late
	QTest::qWait(msecs(0.2 * autosave_interval));
	QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 2, 2000);
}

void AutosaveTest::autosaveStopTest()
{
	{
		AutosaveTestDocument doc(Autosave::Success);
		
		// Enable and trigger Autosave
		doc.setAutosaveNeeded(true);
		
		// Sleep some time
		QTest::qWait(msecs(0.3 * autosave_interval));
		QCOMPARE(doc.autosaveCount(), 0);
		
		// Verify that Autosave does not trigger after setAutosaveNeeded(false)
		doc.setAutosaveNeeded(false);
		QTest::qWait(msecs(autosave_interval) + 2000);
		QCOMPARE(doc.autosaveCount(), 0);
	}
	
	{
		AutosaveTestDocument doc(Autosave::TemporaryFailure);
		
		// Test stopping autosave in temporary failure mode
		doc.setAutosaveNeeded(true);
		QTest::qWait(msecs(autosave_interval));
		QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 1, 2000);

		doc.setAutosaveNeeded(false);
		QTest::qWait(msecs(autosave_interval) + 7000);
		QCOMPARE(doc.autosaveCount(), 1);
	}
	
	{
		AutosaveTestDocument doc(Autosave::PermanentFailure);
		
		// Test stopping autosave in permanent failure mode
		doc.setAutosaveNeeded(true);
		QTest::qWait(msecs(autosave_interval));
		QTRY_COMPARE_WITH_TIMEOUT((doc.autosaveCount()), 1, 2000);

		doc.setAutosaveNeeded(false);
		QTest::qWait(msecs(autosave_interval) + 2000);
		QCOMPARE(doc.autosaveCount(), 1);
	}
}

/*
 * We don't need a real GUI window.
 */
namespace {
	auto Q_DECL_UNUSED qpa_selected = qputenv("QT_QPA_PLATFORM", "minimal");  // clazy:exclude=non-pod-global-static
}


QTEST_MAIN(AutosaveTest)