1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * PURPOSE:         Test for NtUserSelectPalette
5  * PROGRAMMERS:
6  */
7 
8 #include <win32nt.h>
9 
10 FORCEINLINE
11 PALETTEENTRY
12 PALENTRY(BYTE r, BYTE g, BYTE b)
13 {
14 	PALETTEENTRY ret;
15 
16 	ret.peRed = r;
17 	ret.peGreen = g;
18 	ret.peBlue = b;
19 	ret.peFlags = 0;
20 	return ret;
21 }
22 
23 START_TEST(NtUserSelectPalette)
24 {
25     HINSTANCE hinst = GetModuleHandle(NULL);
26 	HPALETTE hPal, hOldPal;
27 	HWND hWnd;
28 	HDC hDC, hCompDC;
29 	struct
30 	{
31 		LOGPALETTE logpal;
32 		PALETTEENTRY entry[20];
33 	} pal;
34 
35 	ZeroMemory(&pal, sizeof(pal));
36 
37 	pal.logpal.palVersion = 0x300;
38 	pal.logpal.palNumEntries = 6;
39 	pal.entry[0] = PALENTRY(0,0,0);
40 	pal.entry[1] = PALENTRY(255,255,255);
41 	pal.entry[2] = PALENTRY(128,128,128);
42 	pal.entry[3] = PALENTRY(128,0,0);
43 	pal.entry[4] = PALENTRY(0,128,0);
44 	pal.entry[5] = PALENTRY(0,0,128);
45 
46 	hPal = CreatePalette(&pal.logpal);
47 	ASSERT(hPal);
48 	TEST(DeleteObject(hPal) == 1);
49 	hPal = CreatePalette(&pal.logpal);
50 	ASSERT(hPal);
51 
52 	/* Create a window */
53 	hWnd = CreateWindowW(L"BUTTON", L"TestWindow", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
54 	                    CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,
55 	                    NULL, NULL, hinst, 0);
56 	hDC = GetDC(hWnd);
57 	ASSERT(hDC);
58 	hCompDC = CreateCompatibleDC(hDC);
59 	ASSERT(hCompDC);
60 
61 	/* Test NULL DC */
62 	SetLastError(ERROR_SUCCESS);
63 	hOldPal = NtUserSelectPalette(NULL, hPal, 0);
64 	TEST(hOldPal == 0);
65 	TEST(GetLastError() == ERROR_SUCCESS);
66 
67 	/* Test invalid DC */
68 	SetLastError(ERROR_SUCCESS);
69 	hOldPal = NtUserSelectPalette((HDC)-1, hPal, 0);
70 	TEST(hOldPal == 0);
71 	TEST(GetLastError() == ERROR_SUCCESS);
72 
73 	/* Test NULL palette */
74 	SetLastError(ERROR_SUCCESS);
75 	hOldPal = NtUserSelectPalette(hDC, NULL, 0);
76 	TEST(hOldPal == 0);
77 	TEST(GetLastError() == ERROR_SUCCESS);
78 
79 	/* Test invalid palette */
80 	SetLastError(ERROR_SUCCESS);
81 	hOldPal = NtUserSelectPalette(hDC, (HPALETTE)-1, 0);
82 	TEST(hOldPal == 0);
83 	TEST(GetLastError() == ERROR_SUCCESS);
84 
85 	/* Test valid palette */
86 	hOldPal = NtUserSelectPalette(hDC, hPal, 0);
87 	TEST(hOldPal != 0);
88 	TEST(hOldPal == GetStockObject(DEFAULT_PALETTE));
89 
90 	/* We cannot Delete the palette */
91 	TEST(DeleteObject(hPal) == 0);
92 
93 	/* We can still select the Palette into a compatible DC */
94 	hOldPal = NtUserSelectPalette(hCompDC, hPal, 0);
95 	TEST(hOldPal != 0);
96 
97 
98 
99 #if 0
100 	RealizePalette(hDC);
101 
102 	GetClientRect(hWnd, &rect);
103 	FillRect(hDC, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
104 
105 	TEST(GetNearestColor(hDC, RGB(0,0,0)) == RGB(0,0,0));
106 	TEST(GetNearestColor(hDC, RGB(0,0,1)) == RGB(0,0,1));
107 
108 	ReleaseDC(hWnd, hDC);
109 	DestroyWindow(hWnd);
110 	RECT rect;
111 	HBITMAP hBmp;
112 
113 
114 	BITMAPINFOHEADER bmih = {sizeof(BITMAPINFOHEADER), // biSize
115 	                         3, // biWidth
116 	                         3, // biHeight
117 	                         1, // biPlanes
118 	                         8, // biBitCount
119 	                         BI_RGB, // biCompression
120 	                         0, // biSizeImage
121 	                         92, // biXPelsPerMeter
122 	                         92, // biYPelsPerMeter
123 	                         6,  // biClrUsed
124 	                         6}; // biClrImportant
125 	BYTE bits[3][3] = {{0,1,2},{3,4,5},{6,1,2}};
126 
127 	struct
128 	{
129 		BITMAPINFOHEADER bmih;
130 		RGBQUAD colors[6];
131 	} bmi = {{sizeof(BITMAPINFOHEADER),3,3,1,8,BI_RGB,0,92,92,6,6},
132 	                  {{0,0,0,0},{255,255,255,0},{255,0,0,0},
133 	                   {0,255,0,0},{0,0,255,0},{128,128,128,0}}};
134 
135 	hBmp = CreateDIBitmap(hCompDC, &bmih, CBM_INIT, &bits, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
136 	ASSERT(hBmp);
137 
138 	SetLastError(0);
139 	TEST(NtGdiSelectBitmap(hCompDC, hBmp));
140 	hOldPal = NtUserSelectPalette(hCompDC, hPal, 0);
141 	TEST(hOldPal != NULL);
142 	RealizePalette(hCompDC);
143 
144 	TEST(GetNearestColor(hCompDC, RGB(0,0,0)) == RGB(0,0,0));
145 	TEST(GetNearestColor(hCompDC, RGB(0,0,1)) == RGB(0,0,0));
146 	TEST(GetNearestColor(hCompDC, RGB(100,0,0)) == RGB(0,0,0));
147 	TEST(GetNearestColor(hCompDC, RGB(250,250,250)) == RGB(255,255,255));
148 	TEST(GetNearestColor(hCompDC, RGB(120,100,110)) == RGB(128,128,128));
149 
150 printf("nearest = 0x%x\n", GetNearestColor(hCompDC, RGB(120,100,110)));
151 #endif
152 
153 }
154