headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / servers / app / stress_test / main.cpp
blobb27695efed41e5abc55e305cf0ff820e87cf4066
1 // main.cpp
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include <Application.h>
8 #include <MessageRunner.h>
9 #include <Window.h>
12 int32 gMaxCount = 0;
14 enum {
15 TEST_MANY_WINDOWS = 0,
16 TEST_SINGLE_WINDOW,
19 class TestApp : public BApplication {
20 public:
21 TestApp(uint32 testMode);
22 virtual ~TestApp();
24 virtual void ReadyToRun();
25 virtual void MessageReceived(BMessage* message);
27 private:
28 BMessageRunner* fPulse;
29 BRect fFrame;
30 BRect fScreenFrame;
31 BWindow* fWindow;
32 uint32 fTestMode;
36 class TestWindow : public BWindow {
37 public:
38 TestWindow(BRect frame);
39 virtual ~TestWindow();
41 private:
42 BMessageRunner* fPulse;
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)
57 TestApp::~TestApp()
59 delete fPulse;
63 void
64 TestApp::ReadyToRun()
66 BMessage message('tick');
67 fPulse = new BMessageRunner(be_app_messenger, &message, 100L);
71 void
72 TestApp::MessageReceived(BMessage* message)
74 static int32 count = 0;
75 if (gMaxCount != 0 && ++count > gMaxCount)
76 PostMessage(B_QUIT_REQUESTED);
78 switch (message->what) {
79 case 'tick':
80 switch (fTestMode) {
81 case TEST_MANY_WINDOWS:
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);
88 if (fFrame.bottom > fScreenFrame.bottom) {
89 // back to top
90 fFrame.OffsetTo(10.0, 30.0);
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
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();
106 break;
108 case 2: // hide
110 BWindow* window = WindowAt(rand() % CountWindows());
111 if (window->Lock()) {
112 if (!window->IsHidden())
113 window->Hide();
114 window->Unlock();
116 break;
118 case 3: // activate
120 BWindow* window = WindowAt(rand() % CountWindows());
121 if (window->Lock()) {
122 if (window->IsHidden())
123 window->Show();
124 window->Activate();
125 window->Unlock();
127 break;
129 case 4: // change workspace
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();
138 break;
140 case 5: // minimize
142 BWindow* window = WindowAt(rand() % CountWindows());
143 if (window->Lock()) {
144 if (window->IsHidden())
145 window->Show();
146 window->Minimize(true);
147 window->Unlock();
149 break;
151 case 6: // change size
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();
160 break;
162 case 7: // set title
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();
174 break;
176 case 8: // set look
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,
187 window->SetLook(looks[rand() % 3]);
188 window->Unlock();
190 break;
192 case 9: // set feel
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,
203 window->SetFeel(feels[rand() % 3]);
204 window->Unlock();
206 break;
209 break;
212 case TEST_SINGLE_WINDOW:
213 if (fWindow) {
214 fWindow->Lock();
215 fWindow->Quit();
217 fWindow = new BWindow(fFrame, "Test", B_TITLED_WINDOW, 0);
218 fWindow->Show();
219 break;
220 default:
221 PostMessage(B_QUIT_REQUESTED);
222 break;
224 break;
225 default:
226 BApplication::MessageReceived(message);
231 TestWindow::TestWindow(BRect frame)
232 : BWindow(frame, "Test", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
234 Show();
236 BMessenger self(this);
237 BMessage message(B_QUIT_REQUESTED);
238 fPulse = new BMessageRunner(self, &message, 100000000LL, 1);
240 if (Thread() < B_OK)
241 Quit();
245 TestWindow::~TestWindow()
247 delete fPulse;
252 // main
254 main(int argc, char** argv)
256 uint32 testMode = TEST_SINGLE_WINDOW;
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;
264 if (argc > 2) {
265 gMaxCount = atol(argv[2]);
268 TestApp app(testMode);
269 app.Run();
271 return 0;