289 lines
12 KiB
C++
289 lines
12 KiB
C++
#include "app.h"
|
|
#include <iostream>
|
|
|
|
namespace
|
|
{
|
|
ks::App *app{};
|
|
HWND host{}, field{};
|
|
ks::Clip saved;
|
|
bool captured = false, failed = false;
|
|
int stage = 0, ru = -1, en = -1;
|
|
size_t punctuationCase = 0;
|
|
const std::vector<std::pair<std::wstring, std::wstring>> punctuationCases{
|
|
{L"(ljvf) ", L"(дома) "}, {L"[ljvf] ", L"[дома] "}, {L"{ljvf} ", L"{дома} "},
|
|
{L"ljvf: ", L"дома: "}, {L"ljvf; ", L"дома; "}, {L"#ljvf%; ", L"#дома%; "}};
|
|
ULONGLONG deadline{};
|
|
std::wstring sentinel = L"KSwitcher integration clipboard sentinel";
|
|
void require(bool ok, const char *name)
|
|
{
|
|
if (!ok)
|
|
throw std::runtime_error(name);
|
|
std::cout << "PASS " << name << '\n';
|
|
}
|
|
std::wstring text()
|
|
{
|
|
wchar_t buffer[2048]{};
|
|
GetWindowTextW(field, buffer, 2048);
|
|
return buffer;
|
|
}
|
|
void prepare(const wchar_t *value, bool select)
|
|
{
|
|
app->resetInput();
|
|
SetWindowTextW(field, value);
|
|
ks::activateWindow(host);
|
|
SetFocus(field);
|
|
int end = GetWindowTextLengthW(field);
|
|
SendMessageW(field, EM_SETSEL, select ? 0 : end, end);
|
|
if (GetForegroundWindow() != host)
|
|
throw std::runtime_error("Test editor could not acquire foreground; no input was sent");
|
|
}
|
|
void record(const std::wstring &keys)
|
|
{
|
|
std::lock_guard lock(app->stateMutex);
|
|
for (wchar_t c : keys)
|
|
app->run.record(c, host, app->layouts[en].handle, GetTickCount64());
|
|
}
|
|
void finish()
|
|
{
|
|
KillTimer(host, 1);
|
|
if (captured)
|
|
{
|
|
try
|
|
{
|
|
app->ownSequence = ks::writeClipboard(app->window, saved);
|
|
}
|
|
catch (...)
|
|
{
|
|
failed = true;
|
|
}
|
|
}
|
|
DestroyWindow(host);
|
|
PostMessageW(app->window, WM_CLOSE, 0, 0);
|
|
}
|
|
LRESULT CALLBACK procedure(HWND w, UINT msg, WPARAM wp, LPARAM lp)
|
|
{
|
|
// Model a host that does not forward layout requests to its embedded editor.
|
|
if (msg == WM_INPUTLANGCHANGEREQUEST)
|
|
return 0;
|
|
if (msg == WM_TIMER)
|
|
{
|
|
try
|
|
{
|
|
if (GetTickCount64() > deadline)
|
|
throw std::runtime_error("Native integration timed out");
|
|
if (app->busy)
|
|
return 0;
|
|
switch (stage++)
|
|
{
|
|
case 0:
|
|
saved = ks::readClipboard(app->window, 64 * 1024 * 1024);
|
|
captured = true;
|
|
app->ownSequence = ks::writeClipboard(app->window, ks::textClip(sentinel));
|
|
prepare(L"ghbdtn", true);
|
|
app->transform(ks::Direct + ru);
|
|
break;
|
|
case 1:
|
|
std::cout << "Observed editor: " << ks::utf8(text()) << "; busy=" << app->busy
|
|
<< "; foreground=" << (GetForegroundWindow() == host) << "\n";
|
|
require(text() == L"привет", "selection conversion through actual Ctrl+C / Ctrl+V");
|
|
require(ks::readClipboard(app->window).text() == sentinel, "clipboard restored");
|
|
app->transform(ks::Undo);
|
|
break;
|
|
case 2:
|
|
require(text() == L"ghbdtn", "verified undo");
|
|
prepare(L"hello world", true);
|
|
app->transform(ks::Case);
|
|
break;
|
|
case 3:
|
|
require(text() == L"Hello World", "case conversion");
|
|
app->transform(ks::Case);
|
|
break;
|
|
case 4:
|
|
require(text() == L"HELLO WORLD", "consecutive command reselects verified previous result");
|
|
prepare(L"OLDghbdtn", false);
|
|
record(L"ghbdtn");
|
|
app->transform(ks::Direct + ru);
|
|
break;
|
|
case 5:
|
|
std::cout << "Recent observed: " << ks::utf8(text()) << "\n";
|
|
require(text() == L"OLDпривет", "recent-input conversion preserves adjacent older text");
|
|
prepare(L"OLDabcdef", false);
|
|
record(L"ghbdtn");
|
|
app->transform(ks::Direct + ru);
|
|
break;
|
|
case 6:
|
|
require(text() == L"OLDabcdef", "recent-input mismatch refuses replacement");
|
|
app->settings.snippets = {{L";addr", L"LINE1\r\n{cursor}LINE2"}};
|
|
prepare(L"prefix ;addr", false);
|
|
record(L";addr");
|
|
app->transform(0, 0);
|
|
break;
|
|
case 7:
|
|
{
|
|
require(text() == L"prefix LINE1\r\nLINE2", "multiline snippet expansion");
|
|
DWORD start{}, end{};
|
|
SendMessageW(field, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
|
|
require(start == end && start == 14, "verified snippet cursor placement");
|
|
require(ks::readClipboard(app->window).text() == sentinel,
|
|
"clipboard restored after repeated copy transactions");
|
|
app->settings.autoCorrect = true;
|
|
app->settings.switchLayout = true;
|
|
app->settings.autoLanguages = {L"en", L"ru"};
|
|
app->settings.cycle = {app->layouts[en].id, app->layouts[ru].id};
|
|
ActivateKeyboardLayout(app->layouts[en].handle, 0);
|
|
prepare(L"ghbdtn ", false);
|
|
record(L"ghbdtn ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
}
|
|
case 8:
|
|
require(text() == L"привет ", "automatic dictionary correction in native editor");
|
|
require(ks::windowLayout(host) == app->layouts[ru].handle,
|
|
"auto-correction switches focused editor layout even when host ignores requests");
|
|
app->transform(ks::Undo);
|
|
break;
|
|
case 9:
|
|
require(text() == L"ghbdtn ", "automatic correction undo retains space");
|
|
require(ks::windowLayout(host) == app->layouts[en].handle,
|
|
"undo restores the original keyboard layout");
|
|
prepare(L"ghbdtn ", false);
|
|
record(L"ghbdtn ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 10:
|
|
require(text() == L"ghbdtn ", "undo prevents repeated automatic correction");
|
|
prepare(L"hello ", false);
|
|
record(L"hello ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 11:
|
|
require(text() == L"hello ", "known word stays unchanged in editor");
|
|
require(ks::readClipboard(app->window).text() == sentinel,
|
|
"automatic correction restores clipboard");
|
|
app->autoCorrector = ks::AutoCorrector{};
|
|
prepare(L"OLDghbdtn ", false);
|
|
record(L"ghbdtn ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 12:
|
|
require(text() == L"OLDghbdtn ", "automatic correction refuses partial word replacement");
|
|
DestroyWindow(field);
|
|
field = CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_PASSWORD, 20, 20,
|
|
590, 30, host, nullptr, GetModuleHandleW(nullptr), nullptr);
|
|
prepare(L"ghbdtn ", false);
|
|
record(L"ghbdtn ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 13:
|
|
require(text() == L"ghbdtn ", "password input is never automatically corrected");
|
|
require(ks::readClipboard(app->window).text() == sentinel,
|
|
"password input never copied to clipboard");
|
|
DestroyWindow(field);
|
|
field = CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 20, 20, 590, 30, host,
|
|
nullptr, GetModuleHandleW(nullptr), nullptr);
|
|
prepare(L"(ljvf ", false);
|
|
record(L"(ljvf ");
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 14:
|
|
require(text() == L"(дома ", "automatic correction preserves opening parenthesis in editor");
|
|
app->transform(ks::Undo);
|
|
break;
|
|
case 15:
|
|
require(text() == L"(ljvf ", "undo preserves opening parenthesis in editor");
|
|
require(ks::readClipboard(app->window).text() == sentinel,
|
|
"parenthesized correction restores clipboard");
|
|
app->settings.autoPrefixChars += L"#";
|
|
app->settings.autoSuffixChars += L"%";
|
|
app->autoCorrector = ks::AutoCorrector{};
|
|
prepare(punctuationCases[0].first.c_str(), false);
|
|
record(punctuationCases[0].first);
|
|
app->transform(ks::AutoCorrect);
|
|
break;
|
|
case 16:
|
|
require(text() == punctuationCases[punctuationCase].second,
|
|
"configured punctuation preserved in editor");
|
|
if (++punctuationCase < punctuationCases.size())
|
|
{
|
|
ActivateKeyboardLayout(app->layouts[en].handle, 0);
|
|
prepare(punctuationCases[punctuationCase].first.c_str(), false);
|
|
record(punctuationCases[punctuationCase].first);
|
|
app->transform(ks::AutoCorrect);
|
|
stage = 16;
|
|
}
|
|
else
|
|
app->transform(ks::Undo);
|
|
break;
|
|
case 17:
|
|
require(text() == punctuationCases.back().first, "undo preserves custom punctuation");
|
|
finish();
|
|
break;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
failed = true;
|
|
std::cerr << "FAIL " << e.what() << '\n';
|
|
finish();
|
|
}
|
|
return 0;
|
|
}
|
|
if (msg == WM_CLOSE)
|
|
{
|
|
failed = true;
|
|
finish();
|
|
return 0;
|
|
}
|
|
return DefWindowProcW(w, msg, wp, lp);
|
|
}
|
|
} // namespace
|
|
int main()
|
|
{
|
|
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
|
auto root = std::filesystem::temp_directory_path() /
|
|
(L"KSwitcher-integration-" + std::to_wstring(GetCurrentProcessId()));
|
|
try
|
|
{
|
|
{
|
|
ks::App instance(GetModuleHandleW(nullptr), false, root);
|
|
app = &instance;
|
|
app->settings.history = false;
|
|
app->settings.switchLayout = false;
|
|
for (int i = 0; i < (int)app->layouts.size(); ++i)
|
|
{
|
|
auto language = LOWORD((uintptr_t)app->layouts[i].handle);
|
|
if (language == 0x409)
|
|
en = i;
|
|
if (language == 0x419)
|
|
ru = i;
|
|
}
|
|
if (en < 0 || ru < 0)
|
|
throw std::runtime_error(
|
|
"Integration test requires installed English US and Russian layouts");
|
|
WNDCLASSW cls{};
|
|
cls.hInstance = GetModuleHandleW(nullptr);
|
|
cls.lpfnWndProc = procedure;
|
|
cls.lpszClassName = L"KSwitcher.TestEditor";
|
|
cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
|
RegisterClassW(&cls);
|
|
host = CreateWindowW(cls.lpszClassName, L"KSwitcher — automated test editor",
|
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 650, 320, nullptr, nullptr,
|
|
cls.hInstance, nullptr);
|
|
field =
|
|
CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_WANTRETURN,
|
|
20, 20, 590, 230, host, nullptr, cls.hInstance, nullptr);
|
|
deadline = GetTickCount64() + 30000;
|
|
SetTimer(host, 1, 100, nullptr);
|
|
app->execute();
|
|
}
|
|
std::filesystem::remove_all(root);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
std::cerr << e.what() << '\n';
|
|
failed = true;
|
|
}
|
|
CoUninitialize();
|
|
return failed ? 1 : 0;
|
|
}
|