shell32/autocomplete: Remove redundant memory initialization.
[wine] / dlls / shell32 / tests / progman_dde.c
1 /*
2  * Unit test of the Program Manager DDE Interfaces
3  *
4  * Copyright 2009 Mikey Alexander
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /* DDE Program Manager Tests
22  * - Covers basic CreateGroup, ShowGroup, DeleteGroup, AddItem, and DeleteItem
23  *   functionality
24  * - Todo: Handle CommonGroupFlag
25  *         Better AddItem Tests (Lots of parameters to test)
26  *         Tests for Invalid Characters in Names / Invalid Parameters
27  */
28
29 #include <stdio.h>
30 #include <wine/test.h>
31 #include <winbase.h>
32 #include "dde.h"
33 #include "ddeml.h"
34 #include "winuser.h"
35 #include "shlobj.h"
36
37 /* Timeout on DdeClientTransaction Call */
38 #define MS_TIMEOUT_VAL 1000
39 /* # of times to poll for window creation */
40 #define PDDE_POLL_NUM 150
41 /* time to sleep between polls */
42 #define PDDE_POLL_TIME 300
43
44 /* Call Info */
45 #define DDE_TEST_MISC            0x00010000
46 #define DDE_TEST_CREATEGROUP     0x00020000
47 #define DDE_TEST_DELETEGROUP     0x00030000
48 #define DDE_TEST_SHOWGROUP       0x00040000
49 #define DDE_TEST_ADDITEM         0x00050000
50 #define DDE_TEST_DELETEITEM      0x00060000
51 #define DDE_TEST_COMPOUND        0x00070000
52 #define DDE_TEST_CALLMASK        0x00ff0000
53
54 #define DDE_TEST_NUMMASK           0x0000ffff
55
56 static HRESULT (WINAPI *pSHGetLocalizedName)(LPCWSTR, LPWSTR, UINT, int *);
57 static BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
58 static BOOL (WINAPI *pReadCabinetState)(CABINETSTATE *, int);
59
60 static void init_function_pointers(void)
61 {
62     HMODULE hmod;
63
64     hmod = GetModuleHandleA("shell32.dll");
65     pSHGetLocalizedName = (void*)GetProcAddress(hmod, "SHGetLocalizedName");
66     pSHGetSpecialFolderPathA = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathA");
67     pReadCabinetState = (void*)GetProcAddress(hmod, "ReadCabinetState");
68     if (!pReadCabinetState)
69         pReadCabinetState = (void*)GetProcAddress(hmod, (LPSTR)651);
70 }
71
72 static BOOL use_common(void)
73 {
74     HMODULE hmod;
75     static BOOL (WINAPI *pIsNTAdmin)(DWORD, LPDWORD);
76
77     /* IsNTAdmin() is available on all platforms. */
78     hmod = LoadLibraryA("advpack.dll");
79     pIsNTAdmin = (void*)GetProcAddress(hmod, "IsNTAdmin");
80
81     if (!pIsNTAdmin(0, NULL))
82     {
83         /* We are definitely not an administrator */
84         FreeLibrary(hmod);
85         return FALSE;
86     }
87     FreeLibrary(hmod);
88
89     /* If we end up here we are on NT4+ as Win9x and WinMe don't have the
90      * notion of administrators (as we need it).
91      */
92
93     /* As of Vista  we should always use the users directory. Tests with the
94      * real Administrator account on Windows 7 proved this.
95      *
96      * FIXME: We need a better way of identifying Vista+ as currently this check
97      * also covers Wine and we don't know yet which behavior we want to follow.
98      */
99     if (pSHGetLocalizedName)
100         return FALSE;
101
102     return TRUE;
103 }
104
105 static BOOL full_title(void)
106 {
107     CABINETSTATE cs;
108
109     memset(&cs, 0, sizeof(cs));
110     if (pReadCabinetState)
111     {
112         pReadCabinetState(&cs, sizeof(cs));
113     }
114     else
115     {
116         HKEY key;
117         DWORD size;
118
119         win_skip("ReadCabinetState is not available, reading registry directly\n");
120         RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CabinetState", &key);
121         size = sizeof(cs);
122         RegQueryValueExA(key, "Settings", NULL, NULL, (LPBYTE)&cs, &size);
123         RegCloseKey(key);
124     }
125
126     return (cs.fFullPathTitle == -1);
127 }
128
129 static char ProgramsDir[MAX_PATH];
130
131 static char Group1Title[MAX_PATH]  = "Group1";
132 static char Group2Title[MAX_PATH]  = "Group2";
133 static char Group3Title[MAX_PATH]  = "Group3";
134 static char StartupTitle[MAX_PATH] = "Startup";
135
136 static void init_strings(void)
137 {
138     char startup[MAX_PATH];
139     char commonprograms[MAX_PATH];
140     char programs[MAX_PATH];
141
142     if (pSHGetSpecialFolderPathA)
143     {
144         pSHGetSpecialFolderPathA(NULL, programs, CSIDL_PROGRAMS, FALSE);
145         pSHGetSpecialFolderPathA(NULL, commonprograms, CSIDL_COMMON_PROGRAMS, FALSE);
146         pSHGetSpecialFolderPathA(NULL, startup, CSIDL_STARTUP, FALSE);
147     }
148     else
149     {
150         HKEY key;
151         DWORD size;
152
153         /* Older Win9x and NT4 */
154
155         RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &key);
156         size = sizeof(programs);
157         RegQueryValueExA(key, "Programs", NULL, NULL, (LPBYTE)&programs, &size);
158         size = sizeof(startup);
159         RegQueryValueExA(key, "Startup", NULL, NULL, (LPBYTE)&startup, &size);
160         RegCloseKey(key);
161
162         RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &key);
163         size = sizeof(commonprograms);
164         RegQueryValueExA(key, "Common Programs", NULL, NULL, (LPBYTE)&commonprograms, &size);
165         RegCloseKey(key);
166     }
167
168     /* ProgramsDir on Vista+ is always the users one (CSIDL_PROGRAMS). Before Vista
169      * it depends on whether the user is an administrator (CSIDL_COMMON_PROGRAMS) or
170      * not (CSIDL_PROGRAMS).
171      */
172     if (use_common())
173         lstrcpyA(ProgramsDir, commonprograms);
174     else
175         lstrcpyA(ProgramsDir, programs);
176
177     if (full_title())
178     {
179         lstrcpyA(Group1Title, ProgramsDir);
180         lstrcatA(Group1Title, "\\Group1");
181         lstrcpyA(Group2Title, ProgramsDir);
182         lstrcatA(Group2Title, "\\Group2");
183         lstrcpyA(Group3Title, ProgramsDir);
184         lstrcatA(Group3Title, "\\Group3");
185
186         lstrcpyA(StartupTitle, startup);
187     }
188     else
189     {
190         /* Vista has the nice habit of displaying the full path in English
191          * and the short one localized. CSIDL_STARTUP on Vista gives us the
192          * English version so we have to 'translate' this one.
193          *
194          * MSDN claims it should be used for files not folders but this one
195          * suits our purposes just fine.
196          */
197         if (pSHGetLocalizedName)
198         {
199             WCHAR startupW[MAX_PATH];
200             WCHAR module[MAX_PATH];
201             WCHAR module_expanded[MAX_PATH];
202             WCHAR localized[MAX_PATH];
203             HRESULT hr;
204             int id;
205
206             MultiByteToWideChar(CP_ACP, 0, startup, -1, startupW, sizeof(startupW)/sizeof(WCHAR));
207             hr = pSHGetLocalizedName(startupW, module, MAX_PATH, &id);
208             todo_wine ok(hr == S_OK, "got 0x%08x\n", hr);
209             /* check to be removed when SHGetLocalizedName is implemented */
210             if (hr == S_OK)
211             {
212                 ExpandEnvironmentStringsW(module, module_expanded, MAX_PATH);
213                 LoadStringW(GetModuleHandleW(module_expanded), id, localized, MAX_PATH);
214
215                 WideCharToMultiByte(CP_ACP, 0, localized, -1, StartupTitle, sizeof(StartupTitle), NULL, NULL);
216             }
217             else
218                 lstrcpyA(StartupTitle, (strrchr(startup, '\\') + 1));
219         }
220         else
221         {
222             lstrcpyA(StartupTitle, (strrchr(startup, '\\') + 1));
223         }
224     }
225 }
226
227 static HDDEDATA CALLBACK DdeCallback(UINT type, UINT format, HCONV hConv, HSZ hsz1, HSZ hsz2,
228                                      HDDEDATA hDDEData, ULONG_PTR data1, ULONG_PTR data2)
229 {
230     trace("Callback: type=%i, format=%i\n", type, format);
231     return NULL;
232 }
233
234 /*
235  * Encoded String for Error Messages so that inner failures can determine
236  * what test is failing.  Format is: [Code:TestNum]
237  */
238 static const char * GetStringFromTestParams(int testParams)
239 {
240     int testNum;
241     static char testParamString[64];
242     const char *callId;
243
244     testNum = testParams & DDE_TEST_NUMMASK;
245     switch (testParams & DDE_TEST_CALLMASK)
246     {
247     default:
248     case DDE_TEST_MISC:
249         callId = "MISC";
250         break;
251     case DDE_TEST_CREATEGROUP:
252         callId = "C_G";
253         break;
254     case DDE_TEST_DELETEGROUP:
255         callId = "D_G";
256         break;
257     case DDE_TEST_SHOWGROUP:
258         callId = "S_G";
259         break;
260     case DDE_TEST_ADDITEM:
261         callId = "A_I";
262         break;
263     case DDE_TEST_DELETEITEM:
264         callId = "D_I";
265         break;
266     case DDE_TEST_COMPOUND:
267         callId = "CPD";
268         break;
269     }
270
271     sprintf(testParamString, "  [%s:%i]", callId, testNum);
272     return testParamString;
273 }
274
275 /* Transfer DMLERR's into text readable strings for Error Messages */
276 #define DMLERR_TO_STR(x) case x: return#x;
277 static const char * GetStringFromError(UINT err)
278 {
279     switch (err)
280     {
281     DMLERR_TO_STR(DMLERR_NO_ERROR);
282     DMLERR_TO_STR(DMLERR_ADVACKTIMEOUT);
283     DMLERR_TO_STR(DMLERR_BUSY);
284     DMLERR_TO_STR(DMLERR_DATAACKTIMEOUT);
285     DMLERR_TO_STR(DMLERR_DLL_NOT_INITIALIZED);
286     DMLERR_TO_STR(DMLERR_DLL_USAGE);
287     DMLERR_TO_STR(DMLERR_EXECACKTIMEOUT);
288     DMLERR_TO_STR(DMLERR_INVALIDPARAMETER);
289     DMLERR_TO_STR(DMLERR_LOW_MEMORY);
290     DMLERR_TO_STR(DMLERR_MEMORY_ERROR);
291     DMLERR_TO_STR(DMLERR_NOTPROCESSED);
292     DMLERR_TO_STR(DMLERR_NO_CONV_ESTABLISHED);
293     DMLERR_TO_STR(DMLERR_POKEACKTIMEOUT);
294     DMLERR_TO_STR(DMLERR_POSTMSG_FAILED);
295     DMLERR_TO_STR(DMLERR_REENTRANCY);
296     DMLERR_TO_STR(DMLERR_SERVER_DIED);
297     DMLERR_TO_STR(DMLERR_SYS_ERROR);
298     DMLERR_TO_STR(DMLERR_UNADVACKTIMEOUT);
299     DMLERR_TO_STR(DMLERR_UNFOUND_QUEUE_ID);
300     default:
301         return "Unknown DML Error";
302     }
303 }
304
305 /* Helper Function to Transfer DdeGetLastError into a String */
306 static const char * GetDdeLastErrorStr(DWORD instance)
307 {
308     UINT err = DdeGetLastError(instance);
309
310     return GetStringFromError(err);
311 }
312
313 /* Execute a Dde Command and return the error & result */
314 /* Note: Progman DDE always returns a pointer to 0x00000001 on a successful result */
315 static void DdeExecuteCommand(DWORD instance, HCONV hConv, const char *strCmd, HDDEDATA *hData, UINT *err, int testParams)
316 {
317     HDDEDATA command;
318
319     command = DdeCreateDataHandle(instance, (LPBYTE) strCmd, strlen(strCmd)+1, 0, 0L, 0, 0);
320     ok (command != NULL, "DdeCreateDataHandle Error %s.%s\n",
321         GetDdeLastErrorStr(instance), GetStringFromTestParams(testParams));
322     *hData = DdeClientTransaction((void *) command,
323                                   -1,
324                                   hConv,
325                                   0,
326                                   0,
327                                   XTYP_EXECUTE,
328                                   MS_TIMEOUT_VAL,
329                                   NULL);
330
331     /* hData is technically a pointer, but for Program Manager,
332      * it is NULL (error) or 1 (success)
333      * TODO: Check other versions of Windows to verify 1 is returned.
334      * While it is unlikely that anyone is actually testing that the result is 1
335      * if all versions of windows return 1, Wine should also.
336      */
337     if (*hData == NULL)
338     {
339         *err = DdeGetLastError(instance);
340     }
341     else
342     {
343         *err = DMLERR_NO_ERROR;
344         todo_wine
345         {
346             ok(*hData == (HDDEDATA) 1, "Expected HDDEDATA Handle == 1, actually %p.%s\n",
347                *hData, GetStringFromTestParams(testParams));
348         }
349     }
350     DdeFreeDataHandle(command);
351 }
352
353 /*
354  * Check if Window is onscreen with the appropriate name.
355  *
356  * Windows are not created synchronously.  So we do not know
357  * when and if the window will be created/shown on screen.
358  * This function implements a polling mechanism to determine
359  * creation.
360  * A more complicated method would be to use SetWindowsHookEx.
361  * Since polling worked fine in my testing, no reason to implement
362  * the other.  Comments about other methods of determining when
363  * window creation happened were not encouraging (not including
364  * SetWindowsHookEx).
365  */
366 static void CheckWindowCreated(const char *winName, int closeWindow, int testParams)
367 {
368     HWND window = NULL;
369     int i;
370
371     /* Poll for Window Creation */
372     for (i = 0; window == NULL && i < PDDE_POLL_NUM; i++)
373     {
374         Sleep(PDDE_POLL_TIME);
375         window = FindWindowA(NULL, winName);
376     }
377     ok (window != NULL, "Window \"%s\" was not created in %i seconds - assumed failure.%s\n",
378         winName, PDDE_POLL_NUM*PDDE_POLL_TIME/1000, GetStringFromTestParams(testParams));
379
380     /* Close Window as desired. */
381     if (window != NULL && closeWindow)
382     {
383         SendMessageA(window, WM_SYSCOMMAND, SC_CLOSE, 0);
384     }
385 }
386
387 /* Check for Existence (or non-existence) of a file or group
388  *   When testing for existence of a group, groupName is not needed
389  */
390 static void CheckFileExistsInProgramGroups(const char *nameToCheck, int shouldExist, int isGroup,
391                                            const char *groupName, int testParams)
392 {
393     char path[MAX_PATH];
394     DWORD attributes;
395     int len;
396
397     lstrcpyA(path, ProgramsDir);
398
399     len = strlen(path) + strlen(nameToCheck)+1;
400     if (groupName != NULL)
401     {
402         len += strlen(groupName)+1;
403     }
404     ok (len <= MAX_PATH, "Path Too Long.%s\n", GetStringFromTestParams(testParams));
405     if (len <= MAX_PATH)
406     {
407         if (groupName != NULL)
408         {
409             strcat(path, "\\");
410             strcat(path, groupName);
411         }
412         strcat(path, "\\");
413         strcat(path, nameToCheck);
414         attributes = GetFileAttributes(path);
415         if (!shouldExist)
416         {
417             ok (attributes == INVALID_FILE_ATTRIBUTES , "File exists and shouldn't %s.%s\n",
418                 path, GetStringFromTestParams(testParams));
419         } else {
420             if (attributes == INVALID_FILE_ATTRIBUTES)
421             {
422                 ok (FALSE, "Created File %s doesn't exist.%s\n", path, GetStringFromTestParams(testParams));
423             } else if (isGroup) {
424                 ok (attributes & FILE_ATTRIBUTE_DIRECTORY, "%s is not a folder (attr=%x).%s\n",
425                     path, attributes, GetStringFromTestParams(testParams));
426             } else {
427                 ok (attributes & FILE_ATTRIBUTE_ARCHIVE, "Created File %s has wrong attributes (%x).%s\n",
428                     path, attributes, GetStringFromTestParams(testParams));
429             }
430         }
431     }
432 }
433
434 /* Create Group Test.
435  *   command and expected_result.
436  *   if expected_result is DMLERR_NO_ERROR, test
437  *        1. group was created
438  *        2. window is open
439  */
440 static void CreateGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
441                             const char *groupName, const char *windowTitle, int testParams)
442 {
443     HDDEDATA hData;
444     UINT error;
445
446     /* Execute Command & Check Result */
447     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
448     todo_wine
449     {
450         ok (expected_result == error, "CreateGroup %s: Expected Error %s, received %s.%s\n",
451             groupName, GetStringFromError(expected_result), GetStringFromError(error),
452             GetStringFromTestParams(testParams));
453     }
454
455     /* No Error */
456     if (error == DMLERR_NO_ERROR)
457     {
458
459         /* Check if Group Now Exists */
460         CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
461         /* Check if Window is Open (polling) */
462         CheckWindowCreated(windowTitle, TRUE, testParams);
463     }
464 }
465
466 /* Show Group Test.
467  *   DDE command, expected_result, and the group name to check for existence
468  *   if expected_result is DMLERR_NO_ERROR, test
469  *        1. window is open
470  */
471 static void ShowGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
472                           const char *groupName, const char *windowTitle, int closeAfterShowing, int testParams)
473 {
474     HDDEDATA hData;
475     UINT error;
476
477     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
478 /* todo_wine...  Is expected to fail, wine stubbed functions DO fail */
479 /* TODO REMOVE THIS CODE!!! */
480     if (expected_result == DMLERR_NOTPROCESSED)
481     {
482         ok (expected_result == error, "ShowGroup %s: Expected Error %s, received %s.%s\n",
483             groupName, GetStringFromError(expected_result), GetStringFromError(error),
484             GetStringFromTestParams(testParams));
485     } else {
486         todo_wine
487         {
488             ok (expected_result == error, "ShowGroup %s: Expected Error %s, received %s.%s\n",
489                 groupName, GetStringFromError(expected_result), GetStringFromError(error),
490                 GetStringFromTestParams(testParams));
491         }
492     }
493
494     if (error == DMLERR_NO_ERROR)
495     {
496         /* Check if Window is Open (polling) */
497         CheckWindowCreated(windowTitle, closeAfterShowing, testParams);
498     }
499 }
500
501 /* Delete Group Test.
502  *   DDE command, expected_result, and the group name to check for existence
503  *   if expected_result is DMLERR_NO_ERROR, test
504  *        1. group does not exist
505  */
506 static void DeleteGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
507                             const char *groupName, int testParams)
508 {
509     HDDEDATA hData;
510     UINT error;
511
512     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
513     todo_wine
514     {
515         ok (expected_result == error, "DeleteGroup %s: Expected Error %s, received %s.%s\n",
516             groupName, GetStringFromError(expected_result), GetStringFromError(error),
517             GetStringFromTestParams(testParams));
518     }
519
520     if (error == DMLERR_NO_ERROR)
521     {
522         /* Check that Group does not exist */
523         CheckFileExistsInProgramGroups(groupName, FALSE, TRUE, NULL, testParams);
524     }
525 }
526
527 /* Add Item Test
528  *   DDE command, expected result, and group and file name where it should exist.
529  *   checks to make sure error code matches expected error code
530  *   checks to make sure item exists if successful
531  */
532 static void AddItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
533                         const char *fileName, const char *groupName, int testParams)
534 {
535     HDDEDATA hData;
536     UINT error;
537
538     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
539     todo_wine
540     {
541         ok (expected_result == error, "AddItem %s: Expected Error %s, received %s.%s\n",
542             fileName, GetStringFromError(expected_result), GetStringFromError(error),
543             GetStringFromTestParams(testParams));
544     }
545
546     if (error == DMLERR_NO_ERROR)
547     {
548         /* Check that File exists */
549         CheckFileExistsInProgramGroups(fileName, TRUE, FALSE, groupName, testParams);
550     }
551 }
552
553 /* Delete Item Test.
554  *   DDE command, expected result, and group and file name where it should exist.
555  *   checks to make sure error code matches expected error code
556  *   checks to make sure item does not exist if successful
557  */
558 static void DeleteItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
559                            const char *fileName, const char *groupName, int testParams)
560 {
561     HDDEDATA hData;
562     UINT error;
563
564     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
565     todo_wine
566     {
567         ok (expected_result == error, "DeleteItem %s: Expected Error %s, received %s.%s\n",
568             fileName, GetStringFromError(expected_result), GetStringFromError(error),
569             GetStringFromTestParams(testParams));
570     }
571
572     if (error == DMLERR_NO_ERROR)
573     {
574         /* Check that File does not exist */
575         CheckFileExistsInProgramGroups(fileName, FALSE, FALSE, groupName, testParams);
576     }
577 }
578
579 /* Compound Command Test.
580  *   not really generic, assumes command of the form:
581  *          [CreateGroup ...][AddItem ...][AddItem ...]
582  *   All samples I've seen using Compound were of this form (CreateGroup,
583  *   AddItems) so this covers minimum expected functionality.
584  */
585 static void CompoundCommandTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
586                                 const char *groupName, const char *windowTitle, const char *fileName1,
587                                 const char *fileName2, int testParams)
588 {
589     HDDEDATA hData;
590     UINT error;
591
592     DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
593     todo_wine
594     {
595         ok (expected_result == error, "Compound String %s: Expected Error %s, received %s.%s\n",
596             command, GetStringFromError(expected_result), GetStringFromError(error),
597             GetStringFromTestParams(testParams));
598     }
599
600     if (error == DMLERR_NO_ERROR)
601     {
602         /* Check that File exists */
603         CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
604         CheckWindowCreated(windowTitle, FALSE, testParams);
605         CheckFileExistsInProgramGroups(fileName1, TRUE, FALSE, groupName, testParams);
606         CheckFileExistsInProgramGroups(fileName2, TRUE, FALSE, groupName, testParams);
607     }
608 }
609
610 static void CreateAddItemText(char *itemtext, const char *cmdline, const char *name)
611 {
612     lstrcpyA(itemtext, "[AddItem(");
613     lstrcatA(itemtext, cmdline);
614     lstrcatA(itemtext, ",");
615     lstrcatA(itemtext, name);
616     lstrcatA(itemtext, ")]");
617 }
618
619 /* 1st set of tests */
620 static int DdeTestProgman(DWORD instance, HCONV hConv)
621 {
622     HDDEDATA hData;
623     UINT error;
624     int testnum;
625     char temppath[MAX_PATH];
626     char f1g1[MAX_PATH], f2g1[MAX_PATH], f3g1[MAX_PATH], f1g3[MAX_PATH], f2g3[MAX_PATH];
627     char itemtext[MAX_PATH + 20];
628     char comptext[2 * (MAX_PATH + 20) + 21];
629
630     testnum = 1;
631     /* Invalid Command */
632     DdeExecuteCommand(instance, hConv, "[InvalidCommand()]", &hData, &error, DDE_TEST_MISC|testnum++);
633     ok (error == DMLERR_NOTPROCESSED, "InvalidCommand(), expected error %s, received %s.\n",
634         GetStringFromError(DMLERR_NOTPROCESSED), GetStringFromError(error));
635
636     /* On Vista+ the files have to exist when adding a link */
637     GetTempPathA(MAX_PATH, temppath);
638     GetTempFileNameA(temppath, "dde", 0, f1g1);
639     GetTempFileNameA(temppath, "dde", 0, f2g1);
640     GetTempFileNameA(temppath, "dde", 0, f3g1);
641     GetTempFileNameA(temppath, "dde", 0, f1g3);
642     GetTempFileNameA(temppath, "dde", 0, f2g3);
643
644     /* CreateGroup Tests (including AddItem, DeleteItem) */
645     CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", Group1Title, DDE_TEST_CREATEGROUP|testnum++);
646     CreateAddItemText(itemtext, f1g1, "f1g1Name");
647     AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f1g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
648     CreateAddItemText(itemtext, f2g1, "f2g1Name");
649     AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
650     DeleteItemTest(instance, hConv, "[DeleteItem(f2g1Name)]", DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_DELETEITEM|testnum++);
651     CreateAddItemText(itemtext, f3g1, "f3g1Name");
652     AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
653     CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", Group2Title, DDE_TEST_CREATEGROUP|testnum++);
654     /* Create Group that already exists - same instance */
655     CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", Group1Title, DDE_TEST_CREATEGROUP|testnum++);
656
657     /* ShowGroup Tests */
658     ShowGroupTest(instance, hConv, "[ShowGroup(Group1)]", DMLERR_NOTPROCESSED, "Group1", Group1Title, TRUE, DDE_TEST_SHOWGROUP|testnum++);
659     DeleteItemTest(instance, hConv, "[DeleteItem(f3g1Name)]", DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_DELETEITEM|testnum++);
660     ShowGroupTest(instance, hConv, "[ShowGroup(Startup,0)]", DMLERR_NO_ERROR, "Startup", StartupTitle, TRUE, DDE_TEST_SHOWGROUP|testnum++);
661     ShowGroupTest(instance, hConv, "[ShowGroup(Group1,0)]", DMLERR_NO_ERROR, "Group1", Group1Title, FALSE, DDE_TEST_SHOWGROUP|testnum++);
662
663     /* DeleteGroup Test - Note that Window is Open for this test */
664     DeleteGroupTest(instance, hConv, "[DeleteGroup(Group1)]", DMLERR_NO_ERROR, "Group1", DDE_TEST_DELETEGROUP|testnum++);
665
666     /* Compound Execute String Command */
667     lstrcpyA(comptext, "[CreateGroup(Group3)]");
668     CreateAddItemText(itemtext, f1g3, "f1g3Name");
669     lstrcatA(comptext, itemtext);
670     CreateAddItemText(itemtext, f2g3, "f2g3Name");
671     lstrcatA(comptext, itemtext);
672     CompoundCommandTest(instance, hConv, comptext, DMLERR_NO_ERROR, "Group3", Group3Title, "f1g3Name.lnk", "f2g3Name.lnk", DDE_TEST_COMPOUND|testnum++);
673
674     DeleteGroupTest(instance, hConv, "[DeleteGroup(Group3)]", DMLERR_NO_ERROR, "Group3", DDE_TEST_DELETEGROUP|testnum++);
675
676     /* Full Parameters of Add Item */
677     /* AddItem(CmdLine[,Name[,IconPath[,IconIndex[,xPos,yPos[,DefDir[,HotKey[,fMinimize[fSeparateSpace]]]]]]]) */
678
679     DeleteFileA(f1g1);
680     DeleteFileA(f2g1);
681     DeleteFileA(f3g1);
682     DeleteFileA(f1g3);
683     DeleteFileA(f2g3);
684
685     return testnum;
686 }
687
688 /* 2nd set of tests - 2nd connection */
689 static void DdeTestProgman2(DWORD instance, HCONV hConv, int testnum)
690 {
691     /* Create Group that already exists on a separate connection */
692     CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", Group2Title, DDE_TEST_CREATEGROUP|testnum++);
693     DeleteGroupTest(instance, hConv, "[DeleteGroup(Group2)]", DMLERR_NO_ERROR, "Group2", DDE_TEST_DELETEGROUP|testnum++);
694 }
695
696 START_TEST(progman_dde)
697 {
698     DWORD instance = 0;
699     UINT err;
700     HSZ hszProgman;
701     HCONV hConv;
702     int testnum;
703
704     init_function_pointers();
705     init_strings();
706
707     /* Initialize DDE Instance */
708     err = DdeInitialize(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
709     ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));
710
711     /* Create Connection */
712     hszProgman = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
713     ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
714     hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
715     ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");
716     /* Seeing failures on early versions of Windows Connecting to progman, exit if connection fails */
717     if (hConv == NULL)
718     {
719         ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
720         return;
721     }
722
723     /* Run Tests */
724     testnum = DdeTestProgman(instance, hConv);
725
726     /* Cleanup & Exit */
727     ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
728     ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
729
730     /* 2nd Instance (Followup Tests) */
731     /* Initialize DDE Instance */
732     instance = 0;
733     err = DdeInitialize(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
734     ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));
735
736     /* Create Connection */
737     hszProgman = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
738     ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
739     hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
740     ok (hConv != NULL, "DdeConnect Error %s\n", GetDdeLastErrorStr(instance));
741     ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");
742
743     /* Run Tests */
744     DdeTestProgman2(instance, hConv, testnum);
745
746     /* Cleanup & Exit */
747     ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
748     ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
749 }