2 * Unit test of the Program Manager DDE Interfaces
4 * Copyright 2009 Mikey Alexander
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* DDE Program Manager Tests
22 * - Covers basic CreateGroup, ShowGroup, DeleteGroup, AddItem, and DeleteItem
24 * - Todo: Handle CommonGroupFlag
25 * Handle Difference between administrator and non-administrator calls
27 * Better AddItem Tests (Lots of parameters to test)
28 * Tests for Invalid Characters in Names / Invalid Parameters
32 #include <wine/test.h>
39 /* Timeout on DdeClientTransaction Call */
40 #define MS_TIMEOUT_VAL 1000
41 /* # of times to poll for window creation */
42 #define PDDE_POLL_NUM 50
43 /* time to sleep between polls */
44 #define PDDE_POLL_TIME 200
47 #define DDE_TEST_MISC 0x00010000
48 #define DDE_TEST_CREATEGROUP 0x00020000
49 #define DDE_TEST_DELETEGROUP 0x00030000
50 #define DDE_TEST_SHOWGROUP 0x00040000
51 #define DDE_TEST_ADDITEM 0x00050000
52 #define DDE_TEST_DELETEITEM 0x00060000
53 #define DDE_TEST_COMPOUND 0x00070000
54 #define DDE_TEST_CALLMASK 0x00ff0000
56 /* Type of Test (Common, Individual) */
57 #define DDE_TEST_COMMON 0x01000000
58 #define DDE_TEST_INDIVIDUAL 0x02000000
60 #define DDE_TEST_NUMMASK 0x0000ffff
62 static HDDEDATA CALLBACK DdeCallback(UINT type, UINT format, HCONV hConv, HSZ hsz1, HSZ hsz2,
63 HDDEDATA hDDEData, ULONG_PTR data1, ULONG_PTR data2)
65 trace("Callback: type=%i, format=%i\n", type, format);
70 * Encoded String for Error Messages so that inner failures can determine
71 * what test is failing. Format is: [Code:TestNum]
73 static const char * GetStringFromTestParams(int testParams)
76 static char testParamString[64];
79 testNum = testParams & DDE_TEST_NUMMASK;
80 switch (testParams & DDE_TEST_CALLMASK)
86 case DDE_TEST_CREATEGROUP:
89 case DDE_TEST_DELETEGROUP:
92 case DDE_TEST_SHOWGROUP:
95 case DDE_TEST_ADDITEM:
98 case DDE_TEST_DELETEITEM:
101 case DDE_TEST_COMPOUND:
106 sprintf(testParamString, " [%s:%i]", callId, testNum);
107 return testParamString;
110 /* Transfer DMLERR's into text readable strings for Error Messages */
111 #define DMLERR_TO_STR(x) case x: return#x;
112 static const char * GetStringFromError(UINT err)
117 DMLERR_TO_STR(DMLERR_NO_ERROR);
118 DMLERR_TO_STR(DMLERR_ADVACKTIMEOUT);
119 DMLERR_TO_STR(DMLERR_BUSY);
120 DMLERR_TO_STR(DMLERR_DATAACKTIMEOUT);
121 DMLERR_TO_STR(DMLERR_DLL_NOT_INITIALIZED);
122 DMLERR_TO_STR(DMLERR_DLL_USAGE);
123 DMLERR_TO_STR(DMLERR_EXECACKTIMEOUT);
124 DMLERR_TO_STR(DMLERR_INVALIDPARAMETER);
125 DMLERR_TO_STR(DMLERR_LOW_MEMORY);
126 DMLERR_TO_STR(DMLERR_MEMORY_ERROR);
127 DMLERR_TO_STR(DMLERR_NOTPROCESSED);
128 DMLERR_TO_STR(DMLERR_NO_CONV_ESTABLISHED);
129 DMLERR_TO_STR(DMLERR_POKEACKTIMEOUT);
130 DMLERR_TO_STR(DMLERR_POSTMSG_FAILED);
131 DMLERR_TO_STR(DMLERR_REENTRANCY);
132 DMLERR_TO_STR(DMLERR_SERVER_DIED);
133 DMLERR_TO_STR(DMLERR_SYS_ERROR);
134 DMLERR_TO_STR(DMLERR_UNADVACKTIMEOUT);
135 DMLERR_TO_STR(DMLERR_UNFOUND_QUEUE_ID);
137 retstr = "Unknown DML Error";
144 /* Helper Function to Transfer DdeGetLastError into a String */
145 static const char * GetDdeLastErrorStr(DWORD instance)
147 UINT err = DdeGetLastError(instance);
149 return GetStringFromError(err);
152 /* Execute a Dde Command and return the error & result */
153 /* Note: Progman DDE always returns a pointer to 0x00000001 on a successful result */
154 static void DdeExecuteCommand(DWORD instance, HCONV hConv, const char *strCmd, HDDEDATA *hData, UINT *err, int testParams)
158 command = DdeCreateDataHandle(instance, (LPBYTE) strCmd, strlen(strCmd)+1, 0, 0L, 0, 0);
159 ok (command != NULL, "DdeCreateDataHandle Error %s.%s\n",
160 GetDdeLastErrorStr(instance), GetStringFromTestParams(testParams));
161 *hData = DdeClientTransaction((void *) command,
170 /* hData is technically a pointer, but for Program Manager,
171 * it is NULL (error) or 1 (success)
172 * TODO: Check other versions of Windows to verify 1 is returned.
173 * While it is unlikely that anyone is actually testing that the result is 1
174 * if all versions of windows return 1, Wine should also.
178 *err = DdeGetLastError(instance);
182 *err = DMLERR_NO_ERROR;
185 ok(*hData == (HDDEDATA) 1, "Expected HDDEDATA Handle == 1, actually %p.%s\n",
186 *hData, GetStringFromTestParams(testParams));
192 * Check if Window is onscreen with the appropriate name.
194 * Windows are not created synchronously. So we do not know
195 * when and if the window will be created/shown on screen.
196 * This function implements a polling mechanism to determine
198 * A more complicated method would be to use SetWindowsHookEx.
199 * Since polling worked fine in my testing, no reason to implement
200 * the other. Comments about other methods of determining when
201 * window creation happened were not encouraging (not including
204 static void CheckWindowCreated(const char *winName, int closeWindow, int testParams)
209 /* Poll for Window Creation */
210 for (i = 0; window == NULL && i < PDDE_POLL_NUM; i++)
212 Sleep(PDDE_POLL_TIME);
213 window = FindWindowA(NULL, winName);
215 ok (window != NULL, "Window \"%s\" was not created in %i seconds - assumed failure.%s\n",
216 winName, PDDE_POLL_NUM*PDDE_POLL_TIME/1000, GetStringFromTestParams(testParams));
218 /* Close Window as desired. */
219 if (window != NULL && closeWindow)
221 SendMessageA(window, WM_SYSCOMMAND, SC_CLOSE, 0);
225 /* Check for Existence (or non-existence) of a file or group
226 * When testing for existence of a group, groupName is not needed
228 static void CheckFileExistsInProgramGroups(const char *nameToCheck, int shouldExist, int isGroup,
229 const char *groupName, int testParams)
236 path = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
242 /* Win 9x doesn't support common */
243 if (testParams & DDE_TEST_COMMON)
245 specialFolder = CSIDL_COMMON_PROGRAMS;
246 err = SHGetSpecialFolderPath(NULL, path, specialFolder, FALSE);
247 /* Win 9x fails, use CSIDL_PROGRAMS (err == FALSE) */
251 specialFolder = CSIDL_PROGRAMS;
252 err = SHGetSpecialFolderPath(NULL, path, specialFolder, FALSE);
254 len = strlen(path) + strlen(nameToCheck)+1;
255 if (groupName != NULL)
257 len += strlen(groupName)+1;
259 ok (len <= MAX_PATH, "Path Too Long.%s\n", GetStringFromTestParams(testParams));
262 if (groupName != NULL)
265 strcat(path, groupName);
268 strcat(path, nameToCheck);
269 attributes = GetFileAttributes(path);
272 ok (attributes == INVALID_FILE_ATTRIBUTES , "File exists and shouldn't %s.%s\n",
273 path, GetStringFromTestParams(testParams));
275 if (attributes == INVALID_FILE_ATTRIBUTES)
277 ok (FALSE, "Created File %s doesn't exist.%s\n", path, GetStringFromTestParams(testParams));
278 } else if (isGroup) {
279 ok (attributes & FILE_ATTRIBUTE_DIRECTORY, "%s is not a folder (attr=%x).%s\n",
280 path, attributes, GetStringFromTestParams(testParams));
282 ok (attributes & FILE_ATTRIBUTE_ARCHIVE, "Created File %s has wrong attributes (%x).%s\n",
283 path, attributes, GetStringFromTestParams(testParams));
287 HeapFree(GetProcessHeap(), 0, path);
291 ok (FALSE, "Could not Allocate Path Buffer\n");
295 /* Create Group Test.
296 * command and expected_result.
297 * if expected_result is DMLERR_NO_ERROR, test
298 * 1. group was created
301 static void CreateGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
302 const char *groupName, int testParams)
307 /* Execute Command & Check Result */
308 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
311 ok (expected_result == error, "CreateGroup %s: Expected Error %s, received %s.%s\n",
312 groupName, GetStringFromError(expected_result), GetStringFromError(error),
313 GetStringFromTestParams(testParams));
317 if (error == DMLERR_NO_ERROR)
320 /* Check if Group Now Exists */
321 CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
322 /* Check if Window is Open (polling) */
323 CheckWindowCreated(groupName, TRUE, testParams);
328 * DDE command, expected_result, and the group name to check for existence
329 * if expected_result is DMLERR_NO_ERROR, test
332 static void ShowGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
333 const char *groupName, int closeAfterShowing, int testParams)
338 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
339 /* todo_wine... Is expected to fail, wine stubbed functions DO fail */
340 /* TODO REMOVE THIS CODE!!! */
341 if (expected_result == DMLERR_NOTPROCESSED)
343 ok (expected_result == error, "ShowGroup %s: Expected Error %s, received %s.%s\n",
344 groupName, GetStringFromError(expected_result), GetStringFromError(error),
345 GetStringFromTestParams(testParams));
349 ok (expected_result == error, "ShowGroup %s: Expected Error %s, received %s.%s\n",
350 groupName, GetStringFromError(expected_result), GetStringFromError(error),
351 GetStringFromTestParams(testParams));
355 if (error == DMLERR_NO_ERROR)
357 /* Check if Window is Open (polling) */
358 CheckWindowCreated(groupName, closeAfterShowing, testParams);
362 /* Delete Group Test.
363 * DDE command, expected_result, and the group name to check for existence
364 * if expected_result is DMLERR_NO_ERROR, test
365 * 1. group does not exist
367 static void DeleteGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
368 const char *groupName, int testParams)
373 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
376 ok (expected_result == error, "DeleteGroup %s: Expected Error %s, received %s.%s\n",
377 groupName, GetStringFromError(expected_result), GetStringFromError(error),
378 GetStringFromTestParams(testParams));
381 if (error == DMLERR_NO_ERROR)
383 /* Check that Group does not exist */
384 CheckFileExistsInProgramGroups(groupName, FALSE, TRUE, NULL, testParams);
389 * DDE command, expected result, and group and file name where it should exist.
390 * checks to make sure error code matches expected error code
391 * checks to make sure item exists if successful
393 static void AddItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
394 const char *fileName, const char *groupName, int testParams)
399 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
402 ok (expected_result == error, "AddItem %s: Expected Error %s, received %s.%s\n",
403 fileName, GetStringFromError(expected_result), GetStringFromError(error),
404 GetStringFromTestParams(testParams));
407 if (error == DMLERR_NO_ERROR)
409 /* Check that File exists */
410 CheckFileExistsInProgramGroups(fileName, TRUE, FALSE, groupName, testParams);
415 * DDE command, expected result, and group and file name where it should exist.
416 * checks to make sure error code matches expected error code
417 * checks to make sure item does not exist if successful
419 static void DeleteItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
420 const char *fileName, const char *groupName, int testParams)
425 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
428 ok (expected_result == error, "DeleteItem %s: Expected Error %s, received %s.%s\n",
429 fileName, GetStringFromError(expected_result), GetStringFromError(error),
430 GetStringFromTestParams(testParams));
433 if (error == DMLERR_NO_ERROR)
435 /* Check that File does not exist */
436 CheckFileExistsInProgramGroups(fileName, FALSE, FALSE, groupName, testParams);
440 /* Compound Command Test.
441 * not really generic, assumes command of the form:
442 * [CreateGroup ...][AddItem ...][AddItem ...]
443 * All samples I've seen using Compound were of this form (CreateGroup,
444 * AddItems) so this covers minimum expected functionality.
446 static void CompoundCommandTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
447 const char *groupName, const char *fileName1,
448 const char *fileName2, int testParams)
453 DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
456 ok (expected_result == error, "Compound String %s: Expected Error %s, received %s.%s\n",
457 command, GetStringFromError(expected_result), GetStringFromError(error),
458 GetStringFromTestParams(testParams));
461 if (error == DMLERR_NO_ERROR)
463 /* Check that File exists */
464 CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
465 CheckWindowCreated(groupName, FALSE, testParams);
466 CheckFileExistsInProgramGroups(fileName1, TRUE, FALSE, groupName, testParams);
467 CheckFileExistsInProgramGroups(fileName2, TRUE, FALSE, groupName, testParams);
471 /* 1st set of tests */
472 static int DdeTestProgman(DWORD instance, HCONV hConv)
479 /* Invalid Command */
480 DdeExecuteCommand(instance, hConv, "[InvalidCommand()]", &hData, &error, DDE_TEST_MISC|testnum++);
481 ok (error == DMLERR_NOTPROCESSED, "InvalidCommand(), expected error %s, received %s.\n",
482 GetStringFromError(DMLERR_NOTPROCESSED), GetStringFromError(error));
484 /* CreateGroup Tests (including AddItem, DeleteItem) */
485 CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", DDE_TEST_COMMON|DDE_TEST_CREATEGROUP|testnum++);
486 AddItemTest(instance, hConv, "[AddItem(c:\\f1g1,f1g1Name)]", DMLERR_NO_ERROR, "f1g1Name.lnk", "Group1", DDE_TEST_COMMON|DDE_TEST_ADDITEM|testnum++);
487 AddItemTest(instance, hConv, "[AddItem(c:\\f2g1,f2g1Name)]", DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_COMMON|DDE_TEST_ADDITEM|testnum++);
488 DeleteItemTest(instance, hConv, "[DeleteItem(f2g1Name)]", DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_COMMON|DDE_TEST_DELETEITEM|testnum++);
489 AddItemTest(instance, hConv, "[AddItem(c:\\f3g1,f3g1Name)]", DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_COMMON|DDE_TEST_ADDITEM|testnum++);
490 CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", DDE_TEST_COMMON|DDE_TEST_CREATEGROUP|testnum++);
491 /* Create Group that already exists - same instance */
492 CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", DDE_TEST_COMMON|DDE_TEST_CREATEGROUP|testnum++);
494 /* ShowGroup Tests */
495 ShowGroupTest(instance, hConv, "[ShowGroup(Group1)]", DMLERR_NOTPROCESSED, "Startup", TRUE, DDE_TEST_SHOWGROUP|testnum++);
496 DeleteItemTest(instance, hConv, "[DeleteItem(f3g1Name)]", DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_COMMON|DDE_TEST_DELETEITEM|testnum++);
497 ShowGroupTest(instance, hConv, "[ShowGroup(Startup,0)]", DMLERR_NO_ERROR, "Startup", TRUE, DDE_TEST_SHOWGROUP|testnum++);
498 ShowGroupTest(instance, hConv, "[ShowGroup(Group1,0)]", DMLERR_NO_ERROR, "Group1", FALSE, DDE_TEST_SHOWGROUP|testnum++);
500 /* DeleteGroup Test - Note that Window is Open for this test */
501 DeleteGroupTest(instance, hConv, "[DeleteGroup(Group1)]", DMLERR_NO_ERROR, "Group1", DDE_TEST_DELETEGROUP|testnum++);
503 /* Compound Execute String Command */
504 CompoundCommandTest(instance, hConv, "[CreateGroup(Group3)][AddItem(c:\\f1g3,f1g3Name)][AddItem(c:\\f2g3,f2g3Name)]", DMLERR_NO_ERROR, "Group3", "f1g3Name.lnk", "f2g3Name.lnk", DDE_TEST_COMMON|DDE_TEST_COMPOUND|testnum++);
505 DeleteGroupTest(instance, hConv, "[DeleteGroup(Group3)]", DMLERR_NO_ERROR, "Group3", DDE_TEST_DELETEGROUP|testnum++);
507 /* Full Parameters of Add Item */
508 /* AddItem(CmdLine[,Name[,IconPath[,IconIndex[,xPos,yPos[,DefDir[,HotKey[,fMinimize[fSeparateSpace]]]]]]]) */
513 /* 2nd set of tests - 2nd connection */
514 static void DdeTestProgman2(DWORD instance, HCONV hConv, int testnum)
516 /* Create Group that already exists on a separate connection */
517 CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", DDE_TEST_COMMON|DDE_TEST_CREATEGROUP|testnum++);
518 DeleteGroupTest(instance, hConv, "[DeleteGroup(Group2)]", DMLERR_NO_ERROR, "Group2", DDE_TEST_COMMON|DDE_TEST_DELETEGROUP|testnum++);
521 START_TEST(progman_dde)
529 /* Initialize DDE Instance */
530 err = DdeInitialize(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
531 ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));
533 /* Create Connection */
534 hszProgman = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
535 ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
536 hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
537 ok (hConv != NULL, "DdeConnect Error %s\n", GetDdeLastErrorStr(instance));
538 ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");
541 testnum = DdeTestProgman(instance, hConv);
544 ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
545 ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
547 /* 2nd Instance (Followup Tests) */
548 /* Initialize DDE Instance */
550 err = DdeInitialize(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
551 ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));
553 /* Create Connection */
554 hszProgman = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
555 ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
556 hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
557 ok (hConv != NULL, "DdeConnect Error %s\n", GetDdeLastErrorStr(instance));
558 ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");
561 DdeTestProgman2(instance, hConv, testnum);
564 ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
565 ok (DdeUninitialize(instance), "DdeUninitialize failed\n");