1//
2//  NSView_frame_bounds.m
3//
4//  Created by Fred Kiefer on 13.11.08.
5//
6#include "Testing.h"
7
8#include <math.h>
9
10#include <Foundation/NSAutoreleasePool.h>
11#include <AppKit/NSApplication.h>
12#include <AppKit/NSView.h>
13#include <AppKit/NSWindow.h>
14
15
16int CHECK(NSView *view, NSRect frame, NSRect bounds)
17{
18	NSRect r;
19
20	r = [view frame];
21	if (fabs(r.origin.x - frame.origin.x)>0.001
22	 || fabs(r.origin.y - frame.origin.y)>0.001
23	 || fabs(r.size.width - frame.size.width)>0.001
24	 || fabs(r.size.height - frame.size.height)>0.001)
25	{
26		printf("(1) expected frame (%g %g)+(%g %g), got (%g %g)+(%g %g)\n",
27			frame.origin.x, frame.origin.y, frame.size.width, frame.size.height,
28			r.origin.x, r.origin.y, r.size.width, r.size.height);
29
30		return 0;
31	}
32
33	r = [view bounds];
34	if (fabs(r.origin.x - bounds.origin.x)>0.001
35	 || fabs(r.origin.y - bounds.origin.y)>0.001
36	 || fabs(r.size.width - bounds.size.width)>0.001
37	 || fabs(r.size.height - bounds.size.height)>0.001)
38	{
39		printf("(2) expected bounds (%g %g)+(%g %g), got (%g %g)+(%g %g)\n",
40			bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height,
41			r.origin.x, r.origin.y, r.size.width, r.size.height);
42
43		return 0;
44	}
45
46	return 1;
47}
48
49int main(int argc, char **argv)
50{
51	CREATE_AUTORELEASE_POOL(arp);
52
53	NSWindow *window;
54	NSView *view1;
55	int passed = 1;
56
57	START_SET("NView GNUstep frame_bounds")
58
59	NS_DURING
60	{
61		[NSApplication sharedApplication];
62	}
63	NS_HANDLER
64	{
65		if ([[localException name] isEqualToString: NSInternalInconsistencyException ])
66			SKIP("It looks like GNUstep backend is not yet installed")
67	}
68	NS_ENDHANDLER
69
70	window = [[NSWindow alloc] initWithContentRect: NSMakeRect(100,100,200,200)
71		styleMask: NSClosableWindowMask
72		backing: NSBackingStoreRetained
73		defer: YES];
74	view1 = [[NSView alloc] initWithFrame: NSMakeRect(20,20,100,100)];
75
76	[[window contentView] addSubview: view1];
77
78	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(0,0,100,100)) && passed;
79
80	[view1 setFrameOrigin: NSMakePoint(10, 10)];
81	passed = CHECK(view1, NSMakeRect(10,10,100,100),NSMakeRect(0,0,100,100)) && passed;
82
83	[view1 setFrameSize: NSMakeSize(80, 80)];
84	passed = CHECK(view1, NSMakeRect(10,10,80,80),NSMakeRect(0,0,80,80)) && passed;
85
86	[view1 setFrameRotation: 45.0];
87	passed = CHECK(view1, NSMakeRect(10,10,80,80),NSMakeRect(0,0,80,80)) && passed;
88
89	[view1 setBoundsRotation: -45.0];
90	passed = CHECK(view1, NSMakeRect(10,10,80,80),NSMakeRect(-56.5685,0,113.137,113.137)) && passed;
91
92	[view1 setFrameSize: NSMakeSize(100, 100)];
93	passed = CHECK(view1, NSMakeRect(10,10,100,100),NSMakeRect(-70.7107,0,141.421,141.421)) && passed;
94
95	[view1 setFrameOrigin: NSMakePoint(20, 20)];
96	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-70.7107,0,141.421,141.421)) && passed;
97
98	[view1 setBoundsOrigin: NSMakePoint(20, 20)];
99	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-50.7107,20,141.421,141.421)) && passed;
100
101	[view1 setBoundsSize: NSMakeSize(100, 100)];
102	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-50.7107,20,141.421,141.421)) && passed;
103
104	[view1 setBoundsSize: NSMakeSize(10, 10)];
105	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-5.07107,2,14.1421,14.1421)) && passed;
106
107	[view1 setBoundsRotation: 0.0];
108	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(2.82843, 0, 10, 10)) && passed;
109
110	[view1 setBoundsSize: NSMakeSize(1, 1)];
111	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(0.282843, 0, 1, 1)) && passed;
112
113	[view1 setBoundsRotation: -45.0];
114	[view1 setBounds: NSMakeRect(10, 10, 100, 100)];
115	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-60.7107,10,141.421,141.421)) && passed;
116
117	[view1 translateOriginToPoint: NSMakePoint(20, 20)];
118	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-80.7107,-10,141.421,141.421)) && passed;
119
120	[view1 scaleUnitSquareToSize: NSMakeSize(2, 3)];
121	passed = CHECK(view1, NSMakeRect(20,20,100,100),NSMakeRect(-40.3553,-3.33333,70.7107,47.1405)) && passed;
122
123	pass(passed,"NSView -frame and -bounds work");
124
125	END_SET("NView GNUstep frame_bounds")
126
127	DESTROY(arp);
128	return 0;
129}
130