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