#include "stdafx.h" #include #include #include #define BOOST_TEST_MAIN #include #include #include #include namespace fs = boost::filesystem; namespace abres = ActiveBasic::Resource; struct HModuleDeleter { typedef HMODULE pointer; void operator ()(pointer hmod) const { ::FreeLibrary(hmod); } }; typedef std::unique_ptr UniqueHModule; template struct ScopeExitHolder { ScopeExitHolder(F f) : f(f) {} ~ScopeExitHolder() { f(); } private: F f; ScopeExitHolder(ScopeExitHolder const&); ScopeExitHolder& operator =(ScopeExitHolder const&); }; #define AB_SCOPE_EXIT_2(f) auto BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_TEMP_, __LINE__) = (f); \ ScopeExitHolder BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_HOLDER_, __LINE__)_ (BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_TEMP_, __LINE__)); #define AB_SCOPE_EXIT(f) AB_SCOPE_EXIT_2([&]() f) template ScopeExitHolder ScopeExit(F fn) { return fn; } #define GET_AT(bd, y, x) (*reinterpret_cast(static_cast((bd).Scan0) + (y) * bdL.Stride + (x) * sizeof (DWORD))) bool IconEquals(HICON hiconL, HICON hiconR) { Gdiplus::Bitmap bmpL(hiconL), bmpR(hiconR); if (bmpL.GetWidth() == bmpR.GetWidth() && bmpR.GetHeight() == bmpR.GetHeight()) { Gdiplus::Rect rc(0, 0, bmpL.GetWidth(), bmpL.GetHeight()); Gdiplus::BitmapData bdL = {}, bdR = {}; BOOST_CHECK_EQUAL(bmpL.LockBits(&rc, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bdL), Gdiplus::Ok); BOOST_CHECK_EQUAL(bmpR.LockBits(&rc, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bdR), Gdiplus::Ok); AB_SCOPE_EXIT({bmpL.UnlockBits(&bdL);}); AB_SCOPE_EXIT({bmpR.UnlockBits(&bdR);}); if (bdL.Width == bdR.Width && bdL.Height == bdR.Height) { for (UINT i = 0; i < bdL.Height; ++i) { if (std::memcmp( static_cast(bdL.Scan0) + i * bdL.Stride, static_cast(bdR.Scan0) + i * bdR.Stride, bdL.Width * sizeof (std::uint32_t)) != 0) { return false; } } return true; } } return false; } BOOST_AUTO_TEST_CASE( ResourceLoading ) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); AB_SCOPE_EXIT_2(std::bind(Gdiplus::GdiplusShutdown, gdiplusToken)); TCHAR moduleName[MAX_PATH]; ::GetModuleFileName(nullptr, moduleName, MAX_PATH); auto systemDir = fs::path(moduleName).parent_path().parent_path() / "build/release/system"; UniqueHModule hmodRes(::LoadLibraryExW((systemDir / "res.dll").wstring().c_str(), nullptr, LOAD_LIBRARY_AS_DATAFILE)); BOOST_CHECK(hmodRes != nullptr); auto hBasicProgramIconTarget = (HICON)LoadImage(hmodRes.get(), MAKEINTRESOURCE(IDI_BASICPROGRAM), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); auto hBasicProgramIcon = abres::LoadIcon(hmodRes.get(), IDI_BASICPROGRAM, 16, 16, LR_DEFAULTCOLOR); BOOST_CHECK(hBasicProgramIconTarget != nullptr); BOOST_CHECK(hBasicProgramIcon != nullptr); BOOST_CHECK(IconEquals(hBasicProgramIconTarget, hBasicProgramIcon)); }