aboutsummaryrefslogtreecommitdiff
path: root/engines/testbed/testsuite.cpp
blob: 5ea6970241def0b827edf2f44c2f0f8dfbcb0e98 (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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* 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.
 *
 * $URL$
 * $Id$
 */

#include "common/config-manager.h"
#include "common/events.h"
#include "common/stream.h"

#include "graphics/fontman.h"
#include "graphics/surface.h"

#include "gui/message.h"

#include "testbed/graphics.h"
#include "testbed/testbed.h"
#include "testbed/testsuite.h"

namespace Testbed {

// Static public variable of Testsuite
bool Testsuite::isSessionInteractive = true;

// Static private variable of Testsuite
Common::String Testsuite::_logDirectory = "";
Common::String Testsuite::_logFilename = "";
Graphics::FontManager::FontUsage Testsuite::_displayFont = Graphics::FontManager::kGUIFont;
Common::WriteStream *Testsuite::_ws = 0;
uint Testsuite::toQuit = kLoopNormal;

void Testsuite::setLogDir(const char *dirname) {
	_logDirectory = dirname;
}

void Testsuite::setLogFile(const char *filename) {
	_logFilename = filename;
}

void Testsuite::deleteWriteStream() {
	if (_ws) {
		delete _ws;
	}
}

void Testsuite::initLogging(const char *logdir, const char *filename, bool enable) {
	setLogDir(logdir);
	setLogFile(filename);

	if (enable) {
		_ws = Common::FSNode(_logDirectory).getChild(_logFilename).createWriteStream();
	} else {
		_ws = 0;
	}
}

void Testsuite::initLogging(bool enable) {
	setLogDir(ConfMan.get("path").c_str());
	setLogFile("testbed.log");

	if (enable) {
		_ws = Common::FSNode(_logDirectory).getChild(_logFilename).createWriteStream();
	} else {
		_ws = 0;
	}
}

void Testsuite::logPrintf(const char *fmt, ...) {
	// Assuming log message size to be not greater than STRINGBUFLEN i.e 256
	char buffer[STRINGBUFLEN];
	va_list vl;
	va_start(vl, fmt);
	vsnprintf(buffer, STRINGBUFLEN, fmt, vl);
	va_end(vl);

	if (_ws) {
		_ws->writeString(buffer);
		debugCN(kTestbedLogOutput, "%s", buffer);
	} else {
		debugCN(kTestbedLogOutput, "%s", buffer);
	}
}

void Testsuite::logDetailedPrintf(const char *fmt, ...) {
	// Assuming log message size to be not greater than STRINGBUFLEN i.e 256
	// Messages with this function would only be displayed if -d1 is specified on command line
	char buffer[STRINGBUFLEN];
	va_list vl;
	va_start(vl, fmt);
	vsnprintf(buffer, STRINGBUFLEN, fmt, vl);
	va_end(vl);

	if (_ws) {
		_ws->writeString(buffer);
		debugCN(1, kTestbedLogOutput, "%s", buffer);
	} else {
		debugCN(1, kTestbedLogOutput, "%s", buffer);
	}
}

Testsuite::Testsuite() {
	_numTestsPassed = 0;
	_numTestsExecuted = 0;
	// Initially all testsuites are enabled, disable them by calling enableTestSuite(name, false)
	_isTsEnabled = true;
	// Set custom color for progress bar
	GFXTestSuite::setCustomColor(0, 0, 0);
}

Testsuite::~Testsuite() {
	for (Common::Array<Test *>::iterator i = _testsToExecute.begin(); i != _testsToExecute.end(); ++i) {
		delete (*i);
	}
}

void Testsuite::reset() {
	_numTestsPassed = 0;
	_numTestsExecuted = 0;
	toQuit = kLoopNormal;
	for (Common::Array<Test *>::iterator i = _testsToExecute.begin(); i != _testsToExecute.end(); ++i) {
		(*i)->passed = false;
	}
}

void Testsuite::genReport() const {
	logPrintf("\n");
	logPrintf("Consolidating results...\n");
	logPrintf("Subsystem: %s ", getName());
	logPrintf("(Tests Executed: %d)\n", _numTestsExecuted);
	logPrintf("Passed: %d ", _numTestsPassed);
	logPrintf("Failed: %d\n", getNumTestsFailed());
	logPrintf("\n");
}

bool Testsuite::handleInteractiveInput(const Common::String &textToDisplay, const char *opt1, const char *opt2, OptionSelected result) {
	GUI::MessageDialog prompt(textToDisplay, opt1, opt2);
	return prompt.runModal() == result ? true : false;
}

void Testsuite::displayMessage(const Common::String &textToDisplay, const char *defaultButton, const char *altButton) {
	GUI::MessageDialog prompt(textToDisplay, defaultButton);
	prompt.runModal();
}

Common::Rect Testsuite::writeOnScreen(const Common::String &textToDisplay, const Common::Point &pt, bool flag) {
	const Graphics::Font &font(*FontMan.getFontByUsage(Graphics::FontManager::kConsoleFont));
	uint fillColor = kColorBlack;
	uint textColor = kColorWhite;

	Graphics::Surface *screen = g_system->lockScreen();

	int height = font.getFontHeight();
	int width = screen->w;

	Common::Rect rect(pt.x, pt.y, pt.x + width, pt.y + height);

	if (flag) {
		Graphics::PixelFormat pf = g_system->getScreenFormat();
		fillColor = pf.RGBToColor(0, 0, 0);
		textColor = pf.RGBToColor(255, 255, 255);
	}

	screen->fillRect(rect, fillColor);
	font.drawString(screen, textToDisplay, rect.left, rect.top, screen->w, textColor, Graphics::kTextAlignCenter);

	g_system->unlockScreen();
	g_system->updateScreen();

	return rect;
}

void Testsuite::clearScreen(const Common::Rect &rect) {
	Graphics::Surface *screen = g_system->lockScreen();

	screen->fillRect(rect, kColorBlack);

	g_system->unlockScreen();
	g_system->updateScreen();
}

void Testsuite::clearScreen() {
	int numBytesPerLine = g_system->getWidth() * g_system->getScreenFormat().bytesPerPixel;
	int height = getDisplayRegionCoordinates().y;
	
	// Don't clear test info display region
	int size =  height * numBytesPerLine;
	byte *buffer = new byte[size];
	memset(buffer, 0, size);
	g_system->copyRectToScreen(buffer, numBytesPerLine, 0, 0, g_system->getWidth(), height);
	g_system->updateScreen();
	delete[] buffer;
}

void Testsuite::clearScreen(bool flag) {
	Graphics::Surface *screen = g_system->lockScreen();
	uint fillColor = kColorBlack;

	if (flag) {
		fillColor = g_system->getScreenFormat().RGBToColor(0, 0, 0);
	}

	screen->fillRect(Common::Rect(0, 0, g_system->getWidth(), g_system->getHeight()), fillColor);

	g_system->unlockScreen();
	g_system->updateScreen();
}

void Testsuite::addTest(const Common::String &name, InvokingFunction f, bool isInteractive) {
	Test *featureTest = new Test(name, f, isInteractive);
	_testsToExecute.push_back(featureTest);
}

int Testsuite::getNumTestsEnabled() {
	int count = 0;
	Common::Array<Test *>::const_iterator iter;
	for (iter = _testsToExecute.begin(); iter != _testsToExecute.end(); iter++) {
		if ((*iter)->enabled) {
			count++;
		}
	}
	return count;
}

uint Testsuite::parseEvents() {
	uint startTime = g_system->getMillis();
	uint end = startTime + kEventHandlingTime;
	do {
		Common::Event ev;
		while (g_system->getEventManager()->pollEvent(ev)) {
			switch (ev.type) {
			case Common::EVENT_KEYDOWN:
				if (ev.kbd.keycode == Common::KEYCODE_ESCAPE) {
					return kSkipNext;
				}
				break;
			case Common::EVENT_QUIT:
			case Common::EVENT_RTL:
				return kEngineQuit;
				break;
			default:
				break;
			}
		}
		g_system->delayMillis(10);
		startTime = g_system->getMillis();
	} while (startTime <= end);

	return kLoopNormal;
}

void Testsuite::updateStats(const char *prefix, const char *info, uint testNum, uint numTests, Common::Point pt) {
	Common::String text = Common::String::printf(" Running %s: %s (%d of %d) ", prefix, info, testNum, numTests);
	writeOnScreen(text, pt);
	uint barColor = kColorSpecial; 
	// below the text a rectangle denoting the progress in the testsuite can be drawn.
	int separation = getLineSeparation();
	pt.y += separation;
	int wRect = 200;
	int lRect = 7;
	pt.x = g_system->getWidth() / 2 - 100;
	byte *buffer = new byte[lRect * wRect];
	memset(buffer, 0, sizeof(byte) * lRect * wRect);

	int wShaded = (int) (wRect * (((float)testNum) / numTests));

	// draw the boundary
	memset(buffer, barColor, sizeof(byte) * wRect);
	memset(buffer + (wRect * (lRect - 1)) , barColor, sizeof(byte) * wRect);
	
	for (int i = 0; i < lRect; i++) {
		for (int j = 0; j < wRect; j++) {
			if (j < wShaded) {
				buffer[i * wRect + j] = barColor;
			}
		}
		buffer[i * wRect + 0] = barColor;
		buffer[i * wRect + wRect - 1] = barColor;
	}
	g_system->copyRectToScreen(buffer, wRect, pt.x, pt.y, wRect, lRect);
	g_system->updateScreen();
}

bool Testsuite::enableTest(const Common::String &testName, bool toEnable) {
	for (uint i = 0; i < _testsToExecute.size(); i++) {
		if (_testsToExecute[i]->featureName.equalsIgnoreCase(testName)) {
			_testsToExecute[i]->enabled = toEnable;
			return true;
		}
	}
	return false;
}


void Testsuite::execute() {
	// Main Loop for a testsuite

	// Do nothing if meant to exit
	if (toQuit == kEngineQuit) {
		return;
	}

	uint count = 0;
	Common::Point pt = getDisplayRegionCoordinates();
	pt.y += getLineSeparation();
	int numEnabledTests = getNumTestsEnabled();

	for (Common::Array<Test *>::iterator i = _testsToExecute.begin(); i != _testsToExecute.end(); ++i) {
		if (!(*i)->enabled) {
			logPrintf("Info! Skipping Test: %s, Skipped by configuration.\n", ((*i)->featureName).c_str());
			continue;	
		}

		if (toQuit == kSkipNext) {
			logPrintf("Info! Skipping Test: %s, Skipped by user.\n", ((*i)->featureName).c_str());
			toQuit = kLoopNormal;
			continue;
		}

		if((*i)->isInteractive && !isSessionInteractive) {
			logPrintf("Info! Skipping Test: %s, non-interactive environment is selected\n", ((*i)->featureName).c_str());
			continue;
		}

		logPrintf("Info! Executing Test: %s\n", ((*i)->featureName).c_str());
		updateStats("Test", ((*i)->featureName).c_str(), count++, numEnabledTests, pt);
		_numTestsExecuted++;
		if ((*i)->driver()) {
			logPrintf("Result: Passed\n");
			_numTestsPassed++;
		} else {
			logPrintf("Result: Failed\n");
		}
		updateStats("Test", ((*i)->featureName).c_str(), count, numEnabledTests, pt);
		// TODO: Display a screen here to user with details of upcoming test, he can skip it or Quit or RTL
		// Check if user wants to quit/RTL/Skip next test by parsing events.
		// Quit directly if explicitly requested

		if (Engine::shouldQuit()) {
			toQuit = kEngineQuit;
			genReport();
			return;
		}

		toQuit = parseEvents();
	}
	genReport();
}

} // End of namespace Testebed