xref: /haiku/src/tests/servers/app/stress_test/main.cpp (revision 5ac9b506412b11afb993bb52d161efe7666958a5)
1 // main.cpp
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include <Application.h>
8 #include <MessageRunner.h>
9 #include <Window.h>
10 
11 
12 int32 gMaxCount = 0;
13 
14 enum {
15 	TEST_MANY_WINDOWS = 0,
16 	TEST_SINGLE_WINDOW,
17 };
18 
19 class TestApp : public BApplication {
20  public:
21 						TestApp(uint32 testMode);
22 	virtual				~TestApp();
23 
24 	virtual	void		ReadyToRun();
25 	virtual	void		MessageReceived(BMessage* message);
26 
27  private:
28 	BMessageRunner*		fPulse;
29 	BRect				fFrame;
30 	BRect				fScreenFrame;
31 	BWindow*			fWindow;
32 	uint32				fTestMode;
33 };
34 
35 
36 class TestWindow : public BWindow {
37  public:
38 						TestWindow(BRect frame);
39 	virtual				~TestWindow();
40 
41  private:
42 	BMessageRunner*		fPulse;
43 };
44 
45 
46 TestApp::TestApp(uint32 testMode)
47 	: BApplication("application/x.vnd-Haiku.stress-test"),
48 	  fPulse(NULL),
49 	  fFrame(10.0, 30.0, 150.0, 100.0),
50 	  fScreenFrame(0.0, 0.0, 640.0, 480.0),
51 	  fWindow(NULL),
52 	  fTestMode(testMode)
53 {
54 }
55 
56 
57 TestApp::~TestApp()
58 {
59 	delete fPulse;
60 }
61 
62 
63 void
64 TestApp::ReadyToRun()
65 {
66 	BMessage message('tick');
67 	fPulse = new BMessageRunner(be_app_messenger, &message, 100L);
68 }
69 
70 
71 void
72 TestApp::MessageReceived(BMessage* message)
73 {
74 	static int32 count = 0;
75 	if (gMaxCount != 0 && ++count > gMaxCount)
76 		PostMessage(B_QUIT_REQUESTED);
77 
78 	switch (message->what) {
79 		case 'tick':
80 			switch (fTestMode) {
81 				case TEST_MANY_WINDOWS:
82 				{
83 					fFrame.OffsetBy(10.0, 0.0);
84 					if (fFrame.right > fScreenFrame.right) {
85 						// next row
86 						fFrame.OffsetTo(10.0, fFrame.top + 10.0);
87 					}
88 					if (fFrame.bottom > fScreenFrame.bottom) {
89 						// back to top
90 						fFrame.OffsetTo(10.0, 30.0);
91 					}
92 					int32 action = CountWindows() > 1 ? rand() % 10 : 0;
93 					switch (action) {
94 						case 0:	// new
95 							new TestWindow(fFrame);
96 							break;
97 						case 1:	// move
98 						{
99 							BWindow* window = WindowAt(rand() % CountWindows());
100 							if (window->Lock()) {
101 								if (window->IsHidden())
102 									window->Show();
103 								window->MoveBy(23, 19);
104 								window->Unlock();
105 							}
106 							break;
107 						}
108 						case 2:	// hide
109 						{
110 							BWindow* window = WindowAt(rand() % CountWindows());
111 							if (window->Lock()) {
112 								if (!window->IsHidden())
113 									window->Hide();
114 								window->Unlock();
115 							}
116 							break;
117 						}
118 						case 3:	// activate
119 						{
120 							BWindow* window = WindowAt(rand() % CountWindows());
121 							if (window->Lock()) {
122 								if (window->IsHidden())
123 									window->Show();
124 								window->Activate();
125 								window->Unlock();
126 							}
127 							break;
128 						}
129 						case 4:	// change workspace
130 						{
131 							BWindow* window = WindowAt(rand() % CountWindows());
132 							if (window->Lock()) {
133 								if (window->IsHidden())
134 									window->Show();
135 								window->SetWorkspaces(1 << (rand() % 4));
136 								window->Unlock();
137 							}
138 							break;
139 						}
140 						case 5:	// minimize
141 						{
142 							BWindow* window = WindowAt(rand() % CountWindows());
143 							if (window->Lock()) {
144 								if (window->IsHidden())
145 									window->Show();
146 								window->Minimize(true);
147 								window->Unlock();
148 							}
149 							break;
150 						}
151 						case 6:	// change size
152 						{
153 							BWindow* window = WindowAt(rand() % CountWindows());
154 							if (window->Lock()) {
155 								if (window->IsHidden())
156 									window->Show();
157 								window->ResizeBy(1, 2);
158 								window->Unlock();
159 							}
160 							break;
161 						}
162 						case 7:	// set title
163 						{
164 							BWindow* window = WindowAt(rand() % CountWindows());
165 							if (window->Lock()) {
166 								if (window->IsHidden())
167 									window->Show();
168 								char title[256];
169 								snprintf(title, sizeof(title), "Title %d",
170 									rand() % 100);
171 								window->SetTitle(title);
172 								window->Unlock();
173 							}
174 							break;
175 						}
176 						case 8:	// set look
177 						{
178 							BWindow* window = WindowAt(rand() % CountWindows());
179 							if (window->Lock()) {
180 								if (window->IsHidden())
181 									window->Show();
182 								window_look looks[] = {
183 									B_DOCUMENT_WINDOW_LOOK,
184 									B_MODAL_WINDOW_LOOK,
185 									B_FLOATING_WINDOW_LOOK,
186 								};
187 								window->SetLook(looks[rand() % 3]);
188 								window->Unlock();
189 							}
190 							break;
191 						}
192 						case 9:	// set feel
193 						{
194 							BWindow* window = WindowAt(rand() % CountWindows());
195 							if (window->Lock()) {
196 								if (window->IsHidden())
197 									window->Show();
198 								window_feel feels[] = {
199 									B_NORMAL_WINDOW_FEEL,
200 									B_FLOATING_APP_WINDOW_FEEL,
201 									B_MODAL_APP_WINDOW_FEEL,
202 								};
203 								window->SetFeel(feels[rand() % 3]);
204 								window->Unlock();
205 							}
206 							break;
207 						}
208 					}
209 					break;
210 				}
211 
212 				case TEST_SINGLE_WINDOW:
213 					if (fWindow) {
214 						fWindow->Lock();
215 						fWindow->Quit();
216 					}
217 					fWindow = new BWindow(fFrame, "Test", B_TITLED_WINDOW, 0);
218 					fWindow->Show();
219 					break;
220 				default:
221 					PostMessage(B_QUIT_REQUESTED);
222 					break;
223 			}
224 			break;
225 		default:
226 			BApplication::MessageReceived(message);
227 	}
228 }
229 
230 
231 TestWindow::TestWindow(BRect frame)
232 	: BWindow(frame, "Test", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
233 {
234 	Show();
235 
236 	BMessenger self(this);
237 	BMessage message(B_QUIT_REQUESTED);
238 	fPulse = new BMessageRunner(self, &message, 100000000LL, 1);
239 
240 	if (Thread() < B_OK)
241 		Quit();
242 }
243 
244 
245 TestWindow::~TestWindow()
246 {
247 	delete fPulse;
248 }
249 
250 
251 
252 // main
253 int
254 main(int argc, char** argv)
255 {
256 	uint32 testMode = TEST_SINGLE_WINDOW;
257 
258 	if (argc > 1) {
259 		if (strcmp(argv[1], "-many") == 0)
260 			testMode = TEST_MANY_WINDOWS;
261 		else if (strcmp(argv[1], "-single") == 0)
262 			testMode = TEST_SINGLE_WINDOW;
263 	}
264 	if (argc > 2) {
265 		gMaxCount = atol(argv[2]);
266 	}
267 
268 	TestApp app(testMode);
269 	app.Run();
270 
271 	return 0;
272 }
273