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