xref: /haiku/src/tests/kits/interface/flatten_picture/PictureTestCases.cpp (revision 62c7fd0f79ec51f0b2938bb0ad3f253fe522544d)
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 #include "PictureTestCases.h"
1082afdb09SMichael Pfeiffer 
1182afdb09SMichael Pfeiffer void testEmptyPicture(BView *view, BRect frame)
1282afdb09SMichael Pfeiffer {
1382afdb09SMichael Pfeiffer 	// no op
1482afdb09SMichael Pfeiffer }
1582afdb09SMichael Pfeiffer 
1682afdb09SMichael Pfeiffer void testDrawString(BView *view, BRect frame)
1782afdb09SMichael Pfeiffer {
1882afdb09SMichael Pfeiffer 	BFont font;
1982afdb09SMichael Pfeiffer 	view->GetFont(&font);
2082afdb09SMichael Pfeiffer 	font_height height;
2182afdb09SMichael Pfeiffer 	font.GetHeight(&height);
2282afdb09SMichael Pfeiffer 	float baseline = frame.bottom - height.descent;
2382afdb09SMichael Pfeiffer 	// draw base line
2482afdb09SMichael Pfeiffer 	view->SetHighColor(0, 255, 0);
2582afdb09SMichael Pfeiffer 	view->StrokeLine(BPoint(frame.left, baseline - 1), BPoint(frame.right, baseline -1));
2682afdb09SMichael Pfeiffer 
2782afdb09SMichael Pfeiffer 	view->SetHighColor(0, 0, 0);
2882afdb09SMichael Pfeiffer 	view->DrawString("Haiku [ÖÜÄöüä]", BPoint(frame.left, baseline));
2982afdb09SMichael Pfeiffer }
3082afdb09SMichael Pfeiffer 
3182afdb09SMichael Pfeiffer void testFillRed(BView *view, BRect frame)
3282afdb09SMichael Pfeiffer {
3382afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
3482afdb09SMichael Pfeiffer 	view->SetHighColor(255, 0, 0);
3582afdb09SMichael Pfeiffer 	view->FillRect(frame);
3682afdb09SMichael Pfeiffer }
3782afdb09SMichael Pfeiffer 
3882afdb09SMichael Pfeiffer void testStrokeRect(BView *view, BRect frame)
3982afdb09SMichael Pfeiffer {
4082afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
4182afdb09SMichael Pfeiffer 	int levels = (int)(frame.Height()/2 + 1);
4282afdb09SMichael Pfeiffer 	for (int i = 0; i < levels; i ++) {
4382afdb09SMichael Pfeiffer 		view->SetHighColor(0, 0, 255 * (levels-i) / levels);
4482afdb09SMichael Pfeiffer 		view->StrokeRect(frame);
4582afdb09SMichael Pfeiffer 		frame.InsetBy(1, 1);
4682afdb09SMichael Pfeiffer 	}
4782afdb09SMichael Pfeiffer }
4882afdb09SMichael Pfeiffer 
49acf1c6adSMichael Pfeiffer void testDiagonalLine(BView *view, BRect frame)
5082afdb09SMichael Pfeiffer {
5182afdb09SMichael Pfeiffer 	view->StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.bottom));
5282afdb09SMichael Pfeiffer }
5382afdb09SMichael Pfeiffer 
5482afdb09SMichael Pfeiffer void testStrokeScaledRect(BView *view, BRect frame)
5582afdb09SMichael Pfeiffer {
5682afdb09SMichael Pfeiffer 	view->SetScale(0.5);
5782afdb09SMichael Pfeiffer 	view->StrokeRect(frame);
5882afdb09SMichael Pfeiffer }
5982afdb09SMichael Pfeiffer 
6082afdb09SMichael Pfeiffer void testRecordPicture(BView *view, BRect frame)
6182afdb09SMichael Pfeiffer {
6282afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
6382afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
6482afdb09SMichael Pfeiffer 	view->FillRect(frame);
6582afdb09SMichael Pfeiffer 	view->EndPicture();
6682afdb09SMichael Pfeiffer 	delete picture;
6782afdb09SMichael Pfeiffer }
6882afdb09SMichael Pfeiffer 
6982afdb09SMichael Pfeiffer void testRecordAndPlayPicture(BView *view, BRect frame)
7082afdb09SMichael Pfeiffer {
7182afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
7282afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
7382afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
7482afdb09SMichael Pfeiffer 	view->FillRect(frame);
7582afdb09SMichael Pfeiffer 	view->EndPicture();
7682afdb09SMichael Pfeiffer 	view->DrawPicture(picture);
7782afdb09SMichael Pfeiffer 	delete picture;
7882afdb09SMichael Pfeiffer }
7982afdb09SMichael Pfeiffer 
8082afdb09SMichael Pfeiffer void testRecordAndPlayPictureWithOffset(BView *view, BRect frame)
8182afdb09SMichael Pfeiffer {
8282afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
8382afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
8482afdb09SMichael Pfeiffer 	frame.InsetBy(frame.Width() / 4, frame.Height() / 4);
8582afdb09SMichael Pfeiffer 	frame.OffsetTo(0, 0);
8682afdb09SMichael Pfeiffer 	view->FillRect(frame);
8782afdb09SMichael Pfeiffer 	view->EndPicture();
8882afdb09SMichael Pfeiffer 
8982afdb09SMichael Pfeiffer 	view->DrawPicture(picture, BPoint(10, 10));
9082afdb09SMichael Pfeiffer 	// color of picture should not change
9182afdb09SMichael Pfeiffer 	view->SetLowColor(0, 255, 0);
9282afdb09SMichael Pfeiffer 	view->SetLowColor(255, 0, 0);
9382afdb09SMichael Pfeiffer 	view->DrawPicture(picture, BPoint(0, 0));
9482afdb09SMichael Pfeiffer 	delete picture;
9582afdb09SMichael Pfeiffer }
9682afdb09SMichael Pfeiffer 
9782afdb09SMichael Pfeiffer void testBitmap(BView *view, BRect frame) {
9882afdb09SMichael Pfeiffer 	BBitmap bitmap(frame, B_RGBA32);
9982afdb09SMichael Pfeiffer 	for (int32 y = 0; y < bitmap.Bounds().IntegerHeight(); y ++) {
10082afdb09SMichael Pfeiffer 		for (int32 x = 0; x < bitmap.Bounds().IntegerWidth(); x ++) {
10182afdb09SMichael Pfeiffer 			char *pixel = (char*)bitmap.Bits();
10282afdb09SMichael Pfeiffer 			pixel += bitmap.BytesPerRow() * y + 4 * x;
10382afdb09SMichael Pfeiffer 			// fill with blue
10482afdb09SMichael Pfeiffer 			pixel[0] = 255;
10582afdb09SMichael Pfeiffer 			pixel[1] = 0;
10682afdb09SMichael Pfeiffer 			pixel[2] = 0;
10782afdb09SMichael Pfeiffer 			pixel[3] = 255;
10882afdb09SMichael Pfeiffer 		}
10982afdb09SMichael Pfeiffer 	}
11082afdb09SMichael Pfeiffer 	view->DrawBitmap(&bitmap, BPoint(0, 0));
11182afdb09SMichael Pfeiffer }
11282afdb09SMichael Pfeiffer 
11382afdb09SMichael Pfeiffer TestCase gTestCases[] = {
11482afdb09SMichael Pfeiffer 	{ "Test Empty Picture", testEmptyPicture },
115acf1c6adSMichael Pfeiffer 	{ "Test Diagonal Line", testDiagonalLine },
11682afdb09SMichael Pfeiffer 	{ "Test Stroke Rect", testStrokeRect },
11782afdb09SMichael Pfeiffer 	{ "Test Draw String", testDrawString },
11882afdb09SMichael Pfeiffer 	{ "Test Fill Red", testFillRed },
11982afdb09SMichael Pfeiffer 	{ "Test Stroke Scaled Rect", testStrokeScaledRect },
12082afdb09SMichael Pfeiffer 	{ "Test Record Picture", testRecordPicture },
12182afdb09SMichael Pfeiffer 	{ "Test Record And Play Picture", testRecordAndPlayPicture },
12282afdb09SMichael Pfeiffer 	{ "Test Record And Play Picture With Offset", testRecordAndPlayPictureWithOffset },
12382afdb09SMichael Pfeiffer 	{ "Test Draw Bitmap", testBitmap },
12482afdb09SMichael Pfeiffer 	{ NULL, NULL }
12582afdb09SMichael Pfeiffer };
126*62c7fd0fSMichael Pfeiffer 
127