aboutsummaryrefslogtreecommitdiff
path: root/engines/testbed/testsuite.h
blob: ffeb2588c4e50ac95fd1cbf8871e7c2766478003 (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
#ifndef TESTSUITE_H
#define TESTSUITE_H

#include "common/system.h"
#include "common/str.h"
#include "common/array.h"

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

#include "gui/message.h"

namespace Testbed {

enum {
	kColorBlack = 0,
	kColorWhite = 1,
	kColorCustom = 2
};

typedef bool (*invokingFunction)();

/**
 * This represents a feature to be tested
 */

struct Test {
	Test(Common::String name, invokingFunction f) : featureName(name), driver(f), enabled(true), passed(false) {}
	Common::String featureName;		///< Name of feature to be tested
	invokingFunction driver;	    ///< Pointer to the function that will invoke this feature test
	bool enabled;				    ///< Decides whether or not this test is to be executed
	bool passed;					///< Collects and stores result of this feature test
};


/**
 * The basic Testsuite class
 * All the other testsuites would inherit it and override its virtual methods
 */

class Testsuite {
public:
	Testsuite() {
		_numTestsPassed = 0;
		_numTestsExecuted = 0;
	}
	
	virtual ~Testsuite() {
		for (Common::Array<Test*>::iterator i = _testsToExecute.begin(); i != _testsToExecute.end(); ++i) {
			delete (*i);
		}	
	}
	
	int getNumTests() { return _testsToExecute.size(); }
	int getNumTestsPassed() { return _numTestsPassed; }
	int getNumTestsFailed() { return _numTestsExecuted - _numTestsPassed; }
	void genReport() {
		printf("Subsystem:%s\n",getName());
		printf("Tests executed:%d\n", _numTestsExecuted);
		printf("Tests Passed:%d\n", _numTestsPassed);
		printf("Tests Failed:%d\n", getNumTestsFailed());
	}
	
	/**
	 * Prompts for User Input in form of "Yes" or "No" for interactive tests
	 * e.g: "Is this like you expect?" "Yes" or "No"
	 *
	 * @param	textToDisplay Display text
	 * @return	true if "Yes" false otherwise
	 */ 
	static bool handleInteractiveInput(const Common::String &textToDisplay) {
		GUI::MessageDialog	prompt(textToDisplay, "Yes", "No");
		return prompt.runModal() == GUI::kMessageOK ? true : false;
	}
	
	static void displayMessage(const Common::String &textToDisplay) {
		GUI::MessageDialog	prompt(textToDisplay);
		prompt.runModal();
	}

	static Common::Rect writeOnScreen(const Common::String &textToDisplay, const Common::Point &pt) {
		const Graphics::Font &font(*FontMan.getFontByUsage(Graphics::FontManager::kConsoleFont));
		
		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);

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

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

		return rect;
	}

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

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

	/**
	 * Adds a test to the list of tests to be executed
	 *
	 * @param	name the string description of the test, for display purposes
	 * @param	f pointer to the function that invokes this test
	 */
	void addTest(const Common::String &name, invokingFunction f) {
		Test*  featureTest = new Test(name, f);
		_testsToExecute.push_back(featureTest);
	}
	
	/**
	 * The driver function for the testsuite
	 * All code should go in here.
	 */
	virtual void execute() {
		for (Common::Array<Test*>::iterator i = _testsToExecute.begin(); i != _testsToExecute.end(); ++i) {
			printf("Executing Test:%s\n", ((*i)->featureName).c_str());
			_numTestsExecuted++;
			if ((*i)->driver()) {
				_numTestsPassed++;
			}
		}
		genReport();
	}
	virtual const char *getName() = 0;

protected:
	Common::Array<Test*> _testsToExecute;			///< List of tests to be executed
	int		    _numTestsPassed;					///< Number of tests passed
	int  		_numTestsExecuted;					///< Number of tests executed
};

}	// End of namespace testbed

#endif