[781] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
| 3 | #include <GdiPlus.h>
|
---|
| 4 |
|
---|
[782] | 5 | #include <boost/preprocessor/cat.hpp>
|
---|
| 6 |
|
---|
[781] | 7 | #include <Resource/Load.h>
|
---|
| 8 |
|
---|
| 9 | #include <res/resource.h>
|
---|
| 10 |
|
---|
| 11 | namespace fs = boost::filesystem;
|
---|
| 12 |
|
---|
| 13 | namespace abres = ActiveBasic::Resource;
|
---|
| 14 |
|
---|
[818] | 15 | using boost::implicit_cast;
|
---|
| 16 |
|
---|
[781] | 17 | struct HModuleDeleter
|
---|
| 18 | {
|
---|
| 19 | typedef HMODULE pointer;
|
---|
| 20 |
|
---|
| 21 | void operator ()(pointer hmod) const
|
---|
| 22 | {
|
---|
| 23 | ::FreeLibrary(hmod);
|
---|
| 24 | }
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | typedef std::unique_ptr<HMODULE, HModuleDeleter> UniqueHModule;
|
---|
| 28 |
|
---|
[782] | 29 | template<typename F>
|
---|
| 30 | struct ScopeExitHolder
|
---|
| 31 | {
|
---|
| 32 | ScopeExitHolder(F f) : f(f) {}
|
---|
| 33 |
|
---|
| 34 | ~ScopeExitHolder()
|
---|
| 35 | {
|
---|
| 36 | f();
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | private:
|
---|
| 40 | F f;
|
---|
| 41 |
|
---|
| 42 | ScopeExitHolder(ScopeExitHolder const&);
|
---|
| 43 | ScopeExitHolder& operator =(ScopeExitHolder const&);
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | #define AB_SCOPE_EXIT_2(f) auto BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_TEMP_, __LINE__) = (f); \
|
---|
| 47 | ScopeExitHolder<decltype(BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_TEMP_, __LINE__))> BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_HOLDER_, __LINE__)_ (BOOST_PP_CAT(AB_SCOPE_EXIT_FUNCTION_TEMP_, __LINE__));
|
---|
| 48 | #define AB_SCOPE_EXIT(f) AB_SCOPE_EXIT_2([&]() f)
|
---|
| 49 |
|
---|
| 50 | template<typename F>
|
---|
| 51 | ScopeExitHolder<F> ScopeExit(F fn)
|
---|
| 52 | {
|
---|
| 53 | return fn;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | #define GET_AT(bd, y, x) (*reinterpret_cast<DWORD const*>(static_cast<BYTE const*>((bd).Scan0) + (y) * bdL.Stride + (x) * sizeof (DWORD)))
|
---|
| 57 |
|
---|
[781] | 58 | bool IconEquals(HICON hiconL, HICON hiconR)
|
---|
| 59 | {
|
---|
[782] | 60 | Gdiplus::Bitmap bmpL(hiconL), bmpR(hiconR);
|
---|
| 61 |
|
---|
| 62 | if (bmpL.GetWidth() == bmpR.GetWidth()
|
---|
| 63 | && bmpR.GetHeight() == bmpR.GetHeight())
|
---|
| 64 | {
|
---|
| 65 | Gdiplus::Rect rc(0, 0, bmpL.GetWidth(), bmpL.GetHeight());
|
---|
| 66 | Gdiplus::BitmapData bdL = {}, bdR = {};
|
---|
| 67 | BOOST_CHECK_EQUAL(bmpL.LockBits(&rc, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bdL), Gdiplus::Ok);
|
---|
| 68 | BOOST_CHECK_EQUAL(bmpR.LockBits(&rc, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bdR), Gdiplus::Ok);
|
---|
[789] | 69 |
|
---|
[782] | 70 | AB_SCOPE_EXIT({bmpL.UnlockBits(&bdL);});
|
---|
| 71 | AB_SCOPE_EXIT({bmpR.UnlockBits(&bdR);});
|
---|
| 72 |
|
---|
| 73 | if (bdL.Width == bdR.Width && bdL.Height == bdR.Height)
|
---|
| 74 | {
|
---|
| 75 | for (UINT i = 0; i < bdL.Height; ++i)
|
---|
| 76 | {
|
---|
| 77 | if (std::memcmp(
|
---|
| 78 | static_cast<BYTE const*>(bdL.Scan0) + i * bdL.Stride,
|
---|
| 79 | static_cast<BYTE const*>(bdR.Scan0) + i * bdR.Stride,
|
---|
| 80 | bdL.Width * sizeof (std::uint32_t)) != 0)
|
---|
| 81 | {
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return true;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[781] | 88 | return false;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[789] | 91 | UniqueHModule LoadABModule(LPCWSTR name)
|
---|
[781] | 92 | {
|
---|
[789] | 93 | TCHAR moduleName[MAX_PATH];
|
---|
| 94 | ::GetModuleFileName(nullptr, moduleName, MAX_PATH);
|
---|
| 95 |
|
---|
[821] | 96 | auto systemDir = fs::path(moduleName).parent_path().parent_path().parent_path() / L"build/release/system";
|
---|
[789] | 97 |
|
---|
| 98 | UniqueHModule hmodRes(::LoadLibraryExW((systemDir / name).wstring().c_str(), nullptr, LOAD_LIBRARY_AS_DATAFILE));
|
---|
[818] | 99 | BOOST_REQUIRE_NE(hmodRes.get(), implicit_cast<HMODULE>(nullptr));
|
---|
[789] | 100 | return hmodRes;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | BOOST_AUTO_TEST_CASE( IconResourceLoading )
|
---|
| 104 | {
|
---|
[782] | 105 | Gdiplus::GdiplusStartupInput gdiplusStartupInput;
|
---|
| 106 |
|
---|
| 107 | ULONG_PTR gdiplusToken;
|
---|
| 108 | Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
|
---|
| 109 | AB_SCOPE_EXIT_2(std::bind(Gdiplus::GdiplusShutdown, gdiplusToken));
|
---|
| 110 |
|
---|
[789] | 111 | UniqueHModule hmodRes = LoadABModule(L"res.dll");
|
---|
[781] | 112 |
|
---|
[789] | 113 | auto hBasicProgramIconL = (HICON)::LoadImage(hmodRes.get(), MAKEINTRESOURCE(IDI_BASICPROGRAM), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
|
---|
[793] | 114 | auto hBasicProgramIconR = abres::LoadIconAlt(hmodRes.get(), IDI_BASICPROGRAM, 16, 16, LR_DEFAULTCOLOR);
|
---|
[781] | 115 |
|
---|
[789] | 116 | BOOST_CHECK(hBasicProgramIconL != nullptr);
|
---|
| 117 | BOOST_CHECK(hBasicProgramIconR != nullptr);
|
---|
[781] | 118 |
|
---|
[789] | 119 | BOOST_CHECK(IconEquals(hBasicProgramIconL, hBasicProgramIconR));
|
---|
[781] | 120 |
|
---|
[789] | 121 | auto hcurL = ::LoadCursor(hmodRes.get(), MAKEINTRESOURCE(IDC_CURSOR_PEN));
|
---|
[793] | 122 | auto hcurR = abres::LoadCursorAlt(hmodRes.get(), IDC_CURSOR_PEN);
|
---|
[781] | 123 |
|
---|
[789] | 124 | BOOST_CHECK(hcurL != nullptr);
|
---|
| 125 | BOOST_CHECK(hcurR != nullptr);
|
---|
[781] | 126 |
|
---|
[789] | 127 | //BOOST_CHECK(IconEquals(hcurL, hcurR));
|
---|
[781] | 128 | }
|
---|
[789] | 129 |
|
---|
| 130 | BOOST_AUTO_TEST_CASE( StringResourceLoading )
|
---|
| 131 | {
|
---|
| 132 | UniqueHModule hmodRes = LoadABModule(L"res.dll");
|
---|
| 133 |
|
---|
| 134 | WCHAR strL[1024];
|
---|
| 135 | ::LoadStringW(hmodRes.get(), IDS_DEV_GROUP, strL, ARRAYSIZE(strL));
|
---|
| 136 |
|
---|
[793] | 137 | boost::optional<std::wstring> strR = abres::LoadStringAlt(hmodRes.get(), IDS_DEV_GROUP);
|
---|
[789] | 138 |
|
---|
| 139 | BOOST_CHECK(!!strR);
|
---|
| 140 | BOOST_CHECK(strL == *strR);
|
---|
| 141 | }
|
---|