1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for NtGdiRestoreDC
5 * PROGRAMMERS:
6 */
7
8 #include "../win32nt.h"
9
10 static HBRUSH hbrush;
11 static HBITMAP hbitmap;
12 static HPEN hpen;
13 static HFONT hfont;
14 static HRGN hrgn, hrgn2;
15
16 static
17 void
SetSpecialDCState(HDC hdc)18 SetSpecialDCState(HDC hdc)
19 {
20 /* Select spcial Objects */
21 SelectObject(hdc, hbrush);
22 SelectObject(hdc, hpen);
23 SelectObject(hdc, hbitmap);
24 SelectObject(hdc, hfont);
25 SelectObject(hdc, hrgn);
26
27 /* Colors */
28 SetDCBrushColor(hdc, RGB(12,34,56));
29 SetDCPenColor(hdc, RGB(23,34,45));
30
31 /* Coordinates */
32 SetMapMode(hdc, MM_ANISOTROPIC);
33 SetGraphicsMode(hdc, GM_ADVANCED);
34 SetWindowOrgEx(hdc, 12, 34, NULL);
35 SetViewportOrgEx(hdc, 56, 78, NULL);
36 SetWindowExtEx(hdc, 123, 456, NULL);
37 SetViewportExtEx(hdc, 234, 567, NULL);
38
39
40
41 }
42
43 static
44 void
SetSpecialDCState2(HDC hdc)45 SetSpecialDCState2(HDC hdc)
46 {
47 /* Select spcial Objects */
48 SelectObject(hdc, GetStockObject(DC_BRUSH));
49 SelectObject(hdc, GetStockObject(DC_PEN));
50 SelectObject(hdc, GetStockObject(DEFAULT_BITMAP));
51 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
52 SelectObject(hdc, hrgn2);
53
54 /* Colors */
55 SetDCBrushColor(hdc, RGB(65,43,21));
56 SetDCPenColor(hdc, RGB(54,43,32));
57
58 /* Coordinates */
59 SetMapMode(hdc, MM_ISOTROPIC);
60 SetGraphicsMode(hdc, GM_COMPATIBLE);
61 SetWindowOrgEx(hdc, 43, 21, NULL);
62 SetViewportOrgEx(hdc, 87, 65, NULL);
63 SetWindowExtEx(hdc, 654, 321, NULL);
64 SetViewportExtEx(hdc, 765, 432, NULL);
65
66
67 }
68
69 static
70 void
Test_IsSpecialState(HDC hdc,BOOL bMemDC)71 Test_IsSpecialState(HDC hdc, BOOL bMemDC)
72 {
73 POINT pt;
74 SIZE sz;
75
76 /* Test Objects */
77 TEST(SelectObject(hdc, GetStockObject(DC_BRUSH)) == hbrush);
78 TEST(SelectObject(hdc, GetStockObject(DC_PEN)) == hpen);
79 TEST(SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT)) == hfont);
80 if (bMemDC)
81 {
82 TEST(SelectObject(hdc, GetStockObject(DEFAULT_BITMAP)) == hbitmap);
83 TEST(SelectObject(hdc, hrgn2) == (PVOID)1);
84 }
85 else
86 {
87 TEST(SelectObject(hdc, GetStockObject(DEFAULT_BITMAP)) == 0);
88 TEST(SelectObject(hdc, hrgn2) == (PVOID)2);
89 }
90
91 /* Test colors */
92 TEST(GetDCBrushColor(hdc) == RGB(12,34,56));
93 TEST(GetDCPenColor(hdc) == RGB(23,34,45));
94
95 /* Test coordinates */
96 TEST(GetMapMode(hdc) == MM_ANISOTROPIC);
97 TEST(GetGraphicsMode(hdc) == GM_ADVANCED);
98 GetWindowOrgEx(hdc, &pt);
99 TEST(pt.x == 12);
100 TEST(pt.y == 34);
101 GetViewportOrgEx(hdc, &pt);
102 TEST(pt.x == 56);
103 TEST(pt.y == 78);
104 GetWindowExtEx(hdc, &sz);
105 ok(sz.cx == 123, "sz.cx == %ld\n", sz.cx);
106 ok(sz.cy == 456, "sz.cy == %ld\n", sz.cy);
107 GetViewportExtEx(hdc, &sz);
108 TEST(sz.cx == 234);
109 TEST(sz.cy == 567);
110
111
112 }
113
114
115 static
116 void
Test_SaveRestore(HDC hdc,BOOL bMemDC)117 Test_SaveRestore(HDC hdc, BOOL bMemDC)
118 {
119 SetSpecialDCState(hdc);
120 NtGdiSaveDC(hdc);
121 SetSpecialDCState2(hdc);
122
123 SetLastError(0);
124 TEST(NtGdiRestoreDC(hdc, 2) == 0);
125 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
126
127 SetLastError(0);
128 TEST(NtGdiRestoreDC(hdc, 0) == 0);
129 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
130
131 SetLastError(0);
132 TEST(NtGdiRestoreDC(hdc, -2) == 0);
133 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
134
135 SetLastError(0);
136 TEST(NtGdiRestoreDC(hdc, 1) == 1);
137 TEST(GetLastError() == 0);
138
139 Test_IsSpecialState(hdc, bMemDC);
140 }
141
142
START_TEST(NtGdiRestoreDC)143 START_TEST(NtGdiRestoreDC)
144 {
145 HDC hdc;
146
147 hdc = CreateCompatibleDC(0);
148 ASSERT(GdiIsHandleValid(hdc));
149
150 SetLastError(0);
151 TEST(NtGdiRestoreDC(0, -10) == 0);
152 TEST(GetLastError() == ERROR_INVALID_HANDLE);
153
154 SetLastError(0);
155 TEST(NtGdiRestoreDC(hdc, 0) == 0);
156 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
157
158 SetLastError(0);
159 TEST(NtGdiRestoreDC(hdc, 1) == 0);
160 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
161
162 /* Initialize objects */
163 hbrush = CreateSolidBrush(12345);
164 ASSERT(GdiIsHandleValid(hbrush));
165 hpen = CreatePen(PS_SOLID, 4, RGB(10,12,32));
166 ASSERT(GdiIsHandleValid(hpen));
167 hbitmap = CreateBitmap(10, 10, 1, 1, NULL);
168 ASSERT(GdiIsHandleValid(hbitmap));
169 hfont = CreateFont(10, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET,
170 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
171 DEFAULT_PITCH, "Arial");
172 ASSERT(GdiIsHandleValid(hfont));
173 hrgn = CreateRectRgn(12, 14, 14, 17);
174 ASSERT(GdiIsHandleValid(hrgn));
175 hrgn2 = CreateRectRgn(1, 1, 2, 2);
176 ASSERT(GdiIsHandleValid(hrgn2));
177
178 /* Test mem dc */
179 Test_SaveRestore(hdc, TRUE);
180 DeleteDC(hdc);
181
182 /* Test screen DC */
183 hdc = GetDC(0);
184 ASSERT(GdiIsHandleValid(hdc));
185 Test_SaveRestore(hdc, FALSE);
186 ReleaseDC(0, hdc);
187
188 /* Test info dc */
189 hdc = CreateICW(L"DISPLAY", NULL, NULL, NULL);
190 ASSERT(GdiIsHandleValid(hdc));
191 Test_SaveRestore(hdc, FALSE);
192 DeleteDC(hdc);
193
194 }
195
196