aboutsummaryrefslogtreecommitdiff
path: root/engines/testbed/misc.cpp
blob: 30cce8cee0a487ce5b6f3192fb725b678318e23e (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "testbed/misc.h"
#include "common/timer.h"

namespace Testbed {

Common::String MiscTests::getHumanReadableFormat(const TimeDate &td) {
	return Common::String::format("%d:%d:%d on %d/%d/%d (dd/mm/yyyy)", td.tm_hour, td.tm_min, td.tm_sec, td.tm_mday, td.tm_mon + 1, td.tm_year + 1900);
}

void MiscTests::timerCallback(void *arg) {
	// Increment arg which actually points to an int
	int &valToModify = *((int *) arg);
	valToModify = 999; // some arbitrary value
}

void MiscTests::criticalSection(void *arg) {
	SharedVars &sv = *((SharedVars *)arg);

	Testsuite::logDetailedPrintf("Before critical section: %d %d\n", sv.first, sv.second);
	g_system->lockMutex(sv.mutex);

	// In any case, the two vars must be equal at entry, if mutex works fine.
	// verify this here.
	if (sv.first != sv.second) {
		sv.resultSoFar = false;
	}

	sv.first++;
	g_system->delayMillis(1000);

	// This should bring no change as well in the difference between vars
	// verify this too.
	if (sv.second + 1 != sv.first) {
		sv.resultSoFar = false;
	}

	sv.second *= sv.first;
	Testsuite::logDetailedPrintf("After critical section: %d %d\n", sv.first, sv.second);
	g_system->unlockMutex(sv.mutex);

	g_system->getTimerManager()->removeTimerProc(criticalSection);
}

TestExitStatus MiscTests::testDateTime() {

	if (ConfParams.isSessionInteractive()) {
		if (Testsuite::handleInteractiveInput("Testing the date time API implementation", "Continue", "Skip", kOptionRight)) {
			Testsuite::logPrintf("Info! Date time tests skipped by the user.\n");
			return kTestSkipped;
		}

		Testsuite::writeOnScreen("Verifying Date-Time...", Common::Point(0, 100));
	}

	TimeDate t1, t2;
	g_system->getTimeAndDate(t1);
	Testsuite::logDetailedPrintf("Current Time and Date: ");
	Common::String dateTimeNow;
	dateTimeNow = getHumanReadableFormat(t1);

	if (ConfParams.isSessionInteractive()) {
		// Directly verify date
		dateTimeNow = "We expect the current date time to be " + dateTimeNow;
		if (Testsuite::handleInteractiveInput(dateTimeNow, "Correct!", "Wrong", kOptionRight)) {
			return kTestFailed;
		}
	}

	g_system->getTimeAndDate(t1);
	dateTimeNow = getHumanReadableFormat(t1);
	Testsuite::logDetailedPrintf("%s\n", dateTimeNow.c_str());
	// Now, Put some delay
	g_system->delayMillis(2000);
	g_system->getTimeAndDate(t2);
	Testsuite::logDetailedPrintf("Time and Date 2s later: ");
	dateTimeNow = getHumanReadableFormat(t2);
	Testsuite::logDetailedPrintf("%s\n", dateTimeNow.c_str());

	if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday) {
		if (t1.tm_mon == t2.tm_mon && t1.tm_year == t2.tm_year) {
			// Ignore lag due to processing time
			if (t1.tm_sec + 2 == t2.tm_sec) {
				return kTestPassed;
			}
		}
	}
	return kTestFailed;
}

TestExitStatus MiscTests::testTimers() {
	int valToModify = 0;
	if (g_system->getTimerManager()->installTimerProc(timerCallback, 100000, &valToModify, "testbedTimer")) {
		g_system->delayMillis(150);
		g_system->getTimerManager()->removeTimerProc(timerCallback);

		if (999 == valToModify) {
			return kTestPassed;
		}
	}
	return kTestFailed;
}

TestExitStatus MiscTests::testMutexes() {

	if (ConfParams.isSessionInteractive()) {
		if (Testsuite::handleInteractiveInput("Testing the Mutual Exclusion API implementation", "Continue", "Skip", kOptionRight)) {
			Testsuite::logPrintf("Info! Mutex tests skipped by the user.\n");
			return kTestSkipped;
		}
		Testsuite::writeOnScreen("Installing mutex", Common::Point(0, 100));
	}

	SharedVars sv = {1, 1, true, g_system->createMutex()};

	if (g_system->getTimerManager()->installTimerProc(criticalSection, 100000, &sv, "testbedMutex")) {
		g_system->delayMillis(150);
	}

	g_system->lockMutex(sv.mutex);
	sv.first++;
	g_system->delayMillis(1000);
	sv.second *= sv.first;
	g_system->unlockMutex(sv.mutex);

	// wait till timed process exits
	if (ConfParams.isSessionInteractive()) {
		Testsuite::writeOnScreen("Waiting for 3s so that timed processes finish", Common::Point(0, 100));
	}
	g_system->delayMillis(3000);

	Testsuite::logDetailedPrintf("Final Value: %d %d\n", sv.first, sv.second);
	g_system->deleteMutex(sv.mutex);

	if (sv.resultSoFar && 6 == sv.second) {
		return kTestPassed;
	}

	return kTestFailed;
}

TestExitStatus MiscTests::testOpenUrl() {
	Common::String info = "Testing openUrl() method.\n"
		"In this test we'll try to open scummvm.org in your default browser.";

	if (Testsuite::handleInteractiveInput(info, "OK", "Skip", kOptionRight)) {
		Testsuite::logPrintf("Info! Skipping test : openUrl()\n");
		return kTestSkipped;
	}

	if (!g_system->openUrl("https://scummvm.org/")) {
		Testsuite::logPrintf("Info! openUrl() says it couldn't open the url (probably not supported on this platform)\n");
		return kTestFailed;
	}

	if (Testsuite::handleInteractiveInput("Was ScummVM able to open 'https://scummvm.org/' in your default browser?", "Yes", "No", kOptionRight)) {
		Testsuite::logDetailedPrintf("Error! openUrl() is not working!\n");
		return kTestFailed;
	}

	Testsuite::logDetailedPrintf("openUrl() is OK\n");
	return kTestPassed;
}

MiscTestSuite::MiscTestSuite() {
	addTest("Datetime", &MiscTests::testDateTime, false);
	addTest("Timers", &MiscTests::testTimers, false);
	addTest("Mutexes", &MiscTests::testMutexes, false);
	addTest("openUrl", &MiscTests::testOpenUrl, true);
}

} // End of namespace Testbed