1 | /* $Id: tstMouseImpl.cpp 47275 2013-07-19 17:43:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Main unit test - Mouse class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | ******************************************************************************/
|
---|
21 | #include "MouseImpl.h"
|
---|
22 | #include "VMMDev.h"
|
---|
23 | #include "DisplayImpl.h"
|
---|
24 |
|
---|
25 | #include <VBox/vmm/cfgm.h>
|
---|
26 | #include <VBox/vmm/pdmdrv.h>
|
---|
27 | #include <VBox/VMMDev.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/test.h>
|
---|
30 |
|
---|
31 | #ifndef RT_OS_WINDOWS
|
---|
32 | NS_DECL_CLASSINFO(Mouse)
|
---|
33 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(Mouse, IMouse)
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | PDMIVMMDEVPORT VMMDevPort;
|
---|
37 |
|
---|
38 | class TestVMMDev : public VMMDevMouseInterface
|
---|
39 | {
|
---|
40 | PPDMIVMMDEVPORT getVMMDevPort(void) { return &VMMDevPort; }
|
---|
41 | };
|
---|
42 |
|
---|
43 | class TestDisplay : public DisplayMouseInterface
|
---|
44 | {
|
---|
45 | void getFramebufferDimensions(int32_t *px1, int32_t *py1,
|
---|
46 | int32_t *px2, int32_t *py2);
|
---|
47 | int getScreenResolution(uint32_t cScreen, ULONG *pcx, ULONG *pcy,
|
---|
48 | ULONG *pcBPP);
|
---|
49 | };
|
---|
50 |
|
---|
51 | class TestConsole : public ConsoleMouseInterface
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | VMMDevMouseInterface *getVMMDevMouseInterface() { return &mVMMDev; }
|
---|
55 | DisplayMouseInterface *getDisplayMouseInterface() { return &mDisplay; }
|
---|
56 | /** @todo why on earth is this not implemented? */
|
---|
57 | void onMouseCapabilityChange(BOOL supportsAbsolute, BOOL supportsRelative,
|
---|
58 | BOOL supportsMT, BOOL needsHostCursor) {}
|
---|
59 |
|
---|
60 | private:
|
---|
61 | TestVMMDev mVMMDev;
|
---|
62 | TestDisplay mDisplay;
|
---|
63 | };
|
---|
64 |
|
---|
65 | static int pdmdrvhlpAttach(PPDMDRVINS pDrvIns, uint32_t fFlags,
|
---|
66 | PPDMIBASE *ppBaseInterface)
|
---|
67 | {
|
---|
68 | return VERR_PDM_NO_ATTACHED_DRIVER;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static struct PDMDRVHLPR3 pdmHlpR3 =
|
---|
72 | {
|
---|
73 | PDM_DRVHLPR3_VERSION,
|
---|
74 | pdmdrvhlpAttach
|
---|
75 | };
|
---|
76 |
|
---|
77 | static struct
|
---|
78 | {
|
---|
79 | int32_t cx;
|
---|
80 | int32_t cy;
|
---|
81 | } mouseEvent;
|
---|
82 |
|
---|
83 | static int mousePutEvent(PPDMIMOUSEPORT pInterface, int32_t iDeltaX,
|
---|
84 | int32_t iDeltaY, int32_t iDeltaZ, int32_t iDeltaW,
|
---|
85 | uint32_t fButtonStates)
|
---|
86 | {
|
---|
87 | mouseEvent.cx = iDeltaX;
|
---|
88 | mouseEvent.cy = iDeltaY;
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static struct
|
---|
93 | {
|
---|
94 | int32_t x;
|
---|
95 | int32_t y;
|
---|
96 | } mouseEventAbs;
|
---|
97 |
|
---|
98 | static int mousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t uX,
|
---|
99 | uint32_t uY, int32_t iDeltaZ, int32_t iDeltaW,
|
---|
100 | uint32_t fButtonStates)
|
---|
101 | {
|
---|
102 | mouseEventAbs.x = uX;
|
---|
103 | mouseEventAbs.y = uY;
|
---|
104 | return VINF_SUCCESS;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static struct PDMIMOUSEPORT pdmiMousePort =
|
---|
108 | {
|
---|
109 | mousePutEvent,
|
---|
110 | mousePutEventAbs
|
---|
111 | };
|
---|
112 |
|
---|
113 | static void *pdmiBaseQuery(struct PDMIBASE *pInterface, const char *pszIID)
|
---|
114 | {
|
---|
115 | return &pdmiMousePort;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static struct PDMIBASE pdmiBase =
|
---|
119 | {
|
---|
120 | pdmiBaseQuery
|
---|
121 | };
|
---|
122 |
|
---|
123 | static struct PDMDRVINS pdmdrvInsCore =
|
---|
124 | {
|
---|
125 | PDM_DRVINS_VERSION,
|
---|
126 | 0,
|
---|
127 | NIL_RTRCPTR,
|
---|
128 | NIL_RTRCPTR,
|
---|
129 | NIL_RTR0PTR,
|
---|
130 | NIL_RTR0PTR,
|
---|
131 | &pdmHlpR3,
|
---|
132 | NULL,
|
---|
133 | NULL,
|
---|
134 | NULL,
|
---|
135 | &pdmiBase
|
---|
136 | };
|
---|
137 |
|
---|
138 | static struct PDMDRVINS *ppdmdrvIns = NULL;
|
---|
139 |
|
---|
140 | ComObjPtr<Mouse> pMouse;
|
---|
141 | ConsoleMouseInterface *pConsole = NULL;
|
---|
142 |
|
---|
143 | static struct
|
---|
144 | {
|
---|
145 | int32_t x;
|
---|
146 | int32_t y;
|
---|
147 | } absoluteMouse;
|
---|
148 |
|
---|
149 | static int setAbsoluteMouse(PPDMIVMMDEVPORT, int32_t x, int32_t y)
|
---|
150 | {
|
---|
151 | absoluteMouse.x = x;
|
---|
152 | absoluteMouse.y = y;
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static int updateMouseCapabilities(PPDMIVMMDEVPORT, uint32_t, uint32_t)
|
---|
157 | {
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void TestDisplay::getFramebufferDimensions(int32_t *px1, int32_t *py1,
|
---|
162 | int32_t *px2, int32_t *py2)
|
---|
163 | {
|
---|
164 | if (px1)
|
---|
165 | *px1 = -320;
|
---|
166 | if (py1)
|
---|
167 | *py1 = -240;
|
---|
168 | if (px2)
|
---|
169 | *px2 = 320;
|
---|
170 | if (py2)
|
---|
171 | *py2 = 240;
|
---|
172 | }
|
---|
173 |
|
---|
174 | int TestDisplay::getScreenResolution(uint32_t cScreen, ULONG *pcx,
|
---|
175 | ULONG *pcy, ULONG *pcBPP)
|
---|
176 | {
|
---|
177 | NOREF(cScreen);
|
---|
178 | if (pcx)
|
---|
179 | *pcx = 640;
|
---|
180 | if (pcy)
|
---|
181 | *pcy = 480;
|
---|
182 | if (pcBPP)
|
---|
183 | *pcBPP = 32;
|
---|
184 | return S_OK;
|
---|
185 | }
|
---|
186 |
|
---|
187 | DECLEXPORT(bool) CFGMR3AreValuesValid(PCFGMNODE, const char *)
|
---|
188 | {
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | DECLEXPORT(int) CFGMR3QueryPtr(PCFGMNODE, const char *, void **pv)
|
---|
193 | {
|
---|
194 | *pv = pMouse;
|
---|
195 | return VINF_SUCCESS;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /******************************************************************************
|
---|
199 | * Main test code *
|
---|
200 | ******************************************************************************/
|
---|
201 |
|
---|
202 | static int setup(void)
|
---|
203 | {
|
---|
204 | VMMDevPort.pfnSetAbsoluteMouse = setAbsoluteMouse;
|
---|
205 | VMMDevPort.pfnUpdateMouseCapabilities = updateMouseCapabilities;
|
---|
206 | HRESULT hrc = pMouse.createObject();
|
---|
207 | AssertComRC(hrc);
|
---|
208 | if (FAILED(hrc))
|
---|
209 | return VERR_GENERAL_FAILURE;
|
---|
210 | pConsole = new TestConsole;
|
---|
211 | pMouse->init(pConsole);
|
---|
212 | ppdmdrvIns = (struct PDMDRVINS *) RTMemAllocZ( sizeof(struct PDMDRVINS)
|
---|
213 | + Mouse::DrvReg.cbInstance);
|
---|
214 | *ppdmdrvIns = pdmdrvInsCore;
|
---|
215 | Mouse::DrvReg.pfnConstruct(ppdmdrvIns, NULL, 0);
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 | static void teardown(void)
|
---|
220 | {
|
---|
221 | pMouse.setNull();
|
---|
222 | if (pConsole)
|
---|
223 | delete pConsole;
|
---|
224 | if (ppdmdrvIns)
|
---|
225 | RTMemFree(ppdmdrvIns);
|
---|
226 | }
|
---|
227 |
|
---|
228 | static bool approxEq(int a, int b, int prec)
|
---|
229 | {
|
---|
230 | return a - b < prec && b - a < prec;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** @test testAbsToVMMDevNewProtocol */
|
---|
234 | static void testAbsToVMMDevNewProtocol(RTTEST hTest)
|
---|
235 | {
|
---|
236 | PPDMIBASE pBase;
|
---|
237 | PPDMIMOUSECONNECTOR pConnector;
|
---|
238 |
|
---|
239 | RTTestSub(hTest, "Absolute event to VMMDev, new protocol");
|
---|
240 | pBase = &ppdmdrvIns->IBase;
|
---|
241 | pConnector = (PPDMIMOUSECONNECTOR)pBase->pfnQueryInterface(pBase,
|
---|
242 | PDMIMOUSECONNECTOR_IID);
|
---|
243 | pConnector->pfnReportModes(pConnector, true, false, false);
|
---|
244 | pMouse->onVMMDevGuestCapsChange( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
|
---|
245 | | VMMDEV_MOUSE_NEW_PROTOCOL);
|
---|
246 | pMouse->PutMouseEventAbsolute(0, 0, 0, 0, 0);
|
---|
247 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, 0x8000, 200),
|
---|
248 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
249 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, 0x8000, 200),
|
---|
250 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
251 | pMouse->PutMouseEventAbsolute(-319, -239, 0, 0, 0);
|
---|
252 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, 0, 200),
|
---|
253 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
254 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, 0, 200),
|
---|
255 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
256 | pMouse->PutMouseEventAbsolute(320, 240, 0, 0, 0);
|
---|
257 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, 0xffff, 200),
|
---|
258 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
259 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, 0xffff, 200),
|
---|
260 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
261 | RTTestSubDone(hTest);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** @test testAbsToVMMDevOldProtocol */
|
---|
265 | static void testAbsToVMMDevOldProtocol(RTTEST hTest)
|
---|
266 | {
|
---|
267 | PPDMIBASE pBase;
|
---|
268 | PPDMIMOUSECONNECTOR pConnector;
|
---|
269 |
|
---|
270 | RTTestSub(hTest, "Absolute event to VMMDev, old protocol");
|
---|
271 | pBase = &ppdmdrvIns->IBase;
|
---|
272 | pConnector = (PPDMIMOUSECONNECTOR)pBase->pfnQueryInterface(pBase,
|
---|
273 | PDMIMOUSECONNECTOR_IID);
|
---|
274 | pConnector->pfnReportModes(pConnector, true, false, false);
|
---|
275 | pMouse->onVMMDevGuestCapsChange(VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE);
|
---|
276 | pMouse->PutMouseEventAbsolute(320, 240, 0, 0, 0);
|
---|
277 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, 0x8000, 200),
|
---|
278 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
279 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, 0x8000, 200),
|
---|
280 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
281 | pMouse->PutMouseEventAbsolute(0, 0, 0, 0, 0);
|
---|
282 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, 0, 200),
|
---|
283 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
284 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, 0, 200),
|
---|
285 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
286 | pMouse->PutMouseEventAbsolute(-319, -239, 0, 0, 0);
|
---|
287 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.x, -0x8000, 200),
|
---|
288 | ("absoluteMouse.x=%d\n", absoluteMouse.x));
|
---|
289 | RTTESTI_CHECK_MSG(approxEq(absoluteMouse.y, -0x8000, 200),
|
---|
290 | ("absoluteMouse.y=%d\n", absoluteMouse.y));
|
---|
291 | RTTestSubDone(hTest);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** @test testAbsToAbsDev */
|
---|
295 | static void testAbsToAbsDev(RTTEST hTest)
|
---|
296 | {
|
---|
297 | PPDMIBASE pBase;
|
---|
298 | PPDMIMOUSECONNECTOR pConnector;
|
---|
299 |
|
---|
300 | RTTestSub(hTest, "Absolute event to absolute device");
|
---|
301 | pBase = &ppdmdrvIns->IBase;
|
---|
302 | pConnector = (PPDMIMOUSECONNECTOR)pBase->pfnQueryInterface(pBase,
|
---|
303 | PDMIMOUSECONNECTOR_IID);
|
---|
304 | pConnector->pfnReportModes(pConnector, false, true, false);
|
---|
305 | pMouse->onVMMDevGuestCapsChange( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
|
---|
306 | | VMMDEV_MOUSE_NEW_PROTOCOL);
|
---|
307 | pMouse->PutMouseEventAbsolute(0, 0, 0, 0, 0);
|
---|
308 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.x, 0x8000, 200),
|
---|
309 | ("mouseEventAbs.x=%d\n", mouseEventAbs.x));
|
---|
310 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.y, 0x8000, 200),
|
---|
311 | ("mouseEventAbs.y=%d\n", mouseEventAbs.y));
|
---|
312 | pMouse->PutMouseEventAbsolute(-319, -239, 0, 0, 0);
|
---|
313 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.x, 0, 200),
|
---|
314 | ("mouseEventAbs.x=%d\n", mouseEventAbs.x));
|
---|
315 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.y, 0, 200),
|
---|
316 | ("mouseEventAbs.y=%d\n", mouseEventAbs.y));
|
---|
317 | pMouse->PutMouseEventAbsolute(320, 240, 0, 0, 0);
|
---|
318 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.x, 0xffff, 200),
|
---|
319 | ("mouseEventAbs.x=%d\n", mouseEventAbs.x));
|
---|
320 | RTTESTI_CHECK_MSG(approxEq(mouseEventAbs.y, 0xffff, 200),
|
---|
321 | ("mouseEventAbs.y=%d\n", mouseEventAbs.y));
|
---|
322 | mouseEventAbs.x = mouseEventAbs.y = 0xffff;
|
---|
323 | pMouse->PutMouseEventAbsolute(-640, -480, 0, 0, 0);
|
---|
324 | RTTESTI_CHECK_MSG(mouseEventAbs.x = 0xffff,
|
---|
325 | ("mouseEventAbs.x=%d\n", mouseEventAbs.x));
|
---|
326 | RTTESTI_CHECK_MSG(mouseEventAbs.y == 0xffff,
|
---|
327 | ("mouseEventAbs.y=%d\n", mouseEventAbs.y));
|
---|
328 | RTTestSubDone(hTest);
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** @todo generate this using the @test blocks above */
|
---|
332 | typedef void (*PFNTEST)(RTTEST);
|
---|
333 | static PFNTEST g_tests[] =
|
---|
334 | {
|
---|
335 | testAbsToVMMDevNewProtocol,
|
---|
336 | testAbsToVMMDevOldProtocol,
|
---|
337 | testAbsToAbsDev,
|
---|
338 | NULL
|
---|
339 | };
|
---|
340 |
|
---|
341 | int main(void)
|
---|
342 | {
|
---|
343 | /*
|
---|
344 | * Init the runtime, test and say hello.
|
---|
345 | */
|
---|
346 | RTTEST hTest;
|
---|
347 | RTEXITCODE rcExit = RTTestInitAndCreate("tstMouseImpl", &hTest);
|
---|
348 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
349 | return rcExit;
|
---|
350 | RTTestBanner(hTest);
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Run the tests.
|
---|
354 | */
|
---|
355 | for (unsigned i = 0; g_tests[i]; ++i)
|
---|
356 | {
|
---|
357 | int rc = setup();
|
---|
358 | AssertRC(rc);
|
---|
359 | if (RT_SUCCESS(rc))
|
---|
360 | g_tests[i](hTest);
|
---|
361 | teardown();
|
---|
362 | }
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Summary
|
---|
366 | */
|
---|
367 | return RTTestSummaryAndDestroy(hTest);
|
---|
368 | }
|
---|
369 |
|
---|