VirtualBox

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

Last change on this file since 27791 was 22269, checked in by vboxsync, 16 years ago

Additions/x11/seamless unit test: r51034 was not quite right

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 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, IsViewable }
347};
348static XRectangle g_aRectangle1[] =
349{
350 { 0, 0, 50, 50 },
351 { 50, 50, 150, 250 }
352};
353static XWindowAttributes g_aAttrib1After[] =
354{ { 200, 300, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IsViewable }
355};
356static const char *g_apszNames1[] = { "Test Window" };
357
358AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib1Before));
359AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib1After));
360AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_apszNames1));
361
362static RTRECT g_aRects1[] =
363{
364 { 200, 300, 250, 350 },
365 { 250, 350, 400, 600 }
366};
367
368static SMLSFIXTURE g_testMove =
369{
370 RT_ELEMENTS(g_ahWin1),
371 g_ahWin1,
372 g_aAttrib1Before,
373 g_apszNames1,
374 20,
375 RT_ELEMENTS(g_aRectangle1),
376 g_aRectangle1,
377 RT_ELEMENTS(g_ahWin1),
378 g_ahWin1,
379 g_aAttrib1After,
380 g_apszNames1,
381 20,
382 RT_ELEMENTS(g_aRectangle1),
383 g_aRectangle1,
384 ConfigureNotify,
385 20,
386 RT_ELEMENTS(g_aRects1),
387 g_aRects1
388};
389
390/*** Test fixture to test the code against X11 configure (resize) events ***/
391
392static XWindowAttributes g_aAttrib2Before[] =
393{ { 100, 200, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IsViewable }
394};
395static XRectangle g_aRectangle2Before[] =
396{
397 { 0, 0, 50, 50 },
398 { 50, 50, 100, 100 }
399};
400
401AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib2Before));
402
403static SMLSFIXTURE g_testResize =
404{
405 RT_ELEMENTS(g_ahWin1),
406 g_ahWin1,
407 g_aAttrib2Before,
408 g_apszNames1,
409 20,
410 RT_ELEMENTS(g_aRectangle2Before),
411 g_aRectangle2Before,
412 RT_ELEMENTS(g_ahWin1),
413 g_ahWin1,
414 g_aAttrib1After,
415 g_apszNames1,
416 20,
417 RT_ELEMENTS(g_aRectangle1),
418 g_aRectangle1,
419 ConfigureNotify,
420 20,
421 RT_ELEMENTS(g_aRects1),
422 g_aRects1
423};
424
425/*** Test fixture to test the code against X11 map events ***/
426
427static XWindowAttributes g_aAttrib3Before[] =
428{ { 200, 300, 200, 300, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IsUnmapped }
429};
430
431AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib3Before));
432
433static SMLSFIXTURE g_testMap =
434{
435 RT_ELEMENTS(g_ahWin1),
436 g_ahWin1,
437 g_aAttrib3Before,
438 g_apszNames1,
439 20,
440 RT_ELEMENTS(g_aRectangle1),
441 g_aRectangle1,
442 RT_ELEMENTS(g_ahWin1),
443 g_ahWin1,
444 g_aAttrib1After,
445 g_apszNames1,
446 20,
447 RT_ELEMENTS(g_aRectangle1),
448 g_aRectangle1,
449 MapNotify,
450 20,
451 RT_ELEMENTS(g_aRects1),
452 g_aRects1
453};
454
455/*** Test fixture to test the code against X11 unmap events ***/
456
457static XWindowAttributes g_aAttrib4After[] =
458{ { 100, 200, 300, 400, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IsUnmapped }
459};
460
461AssertCompile(RT_ELEMENTS(g_ahWin1) == RT_ELEMENTS(g_aAttrib4After));
462
463static SMLSFIXTURE g_testUnmap =
464{
465 RT_ELEMENTS(g_ahWin1),
466 g_ahWin1,
467 g_aAttrib1Before,
468 g_apszNames1,
469 20,
470 RT_ELEMENTS(g_aRectangle1),
471 g_aRectangle1,
472 RT_ELEMENTS(g_ahWin1),
473 g_ahWin1,
474 g_aAttrib4After,
475 g_apszNames1,
476 20,
477 RT_ELEMENTS(g_aRectangle1),
478 g_aRectangle1,
479 UnmapNotify,
480 20,
481 0,
482 NULL
483};
484
485/*** Test fixture to test the code against X11 shape events ***/
486
487static XRectangle g_aRectangle5Before[] =
488{
489 { 0, 0, 200, 200 }
490};
491
492static SMLSFIXTURE g_testShape =
493{
494 RT_ELEMENTS(g_ahWin1),
495 g_ahWin1,
496 g_aAttrib1After,
497 g_apszNames1,
498 20,
499 RT_ELEMENTS(g_aRectangle5Before),
500 g_aRectangle5Before,
501 RT_ELEMENTS(g_ahWin1),
502 g_ahWin1,
503 g_aAttrib1After,
504 g_apszNames1,
505 20,
506 RT_ELEMENTS(g_aRectangle1),
507 g_aRectangle1,
508 VBoxShapeNotify,
509 20,
510 RT_ELEMENTS(g_aRects1),
511 g_aRects1
512};
513
514/*** And the test code proper ***/
515
516/** Compare two RTRECT structures */
517static bool smlsCompRect(RTRECT *pFirst, RTRECT *pSecond)
518{
519 return ( (pFirst->xLeft == pSecond->xLeft)
520 && (pFirst->yTop == pSecond->yTop)
521 && (pFirst->xRight == pSecond->xRight)
522 && (pFirst->yBottom == pSecond->yBottom));
523}
524
525static void smlsPrintDiffRects(RTRECT *pExp, RTRECT *pGot)
526{
527 RTPrintf(" Expected: %d, %d, %d, %d. Got: %d, %d, %d, %d\n",
528 pExp->xLeft, pExp->yTop, pExp->xRight, pExp->yBottom,
529 pGot->xLeft, pGot->yTop, pGot->xRight, pGot->yBottom);
530}
531
532/** Run through a test fixture */
533static unsigned smlsDoFixture(SMLSFIXTURE *pFixture, const char *pszDesc)
534{
535 VBoxGuestSeamlessX11 subject;
536 unsigned cErrs = 0;
537
538 subject.init(NULL);
539 smlsSetWindowAttributes(pFixture->paAttribsBefore,
540 pFixture->pahWindowsBefore,
541 pFixture->cWindowsBefore,
542 pFixture->papszNamesBefore);
543 smlsSetShapeRectangles(pFixture->hShapeWindowBefore,
544 pFixture->cShapeRectsBefore,
545 pFixture->paShapeRectsBefore);
546 subject.start();
547 smlsSetWindowAttributes(pFixture->paAttribsAfter,
548 pFixture->pahWindowsAfter,
549 pFixture->cWindowsAfter,
550 pFixture->papszNamesAfter);
551 smlsSetShapeRectangles(pFixture->hShapeWindowAfter,
552 pFixture->cShapeRectsAfter,
553 pFixture->paShapeRectsAfter);
554 switch(pFixture->x11EventType)
555 {
556 case ConfigureNotify:
557 subject.doConfigureEvent(pFixture->hEventWindow);
558 break;
559 case MapNotify:
560 subject.doMapEvent(pFixture->hEventWindow);
561 break;
562 case UnmapNotify:
563 subject.doUnmapEvent(pFixture->hEventWindow);
564 break;
565 case VBoxShapeNotify:
566 subject.doShapeEvent(pFixture->hEventWindow);
567 break;
568 default:
569 break;
570 }
571 std::auto_ptr<std::vector<RTRECT> > rects = subject.getRects();
572 if (rects->size() != pFixture->cReportedRects)
573 {
574 RTPrintf("%s: fixture: %s. Wrong number of rectangles reported after processing event (expected %u, got %u).\n",
575 g_pszTestName, pszDesc, pFixture->cReportedRects,
576 (*rects).size());
577 ++cErrs;
578 }
579 else
580 for (unsigned i = 0; i < rects->size(); ++i)
581 if (!smlsCompRect(&(*rects)[i], &pFixture->paReportedRects[i]))
582 {
583 RTPrintf("%s: fixture: %s. Rectangle %u wrong after processing event.\n",
584 g_pszTestName, pszDesc, i);
585 smlsPrintDiffRects(&pFixture->paReportedRects[i],
586 &(*rects)[i]);
587 ++cErrs;
588 break;
589 }
590 subject.stop();
591 subject.start();
592 if (rects->size() != pFixture->cReportedRects)
593 {
594 RTPrintf("%s: fixture: %s. Wrong number of rectangles reported without processing event (expected %u, got %u).\n",
595 g_pszTestName, pszDesc, pFixture->cReportedRects,
596 (*rects).size());
597 ++cErrs;
598 }
599 else
600 for (unsigned i = 0; i < rects->size(); ++i)
601 if (!smlsCompRect(&(*rects)[i], &pFixture->paReportedRects[i]))
602 {
603 RTPrintf("%s: fixture: %s. Rectangle %u wrong without processing event.\n",
604 g_pszTestName, pszDesc, i);
605 smlsPrintDiffRects(&pFixture->paReportedRects[i],
606 &(*rects)[i]);
607 ++cErrs;
608 break;
609 }
610 return cErrs;
611}
612
613int main( int argc, char **argv)
614{
615 RTR3Init();
616 unsigned cErrs = 0;
617 g_pszTestName = RTPathFilename(argv[0]);
618
619 RTPrintf("%s: TESTING\n", g_pszTestName);
620 cErrs += smlsDoFixture(&g_testMove,
621 "ConfigureNotify event (window moved)");
622 // Currently not working
623 cErrs += smlsDoFixture(&g_testResize,
624 "ConfigureNotify event (window resized)");
625 cErrs += smlsDoFixture(&g_testMap, "MapNotify event");
626 cErrs += smlsDoFixture(&g_testUnmap, "UnmapNotify event");
627 cErrs += smlsDoFixture(&g_testShape, "ShapeNotify event");
628 if (cErrs > 0)
629 RTPrintf("%u errors\n", cErrs);
630 return cErrs == 0 ? 0 : 1;
631}
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