// Run with the resident stopped. The suite restores the original clipboard. #include "storage.h" #include "ocr.h" #include #include #include #include #include static void require(bool ok, const char *name) { if (!ok) throw std::runtime_error(name); std::cout << "PASS " << name << '\n'; } static HWND owner() { return CreateWindowExW(0,L"STATIC",L"Snapshot test",0,0,0,0,0,HWND_MESSAGE,nullptr,nullptr,nullptr); } static void child(const std::wstring &argument) { auto exe=ks::executable(), command=L"\""+exe+L"\" "+argument; STARTUPINFOW startup{sizeof(startup)}; startup.dwFlags=STARTF_USESHOWWINDOW; startup.wShowWindow=SW_HIDE; PROCESS_INFORMATION p{}; if(!CreateProcessW(exe.c_str(),command.data(),nullptr,nullptr,FALSE,CREATE_NO_WINDOW,nullptr,nullptr,&startup,&p)) throw std::runtime_error("start fixture process"); CloseHandle(p.hThread); auto wait=WaitForSingleObject(p.hProcess,30000); DWORD code=1; if(wait!=WAIT_OBJECT_0) {TerminateProcess(p.hProcess,1);WaitForSingleObject(p.hProcess,INFINITE);} GetExitCodeProcess(p.hProcess,&code); CloseHandle(p.hProcess); if(code) throw std::runtime_error("fixture process failed"); } static size_t resident() { ks::Bytes b(1024*1024); auto ws=reinterpret_cast(b.data()); if(!QueryWorkingSet(GetCurrentProcess(),ws,DWORD(b.size()))) throw std::runtime_error("working set query"); size_t pages=0; for(size_t i=0;iNumberOfEntries;++i) if(!ws->WorkingSetInfo[i].Shared) ++pages; return pages*4096; } static void verifyImage(HWND w) { auto c=ks::readClipboard(w,64*1024*1024); for(auto id:{CF_DIB,CF_DIBV5}) { auto it=std::find_if(c.formats.begin(),c.formats.end(),[&](const auto &f){return f.id==UINT(id);}); require(it!=c.formats.end(),"restores image representation"); auto pixels=ks::decodeImage(ks::Clip{{*it}}); require(pixels.width==1566 && pixels.height==1068 && pixels.bgra.front()==77,"preserves image pixels"); } require(c.text()==L"Snapshot mixed text","preserves accompanying text"); } static LRESULT CALLBACK delayedImageOwner(HWND window, UINT message, WPARAM format, LPARAM param) { if (message == WM_RENDERFORMAT && format == CF_DIB) { const auto dib = ks::imageDib(ks::Pixels{1, 1, ks::Bytes{77, 0, 0, 255}}); if (auto data = GlobalAlloc(GMEM_MOVEABLE, dib.data.size())) { if (auto bytes = GlobalLock(data)) { memcpy(bytes, dib.data.data(), dib.data.size()); GlobalUnlock(data); if (SetClipboardData(CF_DIB, data)) return 0; } GlobalFree(data); } return 0; } return DefWindowProcW(window, message, format, param); } static void verifyDelayedImageSnapshot(HWND window) { std::promise ready; auto owner = ready.get_future(); std::jthread publisher([&] { WNDCLASSW wc{}; wc.lpfnWndProc = delayedImageOwner; wc.hInstance = GetModuleHandleW(nullptr); wc.lpszClassName = L"KSwitcher.DelayedClipboardFixture"; HWND delayed = RegisterClassW(&wc) ? CreateWindowExW(0, wc.lpszClassName, L"", 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, wc.hInstance, nullptr) : nullptr; bool published = false; if (delayed && OpenClipboard(delayed)) { if (EmptyClipboard()) SetClipboardData(CF_DIB, nullptr); published = IsClipboardFormatAvailable(CF_DIB) && GetClipboardOwner() == delayed; CloseClipboard(); } ready.set_value(published ? delayed : nullptr); if (published) { MSG message{}; while (GetMessageW(&message, delayed, 0, 0) > 0 && message.message != WM_CLOSE) DispatchMessageW(&message); } if (delayed) DestroyWindow(delayed); }); const auto delayed = owner.get(); require(delayed != nullptr, "publishes a delayed image format"); struct CloseOwner { HWND window; ~CloseOwner() { PostMessageW(window, WM_CLOSE, 0, 0); } } close{delayed}; const auto before = GetClipboardSequenceNumber(); ks::ClipboardSnapshot snapshot; const auto captured = snapshot.capture(window, before); require(captured == GetClipboardSequenceNumber(), "snapshot reports the sequence after delayed rendering"); const auto temporary = ks::writeClipboard(window, ks::textClip(L"temporary")); snapshot.restore(window, temporary); const auto clip = ks::readClipboard(window); const auto image = ks::decodeImage(clip); require(image.width == 1 && image.height == 1 && image.bgra.front() == 77, "restores the delayed image"); } int main(int argc,char**argv) { CoInitializeEx(nullptr,COINIT_MULTITHREADED); HWND w=owner(); int result=0; ks::Clip original; bool saved=false; try { std::string mode=argc>1?argv[1]:"suite"; if(mode=="temporary") { ks::writeClipboard(w,ks::textClip(L"temporary")); } else if(mode=="publish") { ks::Pixels p{1566,1068,ks::Bytes(1566*1068*4,255)}; p.bgra[0]=77; auto dib=ks::imageDib(p); auto v5=dib; BITMAPV5HEADER h{}; h.bV5Size=sizeof(h); h.bV5Width=p.width; h.bV5Height=-p.height; h.bV5Planes=1; h.bV5BitCount=32; h.bV5Compression=BI_BITFIELDS; h.bV5RedMask=0xff0000; h.bV5GreenMask=0xff00; h.bV5BlueMask=0xff; h.bV5AlphaMask=0xff000000; v5.id=CF_DIBV5; v5.data.resize(sizeof(h)+p.bgra.size()); memcpy(v5.data.data(),&h,sizeof(h)); memcpy(v5.data.data()+sizeof(h),p.bgra.data(),p.bgra.size()); auto clip=ks::textClip(L"Snapshot mixed text"); clip.formats.push_back(dib); clip.formats.push_back(v5); ks::writeClipboard(w,clip); } else if(mode=="old" || mode=="new") { child(L"publish"); auto baseline=resident(); if(mode=="old") { auto clip=ks::readClipboard(w,64*1024*1024); ks::writeClipboard(w,ks::textClip(L"temporary")); ks::writeClipboard(w,clip); } else { ks::ClipboardSnapshot snapshot; auto started=GetTickCount64(); snapshot.capture(w,GetClipboardSequenceNumber()); std::cout<<"Image snapshot capture: "<