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