xref: /haiku/src/tests/kits/interface/balert/AlertTest.cpp (revision 855c5b57b8299ff19d55a27bdd5be739e6b58d25)
1 // AlertTest.cpp
2 #include "AlertTest.h"
3 #include <cppunit/Test.h>
4 #include <cppunit/TestCaller.h>
5 #include <cppunit/TestSuite.h>
6 #include <iostream.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <Application.h>
10 #include <Alert.h>
11 #include <Point.h>
12 #include <TextView.h>
13 #include <Button.h>
14 #include <Rect.h>
15 
16 #define ASSERT_DEQUAL(x,y) CPPUNIT_ASSERT_DOUBLES_EQUAL((x),(y),0.01)
17 
18 // Required by CPPUNIT_ASSERT_EQUAL(rgb_color, rgb_color)
19 bool operator==(const rgb_color &left, const rgb_color &right)
20 {
21 	if (left.red == right.red && left.green == right.green &&
22 	    left.blue == right.blue && left.alpha == right.alpha)
23 	    return true;
24 	else
25 		return false;
26 }
27 // Required by CPPUNIT_ASSERT_EQUAL(rgb_color, rgb_color)
28 ostream &operator<<(ostream &stream, const rgb_color &clr)
29 {
30 	return stream << "rgb_color(" << clr.red << ", " <<
31 		clr.green << ", " << clr.blue << ", " << clr.alpha << ")";
32 }
33 
34 // For storing expected state of windows or views.
35 struct GuiInfo {
36 	const char*	label;
37 	float		width;
38 	float		height;
39 	BPoint		topleft;
40 };
41 
42 // For storing all the information required to create and
43 // verify the state of a BAlert object.
44 class AlertTestInfo {
45 public:
46 	AlertTestInfo(AlertTest *pTest);
47 	~AlertTestInfo();
48 
49 	void SetWinInfo(const GuiInfo &winInfo);
50 	void SetTextViewInfo(const GuiInfo &textInfo);
51 	void SetButtonInfo(int32 btn, const GuiInfo &btnInfo);
52 
53 	void SetButtonWidthMode(button_width widthMode);
54 	void SetButtonSpacingMode(button_spacing spacingMode);
55 	void SetAlertType(alert_type alertType);
56 
57 	void GuiInfoTest();
58 
59 private:
60 	AlertTest*		fTest;
61 	GuiInfo			fWinInfo;
62 	GuiInfo			fTextInfo;
63 	GuiInfo			fButtonInfo[3];
64 	int32 			fButtonCount;
65 	button_width	fWidthMode;
66 	button_spacing	fSpacingMode;
67 	alert_type		fAlertType;
68 };
69 
70 AlertTestInfo::AlertTestInfo(AlertTest *pTest)
71 {
72 	memset(this, 0, sizeof(AlertTestInfo));
73 
74 	fTest = pTest;
75 	fWidthMode = B_WIDTH_AS_USUAL;
76 	fSpacingMode = B_EVEN_SPACING;
77 	fAlertType = B_INFO_ALERT;
78 }
79 
80 AlertTestInfo::~AlertTestInfo()
81 {
82 	fTest = NULL;
83 	fButtonCount = 0;
84 }
85 
86 void
87 AlertTestInfo::SetWinInfo(const GuiInfo &winInfo)
88 {
89 	fWinInfo = winInfo;
90 }
91 
92 void
93 AlertTestInfo::SetTextViewInfo(const GuiInfo &textInfo)
94 {
95 	fTextInfo = textInfo;
96 }
97 
98 void
99 AlertTestInfo::SetButtonInfo(int32 btn, const GuiInfo &btnInfo)
100 {
101 	if (btn < 0 || btn > 2 || btn > fButtonCount) {
102 		CPPUNIT_ASSERT(false);
103 		return;
104 	}
105 	fButtonInfo[btn] = btnInfo;
106 	if (btn == fButtonCount && fButtonCount < 3)
107 		fButtonCount++;
108 }
109 
110 void
111 AlertTestInfo::SetButtonWidthMode(button_width widthMode)
112 {
113 	fWidthMode = widthMode;
114 }
115 
116 void
117 AlertTestInfo::SetButtonSpacingMode(button_spacing spacingMode)
118 {
119 	fSpacingMode = spacingMode;
120 }
121 
122 void
123 AlertTestInfo::SetAlertType(alert_type alertType)
124 {
125 	fAlertType = alertType;
126 }
127 
128 void
129 AlertTestInfo::GuiInfoTest()
130 {
131 	fTest->NextSubTest();
132 	// Dummy application object required to create Window objects.
133 	BApplication app("application/x-vnd.Haiku-interfacekit_alerttest");
134 	BAlert *pAlert = new BAlert(
135 		fWinInfo.label,
136 		fTextInfo.label,
137 		fButtonInfo[0].label,
138 		fButtonInfo[1].label,
139 		fButtonInfo[2].label,
140 		fWidthMode,
141 		fSpacingMode,
142 		fAlertType
143 	);
144 	CPPUNIT_ASSERT(pAlert);
145 
146 	// Alert Window Width/Height
147 	fTest->NextSubTest();
148 	ASSERT_DEQUAL(fWinInfo.width, pAlert->Bounds().Width());
149 	ASSERT_DEQUAL(fWinInfo.height, pAlert->Bounds().Height());
150 
151 	// [k] MasterView
152 	fTest->NextSubTest();
153 	BView *masterView = pAlert->ChildAt(0);
154 	CPPUNIT_ASSERT(masterView);
155 
156 	// [k] MasterView Color
157 	fTest->NextSubTest();
158 	CPPUNIT_ASSERT_EQUAL(ui_color(B_PANEL_BACKGROUND_COLOR),
159 		masterView->ViewColor());
160 
161 	// Test all the buttons
162 	BButton *btns[3] = { NULL };
163 	for (int32 i = 0; i < 3; i++) {
164 		fTest->NextSubTest();
165 		btns[i] = pAlert->ButtonAt(i);
166 
167 		if (i >= fButtonCount) {
168 			// If there is should be no button at this index
169 			CPPUNIT_ASSERT_EQUAL((BButton*)NULL, btns[i]);
170 		} else {
171 			// If there should be a button at this index
172 			CPPUNIT_ASSERT(btns[i]);
173 
174 			CPPUNIT_ASSERT(
175 				strcmp(fButtonInfo[i].label, btns[i]->Label()) == 0);
176 
177 			ASSERT_DEQUAL(fButtonInfo[i].width, btns[i]->Bounds().Width());
178 
179 			ASSERT_DEQUAL(fButtonInfo[i].height, btns[i]->Bounds().Height());
180 
181 			BPoint pt = btns[i]->ConvertToParent(BPoint(0, 0));
182 			ASSERT_DEQUAL(fButtonInfo[i].topleft.x, pt.x);
183 			ASSERT_DEQUAL(fButtonInfo[i].topleft.y, pt.y);
184 
185 			if (i == fButtonCount - 1) {
186 				// Default button
187 				CPPUNIT_ASSERT_EQUAL(true, btns[i]->IsDefault());
188 			}
189 		}
190 	}
191 
192 	// [k] TextView
193 	fTest->NextSubTest();
194 	BTextView *textView = pAlert->TextView();
195 	CPPUNIT_ASSERT(textView);
196 
197 	// [k] TextView ViewColor()
198 	fTest->NextSubTest();
199 	CPPUNIT_ASSERT_EQUAL(ui_color(B_PANEL_BACKGROUND_COLOR), textView->ViewColor());
200 
201 	// [k] TextView IsEditable()
202 	fTest->NextSubTest();
203 	CPPUNIT_ASSERT_EQUAL(false, textView->IsEditable());
204 
205 	// [k] TextView IsSelectable()
206 	fTest->NextSubTest();
207 	CPPUNIT_ASSERT_EQUAL(false, textView->IsSelectable());
208 
209 	// [k] TextView DoesWordWrap()
210 	fTest->NextSubTest();
211 	CPPUNIT_ASSERT_EQUAL(true, textView->DoesWordWrap());
212 
213 	// TextView Text
214 	fTest->NextSubTest();
215 	CPPUNIT_ASSERT(strcmp(fTextInfo.label, textView->Text()) == 0);
216 
217 	// TextView Width/Height
218 	fTest->NextSubTest();
219 	ASSERT_DEQUAL(fTextInfo.width, textView->Bounds().Width());
220 	ASSERT_DEQUAL(fTextInfo.height, textView->Bounds().Height());
221 
222 	// TextView Position
223 	fTest->NextSubTest();
224 	BPoint pt = textView->ConvertToParent(BPoint(0, 0));
225 	ASSERT_DEQUAL(fTextInfo.topleft.x, pt.x);
226 	ASSERT_DEQUAL(fTextInfo.topleft.y, pt.y);
227 
228 	delete pAlert;
229 	pAlert = NULL;
230 }
231 
232 // Suite
233 CppUnit::Test *
234 AlertTest::Suite()
235 {
236 	CppUnit::TestSuite *suite = new CppUnit::TestSuite();
237 	typedef CppUnit::TestCaller<AlertTest> TC;
238 
239 	suite->addTest(
240 		new TC("Alert empty_empty_UW_ES_IA",
241 			&AlertTest::empty_empty_UW_ES_IA));
242 
243 	suite->addTest(
244 		new TC("Alert OK_X_UW_ES_IA",
245 			&AlertTest::OK_X_UW_ES_IA));
246 
247 	suite->addTest(
248 		new TC("Alert OK_60X_UW_ES_IA",
249 			&AlertTest::OK_60X_UW_ES_IA));
250 
251 	return suite;
252 }
253 
254 // setUp
255 void
256 AlertTest::setUp()
257 {
258 	BTestCase::setUp();
259 }
260 
261 // tearDown
262 void
263 AlertTest::tearDown()
264 {
265 	BTestCase::tearDown();
266 }
267 
268 void
269 AlertTest::empty_empty_UW_ES_IA()
270 {
271 	AlertTestInfo ati(this);
272 	GuiInfo wi, ti, bi;
273 	wi.label = "alert1";
274 	wi.width = 310.0f;
275 	wi.height = 64.0f;
276 	ati.SetWinInfo(wi);
277 
278 	ti.label = "";
279 	ti.width = 245.0f;
280 	ti.height = 13.0f;
281 	ti.topleft.Set(55.0f, 6.0f);
282 	ati.SetTextViewInfo(ti);
283 
284 	bi.label = "";
285 	bi.width = 75.0f;
286 	bi.height = 30.0f;
287 	bi.topleft.Set(229.0f, 28.0f);
288 	ati.SetButtonInfo(0, bi);
289 
290 	ati.SetButtonWidthMode(B_WIDTH_AS_USUAL);
291 	ati.SetButtonSpacingMode(B_EVEN_SPACING);
292 	ati.SetAlertType(B_INFO_ALERT);
293 
294 	ati.GuiInfoTest();
295 }
296 
297 void
298 AlertTest::OK_X_UW_ES_IA()
299 {
300 	AlertTestInfo ati(this);
301 	GuiInfo wi, ti, bi;
302 	wi.label = "alert1";
303 	wi.width = 310.0f;
304 	wi.height = 64.0f;
305 	ati.SetWinInfo(wi);
306 
307 	ti.label = "X";
308 	ti.width = 245.0f;
309 	ti.height = 13.0f;
310 	ti.topleft.Set(55.0f, 6.0f);
311 	ati.SetTextViewInfo(ti);
312 
313 	bi.label = "OK";
314 	bi.width = 75.0f;
315 	bi.height = 30.0f;
316 	bi.topleft.Set(229.0f, 28.0f);
317 	ati.SetButtonInfo(0, bi);
318 
319 	ati.SetButtonWidthMode(B_WIDTH_AS_USUAL);
320 	ati.SetButtonSpacingMode(B_EVEN_SPACING);
321 	ati.SetAlertType(B_INFO_ALERT);
322 
323 	ati.GuiInfoTest();
324 }
325 
326 void
327 AlertTest::OK_60X_UW_ES_IA()
328 {
329 	AlertTestInfo ati(this);
330 	GuiInfo wi, ti, bi;
331 	wi.label = "alert1";
332 	wi.width = 310.0f;
333 	wi.height = 77.0f;
334 	ati.SetWinInfo(wi);
335 
336 	ti.label = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
337 	ti.width = 245.0f;
338 	ti.height = 26.0f;
339 	ti.topleft.Set(55.0f, 6.0f);
340 	ati.SetTextViewInfo(ti);
341 
342 	bi.label = "OK";
343 	bi.width = 75.0f;
344 	bi.height = 30.0f;
345 	bi.topleft.Set(229.0f, 41.0f);
346 	ati.SetButtonInfo(0, bi);
347 
348 	ati.SetButtonWidthMode(B_WIDTH_AS_USUAL);
349 	ati.SetButtonSpacingMode(B_EVEN_SPACING);
350 	ati.SetAlertType(B_INFO_ALERT);
351 
352 	ati.GuiInfoTest();
353 }
354 
355 
356 
357