1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * PURPOSE:         Test for NtGdiDeleteObjectApp
5  * PROGRAMMERS:
6  */
7 
8 #include <win32nt.h>
9 
10 START_TEST(NtGdiDeleteObjectApp)
11 {
12     HDC hdc;
13     HBITMAP hbmp;
14     HBRUSH hbrush;
15     HPEN hpen;
16 
17     /* Try to delete 0 */
18     SetLastError(0);
19     TEST(NtGdiDeleteObjectApp(0) == 0);
20     TEST(GetLastError() == 0);
21 
22     /* Try to delete something with a stockbit */
23     SetLastError(0);
24     TEST(NtGdiDeleteObjectApp((PVOID)(GDI_HANDLE_STOCK_MASK | 0x1234)) == 1);
25     TEST(GetLastError() == 0);
26 
27     /* Delete a compatible DC */
28     SetLastError(0);
29     hdc = CreateCompatibleDC(NULL);
30     ASSERT(GdiIsHandleValid(hdc) == 1);
31     TEST(NtGdiDeleteObjectApp(hdc) == 1);
32     TEST(GetLastError() == 0);
33     TEST(GdiIsHandleValid(hdc) == 0);
34 
35     /* Delete a display DC */
36     SetLastError(0);
37     hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
38     ASSERT(GdiIsHandleValid(hdc) == 1);
39     TEST((hpen=SelectObject(hdc, GetStockObject(WHITE_PEN))) != NULL);
40     SelectObject(hdc, hpen);
41     TEST(NtGdiDeleteObjectApp(hdc) != 0);
42     TEST(GetLastError() == 0);
43     TEST(GdiIsHandleValid(hdc) == 1);
44     TEST(SelectObject(hdc, GetStockObject(WHITE_PEN)) == NULL);
45     TESTX(GetLastError() == ERROR_INVALID_PARAMETER, "GetLasterror returned 0x%08x\n", (unsigned int)GetLastError());
46 
47     /* Once more */
48     SetLastError(0);
49     hdc = GetDC(0);
50     ASSERT(GdiIsHandleValid(hdc) == 1);
51     TEST(NtGdiDeleteObjectApp(hdc) != 0);
52     TEST(GetLastError() == 0);
53     TEST(GdiIsHandleValid(hdc) == 1);
54     TEST(SelectObject(hdc, GetStockObject(WHITE_PEN)) == NULL);
55     TESTX(GetLastError() == ERROR_INVALID_PARAMETER, "GetLasterror returned 0x%08x\n", (unsigned int)GetLastError());
56     /* Make sure */
57     TEST(NtUserCallOneParam((DWORD_PTR)hdc, ONEPARAM_ROUTINE_RELEASEDC) == 0);
58 
59     /* Delete a brush */
60     SetLastError(0);
61     hbrush = CreateSolidBrush(0x123456);
62     ASSERT(GdiIsHandleValid(hbrush) == 1);
63     TEST(NtGdiDeleteObjectApp(hbrush) == 1);
64     TEST(GetLastError() == 0);
65     TEST(GdiIsHandleValid(hbrush) == 0);
66 
67     /* Try to delete a stock brush */
68     SetLastError(0);
69     hbrush = GetStockObject(BLACK_BRUSH);
70     ASSERT(GdiIsHandleValid(hbrush) == 1);
71     TEST(NtGdiDeleteObjectApp(hbrush) == 1);
72     TEST(GetLastError() == 0);
73     TEST(GdiIsHandleValid(hbrush) == 1);
74 
75     /* Delete a bitmap */
76     SetLastError(0);
77     hbmp = CreateBitmap(10, 10, 1, 1, NULL);
78     ASSERT(GdiIsHandleValid(hbmp) == 1);
79     TEST(NtGdiDeleteObjectApp(hbmp) == 1);
80     TEST(GetLastError() == 0);
81     TEST(GdiIsHandleValid(hbmp) == 0);
82 
83     /* Create a DC for further use */
84     hdc = CreateCompatibleDC(NULL);
85     ASSERT(hdc);
86 
87     /* Try to delete a brush that is selected into a DC */
88     SetLastError(0);
89     hbrush = CreateSolidBrush(0x123456);
90     ASSERT(GdiIsHandleValid(hbrush) == 1);
91     TEST(NtGdiSelectBrush(hdc, hbrush) != NULL);
92     TEST(NtGdiDeleteObjectApp(hbrush) == 1);
93     TEST(GetLastError() == 0);
94     TEST(GdiIsHandleValid(hbrush) == 1);
95 
96     /* Try to delete a bitmap that is selected into a DC */
97     SetLastError(0);
98     hbmp = CreateBitmap(10, 10, 1, 1, NULL);
99     ASSERT(GdiIsHandleValid(hbmp) == 1);
100     TEST(NtGdiSelectBitmap(hdc, hbmp) != NULL);
101 
102     TEST(NtGdiDeleteObjectApp(hbmp) == 1);
103     TEST(GetLastError() == 0);
104     TEST(GdiIsHandleValid(hbmp) == 1);
105 
106     /* Bitmap get's deleted as soon as we dereference it */
107     NtGdiSelectBitmap(hdc, GetStockObject(DEFAULT_BITMAP));
108     TEST(GdiIsHandleValid(hbmp) == 0);
109 
110     TEST(NtGdiDeleteObjectApp(hbmp) == 1);
111     TEST(GetLastError() == 0);
112     TEST(GdiIsHandleValid(hbmp) == 0);
113 
114     /* Try to delete a brush that is selected into a DC */
115     SetLastError(0);
116     hbrush = CreateSolidBrush(123);
117     ASSERT(GdiIsHandleValid(hbrush) == 1);
118     TEST(NtGdiSelectBrush(hdc, hbrush) != NULL);
119 
120     TEST(NtGdiDeleteObjectApp(hbrush) == 1);
121     TEST(GetLastError() == 0);
122     TEST(GdiIsHandleValid(hbrush) == 1);
123 }
124