upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/lifecycle_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/lifecycle_test.cpp')
-rw-r--r--runtime/test/lifecycle_test.cpp181
1 files changed, 181 insertions, 0 deletions
diff --git a/runtime/test/lifecycle_test.cpp b/runtime/test/lifecycle_test.cpp
new file mode 100644
index 000000000..8f1f51dd9
--- /dev/null
+++ b/runtime/test/lifecycle_test.cpp
@@ -0,0 +1,181 @@
1// Checks the console lifecycle shim (runtime/shims/lifecycle.cpp): app-state change registrations, suspend
2// callback delivery, oversized window fitting, and window subclass forwarding.
3#ifndef WIN32_LEAN_AND_MEAN
4#define WIN32_LEAN_AND_MEAN
5#endif
6#ifndef NOMINMAX
7#define NOMINMAX
8#endif
9
10#include <windows.h>
11
12#include <cstdio>
13#include <cstring>
14
15#include "lifecycle.h"
16#include "shims.h"
17
18namespace {
19
20int g_checks = 0;
21int g_failures = 0;
22
23void check(bool condition, const char* what) {
24 ++g_checks;
25 std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
26 if (!condition) ++g_failures;
27}
28
29struct SuspendProbe {
30 int calls = 0;
31 BOOLEAN last_quiesced = FALSE;
32};
33
34void WINAPI on_app_state(BOOLEAN quiesced, PVOID context) {
35 auto* probe = static_cast<SuspendProbe*>(context);
36 if (probe) {
37 ++probe->calls;
38 probe->last_quiesced = quiesced;
39 }
40}
41
42using UnregFn = void(WINAPI*)(PVOID);
43UnregFn g_unreg = nullptr;
44
45// Context for a callback that unregisters `target` (another registration, or its own handle) when it runs.
46struct UnregProbe {
47 int calls = 0;
48 PVOID target = nullptr;
49};
50
51void WINAPI on_app_state_unregister(BOOLEAN, PVOID context) {
52 auto* probe = static_cast<UnregProbe*>(context);
53 ++probe->calls;
54 if (probe->target) g_unreg(probe->target);
55}
56
57} // namespace
58
59int main() {
60 using RegFn = ULONG(WINAPI*)(void(WINAPI*)(BOOLEAN, PVOID), PVOID, PVOID*);
61 using CreateExWFn = HWND(WINAPI*)(DWORD, LPCWSTR, LPCWSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE,
62 LPVOID);
63
64 const auto reg = reinterpret_cast<RegFn>(
65 rt::shims::find("api-ms-win-core-psm-appnotify-l1-1-0.dll", "RegisterAppStateChangeNotification"));
66 const auto unreg = reinterpret_cast<UnregFn>(
67 rt::shims::find("api-ms-win-core-psm-appnotify-l1-1-0.dll", "UnregisterAppStateChangeNotification"));
68 const auto create_w = reinterpret_cast<CreateExWFn>(rt::shims::find("user32.dll", "CreateWindowExW"));
69 const auto create_w_ext =
70 reinterpret_cast<CreateExWFn>(rt::shims::find("ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "CreateWindowExW"));
71
72 check(reg != nullptr && unreg != nullptr, "Register/UnregisterAppStateChangeNotification resolve");
73 check(create_w != nullptr && create_w == create_w_ext,
74 "CreateWindowExW resolves through user32 and ext-ms-win-rtcore-ntuser-window");
75 check(!rt::shims::owns_dll("user32.dll"), "user32.dll stays partially overridden");
76 // The title imports CreateWindowExA from the ANSI window set, not from user32.dll.
77 const void* create_a = rt::shims::find("user32.dll", "CreateWindowExA");
78 check(create_a != nullptr &&
79 rt::shims::find("ext-ms-win-rtcore-ntuser-window-ansi-l1-1-0.dll", "CreateWindowExA") == create_a,
80 "CreateWindowExA resolves through ext-ms-win-rtcore-ntuser-window-ansi to the same shim");
81 if (!reg || !unreg || !create_w) return 1;
82 g_unreg = unreg;
83
84 // 1. Parameter validation and registration / unregistration / suspend dispatch.
85 PVOID handle_a = nullptr, handle_b = nullptr;
86 check(reg(nullptr, nullptr, &handle_a) == ERROR_INVALID_PARAMETER, "null routine returns ERROR_INVALID_PARAMETER");
87 check(reg(&on_app_state, nullptr, nullptr) == ERROR_INVALID_PARAMETER,
88 "null registration out pointer returns ERROR_INVALID_PARAMETER");
89
90 SuspendProbe probe_a{}, probe_b{};
91 check(reg(&on_app_state, &probe_a, &handle_a) == ERROR_SUCCESS && handle_a != nullptr, "register callback A");
92 check(reg(&on_app_state, &probe_b, &handle_b) == ERROR_SUCCESS && handle_b != nullptr && handle_b != handle_a,
93 "register callback B with distinct handle");
94 check(rt::lifecycle::registered_notification_count() == 2, "two registrations active");
95
96 unreg(handle_a);
97 check(rt::lifecycle::registered_notification_count() == 1, "unregister removes callback A");
98
99 rt::lifecycle::invoke_suspend_callbacks_for_test();
100 check(probe_a.calls == 0 && probe_b.calls == 1 && probe_b.last_quiesced == TRUE,
101 "suspend dispatch invokes only remaining callback with quiesced=TRUE");
102 unreg(handle_b);
103 check(rt::lifecycle::registered_notification_count() == 0, "all registrations removed");
104
105 // 1b. A callback that unregisters a later registration: the later one is skipped during the same dispatch.
106 PVOID handle_later = nullptr, handle_first = nullptr;
107 SuspendProbe later_probe{};
108 UnregProbe first_probe{};
109 check(reg(&on_app_state_unregister, &first_probe, &handle_first) == ERROR_SUCCESS,
110 "register unregistering callback");
111 check(reg(&on_app_state, &later_probe, &handle_later) == ERROR_SUCCESS, "register later callback");
112 first_probe.target = handle_later;
113 rt::lifecycle::invoke_suspend_callbacks_for_test();
114 check(first_probe.calls == 1 && later_probe.calls == 0,
115 "unregistered later callback does not run in the same dispatch");
116 check(rt::lifecycle::registered_notification_count() == 1, "later registration is gone, the first remains");
117 unreg(handle_first);
118 check(rt::lifecycle::registered_notification_count() == 0, "dispatch test registrations removed");
119
120 // 1c. A callback that unregisters itself: no deadlock, it runs once, and it is gone afterwards.
121 PVOID handle_self = nullptr;
122 UnregProbe self_probe{};
123 check(reg(&on_app_state_unregister, &self_probe, &handle_self) == ERROR_SUCCESS,
124 "register self-unregistering callback");
125 self_probe.target = handle_self;
126 rt::lifecycle::invoke_suspend_callbacks_for_test();
127 check(self_probe.calls == 1 && rt::lifecycle::registered_notification_count() == 0,
128 "self-unregistering callback runs once and is removed without deadlock");
129 rt::lifecycle::invoke_suspend_callbacks_for_test();
130 check(self_probe.calls == 1, "removed self-unregistering callback is not called again");
131
132 // 2. Oversized window scaling math.
133 const RECT area{0, 0, 1920, 1040};
134 const auto fitted_4k = rt::lifecycle::fit_window_to_area(area, 3840, 2160);
135 check(fitted_4k.first == 1848 && fitted_4k.second == 1040,
136 "3840x2160 scales proportionally to 1848x1040 inside 1920x1040 work area");
137 const auto fitted_720 = rt::lifecycle::fit_window_to_area(area, 1280, 720);
138 check(fitted_720.first == 1280 && fitted_720.second == 720, "1280x720 stays unchanged");
139 const auto fitted_def = rt::lifecycle::fit_window_to_area(area, CW_USEDEFAULT, 0);
140 check(fitted_def.first == CW_USEDEFAULT && fitted_def.second == 0, "CW_USEDEFAULT stays unchanged");
141
142 // 3. Subclassed window still forwards normal messages to its original window procedure.
143 const HWND hwnd = create_w(0, L"STATIC", L"before", 0, 0, 0, 16, 16, nullptr, nullptr, nullptr, nullptr);
144 check(hwnd != nullptr, "CreateWindowExW shim creates a live window");
145 if (hwnd) {
146 SetWindowTextW(hwnd, L"after");
147 wchar_t buf[16] = {};
148 const int n = GetWindowTextW(hwnd, buf, 16);
149 check(n == 5 && std::wcscmp(buf, L"after") == 0, "subclassed window forwards WM_SETTEXT/WM_GETTEXT");
150 check(GetPropW(hwnd, L"leonida.title.wndproc") != nullptr, "original WNDPROC property installed");
151 DestroyWindow(hwnd);
152 }
153
154 // 4. Only the title's first top-level window is hooked; a window is released when it is destroyed. Hidden windows
155 // only: no SC_CLOSE is posted.
156 const wchar_t* kHookProp = L"leonida.title.wndproc";
157 WNDCLASSEXW wc{};
158 wc.cbSize = sizeof(wc);
159 wc.lpfnWndProc = DefWindowProcW;
160 wc.hInstance = GetModuleHandleW(nullptr);
161 wc.lpszClassName = L"LeonidaLifecycleTest";
162 RegisterClassExW(&wc);
163 const DWORD kMain = WS_OVERLAPPEDWINDOW;
164 const wchar_t* kClass = L"LeonidaLifecycleTest";
165 HWND main_win = create_w(0, kClass, L"main", kMain, 0, 0, 64, 64, nullptr, nullptr, nullptr, nullptr);
166 check(main_win && GetPropW(main_win, kHookProp) != nullptr, "first parentless window is the hooked title window");
167 HWND second = create_w(0, kClass, L"second", kMain, 0, 0, 64, 64, nullptr, nullptr, nullptr, nullptr);
168 check(second && GetPropW(second, kHookProp) == nullptr, "a second parentless window is not hooked");
169 HWND tool = create_w(WS_EX_TOOLWINDOW, L"LeonidaLifecycleTest", L"tool", WS_POPUP, 0, 0, 64, 64, nullptr, nullptr,
170 nullptr, nullptr);
171 check(tool && GetPropW(tool, kHookProp) == nullptr, "a WS_EX_TOOLWINDOW window is not hooked");
172 if (main_win) DestroyWindow(main_win);
173 HWND again = create_w(0, kClass, L"again", kMain, 0, 0, 64, 64, nullptr, nullptr, nullptr, nullptr);
174 check(again && GetPropW(again, kHookProp) != nullptr, "after the title window is destroyed, a new one is hooked");
175 for (HWND w : {second, tool, again}) {
176 if (w) DestroyWindow(w);
177 }
178
179 std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures);
180 return g_failures ? 1 : 0;
181}