Skip to content

Commit 6e72fdf

Browse files
authored
Merge pull request #2 from SeraphimaZ/zone-tests
added Zone unit-tests
2 parents 1218323 + 834eb4e commit 6e72fdf

File tree

5 files changed

+557
-40
lines changed

5 files changed

+557
-40
lines changed

src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
</ClCompile>
104104
<ClCompile Include="RegistryHelpers.Spec.cpp" />
105105
<ClCompile Include="Util.Spec.cpp" />
106+
<ClCompile Include="Util.cpp" />
106107
<ClCompile Include="Zone.Spec.cpp" />
107108
<ClCompile Include="ZoneSet.Spec.cpp" />
108109
<ClCompile Include="ZoneWindow.Spec.cpp" />

src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<ClCompile Include="JsonHelpers.Tests.cpp">
3737
<Filter>Source Files</Filter>
3838
</ClCompile>
39+
<ClCompile Include="Util.cpp">
40+
<Filter>Source Files</Filter>
41+
</ClCompile>
3942
</ItemGroup>
4043
<ItemGroup>
4144
<ClInclude Include="pch.h">
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "Util.h"
2+
#include "pch.h"
3+
4+
static int s_classId = 0;
5+
6+
LRESULT CALLBACK DLLWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7+
{
8+
if (message == WM_DESTROY)
9+
{
10+
PostQuitMessage(0);
11+
return 0;
12+
}
13+
14+
return DefWindowProc(hwnd, message, wParam, lParam);
15+
}
16+
17+
BOOL RegisterDLLWindowClass(LPCWSTR szClassName, Mocks::HwndCreator* creator)
18+
{
19+
if (!creator)
20+
return false;
21+
22+
WNDCLASSEX wc;
23+
24+
wc.hInstance = creator->getHInstance();
25+
wc.lpszClassName = szClassName;
26+
wc.lpfnWndProc = DLLWindowProc;
27+
wc.cbSize = sizeof(WNDCLASSEX);
28+
29+
wc.style = CS_DBLCLKS;
30+
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
31+
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
32+
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
33+
wc.lpszMenuName = NULL;
34+
wc.cbClsExtra = 0;
35+
wc.cbWndExtra = 0;
36+
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
37+
38+
auto regRes = RegisterClassEx(&wc);
39+
return regRes;
40+
}
41+
42+
DWORD WINAPI ThreadProc(LPVOID lpParam)
43+
{
44+
MSG messages;
45+
Mocks::HwndCreator* creator = reinterpret_cast<Mocks::HwndCreator*>(lpParam);
46+
if (!creator)
47+
return -1;
48+
49+
if (RegisterDLLWindowClass((LPCWSTR)creator->getWindowClassName().c_str(), creator) != 0)
50+
{
51+
auto hWnd = CreateWindowEx(0, (LPCWSTR)creator->getWindowClassName().c_str(), (LPCWSTR)creator->getTitle().c_str(), WS_EX_APPWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 10, 10, nullptr, nullptr, creator->getHInstance(), NULL);
52+
creator->setHwnd(hWnd);
53+
creator->setCondition(true);
54+
55+
while (GetMessage(&messages, NULL, 0, 0))
56+
{
57+
TranslateMessage(&messages);
58+
DispatchMessage(&messages);
59+
}
60+
61+
creator->setHwnd(hWnd);
62+
}
63+
else
64+
{
65+
creator->setCondition(true);
66+
}
67+
68+
return 1;
69+
}
70+
71+
namespace Mocks
72+
{
73+
HwndCreator::HwndCreator(const std::wstring& title) :
74+
m_windowTitle(title), m_windowClassName(std::to_wstring(++s_classId)), m_conditionFlag(false), m_thread(nullptr), m_hInst(HINSTANCE{}), m_hWnd(nullptr)
75+
{
76+
}
77+
78+
HwndCreator::~HwndCreator()
79+
{
80+
std::unique_lock<std::mutex> lock(m_mutex);
81+
m_conditionVar.wait(lock, [this] { return m_conditionFlag; });
82+
83+
if (m_thread)
84+
{
85+
CloseHandle(m_thread);
86+
}
87+
}
88+
89+
HWND HwndCreator::operator()(HINSTANCE hInst)
90+
{
91+
m_hInst = hInst;
92+
m_conditionFlag = false;
93+
std::unique_lock<std::mutex> lock(m_mutex);
94+
95+
m_thread = CreateThread(0, NULL, ThreadProc, (LPVOID)this, NULL, NULL);
96+
m_conditionVar.wait(lock, [this] { return m_conditionFlag; });
97+
98+
return m_hWnd;
99+
}
100+
101+
void HwndCreator::setHwnd(HWND val)
102+
{
103+
m_hWnd = val;
104+
}
105+
106+
void HwndCreator::setCondition(bool cond)
107+
{
108+
m_conditionFlag = cond;
109+
m_conditionVar.notify_one();
110+
}
111+
112+
}

src/modules/fancyzones/tests/UnitTests/Util.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,33 @@ namespace Mocks
3939
return reinterpret_cast<HINSTANCE>(++s_nextInstance);
4040
}
4141

42-
}
42+
class HwndCreator
43+
{
44+
public:
45+
HwndCreator(const std::wstring& title = L"");
46+
47+
~HwndCreator();
48+
49+
HWND operator()(HINSTANCE hInst);
50+
51+
void setHwnd(HWND val);
52+
void setCondition(bool cond);
53+
54+
inline HINSTANCE getHInstance() const { return m_hInst; }
55+
inline const std::wstring& getTitle() const { return m_windowTitle; }
56+
inline const std::wstring& getWindowClassName() const { return m_windowClassName; }
57+
58+
private:
59+
std::wstring m_windowTitle;
60+
std::wstring m_windowClassName;
61+
62+
std::mutex m_mutex;
63+
std::condition_variable m_conditionVar;
64+
bool m_conditionFlag;
65+
HANDLE m_thread;
66+
67+
HINSTANCE m_hInst;
68+
HWND m_hWnd;
69+
};
70+
}
71+

0 commit comments

Comments
 (0)