#include "app.h" #include "input_pause.h" #include namespace { void require(bool result, const char *message) { if (!result) throw std::runtime_error(message); } template void until(Predicate ready, const char *message) { auto deadline = GetTickCount64() + 4000; while (!ready()) { require(GetTickCount64() < deadline, message); Sleep(10); } } HWND childWindow(DWORD pid, const wchar_t *type) { HWND w{}; while ((w = FindWindowExW(nullptr, w, type, nullptr))) { DWORD owner{}; GetWindowThreadProcessId(w, &owner); if (owner == pid) return w; } return nullptr; } DWORD_PTR query(HWND w, UINT msg, WPARAM wp = 0) { DWORD_PTR result{}; require(SendMessageTimeoutW(w, msg, wp, 0, SMTO_ABORTIFHUNG | SMTO_ERRORONEXIT, 2000, &result), "test resident did not respond"); return result; } void chooseInputAction(HWND resident, DWORD pid, const wchar_t *expected) { require(PostMessageW(resident, ks::TrayMessage, 0, WM_CONTEXTMENU), "cannot open tray menu"); HWND popup{}; until([&] { popup = childWindow(pid, L"#32768"); return popup && IsWindowVisible(popup); }, "tray menu did not open"); const auto menu = reinterpret_cast(query(popup, MN_GETHMENU)); wchar_t label[100]{}; require(GetMenuStringW(menu, 0, label, std::size(label), MF_BYPOSITION) && std::wstring_view(label).starts_with(expected), "incorrect input state in tray menu"); require(PostMessageW(popup, WM_CHAR, L'i', 0), "cannot select input menu item"); until([&] { return !IsWindow(popup); }, "tray action did not close menu"); query(resident, WM_NULL); } } int testInputPause() { auto name = L"KSwitcher-input-pause-" + ks::newSnippetId(); const auto folder = std::filesystem::temp_directory_path() / name; PROCESS_INFORMATION child{}; HWND resident{}; int result = 1; bool created = false; try { // Never switch the user's visible desktop or direct a command at their resident. const auto desktop = CreateDesktopW(name.c_str(), nullptr, nullptr, 0, GENERIC_ALL, nullptr); require(desktop && SetThreadDesktop(desktop), "cannot use private desktop"); created = std::filesystem::create_directory(folder); require(created, "cannot create fixture directory"); const auto fixture = folder / L"KSwitcherInputTests.exe"; std::filesystem::copy_file(ks::executable(), fixture); auto command = L"\"" + fixture.wstring() + L"\" --input-pause-resident"; STARTUPINFOW startup{sizeof(startup)}; startup.lpDesktop = name.data(); require(CreateProcessW(fixture.c_str(), command.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, folder.c_str(), &startup, &child), "cannot start test resident"); until([&] { resident = childWindow(child.dwProcessId, L"KSwitcher.Main"); return resident && query(resident, ProbeInputState) == 1; }, "resident did not start with both hooks and shortcuts"); constexpr UINT mods = MOD_CONTROL | MOD_SHIFT; require(!RegisterHotKey(nullptr, 1, mods, VK_F23) && GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED, "Files shortcut should be owned by resident before pause"); chooseInputAction(resident, child.dwProcessId, L"Pause &input processing"); until([&] { return query(resident, ProbeInputState) == 2; }, "pause did not release both hooks and bindings and cancel the queued snippet"); require(RegisterHotKey(nullptr, 1, mods, VK_F23), "another process could not acquire Files shortcut"); require(query(resident, ProbeInputState, 1) == 2, "settings, layout reload or IPC restarted input"); chooseInputAction(resident, child.dwProcessId, L"Resume &input processing"); require(query(resident, ProbeInputState) == 2, "Files conflict partially resumed input"); require(UnregisterHotKey(nullptr, 1), "cannot release Files shortcut"); require(RegisterHotKey(nullptr, 2, mods, VK_F22), "cannot occupy hook shortcut during pause"); chooseInputAction(resident, child.dwProcessId, L"Resume &input processing"); require(query(resident, ProbeInputState) == 2, "hook shortcut conflict partially resumed input"); require(RegisterHotKey(nullptr, 1, mods, VK_F23), "failed resume leaked Files registration"); UnregisterHotKey(nullptr, 1); UnregisterHotKey(nullptr, 2); chooseInputAction(resident, child.dwProcessId, L"Resume &input processing"); require(query(resident, ProbeInputState) == 1, "resume did not restore both hooks and shortcuts"); require(!RegisterHotKey(nullptr, 1, mods, VK_F23) && GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED, "resident did not reclaim Files shortcut"); std::cout << "Tray pause/resume, hooks, settings reload, cross-process ownership and conflict rollback passed.\n"; result = 0; } catch (const std::exception &e) { std::cerr << e.what() << '\n'; } if (child.hProcess) { if (resident) PostMessageW(resident, WM_CLOSE, 0, 0); if (WaitForSingleObject(child.hProcess, 2000) != WAIT_OBJECT_0) { TerminateProcess(child.hProcess, 1); WaitForSingleObject(child.hProcess, 2000); result = 1; } CloseHandle(child.hThread); CloseHandle(child.hProcess); } std::error_code error; if (created) std::filesystem::remove_all(folder, error); return result; }