1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * PURPOSE:         Test for CloseWindow
5  * PROGRAMMERS:     Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6  */
7 
8 #include "precomp.h"
9 
10 static const WCHAR s_szClassName[] = L"CloseWindowTest";
11 
12 static BOOL s_bTracing = FALSE;
13 
14 static INT s_nWM_SYSCOMMAND = 0;
15 static INT s_nWM_NCACTIVATE = 0;
16 static INT s_nWM_WINDOWPOSCHANGING = 0;
17 static INT s_nWM_ACTIVATE = 0;
18 
19 #define TIMER_INTERVAL 200
20 
21 static void
22 DoMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
23 {
24     if (uMsg == WM_TIMER || !s_bTracing)
25         return;
26 
27     trace("uMsg:0x%04X, wParam:0x%08lX, lParam:0x%08lX\n", uMsg, (LONG)wParam, (LONG)lParam);
28 
29     if (uMsg == WM_SYSCOMMAND)
30     {
31         ++s_nWM_SYSCOMMAND;
32     }
33 
34     if (uMsg == WM_NCACTIVATE)
35         ++s_nWM_NCACTIVATE;
36 
37     if (uMsg == WM_WINDOWPOSCHANGING)
38         ++s_nWM_WINDOWPOSCHANGING;
39 
40     if (uMsg == WM_ACTIVATE)
41         ++s_nWM_ACTIVATE;
42 }
43 
44 // WM_TIMER
45 static void
46 OnTimer(HWND hwnd, UINT id)
47 {
48     KillTimer(hwnd, id);
49     switch (id)
50     {
51         //
52         // SetActiveWindow(GetDesktopWindow());
53         //
54         case 0:
55             SetForegroundWindow(GetDesktopWindow());
56             SetActiveWindow(GetDesktopWindow());
57             ok(GetForegroundWindow() == NULL, "GetForegroundWindow() != NULL\n");
58             ok(GetActiveWindow() == NULL, "GetActiveWindow() != NULL\n");
59             ok(GetFocus() == NULL, "GetFocus() != NULL\n");
60             s_nWM_SYSCOMMAND = 0;
61             s_nWM_NCACTIVATE = 0;
62             s_nWM_WINDOWPOSCHANGING = 0;
63             s_nWM_ACTIVATE = 0;
64             s_bTracing = TRUE;
65             trace("CloseWindow(hwnd): tracing...\n");
66             ok(CloseWindow(hwnd), "CloseWindow failed\n");
67             break;
68         case 1:
69             ok(GetForegroundWindow() == NULL, "GetForegroundWindow() != NULL\n");
70             ok(GetActiveWindow() == hwnd, "GetActiveWindow() != hwnd\n");
71             ok(GetFocus() == NULL, "GetFocus() != NULL\n");
72             s_bTracing = FALSE;
73             trace("CloseWindow(hwnd): tracing done\n");
74             ok(s_nWM_SYSCOMMAND == 0, "WM_SYSCOMMAND: %d\n", s_nWM_SYSCOMMAND);
75             ok(s_nWM_NCACTIVATE == 1, "WM_NCACTIVATE: %d\n", s_nWM_NCACTIVATE);
76             ok(s_nWM_WINDOWPOSCHANGING == 2, "WM_WINDOWPOSCHANGING: %d\n", s_nWM_WINDOWPOSCHANGING);
77             ok(s_nWM_ACTIVATE == 1, "WM_ACTIVATE: %d\n", s_nWM_ACTIVATE);
78             ShowWindow(hwnd, SW_RESTORE);
79             break;
80         //
81         // SetActiveWindow(hwnd);
82         //
83         case 2:
84             SetForegroundWindow(GetDesktopWindow());
85             SetActiveWindow(hwnd);
86             ok(GetForegroundWindow() == NULL, "GetForegroundWindow() != NULL\n");
87             ok(GetActiveWindow() == hwnd, "GetActiveWindow() != hwnd\n");
88             ok(GetFocus() == hwnd, "GetFocus() != hwnd\n");
89             s_nWM_SYSCOMMAND = 0;
90             s_nWM_NCACTIVATE = 0;
91             s_nWM_WINDOWPOSCHANGING = 0;
92             s_nWM_ACTIVATE = 0;
93             s_bTracing = TRUE;
94             trace("CloseWindow(hwnd): tracing...\n");
95             ok(CloseWindow(hwnd), "CloseWindow failed\n");
96             break;
97         case 3:
98             ok(GetForegroundWindow() == NULL, "GetForegroundWindow() != NULL\n");
99             ok(GetActiveWindow() == hwnd, "GetActiveWindow() != hwnd\n");
100             ok(GetFocus() == NULL, "GetFocus() != NULL\n");
101             s_bTracing = FALSE;
102             trace("CloseWindow(hwnd): tracing done\n");
103             ok(s_nWM_SYSCOMMAND == 0, "WM_SYSCOMMAND: %d\n", s_nWM_SYSCOMMAND);
104             ok(s_nWM_NCACTIVATE == 0, "WM_NCACTIVATE: %d\n", s_nWM_NCACTIVATE);
105             ok(s_nWM_WINDOWPOSCHANGING == 1, "WM_WINDOWPOSCHANGING: %d\n", s_nWM_WINDOWPOSCHANGING);
106             ok(s_nWM_ACTIVATE == 0, "WM_ACTIVATE: %d\n", s_nWM_ACTIVATE);
107             ShowWindow(hwnd, SW_RESTORE);
108             break;
109         //
110         // SetForegroundWindow(hwnd);
111         //
112         case 4:
113             SetActiveWindow(GetDesktopWindow());
114             SetForegroundWindow(hwnd);
115             ok(GetForegroundWindow() == hwnd, "GetForegroundWindow() != hwnd\n");
116             ok(GetActiveWindow() == hwnd, "GetActiveWindow() != hwnd\n");
117             ok(GetFocus() == hwnd, "GetFocus() != hwnd\n");
118             s_nWM_SYSCOMMAND = 0;
119             s_nWM_NCACTIVATE = 0;
120             s_nWM_WINDOWPOSCHANGING = 0;
121             s_nWM_ACTIVATE = 0;
122             s_bTracing = TRUE;
123             trace("CloseWindow(hwnd): tracing...\n");
124             ok(CloseWindow(hwnd), "CloseWindow failed\n");
125             break;
126         case 5:
127             ok(GetForegroundWindow() == hwnd, "GetForegroundWindow() != hwnd\n");
128             ok(GetActiveWindow() == hwnd, "GetActiveWindow() != hwnd\n");
129             ok(GetFocus() == NULL, "GetFocus() != NULL\n");
130             s_bTracing = FALSE;
131             trace("CloseWindow(hwnd): tracing done\n");
132             ok(s_nWM_SYSCOMMAND == 0, "WM_SYSCOMMAND: %d\n", s_nWM_SYSCOMMAND);
133             ok(s_nWM_NCACTIVATE == 0, "WM_NCACTIVATE: %d\n", s_nWM_NCACTIVATE);
134             ok(s_nWM_WINDOWPOSCHANGING == 1, "WM_WINDOWPOSCHANGING: %d\n", s_nWM_WINDOWPOSCHANGING);
135             ok(s_nWM_ACTIVATE == 0, "WM_ACTIVATE: %d\n", s_nWM_ACTIVATE);
136             ShowWindow(hwnd, SW_RESTORE);
137             break;
138         default: // finish
139             DestroyWindow(hwnd);
140             return;
141     }
142     SetTimer(hwnd, id + 1, TIMER_INTERVAL, NULL);
143 }
144 
145 static LRESULT CALLBACK
146 WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
147 {
148     DoMessage(hwnd, uMsg, wParam, lParam);
149     switch (uMsg)
150     {
151         case WM_CREATE:
152             SetTimer(hwnd, 0, TIMER_INTERVAL, NULL);
153             break;
154         case WM_TIMER:
155             OnTimer(hwnd, (UINT)wParam);
156             break;
157         case WM_DESTROY:
158             PostQuitMessage(0);
159             break;
160         default:
161             return DefWindowProc(hwnd, uMsg, wParam, lParam);
162     }
163     return 0;
164 }
165 
166 START_TEST(CloseWindow)
167 {
168     WNDCLASSW wc;
169     HICON hIcon;
170     HCURSOR hCursor;
171     ATOM atom;
172     HWND hwnd;
173     MSG msg;
174 
175     ok(!CloseWindow(NULL), "CloseWindow(NULL) should be failed\n");
176 
177     hIcon = LoadIcon(NULL, IDI_APPLICATION);
178     ok(hIcon != NULL, "hIcon was NULL\n");
179     hCursor = LoadCursor(NULL, IDC_ARROW);
180     ok(hCursor != NULL, "hCursor was NULL\n");
181 
182     ZeroMemory(&wc, sizeof(wc));
183     wc.lpfnWndProc = WindowProc;
184     wc.hInstance = GetModuleHandleW(NULL);
185     wc.hIcon = hIcon;
186     wc.hCursor = hCursor;
187     wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
188     wc.lpszClassName = s_szClassName;
189     atom = RegisterClassW(&wc);
190     ok(atom != 0, "RegisterClassW failed\n");
191 
192     if (!atom)
193     {
194         skip("atom is zero\n");
195         DestroyIcon(hIcon);
196         DestroyCursor(hCursor);
197         return;
198     }
199 
200     hwnd = CreateWindowW(s_szClassName, L"CloseWindow testcase", WS_OVERLAPPEDWINDOW,
201                          CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,
202                          NULL, NULL, GetModuleHandleW(NULL), NULL);
203     ok(hwnd != NULL, "CreateWindowW failed\n");
204     trace("hwnd: %p\n", hwnd);
205 
206     if (!hwnd)
207     {
208         skip("hwnd is NULL\n");
209         UnregisterClassW(s_szClassName, GetModuleHandleW(NULL));
210         DestroyIcon(hIcon);
211         DestroyCursor(hCursor);
212         return;
213     }
214 
215     ShowWindow(hwnd, SW_SHOWNORMAL);
216     UpdateWindow(hwnd);
217 
218     while (GetMessage(&msg, NULL, 0, 0))
219     {
220         TranslateMessage(&msg);
221         DispatchMessage(&msg);
222     }
223 
224     UnregisterClassW(s_szClassName, GetModuleHandleW(NULL));
225     DestroyIcon(hIcon);
226     DestroyCursor(hCursor);
227 }
228