VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/testcase/tstSeamlessX11-auto.cpp@ 22232

Last change on this file since 22232 was 22232, checked in by vboxsync, 15 years ago

gcc warnings (not important)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.0 KB
Line 
1/** @file
2 * Automated test of the X11 seamless Additions code.
3 */
4
5/*
6 * Copyright (C) 2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21#include <iostream>
22#include <stdlib.h> /* exit() */
23
24#include <X11/Xatom.h>
25#include <X11/Xmu/WinUtil.h>
26
27#include <iprt/initterm.h>
28#include <iprt/mem.h>
29#include <iprt/path.h>
30#include <iprt/semaphore.h>
31#include <iprt/stream.h>
32#include <iprt/string.h>
33
34#include "../seamless.h"
35
36#undef DefaultRootWindow
37
38/******************************************************
39* Mock X11 functions needed by the seamless X11 class *
40******************************************************/
41
42int XFree(void *data)
43{
44 RTMemFree(data);
45 return 0;
46}
47
48#define TEST_DISPLAY ((Display *)0xffff)
49#define TEST_ROOT ((Window)1)
50
51extern "C" Display *XOpenDisplay(const char *display_name);
52Display *XOpenDisplay(const char *display_name)
53{
54 return TEST_DISPLAY;
55}
56
57extern "C" int XCloseDisplay(Display *display);
58int XCloseDisplay(Display *display)
59{
60 Assert(display == TEST_DISPLAY);
61 return 0;
62}
63
64enum
65{
66 ATOM_PROP = 1,
67 ATOM_DESKTOP_PROP
68};
69
70extern "C" Atom XInternAtom(Display *display, const char *atom_name,
71 Bool only_if_exists);
72Atom XInternAtom(Display *display, const char *atom_name, Bool only_if_exists)
73{
74 if (!RTStrCmp(atom_name, WM_TYPE_PROP))
75 return (Atom) ATOM_PROP;
76 if (!RTStrCmp(atom_name, WM_TYPE_DESKTOP_PROP))
77 return (Atom) ATOM_DESKTOP_PROP;
78 AssertFailed();
79 return (Atom)0;
80}
81
82/** The window (if any) on which the WM_TYPE_PROP property is set to the
83 * WM_TYPE_DESKTOP_PROP atom. */
84static Window g_hSmlsDesktopWindow = 0;
85
86extern "C" int XGetWindowProperty(Display *display, Window w, Atom property,
87 long long_offset, long long_length,
88 Bool delProp, Atom req_type,
89 Atom *actual_type_return,
90 int *actual_format_return,
91 unsigned long *nitems_return,
92 unsigned long *bytes_after_return,
93 unsigned char **prop_return);
94int XGetWindowProperty(Display *display, Window w, Atom property,
95 long long_offset, long long_length, Bool delProp,
96 Atom req_type, Atom *actual_type_return,
97 int *actual_format_return,
98 unsigned long *nitems_return,
99 unsigned long *bytes_after_return,
100 unsigned char **prop_return)
101{
102 Atom atomType = XInternAtom (NULL, WM_TYPE_PROP, true);
103 Atom atomTypeDesktop = XInternAtom (NULL, WM_TYPE_DESKTOP_PROP, true);
104 /* We only handle things we expect. */
105 AssertReturn((req_type == XA_ATOM) || (req_type == AnyPropertyType),
106 0xffff);
107 AssertReturn(property == atomType, 0xffff);
108 *actual_type_return = XA_ATOM;
109 *actual_format_return = sizeof(Atom) * 8;
110 *nitems_return = 0;
111 *bytes_after_return = sizeof(Atom);
112 *prop_return = NULL;
113 if ((w != g_hSmlsDesktopWindow) || (g_hSmlsDesktopWindow == 0))
114 return Success;
115 AssertReturn(long_offset == 0, 0);
116 AssertReturn(delProp == false, 0);
117 unsigned char *pProp;
118 pProp = (unsigned char *)RTMemDup(&atomTypeDesktop,
119 sizeof(atomTypeDesktop));
120 AssertReturn(pProp, 0xffff);
121 *nitems_return = 1;
122 *prop_return = pProp;
123 *bytes_after_return = 0;
124 return 0;
125}
126
127/** Sets the current set of properties for all mock X11 windows */
128static void smlsSetDesktopWindow(Window hWin)
129{
130 g_hSmlsDesktopWindow = hWin;
131}
132
133extern "C" Bool XShapeQueryExtension (Display *dpy, int *event_basep,
134 int *error_basep);
135Bool XShapeQueryExtension (Display *dpy, int *event_basep, int *error_basep)
136{
137 return true;
138}
139
140/* We silently ignore this for now. */
141extern "C" int XSelectInput(Display *display, Window w, long event_mask);
142int XSelectInput(Display *display, Window w, long event_mask)
143{
144 return 0;
145}
146
147/* We silently ignore this for now. */
148extern "C" void XShapeSelectInput(Display *display, Window w,
149 unsigned long event_mask);
150void XShapeSelectInput(Display *display, Window w, unsigned long event_mask)
151{}
152
153extern "C" Window XDefaultRootWindow(Display *display);
154Window XDefaultRootWindow(Display *display)
155{
156 return TEST_ROOT;
157}
158
159static unsigned g_cSmlsWindows = 0;
160static Window *g_paSmlsWindows = NULL;
161static XWindowAttributes *g_paSmlsWinAttribs = NULL;
162static const char **g_papszSmlsWinNames = NULL;
163
164extern "C" Status XQueryTree(Display *display, Window w, Window *root_return,
165 Window *parent_return, Window **children_return,
166 unsigned int *nchildren_return);
167Status XQueryTree(Display *display, Window w, Window *root_return,
168 Window *parent_return, Window **children_return,
169 unsigned int *nchildren_return)
170{
171 AssertReturn(w == TEST_ROOT, False); /* We support nothing else */
172 AssertPtrReturn(children_return, False);
173 AssertReturn(g_paSmlsWindows, False);
174 if (root_return)
175 *root_return = TEST_ROOT;
176 if (parent_return)
177 *parent_return = TEST_ROOT;
178 *children_return = (Window *)RTMemDup(g_paSmlsWindows,
179 g_cSmlsWindows * sizeof(Window));
180 if (nchildren_return)
181 *nchildren_return = g_cSmlsWindows;
182 return (g_cSmlsWindows != 0);
183}
184
185extern "C" Window XmuClientWindow(Display *dpy, Window win);
186Window XmuClientWindow(Display *dpy, Window win)
187{
188 return win;
189}
190
191extern "C" Status XGetWindowAttributes(Display *display, Window w,
192 XWindowAttributes *window_attributes_return);
193Status XGetWindowAttributes(Display *display, Window w,
194 XWindowAttributes *window_attributes_return)
195{
196 AssertPtrReturn(window_attributes_return, 1);
197 for (unsigned i = 0; i < g_cSmlsWindows; ++i)
198 if (g_paSmlsWindows[i] == w)
199 {
200 *window_attributes_return = g_paSmlsWinAttribs[i];
201 return 1;
202 }
203 return 0;
204}
205
206extern "C" Status XGetWMNormalHints(Display *display, Window w,
207 XSizeHints *hints_return,
208 long *supplied_return);
209
210Status XGetWMNormalHints(Display *display, Window w,
211 XSizeHints *hints_return, long *supplied_return)
212{
213 return 1;
214}
215
216static void smlsSetWindowAttributes(XWindowAttributes *pAttribs,
217 Window *pWindows, unsigned cAttribs,
218 const char **paNames)
219{
220 g_paSmlsWinAttribs = pAttribs;
221 g_paSmlsWindows = pWindows;
222 g_cSmlsWindows = cAttribs;
223 g_papszSmlsWinNames = paNames;
224}
225
226static Window g_SmlsShapedWindow = 0;
227static int g_cSmlsShapeRectangles = 0;
228static XRectangle *g_pSmlsShapeRectangles = NULL;
229
230extern "C" XRectangle *XShapeGetRectangles (Display *dpy, Window window,
231 int kind, int *count,
232 int *ordering);
233XRectangle *XShapeGetRectangles (Display *dpy, Window window, int kind,
234 int *count, int *ordering)
235{
236 if ((window != g_SmlsShapedWindow) || (window == 0))
237 return NULL; /* Probably not correct, but works for us. */
238 *count = g_cSmlsShapeRectangles;
239 *ordering = 0;
240 return (XRectangle *)RTMemDup(g_pSmlsShapeRectangles,
241 sizeof(XRectangle)
242 * g_cSmlsShapeRectangles);
243}
244
245static void smlsSetShapeRectangles(Window window, int cRects,
246 XRectangle *pRects)
247{
248 g_SmlsShapedWindow = window;
249 g_cSmlsShapeRectangles = cRects;
250 g_pSmlsShapeRectangles = pRects;
251}
252
253/* This should not be needed in the bits of the code we test. */
254extern "C" int XNextEvent(Display *display, XEvent *event_return);
255int XNextEvent(Display *display, XEvent *event_return)
256{
257 AssertFailedReturn(0);
258}
259
260/* This should not be needed in the bits of the code we test. */
261extern "C" Status XSendEvent(Display *display, Window w, Bool propagate,
262 long event_mask, XEvent *event_send);
263Status XSendEvent(Display *display, Window w, Bool propagate,
264 long event_mask, XEvent *event_send)
265{
266 AssertFailedReturn(0);
267}
268
269/* This should not be needed in the bits of the code we test. */
270extern "C" int XFlush(Display *display);
271int XFlush(Display *display)
272{
273 AssertFailedReturn(0);
274}
275
276/*****************************
277* The actual tests to be run *
278*****************************/
279
280/** The name of the unit test */
281static const char *g_pszTestName = NULL;
282
283/*** Test fixture data and data structures ***/
284
285/** A structure describing a test fixture to be run through. Each fixture
286 * describes the state of the windows visible (and unmapped) on the X server
287 * before and after a particular event is delivered, and the expected
288 * on-screen positions of all interesting visible windows at the end of the
289 * fixture as reported by the code (currently in the order it is likely to
290 * report them in, @todo sort this). We expect that the set of visible
291 * windows will be the same whether we start the code before the event and
292 * handle it or start the code after the event.
293 *
294 * If it is ever needed I could write a small tool to record a fixture on
295 * a live guest, but I will put that off as long as I can.
296 */
297struct SMLSFIXTURE
298{
299 /** The number of windows visible before the event */
300 unsigned cWindowsBefore;
301 /** An array of Window IDs for the visible and unmapped windows before
302 * the event */
303 Window *pahWindowsBefore;
304 /** The window attributes matching the windows in @a paWindowsBefore */
305 XWindowAttributes *paAttribsBefore;
306 /** The window names matching the windows in @a paWindowsBefore */
307 const char **papszNamesBefore;
308 /** The shaped window before the event - we allow at most one of these.
309 * Zero for none. */
310 Window hShapeWindowBefore;
311 /** The number of rectangles in the shaped window before the event. */
312 int cShapeRectsBefore;
313 /** The rectangles in the shaped window before the event */
314 XRectangle *paShapeRectsBefore;
315 /** The number of windows visible after the event */
316 unsigned cWindowsAfter;
317 /** An array of Window IDs for the visible and unmapped windows after
318 * the event */
319 Window *pahWindowsAfter;
320 /** The window attributes matching the windows in @a paWindowsAfter */
321 XWindowAttributes *paAttribsAfter;
322 /** The window names matching the windows in @a paWindowsAfter */
323 const char **papszNamesAfter;
324 /** The shaped window after the event - we allow at most one of these.
325 * Zero for none. */
326 Window hShapeWindowAfter;
327 /** The number of rectangles in the shaped window after the event. */
328 int cShapeRectsAfter;
329 /** The rectangles in the shaped window after the event */
330 XRectangle *paShapeRectsAfter;
331 /** The event to delivered */
332 int x11EventType;
333 /** The windows for which the event in @enmEvent is delivered */
334 Window hEventWindow;
335 /** The number of windows expected to be reported at the end of the
336 * fixture */
337 unsigned cReportedRects;
338 /** The onscreen positions of those windows. */
339 RTRECT *paReportedRects;
340};
341
342/*** Test fixture to test the code against X11 configure (move) events ***/
343
344static Window g_ahWin1[] = { 20 };
345static XWindowAttributes g_aAttrib1Before[] =
346{ { 100, 200, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
347 IsViewable }
348};
349static XRectangle g_aRectangle1[] =
350{
351 { 0, 0, 50, 50 },
352 { 50, 50, 150, 250 }
353};
354static XWindowAttributes g_aAttrib1After[] =
355{ { 200, 300, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
356 IsViewable }
357};
358static const char *g_apszNames1[] = { "Test Window" };
359
360AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib1Before));
361AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib1After));
362AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_apszNames1));
363
364static RTRECT g_aRects1[] =
365{
366 { 200, 300, 250, 350 },
367 { 250, 350, 400, 600 }
368};
369
370static SMLSFIXTURE g_testMove =
371{
372 RT_ELEMENTS(g_ahWin1),
373 g_ahWin1,
374 g_aAttrib1Before,
375 g_apszNames1,
376 20,
377 RT_ELEMENTS(g_aRectangle1),
378 g_aRectangle1,
379 RT_ELEMENTS(g_ahWin1),
380 g_ahWin1,
381 g_aAttrib1After,
382 g_apszNames1,
383 20,
384 RT_ELEMENTS(g_aRectangle1),
385 g_aRectangle1,
386 ConfigureNotify,
387 20,
388 RT_ELEMENTS(g_aRects1),
389 g_aRects1
390};
391
392/*** Test fixture to test the code against X11 configure (resize) events ***/
393
394static XWindowAttributes g_aAttrib2Before[] =
395{ { 100, 200, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
396 IsViewable }
397};
398static XRectangle g_aRectangle2Before[] =
399{
400 { 0, 0, 50, 50 },
401 { 50, 50, 100, 100 }
402};
403
404AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib2Before));
405
406static SMLSFIXTURE g_testResize =
407{
408 RT_ELEMENTS(g_ahWin1),
409 g_ahWin1,
410 g_aAttrib2Before,
411 g_apszNames1,
412 20,
413 RT_ELEMENTS(g_aRectangle2Before),
414 g_aRectangle2Before,
415 RT_ELEMENTS(g_ahWin1),
416 g_ahWin1,
417 g_aAttrib1After,
418 g_apszNames1,
419 20,
420 RT_ELEMENTS(g_aRectangle1),
421 g_aRectangle1,
422 ConfigureNotify,
423 20,
424 RT_ELEMENTS(g_aRects1),
425 g_aRects1
426};
427
428/*** Test fixture to test the code against X11 map events ***/
429
430static XWindowAttributes g_aAttrib3Before[] =
431{ { 200, 300, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
432 IsUnmapped }
433};
434
435AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib3Before));
436
437static SMLSFIXTURE g_testMap =
438{
439 RT_ELEMENTS(g_ahWin1),
440 g_ahWin1,
441 g_aAttrib3Before,
442 g_apszNames1,
443 20,
444 RT_ELEMENTS(g_aRectangle1),
445 g_aRectangle1,
446 RT_ELEMENTS(g_ahWin1),
447 g_ahWin1,
448 g_aAttrib1After,
449 g_apszNames1,
450 20,
451 RT_ELEMENTS(g_aRectangle1),
452 g_aRectangle1,
453 MapNotify,
454 20,
455 RT_ELEMENTS(g_aRects1),
456 g_aRects1
457};
458
459/*** Test fixture to test the code against X11 unmap events ***/
460
461static XWindowAttributes g_aAttrib4After[] =
462{ { 100, 200, 300, 400, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
463 IsUnmapped }
464};
465
466AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib4After));
467
468static SMLSFIXTURE g_testUnmap =
469{
470 RT_ELEMENTS(g_ahWin1),
471 g_ahWin1,
472 g_aAttrib1Before,
473 g_apszNames1,
474 20,
475 RT_ELEMENTS(g_aRectangle1),
476 g_aRectangle1,
477 RT_ELEMENTS(g_ahWin1),
478 g_ahWin1,
479 g_aAttrib4After,
480 g_apszNames1,
481 20,
482 RT_ELEMENTS(g_aRectangle1),
483 g_aRectangle1,
484 UnmapNotify,
485 20,
486 0,
487 NULL
488};
489
490/*** Test fixture to test the code against X11 shape events ***/
491
492static XRectangle g_aRectangle5Before[] =
493{
494 { 0, 0, 200, 200 }
495};
496
497static SMLSFIXTURE g_testShape =
498{
499 RT_ELEMENTS(g_ahWin1),
500 g_ahWin1,
501 g_aAttrib1After,
502 g_apszNames1,
503 20,
504 RT_ELEMENTS(g_aRectangle5Before),
505 g_aRectangle5Before,
506 RT_ELEMENTS(g_ahWin1),
507 g_ahWin1,
508 g_aAttrib1After,
509 g_apszNames1,
510 20,
511 RT_ELEMENTS(g_aRectangle1),
512 g_aRectangle1,
513 VBoxShapeNotify,
514 20,
515 RT_ELEMENTS(g_aRects1),
516 g_aRects1
517};
518
519/*** And the test code proper ***/
520
521/** Compare two RTRECT structures */
522static bool smlsCompRect(RTRECT *pFirst, RTRECT *pSecond)
523{
524 return ( (pFirst->xLeft == pSecond->xLeft)
525 && (pFirst->yTop == pSecond->yTop)
526 && (pFirst->xRight == pSecond->xRight)
527 && (pFirst->yBottom == pSecond->yBottom));
528}
529
530static void smlsPrintDiffRects(RTRECT *pExp, RTRECT *pGot)
531{
532 RTPrintf(" Expected: %d, %d, %d, %d. Got: %d, %d, %d, %d\n",
533 pExp->xLeft, pExp->yTop, pExp->xRight, pExp->yBottom,
534 pGot->xLeft, pGot->yTop, pGot->xRight, pGot->yBottom);
535}
536
537/** Run through a test fixture */
538static unsigned smlsDoFixture(SMLSFIXTURE *pFixture, const char *pszDesc)
539{
540 VBoxGuestSeamlessX11 subject;
541 unsigned cErrs = 0;
542
543 subject.init(NULL);
544 smlsSetWindowAttributes(pFixture->paAttribsBefore,
545 pFixture->pahWindowsBefore,
546 pFixture->cWindowsBefore,
547 pFixture->papszNamesBefore);
548 smlsSetShapeRectangles(pFixture->hShapeWindowBefore,
549 pFixture->cShapeRectsBefore,
550 pFixture->paShapeRectsBefore);
551 subject.start();
552 smlsSetWindowAttributes(pFixture->paAttribsAfter,
553 pFixture->pahWindowsAfter,
554 pFixture->cWindowsAfter,
555 pFixture->papszNamesAfter);
556 smlsSetShapeRectangles(pFixture->hShapeWindowAfter,
557 pFixture->cShapeRectsAfter,
558 pFixture->paShapeRectsAfter);
559 switch(pFixture->x11EventType)
560 {
561 case ConfigureNotify:
562 subject.doConfigureEvent(pFixture->hEventWindow);
563 break;
564 case MapNotify:
565 subject.doMapEvent(pFixture->hEventWindow);
566 break;
567 case UnmapNotify:
568 subject.doUnmapEvent(pFixture->hEventWindow);
569 break;
570 case VBoxShapeNotify:
571 subject.doShapeEvent(pFixture->hEventWindow);
572 break;
573 default:
574 break;
575 }
576 std::auto_ptr<std::vector<RTRECT> > rects = subject.getRects();
577 if (rects->size() != pFixture->cReportedRects)
578 {
579 RTPrintf("%s: fixture: %s. Wrong number of rectangles reported after processing event (expected %u, got %u).\n",
580 g_pszTestName, pszDesc, pFixture->cReportedRects,
581 (*rects).size());
582 ++cErrs;
583 }
584 else
585 for (unsigned i = 0; i < rects->size(); ++i)
586 if (!smlsCompRect(&(*rects)[i], &pFixture->paReportedRects[i]))
587 {
588 RTPrintf("%s: fixture: %s. Rectangle %u wrong after processing event.\n",
589 g_pszTestName, pszDesc, i);
590 smlsPrintDiffRects(&pFixture->paReportedRects[i],
591 &(*rects)[i]);
592 ++cErrs;
593 break;
594 }
595 subject.stop();
596 subject.start();
597 if (rects->size() != pFixture->cReportedRects)
598 {
599 RTPrintf("%s: fixture: %s. Wrong number of rectangles reported without processing event (expected %u, got %u).\n",
600 g_pszTestName, pszDesc, pFixture->cReportedRects,
601 (*rects).size());
602 ++cErrs;
603 }
604 else
605 for (unsigned i = 0; i < rects->size(); ++i)
606 if (!smlsCompRect(&(*rects)[i], &pFixture->paReportedRects[i]))
607 {
608 RTPrintf("%s: fixture: %s. Rectangle %u wrong without processing event.\n",
609 g_pszTestName, pszDesc, i);
610 smlsPrintDiffRects(&pFixture->paReportedRects[i],
611 &(*rects)[i]);
612 ++cErrs;
613 break;
614 }
615 return cErrs;
616}
617
618int main( int argc, char **argv)
619{
620 RTR3Init();
621 unsigned cErrs = 0;
622 g_pszTestName = RTPathFilename(argv[0]);
623
624 RTPrintf("%s: TESTING\n", g_pszTestName);
625 cErrs += smlsDoFixture(&g_testMove,
626 "ConfigureNotify event (window moved)");
627 // Currently not working
628 cErrs += smlsDoFixture(&g_testResize,
629 "ConfigureNotify event (window resized)");
630 cErrs += smlsDoFixture(&g_testMap, "MapNotify event");
631 cErrs += smlsDoFixture(&g_testUnmap, "UnmapNotify event");
632 cErrs += smlsDoFixture(&g_testShape, "ShapeNotify event");
633 if (cErrs > 0)
634 RTPrintf("%u errors\n", cErrs);
635 return cErrs == 0 ? 0 : 1;
636}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette