forked from chcg/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMenuManager.cpp
More file actions
258 lines (198 loc) · 9.45 KB
/
TestMenuManager.cpp
File metadata and controls
258 lines (198 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "stdafx.h"
#include <gtest/gtest.h>
#include "MenuManager.h"
#include <Notepad_plus_msgs.h>
namespace NppPythonScript
{
static volatile HMENU g_hPluginMenu;
static LRESULT dummyWindowProc(HWND /* hWnd */, UINT message, WPARAM /* wParam */, LPARAM /* lParam */)
{
switch(message)
{
case WM_CREATE:
return FALSE;
case NPPM_GETMENUHANDLE:
return reinterpret_cast<LRESULT>(g_hPluginMenu);
default:
return TRUE;
}
}
#define UNIT_TEST_WINDOW_CLASS L"UnitTestDummy"
enum MenuIds {
MENUID_FILE_OPEN = 1000,
MENUID_FILE_CLOSE,
MENUID_FILE_RECENT,
MENUID_FILE_EXIT,
MENUID_FILE_RECENT_1,
MENUID_FILE_RECENT_2,
MENUID_FILE_RECENT_3,
MENUID_EDIT_COPY,
MENUID_EDIT_PASTE,
MENUID_XMLTOOLS_PRETTYPRINT,
MENUID_XMLTOOLS_OPEN,
MENUID_XMLTOOLS_RECENT_1,
MENUID_XMLTOOLS_ABOUT,
MENUID_SECONDPLUGIN_PRETTYPRINT,
MENUID_SECONDPLUGIN_OPEN,
MENUID_SECONDPLUGIN_ABOUT,
MENUID_SECONDPLUGIN_RECENT_1,
MENUID_SECONDPLUGIN_RECENT_2
};
class MenuManagerTest : public ::testing::Test {
virtual void SetUp() {
m_hModule = ::GetModuleHandle(NULL);
RegisterTestWindowClass();
CreateTestMenu();
CreateTestWindow();
ASSERT_NE(0, (LONG_PTR)m_hWindow);
MenuManager::create(m_hWindow, reinterpret_cast<HINSTANCE>(m_hModule), NULL);
m_menuManager = MenuManager::getInstance();
}
virtual void TearDown() {
DestroyWindow(m_hWindow);
UnregisterClass(UNIT_TEST_WINDOW_CLASS, m_hModule);
DestroyMenu(m_hMenu);
MenuManager::destroyForTests();
}
protected:
HMENU m_hMenu;
HWND m_hWindow;
HMODULE m_hModule;
MenuManager* m_menuManager;
void RegisterTestWindowClass() {
WNDCLASS wndClass;
memset(&wndClass, 0, sizeof(WNDCLASS));
wndClass.lpfnWndProc = (WNDPROC)&dummyWindowProc;
wndClass.hInstance = m_hModule;
wndClass.lpszClassName = UNIT_TEST_WINDOW_CLASS;
wndClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
::RegisterClassW(&wndClass);
}
/*
Menu Structure:
File
--> Open
--> Close
--> Recent
--> Recent 1
--> Recent 2
--> Recent 3
--> Exit
Edit
--> Copy
--> Paste
Plugins
--> XML Tools
--> Pretty Print
--> Open
--> Recent 1
--> About
--> Second Plugin
--> Pretty Print
--> Open
--> Recent
--> Recent 1
--> Recent 2
--> About
*/
void CreateTestMenu() {
m_hMenu = ::CreateMenu();
HMENU fileMenu = ::CreateMenu();
::AppendMenu(m_hMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(fileMenu), L"File");
::AppendMenu(fileMenu, MF_STRING, MENUID_FILE_OPEN, L"Open\tCtrl-O");
::AppendMenu(fileMenu, MF_STRING, MENUID_FILE_CLOSE, L"Close\tCtrl-F4");
HMENU recentMenu = ::CreateMenu();
::AppendMenu(recentMenu, MF_STRING, MENUID_FILE_RECENT_1, L"Recent 1");
::AppendMenu(recentMenu, MF_STRING, MENUID_FILE_RECENT_2, L"Recent 2");
::AppendMenu(recentMenu, MF_STRING, MENUID_FILE_RECENT_3, L"Recent 3");
::AppendMenu(fileMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(recentMenu), L"Recent");
::AppendMenu(fileMenu, MF_STRING, MENUID_FILE_CLOSE, L"Exit\tAlt-F4");
HMENU editMenu = ::CreateMenu();
::AppendMenu(m_hMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(editMenu), L"Edit");
::AppendMenu(editMenu, MF_STRING, MENUID_EDIT_COPY, L"Copy");
::AppendMenu(editMenu, MF_STRING, MENUID_EDIT_PASTE, L"Paste\tCtrl-V");
g_hPluginMenu = ::CreateMenu();
::AppendMenu(m_hMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(g_hPluginMenu), L"Plugins");
HMENU xmlToolsPluginMenu = ::CreateMenu();
::AppendMenu(g_hPluginMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(xmlToolsPluginMenu), L"XML Tools");
::AppendMenu(xmlToolsPluginMenu, MF_STRING, MENUID_XMLTOOLS_PRETTYPRINT, L"Pretty Print\tCtrl-Alt-Shift-B");
::AppendMenu(xmlToolsPluginMenu, MF_STRING, MENUID_XMLTOOLS_OPEN, L"Open\tCtrl-O");
::AppendMenu(xmlToolsPluginMenu, MF_STRING, MENUID_XMLTOOLS_RECENT_1, L"Recent 1");
::AppendMenu(xmlToolsPluginMenu, MF_STRING, MENUID_XMLTOOLS_ABOUT, L"A&bout");
HMENU secondPluginMenu = ::CreateMenu();
::AppendMenu(g_hPluginMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(secondPluginMenu), L"Second Plugin");
::AppendMenu(secondPluginMenu, MF_STRING, MENUID_SECONDPLUGIN_PRETTYPRINT, L"Pretty Print\tCtrl-Alt-Shift-B");
::AppendMenu(secondPluginMenu, MF_STRING, MENUID_SECONDPLUGIN_OPEN, L"Open\tCtrl-O");
HMENU secondPluginRecentMenu = ::CreateMenu();
::AppendMenu(secondPluginMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(secondPluginRecentMenu), L"Recent");
::AppendMenu(secondPluginRecentMenu, MF_STRING, MENUID_SECONDPLUGIN_RECENT_1, L"Recent 1");
::AppendMenu(secondPluginRecentMenu, MF_STRING, MENUID_SECONDPLUGIN_RECENT_2, L"Recent 2");
::AppendMenu(secondPluginMenu, MF_STRING, MENUID_SECONDPLUGIN_ABOUT, L"A&bout");
}
void CreateTestWindow() {
m_hWindow = ::CreateWindow(
UNIT_TEST_WINDOW_CLASS, /* Class name */
L"Test window",
WS_OVERLAPPEDWINDOW,
0, 0, 100, 100, /* x, y, width, heigth */
NULL, /* hWndParent */
m_hMenu,
m_hModule,
NULL /* lpParam */
);
}
};
struct MenuTestCase {
MenuTestCase() {}
MenuTestCase(const TCHAR *pmenuName, const TCHAR *poptionName, int pexpectedID)
: menuName(pmenuName),
optionName(poptionName),
expectedID(pexpectedID)
{}
const TCHAR *menuName;
const TCHAR *optionName;
size_t expectedID;
};
class MenuManagerFindMenuCommandTest : public MenuManagerTest,
public ::testing::WithParamInterface<MenuTestCase> {
};
TEST_P(MenuManagerFindMenuCommandTest, testFindOptionFindsCorrectOption) {
MenuTestCase testCase = GetParam();
LONG_PTR windowWndProc = GetWindowLongPtr(m_hWindow, GWLP_WNDPROC);
LONG_PTR dummyProc = reinterpret_cast<LONG_PTR>(&dummyWindowProc);
ASSERT_EQ(windowWndProc, dummyProc);
size_t foundMenuId = m_menuManager->findMenuCommand(m_hMenu, NULL, testCase.menuName, testCase.optionName);
ASSERT_EQ(testCase.expectedID, foundMenuId);
}
INSTANTIATE_TEST_CASE_P(findMenuCommand, MenuManagerFindMenuCommandTest,
::testing::Values(MenuTestCase(L"File", L"Open", MENUID_FILE_OPEN), // First option
MenuTestCase(L"File", L"Close", MENUID_FILE_CLOSE), // First menu, middle option
MenuTestCase(L"Edit", L"Copy", MENUID_EDIT_COPY), // Mid menu, first option
MenuTestCase(L"Edit", L"Paste", MENUID_EDIT_PASTE), // Mid menu, last option
MenuTestCase(L"Recent", L"Recent 1", MENUID_FILE_RECENT_1), // Sub menu, first option
MenuTestCase(L"Recent", L"Recent 2", MENUID_FILE_RECENT_2),
MenuTestCase(L"Recent", L"Recent 3", MENUID_FILE_RECENT_3), // Sub menu, last option
MenuTestCase(L"File", L"Recent 2", 0), // Shouldn't find Recent 2 directly under the File menu
MenuTestCase(L"XML Tools", L"Open", MENUID_XMLTOOLS_OPEN), // Repeated option name, on third menu
MenuTestCase(L"Second Plugin", L"About", MENUID_SECONDPLUGIN_ABOUT), // Repeated option name, with & in title
MenuTestCase(L"Second Plugin", L"Not found", 0), // Non existent option
MenuTestCase(L"Not exists", L"Open", 0) // Non existent menu
));
class MenuManagerFindPluginCommandTest : public MenuManagerTest,
public ::testing::WithParamInterface<MenuTestCase> {
};
TEST_P(MenuManagerFindPluginCommandTest, testFindPluginCommand) {
MenuTestCase testCase = GetParam();
size_t foundMenuId = m_menuManager->findPluginCommand(testCase.menuName, testCase.optionName, false);
ASSERT_EQ(testCase.expectedID, foundMenuId);
}
INSTANTIATE_TEST_CASE_P(findPluginMenuCommand, MenuManagerFindPluginCommandTest,
::testing::Values(MenuTestCase(L"XML Tools", L"About", MENUID_XMLTOOLS_ABOUT), // First plugin, last option
MenuTestCase(L"Second Plugin", L"About", MENUID_SECONDPLUGIN_ABOUT), // Second plugin, repeated option name from other plugin
MenuTestCase(L"XML Tools", L"Recent 1", MENUID_XMLTOOLS_RECENT_1), // First plugin, repeated option name from main menu
MenuTestCase(L"Second Plugin", L"Recent 1", MENUID_SECONDPLUGIN_RECENT_1), // Second plugin, sub menu option
MenuTestCase(L"Second Plugin", L"Recent 2", MENUID_SECONDPLUGIN_RECENT_2), // Second plugin, sub menu option
MenuTestCase(L"Second Plugin", L"Not exists", 0), // Plugin option doesn't exist
MenuTestCase(L"New Plugin", L"Open", 0) // Plugin doesn't exist
));
}