xref: /haiku/src/tests/servers/app/window_creation/main.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <Application.h>
7 #include <Box.h>
8 #include <OS.h>
9 #include <Screen.h>
10 #include <Window.h>
11 
12 
13 static BRect* sFrames = NULL;
14 static uint32 sNumFrames = 0;
15 
16 
17 class TestApp : public BApplication {
18 public:
19 								TestApp(uint32 numWindows, bool views);
20 	virtual						~TestApp();
21 
22 	virtual	void				ReadyToRun();
23 
24 private:
25 			void				_CreateFrames(uint32 numWindows);
26 			int32				_WindowCreator();
27 			static int32		_ThreadStarter(void* data);
28 
29 private:
30 			uint32				fFrameNum;
31 			BRect				fScreenFrame;
32 			uint32				fNumWindows;
33 			uint32				fMaxWindows;
34 			bool				fTestViews;
35 };
36 
37 
38 class TestWindow : public BWindow {
39 public:
40 						TestWindow(BRect frame, bool createView);
41 	virtual				~TestWindow();
42 };
43 
44 
45 TestApp::TestApp(uint32 numWindows, bool views)
46 	:
47 	BApplication("application/x.vnd-Haiku.window-creation"),
48 	fScreenFrame(),
49 	fNumWindows(0),
50 	fMaxWindows(numWindows),
51 	fTestViews(views)
52 
53 {
54 	fScreenFrame = BScreen().Frame();
55 	_CreateFrames(numWindows);
56 }
57 
58 
59 TestApp::~TestApp()
60 {
61 	delete[] sFrames;
62 }
63 
64 
65 void
66 TestApp::ReadyToRun()
67 {
68 	thread_id thread = spawn_thread(_ThreadStarter, "Window creator",
69 		B_NORMAL_PRIORITY, this);
70 	resume_thread(thread);
71 }
72 
73 
74 void
75 TestApp::_CreateFrames(uint32 numWindows)
76 {
77 	BRect frame(0, 0, 50, 50);
78 	uint32 numHorizontal = (fScreenFrame.IntegerWidth() + 1)
79 		/ (frame.IntegerWidth() + 1);
80 	uint32 numVertical = (fScreenFrame.IntegerHeight() + 1)
81 		/ (frame.IntegerHeight() + 1);
82 	sNumFrames = numHorizontal * numVertical;
83 	sFrames = new BRect[sNumFrames];
84 	for (uint32 i = 0; i < sNumFrames; i++) {
85 		sFrames[i] = frame;
86 		frame.OffsetBy(50, 0);
87 		if (!fScreenFrame.Contains(frame))
88 			frame.OffsetTo(0, frame.bottom + 1);
89 	}
90 }
91 
92 
93 int32
94 TestApp::_WindowCreator()
95 {
96 	bigtime_t startTime = system_time();
97 
98 	while (fNumWindows < fMaxWindows) {
99 		if (fFrameNum >= sNumFrames)
100 			fFrameNum = 0;
101 
102 		BWindow* window = new TestWindow(sFrames[fFrameNum++], fTestViews);
103 		window->Show();
104 		fNumWindows++;
105 	}
106 
107 	bigtime_t endTime = system_time();
108 
109 	printf("Test completed. %ld windows created in %lld usecs.\n", fNumWindows,
110 		endTime - startTime);
111 
112 	PostMessage(B_QUIT_REQUESTED);
113 	return B_OK;
114 }
115 
116 
117 int32
118 TestApp::_ThreadStarter(void* data)
119 {
120 	return static_cast<TestApp*>(data)->_WindowCreator();
121 }
122 
123 
124 TestWindow::TestWindow(BRect frame, bool views)
125 	:
126 	BWindow(frame, "Test", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
127 {
128 	if (views)
129 		AddChild(new BBox(Bounds()));
130 }
131 
132 
133 TestWindow::~TestWindow()
134 {
135 }
136 
137 
138 int
139 main(int argc, char** argv)
140 {
141 	uint32 numWindows = 10;
142 	bool testViews = false;
143 	if (argc > 1)
144 		numWindows = atoi(argv[1]);
145 
146 	if (argc > 2) {
147 		if (!strcmp(argv[2], "views"))
148 			testViews = true;
149 	}
150 
151 	TestApp app(numWindows, testViews);
152 	app.Run();
153 
154 	return 0;
155 }
156