xref: /haiku/src/tests/kits/interface/flatten_picture/PictureTestWindow.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2007, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Pfeiffer
7  */
8 
9 
10 #include <Application.h>
11 #include <MenuItem.h>
12 #include <MenuBar.h>
13 #include <StringView.h>
14 #include <ListItem.h>
15 #include <View.h>
16 
17 #include <stdio.h>
18 
19 #include "PictureTest.h"
20 #include "PictureTestCases.h"
21 #include "PictureTestWindow.h"
22 #include "TestResultItem.h"
23 
24 PictureTestWindow::PictureTestWindow()
25 	: Inherited(BRect(10, 30, 630, 470), "Bitmap Drawing Tests", B_DOCUMENT_WINDOW, 0)
26 	, fFailedTests(0)
27 	, fNumberOfTests(0)
28 {
29 	BuildGUI();
30 }
31 
32 bool PictureTestWindow::QuitRequested()
33 {
34 	bool isOk = Inherited::QuitRequested();
35 	if (isOk) {
36 		be_app->PostMessage(B_QUIT_REQUESTED);
37 	}
38 
39 	return isOk;
40 }
41 
42 
43 void PictureTestWindow::BuildGUI()
44 {
45 	BView* backdrop = new BView(Bounds(), "backdrop", B_FOLLOW_ALL, B_WILL_DRAW);
46 	backdrop->SetViewColor(::ui_color(B_PANEL_BACKGROUND_COLOR));
47 	AddChild(backdrop);
48 
49 	BMenuBar* mb = new BMenuBar(Bounds(), "menubar");
50 	BMenu* m = new BMenu("File");
51 		m->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
52 		m->SetTargetForItems(be_app_messenger);
53 	mb->AddItem(m);
54 
55 	m = new BMenu("Tests");
56 		m->AddItem(new BMenuItem("Run", new BMessage(kMsgRunTests), 'R'));
57 		m->AddItem(new BMenuItem("Run Color Space B_RGB32", new BMessage(kMsgRunTests1), 'S'));
58 	mb->AddItem(m);
59 
60 	backdrop->AddChild(mb);
61 
62 	BRect b = Bounds();
63 	b.top = mb->Bounds().bottom + 1;
64 
65 	fHeader = new BStringView(b, "header",
66 		"X", B_FOLLOW_LEFT | B_FOLLOW_RIGHT | B_FOLLOW_TOP);
67 	float width, height;
68 	fHeader->GetPreferredSize(&width, &height);
69 	fHeader->ResizeTo(b.Width(), height);
70 	backdrop->AddChild(fHeader);
71 	b.top = fHeader->Frame().bottom + 1;
72 
73 	b.right -= B_V_SCROLL_BAR_WIDTH;
74 	b.bottom -= B_H_SCROLL_BAR_HEIGHT;
75 	fListView = new BListView(b, "Results", B_SINGLE_SELECTION_LIST,
76 		B_FOLLOW_ALL_SIDES,
77 		B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE);
78 	backdrop->AddChild(new BScrollView("scroll_results", fListView, B_FOLLOW_ALL_SIDES, 0, true, true));
79 
80 	UpdateHeader();
81 }
82 
83 void
84 PictureTestWindow::UpdateHeader()
85 {
86 	BString text("Direct Drawing, Picture Drawing, Restored Picture Drawing, Test Name, Error Message");
87 	text << " (failures = " << fFailedTests << ",  tests =" << fNumberOfTests << ")";
88 	fHeader->SetText(text.String());
89 }
90 
91 void
92 PictureTestWindow::MessageReceived(BMessage *msg) {
93 	switch (msg->what) {
94 		case kMsgRunTests:
95 			RunTests();
96 			break;
97 		case kMsgRunTests1:
98 			RunTests1();
99 			break;
100 	}
101 	Inherited::MessageReceived(msg);
102 }
103 
104 
105 void
106 PictureTestWindow::RunTests()
107 {
108 	color_space colorSpaces[] = {
109 		B_RGBA32,
110 		B_RGB32,
111 		B_RGB24,
112 		B_RGB16,
113 		B_RGB15
114 	};
115 
116 	RunTests(colorSpaces, sizeof(colorSpaces) / sizeof(color_space));
117 }
118 
119 void
120 PictureTestWindow::RunTests1()
121 {
122 	color_space colorSpaces[] = {
123 		B_RGBA32
124 	};
125 	RunTests(colorSpaces, 1);
126 }
127 
128 void
129 PictureTestWindow::RunTests(color_space *colorSpaces, int32 n)
130 {
131 	for (int testIndex = 0; testIndex < 2; testIndex ++) {
132 		BString text;
133 		switch (testIndex)
134 		{
135 			case 0:
136 				text = "Flatten Picture Test";
137 				break;
138 			case 1:
139 				text = "Archive Picture Test";
140 				break;
141 			default:
142 				text = "Unknown test method!";
143 		}
144 		fListView->AddItem(new BStringItem(text.String()));
145 		RunTests(testIndex, colorSpaces, n);
146 	}
147 
148 	UpdateHeader();
149 }
150 
151 void
152 PictureTestWindow::RunTests(int32 testIndex, color_space *colorSpaces, int32 n)
153 {
154 	for (int32 csIndex = 0; csIndex < n; csIndex ++) {
155 		color_space colorSpace = colorSpaces[csIndex];
156 		const char *csText;
157 		switch (colorSpace) {
158 			case B_RGBA32:
159 				csText = "B_RGB32";
160 				break;
161 			case B_RGB32:
162 				csText = "B_RGB32";
163 				break;
164 			case B_RGB24:
165 				csText = "B_RGB24";
166 				break;
167 			case B_RGB16:
168 				csText = "B_RGB16";
169 				break;
170 			case B_RGB15:
171 				csText = "B_RGB15";
172 				break;
173 			default:
174 				csText = "Unknown";
175 		}
176 
177 		BString text;
178 		text = "Color space: ";
179 		text += csText;
180 		fListView->AddItem(new BStringItem(text.String()));
181 
182 		RunTests(testIndex, colorSpace);
183 	}
184 }
185 
186 void
187 PictureTestWindow::RunTests(int32 testIndex, color_space colorSpace)
188 {
189 	BRect frame(0, 0, 100, 30);
190 	for (int i = 0; gTestCases[i].name != NULL; i ++) {
191 		TestCase *testCase = &gTestCases[i];
192 		PictureTest *test;
193 		switch (testIndex) {
194 			case 0:
195 				test = new FlattenPictureTest();
196 				break;
197 			case 1:
198 				test = new ArchivePictureTest();
199 				break;
200 			default:
201 				continue;
202 		}
203 
204 		test->SetColorSpace(colorSpace);
205 		bool ok = test->Test(testCase->func, frame);
206 
207 		TestResultItem *item = new TestResultItem(testCase->name, frame);
208 		item->SetOk(ok);
209 		item->SetErrorMessage(test->ErrorMessage());
210 		item->SetDirectBitmap(test->DirectBitmap(true));
211 		item->SetOriginalBitmap(test->BitmapFromPicture(true));
212 		item->SetArchivedBitmap(test->BitmapFromRestoredPicture(true));
213 
214 		delete test;
215 
216 		fListView->AddItem(item);
217 
218 		fNumberOfTests ++;
219 		if (!ok)
220 			fFailedTests ++;
221 	}
222 }
223