Initial checkin of version 0.1
[rofl0r-libxauto.git] / src / main.c
blob8bc5d037061c9951151617b8b8922bb5875b8ffd
1 /***************************************************************************
2 * Copyright (C) 2009 by Chris Parker *
3 * chrsprkr3@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the Python License version 2.5 or later. *
7 * *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
11 ***************************************************************************/
14 $URL$
15 $Author$
16 $Date$
17 $Rev$
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <X11/Xlib.h>
25 #include <assert.h>
27 #include "xaut.h"
29 char *testing = "Testing %s\n";
30 char *done = "Finished testing %s\n";
31 //The following are tests - plus they enable me to
32 // test the program with valgrind
34 void TEST_cleanup() {
35 char *routine = "TEST_cleanup";
36 printf(testing, routine);
37 assert(_check_init()); //In case this hasn't been called
38 assert(defaults);
39 cleanup();
40 assert(defaults == NULL);
41 assert(_check_init()); //So that I don't have to call this elsewhere
42 printf(done, routine);
44 void TEST_is_valid() {
45 Window win = active_window();
46 assert(is_valid(win));
47 assert(is_valid(20) == FALSE);
49 void TEST_verbose() {
50 char *routine = "TEST_verbose";
51 printf(testing, routine);
52 verbose(FALSE);
53 assert(defaults->verbose == FALSE);
54 verbose(TRUE);
55 assert(defaults->verbose);
56 verbose(FALSE);
57 printf(done, routine);
59 void TEST_extra_verbose() {
60 char *routine = "TEST_extra_verbose";
61 printf(testing, routine);
62 extra_verbose(FALSE);
63 assert(defaults->extra_verbose == FALSE);
64 extra_verbose(TRUE);
65 assert(defaults->extra_verbose);
66 verbose(FALSE);
67 extra_verbose(FALSE);
68 printf(done, routine);
70 void TEST_init_defaults() {
71 char *routine = "TEST_init_defaults";
72 printf(testing, routine);
73 assert(init_defaults());
74 printf(done, routine);
76 void TEST_display_h() {
77 char *routine = "TEST_display_h";
78 printf(testing, routine);
79 init_defaults();
80 assert(defaults->dsp_height);
81 printf(done, routine);
83 void TEST_display_w() {
84 char *routine = "TEST_display_w";
85 printf(testing, routine);
86 init_defaults();
87 assert(defaults->dsp_width);
88 printf(done, routine);
90 void TEST_desktop_count() {
91 char *routine = "TEST_desktop_count";
92 printf(testing, routine);
93 assert(desktop_count());
94 printf(done, routine);
96 void TEST_get_current_desktop() {
97 char *routine = "TEST_get_current_desktop";
98 printf(testing, routine);
99 assert(get_current_desktop());
100 printf(done, routine);
102 void TEST_set_current_desktop() {
103 char *routine = "TEST_set_current_desktop";
104 printf(testing, routine);
105 long max = desktop_count();
106 if(max == 1) {
107 return;
109 long before = get_current_desktop();
110 if(before < max) {
111 assert(set_current_desktop(max));
112 sleep(1);
113 assert(desktop_count() == get_current_desktop());
114 } else {
115 assert(set_current_desktop(max -1));
116 assert(desktop_count() != get_current_desktop());
118 set_current_desktop(before);
119 printf(done, routine);
121 void TEST_key_down_delay() {
122 char *routine = "TEST_key_down_delay";
123 printf(testing, routine);
124 unsigned short before = defaults->key_down_delay;
125 key_down_delay(before + 5);
126 unsigned short after = defaults->key_down_delay;
127 assert(before != after);
128 key_down_delay(before);
129 printf(done, routine);
131 void TEST_key_click_delay() {
132 char *routine = "TEST_key_click_delay";
133 printf(testing, routine);
134 unsigned short before = defaults->key_click_delay;
135 key_click_delay(before + 5);
136 unsigned short after = defaults->key_click_delay;
137 assert(before != after);
138 key_click_delay(before);
139 printf(done, routine);
141 void TEST_key_click() {
142 char *routine = "TEST_key_click";
143 printf(testing, routine);
144 //"36" happens to be the code for "Return" on my computer
145 assert(key_click(36));
146 printf(done, routine);
148 void TEST_key_down() {
149 char *routine = "TEST_key_down";
150 printf(testing, routine);
151 //It's necessary to send both down and up commands
152 //"100" happens to be the code for "Left" on my computer
153 assert(key_down(100));
154 assert(key_up(100));
155 printf(done, routine);
157 void TEST_key_up() {
158 char *routine = "TEST_key_up";
159 printf(testing, routine);
160 //It's necessary to send both down and up commands
161 //"100" happens to be the code for "Left" on my computer
162 assert(key_down(100));
163 assert(key_up(100));
164 printf(done, routine);
166 void TEST_interpret_meta_symbols() {
167 char *routine = "TEST_interpret_meta_symbols";
168 printf(testing, routine);
169 unsigned short before = defaults->interpret_meta_symbols;
170 unsigned short neu = before ? FALSE : TRUE;
171 interpret_meta_symbols(neu);
172 unsigned short after = defaults->interpret_meta_symbols;
173 assert(before != after);
174 interpret_meta_symbols(before);
175 printf(done, routine);
177 void TEST_print_keycodes() {
178 char *routine = "TEST_print_keycodes";
179 printf(testing, routine);
180 //I can't think of anything to "test". This is here
181 //to give valgrind something to look at while checking
182 //for memory leaks.
183 print_keycodes();
184 printf(done, routine);
186 void TEST_type() {
187 char *routine = "TEST_type";
188 printf(testing, routine);
189 assert(type("{Return}"));
190 printf(done, routine);
192 void TEST_mouse_move_delay() {
193 char *routine = "TEST_mouse_move_delay";
194 printf(testing, routine);
195 unsigned short before = defaults->mouse_move_delay;
196 mouse_move_delay(before + 5);
197 unsigned short after = defaults->mouse_move_delay;
198 assert(before != after);
199 mouse_move_delay(before);
200 printf(done, routine);
202 void TEST_mouse_down_delay() {
203 char *routine = "TEST_mouse_down_delay";
204 printf(testing, routine);
205 unsigned short before = defaults->mouse_down_delay;
206 mouse_down_delay(before + 5);
207 unsigned short after = defaults->mouse_down_delay;
208 assert(before != after);
209 mouse_down_delay(before);
210 printf(done, routine);
212 void TEST_mouse_click_delay() {
213 char *routine = "TEST_mouse_click_delay";
214 printf(testing, routine);
215 unsigned short before = defaults->mouse_click_delay;
216 mouse_click_delay(before + 5);
217 unsigned short after = defaults->mouse_click_delay;
218 assert(before != after);
219 mouse_click_delay(before);
220 printf(done, routine);
222 void TEST_mouse_click() {
223 char *routine = "TEST_mouse_click";
224 printf(testing, routine);
225 assert(mouse_click(1, 1));
226 printf(done, routine);
228 void TEST_mouse_down() {
229 char *routine = "TEST_mouse_down";
230 printf(testing, routine);
231 //It is necessary to include both down and up
232 assert(mouse_down(1));
233 assert(mouse_up(1));
234 printf(done, routine);
236 void TEST_mouse_up() {
237 char *routine = "TEST_mouse_up";
238 printf(testing, routine);
239 //It is necessary to include both down and up
240 assert(mouse_down(1));
241 assert(mouse_up(1));
242 printf(done, routine);
244 void TEST_move_mouse() {
245 char *routine = "TEST_move_mouse";
246 printf(testing, routine);
247 //First we test with no window
248 int before_x = mouse_x(0);
249 int before_y = mouse_y(0);
250 assert(move_mouse(0, 0, 0));
251 int after_x = mouse_x(0);
252 int after_y = mouse_y(0);
253 assert(after_x == 0 && after_y == 0);
255 //Now we try it with a valid window
256 Window win = active_window();
257 activate_window(win);
258 assert(move_mouse(10, 10, win));
259 after_x = mouse_x(win);
260 after_y = mouse_y(win);
261 assert(after_x == 10 && after_y == 10);
263 //Finally, let's make sure it doesn't bomb with an invalid window
264 //The correct action is to leave the mouse where it is
265 assert(move_mouse(20, 20, 20) == FALSE);
266 assert(mouse_x(0) != 20 && mouse_y(0) != 20);
268 move_mouse(before_x, before_y, 0);
269 printf(done, routine);
271 void TEST_mouse_x() {
272 char *routine = "TEST_mouse_x";
273 printf(testing, routine);
275 //There's no way to know if the values we receive are "correct",
276 //so we can only test to make sure it doesn't cause problems
278 //First with no window
279 mouse_x(0);
281 //Then with a valid window
282 Window win = active_window();
283 mouse_x(win);
287 //Finally with an invalid window - which should return INT_MIN
288 int invalid_x = mouse_x(20);
289 assert(invalid_x == INT_MIN);
290 printf(done, routine);
292 void TEST_mouse_y() {
293 char *routine = "TEST_mouse_y";
294 printf(testing, routine);
296 //There's no way to know if the values we receive are "correct",
297 //so we can only test to make sure it doesn't cause problems
299 //First with no window
300 mouse_y(0);
302 //Then with a valid window
303 Window win = active_window();
304 mouse_y(win);
308 //Finally with an invalid window - which should return INT_MIN
309 int invalid_y = mouse_y(20);
310 assert(invalid_y == INT_MIN);
311 printf(done, routine);
313 void TEST_activate_window() {
314 char *routine = "TEST_activate_window";
315 printf(testing, routine);
316 //First with a valid window
317 Window win = active_window();
318 assert(activate_window(win));
320 //Then with an invalid window
321 assert(activate_window(20) == FALSE);
323 void TEST_active_window() {
324 char *routine = "TEST_active_window";
325 printf(testing, routine);
326 assert(active_window());
327 printf(done, routine);
329 void TEST_find_window() {
330 char *routine = "TEST_find_window";
331 printf(testing, routine);
332 //Note that this fails when run in Eclipse...
333 Window active = active_window();
334 char *name = window_name(active);
335 if(name == NULL) {
336 printf("Aborting %s - conditions are invalid\n", routine);
337 return;
339 Window found = find_window(name);
340 assert(found == active);
341 free(name);
342 printf(done, routine);
344 void TEST_search_for_window() {
345 char *routine = "TEST_search_for_window";
346 printf(testing, routine);
347 Window active = active_window();
348 char *name = window_name(active);
349 Window* found = search_for_window(name);
350 //Note that this fails when run in Eclipse...
351 assert(found);
352 free(name);
353 printf(done, routine);
355 void TEST_maximize_window() {
356 char *routine = "TEST_maximize_window";
357 printf(testing, routine);
358 //First with a valid window
359 Window win = active_window();
360 assert(maximize_window(win, TRUE));
361 assert(maximize_window(win, FALSE));
362 activate_window(win);
364 //Then with an invalid window
365 assert(maximize_window(20, TRUE) == FALSE);
366 printf(done, routine);
368 void TEST_maximize_window_horz() {
369 char *routine = "TEST_maximize_horz";
370 printf(testing, routine);
371 //First with a valid window
372 Window win = active_window();
373 assert(maximize_window_horz(win, TRUE));
374 assert(maximize_window(win, FALSE));
375 activate_window(win);
377 //Then with an invalid window
378 assert(maximize_window_horz(20, TRUE) == FALSE);
379 printf(done, routine);
381 void TEST_maximize_window_vert() {
382 char *routine = "TEST_maximize_vert";
383 printf(testing, routine);
384 //First with a valid window
385 Window win = active_window();
386 assert(maximize_window_vert(win, TRUE));
387 assert(maximize_window_vert(win, FALSE));
388 activate_window(win);
390 //Then with an invalid window
391 assert(maximize_window_vert(20, TRUE) == FALSE);
392 printf(done, routine);
394 void TEST_minimize_window() {
395 char *routine = "TEST_minimize_window";
396 printf(testing, routine);
397 //First with a valid window
398 Window win = active_window();
399 assert(minimize_window(win, TRUE));
400 assert(minimize_window(win, FALSE));
401 activate_window(win);
403 //Then with an invalid window
404 assert(minimize_window(20, TRUE) == FALSE);
405 assert(minimize_window(20, FALSE) == FALSE);
406 printf(done, routine);
408 void TEST_iconify_window() {
409 char *routine = "TEST_iconify_window";
410 printf(testing, routine);
411 //First with a valid window
412 Window win = active_window();
413 assert(iconify_window(win, TRUE));
414 assert(iconify_window(win, FALSE));
415 activate_window(win);
417 //Then with an invalid window
418 assert(iconify_window(20, TRUE) == FALSE);
419 assert(iconify_window(20, FALSE) == FALSE);
420 printf(done, routine);
422 void TEST_full_screen_window() {
423 char *routine = "TEST_full_screen_window";
424 printf(testing, routine);
425 //First with a valid window
426 Window win = active_window();
427 assert(full_screen_window(win, TRUE));
428 assert(full_screen_window(win, FALSE));
429 activate_window(win);
431 //Then with an invalid window
432 assert(full_screen_window(20, TRUE) == FALSE);
433 assert(full_screen_window(20, FALSE) == FALSE);
434 printf(done, routine);
436 void TEST_shade_window() {
437 char *routine = "TEST_shade_window";
438 printf(testing, routine);
439 //First with a valid window
440 Window win = active_window();
441 assert(shade_window(win, TRUE));
442 assert(shade_window(win, FALSE));
443 activate_window(win);
445 //Then with an invalid window
446 assert(shade_window(20, TRUE) == FALSE);
447 assert(shade_window(20, FALSE) == FALSE);
448 printf(done, routine);
450 void TEST_restore_window() {
451 char *routine = "TEST_restore_window";
452 printf(testing, routine);
453 //First with a valid window
454 Window win = active_window();
455 assert(restore_window(win));
456 activate_window(win);
458 //Then with an invalid window
459 assert(restore_window(20) == FALSE);
460 printf(done, routine);
462 void TEST_move_window() {
463 char *routine = "TEST_move_window";
464 printf(testing, routine);
465 //First with a valid window
466 Window win = active_window();
467 int before_x = window_x(win);
468 int before_y = window_y(win);
469 assert(move_window(win, before_x + 10, before_y + 10, -1));
470 int after_x = window_x(win);
471 int after_y = window_y(win);
473 //"Snap to grid" makes this a bit hard to test.
474 //What if the grid isn't "10"? If it's e.g. 12,
475 //then we'll be off by 2 pixels.
476 assert(before_x != after_x);
477 assert(before_y != after_y);
478 move_window(win, before_x, before_y, -1);
479 activate_window(win);
481 //Now with an invalid window
482 assert(move_window(20, 0, 0, -1) == FALSE);
483 printf(done, routine);
485 void TEST_window_x() {
486 char *routine = "TEST_window_x";
487 printf(testing, routine);
488 //First a valid window
489 Window win = active_window();
490 int x = window_x(win);
491 assert(x > INT_MIN);
493 //Then an invalid window
494 assert(window_x(20) == INT_MIN);
495 printf(done, routine);
497 void TEST_window_y() {
498 char *routine = "TEST_window_y";
499 printf(testing, routine);
500 //First a valid window
501 Window win = active_window();
502 int y = window_y(win);
503 assert(y > INT_MIN);
505 //Then an invalid window
506 assert(window_y(20) == INT_MIN);
507 printf(done, routine);
509 void TEST_window_w() {
510 char *routine = "TEST_window_w";
511 printf(testing, routine);
512 //First a valid window
513 Window win = active_window();
514 int w = window_w(win);
515 assert(w > INT_MIN);
517 //Then an invalid window
518 assert(window_w(20) == INT_MIN);
519 printf(done, routine);
521 void TEST_window_h() {
522 char *routine = "TEST_window_h";
523 printf(testing, routine);
524 //First a valid window
525 Window win = active_window();
526 int h = window_h(win);
527 assert(h > INT_MIN);
529 //Then an invalid window
530 assert(window_h(20) == INT_MIN);
531 printf(done, routine);
533 void TEST_window_name() {
534 char *routine = "TEST_window_name";
535 printf(testing, routine);
536 //First a valid window
537 Window win = active_window();
538 char *name = window_name(win);
539 //This fails when run from Eclipse
540 assert(name);
541 free(name);
543 //Now an invalid window
544 char *bad = window_name(20);
545 assert(bad == NULL);
546 free(bad); //Just in case it's not null
547 printf(done, routine);
549 void TEST_window_desktop() {
550 char *routine = "TEST_window_desktop";
551 printf(testing, routine);
552 //First a valid window
553 Window win = active_window();
554 long desk = window_desktop(win);
555 assert(desk > -1);
557 //Then an invalid window
558 long bad = window_desktop(20);
559 assert(bad == LONG_MIN);
560 printf(done, routine);
563 int main(int argc, char* argv[]) {
564 Window win = active_window();
565 printf("Running validation tests...\n");
566 printf("For best results, run this from a prompt in a window that's");
567 printf(" on it's own desktop.\n");
568 printf("Also, make sure the mouse starts in the middle");
569 printf(" of the title bar.\n");
570 sleep(3);
571 TEST_cleanup();
572 TEST_is_valid();
574 // return 0;
575 TEST_verbose();
576 TEST_extra_verbose();
577 TEST_init_defaults();
578 TEST_display_h();
579 TEST_display_w();
580 TEST_desktop_count();
581 TEST_get_current_desktop();
582 TEST_set_current_desktop();
583 sleep(1);
584 TEST_key_down_delay();
585 TEST_key_click_delay();
586 TEST_key_click();
587 sleep(1);
588 TEST_key_down();
589 TEST_key_up();
590 sleep(1);
591 TEST_interpret_meta_symbols();
592 TEST_print_keycodes();
593 TEST_type();
594 sleep(1);
595 TEST_mouse_move_delay();
596 TEST_mouse_down_delay();
597 TEST_mouse_click_delay();
598 TEST_mouse_click();
599 activate_window(win);
600 sleep(1);
601 TEST_mouse_down();
602 activate_window(win);
603 sleep(1);
604 TEST_mouse_up();
605 activate_window(win);
606 sleep(1);
607 TEST_move_mouse();
608 TEST_mouse_x();
609 TEST_mouse_y();
610 sleep(1);
611 TEST_activate_window();
612 sleep(1);
613 TEST_active_window();
614 sleep(1);
615 TEST_find_window();
616 sleep(1);
617 TEST_search_for_window();
618 sleep(1);
619 TEST_maximize_window();
620 sleep(1);
621 TEST_maximize_window_horz();
622 sleep(1);
623 TEST_maximize_window_vert();
624 sleep(1);
625 TEST_minimize_window();
626 sleep(1);
627 TEST_iconify_window();
628 sleep(1);
629 TEST_full_screen_window();
630 sleep(1);
631 TEST_shade_window();
632 sleep(1);
633 TEST_restore_window();
634 sleep(1);
635 TEST_move_window();
636 sleep(1);
637 TEST_window_x();
638 TEST_window_y();
639 TEST_window_w();
640 TEST_window_h();
641 TEST_window_name();
642 TEST_window_desktop();
643 cleanup();
645 return 0;