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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
/*
-------------------------------------------------------------------------
CxxTest: A lightweight C++ unit testing library.
Copyright (c) 2008 Sandia Corporation.
This software is distributed under the LGPL License v3
For more information, see the COPYING file in the top CxxTest directory.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
*/
#ifndef __cxxtest__Win32Gui_h__
#define __cxxtest__Win32Gui_h__
//
// The Win32Gui displays a simple progress bar using the Win32 API.
//
// It accepts the following command line options:
// -minimized Start minimized, pop up on error
// -keep Don't close the window at the end
// -title TITLE Set the window caption
//
// If both -minimized and -keep are specified, GUI will only keep the
// window if it's in focus.
//
// N.B. If you're wondering why this class doesn't use any standard
// library or STL (<string> would have been nice) it's because it only
// uses "straight" Win32 API.
//
#include <cxxtest/Gui.h>
#include <windows.h>
#include <commctrl.h>
namespace CxxTest
{
class Win32Gui : public GuiListener
{
public:
void enterGui(int &argc, char **argv)
{
parseCommandLine(argc, argv);
}
void enterWorld(const WorldDescription &wd)
{
getTotalTests(wd);
_testsDone = 0;
startGuiThread();
}
void guiEnterSuite(const char *suiteName)
{
showSuiteName(suiteName);
reset(_suiteStart);
}
void guiEnterTest(const char *suiteName, const char *testName)
{
++ _testsDone;
setTestCaption(suiteName, testName);
showTestName(testName);
showTestsDone();
progressBarMessage(PBM_STEPIT);
reset(_testStart);
}
void yellowBar()
{
setColor(255, 255, 0);
setIcon(IDI_WARNING);
getTotalTests();
}
void redBar()
{
if (_startMinimized)
{
showMainWindow(SW_SHOWNORMAL);
}
setColor(255, 0, 0);
setIcon(IDI_ERROR);
getTotalTests();
}
void leaveGui()
{
if (keep())
{
showSummary();
WaitForSingleObject(_gui, INFINITE);
}
DestroyWindow(_mainWindow);
}
private:
const char *_title;
bool _startMinimized, _keep;
HANDLE _gui;
WNDCLASSEX _windowClass;
HWND _mainWindow, _progressBar, _statusBar;
HANDLE _canStartTests;
unsigned _numTotalTests, _testsDone;
char _strTotalTests[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
enum
{
STATUS_SUITE_NAME, STATUS_SUITE_TIME,
STATUS_TEST_NAME, STATUS_TEST_TIME,
STATUS_TESTS_DONE, STATUS_WORLD_TIME,
STATUS_TOTAL_PARTS
};
int _statusWidths[STATUS_TOTAL_PARTS];
unsigned _statusOffsets[STATUS_TOTAL_PARTS];
unsigned _statusTotal;
char _statusTestsDone[sizeof("1000000000 of (100%)") + WorldDescription::MAX_STRLEN_TOTAL_TESTS];
DWORD _worldStart, _suiteStart, _testStart;
char _timeString[sizeof("00:00:00")];
void parseCommandLine(int argc, char **argv)
{
_startMinimized = _keep = false;
_title = argv[0];
for (int i = 1; i < argc; ++ i)
{
if (!lstrcmpA(argv[i], "-minimized"))
{
_startMinimized = true;
}
else if (!lstrcmpA(argv[i], "-keep"))
{
_keep = true;
}
else if (!lstrcmpA(argv[i], "-title") && (i + 1 < argc))
{
_title = argv[++i];
}
}
}
void getTotalTests()
{
getTotalTests(tracker().world());
}
void getTotalTests(const WorldDescription &wd)
{
_numTotalTests = wd.numTotalTests();
wd.strTotalTests(_strTotalTests);
}
void startGuiThread()
{
_canStartTests = CreateEvent(NULL, TRUE, FALSE, NULL);
DWORD threadId;
_gui = CreateThread(NULL, 0, &(Win32Gui::guiThread), (LPVOID)this, 0, &threadId);
WaitForSingleObject(_canStartTests, INFINITE);
}
static DWORD WINAPI guiThread(LPVOID parameter)
{
((Win32Gui *)parameter)->gui();
return 0;
}
void gui()
{
registerWindowClass();
createMainWindow();
initCommonControls();
createProgressBar();
createStatusBar();
centerMainWindow();
showMainWindow();
startTimer();
startTests();
messageLoop();
}
void registerWindowClass()
{
_windowClass.cbSize = sizeof(_windowClass);
_windowClass.style = CS_HREDRAW | CS_VREDRAW;
_windowClass.lpfnWndProc = &(Win32Gui::windowProcedure);
_windowClass.cbClsExtra = 0;
_windowClass.cbWndExtra = sizeof(LONG);
_windowClass.hInstance = (HINSTANCE)NULL;
_windowClass.hIcon = (HICON)NULL;
_windowClass.hCursor = (HCURSOR)NULL;
_windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
_windowClass.lpszMenuName = NULL;
_windowClass.lpszClassName = TEXT("CxxTest Window Class");
_windowClass.hIconSm = (HICON)NULL;
RegisterClassEx(&_windowClass);
}
void createMainWindow()
{
_mainWindow = createWindow(_windowClass.lpszClassName, WS_OVERLAPPEDWINDOW);
}
void initCommonControls()
{
HMODULE dll = LoadLibraryA("comctl32.dll");
if (!dll) { return; }
typedef void (WINAPI * FUNC)(void);
FUNC func = (FUNC)GetProcAddress(dll, "InitCommonControls");
if (!func) { return; }
func();
}
void createProgressBar()
{
_progressBar = createWindow(PROGRESS_CLASS, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, _mainWindow);
#ifdef PBM_SETRANGE32
progressBarMessage(PBM_SETRANGE32, 0, _numTotalTests);
#else // No PBM_SETRANGE32, use PBM_SETRANGE
progressBarMessage(PBM_SETRANGE, 0, MAKELPARAM(0, (WORD)_numTotalTests));
#endif // PBM_SETRANGE32
progressBarMessage(PBM_SETPOS, 0);
progressBarMessage(PBM_SETSTEP, 1);
greenBar();
UpdateWindow(_progressBar);
}
void createStatusBar()
{
_statusBar = createWindow(STATUSCLASSNAME, WS_CHILD | WS_VISIBLE, _mainWindow);
setRatios(4, 1, 3, 1, 3, 1);
}
void setRatios(unsigned suiteNameRatio, unsigned suiteTimeRatio,
unsigned testNameRatio, unsigned testTimeRatio,
unsigned testsDoneRatio, unsigned worldTimeRatio)
{
_statusTotal = 0;
_statusOffsets[STATUS_SUITE_NAME] = (_statusTotal += suiteNameRatio);
_statusOffsets[STATUS_SUITE_TIME] = (_statusTotal += suiteTimeRatio);
_statusOffsets[STATUS_TEST_NAME] = (_statusTotal += testNameRatio);
_statusOffsets[STATUS_TEST_TIME] = (_statusTotal += testTimeRatio);
_statusOffsets[STATUS_TESTS_DONE] = (_statusTotal += testsDoneRatio);
_statusOffsets[STATUS_WORLD_TIME] = (_statusTotal += worldTimeRatio);
}
HWND createWindow(LPCTSTR className, DWORD style, HWND parent = (HWND)NULL)
{
return CreateWindow(className, NULL, style, 0, 0, 0, 0, parent,
(HMENU)NULL, (HINSTANCE)NULL, (LPVOID)this);
}
void progressBarMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
{
SendMessage(_progressBar, message, wParam, lParam);
}
void centerMainWindow()
{
RECT screen;
getScreenArea(screen);
LONG screenWidth = screen.right - screen.left;
LONG screenHeight = screen.bottom - screen.top;
LONG xCenter = (screen.right + screen.left) / 2;
LONG yCenter = (screen.bottom + screen.top) / 2;
LONG windowWidth = (screenWidth * 4) / 5;
LONG windowHeight = screenHeight / 10;
LONG minimumHeight = 2 * (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME));
if (windowHeight < minimumHeight)
{
windowHeight = minimumHeight;
}
SetWindowPos(_mainWindow, HWND_TOP,
xCenter - (windowWidth / 2), yCenter - (windowHeight / 2),
windowWidth, windowHeight, 0);
}
void getScreenArea(RECT &area)
{
if (!getScreenAreaWithoutTaskbar(area))
{
getWholeScreenArea(area);
}
}
bool getScreenAreaWithoutTaskbar(RECT &area)
{
return (SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &area, 0) != 0);
}
void getWholeScreenArea(RECT &area)
{
area.left = area.top = 0;
area.right = GetSystemMetrics(SM_CXSCREEN);
area.bottom = GetSystemMetrics(SM_CYSCREEN);
}
void showMainWindow()
{
showMainWindow(_startMinimized ? SW_MINIMIZE : SW_SHOWNORMAL);
UpdateWindow(_mainWindow);
}
void showMainWindow(int mode)
{
ShowWindow(_mainWindow, mode);
}
enum { TIMER_ID = 1, TIMER_DELAY = 1000 };
void startTimer()
{
reset(_worldStart);
reset(_suiteStart);
reset(_testStart);
SetTimer(_mainWindow, TIMER_ID, TIMER_DELAY, 0);
}
void reset(DWORD &tick)
{
tick = GetTickCount();
}
void startTests()
{
SetEvent(_canStartTests);
}
void messageLoop()
{
MSG message;
while (BOOL haveMessage = GetMessage(&message, NULL, 0, 0))
{
if (haveMessage != -1)
{
DispatchMessage(&message);
}
}
}
static LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_CREATE)
{
setUp(window, (LPCREATESTRUCT)lParam);
}
Win32Gui *that = (Win32Gui *)GetWindowLong(window, GWL_USERDATA);
return that->handle(window, message, wParam, lParam);
}
static void setUp(HWND window, LPCREATESTRUCT create)
{
SetWindowLong(window, GWL_USERDATA, (LONG)create->lpCreateParams);
}
LRESULT handle(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SIZE: resizeControls(); break;
case WM_TIMER: updateTime(); break;
case WM_CLOSE:
case WM_DESTROY:
case WM_QUIT:
ExitProcess(tracker().failedTests());
default: return DefWindowProc(window, message, wParam, lParam);
}
return 0;
}
void resizeControls()
{
RECT r;
GetClientRect(_mainWindow, &r);
LONG width = r.right - r.left;
LONG height = r.bottom - r.top;
GetClientRect(_statusBar, &r);
LONG statusHeight = r.bottom - r.top;
LONG resizeGripWidth = statusHeight;
LONG progressHeight = height - statusHeight;
SetWindowPos(_progressBar, HWND_TOP, 0, 0, width, progressHeight, 0);
SetWindowPos(_statusBar, HWND_TOP, 0, progressHeight, width, statusHeight, 0);
setStatusParts(width - resizeGripWidth);
}
void setStatusParts(LONG width)
{
for (unsigned i = 0; i < STATUS_TOTAL_PARTS; ++ i)
{
_statusWidths[i] = (width * _statusOffsets[i]) / _statusTotal;
}
statusBarMessage(SB_SETPARTS, STATUS_TOTAL_PARTS, _statusWidths);
}
void statusBarMessage(UINT message, WPARAM wParam = 0, const void *lParam = 0)
{
SendMessage(_statusBar, message, wParam, (LPARAM)lParam);
}
void greenBar()
{
setColor(0, 255, 0);
setIcon(IDI_INFORMATION);
}
#ifdef PBM_SETBARCOLOR
void setColor(BYTE red, BYTE green, BYTE blue)
{
progressBarMessage(PBM_SETBARCOLOR, 0, RGB(red, green, blue));
}
#else // !PBM_SETBARCOLOR
void setColor(BYTE, BYTE, BYTE)
{
}
#endif // PBM_SETBARCOLOR
void setIcon(LPCTSTR icon)
{
SendMessage(_mainWindow, WM_SETICON, ICON_BIG, (LPARAM)loadStandardIcon(icon));
}
HICON loadStandardIcon(LPCTSTR icon)
{
return LoadIcon((HINSTANCE)NULL, icon);
}
void setTestCaption(const char *suiteName, const char *testName)
{
setCaption(suiteName, "::", testName, "()");
}
void setCaption(const char *a = "", const char *b = "", const char *c = "", const char *d = "")
{
unsigned length = lstrlenA(_title) + sizeof(" - ") +
lstrlenA(a) + lstrlenA(b) + lstrlenA(c) + lstrlenA(d);
char *name = allocate(length);
lstrcpyA(name, _title);
lstrcatA(name, " - ");
lstrcatA(name, a);
lstrcatA(name, b);
lstrcatA(name, c);
lstrcatA(name, d);
SetWindowTextA(_mainWindow, name);
deallocate(name);
}
void showSuiteName(const char *suiteName)
{
setStatusPart(STATUS_SUITE_NAME, suiteName);
}
void showTestName(const char *testName)
{
setStatusPart(STATUS_TEST_NAME, testName);
}
void showTestsDone()
{
wsprintfA(_statusTestsDone, "%u of %s (%u%%)",
_testsDone, _strTotalTests,
(_testsDone * 100) / _numTotalTests);
setStatusPart(STATUS_TESTS_DONE, _statusTestsDone);
}
void updateTime()
{
setStatusTime(STATUS_WORLD_TIME, _worldStart);
setStatusTime(STATUS_SUITE_TIME, _suiteStart);
setStatusTime(STATUS_TEST_TIME, _testStart);
}
void setStatusTime(unsigned part, DWORD start)
{
unsigned total = (GetTickCount() - start) / 1000;
unsigned hours = total / 3600;
unsigned minutes = (total / 60) % 60;
unsigned seconds = total % 60;
if (hours)
{
wsprintfA(_timeString, "%u:%02u:%02u", hours, minutes, seconds);
}
else
{
wsprintfA(_timeString, "%02u:%02u", minutes, seconds);
}
setStatusPart(part, _timeString);
}
bool keep()
{
if (!_keep)
{
return false;
}
if (!_startMinimized)
{
return true;
}
return (_mainWindow == GetForegroundWindow());
}
void showSummary()
{
stopTimer();
setSummaryStatusBar();
setSummaryCaption();
}
void setStatusPart(unsigned part, const char *text)
{
statusBarMessage(SB_SETTEXTA, part, text);
}
void stopTimer()
{
KillTimer(_mainWindow, TIMER_ID);
setStatusTime(STATUS_WORLD_TIME, _worldStart);
}
void setSummaryStatusBar()
{
setRatios(0, 0, 0, 0, 1, 1);
resizeControls();
const char *tests = (_numTotalTests == 1) ? "test" : "tests";
if (tracker().failedTests())
{
wsprintfA(_statusTestsDone, "Failed %u of %s %s",
tracker().failedTests(), _strTotalTests, tests);
}
else
{
wsprintfA(_statusTestsDone, "%s %s passed", _strTotalTests, tests);
}
setStatusPart(STATUS_TESTS_DONE, _statusTestsDone);
}
void setSummaryCaption()
{
setCaption(_statusTestsDone);
}
char *allocate(unsigned length)
{
return (char *)HeapAlloc(GetProcessHeap(), 0, length);
}
void deallocate(char *data)
{
HeapFree(GetProcessHeap(), 0, data);
}
};
}
#endif // __cxxtest__Win32Gui_h__
|