nstc.c 88.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
/*
 *    Unit tests for the NamespaceTree Control
 *
 * Copyright 2010 David Hedberg
 *
 * 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
 */

#include <stdio.h>

#define COBJMACROS
24
#define CONST_VTABLE
25 26 27 28

#include "shlobj.h"
#include "wine/test.h"

29 30
#include "msg.h"

31 32 33 34 35
static HWND hwnd;

/* "Intended for internal use" */
#define TVS_EX_NOSINGLECOLLAPSE 0x1

36 37
static HRESULT (WINAPI *pSHCreateShellItem)(LPCITEMIDLIST,IShellFolder*,LPCITEMIDLIST,IShellItem**);
static HRESULT (WINAPI *pSHGetIDListFromObject)(IUnknown*, PIDLIST_ABSOLUTE*);
38
static HRESULT (WINAPI *pSHCreateItemFromParsingName)(PCWSTR,IBindCtx*,REFIID,void**);
39
static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
40

41 42 43 44 45 46 47 48 49
#define NUM_MSG_SEQUENCES 1
#define TREEVIEW_SEQ_INDEX 0

static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];

/* Keep a copy of the last structure passed by TVM_SETITEMW */
static TVITEMEXW last_tvi;
static int tvi_count;

50 51 52 53 54 55 56
static void init_function_pointers(void)
{
    HMODULE hmod;

    hmod = GetModuleHandleA("shell32.dll");
    pSHCreateShellItem = (void*)GetProcAddress(hmod, "SHCreateShellItem");
    pSHGetIDListFromObject = (void*)GetProcAddress(hmod, "SHGetIDListFromObject");
57
    pSHCreateItemFromParsingName = (void*)GetProcAddress(hmod, "SHCreateItemFromParsingName");
58
    pSHGetSpecialFolderLocation = (void*)GetProcAddress(hmod, "SHGetSpecialFolderLocation");
59 60 61 62 63 64 65 66 67 68 69 70
}

/*******************************************************
 * INameSpaceTreeControlEvents implementation.
 */
enum { OnItemClick = 0, OnPropertyItemCommit, OnItemStateChanging, OnItemStateChanged,
       OnSelectionChanged, OnKeyboardInput, OnBeforeExpand, OnAfterExpand, OnBeginLabelEdit,
       OnEndLabelEdit, OnGetToolTip, OnBeforeItemDelete, OnItemAdded, OnItemDeleted,
       OnBeforeContextMenu, OnAfterContextMenu, OnBeforeStateImageChange, OnGetDefaultIconIndex,
       LastEvent };

typedef struct {
71
    INameSpaceTreeControlEvents INameSpaceTreeControlEvents_iface;
72 73 74 75 76 77
    UINT qi_called_count;     /* Keep track of calls to QueryInterface */
    BOOL qi_enable_events;    /* If FALSE, QueryInterface returns only E_NOINTERFACE */
    UINT count[LastEvent];    /* Keep track of calls to all On* functions. */
    LONG ref;
} INameSpaceTreeControlEventsImpl;

78 79 80 81
static inline INameSpaceTreeControlEventsImpl *impl_from_INameSpaceTreeControlEvents(INameSpaceTreeControlEvents *iface)
{
    return CONTAINING_RECORD(iface, INameSpaceTreeControlEventsImpl, INameSpaceTreeControlEvents_iface);
}
82 83 84 85 86 87

static HRESULT WINAPI NSTCEvents_fnQueryInterface(
    INameSpaceTreeControlEvents* iface,
    REFIID riid,
    void **ppvObject)
{
88 89 90
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    This->qi_called_count++;
91

92
    if(This->qi_enable_events && IsEqualIID(riid, &IID_INameSpaceTreeControlEvents))
93 94 95 96 97 98 99 100 101 102 103 104
    {
        IUnknown_AddRef(iface);
        *ppvObject = iface;
        return S_OK;
    }

    return E_NOINTERFACE;
}

static ULONG WINAPI NSTCEvents_fnAddRef(
    INameSpaceTreeControlEvents* iface)
{
105 106 107
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    return InterlockedIncrement(&This->ref);
108 109 110 111 112
}

static ULONG WINAPI NSTCEvents_fnRelease(
    INameSpaceTreeControlEvents* iface)
{
113 114 115
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    return InterlockedDecrement(&This->ref);
116 117 118 119 120 121 122 123
}

static HRESULT WINAPI NSTCEvents_fnOnItemClick(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    NSTCEHITTEST nstceHitTest,
    NSTCECLICKTYPE nstceClickType)
{
124 125
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

126
    ok(psi != NULL, "NULL IShellItem\n");
127
    This->count[OnItemClick]++;
128 129 130 131 132 133 134
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnPropertyItemCommit(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
135 136
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

137
    ok(psi != NULL, "NULL IShellItem\n");
138
    This->count[OnPropertyItemCommit]++;
139 140 141 142 143 144 145 146 147
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnItemStateChanging(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    NSTCITEMSTATE nstcisMask,
    NSTCITEMSTATE nstcisState)
{
148 149
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

150
    ok(psi != NULL, "NULL IShellItem\n");
151
    This->count[OnItemStateChanging]++;
152 153 154 155 156 157 158 159 160
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnItemStateChanged(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    NSTCITEMSTATE nstcisMask,
    NSTCITEMSTATE nstcisState)
{
161 162
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

163
    ok(psi != NULL, "NULL IShellItem\n");
164
    This->count[OnItemStateChanged]++;
165 166 167 168 169 170 171
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnSelectionChanged(
    INameSpaceTreeControlEvents* iface,
    IShellItemArray *psiaSelection)
{
172 173
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

174 175 176 177 178 179 180 181 182
    ok(psiaSelection != NULL, "IShellItemArray was NULL.\n");
    if(psiaSelection)
    {
        HRESULT hr;
        DWORD count = 0xdeadbeef;
        hr = IShellItemArray_GetCount(psiaSelection, &count);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(count == 1, "Got count 0x%x\n", count);
    }
183
    This->count[OnSelectionChanged]++;
184 185 186 187 188 189 190 191 192
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnKeyboardInput(
    INameSpaceTreeControlEvents* iface,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam)
{
193 194 195
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    This->count[OnKeyboardInput]++;
196 197
    ok(wParam == 0x1234, "Got unexpected wParam %lx\n", wParam);
    ok(lParam == 0x1234, "Got unexpected lParam %lx\n", lParam);
198 199 200 201 202 203 204
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnBeforeExpand(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
205 206
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

207
    ok(psi != NULL, "NULL IShellItem\n");
208
    This->count[OnBeforeExpand]++;
209 210 211 212 213 214 215
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnAfterExpand(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
216 217
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

218
    ok(psi != NULL, "NULL IShellItem\n");
219
    This->count[OnAfterExpand]++;
220 221 222 223 224 225 226
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnBeginLabelEdit(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
227 228
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

229
    ok(psi != NULL, "NULL IShellItem\n");
230
    This->count[OnBeginLabelEdit]++;
231 232 233 234 235 236 237
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnEndLabelEdit(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
238 239
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

240
    ok(psi != NULL, "NULL IShellItem\n");
241
    This->count[OnEndLabelEdit]++;
242 243 244 245 246 247 248 249 250
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnGetToolTip(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    LPWSTR pszTip,
    int cchTip)
{
251 252
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

253
    ok(psi != NULL, "NULL IShellItem\n");
254
    This->count[OnGetToolTip]++;
255 256 257 258 259 260 261
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnBeforeItemDelete(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi)
{
262 263
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

264
    ok(psi != NULL, "NULL IShellItem\n");
265
    This->count[OnBeforeItemDelete]++;
266 267 268 269 270 271 272 273
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnItemAdded(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    BOOL fIsRoot)
{
274 275
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

276
    ok(psi != NULL, "NULL IShellItem\n");
277
    This->count[OnItemAdded]++;
278 279 280 281 282 283 284 285
    return S_OK;
}

static HRESULT WINAPI NSTCEvents_fnOnItemDeleted(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    BOOL fIsRoot)
{
286 287
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

288
    ok(psi != NULL, "NULL IShellItem\n");
289
    This->count[OnItemDeleted]++;
290 291 292 293 294 295 296 297 298
    return S_OK;
}

static HRESULT WINAPI NSTCEvents_fnOnBeforeContextMenu(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    REFIID riid,
    void **ppv)
{
299 300 301
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    This->count[OnBeforeContextMenu]++;
302 303 304 305 306 307 308 309 310 311
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnAfterContextMenu(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    IContextMenu *pcmIn,
    REFIID riid,
    void **ppv)
{
312 313 314
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

    This->count[OnAfterContextMenu]++;
315 316 317 318 319
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnBeforeStateImageChange(
    INameSpaceTreeControlEvents* iface,
320
    IShellItem *psi)
321
{
322 323
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

324
    ok(psi != NULL, "NULL IShellItem\n");
325
    This->count[OnBeforeStateImageChange]++;
326 327 328 329 330 331 332 333 334
    return E_NOTIMPL;
}

static HRESULT WINAPI NSTCEvents_fnOnGetDefaultIconIndex(
    INameSpaceTreeControlEvents* iface,
    IShellItem *psi,
    int *piDefaultIcon,
    int *piOpenIcon)
{
335 336
    INameSpaceTreeControlEventsImpl *This = impl_from_INameSpaceTreeControlEvents(iface);

337
    ok(psi != NULL, "NULL IShellItem\n");
338
    This->count[OnGetDefaultIconIndex]++;
339 340 341
    return E_NOTIMPL;
}

342
static const INameSpaceTreeControlEventsVtbl vt_NSTCEvents = {
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    NSTCEvents_fnQueryInterface,
    NSTCEvents_fnAddRef,
    NSTCEvents_fnRelease,
    NSTCEvents_fnOnItemClick,
    NSTCEvents_fnOnPropertyItemCommit,
    NSTCEvents_fnOnItemStateChanging,
    NSTCEvents_fnOnItemStateChanged,
    NSTCEvents_fnOnSelectionChanged,
    NSTCEvents_fnOnKeyboardInput,
    NSTCEvents_fnOnBeforeExpand,
    NSTCEvents_fnOnAfterExpand,
    NSTCEvents_fnOnBeginLabelEdit,
    NSTCEvents_fnOnEndLabelEdit,
    NSTCEvents_fnOnGetToolTip,
    NSTCEvents_fnOnBeforeItemDelete,
    NSTCEvents_fnOnItemAdded,
    NSTCEvents_fnOnItemDeleted,
    NSTCEvents_fnOnBeforeContextMenu,
    NSTCEvents_fnOnAfterContextMenu,
    NSTCEvents_fnOnBeforeStateImageChange,
    NSTCEvents_fnOnGetDefaultIconIndex
};

static INameSpaceTreeControlEventsImpl *create_nstc_events(void)
{
    INameSpaceTreeControlEventsImpl *This;
    This = HeapAlloc(GetProcessHeap(), 0, sizeof(INameSpaceTreeControlEventsImpl));
370
    This->INameSpaceTreeControlEvents_iface.lpVtbl = &vt_NSTCEvents;
371 372 373 374 375
    This->ref = 1;

    return This;
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/*********************************************************************
 * Event count checking
 */
static void ok_no_events_(INameSpaceTreeControlEventsImpl *impl,
                          const char *file, int line)
{
    UINT i;
    for(i = 0; i < LastEvent; i++)
    {
        ok_(file, line)
            (!impl->count[i], "Got event %d, count %d\n", i, impl->count[i]);
        impl->count[i] = 0;
    }
}
#define ok_no_events(impl)                      \
    ok_no_events_(impl, __FILE__, __LINE__)

#define ok_event_count_broken(impl, event, c, b)                        \
    do { ok(impl->count[event] == c || broken(impl->count[event] == b), \
            "Got event %d, count %d\n", event, impl->count[event]);     \
        impl->count[event] = 0;                                         \
    } while(0)

#define ok_event_count(impl, event, c)          \
    ok_event_count_broken(impl, event, c, -1)

#define ok_event_broken(impl, event)                                    \
    do { ok(impl->count[event] || broken(!impl->count[event]),          \
            "No event.\n");                                             \
        impl->count[event] = 0;                                         \
    } while(0)

#define ok_event(impl, event)                                           \
    do { ok(impl->count[event], "No event %d.\n", event);               \
        impl->count[event] = 0;                                         \
    } while(0)

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/* Process some messages */
static void process_msgs(void)
{
    MSG msg;
    BOOL got_msg;
    do {
        got_msg = FALSE;
        Sleep(100);
        while(PeekMessage( &msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            got_msg = TRUE;
        }
    } while(got_msg);

    /* There seem to be a timer that sometimes fires after about
       500ms, we need to wait for it. Failing to wait can result in
       seemingly sporadic selection change events. (Timer ID is 87,
       sending WM_TIMER manually does not seem to help us.) */
    Sleep(500);

    while(PeekMessage( &msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/** Some functions from shell32/tests/shlfolder.c */
/* creates a file with the specified name for tests */
static void CreateTestFile(const CHAR *name)
{
    HANDLE file;
    DWORD written;

    file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if (file != INVALID_HANDLE_VALUE)
    {
       WriteFile(file, name, strlen(name), &written, NULL);
       WriteFile(file, "\n", strlen("\n"), &written, NULL);
       CloseHandle(file);
    }
}
/* initializes the tests */
static void CreateFilesFolders(void)
{
    CreateDirectoryA(".\\testdir", NULL);
    CreateTestFile  (".\\testdir\\test1.txt ");
    CreateTestFile  (".\\testdir\\test2.txt ");
    CreateTestFile  (".\\testdir\\test3.txt ");
    CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
    CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
}

/* cleans after tests */
static void Cleanup(void)
{
    DeleteFileA(".\\testdir\\test1.txt");
    DeleteFileA(".\\testdir\\test2.txt");
    DeleteFileA(".\\testdir\\test3.txt");
    RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
    RemoveDirectoryA(".\\testdir\\testdir2");
    RemoveDirectoryA(".\\testdir");
}

/* Based on PathAddBackslashW from dlls/shlwapi/path.c */
static LPWSTR myPathAddBackslashW( LPWSTR lpszPath )
{
  size_t iLen;

  if (!lpszPath || (iLen = lstrlenW(lpszPath)) >= MAX_PATH)
    return NULL;

  if (iLen)
  {
    lpszPath += iLen;
    if (lpszPath[-1] != '\\')
    {
      *lpszPath++ = '\\';
      *lpszPath = '\0';
    }
  }
  return lpszPath;
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
static HWND get_treeview_hwnd(INameSpaceTreeControl *pnstc)
{
    IOleWindow *pow;
    HRESULT hr;
    HWND treeview = NULL;

    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        HWND host;
        hr = IOleWindow_GetWindow(pow, &host);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
            treeview = FindWindowExW(host, NULL, WC_TREEVIEWW, NULL);
        IOleWindow_Release(pow);
    }

    return treeview;
}

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
static LRESULT WINAPI treeview_subclass_proc(HWND hwnd_tv, UINT message, WPARAM wParam, LPARAM lParam)
{
    WNDPROC oldproc = (WNDPROC)GetWindowLongPtrW(hwnd_tv, GWLP_USERDATA);
    static LONG defwndproc_counter = 0;
    LRESULT ret;
    struct message msg;

    msg.message = message;
    msg.flags = sent|wparam|lparam;
    if (defwndproc_counter) msg.flags |= defwinproc;
    msg.wParam = wParam;
    msg.lParam = lParam;
    msg.id = 0;
    add_message(sequences, TREEVIEW_SEQ_INDEX, &msg);

    if(message == TVM_SETITEMW)
    {
        memcpy(&last_tvi, (void*)lParam, sizeof(TVITEMEXW));
        tvi_count++;
    }

    defwndproc_counter++;
    ret = CallWindowProcW(oldproc, hwnd_tv, message, wParam, lParam);
    defwndproc_counter--;
    return ret;
}

static BOOL subclass_treeview(INameSpaceTreeControl *pnstc)
{
    HWND hwnd_tv;
    WNDPROC oldproc = NULL;

    hwnd_tv = get_treeview_hwnd(pnstc);
    if(hwnd_tv)
    {
        oldproc = (WNDPROC)SetWindowLongPtrW(hwnd_tv, GWLP_WNDPROC,
                                             (LONG_PTR)treeview_subclass_proc);
        SetWindowLongPtrW(hwnd_tv, GWLP_USERDATA, (LONG_PTR)oldproc);
        ok(oldproc != NULL, "Failed to subclass.\n");
    }

    return oldproc?TRUE:FALSE;
}

static UINT get_msg_count(struct msg_sequence **seq, int sequence_index, UINT message)
{
    struct msg_sequence *msg_seq = seq[sequence_index];
    UINT i, count = 0;

    for(i = 0; i < msg_seq->count ; i++)
        if(msg_seq->sequence[i].message == message)
            count++;

    return count;
}

576 577 578 579
/* Returns FALSE if the NamespaceTreeControl failed to be instantiated. */
static BOOL test_initialization(void)
{
    INameSpaceTreeControl *pnstc;
580
    IOleWindow *pow;
581
    IUnknown *punk;
582
    HWND hwnd_host1;
583
    LONG lres;
584
    HRESULT hr;
585
    RECT rc;
586 587 588 589 590 591 592 593 594

    hr = CoCreateInstance(&CLSID_NamespaceTreeControl, NULL, CLSCTX_INPROC_SERVER,
                          &IID_INameSpaceTreeControl, (void**)&pnstc);
    ok(hr == S_OK || hr == REGDB_E_CLASSNOTREG, "Got 0x%08x\n", hr);
    if(FAILED(hr))
    {
        return FALSE;
    }

595 596 597 598 599 600 601 602 603 604 605 606 607
    hr = INameSpaceTreeControl_Initialize(pnstc, NULL, NULL, 0);
    ok(hr == HRESULT_FROM_WIN32(ERROR_TLW_WITH_WSCHILD), "Got (0x%08x)\n", hr);

    hr = INameSpaceTreeControl_Initialize(pnstc, (HWND)0xDEADBEEF, NULL, 0);
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), "Got (0x%08x)\n", hr);

    ZeroMemory(&rc, sizeof(RECT));
    hr = INameSpaceTreeControl_Initialize(pnstc, NULL, &rc, 0);
    ok(hr == HRESULT_FROM_WIN32(ERROR_TLW_WITH_WSCHILD), "Got (0x%08x)\n", hr);

    hr = INameSpaceTreeControl_Initialize(pnstc, (HWND)0xDEADBEEF, &rc, 0);
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), "Got (0x%08x)\n", hr);

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = IOleWindow_GetWindow(pow, &hwnd_host1);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
        ok(hwnd_host1 == NULL, "hwnd is not null.\n");

        hr = IOleWindow_ContextSensitiveHelp(pow, TRUE);
        ok(hr == E_NOTIMPL, "Got (0x%08x)\n", hr);
        hr = IOleWindow_ContextSensitiveHelp(pow, FALSE);
        ok(hr == E_NOTIMPL, "Got (0x%08x)\n", hr);
        IOleWindow_Release(pow);
    }

623 624
    hr = INameSpaceTreeControl_Initialize(pnstc, hwnd, NULL, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        static const CHAR namespacetree[] = "NamespaceTreeControl";
        char buf[1024];
        LONG style, expected_style;
        HWND hwnd_tv;
        hr = IOleWindow_GetWindow(pow, &hwnd_host1);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
        ok(hwnd_host1 != NULL, "hwnd_host1 is null.\n");
        buf[0] = '\0';
        GetClassNameA(hwnd_host1, buf, 1024);
        ok(!lstrcmpA(namespacetree, buf), "Class name was %s\n", buf);

        expected_style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        style = GetWindowLongPtrW(hwnd_host1, GWL_STYLE);
        ok(style == expected_style, "Got style %08x\n", style);

        expected_style = 0;
        style = GetWindowLongPtrW(hwnd_host1, GWL_EXSTYLE);
        ok(style == expected_style, "Got style %08x\n", style);

        expected_style = 0;
        style = SendMessageW(hwnd_host1, TVM_GETEXTENDEDSTYLE, 0, 0);
        ok(style == expected_style, "Got 0x%08x\n", style);

        hwnd_tv = FindWindowExW(hwnd_host1, NULL, WC_TREEVIEWW, NULL);
        ok(hwnd_tv != NULL, "Failed to get treeview hwnd.\n");
        if(hwnd_tv)
        {
            expected_style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
                WS_CLIPCHILDREN | WS_TABSTOP | TVS_NOHSCROLL |
                TVS_NONEVENHEIGHT | TVS_INFOTIP | TVS_TRACKSELECT | TVS_EDITLABELS;
            style = GetWindowLongPtrW(hwnd_tv, GWL_STYLE);
            ok(style == expected_style, "Got style %08x\n", style);

            expected_style = 0;
            style = GetWindowLongPtrW(hwnd_tv, GWL_EXSTYLE);
            ok(style == expected_style, "Got style %08x\n", style);

            expected_style = TVS_EX_NOSINGLECOLLAPSE | TVS_EX_DOUBLEBUFFER |
                TVS_EX_RICHTOOLTIP | TVS_EX_DRAWIMAGEASYNC;
            style = SendMessageW(hwnd_tv, TVM_GETEXTENDEDSTYLE, 0, 0);
            todo_wine ok(style == expected_style, "Got 0x%08x\n", style);
        }

        IOleWindow_Release(pow);
    }

    if(0)
    {
        /* The control can be initialized again without crashing, but
         * the reference counting will break. */
        hr = INameSpaceTreeControl_Initialize(pnstc, hwnd, &rc, 0);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
        hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
        if(SUCCEEDED(hr))
        {
            HWND hwnd_host2;
            hr = IOleWindow_GetWindow(pow, &hwnd_host2);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            ok(hwnd_host1 != hwnd_host2, "Same hwnd.\n");
            IOleWindow_Release(pow);
        }
    }
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

    /* Some "random" interfaces */
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceObject, (void**)&punk);
    ok(hr == E_NOINTERFACE || hr == S_OK /* vista, w2k8 */, "Got (0x%08x)\n", hr);
    if(SUCCEEDED(hr)) IUnknown_Release(punk);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceActiveObject, (void**)&punk);
    ok(hr == E_NOINTERFACE || hr == S_OK /* vista, w2k8 */, "Got (0x%08x)\n", hr);
    if(SUCCEEDED(hr)) IUnknown_Release(punk);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceObjectWindowless, (void**)&punk);
    ok(hr == E_NOINTERFACE || hr == S_OK /* vista, w2k8 */, "Got (0x%08x)\n", hr);
    if(SUCCEEDED(hr)) IUnknown_Release(punk);

    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceUIWindow, (void**)&punk);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceFrame, (void**)&punk);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceSite, (void**)&punk);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceSiteEx, (void**)&punk);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleInPlaceSiteWindowless, (void**)&punk);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);

    /* On windows, the reference count won't go to zero until the
     * window is destroyed. */
716 717 718 719 720
    INameSpaceTreeControl_AddRef(pnstc);
    lres = INameSpaceTreeControl_Release(pnstc);
    ok(lres > 1, "Reference count was (%d).\n", lres);

    DestroyWindow(hwnd_host1);
721
    lres = INameSpaceTreeControl_Release(pnstc);
722
    ok(!lres, "lres was %d\n", lres);
723 724 725 726

    return TRUE;
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
static void verify_root_order_(INameSpaceTreeControl *pnstc, IShellItem **roots,
                               const char *file, int line)
{
    HRESULT hr;
    IShellItemArray *psia;

    hr = INameSpaceTreeControl_GetRootItems(pnstc, &psia);
    ok_(file,line) (hr == S_OK, "GetRootItems: got (0x%08x)\n", hr);
    if(SUCCEEDED(hr))
    {
        DWORD i, expected, count = -1;
        hr = IShellItemArray_GetCount(psia, &count);
        ok_(file,line) (hr == S_OK, "Got (0x%08x)\n", hr);

        for(expected = 0; roots[expected] != NULL; expected++);
        ok_(file,line) (count == expected, "Got %d roots, expected %d\n", count, expected);

        for(i = 0; i < count && roots[i] != NULL; i++)
        {
            IShellItem *psi;
            hr = IShellItemArray_GetItemAt(psia, i, &psi);
            ok_(file,line) (hr == S_OK, "GetItemAt %i: got 0x%08x\n", i, hr);
            if(SUCCEEDED(hr))
            {
                int cmp;
                hr = IShellItem_Compare(psi, roots[i], SICHINT_DISPLAY, &cmp);
                ok_(file,line) (hr == S_OK, "Compare %i: got 0x%08x\n", i, hr);
                IShellItem_Release(psi);
            }
        }
        IShellItem_Release(psia);
    }
}
#define verify_root_order(pnstc, psi_a)         \
    verify_root_order_(pnstc, psi_a, __FILE__, __LINE__)

763 764 765 766
static void test_basics(void)
{
    INameSpaceTreeControl *pnstc;
    INameSpaceTreeControl2 *pnstc2;
767
    IShellItemArray *psia;
768
    IShellFolder *psfdesktop;
769
    IShellItem *psi;
770
    IShellItem *psidesktop, *psidesktop2;
771
    IShellItem *psitestdir, *psitestdir2, *psitest1;
772
    IOleWindow *pow;
773
    LPITEMIDLIST pidl_desktop;
774
    NSTCITEMSTATE istate;
775
    HRESULT hr;
776 777
    UINT i, res, height;
    HWND hwnd_tv;
778
    RECT rc;
779
    IShellItem *roots[10];
780
    POINT pt;
781
    int cbstate;
782 783 784 785 786
    WCHAR curdirW[MAX_PATH];
    WCHAR buf[MAX_PATH];
    static const WCHAR testdirW[] = {'t','e','s','t','d','i','r',0};
    static const WCHAR testdir2W[] =
        {'t','e','s','t','d','i','r','\\','t','e','s','t','d','i','r','2',0};
787 788
    static const WCHAR test1W[] =
        {'t','e','s','t','d','i','r','\\','t','e','s','t','1','.','t','x','t',0};
789 790
    static const WCHAR explorerW[] = {'E','x','p','l','o','r','e','r',0};
    static const WCHAR randomW[] = {'_','_','h','e','l','l','o',0};
791

792 793
    /* These should exist on platforms supporting the NSTC */
    ok(pSHCreateShellItem != NULL, "No SHCreateShellItem.\n");
794
    ok(pSHCreateItemFromParsingName != NULL, "No SHCreateItemFromParsingName\n");
795
    ok(pSHGetIDListFromObject != NULL, "No SHCreateShellItem.\n");
796
    ok(pSHCreateItemFromParsingName != NULL, "No SHCreateItemFromParsingName\n");
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

    /* Create ShellItems for testing. */
    SHGetDesktopFolder(&psfdesktop);
    hr = pSHGetIDListFromObject((IUnknown*)psfdesktop, &pidl_desktop);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = pSHCreateShellItem(NULL, NULL, pidl_desktop, &psidesktop);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            hr = pSHCreateShellItem(NULL, NULL, pidl_desktop, &psidesktop2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(FAILED(hr)) IShellItem_Release(psidesktop);
        }
        ILFree(pidl_desktop);
    }
    IShellFolder_Release(psfdesktop);

    if(FAILED(hr))
    {
        win_skip("Test setup failed.\n");
        return;
    }

822 823
    ok(psidesktop != psidesktop2, "psidesktop == psidesktop2\n");

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    CreateFilesFolders();
    GetCurrentDirectoryW(MAX_PATH, curdirW);
    ok(lstrlenW(curdirW), "Got 0 length string.\n");

    lstrcpyW(buf, curdirW);
    myPathAddBackslashW(buf);
    lstrcatW(buf, testdirW);
    hr = pSHCreateItemFromParsingName(buf, NULL, &IID_IShellItem, (void**)&psitestdir);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) goto cleanup;
    lstrcpyW(buf, curdirW);
    myPathAddBackslashW(buf);
    lstrcatW(buf, testdir2W);
    hr = pSHCreateItemFromParsingName(buf, NULL, &IID_IShellItem, (void**)&psitestdir2);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) goto cleanup;
840 841 842 843 844 845
    lstrcpyW(buf, curdirW);
    myPathAddBackslashW(buf);
    lstrcatW(buf, test1W);
    hr = pSHCreateItemFromParsingName(buf, NULL, &IID_IShellItem, (void**)&psitest1);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(FAILED(hr)) goto cleanup;
846

847 848 849 850
    hr = CoCreateInstance(&CLSID_NamespaceTreeControl, NULL, CLSCTX_INPROC_SERVER,
                          &IID_INameSpaceTreeControl, (void**)&pnstc);
    ok(hr == S_OK, "Failed to initialize control (0x%08x)\n", hr);

851
    /* Some tests on an uninitialized control */
852 853 854 855 856 857
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, psidesktop);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, NULL);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);
858 859 860 861
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_NONFOLDERS, 0, NULL);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    process_msgs();

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    /* Initialize the control */
    rc.top = rc.left = 0; rc.right = rc.bottom = 200;
    hr = INameSpaceTreeControl_Initialize(pnstc, hwnd, &rc, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);


    /* Set/GetControlStyle(2) */
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_INameSpaceTreeControl2, (void**)&pnstc2);
    ok(hr == S_OK || broken(hr == E_NOINTERFACE), "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        DWORD tmp;
        NSTCSTYLE style;
        NSTCSTYLE2 style2;
        static const NSTCSTYLE2 styles2[] =
            { NSTCS2_INTERRUPTNOTIFICATIONS,NSTCS2_SHOWNULLSPACEMENU,
              NSTCS2_DISPLAYPADDING,NSTCS2_DISPLAYPINNEDONLY,
              NTSCS2_NOSINGLETONAUTOEXPAND,NTSCS2_NEVERINSERTNONENUMERATED, 0};


        /* We can use this to differentiate between two versions of
         * this interface. Windows 7 returns hr == S_OK. */
        hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, 0, 0);
        ok(hr == S_OK || broken(hr == E_FAIL), "Got 0x%08x\n", hr);
        if(hr == S_OK)
        {
            static const NSTCSTYLE styles_setable[] =
                { NSTCS_HASEXPANDOS,NSTCS_HASLINES,NSTCS_SINGLECLICKEXPAND,
                  NSTCS_FULLROWSELECT,NSTCS_HORIZONTALSCROLL,
                  NSTCS_ROOTHASEXPANDO,NSTCS_SHOWSELECTIONALWAYS,NSTCS_NOINFOTIP,
                  NSTCS_EVENHEIGHT,NSTCS_NOREPLACEOPEN,NSTCS_DISABLEDRAGDROP,
                  NSTCS_NOORDERSTREAM,NSTCS_BORDER,NSTCS_NOEDITLABELS,
                  NSTCS_TABSTOP,NSTCS_FAVORITESMODE,NSTCS_EMPTYTEXT,NSTCS_CHECKBOXES,
                  NSTCS_ALLOWJUNCTIONS,NSTCS_SHOWTABSBUTTON,NSTCS_SHOWDELETEBUTTON,
                  NSTCS_SHOWREFRESHBUTTON, 0};
            static const NSTCSTYLE styles_nonsetable[] =
                { NSTCS_SPRINGEXPAND, NSTCS_RICHTOOLTIP, NSTCS_AUTOHSCROLL,
                  NSTCS_FADEINOUTEXPANDOS,
                  NSTCS_PARTIALCHECKBOXES, NSTCS_EXCLUSIONCHECKBOXES,
                  NSTCS_DIMMEDCHECKBOXES, NSTCS_NOINDENTCHECKS,0};

            /* Set/GetControlStyle */
            style = style2 = 0xdeadbeef;
            hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, 0xFFFFFFFF, &style);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style == 0, "Got style %x\n", style);

            hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, 0, 0xFFFFFFF);
            ok(hr == S_OK, "Got 0x%08x\n", hr);

            hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, 0xFFFFFFFF, 0);
            ok(hr == E_FAIL, "Got 0x%08x\n", hr);
            hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, 0xFFFFFFFF, 0xFFFFFFFF);
            ok(hr == E_FAIL, "Got 0x%08x\n", hr);

            tmp = 0;
            for(i = 0; styles_setable[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, styles_setable[i], styles_setable[i]);
                ok(hr == S_OK, "Got 0x%08x (%x)\n", hr, styles_setable[i]);
                if(SUCCEEDED(hr)) tmp |= styles_setable[i];
            }
            for(i = 0; styles_nonsetable[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, styles_nonsetable[i], styles_nonsetable[i]);
                ok(hr == E_FAIL, "Got 0x%08x (%x)\n", hr, styles_nonsetable[i]);
            }

            hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, 0xFFFFFFFF, &style);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style == tmp, "Got style %x (expected %x)\n", style, tmp);
            if(SUCCEEDED(hr))
            {
                DWORD tmp2;
                for(i = 0; styles_setable[i] != 0; i++)
                {
                    hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, styles_setable[i], &tmp2);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    ok(tmp2 == (style & styles_setable[i]), "Got %x\n", tmp2);
                }
            }

            for(i = 0; styles_setable[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, styles_setable[i], 0);
                ok(hr == S_OK, "Got 0x%08x (%x)\n", hr, styles_setable[i]);
            }
            for(i = 0; styles_nonsetable[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, styles_nonsetable[i], 0);
                ok(hr == E_FAIL, "Got 0x%08x (%x)\n", hr, styles_nonsetable[i]);
            }
            hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, 0xFFFFFFFF, &style);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style == 0, "Got style %x\n", style);

            /* Set/GetControlStyle2 */
            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFFFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == 0, "Got style %x\n", style2);

            hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, 0, 0);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, 0, 0xFFFFFFFF);
            ok(hr == S_OK, "Got 0x%08x\n", hr);

            hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, 0xFFFFFFFF, 0);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, 0xFFFFFFFF, 0xFFFFFFFF);
            ok(hr == S_OK, "Got 0x%08x\n", hr);

            hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, 0xFFFFFFFF, 0);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFFFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == 0x00000000, "Got style %x\n", style2);

            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == 0, "Got style %x\n", style2);

            tmp = 0;
            for(i = 0; styles2[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, styles2[i], styles2[i]);
                ok(hr == S_OK, "Got 0x%08x (%x)\n", hr, styles2[i]);
                if(SUCCEEDED(hr)) tmp |= styles2[i];
            }

            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == tmp, "Got style %x (expected %x)\n", style2, tmp);
            if(SUCCEEDED(hr))
            {
                DWORD tmp2;
                for(i = 0; styles2[i] != 0; i++)
                {
                    hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, styles2[i], &tmp2);
                    ok(hr == S_OK, "Got 0x%08x\n", hr);
                    ok(tmp2 == (style2 & styles2[i]), "Got %x\n", tmp2);
                }
            }

            for(i = 0; styles2[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, styles2[i], 0);
                ok(hr == S_OK, "Got 0x%08x (%x)\n", hr, styles2[i]);
            }
            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == 0, "Got style %x (expected 0)\n", style2);
        }
        else
        {
            /* 64-bit Windows Vista (others?) seems to have a somewhat
             * different idea of how the methods of this interface
             * should behave. */

            static const NSTCSTYLE styles[] =
                { NSTCS_HASEXPANDOS,NSTCS_HASLINES,NSTCS_SINGLECLICKEXPAND,
                  NSTCS_FULLROWSELECT,NSTCS_SPRINGEXPAND,NSTCS_HORIZONTALSCROLL,
                  NSTCS_RICHTOOLTIP, NSTCS_AUTOHSCROLL,
                  NSTCS_FADEINOUTEXPANDOS,
                  NSTCS_PARTIALCHECKBOXES,NSTCS_EXCLUSIONCHECKBOXES,
                  NSTCS_DIMMEDCHECKBOXES, NSTCS_NOINDENTCHECKS,
                  NSTCS_ROOTHASEXPANDO,NSTCS_SHOWSELECTIONALWAYS,NSTCS_NOINFOTIP,
                  NSTCS_EVENHEIGHT,NSTCS_NOREPLACEOPEN,NSTCS_DISABLEDRAGDROP,
                  NSTCS_NOORDERSTREAM,NSTCS_BORDER,NSTCS_NOEDITLABELS,
                  NSTCS_TABSTOP,NSTCS_FAVORITESMODE,NSTCS_EMPTYTEXT,NSTCS_CHECKBOXES,
                  NSTCS_ALLOWJUNCTIONS,NSTCS_SHOWTABSBUTTON,NSTCS_SHOWDELETEBUTTON,
                  NSTCS_SHOWREFRESHBUTTON, 0};
            trace("Detected broken INameSpaceTreeControl2.\n");

            style = 0xdeadbeef;
            hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, 0xFFFFFFFF, &style);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style == 0xdeadbeef, "Got style %x\n", style);

            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFFFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == 0, "Got style %x\n", style2);

            tmp = 0;
            for(i = 0; styles[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle(pnstc2, styles[i], styles[i]);
                ok(hr == E_FAIL || ((styles[i] & NSTCS_SPRINGEXPAND) && hr == S_OK),
                   "Got 0x%08x (%x)\n", hr, styles[i]);
                if(SUCCEEDED(hr)) tmp |= styles[i];
            }

            style = 0xdeadbeef;
            hr = INameSpaceTreeControl2_GetControlStyle(pnstc2, tmp, &style);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style == 0xdeadbeef, "Got style %x\n", style);

            tmp = 0;
            for(i = 0; styles2[i] != 0; i++)
            {
                hr = INameSpaceTreeControl2_SetControlStyle2(pnstc2, styles2[i], styles2[i]);
                ok(hr == S_OK, "Got 0x%08x (%x)\n", hr, styles2[i]);
                if(SUCCEEDED(hr)) tmp |= styles2[i];
            }

            style2 = 0xdeadbeef;
            hr = INameSpaceTreeControl2_GetControlStyle2(pnstc2, 0xFFFFFFFF, &style2);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(style2 == tmp, "Got style %x\n", style2);

        }

        INameSpaceTreeControl2_Release(pnstc2);
    }
    else
    {
        skip("INameSpaceTreeControl2 missing.\n");
    }

1080 1081 1082
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, NULL);
    ok(hr == E_NOINTERFACE, "Got (0x%08x)\n", hr);

1083 1084 1085 1086
    /* Append / Insert root */
    if(0)
    {
        /* Crashes under Windows 7 */
1087 1088
        INameSpaceTreeControl_AppendRoot(pnstc, NULL, SHCONTF_FOLDERS, 0, NULL);
        INameSpaceTreeControl_InsertRoot(pnstc, 0, NULL, SHCONTF_FOLDERS, 0, NULL);
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    }

    /* Note the usage of psidesktop and psidesktop2 */
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop2, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, psidesktop);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, psidesktop);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveRoot(pnstc, psidesktop);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

    hr = INameSpaceTreeControl_RemoveRoot(pnstc, psidesktop);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    hr = INameSpaceTreeControl_InsertRoot(pnstc, 0, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_InsertRoot(pnstc, -1, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_InsertRoot(pnstc, -1, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_InsertRoot(pnstc, 50, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_InsertRoot(pnstc, 1, psidesktop, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

1128 1129 1130
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

1131 1132 1133 1134
    /* GetRootItems */
    if(0)
    {
        /* Crashes on native. */
1135
        INameSpaceTreeControl_GetRootItems(pnstc, NULL);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    }

    hr = INameSpaceTreeControl_GetRootItems(pnstc, &psia);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop2, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir2, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[0] = psidesktop;
    roots[1] = psidesktop2;
    roots[2] = psitestdir;
    roots[3] = psitestdir2;
    roots[4] = NULL;
    verify_root_order(pnstc, roots);

    hr = INameSpaceTreeControl_InsertRoot(pnstc, 0, psitestdir2, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[0] = psitestdir2;
    roots[1] = psidesktop;
    roots[2] = psidesktop2;
    roots[3] = psitestdir;
    roots[4] = psitestdir2;
    roots[5] = NULL;
    verify_root_order(pnstc, roots);

    hr = INameSpaceTreeControl_InsertRoot(pnstc, 5, psidesktop, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[5] = psidesktop;
    roots[6] = NULL;
    verify_root_order(pnstc, roots);

    hr = INameSpaceTreeControl_InsertRoot(pnstc, 3, psitestdir2, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[3] = psitestdir2;
    roots[4] = psitestdir;
    roots[5] = psitestdir2;
    roots[6] = psidesktop;
    roots[7] = NULL;
    verify_root_order(pnstc, roots);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir2, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[7] = psitestdir2;
    roots[8] = NULL;
    verify_root_order(pnstc, roots);

    hr = INameSpaceTreeControl_InsertRoot(pnstc, -1, psidesktop, 0, 0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    roots[0] = psidesktop;
    roots[1] = psitestdir2;
    roots[2] = psidesktop;
    roots[3] = psidesktop2;
    roots[4] = psitestdir2;
    roots[5] = psitestdir;
    roots[6] = psitestdir2;
    roots[7] = psidesktop;
    roots[8] = psitestdir2;
    roots[9] = NULL;
    verify_root_order(pnstc, roots);

1207 1208 1209 1210
    /* CollapseAll */
    hr = INameSpaceTreeControl_CollapseAll(pnstc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

1211 1212 1213
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

1214 1215 1216
    hr = INameSpaceTreeControl_CollapseAll(pnstc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
    /* SetItemState message checks */
    res = subclass_treeview(pnstc);
    ok(res, "Failed to subclass treeview.\n");
    if(res)
    {
        UINT isMask, isFlags;

        hr = INameSpaceTreeControl_AppendRoot(
            pnstc, psidesktop, SHCONTF_NONFOLDERS | SHCONTF_FOLDERS, 0, NULL);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
        flush_sequences(sequences, NUM_MSG_SEQUENCES);

        /* A special case -
         *  The first expansion results in an "unrelated" TVM_SETITEMW being sent
         *  (mask == 0x50 (TVIF_CHILDREN|TVIF_HANDLE) )
         */
        tvi_count = 0;
        hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop,
                                                NSTCIS_EXPANDED, NSTCIS_EXPANDED);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        todo_wine
        {
            ok(tvi_count == 1, "Got %d\n", tvi_count);
            ok(last_tvi.mask == 0x50, "Got mask %x, expected 0x50\n", last_tvi.mask);
        }

        tvi_count = 0;
        hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop,
                                                NSTCIS_EXPANDED, NSTCIS_EXPANDED);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(tvi_count == 0, "Got %d\n", tvi_count);

        /* Test all the combinations of NSTCIS_SELECTED to NSTCIS_SELECTEDNOEXPAND */
        flush_sequences(sequences, NUM_MSG_SEQUENCES);
        for(isMask = 0; isMask <= 0x1f; isMask++)
        {
            for(isFlags = 0; isFlags <= 0x1f; isFlags++)
            {
                UINT select_sent, select_sent_vista, ensurev_sent, expand_sent;
                TVITEMEXW tviexp;
                UINT msg_count;

                flush_sequences(sequences, NUM_MSG_SEQUENCES);
                tvi_count = 0;

                hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, isMask, isFlags);
                ok(hr == S_OK, "(%x|%x)Got 0x%08x\n", isMask, isFlags, hr);

                /*****************************/
                /* Calculate expected values */
                /*****************************/

                /* Number of TVM_SELECTITEM/TVM_ENSUREVISIBLE and TVM_EXPAND sent */
                select_sent = ((isMask&isFlags) & NSTCIS_SELECTED)?1:0;
                select_sent_vista = ensurev_sent = select_sent;

                select_sent +=       ((isMask&isFlags) & NSTCIS_SELECTEDNOEXPAND)?1:0;
                select_sent_vista += ((isMask&isFlags) & NSTCIS_EXPANDED)?1:0;
                expand_sent =        ((isMask|isFlags) & NSTCIS_EXPANDED)?1:0;

                /* Possible TWM_SETITEMW message and its contents */
                if(isMask & NSTCIS_DISABLED)
                    tviexp.mask = TVIF_STATE | TVIF_STATEEX;
                else if( ((isMask^isFlags) & (NSTCIS_SELECTED|NSTCIS_EXPANDED|NSTCIS_SELECTEDNOEXPAND)) ||
                         ((isMask|isFlags) & NSTCIS_BOLD) || (isFlags & NSTCIS_DISABLED) )
                    tviexp.mask = TVIF_STATE;
                else
                    tviexp.mask = 0;

                if(tviexp.mask)
                {
                    tviexp.stateMask = tviexp.state = 0;
                    tviexp.stateMask |= ((isMask^isFlags)&NSTCIS_SELECTED) ? TVIS_SELECTED : 0;
                    tviexp.stateMask |= (isMask|isFlags)&NSTCIS_BOLD ? TVIS_BOLD:0;
                    tviexp.state     |= (isMask&isFlags)&NSTCIS_BOLD ? TVIS_BOLD:0;

                    if((isMask&NSTCIS_EXPANDED)^(isFlags&NSTCIS_EXPANDED))
                    {
                        tviexp.stateMask = 0;
                    }

                    tviexp.uStateEx = (isFlags&isMask)&NSTCIS_DISABLED?TVIS_EX_DISABLED:0;
                }
                else
                {
                    /* Make sure that no tests accidentally succeeded
                     * (and avoid a gcc warning) */
                    tviexp.stateMask = tviexp.state = tviexp.uStateEx = -1;
                }

                /*****************************/
                /*      Check the values.    */
                /*****************************/

                msg_count = get_msg_count(sequences, TREEVIEW_SEQ_INDEX, TVM_SELECTITEM);
                ok(msg_count == select_sent || broken(msg_count == select_sent_vista),
                   "(%x|%x) Got msg_count %d, expected %d (%d)\n",
                   isMask, isFlags, msg_count, select_sent, select_sent_vista);
                msg_count = get_msg_count(sequences, TREEVIEW_SEQ_INDEX, TVM_ENSUREVISIBLE);
                ok(msg_count == ensurev_sent || broken(msg_count == 0 /* Vista */),
                   "(%x|%x) Got msg_count %d, expected %d\n",
                   isMask, isFlags, msg_count, ensurev_sent);
                msg_count = get_msg_count(sequences, TREEVIEW_SEQ_INDEX, TVM_EXPAND);
                ok(msg_count == expand_sent, "(%x|%x) Got msg_count %d, expected %d\n",
                   isMask, isFlags, msg_count, expand_sent);

                msg_count = get_msg_count(sequences, TREEVIEW_SEQ_INDEX, TVM_SETITEMW);
                if(!tviexp.mask)
                {
                    /* Four special cases for vista */
                    BOOL vista_check = ( (isMask == 0x10 && isFlags == 0x10) ||
                                         (isMask == 0x11 && isFlags == 0x11) ||
                                         (isMask == 0x12 && isFlags == 0x12) ||
                                         (isMask == 0x13 && isFlags == 0x13) );

                    ok(msg_count == 0 || broken(msg_count == 1 && vista_check),
                       "(%x|%x) Got msg_count %d (tviexp.mask %x)\n",
                       isMask, isFlags, msg_count, tviexp.mask);
                }
                else
                {
                    ok(msg_count == 1, "(%x|%x) Got msg_count %d, expected 1\n",
                       isMask, isFlags, msg_count);
                    ok(last_tvi.mask == tviexp.mask,
                       "(%x|%x) Got mask %x, expected %x\n",
                       isMask, isFlags, last_tvi.mask, tviexp.mask);
                    ok(last_tvi.stateMask == tviexp.stateMask,
                       "(%x|%x) Got stateMask %x, expected %x\n",
                       isMask, isFlags, last_tvi.stateMask, tviexp.stateMask);
                    ok(last_tvi.state == tviexp.state,
                       "(%x|%x) Got state %x, expected %x\n",
                       isMask, isFlags,     last_tvi.state, tviexp.state);
                    ok(last_tvi.uStateEx == tviexp.uStateEx,
                       "(%x|%x) Got uStateEx %x, expected %x\n",
                       isMask, isFlags,   last_tvi.uStateEx, tviexp.uStateEx);
                }
            }
        }
        hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
    }

1359 1360 1361 1362
    /* GetSelectedItems */
    if(0)
    {
        /* Crashes under Windows 7 */
1363
        INameSpaceTreeControl_GetSelectedItems(pnstc, NULL);
1364 1365 1366 1367 1368
    }

    psia = (void*)0xdeadbeef;
    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    ok(hr == E_FAIL, "Got 0x%08x\n", hr);
1369
    ok(psia == (void*)0xdeadbeef, "Got %p\n", psia);
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir2, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir, SHCONTF_FOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_SetItemState(pnstc, psitestdir,
                                            NSTCIS_SELECTED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        UINT count;
        hr = IShellItemArray_GetCount(psia, &count);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(count == 1, "Got %d selected items.\n", count);
        if(count)
        {
            hr = IShellItemArray_GetItemAt(psia, 0, &psi);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                int cmp;
                hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
                ok(hr == S_OK, "Got 0x%08x\n", hr);
                IShellItem_Release(psi);
            }
        }
        IShellItemArray_Release(psia);
    }

    hr = INameSpaceTreeControl_SetItemState(pnstc, psitestdir2,
                                            NSTCIS_SELECTED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        UINT count;
        hr = IShellItemArray_GetCount(psia, &count);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(count == 1, "Got %d selected items.\n", count);
        if(count)
        {
            hr = IShellItemArray_GetItemAt(psia, 0, &psi);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                int cmp;
                hr = IShellItem_Compare(psi, psitestdir2, SICHINT_DISPLAY, &cmp);
                ok(hr == S_OK, "Got 0x%08x\n", hr);
                IShellItem_Release(psi);
            }
        }
        IShellItemArray_Release(psia);
    }

    hr = INameSpaceTreeControl_SetItemState(pnstc, psitest1,
                                            NSTCIS_SELECTED, NSTCIS_SELECTED);
    todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        UINT count;
        hr = IShellItemArray_GetCount(psia, &count);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(count == 1, "Got %d selected items.\n", count);
        if(count)
        {
            hr = IShellItemArray_GetItemAt(psia, 0, &psi);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            if(SUCCEEDED(hr))
            {
                int cmp;
                hr = IShellItem_Compare(psi, psitest1, SICHINT_DISPLAY, &cmp);
                todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
                IShellItem_Release(psi);
            }
        }
        IShellItemArray_Release(psia);
    }

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK || broken(hr == E_FAIL), "Got 0x%08x\n", hr);
    if(hr == E_FAIL)
    {
        /* For some reason, Vista fails to properly remove both the
         * roots here on the first try. */
        hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
    }

    /* Adding without NSTCRS_EXPANDED does not set the selection */
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          0, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_GetItemState(pnstc, psitestdir, 0xFF, &istate);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(!(istate & NSTCIS_SELECTED), "Got 0x%08x\n", istate);
    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    ok(hr == E_FAIL, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr)) IShellItemArray_Release(psia);

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    /* Adding with NSTCRS_EXPANDED sets the selection */
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          NSTCRS_EXPANDED, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_GetItemState(pnstc, psitestdir, 0xFF, &istate);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    todo_wine ok(istate & NSTCIS_SELECTED, "Got 0x%08x\n", istate);
    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        IShellItem *psi;

        hr = IShellItemArray_GetItemAt(psia, 0, &psi);
        if(SUCCEEDED(hr))
        {
            INT cmp;
            hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
            ok(hr == S_OK, "Got 0x%08x\n", hr);

            IShellItem_Release(psi);
        }

        IShellItemArray_Release(psia);
    }

    /* Adding a second root with NSTCRS_EXPANDED does not change the selection */
    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir2,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          NSTCRS_EXPANDED, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_GetItemState(pnstc, psitestdir2, 0xFF, &istate);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(!(istate & NSTCIS_SELECTED), "Got 0x%08x\n", istate);
    hr = INameSpaceTreeControl_GetSelectedItems(pnstc, &psia);
    todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        IShellItem *psi;

        hr = IShellItemArray_GetItemAt(psia, 0, &psi);
        if(SUCCEEDED(hr))
        {
            INT cmp;
            hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
            ok(hr == S_OK, "Got 0x%08x\n", hr);

            IShellItem_Release(psi);
        }

        IShellItemArray_Release(psia);
    }

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
1547 1548 1549 1550 1551 1552

    /* GetItemRect */
    rc.top = rc.left = rc.bottom = rc.right = 0;
    if(0)
    {
        /* Crashes under win 7 */
1553 1554 1555
        INameSpaceTreeControl_GetItemRect(pnstc, NULL, NULL);
        INameSpaceTreeControl_GetItemRect(pnstc, psitestdir, NULL);
        INameSpaceTreeControl_GetItemRect(pnstc, NULL, &rc);
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    }

    hr = INameSpaceTreeControl_GetItemRect(pnstc, psitestdir, &rc);
    ok(hr == E_INVALIDARG, "Got 0x%08x\n", hr);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          NSTCRS_EXPANDED, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_GetItemRect(pnstc, psitestdir, &rc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ok(rc.top != rc.bottom, "Got 0 height.\n");
    ok(rc.left != rc.bottom, "Got 0 width.\n");

    height = 0;
    hwnd_tv = get_treeview_hwnd(pnstc);
    if(hwnd_tv)
    {
        HTREEITEM hroot = (HTREEITEM)SendMessageW(hwnd_tv, TVM_GETNEXTITEM, TVGN_ROOT, 0);
        ok(hroot != NULL, "Failed to get root.\n");
        if(hroot)
        {
            RECT tv_rc;
            BOOL bret;

            *(HTREEITEM*)&tv_rc = hroot;
            bret = SendMessageW(hwnd_tv, TVM_GETITEMRECT, FALSE, (LPARAM)&tv_rc);
            ok(bret, "TVM_GETITEMRECT failed.\n");

            /* The NamespaceTreeControl returns screen coordinates. */
            MapWindowPoints(NULL, hwnd, (POINT*)&rc, 2);
            ok(rc.left == tv_rc.left, "Differed, got %d and %d\n", rc.left, tv_rc.left);
            ok(rc.top == tv_rc.top, "Differed, got %d and %d\n", rc.top, tv_rc.top);
            ok(rc.right == tv_rc.right, "Differed, got %d and %d\n", rc.right, tv_rc.right);
            ok(rc.bottom == tv_rc.bottom, "Differed, got %d and %d\n", rc.bottom, tv_rc.bottom);

            /* Save the height and compare to that of other items.
               Observed values: 18, 19, 21 */
            height = rc.bottom - rc.top;
            trace("height: %d\n", height);
        }
    }
    else
        win_skip("Skipping some GetItemRect tests.\n");

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614
    /*  HitTest */
    hr = INameSpaceTreeControl_HitTest(pnstc, NULL, NULL);
    ok(hr == E_POINTER, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_HitTest(pnstc, &pt, NULL);
    ok(hr == E_POINTER, "Got 0x%08x\n", hr);
    hr = INameSpaceTreeControl_HitTest(pnstc, NULL, &psi);
    ok(hr == E_POINTER, "Got 0x%08x\n", hr);

    psi = (void*)0xdeadbeef;
1615
    pt.x = pt.y = 0;
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
    hr = INameSpaceTreeControl_HitTest(pnstc, &pt, &psi);
    ok(hr == S_FALSE, "Got 0x%08x\n", hr);
    ok(psi == NULL, "Got psi %p\n", psi);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          NSTCRS_EXPANDED, NULL);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    process_msgs();

    hr = INameSpaceTreeControl_HitTest(pnstc, &pt, &psi);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        int cmp;
        hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(!cmp, "Got cmp %d\n", cmp);
        IShellItem_Release(psi);
    }

    pt.y += height - 1;
    hr = INameSpaceTreeControl_HitTest(pnstc, &pt, &psi);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        int cmp;
        hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(!cmp, "Got cmp %d\n", cmp);
        IShellItem_Release(psi);
    }

    pt.y += 1;
    hr = INameSpaceTreeControl_HitTest(pnstc, &pt, &psi);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        int cmp;
        todo_wine
        {
            hr = IShellItem_Compare(psi, psitestdir, SICHINT_DISPLAY, &cmp);
            ok(hr == S_FALSE, "Got 0x%08x\n", hr);
            ok(cmp, "no cmp value.\n");
            hr = IShellItem_Compare(psi, psitestdir2, SICHINT_DISPLAY, &cmp);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(!cmp, "Got cmp %d\n", cmp);
        }
        IShellItem_Release(psi);
    }

    hr = INameSpaceTreeControl_GetItemRect(pnstc, psitestdir2, &rc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        MapWindowPoints(NULL, hwnd, (POINT*)&rc, 2);
        pt.x = rc.left; pt.y = rc.top;

        hr = INameSpaceTreeControl_HitTest(pnstc, &pt, &psi);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        if(SUCCEEDED(hr))
        {
            int cmp;
            hr = IShellItem_Compare(psi, psitestdir2, SICHINT_DISPLAY, &cmp);
            ok(hr == S_OK, "Got 0x%08x\n", hr);
            ok(!cmp, "Got cmp %d\n", cmp);
            IShellItem_Release(psi);
        }
    }

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

1689 1690 1691 1692
    /* GetItemCustomState / SetItemCustomState */
    if(0)
    {
        /* Crashes under Windows 7 */
1693 1694 1695 1696
        INameSpaceTreeControl_GetItemCustomState(pnstc, NULL, NULL);
        INameSpaceTreeControl_GetItemCustomState(pnstc, NULL, &cbstate);
        INameSpaceTreeControl_GetItemCustomState(pnstc, psitestdir, NULL);
        INameSpaceTreeControl_SetItemCustomState(pnstc, NULL, 0);
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    }

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psitestdir,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
                                          0, NULL);
    process_msgs();
    ok(hr == S_OK, "Got 0x%08x\n", hr);

    todo_wine
    {
1707
        cbstate = -1;
1708 1709 1710 1711 1712 1713 1714 1715
        hr = INameSpaceTreeControl_GetItemCustomState(pnstc, psitestdir, &cbstate);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(cbstate == BST_UNCHECKED || broken(cbstate == BST_CHECKED /* Vista x64 */),
           "Got %d\n", cbstate);

        hr = INameSpaceTreeControl_SetItemCustomState(pnstc, psitestdir, BST_CHECKED);
        ok(hr == S_OK, "Got 0x%08x\n", hr);

1716
        cbstate = -1;
1717 1718 1719 1720 1721 1722 1723
        hr = INameSpaceTreeControl_GetItemCustomState(pnstc, psitestdir, &cbstate);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(cbstate == BST_CHECKED, "Got %d\n", cbstate);

        hr = INameSpaceTreeControl_SetItemCustomState(pnstc, psitestdir, 0xFFF);
        ok(hr == S_OK, "Got 0x%08x\n", hr);

1724
        cbstate = -1;
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
        hr = INameSpaceTreeControl_GetItemCustomState(pnstc, psitestdir, &cbstate);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        ok(cbstate == 0xF, "Got %d\n", cbstate);
    }

    /* SetTheme */
    todo_wine
    {
        hr = INameSpaceTreeControl_SetTheme(pnstc, NULL);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        hr = INameSpaceTreeControl_SetTheme(pnstc, explorerW);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        hr = INameSpaceTreeControl_SetTheme(pnstc, randomW);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
    }

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);

1744 1745
    IShellItem_Release(psidesktop);
    IShellItem_Release(psidesktop2);
1746 1747
    IShellItem_Release(psitestdir);
    IShellItem_Release(psitestdir2);
1748
    IShellItem_Release(psitest1);
1749

1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        HWND hwnd_nstc;
        hr = IOleWindow_GetWindow(pow, &hwnd_nstc);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        DestroyWindow(hwnd_nstc);
        IOleWindow_Release(pow);
    }

    res = INameSpaceTreeControl_Release(pnstc);
    ok(!res, "res was %d!\n", res);
1763 1764 1765

cleanup:
    Cleanup();
1766 1767
}

1768 1769 1770 1771 1772 1773 1774 1775 1776
static void test_events(void)
{
    INameSpaceTreeControl *pnstc;
    INameSpaceTreeControlEventsImpl *pnstceimpl, *pnstceimpl2;
    INameSpaceTreeControlEvents *pnstce, *pnstce2;
    IShellFolder *psfdesktop;
    IShellItem *psidesktop;
    IOleWindow *pow;
    LPITEMIDLIST pidl_desktop;
1777
    LPITEMIDLIST pidl_drives;
1778
    NSTCITEMSTATE itemstate;
1779
    IShellItem *psi;
1780
    DWORD cookie1, cookie2;
1781
    HWND hwnd_tv;
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
    HRESULT hr;
    UINT res;

    hr = CoCreateInstance(&CLSID_NamespaceTreeControl, NULL, CLSCTX_INPROC_SERVER,
                          &IID_INameSpaceTreeControl, (void**)&pnstc);
    ok(hr == S_OK, "Failed to initialize control (0x%08x)\n", hr);

    ok(pSHCreateShellItem != NULL, "No SHCreateShellItem.\n");
    ok(pSHGetIDListFromObject != NULL, "No SHCreateShellItem.\n");

    SHGetDesktopFolder(&psfdesktop);
    hr = pSHGetIDListFromObject((IUnknown*)psfdesktop, &pidl_desktop);
    IShellFolder_Release(psfdesktop);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    hr = pSHCreateShellItem(NULL, NULL, pidl_desktop, &psidesktop);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    ILFree(pidl_desktop);

    /* Create two instances of INameSpaceTreeControlEvents */
    pnstceimpl = create_nstc_events();
1802
    pnstce = &pnstceimpl->INameSpaceTreeControlEvents_iface;
1803
    ZeroMemory(&pnstceimpl->count, sizeof(UINT)*LastEvent);
1804
    pnstceimpl2 = create_nstc_events();
1805
    pnstce2 = &pnstceimpl2->INameSpaceTreeControlEvents_iface;
1806 1807 1808 1809

    if(0)
    {
        /* Crashes native */
1810 1811 1812
        INameSpaceTreeControl_TreeAdvise(pnstc, NULL, NULL);
        INameSpaceTreeControl_TreeAdvise(pnstc, NULL, &cookie1);
        INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce, NULL);
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
    }

    /* TreeAdvise in NameSpaceTreeController seems to support only one
     * client at the time.
     */

    /* First, respond with E_NOINTERFACE to all QI's */
    pnstceimpl->qi_enable_events = FALSE;
    pnstceimpl->qi_called_count = 0;
    cookie1 = 0xDEADBEEF;
    hr = INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce, &cookie1);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok(cookie1 == 0, "cookie now (0x%08x)\n", cookie1);
    todo_wine
    {
        ok(pnstceimpl->qi_called_count == 7 || pnstceimpl->qi_called_count == 4 /* Vista */,
           "QueryInterface called %d times.\n",
           pnstceimpl->qi_called_count);
    }
    ok(pnstceimpl->ref == 1, "refcount was %d\n", pnstceimpl->ref);

    /* Accept query for IID_INameSpaceTreeControlEvents */
    pnstceimpl->qi_enable_events = TRUE;
    pnstceimpl->qi_called_count = 0;
    cookie1 = 0xDEADBEEF;
    hr = INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce, &cookie1);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(cookie1 == 1, "cookie now (0x%08x)\n", cookie1);
    todo_wine
    {
        ok(pnstceimpl->qi_called_count == 7 || pnstceimpl->qi_called_count == 4 /* Vista */,
           "QueryInterface called %d times.\n",
           pnstceimpl->qi_called_count);
    }
    ok(pnstceimpl->ref == 2, "refcount was %d\n", pnstceimpl->ref);

    /* A second time, query interface will not be called at all. */
    pnstceimpl->qi_enable_events = TRUE;
    pnstceimpl->qi_called_count = 0;
    cookie2 = 0xDEADBEEF;
    hr = INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce, &cookie2);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok(cookie2 == 0, "cookie now (0x%08x)\n", cookie2);
    ok(!pnstceimpl->qi_called_count, "QueryInterface called %d times.\n",
       pnstceimpl->qi_called_count);
    ok(pnstceimpl->ref == 2, "refcount was %d\n", pnstceimpl->ref);

    /* Using another "instance" does not help. */
    pnstceimpl2->qi_enable_events = TRUE;
    pnstceimpl2->qi_called_count = 0;
    cookie2 = 0xDEADBEEF;
    hr = INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce2, &cookie2);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok(cookie2 == 0, "cookie now (0x%08x)\n", cookie2);
    ok(!pnstceimpl2->qi_called_count, "QueryInterface called %d times.\n",
       pnstceimpl2->qi_called_count);
    ok(pnstceimpl2->ref == 1, "refcount was %d\n", pnstceimpl->ref);

    /* Unadvise with bogus cookie (will actually unadvise properly) */
    pnstceimpl->qi_enable_events = TRUE;
    pnstceimpl->qi_called_count = 0;
    hr = INameSpaceTreeControl_TreeUnadvise(pnstc, 1234);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(!pnstceimpl->qi_called_count, "QueryInterface called %d times.\n",
       pnstceimpl->qi_called_count);
    ok(pnstceimpl->ref == 1, "refcount was %d\n", pnstceimpl->ref);

    /* Unadvise "properly" (will have no additional effect) */
    pnstceimpl->qi_enable_events = TRUE;
    pnstceimpl->qi_called_count = 0;
    hr = INameSpaceTreeControl_TreeUnadvise(pnstc, cookie1);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(!pnstceimpl->qi_called_count, "QueryInterface called %d times.\n",
       pnstceimpl->qi_called_count);
    ok(pnstceimpl->ref == 1, "refcount was %d\n", pnstceimpl->ref);

1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
    /* Advise again.. */
    pnstceimpl->qi_enable_events = 1;
    pnstceimpl->qi_called_count = 0;
    hr = INameSpaceTreeControl_TreeAdvise(pnstc, (IUnknown*)pnstce, &cookie2);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(cookie2 == 1, "Cookie is %d\n", cookie2);
    ok(cookie1 == cookie2, "Old cookie differs from old cookie.\n");
    todo_wine
    {
        ok(pnstceimpl->qi_called_count == 7 || pnstceimpl->qi_called_count == 4 /* Vista */,
           "QueryInterface called %d times.\n",
           pnstceimpl->qi_called_count);
    }
    ok(pnstceimpl->ref == 2, "refcount was %d\n", pnstceimpl->ref);

    /* Initialize the control */
    hr = INameSpaceTreeControl_Initialize(pnstc, hwnd, NULL, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
    ok_event_count(pnstceimpl, OnGetDefaultIconIndex, 0);
    ok_no_events(pnstceimpl);

    hwnd_tv = get_treeview_hwnd(pnstc);
    ok(hwnd_tv != NULL, "Failed to get hwnd_tv HWND.\n");
    if(hwnd_tv)
    {
1921
        HTREEITEM hroot, hitem;
1922 1923 1924 1925 1926 1927
        UINT i;
        static const UINT kbd_msgs_event[] = {
            WM_KEYDOWN, WM_KEYUP, WM_CHAR, WM_SYSKEYDOWN, WM_SYSKEYUP,
            WM_SYSCHAR, 0 };
        static const UINT kbd_msgs_noevent[] ={
            WM_DEADCHAR, WM_SYSDEADCHAR, WM_UNICHAR, 0 };
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943

        /* Test On*Expand */
        hroot = (HTREEITEM)SendMessageW(hwnd_tv, TVM_GETNEXTITEM, TVGN_ROOT, 0);
        SendMessage(hwnd_tv, TVM_EXPAND, TVE_EXPAND, (LPARAM)hroot);
        process_msgs();
        ok_event_count(pnstceimpl, OnBeforeExpand, 1);
        ok_event_count(pnstceimpl, OnAfterExpand, 1);
        ok_event_broken(pnstceimpl, OnItemAdded); /* No event on Vista */
        todo_wine ok_event_count(pnstceimpl, OnSelectionChanged, 1);
        ok_no_events(pnstceimpl);
        SendMessage(hwnd_tv, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)hroot);
        process_msgs();
        ok_no_events(pnstceimpl);
        SendMessage(hwnd_tv, TVM_EXPAND, TVE_EXPAND, (LPARAM)hroot);
        process_msgs();
        ok_no_events(pnstceimpl);
1944 1945 1946 1947 1948 1949 1950

        /* Test OnSelectionChanged */
        hitem = (HTREEITEM)SendMessageW(hwnd_tv, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hroot);
        SendMessageW(hwnd_tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hitem);
        process_msgs();
        ok_event_count(pnstceimpl, OnSelectionChanged, 1);
        ok_no_events(pnstceimpl);
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970

        /* Test OnKeyboardInput */
        for(i = 0; kbd_msgs_event[i] != 0; i++)
        {
            SendMessageW(hwnd_tv, kbd_msgs_event[i], 0x1234, 0x1234);
            ok(pnstceimpl->count[OnKeyboardInput] == 1,
               "%d (%x): Got count %d\n",
               kbd_msgs_event[i], kbd_msgs_event[i], pnstceimpl->count[OnKeyboardInput]);
            pnstceimpl->count[OnKeyboardInput] = 0;
        }

        for(i = 0; kbd_msgs_noevent[i] != 0; i++)
        {
            SendMessageW(hwnd_tv, kbd_msgs_noevent[i], 0x1234, 0x1234);
            ok(pnstceimpl->count[OnKeyboardInput] == 0,
               "%d (%x): Got count %d\n",
               kbd_msgs_noevent[i], kbd_msgs_noevent[i], pnstceimpl->count[OnKeyboardInput]);
            pnstceimpl->count[OnKeyboardInput] = 0;
        }
        ok_no_events(pnstceimpl);
1971 1972 1973 1974 1975 1976 1977
    }
    else
        skip("Skipping some tests.\n");

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    process_msgs();
    ok(hr == S_OK, "Got 0x%08x\n", hr);
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
    ok_event(pnstceimpl, OnItemDeleted);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_NONE, "itemstate is 0x%08x\n", itemstate);
    process_msgs();
    ok_no_events(pnstceimpl);

    /* Expand the root */
    itemstate |= NSTCIS_EXPANDED;
    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count(pnstceimpl, OnBeforeExpand, 1);
    ok_event_broken(pnstceimpl, OnItemAdded); /* Does not fire on Vista */
    ok_event_count(pnstceimpl, OnAfterExpand, 1);
    todo_wine
    {
        ok_event_count_broken(pnstceimpl, OnSelectionChanged, 1, 0 /* Vista*/);
    }
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate & NSTCIS_EXPANDED, "Item not expanded.\n");
    todo_wine
    {
        ok(itemstate == (NSTCIS_SELECTED | NSTCIS_EXPANDED)||
           broken(itemstate == NSTCIS_EXPANDED) /* Vista */,
           "itemstate is 0x%08x\n", itemstate);
        process_msgs();
        ok_event_count_broken(pnstceimpl, OnSelectionChanged, 1, 0 /* Vista*/);
    }
    ok_no_events(pnstceimpl);

    /* Deselect the root */
    itemstate &= ~NSTCIS_SELECTED;
    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == (NSTCIS_EXPANDED), "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_CollapseAll(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    /* Delete all roots */
    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_event_count(pnstceimpl, OnItemDeleted, 1);
    ok_no_events(pnstceimpl);

    /* Get/SetItemState */
    if(0)
    {
        /* Crashes on Windows 7 */
2047 2048 2049 2050 2051 2052
        INameSpaceTreeControl_SetItemState(pnstc, NULL, 0, 0);
        INameSpaceTreeControl_GetItemState(pnstc, NULL, 0, NULL);
        INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0, NULL);
        INameSpaceTreeControl_GetItemState(pnstc, NULL, 0, &itemstate);
        INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0, NULL);
        INameSpaceTreeControl_GetItemState(pnstc, NULL, 0, &itemstate);
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
    }

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);
    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, 0);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop,
                                          SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_NONE, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0, 0xffff);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    todo_wine
    {
        ok_event_count(pnstceimpl, OnBeforeExpand, 0);
        ok_event_count(pnstceimpl, OnAfterExpand, 0);
        ok_event_count(pnstceimpl, OnItemAdded, 0);
    }
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    todo_wine
        ok(itemstate == NSTCIS_NONE, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_NONE, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count(pnstceimpl, OnSelectionChanged, 1);
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_SELECTED, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, NSTCIS_EXPANDED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_SELECTED, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_SELECTED, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, NSTCIS_SELECTEDNOEXPAND);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_SELECTED, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, NSTCIS_EXPANDED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    todo_wine
    {
        ok_event_count(pnstceimpl, OnBeforeExpand, 1);
        ok_event_broken(pnstceimpl, OnItemAdded); /* Does not fire on Vista */
        ok_event_count(pnstceimpl, OnAfterExpand, 1);
    }
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_EXPANDED, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == NSTCIS_NONE, "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff, 0xffff);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    todo_wine
    {
        ok(itemstate == (NSTCIS_EXPANDED | NSTCIS_BOLD | NSTCIS_DISABLED),
           "itemstate is 0x%08x\n", itemstate);
    }
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, NSTCIS_SELECTED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    todo_wine
    {
        ok(itemstate == (NSTCIS_EXPANDED | NSTCIS_BOLD | NSTCIS_DISABLED),
           "itemstate is 0x%08x\n", itemstate);
    }
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop,
                                            NSTCIS_SELECTED | NSTCIS_DISABLED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == (NSTCIS_BOLD | NSTCIS_EXPANDED),
       "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, NSTCIS_SELECTED, NSTCIS_SELECTED);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == (NSTCIS_BOLD | NSTCIS_EXPANDED),
       "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, 0xffff & ~NSTCIS_DISABLED, 0);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_no_events(pnstceimpl);

    itemstate = 0xDEADBEEF;
    hr = INameSpaceTreeControl_GetItemState(pnstc, psidesktop, 0xffff, &itemstate);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok(itemstate == (NSTCIS_BOLD), "itemstate is 0x%08x\n", itemstate);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_event_count(pnstceimpl, OnItemDeleted, 1);
    ok_no_events(pnstceimpl);
2247

2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
    /* GetNextItem */
    hr = INameSpaceTreeControl_GetNextItem(pnstc, NULL, 0, NULL);
    ok(hr == E_POINTER, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetNextItem(pnstc, psidesktop, 0, NULL);
    ok(hr == E_POINTER, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetNextItem(pnstc, NULL, 0, &psi);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_GetNextItem(pnstc, psidesktop, 0, &psi);
    ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
    ok_no_events(pnstceimpl);

    /* Get child from unexpanded and unfilled parent */
    psi = (void*)0xDEADBEEF;
    hr = INameSpaceTreeControl_GetNextItem(pnstc, psidesktop, NSTCGNI_CHILD, &psi);
    ok(hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok(psi == NULL, "psi is %p\n", psi);
    process_msgs();
    ok_no_events(pnstceimpl);

    /* Expand and try again */
    hr = INameSpaceTreeControl_SetItemState(pnstc, psidesktop, NSTCIS_EXPANDED, 0xffff);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    process_msgs();
    ok_event_count(pnstceimpl, OnBeforeExpand, 1);
    ok_event_broken(pnstceimpl, OnItemAdded); /* Does not fire on Vista */
    ok_event_count(pnstceimpl, OnAfterExpand, 1);
    todo_wine ok_event_count_broken(pnstceimpl, OnSelectionChanged, 1, 0 /*Vista */);
    ok_no_events(pnstceimpl);
    psi = (void*)0xDEADBEEF;
    hr = INameSpaceTreeControl_GetNextItem(pnstc, psidesktop, NSTCGNI_CHILD, &psi);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok((psi != NULL) && (psi != (void*)0xDEADBEEF), "psi is %p\n", psi);
    process_msgs();
    ok_no_events(pnstceimpl);
    if(SUCCEEDED(hr)) IShellItem_Release(psi);

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_event_count(pnstceimpl, OnItemDeleted, 1);
    ok_no_events(pnstceimpl);

2301 2302 2303 2304
    /* EnsureItemVisible */
    if(0)
    {
        /* Crashes on Windows 7 */
2305
        INameSpaceTreeControl_EnsureItemVisible(pnstc, NULL);
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
    }

    hr = INameSpaceTreeControl_EnsureItemVisible(pnstc, psidesktop);
    ok(hr == E_INVALIDARG || hr == E_FAIL, "Got (0x%08x)\n", hr);
    ok_no_events(pnstceimpl);

    hr = pSHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidl_drives);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    if(SUCCEEDED(hr))
    {
        hr = pSHCreateShellItem(NULL, NULL, pidl_drives, &psi);
        ok(hr == S_OK, "Got (0x%08x)\n", hr);
        if(SUCCEEDED(hr))
        {
            hr = INameSpaceTreeControl_AppendRoot(pnstc, psi, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            process_msgs();
            ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
            ok_no_events(pnstceimpl);

            hr = INameSpaceTreeControl_EnsureItemVisible(pnstc, psidesktop);
            ok(hr == E_INVALIDARG, "Got (0x%08x)\n", hr);
            ok_no_events(pnstceimpl);

            hr = INameSpaceTreeControl_EnsureItemVisible(pnstc, psi);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            ok_no_events(pnstceimpl);

            hr = INameSpaceTreeControl_AppendRoot(pnstc, psidesktop, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, 0, NULL);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            process_msgs();
            ok_event_count_broken(pnstceimpl, OnItemAdded, 1, 0 /* Vista */);
            ok_no_events(pnstceimpl);

            hr = INameSpaceTreeControl_EnsureItemVisible(pnstc, psidesktop);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            ok_no_events(pnstceimpl);

            hr = INameSpaceTreeControl_EnsureItemVisible(pnstc, psi);
            ok(hr == S_OK, "Got (0x%08x)\n", hr);
            ok_no_events(pnstceimpl);

        }
        else
            skip("Failed to create shellitem.\n");

        ILFree(pidl_drives);
    }
    else
        skip("Failed to get pidl for CSIDL_DRIVES.\n");

    hr = INameSpaceTreeControl_RemoveAllRoots(pnstc);
    ok(hr == S_OK, "Got (0x%08x)\n", hr);
    ok_event_count(pnstceimpl, OnItemDeleted, 2);
    ok_no_events(pnstceimpl);

2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
    hr = INameSpaceTreeControl_QueryInterface(pnstc, &IID_IOleWindow, (void**)&pow);
    ok(hr == S_OK, "Got 0x%08x\n", hr);
    if(SUCCEEDED(hr))
    {
        HWND hwnd_nstc;
        hr = IOleWindow_GetWindow(pow, &hwnd_nstc);
        ok(hr == S_OK, "Got 0x%08x\n", hr);
        DestroyWindow(hwnd_nstc);
        IOleWindow_Release(pow);
    }

2373 2374 2375
    hr = INameSpaceTreeControl_TreeUnadvise(pnstc, cookie2);
    ok(hr == S_OK, "Got 0x%08x\n", hr);

2376 2377 2378 2379 2380 2381 2382 2383 2384
    res = INameSpaceTreeControl_Release(pnstc);
    ok(!res, "res was %d!\n", res);

    if(!res)
    {
        /* Freeing these prematurely causes a crash. */
        HeapFree(GetProcessHeap(), 0, pnstceimpl);
        HeapFree(GetProcessHeap(), 0, pnstceimpl2);
    }
2385 2386 2387

    IShellItem_Release(psi);
    IShellItem_Release(psidesktop);
2388 2389
}

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
static void setup_window(void)
{
    WNDCLASSA wc;
    static const char nstctest_wnd_name[] = "nstctest_wnd";

    ZeroMemory(&wc, sizeof(WNDCLASSA));
    wc.lpfnWndProc      = DefWindowProcA;
    wc.lpszClassName    = nstctest_wnd_name;
    RegisterClassA(&wc);
    hwnd = CreateWindowA(nstctest_wnd_name, NULL, WS_TABSTOP,
                         0, 0, 200, 200, NULL, 0, 0, NULL);
    ok(hwnd != NULL, "Failed to create window for test (lasterror: %d).\n",
       GetLastError());
}

static void destroy_window(void)
{
    DestroyWindow(hwnd);
}

START_TEST(nstc)
{
    OleInitialize(NULL);
    setup_window();
2414
    init_function_pointers();
2415
    init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
2416

2417 2418 2419
    if(test_initialization())
    {
        test_basics();
2420
        test_events();
2421 2422 2423 2424 2425
    }
    else
    {
        win_skip("No NamespaceTreeControl (or instantiation failed).\n");
    }
2426 2427 2428 2429

    destroy_window();
    OleUninitialize();
}