[SHELL32_APITEST] Make SHChangeNotify testcase more stable (#6796)

Make the testcase more stable and faster. Reduce code.
JIRA issue: CORE-13950
- Use WM_COPYDATA message to communicate with shell32_apitest_sub.exe.
- Use concept of "stage" and "step".
- Do multiple checks in one step.
- Flush notification by using (SHCNF_FLUSH | SHCNF_FLUSHNOWAIT) flags.
This commit is contained in:
Katayama Hirofumi MZ 2024-04-24 09:10:29 +09:00 committed by GitHub
parent 6ac260dcec
commit 25e2f5f277
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 764 additions and 1115 deletions

File diff suppressed because it is too large Load diff

View file

@ -747,6 +747,6 @@ START_TEST(ShellExecCmdLine)
ok(DeleteFileA("Test File 2.bat"), "failed to delete the test file\n");
free(s_wi0.phwnd);
DoWaitForWindow(CLASSNAME, CLASSNAME, TRUE, TRUE);
DoWaitForWindow(SUB_CLASSNAME, SUB_CLASSNAME, TRUE, TRUE);
Sleep(100);
}

View file

@ -2,150 +2,239 @@
* PROJECT: ReactOS api tests
* LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
* PURPOSE: Test for SHChangeNotify
* COPYRIGHT: Copyright 2020 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
* COPYRIGHT: Copyright 2020-2024 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
*/
// This program is used in SHChangeNotify and ShellExecCmdLine testcases.
#include "shelltest.h"
#include "shell32_apitest_sub.h"
#include <assert.h>
static HWND s_hwnd = NULL;
typedef enum DIRTYPE
{
DIRTYPE_DESKTOP = 0,
DIRTYPE_DRIVES,
DIRTYPE_DIR1,
DIRTYPE_MAX
} DIRTYPE;
static HWND s_hMainWnd = NULL, s_hSubWnd = NULL;
static LPITEMIDLIST s_pidl[DIRTYPE_MAX];
static UINT s_uRegID = 0;
static BOOL s_fRecursive = FALSE;
static DIRTYPE s_iWatchDir = DIRTYPE_NULL;
static INT s_nSources = 0;
static LPITEMIDLIST s_pidl = NULL;
static WCHAR s_path1[MAX_PATH], s_path2[MAX_PATH];
static BYTE s_counters[TYPE_MAX + 1];
static HANDLE s_hEvent = NULL;
static INT s_iStage = -1;
static BOOL
OnCreate(HWND hwnd)
#define NUM_STAGE 4
#define EVENTS (SHCNE_CREATE | SHCNE_DELETE | SHCNE_MKDIR | SHCNE_RMDIR | \
SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEDIR | SHCNE_UPDATEITEM)
inline LPITEMIDLIST DoGetPidl(INT iDir)
{
s_hwnd = hwnd;
s_pidl = DoGetPidl(s_iWatchDir);
LPITEMIDLIST ret = NULL;
SHChangeNotifyEntry entry;
entry.pidl = s_pidl;
entry.fRecursive = s_fRecursive;
s_uRegID = SHChangeNotifyRegister(hwnd, s_nSources, SHCNE_ALLEVENTS, WM_SHELL_NOTIFY, 1, &entry);
return s_uRegID != 0;
}
static void
OnCommand(HWND hwnd, UINT id)
{
switch (id)
switch (iDir)
{
case IDOK:
case IDCANCEL:
DestroyWindow(hwnd);
case DIRTYPE_DESKTOP:
{
SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &ret);
break;
}
}
static void
OnDestroy(HWND hwnd)
{
SHChangeNotifyDeregister(s_uRegID);
s_uRegID = 0;
CoTaskMemFree(s_pidl);
s_pidl = NULL;
PostQuitMessage(0);
s_hwnd = NULL;
}
static BOOL DoPathes(PIDLIST_ABSOLUTE pidl1, PIDLIST_ABSOLUTE pidl2)
{
WCHAR path[MAX_PATH];
if (!SHGetPathFromIDListW(pidl1, path))
{
s_path1[0] = s_path2[0] = 0;
return FALSE;
}
case DIRTYPE_DRIVES:
{
SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &ret);
break;
}
case DIRTYPE_DIR1:
{
WCHAR szPath1[MAX_PATH];
SHGetSpecialFolderPathW(NULL, szPath1, CSIDL_PERSONAL, FALSE); // My Documents
PathAppendW(szPath1, L"_TESTDIR_1_");
ret = ILCreateFromPathW(szPath1);
break;
}
default:
{
assert(0);
break;
}
}
if (wcsstr(path, L"Recent") != NULL)
return FALSE;
return ret;
}
StringCchCopyW(s_path1, _countof(s_path1), path);
static BOOL OnCreate(HWND hwnd)
{
s_hSubWnd = hwnd;
if (!SHGetPathFromIDListW(pidl2, s_path2))
s_path2[0] = 0;
for (INT i = 0; i < DIRTYPE_MAX; ++i)
s_pidl[i] = DoGetPidl(i);
return TRUE;
}
static VOID DoPathesAndFlags(UINT type, PIDLIST_ABSOLUTE pidl1, PIDLIST_ABSOLUTE pidl2)
static BOOL InitSHCN(HWND hwnd)
{
if (DoPathes(pidl1, pidl2))
{
s_counters[type] = 1;
SetEvent(s_hEvent);
}
}
assert(0 <= s_iStage);
assert(s_iStage < NUM_STAGE);
static void
DoShellNotify(HWND hwnd, PIDLIST_ABSOLUTE pidl1, PIDLIST_ABSOLUTE pidl2, LONG lEvent)
{
switch (lEvent)
SHChangeNotifyEntry entry;
INT sources;
LONG events;
switch (s_iStage)
{
case SHCNE_RENAMEITEM:
DoPathesAndFlags(TYPE_RENAMEITEM, pidl1, pidl2);
case 0:
{
entry.fRecursive = TRUE;
entry.pidl = s_pidl[DIRTYPE_DESKTOP];
sources = SHCNRF_NewDelivery | SHCNRF_ShellLevel;
events = EVENTS;
break;
case SHCNE_CREATE:
DoPathesAndFlags(TYPE_CREATE, pidl1, pidl2);
}
case 1:
{
entry.fRecursive = TRUE;
entry.pidl = s_pidl[DIRTYPE_DRIVES];
sources = SHCNRF_NewDelivery | SHCNRF_ShellLevel;
events = EVENTS;
break;
case SHCNE_DELETE:
DoPathesAndFlags(TYPE_DELETE, pidl1, pidl2);
}
case 2:
{
entry.fRecursive = FALSE;
entry.pidl = s_pidl[DIRTYPE_DIR1];
sources = SHCNRF_NewDelivery | SHCNRF_ShellLevel;
events = EVENTS;
break;
case SHCNE_MKDIR:
DoPathesAndFlags(TYPE_MKDIR, pidl1, pidl2);
break;
case SHCNE_RMDIR:
DoPathesAndFlags(TYPE_RMDIR, pidl1, pidl2);
break;
case SHCNE_MEDIAINSERTED:
break;
case SHCNE_MEDIAREMOVED:
break;
case SHCNE_DRIVEREMOVED:
break;
case SHCNE_DRIVEADD:
break;
case SHCNE_NETSHARE:
break;
case SHCNE_NETUNSHARE:
break;
case SHCNE_ATTRIBUTES:
break;
case SHCNE_UPDATEDIR:
DoPathesAndFlags(TYPE_UPDATEDIR, pidl1, pidl2);
break;
case SHCNE_UPDATEITEM:
break;
case SHCNE_SERVERDISCONNECT:
break;
case SHCNE_UPDATEIMAGE:
break;
case SHCNE_DRIVEADDGUI:
break;
case SHCNE_RENAMEFOLDER:
DoPathesAndFlags(TYPE_RENAMEFOLDER, pidl1, pidl2);
break;
case SHCNE_FREESPACE:
break;
case SHCNE_EXTENDED_EVENT:
break;
case SHCNE_ASSOCCHANGED:
}
case 3:
{
entry.fRecursive = TRUE;
entry.pidl = s_pidl[DIRTYPE_DIR1];
sources = SHCNRF_NewDelivery | SHCNRF_ShellLevel | SHCNRF_InterruptLevel |
SHCNRF_RecursiveInterrupt;
events = EVENTS;
break;
}
default:
{
assert(0);
break;
}
}
s_uRegID = SHChangeNotifyRegister(hwnd, sources, events, WM_SHELL_NOTIFY, 1, &entry);
if (s_uRegID == 0)
return FALSE;
return TRUE;
}
static void UnInitSHCN(HWND hwnd)
{
if (s_uRegID)
{
SHChangeNotifyDeregister(s_uRegID);
s_uRegID = 0;
}
}
static INT_PTR
OnShellNotify(HWND hwnd, WPARAM wParam, LPARAM lParam)
static void OnCommand(HWND hwnd, UINT id)
{
switch (id)
{
case IDYES: // Start testing
{
s_hMainWnd = ::FindWindow(MAIN_CLASSNAME, MAIN_CLASSNAME);
if (!s_hMainWnd)
{
::DestroyWindow(hwnd);
break;
}
s_iStage = 0;
InitSHCN(hwnd);
::PostMessageW(s_hMainWnd, WM_COMMAND, IDYES, 0);
break;
}
case IDRETRY: // New stage
{
UnInitSHCN(hwnd);
++s_iStage;
InitSHCN(hwnd);
::PostMessageW(s_hMainWnd, WM_COMMAND, IDRETRY, 0);
break;
}
case IDNO: // Quit
{
s_iStage = -1;
UnInitSHCN(hwnd);
::DestroyWindow(hwnd);
break;
}
}
}
static void OnDestroy(HWND hwnd)
{
UnInitSHCN(hwnd);
for (auto& pidl : s_pidl)
{
CoTaskMemFree(pidl);
pidl = NULL;
}
::PostMessageW(s_hMainWnd, WM_COMMAND, IDNO, 0);
PostQuitMessage(0);
}
static BOOL DoSendData(LONG lEvent, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
{
DWORD cbPidl1 = ILGetSize(pidl1), cbPidl2 = ILGetSize(pidl2);
DWORD cbTotal = sizeof(lEvent) + sizeof(cbPidl1) + sizeof(cbPidl2) + cbPidl1 + cbPidl2;
LPBYTE pbData = (LPBYTE)::LocalAlloc(LPTR, cbTotal);
if (!pbData)
return FALSE;
LPBYTE pb = pbData;
*(LONG*)pb = lEvent;
pb += sizeof(lEvent);
*(DWORD*)pb = cbPidl1;
pb += sizeof(cbPidl1);
*(DWORD*)pb = cbPidl2;
pb += sizeof(cbPidl2);
CopyMemory(pb, pidl1, cbPidl1);
pb += cbPidl1;
CopyMemory(pb, pidl2, cbPidl2);
pb += cbPidl2;
assert(INT(pb - pbData) == INT(cbTotal));
COPYDATASTRUCT CopyData;
CopyData.dwData = 0xBEEFCAFE;
CopyData.cbData = cbTotal;
CopyData.lpData = pbData;
BOOL ret = (BOOL)::SendMessageW(s_hMainWnd, WM_COPYDATA, (WPARAM)s_hSubWnd, (LPARAM)&CopyData);
::LocalFree(pbData);
return ret;
}
static void DoShellNotify(HWND hwnd, PIDLIST_ABSOLUTE pidl1, PIDLIST_ABSOLUTE pidl2, LONG lEvent)
{
if (s_iStage < 0)
return;
DoSendData(lEvent, pidl1, pidl2);
}
static INT_PTR OnShellNotify(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
LONG lEvent;
PIDLIST_ABSOLUTE *pidlAbsolute;
@ -163,40 +252,7 @@ OnShellNotify(HWND hwnd, WPARAM wParam, LPARAM lParam)
return TRUE;
}
static LRESULT
OnGetNotifyFlags(HWND hwnd)
{
if (s_uRegID == 0)
return 0xFFFFFFFF;
DWORD dwFlags = 0;
for (size_t i = 0; i < _countof(s_counters); ++i)
{
if (s_counters[i])
dwFlags |= (1 << i);
}
return dwFlags;
}
static void
DoSetPaths(HWND hwnd)
{
WCHAR szText[MAX_PATH * 2];
StringCchCopyW(szText, _countof(szText), s_path1);
StringCchCatW(szText, _countof(szText), L"|");
StringCchCatW(szText, _countof(szText), s_path2);
FILE *fp = _wfopen(TEMP_FILE, L"wb");
if (fp)
{
fwrite(szText, (wcslen(szText) + 1) * sizeof(WCHAR), 1, fp);
fflush(fp);
fclose(fp);
}
}
static LRESULT CALLBACK
WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
static LRESULT CALLBACK SubWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
@ -214,73 +270,36 @@ WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
OnDestroy(hwnd);
break;
case WM_GET_NOTIFY_FLAGS:
return OnGetNotifyFlags(hwnd);
case WM_CLEAR_FLAGS:
ZeroMemory(&s_counters, sizeof(s_counters));
s_path1[0] = s_path2[0] = 0;
break;
case WM_SET_PATHS:
DoSetPaths(hwnd);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
return ::DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
static BOOL ParseCommandLine(LPWSTR lpCmdLine)
{
LPWSTR pch = lpCmdLine; // fRecursive,iWatchDir,nSources
s_fRecursive = !!wcstoul(pch, NULL, 0);
pch = wcschr(pch, L',');
if (!pch)
return FALSE;
++pch;
s_iWatchDir = (DIRTYPE)wcstoul(pch, NULL, 0);
pch = wcschr(pch, L',');
if (!pch)
return FALSE;
++pch;
s_nSources = wcstoul(pch, NULL, 0);
return TRUE;
}
INT APIENTRY
wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
INT nCmdShow)
wWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
INT nCmdShow)
{
if (lstrcmpiW(lpCmdLine, L"") == 0 || lstrcmpiW(lpCmdLine, L"TEST") == 0)
return 0;
if (!ParseCommandLine(lpCmdLine))
return -1;
s_hEvent = OpenEventW(EVENT_ALL_ACCESS, TRUE, EVENT_NAME);
WNDCLASSW wc;
ZeroMemory(&wc, sizeof(wc));
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandleW(NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
WNDCLASSW wc = { 0, SubWindowProc };
wc.hInstance = hInstance;
wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
wc.lpszClassName = CLASSNAME;
wc.lpszClassName = SUB_CLASSNAME;
if (!RegisterClassW(&wc))
return -1;
HWND hwnd = CreateWindowW(CLASSNAME, CLASSNAME, WS_OVERLAPPEDWINDOW,
HWND hwnd = CreateWindowW(SUB_CLASSNAME, SUB_CLASSNAME, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 100,
NULL, NULL, GetModuleHandleW(NULL), NULL);
NULL, NULL, hInstance, NULL);
if (!hwnd)
return -1;
return -2;
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
@ -292,7 +311,5 @@ wWinMain(HINSTANCE hInstance,
DispatchMessageW(&msg);
}
CloseHandle(s_hEvent);
return 0;
}

View file

@ -5,72 +5,10 @@
#include <stdio.h>
#include <strsafe.h>
#define TEMP_FILE L"shell-notify-temporary.txt"
#define CLASSNAME L"SHChangeNotify testcase window"
#define EVENT_NAME L"SHChangeNotify testcase event"
#define MAIN_CLASSNAME L"SHChangeNotify main window"
#define SUB_CLASSNAME L"SHChangeNotify sub-window"
#define WM_SHELL_NOTIFY (WM_USER + 100)
#define WM_GET_NOTIFY_FLAGS (WM_USER + 101)
#define WM_CLEAR_FLAGS (WM_USER + 102)
#define WM_SET_PATHS (WM_USER + 103)
typedef enum TYPE
{
TYPE_RENAMEITEM,
TYPE_CREATE,
TYPE_DELETE,
TYPE_MKDIR,
TYPE_RMDIR,
TYPE_RENAMEFOLDER,
TYPE_UPDATEDIR,
TYPE_MAX = TYPE_UPDATEDIR
} TYPE;
typedef enum DIRTYPE
{
DIRTYPE_NULL = 0,
DIRTYPE_DESKTOP,
DIRTYPE_MYCOMPUTER,
DIRTYPE_MYDOCUMENTS
} DIRTYPE;
inline LPITEMIDLIST DoGetPidl(DIRTYPE iDir)
{
LPITEMIDLIST ret = NULL;
switch (iDir)
{
case DIRTYPE_NULL:
break;
case DIRTYPE_DESKTOP:
SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &ret);
break;
case DIRTYPE_MYCOMPUTER:
SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &ret);
break;
case DIRTYPE_MYDOCUMENTS:
SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &ret);
break;
}
return ret;
}
static inline LPWSTR DoGetDir(DIRTYPE iDir)
{
static size_t s_index = 0;
static WCHAR s_pathes[3][MAX_PATH];
LPWSTR psz = s_pathes[s_index];
LPITEMIDLIST pidl = DoGetPidl(iDir);
psz[0] = 0;
SHGetPathFromIDListW(pidl, psz);
CoTaskMemFree(pidl);
s_index = (s_index + 1) % _countof(s_pathes);
return psz;
}
static inline HWND DoWaitForWindow(LPCWSTR clsname, LPCWSTR text, BOOL bClosing, BOOL bForce)
{