progman_dde.c 27.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * Unit test of the Program Manager DDE Interfaces
 *
 * Copyright 2009 Mikey Alexander
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

/* DDE Program Manager Tests
 * - Covers basic CreateGroup, ShowGroup, DeleteGroup, AddItem, and DeleteItem
 *   functionality
 * - Todo: Handle CommonGroupFlag
 *         Better AddItem Tests (Lots of parameters to test)
 *         Tests for Invalid Characters in Names / Invalid Parameters
 */

#include <stdio.h>
#include <wine/test.h>
#include <winbase.h>
#include "dde.h"
#include "ddeml.h"
#include "winuser.h"
#include "shlobj.h"

/* Timeout on DdeClientTransaction Call */
#define MS_TIMEOUT_VAL 1000
/* # 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 45 46 47 48 49 50 51 52 53 54 55

/* Call Info */
#define DDE_TEST_MISC            0x00010000
#define DDE_TEST_CREATEGROUP     0x00020000
#define DDE_TEST_DELETEGROUP     0x00030000
#define DDE_TEST_SHOWGROUP       0x00040000
#define DDE_TEST_ADDITEM         0x00050000
#define DDE_TEST_DELETEITEM      0x00060000
#define DDE_TEST_COMPOUND        0x00070000
#define DDE_TEST_CALLMASK        0x00ff0000

#define DDE_TEST_NUMMASK           0x0000ffff

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 61 62 63 64

static void init_function_pointers(void)
{
    HMODULE hmod;

    hmod = GetModuleHandleA("shell32.dll");
65
    pSHGetLocalizedName = (void*)GetProcAddress(hmod, "SHGetLocalizedName");
66
    pSHGetSpecialFolderPathA = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathA");
67 68 69
    pReadCabinetState = (void*)GetProcAddress(hmod, "ReadCabinetState");
    if (!pReadCabinetState)
        pReadCabinetState = (void*)GetProcAddress(hmod, (LPSTR)651);
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static BOOL use_common(void)
{
    HMODULE hmod;
    static BOOL (WINAPI *pIsNTAdmin)(DWORD, LPDWORD);

    /* IsNTAdmin() is available on all platforms. */
    hmod = LoadLibraryA("advpack.dll");
    pIsNTAdmin = (void*)GetProcAddress(hmod, "IsNTAdmin");

    if (!pIsNTAdmin(0, NULL))
    {
        /* We are definitely not an administrator */
        FreeLibrary(hmod);
        return FALSE;
    }
    FreeLibrary(hmod);

    /* If we end up here we are on NT4+ as Win9x and WinMe don't have the
     * notion of administrators (as we need it).
     */

    /* As of Vista  we should always use the users directory. Tests with the
     * real Administrator account on Windows 7 proved this.
     *
     * FIXME: We need a better way of identifying Vista+ as currently this check
     * also covers Wine and we don't know yet which behavior we want to follow.
     */
    if (pSHGetLocalizedName)
        return FALSE;

    return TRUE;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static BOOL full_title(void)
{
    CABINETSTATE cs;

    memset(&cs, 0, sizeof(cs));
    if (pReadCabinetState)
    {
        pReadCabinetState(&cs, sizeof(cs));
    }
    else
    {
        HKEY key;
        DWORD size;

        win_skip("ReadCabinetState is not available, reading registry directly\n");
        RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CabinetState", &key);
        size = sizeof(cs);
        RegQueryValueExA(key, "Settings", NULL, NULL, (LPBYTE)&cs, &size);
        RegCloseKey(key);
    }

    return (cs.fFullPathTitle == -1);
}

129
static char ProgramsDir[MAX_PATH];
130

131 132 133 134 135 136 137
static char Group1Title[MAX_PATH]  = "Group1";
static char Group2Title[MAX_PATH]  = "Group2";
static char Group3Title[MAX_PATH]  = "Group3";
static char StartupTitle[MAX_PATH] = "Startup";

static void init_strings(void)
{
138
    char startup[MAX_PATH];
139 140 141
    char commonprograms[MAX_PATH];
    char programs[MAX_PATH];

142
    if (pSHGetSpecialFolderPathA)
143
    {
144 145
        pSHGetSpecialFolderPathA(NULL, programs, CSIDL_PROGRAMS, FALSE);
        pSHGetSpecialFolderPathA(NULL, commonprograms, CSIDL_COMMON_PROGRAMS, FALSE);
146 147 148 149
        pSHGetSpecialFolderPathA(NULL, startup, CSIDL_STARTUP, FALSE);
    }
    else
    {
150 151
        HKEY key;
        DWORD size;
152 153

        /* Older Win9x and NT4 */
154

155
        RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &key);
156 157
        size = sizeof(programs);
        RegQueryValueExA(key, "Programs", NULL, NULL, (LPBYTE)&programs, &size);
158 159 160 161
        size = sizeof(startup);
        RegQueryValueExA(key, "Startup", NULL, NULL, (LPBYTE)&startup, &size);
        RegCloseKey(key);

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 169 170 171 172 173 174 175 176
    /* ProgramsDir on Vista+ is always the users one (CSIDL_PROGRAMS). Before Vista
     * it depends on whether the user is an administrator (CSIDL_COMMON_PROGRAMS) or
     * not (CSIDL_PROGRAMS).
     */
    if (use_common())
        lstrcpyA(ProgramsDir, commonprograms);
    else
        lstrcpyA(ProgramsDir, programs);

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 185 186 187
        lstrcatA(Group3Title, "\\Group3");

        lstrcpyA(StartupTitle, startup);
    }
188 189
    else
    {
190 191 192 193 194 195 196 197 198 199 200 201 202
        /* Vista has the nice habit of displaying the full path in English
         * and the short one localized. CSIDL_STARTUP on Vista gives us the
         * English version so we have to 'translate' this one.
         *
         * MSDN claims it should be used for files not folders but this one
         * suits our purposes just fine.
         */
        if (pSHGetLocalizedName)
        {
            WCHAR startupW[MAX_PATH];
            WCHAR module[MAX_PATH];
            WCHAR module_expanded[MAX_PATH];
            WCHAR localized[MAX_PATH];
203
            HRESULT hr;
204 205 206
            int id;

            MultiByteToWideChar(CP_ACP, 0, startup, -1, startupW, sizeof(startupW)/sizeof(WCHAR));
207 208 209 210 211 212 213 214 215 216 217 218
            hr = pSHGetLocalizedName(startupW, module, MAX_PATH, &id);
            todo_wine ok(hr == S_OK, "got 0x%08x\n", hr);
            /* check to be removed when SHGetLocalizedName is implemented */
            if (hr == S_OK)
            {
                ExpandEnvironmentStringsW(module, module_expanded, MAX_PATH);
                LoadStringW(GetModuleHandleW(module_expanded), id, localized, MAX_PATH);

                WideCharToMultiByte(CP_ACP, 0, localized, -1, StartupTitle, sizeof(StartupTitle), NULL, NULL);
            }
            else
                lstrcpyA(StartupTitle, (strrchr(startup, '\\') + 1));
219 220 221 222 223
        }
        else
        {
            lstrcpyA(StartupTitle, (strrchr(startup, '\\') + 1));
        }
224
    }
225 226
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static HDDEDATA CALLBACK DdeCallback(UINT type, UINT format, HCONV hConv, HSZ hsz1, HSZ hsz2,
                                     HDDEDATA hDDEData, ULONG_PTR data1, ULONG_PTR data2)
{
    trace("Callback: type=%i, format=%i\n", type, format);
    return NULL;
}

/*
 * Encoded String for Error Messages so that inner failures can determine
 * what test is failing.  Format is: [Code:TestNum]
 */
static const char * GetStringFromTestParams(int testParams)
{
    int testNum;
    static char testParamString[64];
    const char *callId;

    testNum = testParams & DDE_TEST_NUMMASK;
    switch (testParams & DDE_TEST_CALLMASK)
    {
    default:
    case DDE_TEST_MISC:
        callId = "MISC";
        break;
    case DDE_TEST_CREATEGROUP:
        callId = "C_G";
        break;
    case DDE_TEST_DELETEGROUP:
        callId = "D_G";
        break;
    case DDE_TEST_SHOWGROUP:
        callId = "S_G";
        break;
    case DDE_TEST_ADDITEM:
        callId = "A_I";
        break;
    case DDE_TEST_DELETEITEM:
        callId = "D_I";
        break;
    case DDE_TEST_COMPOUND:
        callId = "CPD";
        break;
    }

    sprintf(testParamString, "  [%s:%i]", callId, testNum);
    return testParamString;
}

/* Transfer DMLERR's into text readable strings for Error Messages */
#define DMLERR_TO_STR(x) case x: return#x;
static const char * GetStringFromError(UINT err)
{
    switch (err)
    {
    DMLERR_TO_STR(DMLERR_NO_ERROR);
    DMLERR_TO_STR(DMLERR_ADVACKTIMEOUT);
    DMLERR_TO_STR(DMLERR_BUSY);
    DMLERR_TO_STR(DMLERR_DATAACKTIMEOUT);
    DMLERR_TO_STR(DMLERR_DLL_NOT_INITIALIZED);
    DMLERR_TO_STR(DMLERR_DLL_USAGE);
    DMLERR_TO_STR(DMLERR_EXECACKTIMEOUT);
    DMLERR_TO_STR(DMLERR_INVALIDPARAMETER);
    DMLERR_TO_STR(DMLERR_LOW_MEMORY);
    DMLERR_TO_STR(DMLERR_MEMORY_ERROR);
    DMLERR_TO_STR(DMLERR_NOTPROCESSED);
    DMLERR_TO_STR(DMLERR_NO_CONV_ESTABLISHED);
    DMLERR_TO_STR(DMLERR_POKEACKTIMEOUT);
    DMLERR_TO_STR(DMLERR_POSTMSG_FAILED);
    DMLERR_TO_STR(DMLERR_REENTRANCY);
    DMLERR_TO_STR(DMLERR_SERVER_DIED);
    DMLERR_TO_STR(DMLERR_SYS_ERROR);
    DMLERR_TO_STR(DMLERR_UNADVACKTIMEOUT);
    DMLERR_TO_STR(DMLERR_UNFOUND_QUEUE_ID);
    default:
301
        return "Unknown DML Error";
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    }
}

/* Helper Function to Transfer DdeGetLastError into a String */
static const char * GetDdeLastErrorStr(DWORD instance)
{
    UINT err = DdeGetLastError(instance);

    return GetStringFromError(err);
}

/* Execute a Dde Command and return the error & result */
/* Note: Progman DDE always returns a pointer to 0x00000001 on a successful result */
static void DdeExecuteCommand(DWORD instance, HCONV hConv, const char *strCmd, HDDEDATA *hData, UINT *err, int testParams)
{
    HDDEDATA command;

    command = DdeCreateDataHandle(instance, (LPBYTE) strCmd, strlen(strCmd)+1, 0, 0L, 0, 0);
    ok (command != NULL, "DdeCreateDataHandle Error %s.%s\n",
        GetDdeLastErrorStr(instance), GetStringFromTestParams(testParams));
    *hData = DdeClientTransaction((void *) command,
                                  -1,
                                  hConv,
                                  0,
                                  0,
                                  XTYP_EXECUTE,
                                  MS_TIMEOUT_VAL,
                                  NULL);

    /* hData is technically a pointer, but for Program Manager,
     * it is NULL (error) or 1 (success)
     * TODO: Check other versions of Windows to verify 1 is returned.
     * While it is unlikely that anyone is actually testing that the result is 1
     * if all versions of windows return 1, Wine should also.
     */
    if (*hData == NULL)
    {
        *err = DdeGetLastError(instance);
    }
    else
    {
        *err = DMLERR_NO_ERROR;
        todo_wine
        {
            ok(*hData == (HDDEDATA) 1, "Expected HDDEDATA Handle == 1, actually %p.%s\n",
               *hData, GetStringFromTestParams(testParams));
        }
    }
350
    DdeFreeDataHandle(command);
351 352 353 354 355
}

/*
 * Check if Window is onscreen with the appropriate name.
 *
356
 * Windows are not created synchronously.  So we do not know
357 358 359 360 361 362 363 364 365
 * when and if the window will be created/shown on screen.
 * This function implements a polling mechanism to determine
 * creation.
 * A more complicated method would be to use SetWindowsHookEx.
 * Since polling worked fine in my testing, no reason to implement
 * the other.  Comments about other methods of determining when
 * window creation happened were not encouraging (not including
 * SetWindowsHookEx).
 */
366
static HWND CheckWindowCreated(const char *winName, BOOL closeWindow, int testParams)
367 368 369 370 371 372 373 374
{
    HWND window = NULL;
    int i;

    /* Poll for Window Creation */
    for (i = 0; window == NULL && i < PDDE_POLL_NUM; i++)
    {
        Sleep(PDDE_POLL_TIME);
375 376 377 378 379 380 381
        /* Specify the window class name to make sure what we find is really an
         * Explorer window. Explorer used two different window classes so try
         * both.
         */
        window = FindWindowA("ExplorerWClass", winName);
        if (!window)
            window = FindWindowA("CabinetWClass", winName);
382 383 384 385 386 387 388 389
    }
    ok (window != NULL, "Window \"%s\" was not created in %i seconds - assumed failure.%s\n",
        winName, PDDE_POLL_NUM*PDDE_POLL_TIME/1000, GetStringFromTestParams(testParams));

    /* Close Window as desired. */
    if (window != NULL && closeWindow)
    {
        SendMessageA(window, WM_SYSCOMMAND, SC_CLOSE, 0);
390
        window = NULL;
391
    }
392
    return window;
393 394 395 396 397
}

/* Check for Existence (or non-existence) of a file or group
 *   When testing for existence of a group, groupName is not needed
 */
398
static void CheckFileExistsInProgramGroups(const char *nameToCheck, BOOL shouldExist, BOOL isGroup,
399 400
                                           const char *groupName, int testParams)
{
401
    char path[MAX_PATH];
402 403 404
    DWORD attributes;
    int len;

405
    lstrcpyA(path, ProgramsDir);
406

407 408 409 410 411 412 413 414
    len = strlen(path) + strlen(nameToCheck)+1;
    if (groupName != NULL)
    {
        len += strlen(groupName)+1;
    }
    ok (len <= MAX_PATH, "Path Too Long.%s\n", GetStringFromTestParams(testParams));
    if (len <= MAX_PATH)
    {
415 416
        if (groupName != NULL)
        {
417 418
            strcat(path, "\\");
            strcat(path, groupName);
419
        }
420 421
        strcat(path, "\\");
        strcat(path, nameToCheck);
422
        attributes = GetFileAttributesA(path);
423
        if (!shouldExist)
424
        {
425 426 427 428
            ok (attributes == INVALID_FILE_ATTRIBUTES , "File exists and shouldn't %s.%s\n",
                path, GetStringFromTestParams(testParams));
        } else {
            if (attributes == INVALID_FILE_ATTRIBUTES)
429
            {
430 431 432 433
                ok (FALSE, "Created File %s doesn't exist.%s\n", path, GetStringFromTestParams(testParams));
            } else if (isGroup) {
                ok (attributes & FILE_ATTRIBUTE_DIRECTORY, "%s is not a folder (attr=%x).%s\n",
                    path, attributes, GetStringFromTestParams(testParams));
434
            } else {
435 436
                ok (attributes & FILE_ATTRIBUTE_ARCHIVE, "Created File %s has wrong attributes (%x).%s\n",
                    path, attributes, GetStringFromTestParams(testParams));
437 438 439 440 441 442 443 444 445 446 447 448
            }
        }
    }
}

/* Create Group Test.
 *   command and expected_result.
 *   if expected_result is DMLERR_NO_ERROR, test
 *        1. group was created
 *        2. window is open
 */
static void CreateGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
449
                            const char *groupName, const char *windowTitle, int testParams)
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
    HDDEDATA hData;
    UINT error;

    /* Execute Command & Check Result */
    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
    todo_wine
    {
        ok (expected_result == error, "CreateGroup %s: Expected Error %s, received %s.%s\n",
            groupName, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));
    }

    /* No Error */
    if (error == DMLERR_NO_ERROR)
    {

        /* Check if Group Now Exists */
        CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
        /* Check if Window is Open (polling) */
470
        CheckWindowCreated(windowTitle, TRUE, testParams);
471 472 473 474 475 476 477 478
    }
}

/* Show Group Test.
 *   DDE command, expected_result, and the group name to check for existence
 *   if expected_result is DMLERR_NO_ERROR, test
 *        1. window is open
 */
479
static HWND ShowGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
480
                          const char *groupName, const char *windowTitle, BOOL closeAfterShowing, int testParams)
481 482 483
{
    HDDEDATA hData;
    UINT error;
484
    HWND hwnd = 0;
485 486 487 488

    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
/* todo_wine...  Is expected to fail, wine stubbed functions DO fail */
/* TODO REMOVE THIS CODE!!! */
489
    todo_wine_if (expected_result != DMLERR_NOTPROCESSED)
490 491 492 493 494 495 496
        ok (expected_result == error, "ShowGroup %s: Expected Error %s, received %s.%s\n",
            groupName, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));

    if (error == DMLERR_NO_ERROR)
    {
        /* Check if Window is Open (polling) */
497
        hwnd = CheckWindowCreated(windowTitle, closeAfterShowing, testParams);
498
    }
499
    return hwnd;
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
}

/* Delete Group Test.
 *   DDE command, expected_result, and the group name to check for existence
 *   if expected_result is DMLERR_NO_ERROR, test
 *        1. group does not exist
 */
static void DeleteGroupTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
                            const char *groupName, int testParams)
{
    HDDEDATA hData;
    UINT error;

    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
    todo_wine
    {
        ok (expected_result == error, "DeleteGroup %s: Expected Error %s, received %s.%s\n",
            groupName, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));
    }

    if (error == DMLERR_NO_ERROR)
    {
        /* Check that Group does not exist */
        CheckFileExistsInProgramGroups(groupName, FALSE, TRUE, NULL, testParams);
    }
}

/* Add Item Test
 *   DDE command, expected result, and group and file name where it should exist.
 *   checks to make sure error code matches expected error code
 *   checks to make sure item exists if successful
 */
static void AddItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
                        const char *fileName, const char *groupName, int testParams)
{
    HDDEDATA hData;
    UINT error;

    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
    todo_wine
    {
        ok (expected_result == error, "AddItem %s: Expected Error %s, received %s.%s\n",
            fileName, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));
    }

    if (error == DMLERR_NO_ERROR)
    {
        /* Check that File exists */
        CheckFileExistsInProgramGroups(fileName, TRUE, FALSE, groupName, testParams);
    }
}

/* Delete Item Test.
 *   DDE command, expected result, and group and file name where it should exist.
 *   checks to make sure error code matches expected error code
 *   checks to make sure item does not exist if successful
 */
static void DeleteItemTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
                           const char *fileName, const char *groupName, int testParams)
{
    HDDEDATA hData;
    UINT error;

    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
    todo_wine
    {
        ok (expected_result == error, "DeleteItem %s: Expected Error %s, received %s.%s\n",
            fileName, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));
    }

    if (error == DMLERR_NO_ERROR)
    {
        /* Check that File does not exist */
        CheckFileExistsInProgramGroups(fileName, FALSE, FALSE, groupName, testParams);
    }
}

/* Compound Command Test.
 *   not really generic, assumes command of the form:
 *          [CreateGroup ...][AddItem ...][AddItem ...]
 *   All samples I've seen using Compound were of this form (CreateGroup,
 *   AddItems) so this covers minimum expected functionality.
 */
586
static HWND CompoundCommandTest(DWORD instance, HCONV hConv, const char *command, UINT expected_result,
587
                                const char *groupName, const char *windowTitle, const char *fileName1,
588 589 590 591
                                const char *fileName2, int testParams)
{
    HDDEDATA hData;
    UINT error;
592
    HWND hwnd = 0;
593 594 595 596 597 598 599 600 601 602 603 604 605

    DdeExecuteCommand(instance, hConv, command, &hData, &error, testParams);
    todo_wine
    {
        ok (expected_result == error, "Compound String %s: Expected Error %s, received %s.%s\n",
            command, GetStringFromError(expected_result), GetStringFromError(error),
            GetStringFromTestParams(testParams));
    }

    if (error == DMLERR_NO_ERROR)
    {
        /* Check that File exists */
        CheckFileExistsInProgramGroups(groupName, TRUE, TRUE, NULL, testParams);
606
        hwnd = CheckWindowCreated(windowTitle, FALSE, testParams);
607 608 609
        CheckFileExistsInProgramGroups(fileName1, TRUE, FALSE, groupName, testParams);
        CheckFileExistsInProgramGroups(fileName2, TRUE, FALSE, groupName, testParams);
    }
610
    return hwnd;
611 612
}

613 614 615 616 617 618 619 620 621
static void CreateAddItemText(char *itemtext, const char *cmdline, const char *name)
{
    lstrcpyA(itemtext, "[AddItem(");
    lstrcatA(itemtext, cmdline);
    lstrcatA(itemtext, ",");
    lstrcatA(itemtext, name);
    lstrcatA(itemtext, ")]");
}

622 623 624 625 626 627
/* 1st set of tests */
static int DdeTestProgman(DWORD instance, HCONV hConv)
{
    HDDEDATA hData;
    UINT error;
    int testnum;
628 629 630 631
    char temppath[MAX_PATH];
    char f1g1[MAX_PATH], f2g1[MAX_PATH], f3g1[MAX_PATH], f1g3[MAX_PATH], f2g3[MAX_PATH];
    char itemtext[MAX_PATH + 20];
    char comptext[2 * (MAX_PATH + 20) + 21];
632
    HWND hwnd;
633 634 635 636 637 638 639

    testnum = 1;
    /* Invalid Command */
    DdeExecuteCommand(instance, hConv, "[InvalidCommand()]", &hData, &error, DDE_TEST_MISC|testnum++);
    ok (error == DMLERR_NOTPROCESSED, "InvalidCommand(), expected error %s, received %s.\n",
        GetStringFromError(DMLERR_NOTPROCESSED), GetStringFromError(error));

640 641 642 643 644 645 646 647
    /* On Vista+ the files have to exist when adding a link */
    GetTempPathA(MAX_PATH, temppath);
    GetTempFileNameA(temppath, "dde", 0, f1g1);
    GetTempFileNameA(temppath, "dde", 0, f2g1);
    GetTempFileNameA(temppath, "dde", 0, f3g1);
    GetTempFileNameA(temppath, "dde", 0, f1g3);
    GetTempFileNameA(temppath, "dde", 0, f2g3);

648
    /* CreateGroup Tests (including AddItem, DeleteItem) */
649
    CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", Group1Title, DDE_TEST_CREATEGROUP|testnum++);
650
    CreateAddItemText(itemtext, f1g1, "f1g1Name");
651
    AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f1g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
652
    CreateAddItemText(itemtext, f2g1, "f2g1Name");
653 654
    AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
    DeleteItemTest(instance, hConv, "[DeleteItem(f2g1Name)]", DMLERR_NO_ERROR, "f2g1Name.lnk", "Group1", DDE_TEST_DELETEITEM|testnum++);
655
    CreateAddItemText(itemtext, f3g1, "f3g1Name");
656 657
    AddItemTest(instance, hConv, itemtext, DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_ADDITEM|testnum++);
    CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", Group2Title, DDE_TEST_CREATEGROUP|testnum++);
658
    /* Create Group that already exists - same instance */
659
    CreateGroupTest(instance, hConv, "[CreateGroup(Group1)]", DMLERR_NO_ERROR, "Group1", Group1Title, DDE_TEST_CREATEGROUP|testnum++);
660 661

    /* ShowGroup Tests */
662
    ShowGroupTest(instance, hConv, "[ShowGroup(Group1)]", DMLERR_NOTPROCESSED, "Group1", Group1Title, TRUE, DDE_TEST_SHOWGROUP|testnum++);
663
    DeleteItemTest(instance, hConv, "[DeleteItem(f3g1Name)]", DMLERR_NO_ERROR, "f3g1Name.lnk", "Group1", DDE_TEST_DELETEITEM|testnum++);
664
    ShowGroupTest(instance, hConv, "[ShowGroup(Startup,0)]", DMLERR_NO_ERROR, "Startup", StartupTitle, TRUE, DDE_TEST_SHOWGROUP|testnum++);
665
    hwnd = ShowGroupTest(instance, hConv, "[ShowGroup(Group1,0)]", DMLERR_NO_ERROR, "Group1", Group1Title, FALSE, DDE_TEST_SHOWGROUP|testnum++);
666 667 668

    /* DeleteGroup Test - Note that Window is Open for this test */
    DeleteGroupTest(instance, hConv, "[DeleteGroup(Group1)]", DMLERR_NO_ERROR, "Group1", DDE_TEST_DELETEGROUP|testnum++);
669
    if (hwnd) SendMessageA(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
670 671

    /* Compound Execute String Command */
672 673 674 675 676
    lstrcpyA(comptext, "[CreateGroup(Group3)]");
    CreateAddItemText(itemtext, f1g3, "f1g3Name");
    lstrcatA(comptext, itemtext);
    CreateAddItemText(itemtext, f2g3, "f2g3Name");
    lstrcatA(comptext, itemtext);
677
    hwnd = CompoundCommandTest(instance, hConv, comptext, DMLERR_NO_ERROR, "Group3", Group3Title, "f1g3Name.lnk", "f2g3Name.lnk", DDE_TEST_COMPOUND|testnum++);
678

679
    DeleteGroupTest(instance, hConv, "[DeleteGroup(Group3)]", DMLERR_NO_ERROR, "Group3", DDE_TEST_DELETEGROUP|testnum++);
680
    if (hwnd) SendMessageA(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
681 682 683 684

    /* Full Parameters of Add Item */
    /* AddItem(CmdLine[,Name[,IconPath[,IconIndex[,xPos,yPos[,DefDir[,HotKey[,fMinimize[fSeparateSpace]]]]]]]) */

685 686 687 688 689 690
    DeleteFileA(f1g1);
    DeleteFileA(f2g1);
    DeleteFileA(f3g1);
    DeleteFileA(f1g3);
    DeleteFileA(f2g3);

691 692 693 694 695 696 697
    return testnum;
}

/* 2nd set of tests - 2nd connection */
static void DdeTestProgman2(DWORD instance, HCONV hConv, int testnum)
{
    /* Create Group that already exists on a separate connection */
698 699
    CreateGroupTest(instance, hConv, "[CreateGroup(Group2)]", DMLERR_NO_ERROR, "Group2", Group2Title, DDE_TEST_CREATEGROUP|testnum++);
    DeleteGroupTest(instance, hConv, "[DeleteGroup(Group2)]", DMLERR_NO_ERROR, "Group2", DDE_TEST_DELETEGROUP|testnum++);
700 701 702 703 704 705 706 707 708 709
}

START_TEST(progman_dde)
{
    DWORD instance = 0;
    UINT err;
    HSZ hszProgman;
    HCONV hConv;
    int testnum;

710
    init_function_pointers();
711
    init_strings();
712

713
    /* Initialize DDE Instance */
714
    err = DdeInitializeA(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
715 716 717
    ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));

    /* Create Connection */
718
    hszProgman = DdeCreateStringHandleA(instance, "PROGMAN", CP_WINANSI);
719 720 721
    ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
    hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
    ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");
722 723 724 725 726 727
    /* Seeing failures on early versions of Windows Connecting to progman, exit if connection fails */
    if (hConv == NULL)
    {
        ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
        return;
    }
728 729 730 731 732 733 734 735 736 737 738

    /* Run Tests */
    testnum = DdeTestProgman(instance, hConv);

    /* Cleanup & Exit */
    ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
    ok (DdeUninitialize(instance), "DdeUninitialize failed\n");

    /* 2nd Instance (Followup Tests) */
    /* Initialize DDE Instance */
    instance = 0;
739
    err = DdeInitializeA(&instance, DdeCallback, APPCMD_CLIENTONLY, 0);
740 741 742
    ok (err == DMLERR_NO_ERROR, "DdeInitialize Error %s\n", GetStringFromError(err));

    /* Create Connection */
743
    hszProgman = DdeCreateStringHandleA(instance, "PROGMAN", CP_WINANSI);
744 745 746 747 748 749 750 751 752 753 754 755
    ok (hszProgman != NULL, "DdeCreateStringHandle Error %s\n", GetDdeLastErrorStr(instance));
    hConv = DdeConnect(instance, hszProgman, hszProgman, NULL);
    ok (hConv != NULL, "DdeConnect Error %s\n", GetDdeLastErrorStr(instance));
    ok (DdeFreeStringHandle(instance, hszProgman), "DdeFreeStringHandle failure\n");

    /* Run Tests */
    DdeTestProgman2(instance, hConv, testnum);

    /* Cleanup & Exit */
    ok (DdeDisconnect(hConv), "DdeDisonnect Error %s\n", GetDdeLastErrorStr(instance));
    ok (DdeUninitialize(instance), "DdeUninitialize failed\n");
}