148 lines
6.9 KiB
C++
148 lines
6.9 KiB
C++
#include "app.h"
|
|
#include "input_pause.h"
|
|
#include <iostream>
|
|
|
|
namespace {
|
|
void require(bool result, const char *message)
|
|
{
|
|
if (!result) throw std::runtime_error(message);
|
|
}
|
|
|
|
template<class Predicate> 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<HMENU>(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);
|
|
}
|
|
}
|
|
|
|
bool trayIconMatchesInput(HICON icon, bool active)
|
|
{
|
|
HDC dc = CreateCompatibleDC(nullptr);
|
|
HBITMAP bitmap = CreateBitmap(16, 16, 1, 32, nullptr);
|
|
bool matches = false;
|
|
if (dc && bitmap) {
|
|
const auto previous = SelectObject(dc, bitmap);
|
|
if (icon && DrawIconEx(dc, 0, 0, icon, 16, 16, 0, nullptr, DI_NORMAL)) {
|
|
const auto tile = GetPixel(dc, 2, 8);
|
|
const auto white = RGB(255, 255, 255);
|
|
const auto gap = GetPixel(dc, 8, 8);
|
|
// The active keycap is purple. The pause glyph has two white strokes with a gap.
|
|
matches = active ? GetBValue(tile) > GetRValue(tile) + 70 :
|
|
GetPixel(dc, 5, 8) == white && GetPixel(dc, 10, 8) == white &&
|
|
GetRValue(gap) < 160 && std::abs(GetBValue(gap) - GetRValue(gap)) < 32;
|
|
}
|
|
SelectObject(dc, previous);
|
|
}
|
|
if (bitmap) DeleteObject(bitmap);
|
|
if (dc) DeleteDC(dc);
|
|
return matches;
|
|
}
|
|
|
|
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");
|
|
require(query(resident, ProbeTrayIcon, TRUE), "active tray icon missing at startup");
|
|
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(query(resident, ProbeTrayIcon), "pause bars missing after pause");
|
|
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(query(resident, ProbeTrayIcon), "Files shortcut conflict cleared the pause icon");
|
|
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(query(resident, ProbeTrayIcon), "hook shortcut conflict cleared the pause icon");
|
|
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(query(resident, ProbeTrayIcon, TRUE), "active tray icon missing after resume");
|
|
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;
|
|
}
|