Skip to content

Commit f7c36c6

Browse files
authored
[SHELL32_APITEST] Certainly close newly-opened windows (reactos#8171)
It's frustrating when windows open during a test and remain after the test is over. JIRA issue: ROSTESTS-402 - Delete closewnd.cpp. Enhance closewnd.h. - Modify ShellExecCmdLine, ShellExec_RunDLL, ShellExecuteEx, and ShellExecuteW testcases. - Certainly close the newly-opened windows.
1 parent f1332c7 commit f7c36c6

File tree

8 files changed

+210
-277
lines changed

8 files changed

+210
-277
lines changed

modules/rostests/apitests/shell32/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
spec2def(shell32_apitest.exe shell32_apitest.spec)
33

44
list(APPEND SOURCE
5-
closewnd.cpp
65
AddCommas.cpp
76
CFSFolder.cpp
87
CheckEscapes.cpp

modules/rostests/apitests/shell32/RealShellExecuteEx.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ static void TEST_Start(void)
5353

5454
static void TEST_End(void)
5555
{
56-
Sleep(500);
56+
// Execution can be asynchronous; you have to wait for it to finish.
57+
Sleep(2000);
58+
59+
// Close newly-opened window(s)
5760
GetWindowList(&s_List2);
5861
CloseNewWindows(&s_List1, &s_List2);
5962
FreeWindowList(&s_List1);
@@ -62,8 +65,6 @@ static void TEST_End(void)
6265

6366
static void TEST_RealShellExecuteExA(void)
6467
{
65-
TEST_Start();
66-
6768
INT_PTR ret;
6869

6970
ret = (INT_PTR)s_fnRealShellExecuteExA(
@@ -82,14 +83,10 @@ static void TEST_RealShellExecuteExA(void)
8283
ok_long((LONG)ret, 42);
8384
else
8485
ok_long((LONG)ret, 2);
85-
86-
TEST_End();
8786
}
8887

8988
static void TEST_RealShellExecuteExW(void)
9089
{
91-
TEST_Start();
92-
9390
INT_PTR ret;
9491

9592
ret = (INT_PTR)s_fnRealShellExecuteExW(
@@ -105,8 +102,6 @@ static void TEST_RealShellExecuteExW(void)
105102
NULL,
106103
0);
107104
ok_long((LONG)ret, 42);
108-
109-
TEST_End();
110105
}
111106

112107
START_TEST(RealShellExecuteEx)
@@ -129,8 +124,12 @@ START_TEST(RealShellExecuteEx)
129124
return;
130125
}
131126

127+
TEST_Start();
128+
132129
TEST_RealShellExecuteExA();
133130
TEST_RealShellExecuteExW();
134131

132+
TEST_End();
133+
135134
FreeLibrary(s_hSHELL32);
136135
}

modules/rostests/apitests/shell32/ShellExecCmdLine.cpp

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <strsafe.h>
1010
#include <versionhelpers.h>
1111
#include "shell32_apitest_sub.h"
12+
#include "closewnd.h"
1213

1314
#define NDEBUG
1415
#include <debug.h>
@@ -549,46 +550,6 @@ static const TEST_ENTRY s_entries_2[] =
549550
{ __LINE__, FALSE, TRUE, L"\"Test File 2.bat\" \"Test File.txt\"", s_cur_dir },
550551
};
551552

552-
typedef struct OPENWNDS
553-
{
554-
UINT count;
555-
HWND *phwnd;
556-
} OPENWNDS;
557-
558-
static OPENWNDS s_wi0 = { 0 }, s_wi1 = { 0 };
559-
560-
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
561-
{
562-
OPENWNDS *info = (OPENWNDS *)lParam;
563-
info->phwnd = (HWND *)realloc(info->phwnd, (info->count + 1) * sizeof(HWND));
564-
if (!info->phwnd)
565-
return FALSE;
566-
info->phwnd[info->count] = hwnd;
567-
++(info->count);
568-
return TRUE;
569-
}
570-
571-
static void CleanupNewlyCreatedWindows(void)
572-
{
573-
EnumWindows(EnumWindowsProc, (LPARAM)&s_wi1);
574-
for (UINT i1 = 0; i1 < s_wi1.count; ++i1)
575-
{
576-
BOOL bFound = FALSE;
577-
for (UINT i0 = 0; i0 < s_wi0.count; ++i0)
578-
{
579-
if (s_wi1.phwnd[i1] == s_wi0.phwnd[i0])
580-
{
581-
bFound = TRUE;
582-
break;
583-
}
584-
}
585-
if (!bFound)
586-
PostMessageW(s_wi1.phwnd[i1], WM_CLOSE, 0, 0);
587-
}
588-
free(s_wi1.phwnd);
589-
ZeroMemory(&s_wi1, sizeof(s_wi1));
590-
}
591-
592553
static void DoEntry(const TEST_ENTRY *pEntry)
593554
{
594555
HRESULT hr;
@@ -626,8 +587,33 @@ static void DoEntry(const TEST_ENTRY *pEntry)
626587

627588
ok(result == pEntry->result, "Line %d: result expected %d, was %d\n",
628589
pEntry->lineno, pEntry->result, result);
590+
}
591+
592+
static WINDOW_LIST s_List1, s_List2;
593+
594+
static VOID TEST_ShellExecCmdLine(VOID)
595+
{
596+
GetWindowList(&s_List1);
629597

630-
CleanupNewlyCreatedWindows();
598+
// do tests
599+
for (size_t i = 0; i < _countof(s_entries_1); ++i)
600+
{
601+
DoEntry(&s_entries_1[i]);
602+
}
603+
SetEnvironmentVariableW(L"PATH", s_cur_dir);
604+
for (size_t i = 0; i < _countof(s_entries_2); ++i)
605+
{
606+
DoEntry(&s_entries_2[i]);
607+
}
608+
609+
// Execution can be asynchronous; you have to wait for it to finish.
610+
Sleep(2000);
611+
612+
// Close newly-opened window(s)
613+
GetWindowList(&s_List2);
614+
CloseNewWindows(&s_List1, &s_List2);
615+
FreeWindowList(&s_List1);
616+
FreeWindowList(&s_List2);
631617
}
632618

633619
START_TEST(ShellExecCmdLine)
@@ -657,22 +643,13 @@ START_TEST(ShellExecCmdLine)
657643
return;
658644
}
659645

660-
// record open windows
661-
if (!EnumWindows(EnumWindowsProc, (LPARAM)&s_wi0))
662-
{
663-
skip("EnumWindows failed\n");
664-
free(s_wi0.phwnd);
665-
return;
666-
}
667-
668646
// s_win_test_exe
669647
GetWindowsDirectoryW(s_win_test_exe, _countof(s_win_test_exe));
670648
PathAppendW(s_win_test_exe, L"test program.exe");
671649
BOOL ret = CopyFileW(s_sub_program, s_win_test_exe, FALSE);
672650
if (!ret)
673651
{
674652
skip("Please retry with admin rights\n");
675-
free(s_wi0.phwnd);
676653
return;
677654
}
678655

@@ -700,27 +677,14 @@ START_TEST(ShellExecCmdLine)
700677
// s_cur_dir
701678
GetCurrentDirectoryW(_countof(s_cur_dir), s_cur_dir);
702679

703-
// do tests
704-
for (size_t i = 0; i < _countof(s_entries_1); ++i)
705-
{
706-
DoEntry(&s_entries_1[i]);
707-
}
708-
SetEnvironmentVariableW(L"PATH", s_cur_dir);
709-
for (size_t i = 0; i < _countof(s_entries_2); ++i)
710-
{
711-
DoEntry(&s_entries_2[i]);
712-
}
680+
TEST_ShellExecCmdLine();
713681

714-
Sleep(2000);
715-
CleanupNewlyCreatedWindows();
682+
// Some process can lock the file of s_win_test_exe
683+
Sleep(1000);
716684

717685
// clean up
718686
ok(DeleteFileW(s_win_test_exe), "failed to delete the test file\n");
719687
ok(DeleteFileW(s_sys_bat_file), "failed to delete the test file\n");
720688
ok(DeleteFileA("Test File 1.txt"), "failed to delete the test file\n");
721689
ok(DeleteFileA("Test File 2.bat"), "failed to delete the test file\n");
722-
free(s_wi0.phwnd);
723-
724-
DoWaitForWindow(SUB_CLASSNAME, SUB_CLASSNAME, TRUE, TRUE);
725-
Sleep(100);
726690
}

modules/rostests/apitests/shell32/ShellExec_RunDLL.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,41 @@ static VOID TEST_ShellExec_RunDLLW(VOID)
2626
ShellExec_RunDLLW(NULL, NULL, L"?0?notepad.exe", SW_SHOWNORMAL);
2727
}
2828

29-
static VOID CleanupWindowList(VOID)
29+
static BOOL CloseNotepad(VOID)
3030
{
31-
GetWindowListForClose(&s_List2);
31+
HWND hwndNew;
32+
WCHAR szClass[64];
33+
34+
// Execution can be asynchronous; you have to wait for it to finish.
35+
Sleep(1000);
36+
37+
// Close newly-opened window(s)
38+
GetWindowList(&s_List2);
39+
hwndNew = FindNewWindow(&s_List1, &s_List2);
40+
if (!GetClassNameW(hwndNew, szClass, _countof(szClass)))
41+
szClass[0] = UNICODE_NULL;
3242
CloseNewWindows(&s_List1, &s_List2);
3343
FreeWindowList(&s_List1);
3444
FreeWindowList(&s_List2);
45+
return lstrcmpiW(szClass, L"Notepad") == 0;
3546
}
3647

3748
START_TEST(ShellExec_RunDLL)
3849
{
39-
HWND hwndNotepad;
50+
BOOL ret;
4051

4152
GetWindowList(&s_List1);
4253
TEST_ShellExec_RunDLL();
43-
Sleep(1000);
44-
hwndNotepad = FindWindowW(L"Notepad", NULL);
45-
ok(hwndNotepad != NULL, "Notepad not found\n");
46-
CleanupWindowList();
54+
ret = CloseNotepad();
55+
ok(ret, "Notepad not found\n");
4756

4857
GetWindowList(&s_List1);
4958
TEST_ShellExec_RunDLLA();
50-
Sleep(1000);
51-
hwndNotepad = FindWindowW(L"Notepad", NULL);
52-
ok(hwndNotepad != NULL, "Notepad not found\n");
53-
CleanupWindowList();
59+
ret = CloseNotepad();
60+
ok(ret, "Notepad not found\n");
5461

5562
GetWindowList(&s_List1);
5663
TEST_ShellExec_RunDLLW();
57-
Sleep(1000);
58-
hwndNotepad = FindWindowW(L"Notepad", NULL);
59-
ok(hwndNotepad != NULL, "Notepad not found\n");
60-
CleanupWindowList();
64+
ret = CloseNotepad();
65+
ok(ret, "Notepad not found\n");
6166
}

modules/rostests/apitests/shell32/ShellExecuteEx.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,30 @@ static BOOL TEST_Start(void)
314314

315315
static void TEST_End(void)
316316
{
317-
GetWindowListForClose(&s_List2);
318-
CloseNewWindows(&s_List1, &s_List2);
319-
FreeWindowList(&s_List1);
320-
FreeWindowList(&s_List2);
321-
322317
DeleteFileW(s_win_test_exe);
323318
DeleteFileW(s_sys_test_exe);
324319
DeleteFileW(s_win_txt_file);
325320
DeleteFileW(s_sys_txt_file);
326321
DeleteFileW(s_win_bat_file);
327322
DeleteFileW(s_sys_bat_file);
323+
324+
// Execution can be asynchronous; you have to wait for it to finish.
325+
INT nCount = GetWindowCount();
326+
for (INT i = 0; i < 100; ++i)
327+
{
328+
INT nOldCount = nCount;
329+
Sleep(3000);
330+
nCount = GetWindowCount();
331+
if (nOldCount == nCount)
332+
break;
333+
}
334+
Sleep(3000);
335+
336+
// Close newly-opened window(s)
337+
GetWindowList(&s_List2);
338+
CloseNewWindows(&s_List1, &s_List2);
339+
FreeWindowList(&s_List1);
340+
FreeWindowList(&s_List2);
328341
}
329342

330343
static void test_properties()

0 commit comments

Comments
 (0)