182afdb09SMichael Pfeiffer/*
282afdb09SMichael Pfeiffer * Copyright 2007, Haiku. All rights reserved.
382afdb09SMichael Pfeiffer * Distributed under the terms of the MIT License.
482afdb09SMichael Pfeiffer *
582afdb09SMichael Pfeiffer * Authors:
682afdb09SMichael Pfeiffer *		Michael Pfeiffer
782afdb09SMichael Pfeiffer */
882afdb09SMichael Pfeiffer
982afdb09SMichael Pfeiffer
1082afdb09SMichael Pfeiffer#include <Application.h>
1182afdb09SMichael Pfeiffer#include <MenuItem.h>
1282afdb09SMichael Pfeiffer#include <MenuBar.h>
1382afdb09SMichael Pfeiffer#include <StringView.h>
1482afdb09SMichael Pfeiffer#include <ListItem.h>
1582afdb09SMichael Pfeiffer#include <View.h>
1682afdb09SMichael Pfeiffer
1782afdb09SMichael Pfeiffer#include <stdio.h>
1882afdb09SMichael Pfeiffer
1982afdb09SMichael Pfeiffer#include "PictureTest.h"
2082afdb09SMichael Pfeiffer#include "PictureTestCases.h"
2182afdb09SMichael Pfeiffer#include "PictureTestWindow.h"
2282afdb09SMichael Pfeiffer#include "TestResultItem.h"
2382afdb09SMichael Pfeiffer
24ae09eb30SJackBurton
2582afdb09SMichael PfeifferPictureTestWindow::PictureTestWindow()
26acf1c6adSMichael Pfeiffer	: Inherited(BRect(10, 30, 630, 470), "Bitmap Drawing Tests", B_DOCUMENT_WINDOW, 0)
27acf1c6adSMichael Pfeiffer	, fFailedTests(0)
28acf1c6adSMichael Pfeiffer	, fNumberOfTests(0)
2982afdb09SMichael Pfeiffer{
3082afdb09SMichael Pfeiffer	BuildGUI();
3182afdb09SMichael Pfeiffer}
3282afdb09SMichael Pfeiffer
3382afdb09SMichael Pfeifferbool PictureTestWindow::QuitRequested()
3482afdb09SMichael Pfeiffer{
3582afdb09SMichael Pfeiffer	bool isOk = Inherited::QuitRequested();
3682afdb09SMichael Pfeiffer	if (isOk) {
3782afdb09SMichael Pfeiffer		be_app->PostMessage(B_QUIT_REQUESTED);
3882afdb09SMichael Pfeiffer	}
3982afdb09SMichael Pfeiffer
4082afdb09SMichael Pfeiffer	return isOk;
4182afdb09SMichael Pfeiffer}
4282afdb09SMichael Pfeiffer
4382afdb09SMichael Pfeiffer
4482afdb09SMichael Pfeiffervoid PictureTestWindow::BuildGUI()
4582afdb09SMichael Pfeiffer{
4682afdb09SMichael Pfeiffer	BView* backdrop = new BView(Bounds(), "backdrop", B_FOLLOW_ALL, B_WILL_DRAW);
4782afdb09SMichael Pfeiffer	backdrop->SetViewColor(::ui_color(B_PANEL_BACKGROUND_COLOR));
4882afdb09SMichael Pfeiffer	AddChild(backdrop);
4982afdb09SMichael Pfeiffer
5082afdb09SMichael Pfeiffer	BMenuBar* mb = new BMenuBar(Bounds(), "menubar");
5182afdb09SMichael Pfeiffer	BMenu* m = new BMenu("File");
5282afdb09SMichael Pfeiffer		m->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
5382afdb09SMichael Pfeiffer		m->SetTargetForItems(be_app_messenger);
5482afdb09SMichael Pfeiffer	mb->AddItem(m);
5582afdb09SMichael Pfeiffer
5682afdb09SMichael Pfeiffer	m = new BMenu("Tests");
5782afdb09SMichael Pfeiffer		m->AddItem(new BMenuItem("Run", new BMessage(kMsgRunTests), 'R'));
5854730332SMichael Pfeiffer		m->AddItem(new BMenuItem("Run Color Space B_RGB32", new BMessage(kMsgRunTests1), 'S'));
5982afdb09SMichael Pfeiffer	mb->AddItem(m);
6082afdb09SMichael Pfeiffer
6182afdb09SMichael Pfeiffer	backdrop->AddChild(mb);
6282afdb09SMichael Pfeiffer
6382afdb09SMichael Pfeiffer	BRect b = Bounds();
6482afdb09SMichael Pfeiffer	b.top = mb->Bounds().bottom + 1;
65ae09eb30SJackBurton
66acf1c6adSMichael Pfeiffer	fHeader = new BStringView(b, "header",
67acf1c6adSMichael Pfeiffer		"X", B_FOLLOW_LEFT | B_FOLLOW_RIGHT | B_FOLLOW_TOP);
68acf1c6adSMichael Pfeiffer	float width, height;
69acf1c6adSMichael Pfeiffer	fHeader->GetPreferredSize(&width, &height);
70acf1c6adSMichael Pfeiffer	fHeader->ResizeTo(b.Width(), height);
71acf1c6adSMichael Pfeiffer	backdrop->AddChild(fHeader);
72acf1c6adSMichael Pfeiffer	b.top = fHeader->Frame().bottom + 1;
73ae09eb30SJackBurton
7482afdb09SMichael Pfeiffer	b.right -= B_V_SCROLL_BAR_WIDTH;
7582afdb09SMichael Pfeiffer	b.bottom -= B_H_SCROLL_BAR_HEIGHT;
7682afdb09SMichael Pfeiffer	fListView = new BListView(b, "Results", B_SINGLE_SELECTION_LIST,
7782afdb09SMichael Pfeiffer		B_FOLLOW_ALL_SIDES,
7882afdb09SMichael Pfeiffer		B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE);
7982afdb09SMichael Pfeiffer	backdrop->AddChild(new BScrollView("scroll_results", fListView, B_FOLLOW_ALL_SIDES, 0, true, true));
80acf1c6adSMichael Pfeiffer
81acf1c6adSMichael Pfeiffer	UpdateHeader();
82acf1c6adSMichael Pfeiffer}
83acf1c6adSMichael Pfeiffer
84acf1c6adSMichael Pfeiffervoid
85acf1c6adSMichael PfeifferPictureTestWindow::UpdateHeader()
86acf1c6adSMichael Pfeiffer{
87ae09eb30SJackBurton	BString text;
88ae09eb30SJackBurton	text << "failures = " << fFailedTests << ",  tests =" << fNumberOfTests;
89acf1c6adSMichael Pfeiffer	fHeader->SetText(text.String());
9082afdb09SMichael Pfeiffer}
9182afdb09SMichael Pfeiffer
9282afdb09SMichael Pfeiffervoid
9382afdb09SMichael PfeifferPictureTestWindow::MessageReceived(BMessage *msg) {
9482afdb09SMichael Pfeiffer	switch (msg->what) {
9582afdb09SMichael Pfeiffer		case kMsgRunTests:
9682afdb09SMichael Pfeiffer			RunTests();
9782afdb09SMichael Pfeiffer			break;
9854730332SMichael Pfeiffer		case kMsgRunTests1:
9954730332SMichael Pfeiffer			RunTests1();
10054730332SMichael Pfeiffer			break;
10182afdb09SMichael Pfeiffer	}
10282afdb09SMichael Pfeiffer	Inherited::MessageReceived(msg);
10382afdb09SMichael Pfeiffer}
10482afdb09SMichael Pfeiffer
10582afdb09SMichael Pfeiffer
10682afdb09SMichael Pfeiffervoid
10782afdb09SMichael PfeifferPictureTestWindow::RunTests()
10854730332SMichael Pfeiffer{
10954730332SMichael Pfeiffer	color_space colorSpaces[] = {
11054730332SMichael Pfeiffer		B_RGBA32,
11154730332SMichael Pfeiffer		B_RGB32,
11254730332SMichael Pfeiffer		B_RGB24,
11354730332SMichael Pfeiffer		B_RGB16,
11454730332SMichael Pfeiffer		B_RGB15
11554730332SMichael Pfeiffer	};
11654730332SMichael Pfeiffer
11754730332SMichael Pfeiffer	RunTests(colorSpaces, sizeof(colorSpaces) / sizeof(color_space));
11854730332SMichael Pfeiffer}
11954730332SMichael Pfeiffer
12054730332SMichael Pfeiffervoid
12154730332SMichael PfeifferPictureTestWindow::RunTests1()
12254730332SMichael Pfeiffer{
12354730332SMichael Pfeiffer	color_space colorSpaces[] = {
12454730332SMichael Pfeiffer		B_RGBA32
12554730332SMichael Pfeiffer	};
12654730332SMichael Pfeiffer	RunTests(colorSpaces, 1);
12754730332SMichael Pfeiffer}
12854730332SMichael Pfeiffer
12954730332SMichael Pfeiffervoid
13054730332SMichael PfeifferPictureTestWindow::RunTests(color_space *colorSpaces, int32 n)
131acf1c6adSMichael Pfeiffer{
132acf1c6adSMichael Pfeiffer	for (int testIndex = 0; testIndex < 2; testIndex ++) {
133acf1c6adSMichael Pfeiffer		BString text;
134acf1c6adSMichael Pfeiffer		switch (testIndex)
135acf1c6adSMichael Pfeiffer		{
136acf1c6adSMichael Pfeiffer			case 0:
137acf1c6adSMichael Pfeiffer				text = "Flatten Picture Test";
138acf1c6adSMichael Pfeiffer				break;
139acf1c6adSMichael Pfeiffer			case 1:
140acf1c6adSMichael Pfeiffer				text = "Archive Picture Test";
141acf1c6adSMichael Pfeiffer				break;
142acf1c6adSMichael Pfeiffer			default:
143acf1c6adSMichael Pfeiffer				text = "Unknown test method!";
144acf1c6adSMichael Pfeiffer		}
145acf1c6adSMichael Pfeiffer		fListView->AddItem(new BStringItem(text.String()));
14654730332SMichael Pfeiffer		RunTests(testIndex, colorSpaces, n);
147acf1c6adSMichael Pfeiffer	}
148ae09eb30SJackBurton
149c43f0163SMichael Pfeiffer	UpdateHeader();
150acf1c6adSMichael Pfeiffer}
151acf1c6adSMichael Pfeiffer
152acf1c6adSMichael Pfeiffervoid
15354730332SMichael PfeifferPictureTestWindow::RunTests(int32 testIndex, color_space *colorSpaces, int32 n)
15482afdb09SMichael Pfeiffer{
15554730332SMichael Pfeiffer	for (int32 csIndex = 0; csIndex < n; csIndex ++) {
15682afdb09SMichael Pfeiffer		color_space colorSpace = colorSpaces[csIndex];
15782afdb09SMichael Pfeiffer		const char *csText;
15882afdb09SMichael Pfeiffer		switch (colorSpace) {
15982afdb09SMichael Pfeiffer			case B_RGBA32:
16082afdb09SMichael Pfeiffer				csText = "B_RGB32";
16182afdb09SMichael Pfeiffer				break;
16282afdb09SMichael Pfeiffer			case B_RGB32:
16382afdb09SMichael Pfeiffer				csText = "B_RGB32";
16482afdb09SMichael Pfeiffer				break;
16582afdb09SMichael Pfeiffer			case B_RGB24:
16682afdb09SMichael Pfeiffer				csText = "B_RGB24";
16782afdb09SMichael Pfeiffer				break;
16882afdb09SMichael Pfeiffer			case B_RGB16:
16982afdb09SMichael Pfeiffer				csText = "B_RGB16";
17082afdb09SMichael Pfeiffer				break;
17182afdb09SMichael Pfeiffer			case B_RGB15:
17282afdb09SMichael Pfeiffer				csText = "B_RGB15";
17382afdb09SMichael Pfeiffer				break;
17482afdb09SMichael Pfeiffer			default:
17582afdb09SMichael Pfeiffer				csText = "Unknown";
17682afdb09SMichael Pfeiffer		}
17782afdb09SMichael Pfeiffer
17882afdb09SMichael Pfeiffer		BString text;
17982afdb09SMichael Pfeiffer		text = "Color space: ";
18082afdb09SMichael Pfeiffer		text += csText;
18182afdb09SMichael Pfeiffer		fListView->AddItem(new BStringItem(text.String()));
18282afdb09SMichael Pfeiffer
183ae09eb30SJackBurton		BRect frame(0, 0, 100, 30);
184ae09eb30SJackBurton		fListView->AddItem(new HeaderListItem("Direct Drawing", "Picture Drawing",
185ae09eb30SJackBurton						"Restored Picture", "", "Test Name", "Error Message", frame));
186ae09eb30SJackBurton
187acf1c6adSMichael Pfeiffer		RunTests(testIndex, colorSpace);
188acf1c6adSMichael Pfeiffer	}
189acf1c6adSMichael Pfeiffer}
190acf1c6adSMichael Pfeiffer
191acf1c6adSMichael Pfeiffervoid
192acf1c6adSMichael PfeifferPictureTestWindow::RunTests(int32 testIndex, color_space colorSpace)
193acf1c6adSMichael Pfeiffer{
194acf1c6adSMichael Pfeiffer	BRect frame(0, 0, 100, 30);
195acf1c6adSMichael Pfeiffer	for (int i = 0; gTestCases[i].name != NULL; i ++) {
196acf1c6adSMichael Pfeiffer		TestCase *testCase = &gTestCases[i];
197acf1c6adSMichael Pfeiffer		PictureTest *test;
198acf1c6adSMichael Pfeiffer		switch (testIndex) {
199acf1c6adSMichael Pfeiffer			case 0:
200acf1c6adSMichael Pfeiffer				test = new FlattenPictureTest();
201acf1c6adSMichael Pfeiffer				break;
202acf1c6adSMichael Pfeiffer			case 1:
203acf1c6adSMichael Pfeiffer				test = new ArchivePictureTest();
204acf1c6adSMichael Pfeiffer				break;
205acf1c6adSMichael Pfeiffer			default:
206acf1c6adSMichael Pfeiffer				continue;
20782afdb09SMichael Pfeiffer		}
208acf1c6adSMichael Pfeiffer
209acf1c6adSMichael Pfeiffer		test->SetColorSpace(colorSpace);
210acf1c6adSMichael Pfeiffer		bool ok = test->Test(testCase->func, frame);
211acf1c6adSMichael Pfeiffer
212acf1c6adSMichael Pfeiffer		TestResultItem *item = new TestResultItem(testCase->name, frame);
213acf1c6adSMichael Pfeiffer		item->SetOk(ok);
214acf1c6adSMichael Pfeiffer		item->SetErrorMessage(test->ErrorMessage());
215acf1c6adSMichael Pfeiffer		item->SetDirectBitmap(test->DirectBitmap(true));
216acf1c6adSMichael Pfeiffer		item->SetOriginalBitmap(test->BitmapFromPicture(true));
217acf1c6adSMichael Pfeiffer		item->SetArchivedBitmap(test->BitmapFromRestoredPicture(true));
218acf1c6adSMichael Pfeiffer
219acf1c6adSMichael Pfeiffer		delete test;
220acf1c6adSMichael Pfeiffer
221acf1c6adSMichael Pfeiffer		fListView->AddItem(item);
222acf1c6adSMichael Pfeiffer
223acf1c6adSMichael Pfeiffer		fNumberOfTests ++;
224acf1c6adSMichael Pfeiffer		if (!ok)
225acf1c6adSMichael Pfeiffer			fFailedTests ++;
22682afdb09SMichael Pfeiffer	}
227acf1c6adSMichael Pfeiffer}
228