Added tests for basic drawing operations.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21907 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer 2007-08-12 13:01:52 +00:00
parent c954d33405
commit 54730332d2
3 changed files with 325 additions and 51 deletions

View File

@ -8,12 +8,33 @@
#include "PictureTestCases.h"
void testEmptyPicture(BView *view, BRect frame)
static const rgb_color kBlack = {0, 0, 0};
static const rgb_color kWhite = {255, 255, 255};
static const rgb_color kRed = {255, 0, 0};
static const rgb_color kGreen = {0, 255, 0};
static const rgb_color kBlue = {0, 0, 255};
static BPoint centerPoint(BRect rect)
{
int x = (int)(rect.left + rect.IntegerWidth() / 2);
int y = (int)(rect.top + rect.IntegerHeight() / 2);
return BPoint(x, y);
}
static void testNoOp(BView *view, BRect frame)
{
// no op
}
void testDrawString(BView *view, BRect frame)
static void testDrawChar(BView *view, BRect frame)
{
view->MovePenTo(frame.left, frame.bottom - 5);
view->DrawChar('A');
view->DrawChar('B', BPoint(frame.left + 20, frame.bottom - 5));
}
static void testDrawString(BView *view, BRect frame)
{
BFont font;
view->GetFont(&font);
@ -21,43 +42,200 @@ void testDrawString(BView *view, BRect frame)
font.GetHeight(&height);
float baseline = frame.bottom - height.descent;
// draw base line
view->SetHighColor(0, 255, 0);
view->SetHighColor(kGreen);
view->StrokeLine(BPoint(frame.left, baseline - 1), BPoint(frame.right, baseline -1));
view->SetHighColor(0, 0, 0);
view->SetHighColor(kBlack);
view->DrawString("Haiku [ÖÜÄöüä]", BPoint(frame.left, baseline));
}
void testFillRed(BView *view, BRect frame)
static void testDrawStringWithLength(BView *view, BRect frame)
{
BFont font;
view->GetFont(&font);
font_height height;
font.GetHeight(&height);
float baseline = frame.bottom - height.descent;
// draw base line
view->SetHighColor(kGreen);
view->StrokeLine(BPoint(frame.left, baseline - 1), BPoint(frame.right, baseline -1));
view->SetHighColor(kBlack);
view->DrawString("Haiku [ÖÜÄöüä]", 13, BPoint(frame.left, baseline));
}
static void testFillArc(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->FillArc(frame, 45, 180);
}
static void testStrokeArc(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->StrokeArc(frame, 45, 180);
}
static void testFillBezier(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[4];
points[0] = BPoint(frame.left, frame.bottom);
points[1] = BPoint(frame.left, frame.top);
points[1] = BPoint(frame.left, frame.top);
points[3] = BPoint(frame.right, frame.top);
view->FillBezier(points);
}
static void testStrokeBezier(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[4];
points[0] = BPoint(frame.left, frame.bottom);
points[1] = BPoint(frame.left, frame.top);
points[1] = BPoint(frame.left, frame.top);
points[3] = BPoint(frame.right, frame.top);
view->StrokeBezier(points);
}
static void testFillEllipse(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->FillEllipse(frame);
view->SetHighColor(kRed);
float r = frame.Width() / 3;
float s = frame.Height() / 4;
view->FillEllipse(centerPoint(frame), r, s);
}
static void testStrokeEllipse(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->StrokeEllipse(frame);
view->SetHighColor(kRed);
float r = frame.Width() / 3;
float s = frame.Height() / 4;
view->StrokeEllipse(centerPoint(frame), r, s);
}
static void testFillPolygon(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[4];
points[0] = BPoint(frame.left, frame.top);
points[1] = BPoint(frame.right, frame.bottom);
points[2] = BPoint(frame.right, frame.top);
points[3] = BPoint(frame.left, frame.bottom);
view->FillPolygon(points, 4);
}
static void testStrokePolygon(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[4];
points[0] = BPoint(frame.left, frame.top);
points[1] = BPoint(frame.right, frame.bottom);
points[2] = BPoint(frame.right, frame.top);
points[3] = BPoint(frame.left, frame.bottom);
view->StrokePolygon(points, 4);
}
static void testFillRect(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->SetHighColor(255, 0, 0);
view->FillRect(frame);
}
void testStrokeRect(BView *view, BRect frame)
static void testStrokeRect(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
int levels = (int)(frame.Height()/2 + 1);
for (int i = 0; i < levels; i ++) {
view->SetHighColor(0, 0, 255 * (levels-i) / levels);
view->StrokeRect(frame);
frame.InsetBy(1, 1);
}
}
void testDiagonalLine(BView *view, BRect frame)
{
view->StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.bottom));
}
void testStrokeScaledRect(BView *view, BRect frame)
{
view->SetScale(0.5);
view->StrokeRect(frame);
}
void testRecordPicture(BView *view, BRect frame)
static void testFillRegion(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BRegion region(frame);
frame.InsetBy(2, 2);
region.Exclude(frame);
view->FillRegion(&region);
}
static void testFillRoundRect(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->FillRoundRect(frame, 5, 3);
}
static void testStrokeRoundRect(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->StrokeRoundRect(frame, 5, 3);
}
static void testFillTriangle(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[3];
points[0] = BPoint(frame.left, frame.bottom);
points[1] = BPoint(centerPoint(frame).x, frame.top);
points[2] = BPoint(frame.right, frame.bottom);
view->FillTriangle(points[0], points[1], points[2]);
}
static void testStrokeTriangle(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BPoint points[3];
points[0] = BPoint(frame.left, frame.bottom);
points[1] = BPoint(centerPoint(frame).x, frame.top);
points[2] = BPoint(frame.right, frame.bottom);
view->StrokeTriangle(points[0], points[1], points[2]);
}
static void testStrokeLine(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.top));
frame.top += 2;
frame.bottom -= 2;
view->StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.bottom));
frame.bottom += 2;;
frame.top = frame.bottom;
view->StrokeLine(BPoint(frame.right, frame.top), BPoint(frame.left, frame.top));
}
static void testFillShape(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BShape shape;
shape.MoveTo(BPoint(frame.left, frame.bottom));
shape.LineTo(BPoint(frame.right, frame.top));
shape.LineTo(BPoint(frame.left, frame.top));
shape.LineTo(BPoint(frame.right, frame.bottom));
view->FillShape(&shape);
}
static void testStrokeShape(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BShape shape;
shape.MoveTo(BPoint(frame.left, frame.bottom));
shape.LineTo(BPoint(frame.right, frame.top));
shape.LineTo(BPoint(frame.left, frame.top));
shape.LineTo(BPoint(frame.right, frame.bottom));
view->StrokeShape(&shape);
}
static void testRecordPicture(BView *view, BRect frame)
{
BPicture *picture = new BPicture();
view->BeginPicture(picture);
@ -66,7 +244,7 @@ void testRecordPicture(BView *view, BRect frame)
delete picture;
}
void testRecordAndPlayPicture(BView *view, BRect frame)
static void testRecordAndPlayPicture(BView *view, BRect frame)
{
BPicture *picture = new BPicture();
view->BeginPicture(picture);
@ -77,7 +255,7 @@ void testRecordAndPlayPicture(BView *view, BRect frame)
delete picture;
}
void testRecordAndPlayPictureWithOffset(BView *view, BRect frame)
static void testRecordAndPlayPictureWithOffset(BView *view, BRect frame)
{
BPicture *picture = new BPicture();
view->BeginPicture(picture);
@ -88,13 +266,70 @@ void testRecordAndPlayPictureWithOffset(BView *view, BRect frame)
view->DrawPicture(picture, BPoint(10, 10));
// color of picture should not change
view->SetLowColor(0, 255, 0);
view->SetLowColor(255, 0, 0);
view->SetLowColor(kGreen);
view->SetLowColor(kRed);
view->DrawPicture(picture, BPoint(0, 0));
delete picture;
}
void testBitmap(BView *view, BRect frame) {
static void testAppendToPicture(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->BeginPicture(new BPicture());
view->FillRect(frame);
BPicture* picture = view->EndPicture();
if (picture == NULL)
return;
frame.InsetBy(2, 2);
view->AppendToPicture(picture);
view->SetHighColor(kRed);
view->FillRect(frame);
if (view->EndPicture() != picture)
return;
view->DrawPicture(picture);
delete picture;
}
static void testLineArray(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->BeginLineArray(3);
view->AddLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.top), kBlack);
frame.top += 2;
frame.bottom -= 2;
view->AddLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.bottom), kRed);
frame.bottom += 2;;
frame.top = frame.bottom;
view->AddLine(BPoint(frame.right, frame.top), BPoint(frame.left, frame.top), kGreen);
view->EndLineArray();
}
static void testCopyBits(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
BRect leftHalf(frame);
leftHalf.right = centerPoint(frame).x - 1;
BRect rightHalf(frame);
rightHalf.left = centerPoint(frame).x + 1;
view->StrokeRect(leftHalf);
view->CopyBits(leftHalf, rightHalf);
}
static void testInvertRect(BView *view, BRect frame)
{
frame.InsetBy(2, 2);
view->InvertRect(frame);
}
static void testBitmap(BView *view, BRect frame) {
BBitmap bitmap(frame, B_RGBA32);
for (int32 y = 0; y < bitmap.Bounds().IntegerHeight(); y ++) {
for (int32 x = 0; x < bitmap.Bounds().IntegerWidth(); x ++) {
@ -111,16 +346,37 @@ void testBitmap(BView *view, BRect frame) {
}
TestCase gTestCases[] = {
{ "Test Empty Picture", testEmptyPicture },
{ "Test Diagonal Line", testDiagonalLine },
{ "Test Stroke Rect", testStrokeRect },
{ "Test No Operation", testNoOp },
{ "Test DrawChar", testDrawChar },
{ "Test Draw String", testDrawString },
{ "Test Fill Red", testFillRed },
{ "Test Stroke Scaled Rect", testStrokeScaledRect },
{ "Test Draw String With Length", testDrawStringWithLength },
{ "Test FillArc", testFillArc },
{ "Test StrokeArc", testStrokeArc },
{ "Test FillBezier", testFillBezier },
{ "Test StrokeBezier", testStrokeBezier },
{ "Test FillEllipse", testFillEllipse },
{ "Test StrokeEllipse", testStrokeEllipse },
{ "Test FillPolygon", testFillPolygon },
{ "Test StrokePolygon", testStrokePolygon },
{ "Test FillRect", testFillRect },
{ "Test StrokeRect", testStrokeRect },
{ "Test FillRegion", testFillRegion },
{ "Test FillRoundRect", testFillRoundRect },
{ "Test StrokeRoundRect", testStrokeRoundRect },
{ "Test FillTriangle", testFillTriangle },
{ "Test StrokeTriangle", testStrokeTriangle },
{ "Test StrokeLine", testStrokeLine },
{ "Test FillShape", testFillShape },
{ "Test StrokeShape", testStrokeShape },
{ "Test Record Picture", testRecordPicture },
{ "Test Record And Play Picture", testRecordAndPlayPicture },
{ "Test Record And Play Picture With Offset", testRecordAndPlayPictureWithOffset },
{ "Test Draw Bitmap", testBitmap },
{ "Test AppendToPicture", testAppendToPicture },
{ "Test LineArray", testLineArray },
// does not work under R5 for pictures!
{ "Test CopyBits*", testCopyBits },
{ "Test InvertRect", testInvertRect },
{ "Test Bitmap", testBitmap },
{ NULL, NULL }
};

View File

@ -54,9 +54,7 @@ void PictureTestWindow::BuildGUI()
m = new BMenu("Tests");
m->AddItem(new BMenuItem("Run", new BMessage(kMsgRunTests), 'R'));
// not implemented
// m->AddSeparatorItem();
// m->AddItem(new BMenuItem("Write images", new BMessage(kMsgWriteImages), 'W'));
m->AddItem(new BMenuItem("Run Color Space B_RGB32", new BMessage(kMsgRunTests1), 'S'));
mb->AddItem(m);
backdrop->AddChild(mb);
@ -96,6 +94,9 @@ PictureTestWindow::MessageReceived(BMessage *msg) {
case kMsgRunTests:
RunTests();
break;
case kMsgRunTests1:
RunTests1();
break;
}
Inherited::MessageReceived(msg);
}
@ -103,6 +104,29 @@ PictureTestWindow::MessageReceived(BMessage *msg) {
void
PictureTestWindow::RunTests()
{
color_space colorSpaces[] = {
B_RGBA32,
B_RGB32,
B_RGB24,
B_RGB16,
B_RGB15
};
RunTests(colorSpaces, sizeof(colorSpaces) / sizeof(color_space));
}
void
PictureTestWindow::RunTests1()
{
color_space colorSpaces[] = {
B_RGBA32
};
RunTests(colorSpaces, 1);
}
void
PictureTestWindow::RunTests(color_space *colorSpaces, int32 n)
{
for (int testIndex = 0; testIndex < 2; testIndex ++) {
BString text;
@ -118,22 +142,14 @@ PictureTestWindow::RunTests()
text = "Unknown test method!";
}
fListView->AddItem(new BStringItem(text.String()));
RunTests(testIndex);
RunTests(testIndex, colorSpaces, n);
}
}
void
PictureTestWindow::RunTests(int32 testIndex)
PictureTestWindow::RunTests(int32 testIndex, color_space *colorSpaces, int32 n)
{
color_space colorSpaces[] = {
B_RGBA32,
B_RGB32,
B_RGB24,
B_RGB16,
B_RGB15
};
for (uint32 csIndex = 0; csIndex < sizeof(colorSpaces)/sizeof(color_space); csIndex ++) {
for (int32 csIndex = 0; csIndex < n; csIndex ++) {
color_space colorSpace = colorSpaces[csIndex];
const char *csText;
switch (colorSpace) {

View File

@ -24,13 +24,15 @@ private:
enum {
kMsgRunTests = 'PTst',
kMsgWriteImages,
kMsgRunTests1,
};
void BuildGUI();
void UpdateHeader();
void RunTests();
void RunTests(int32 testIndex);
void RunTests1();
void RunTests(color_space *colorSpaces, int32 n);
void RunTests(int32 testIndex, color_space *colorSpaces, int32 n);
void RunTests(int32 testIndex, color_space colorSpace);
BListView *fListView;