source: dev/trunk/ab5.0/abdev/ab-test/ab-test.cpp

Last change on this file was 829, checked in by イグトランス (egtra), 13 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain; charset=Shift_JIS
File size: 3.8 KB
Line 
1#include "stdafx.h"
2
3#include <GdiPlus.h>
4
5#include <boost/preprocessor/cat.hpp>
6
7#include <Resource/Load.h>
8
9#include <res/resource.h>
10
11namespace fs = boost::filesystem;
12
13namespace abres = ActiveBasic::Resource;
14
15using boost::implicit_cast;
16
17struct HModuleDeleter
18{
19 typedef HMODULE pointer;
20
21 void operator ()(pointer hmod) const
22 {
23 ::FreeLibrary(hmod);
24 }
25};
26
27typedef std::unique_ptr<HMODULE, HModuleDeleter> UniqueHModule;
28
29template<typename F>
30struct ScopeExitHolder
31{
32 ScopeExitHolder(F f) : f(f) {}
33
34 ~ScopeExitHolder()
35 {
36 f();
37 }
38
39private:
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
50template<typename F>
51ScopeExitHolder<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
58bool IconEquals(HICON hiconL, HICON hiconR)
59{
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);
69
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 }
88 return false;
89}
90
91UniqueHModule LoadABModule(LPCWSTR name)
92{
93 TCHAR moduleName[MAX_PATH];
94 ::GetModuleFileName(nullptr, moduleName, MAX_PATH);
95
96 auto systemDir = fs::path(moduleName).parent_path().parent_path().parent_path() / L"build/release/system";
97
98 UniqueHModule hmodRes(::LoadLibraryExW((systemDir / name).wstring().c_str(), nullptr, LOAD_LIBRARY_AS_DATAFILE));
99 BOOST_REQUIRE_NE(hmodRes.get(), implicit_cast<HMODULE>(nullptr));
100 return hmodRes;
101}
102
103BOOST_AUTO_TEST_CASE( IconResourceLoading )
104{
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
111 UniqueHModule hmodRes = LoadABModule(L"res.dll");
112
113 auto hBasicProgramIconL = (HICON)::LoadImage(hmodRes.get(), MAKEINTRESOURCE(IDI_BASICPROGRAM), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
114 auto hBasicProgramIconR = abres::LoadIconAlt(hmodRes.get(), IDI_BASICPROGRAM, 16, 16, LR_DEFAULTCOLOR);
115
116 BOOST_CHECK(hBasicProgramIconL != nullptr);
117 BOOST_CHECK(hBasicProgramIconR != nullptr);
118
119 BOOST_CHECK(IconEquals(hBasicProgramIconL, hBasicProgramIconR));
120
121 auto hcurL = ::LoadCursor(hmodRes.get(), MAKEINTRESOURCE(IDC_CURSOR_PEN));
122 auto hcurR = abres::LoadCursorAlt(hmodRes.get(), IDC_CURSOR_PEN);
123
124 BOOST_CHECK(hcurL != nullptr);
125 BOOST_CHECK(hcurR != nullptr);
126
127 //BOOST_CHECK(IconEquals(hcurL, hcurR));
128}
129
130BOOST_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
137 boost::optional<std::wstring> strR = abres::LoadStringAlt(hmodRes.get(), IDS_DEV_GROUP);
138
139 BOOST_CHECK(!!strR);
140 BOOST_CHECK(strL == *strR);
141}
Note: See TracBrowser for help on using the repository browser.