Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

513 lines
15 KiB

  1. //---------------------------------------------------------------------------
  2. // tests.cpp - tests for uxbud
  3. //---------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "uxbud.h"
  6. #include "tests.h"
  7. #include "winuserp.h"
  8. #include "wingdip.h"
  9. //---------------------------------------------------------------------------
  10. #define MAX_PRINT_FILE_SIZE 512
  11. //---------------------------------------------------------------------------
  12. int GetStockAvailCount()
  13. {
  14. //---- slow but better than nothing! ----
  15. int iCount=0;
  16. HANDLE *pHandles = new HANDLE[10000];
  17. if (pHandles)
  18. {
  19. //---- create a bunch of stock bitmaps ----
  20. while (1)
  21. {
  22. HBITMAP hBitmap = CreateBitmap(1, 1, 1, 24, NULL);
  23. if (! hBitmap)
  24. {
  25. MessageBox(NULL, L"CreateBitmap() failed", L"bummer, man!", MB_OK);
  26. break;
  27. }
  28. hBitmap = SetBitmapAttributes(hBitmap, SBA_STOCK);
  29. if (! hBitmap)
  30. {
  31. //---- finally used up all avail stock bitmaps ----
  32. break;
  33. }
  34. pHandles[iCount++] = hBitmap;
  35. }
  36. //---- free a bunch of stock bitmaps ----
  37. for (int i=0; i < iCount; i++)
  38. {
  39. HBITMAP hBitmap = ClearBitmapAttributes((HBITMAP)pHandles[i], SBA_STOCK);
  40. if (! hBitmap)
  41. {
  42. MessageBox(NULL, L"SetBitmapAttributes() failed to reset stock", L"bummer, man!", MB_OK);
  43. }
  44. else
  45. {
  46. DeleteObject(hBitmap);
  47. }
  48. }
  49. delete [] pHandles;
  50. }
  51. else
  52. {
  53. MessageBox(NULL, L"cannot allocate 10K handle array", L"bummer, man!", MB_OK);
  54. }
  55. return iCount;
  56. }
  57. //---------------------------------------------------------------------------
  58. BOOL ZapDir(LPCWSTR pszDirName)
  59. {
  60. //---- does this guy exist? ----
  61. DWORD dwMask = GetFileAttributes(pszDirName);
  62. BOOL fExists = (dwMask != 0xffffffff);
  63. if (! fExists)
  64. return TRUE; // not an error
  65. //---- delete all files or subdirs within the dir ----
  66. HANDLE hFile;
  67. WIN32_FIND_DATA wfd;
  68. BOOL bFile = TRUE;
  69. WCHAR szSearchPattern[MAX_PATH];
  70. StringCchPrintfW(szSearchPattern, ARRAYSIZE(szSearchPattern), L"%s\\*.*", pszDirName);
  71. for (hFile=FindFirstFile(szSearchPattern, &wfd); (hFile != INVALID_HANDLE_VALUE) && (bFile);
  72. bFile=FindNextFile(hFile, &wfd))
  73. {
  74. if ((lstrcmp(wfd.cFileName, TEXT("."))==0) || (lstrcmp(wfd.cFileName, TEXT(".."))==0))
  75. continue;
  76. WCHAR szFullName[MAX_PATH];
  77. StringCchPrintfW(szFullName, ARRAYSIZE(szFullName), L"%s\\%s", pszDirName, wfd.cFileName);
  78. if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  79. {
  80. if (! ZapDir(szFullName))
  81. return FALSE;
  82. }
  83. else
  84. {
  85. if (! DeleteFile(szFullName))
  86. return FALSE;
  87. }
  88. }
  89. FindClose(hFile);
  90. //---- this requires an empty dir ----
  91. return RemoveDirectory(pszDirName);
  92. }
  93. //---------------------------------------------------------------------------
  94. BOOL TestFile(LPCWSTR pszFileName)
  95. {
  96. DWORD dwMask = GetFileAttributes(pszFileName);
  97. BOOL fExists = (dwMask != 0xffffffff);
  98. Output(" TestFile(%S)=%s\n", pszFileName, (fExists) ? "true" : "false");
  99. return fExists;
  100. }
  101. //---------------------------------------------------------------------------
  102. BOOL PrintFileContents(LPCSTR pszTitle, LPCWSTR pszFileName)
  103. {
  104. HANDLE hFile = NULL;
  105. DWORD dw;
  106. CHAR szBuff[MAX_PRINT_FILE_SIZE];
  107. BOOL fRead = FALSE;
  108. //---- open files ----
  109. hFile = CreateFile(pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  110. if (hFile == INVALID_HANDLE_VALUE)
  111. goto exit;
  112. ReadFile(hFile, szBuff, MAX_PRINT_FILE_SIZE, &dw, NULL);
  113. if (! dw)
  114. goto exit;
  115. szBuff[dw] = 0; // null terminate string
  116. fRead = TRUE;
  117. Output(" %s: %s\n", pszTitle, szBuff);
  118. exit:
  119. Output(" PrintFileContents: %S (fRead=%d)\n", pszFileName, fRead);
  120. CloseHandle(hFile);
  121. return fRead;
  122. }
  123. //---------------------------------------------------------------------------
  124. BOOL ErrorTester(LPCSTR pszCallTitle, HRESULT hr)
  125. {
  126. WCHAR szErrBuff[2*MAX_PATH];
  127. HRESULT hr2;
  128. BOOL fGotMsg = FALSE;
  129. if (SUCCEEDED(hr))
  130. {
  131. //---- error - should have FAILED ----
  132. Output(" Error - %s Succeeded (expected error)\n");
  133. goto exit;
  134. }
  135. hr2 = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, 0, szErrBuff, ARRAYSIZE(szErrBuff), NULL);
  136. if (FAILED(hr2))
  137. {
  138. StringCchPrintfW(szErrBuff, ARRAYSIZE(szErrBuff), L"Cannot format error=0x%x (FormatError=0x%x)", hr, hr2);
  139. }
  140. Output(" %s returned hr=0x%x, error: %S\n", pszCallTitle, hr, szErrBuff);
  141. fGotMsg = TRUE;
  142. exit:
  143. return fGotMsg;
  144. }
  145. //---------------------------------------------------------------------------
  146. BOOL CompareFiles(LPCWSTR pszName1, LPCWSTR pszName2)
  147. {
  148. BOOL fSame = FileCompare(pszName1, pszName2);
  149. Output(" Compare(%S, %S) = %s\n", pszName1, pszName2,
  150. (fSame) ? "same" : "different");
  151. return fSame;
  152. }
  153. //---------------------------------------------------------------------------
  154. //---------------------------------------------------------------------------
  155. //---------------------------------------------------------------------------
  156. BOOL LoadTest()
  157. {
  158. Output("LoadTest\n");
  159. BOOL fPassed = FALSE;
  160. HRESULT hr = S_OK;
  161. //---- open and close a theme file a few times; ensure working set doesn't ----
  162. //---- grow much for this process or for theme service ----
  163. for (int i=0; i < 6; i++)
  164. {
  165. Output(" LoadTest: pass %d\n", i);
  166. //---- load the luna theme ----
  167. HTHEMEFILE hThemeFile;
  168. //---- use "luna.msstyles" ----
  169. WCHAR szName[MAX_PATH];
  170. GetWindowsDirectoryW(szName, MAX_PATH);
  171. StringCchCatW(szName, ARRAYSIZE(szName), L"\\resources\\themes\\luna\\luna.msstyles");
  172. //---- load for local (not global) use to avoid stock brush/bitmap issues ----
  173. HRESULT hr = OpenThemeFile(szName, NULL, NULL, &hThemeFile, FALSE);
  174. if (FAILED(hr))
  175. goto exit;
  176. hr = CloseThemeFile(hThemeFile);
  177. if (FAILED(hr))
  178. goto exit;
  179. }
  180. fPassed = TRUE;
  181. exit:
  182. return ReportResults(fPassed, hr, L"LoadTest");
  183. }
  184. //---------------------------------------------------------------------------
  185. BOOL ApplyTest()
  186. {
  187. BOOL fPassed = FALSE;
  188. HTHEMEFILE hThemeFile;
  189. WCHAR szName[MAX_PATH];
  190. HRESULT hr;
  191. Output("ApplyTest\n");
  192. //---- apply "classic", "luna", and "luna" themes a few times ----
  193. for (int i=0; i < 3; i++)
  194. {
  195. Output(" ApplyTest: pass %d\n", i);
  196. //---- apply "classic" ----
  197. Output(" Classic\n");
  198. ApplyTheme(NULL, 0, NULL);
  199. Sleep(500);
  200. //---- load LUNA theme ----
  201. Output(" LUNA\n");
  202. GetWindowsDirectoryW(szName, MAX_PATH);
  203. StringCchCatW(szName, ARRAYSIZE(szName), L"\\resources\\themes\\luna\\luna.msstyles");
  204. hr = SetSystemVisualStyle(szName, NULL, NULL, AT_LOAD_SYSMETRICS);
  205. if (FAILED(hr))
  206. goto exit;
  207. Output(" ApplyTest: after applying Luna, StockAvailCount=%d\n", GetStockAvailCount());
  208. }
  209. fPassed = TRUE;
  210. exit:
  211. return ReportResults(fPassed, hr, L"ApplyTest");
  212. }
  213. //---------------------------------------------------------------------------
  214. BOOL PackTest()
  215. {
  216. BOOL fPassed = FALSE;
  217. WCHAR szParams[512];
  218. WCHAR szWinDir[MAX_PATH];
  219. HRESULT hr = S_OK;
  220. Output("PackTest\n");
  221. //---- unpack luna.msstyles ----
  222. if (! ZapDir(L"luna"))
  223. goto exit;
  224. CreateDirectory(L"luna", NULL);
  225. GetWindowsDirectory(szWinDir, ARRAYSIZE(szWinDir));
  226. StringCchPrintfW(szParams, ARRAYSIZE(szParams), L"/a /u %s\\resources\\themes\\luna\\luna.msstyles", szWinDir);
  227. //---- run unpack in "luna" subdir ----
  228. SetCurrentDirectory(L"luna");
  229. BOOL fRunOk = RunCmd(L"packthem", szParams, TRUE, FALSE);
  230. SetCurrentDirectory(L"..");
  231. if (! fRunOk)
  232. goto exit;
  233. if (! TestFile(L"luna\\themes.ini"))
  234. goto exit;
  235. //---- pack it up ----
  236. if (! RunCmd(L"packthem", L"luna", TRUE, TRUE))
  237. goto exit;
  238. if (! TestFile(L"luna\\luna.msstyles"))
  239. goto exit;
  240. fPassed = TRUE;
  241. exit:
  242. return ReportResults(fPassed, hr, L"PackTest");
  243. }
  244. //---------------------------------------------------------------------------
  245. BOOL PackErrTest()
  246. {
  247. BOOL fPassed = FALSE;
  248. HRESULT hr = S_OK;
  249. Output("PackErrTest\n");
  250. //---- run packthem on dir with missing "themes.ini" file ----
  251. if (! ZapDir(L"TestTheme"))
  252. goto exit;
  253. CreateDirectory(L"TestTheme", NULL);
  254. if (! RunCmd(L"packthem", L"/e TestTheme", TRUE, TRUE))
  255. goto exit;
  256. if (! TestFile(L"packthem.err"))
  257. goto exit;
  258. if (! PrintFileContents("Packthem Missing File: ", L"packthem.err"))
  259. goto exit;
  260. //---- run packthem on dir with bad syntax "themes.ini" file ----
  261. CopyFile(L".\\TestTheme.ini", L".\\TestTheme\\themes.ini", TRUE);
  262. if (! RunCmd(L"packthem", L"/e TestTheme", TRUE, TRUE))
  263. goto exit;
  264. if (! TestFile(L"packthem.err"))
  265. goto exit;
  266. if (! PrintFileContents("Packthem Bad Syntax: ", L"packthem.err"))
  267. goto exit;
  268. fPassed = TRUE;
  269. exit:
  270. return ReportResults(fPassed, hr, L"PackErrTest");
  271. }
  272. //---------------------------------------------------------------------------
  273. BOOL ApiErrTest()
  274. {
  275. Output("ApiErrTest\n");
  276. BOOL fPassed = FALSE;
  277. WCHAR szErrBuff[2*MAX_PATH];
  278. COLORREF crValue;
  279. HRESULT hr;
  280. HTHEMEFILE hThemeFile;
  281. //---- GetThemeColor() with bad HTHEME ----
  282. hr = GetThemeColor(NULL, 1, 1, TMT_TEXTCOLOR, &crValue);
  283. ErrorTester("GetThemeColor()", hr);
  284. //---- OpenThemeFile() with corrupt file ----
  285. hr = OpenThemeFile(L"rcdll.dll", NULL, NULL, &hThemeFile, FALSE);
  286. ErrorTester("OpenThemeFile()", hr);
  287. fPassed = TRUE;
  288. return ReportResults(fPassed, hr, L"ApiErrTest");
  289. }
  290. //---------------------------------------------------------------------------
  291. BOOL ImageConTest()
  292. {
  293. BOOL fPassed = FALSE;
  294. HRESULT hr = S_OK;
  295. Output("ImageConTest\n");
  296. DeleteFile(L"image.bmp");
  297. if (! RunCmd(L"imagecon", L"image.png image.bmp", TRUE, TRUE))
  298. goto exit;
  299. if (! TestFile(L"image.bmp"))
  300. goto exit;
  301. fPassed = TRUE;
  302. exit:
  303. return ReportResults(fPassed, hr, L"ImageConTest");
  304. }
  305. //---------------------------------------------------------------------------
  306. BOOL BinaryTest()
  307. {
  308. BOOL fPassed = FALSE;
  309. BOOL fFailed = FALSE;
  310. Output("BinaryTest\n");
  311. //---- load the luna theme ----
  312. HTHEMEFILE hThemeFile;
  313. //---- use "profesional.msstyles" ----
  314. WCHAR szName[MAX_PATH];
  315. GetWindowsDirectoryW(szName, MAX_PATH);
  316. StringCchCatW(szName, ARRAYSIZE(szName), L"\\resources\\themes\\luna\\luna.msstyles");
  317. //---- load for local (not global) use to avoid stock brush/bitmap issues ----
  318. HRESULT hr = OpenThemeFile(szName, NULL, NULL, &hThemeFile, FALSE);
  319. if (FAILED(hr))
  320. {
  321. Output(" OpenThemeFile() failed with hr=0x%x\n", hr);
  322. goto exit;
  323. }
  324. //---- dump out the properties to "PropDump.txt" ----
  325. hr = DumpLoadedThemeToTextFile(hThemeFile, L"PropDump.txt", FALSE, FALSE);
  326. if (FAILED(hr))
  327. {
  328. Output(" DumpLoadedThemeToTextFile() failed with hr=0x%x\n", hr);
  329. goto exit;
  330. }
  331. //---- compare to known good file ----
  332. if (! CompareFiles(L"PropDump.ok", L"PropDump.txt"))
  333. fFailed = TRUE;
  334. //---- dump out the packed object to "ObjDump.txt" ----
  335. hr = DumpLoadedThemeToTextFile(hThemeFile, L"ObjDump.txt", TRUE, FALSE);
  336. if (FAILED(hr))
  337. {
  338. Output(" DumpLoadedThemeToTextFile() failed with hr=0x%x\n", hr);
  339. goto exit;
  340. }
  341. //---- compare to known good file ----
  342. if (! CompareFiles(L"ObjDump.ok", L"ObjDump.txt"))
  343. fFailed = TRUE;
  344. if (! fFailed)
  345. fPassed = TRUE;
  346. exit:
  347. return ReportResults(fPassed, hr, L"BinaryTest");
  348. }
  349. //---------------------------------------------------------------------------
  350. WCHAR *BitmapNames[] =
  351. {
  352. L"BorderFill",
  353. L"BorderFill-R",
  354. L"ImageFile",
  355. L"ImageFile-R",
  356. L"Glyph",
  357. L"Glyph-R",
  358. L"MultiImage",
  359. L"MultiImage-R",
  360. L"Text",
  361. L"Text-R",
  362. L"Borders",
  363. L"Borders-R",
  364. L"SourceSizing",
  365. L"SourceSizing-R",
  366. };
  367. //---------------------------------------------------------------------------
  368. BOOL DrawingTest()
  369. {
  370. BOOL fPassed = FALSE;
  371. BOOL fFailed = FALSE;
  372. HRESULT hr = S_OK;
  373. Output("DrawingTest\n");
  374. //---- run "clipper -c" to produce drawing bitmaps ----
  375. if (! RunCmd(L"clipper", L"-c", FALSE, TRUE))
  376. goto exit;
  377. //---- compare bitmaps to known good files ----
  378. int iCount = ARRAYSIZE(BitmapNames);
  379. for (int i=0; i < iCount; i++)
  380. {
  381. WCHAR szOkName[MAX_PATH];
  382. WCHAR szTestName[MAX_PATH];
  383. StringCchPrintfW(szOkName, ARRAYSIZE(szOkName), L"%s.bok", BitmapNames[i]);
  384. StringCchPrintfW(szTestName, ARRAYSIZE(szTestName), L"%s.bmp", BitmapNames[i]);
  385. if (! CompareFiles(szOkName, szTestName))
  386. fFailed = TRUE;
  387. }
  388. if (! fFailed)
  389. fPassed = TRUE;
  390. exit:
  391. return ReportResults(fPassed, hr, L"DrawingTest");
  392. }
  393. //---------------------------------------------------------------------------
  394. TESTINFO TestInfo[] =
  395. {
  396. {DrawingTest, "drawing", "test out low level drawing"},
  397. {PackTest, "pack", "test out theme file packing & unpacking"},
  398. {PackErrTest, "packerr", "test out err msgs from theme file packing"},
  399. {BinaryTest, "binary", "dump out text from binary theme data"},
  400. {LoadTest, "load", "test loading and unloading of theme files"},
  401. {ApplyTest, "apply", "test global loading & setting of themes"},
  402. {ApiErrTest, "apierr", "test err msgs from api calls"},
  403. //{ApiTest, "api", "test uxtheme public api"},
  404. //{PrivateTest, "private", "test private api calls"},
  405. {ImageConTest, "imagecon", "test out theme file packing & unpacking"},
  406. };
  407. //---------------------------------------------------------------------------
  408. BOOL GetTestInfo(TESTINFO **ppTestInfo, int *piCount)
  409. {
  410. *ppTestInfo = TestInfo;
  411. *piCount = ARRAYSIZE(TestInfo);
  412. return TRUE;
  413. }
  414. //---------------------------------------------------------------------------