1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * PURPOSE:         Test for CreateBitmap
5  * PROGRAMMERS:     Timo Kreuzer
6  */
7 
8 #include "precomp.h"
9 
10 DWORD WINAPI GetBitmapAttributes(HBITMAP hbm);
11 HBITMAP WINAPI SetBitmapAttributes(HBITMAP hbm, DWORD dwFlags);
12 HBITMAP WINAPI ClearBitmapAttributes(HBITMAP hbm, DWORD dwFlags);
13 
14 void Test_CreateBitmap_Params()
15 {
16     HBITMAP hbmp;
17 
18     /* All of these should get us the default bitmap */
19     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
20     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
21     hbmp = CreateBitmap(1, 0, 0, 0, NULL);
22     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
23     hbmp = CreateBitmap(0, 1, 0, 0, NULL);
24     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
25     hbmp = CreateBitmap(0, 1, 1, 0, NULL);
26     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
27     hbmp = CreateBitmap(0, 1, 63, 33, NULL);
28     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
29     hbmp = CreateBitmap(0, -4, -32, 233, NULL);
30     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
31 
32     SetLastError(0);
33     hbmp = CreateBitmap(1, -1, 1, 0, NULL);
34     ok(hbmp == 0, "CreateBitmap should fail\n");
35     ok_err(ERROR_INVALID_PARAMETER);
36 
37     SetLastError(0);
38     hbmp = CreateBitmap(-1, 1, 1, 0, NULL);
39     ok(hbmp == 0, "CreateBitmap should fail\n");
40     ok_err(ERROR_INVALID_PARAMETER);
41 
42     SetLastError(0);
43     hbmp = CreateBitmap(-1, 1, 1, 1, NULL);
44     ok(hbmp == 0, "CreateBitmap should fail\n");
45     ok_err(ERROR_INVALID_PARAMETER);
46 
47     SetLastError(0);
48     hbmp = CreateBitmap(1, -1, 1, 1, NULL);
49     ok(hbmp == 0, "CreateBitmap should fail\n");
50     ok_err(ERROR_INVALID_PARAMETER);
51 
52     /* Check if an overflow in cPlanes * cBitsPixel is handled */
53     SetLastError(0);
54     hbmp = CreateBitmap(1, 1, 2, 0x80000004, NULL);
55     ok(hbmp == 0, "CreateBitmap should fail\n");
56     ok_err(ERROR_INVALID_PARAMETER);
57 
58     /* Check for maximum width */
59     hbmp = CreateBitmap(0x7FFFFFF, 1, 1, 1, NULL);
60     ok(hbmp != 0, "CreateBitmap failed\n");
61     DeleteObject(hbmp);
62 
63     SetLastError(0);
64     hbmp = CreateBitmap(0x8000000, 1, 1, 1, NULL);
65     ok(hbmp == 0, "CreateBitmap should fail\n");
66     ok_err(ERROR_INVALID_PARAMETER);
67 
68     /* Check for maximum height */
69     hbmp = CreateBitmap(1, 0x1FFFFF00, 1, 1, NULL);
70     //ok(hbmp != 0, "\n"); // fails on windows 2003
71     DeleteObject(hbmp);
72 
73     SetLastError(0);
74     hbmp = CreateBitmap(1, 0x1FFFFFFF, 1, 1, NULL);
75     ok(hbmp == 0, "CreateBitmap should fail\n");
76     ok_err(0);
77 
78     SetLastError(0);
79     hbmp = CreateBitmap(1, -1, 1, 1, NULL);
80     ok(hbmp == 0, "CreateBitmap should fail\n");
81     ok_err(ERROR_INVALID_PARAMETER);
82 
83     /* Test huge allocation (256 GB) */
84     SetLastError(0);
85     hbmp = CreateBitmap(0x40000, 0x40000, 32, 1, NULL);
86     ok(hbmp == 0, "CreateBitmap should fail\n");
87     ok_err(ERROR_INVALID_PARAMETER);
88 
89     /* Test planes / bpp */
90     hbmp = CreateBitmap(10, 10, 32, 1, NULL);
91     ok(hbmp != 0, "CreateBitmap failed\n");
92     DeleteObject(hbmp);
93     hbmp = CreateBitmap(10, 10, 5, 5, NULL);
94     ok(hbmp != 0, "CreateBitmap failed\n");
95     DeleteObject(hbmp);
96 
97     SetLastError(0);
98     hbmp = CreateBitmap(10, 10, 33, 1, NULL);
99     ok(hbmp == 0, "CreateBitmap should fail\n");
100     ok_err(ERROR_INVALID_PARAMETER);
101 
102     SetLastError(0);
103     hbmp = CreateBitmap(10, 10, 1, 33, NULL);
104     ok(hbmp == 0, "CreateBitmap should fail\n");
105     ok_err(ERROR_INVALID_PARAMETER);
106 
107     SetLastError(0);
108     hbmp = CreateBitmap(10, 10, 6, 6, NULL);
109     ok(hbmp == 0, "CreateBitmap should fail\n");
110     ok_err(ERROR_INVALID_PARAMETER);
111 
112     SetLastError(0);
113     hbmp = CreateBitmap(10, 10, 8, 8, NULL);
114     ok(hbmp == 0, "CreateBitmap should fail\n");
115     ok_err(ERROR_INVALID_PARAMETER);
116 
117 }
118 
119 void Test_CreateBitmap()
120 {
121     HBITMAP hbmp;
122     BITMAP bitmap;
123     ULONG cjWidthBytes, cBitsPixel, cExpectedBitsPixel;
124 
125     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
126     ok(hbmp != 0, "should get a 1x1 bitmap\n");
127     ok(hbmp == GetStockObject(DEFAULT_BITMAP), "\n");
128     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
129     ok_int(bitmap.bmType, 0);
130     ok_int(bitmap.bmWidth, 1);
131     ok_int(bitmap.bmHeight, 1);
132     ok_int(bitmap.bmWidthBytes, 2);
133     ok_int(bitmap.bmPlanes, 1);
134     ok_int(bitmap.bmBitsPixel, 1);
135     ok_ptr(bitmap.bmBits, 0);
136     DeleteObject(hbmp);
137 
138     /* Above 32 bpp should fail */
139     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
140     ok(hbmp == 0, "should fail\n");
141 
142     hbmp = CreateBitmap(1, 2, 1, 1, NULL);
143     ok(hbmp != 0, "should get a 1x2 bitmap\n");
144     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
145     ok_int(bitmap.bmType, 0);
146     ok_int(bitmap.bmWidth, 1);
147     ok_int(bitmap.bmHeight, 2);
148     ok_int(bitmap.bmWidthBytes, 2);
149     ok_int(bitmap.bmPlanes, 1);
150     ok_int(bitmap.bmBitsPixel, 1);
151     ok_ptr(bitmap.bmBits, 0);
152     DeleteObject(hbmp);
153 
154     for (cBitsPixel = 0; cBitsPixel <= 32; cBitsPixel++)
155     {
156         /* CreateBitmap API accepts any number as BitsPixels param.
157            but it can only create 1, 4, 8, 16, 24, 32 bpp bitmaps */
158         if (cBitsPixel <= 1)       cExpectedBitsPixel = 1;
159         else if (cBitsPixel <= 4)  cExpectedBitsPixel = 4;
160         else if (cBitsPixel <= 8)  cExpectedBitsPixel = 8;
161         else if (cBitsPixel <= 16) cExpectedBitsPixel = 16;
162         else if (cBitsPixel <= 24) cExpectedBitsPixel = 24;
163         else if (cBitsPixel <= 32) cExpectedBitsPixel = 32;
164 
165         hbmp = CreateBitmap(1, 2, 1, cBitsPixel, NULL);
166         ok(hbmp != 0, "should get a 1x2 bitmap %ld\n", cBitsPixel);
167         ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
168 
169         /* Calculate expected line width */
170         cjWidthBytes = ((bitmap.bmWidth * bitmap.bmBitsPixel + 15) & ~15) >> 3;
171 
172         ok_int(bitmap.bmType, 0);
173         ok_int(bitmap.bmWidth, 1);
174         ok_int(bitmap.bmHeight, 2);
175         ok_int(bitmap.bmPlanes, 1);
176         ok_int(bitmap.bmBitsPixel, cExpectedBitsPixel);
177         ok_int(bitmap.bmWidthBytes, cjWidthBytes);
178         ok_ptr(bitmap.bmBits, 0);
179         DeleteObject(hbmp);
180     }
181 
182     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
183     ok(hbmp == 0, "Expected failure for 33 bpp\n");
184 }
185 
186 void Test_BitmapAttributes()
187 {
188     static const WORD pattern_bits[] = { 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa };
189     HBITMAP hbmp,hbmpso,hbmpsoc;
190     INT ret;
191     BITMAP bm;
192 
193     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
194     ok(hbmp != 0, "should get a 1x1 bitmap\n");
195     ok(hbmp == GetStockObject(DEFAULT_BITMAP), "\n");
196     ok(GetBitmapAttributes(hbmp) == 1,"\n");
197 
198     ok(SetBitmapAttributes(hbmp, 0x00000002) == 0,"\n");
199     ok(SetBitmapAttributes(hbmp, 0x00000004) == 0,"\n");
200     ok(SetBitmapAttributes(hbmp, 0x00000008) == 0,"\n");
201     ok(SetBitmapAttributes(hbmp, 0x00000010) == 0,"\n");
202     ok(SetBitmapAttributes(hbmp, 0x00000020) == 0,"\n");
203     ok(SetBitmapAttributes(hbmp, 0x00000040) == 0,"\n");
204     ok(SetBitmapAttributes(hbmp, 0x00000080) == 0,"\n");
205     ok(SetBitmapAttributes(hbmp, 0x00000100) == 0,"\n");
206     ok(SetBitmapAttributes(hbmp, 0x00000200) == 0,"\n");
207     ok(SetBitmapAttributes(hbmp, 0x00000400) == 0,"\n");
208     ok(SetBitmapAttributes(hbmp, 0x00000800) == 0,"\n");
209     ok(SetBitmapAttributes(hbmp, 0xFFFFFFFF) == 0,"\n");
210     ok(ClearBitmapAttributes(hbmp, 0x00000002) == 0,"\n");
211     ok(ClearBitmapAttributes(hbmp, 0x00000004) == 0,"\n");
212     ok(ClearBitmapAttributes(hbmp, 0x00000008) == 0,"\n");
213     ok(ClearBitmapAttributes(hbmp, 0x00000010) == 0,"\n");
214     ok(ClearBitmapAttributes(hbmp, 0x00000020) == 0,"\n");
215     ok(ClearBitmapAttributes(hbmp, 0x00000040) == 0,"\n");
216     ok(ClearBitmapAttributes(hbmp, 0x00000080) == 0,"\n");
217     ok(ClearBitmapAttributes(hbmp, 0x00000100) == 0,"\n");
218     ok(ClearBitmapAttributes(hbmp, 0x00000200) == 0,"\n");
219     ok(ClearBitmapAttributes(hbmp, 0x00000400) == 0,"\n");
220     ok(ClearBitmapAttributes(hbmp, 0x00000800) == 0,"\n");
221     ok(ClearBitmapAttributes(hbmp, 0xFFFFFFFF) == 0,"\n");
222     DeleteObject(hbmp);
223 
224     hbmp = CreateBitmap( 16, 8, 1, 1, pattern_bits );
225     ok(hbmp != 0, "should get a pattern bitmap\n");
226     ok(GetBitmapAttributes(hbmp) == 0,"\n");
227 
228     hbmpso = SetBitmapAttributes(hbmp, 1);
229     ok(hbmpso != 0, "should get stock pattern bitmap\n");
230     ok(GetBitmapAttributes(hbmpso) == 1,"\n");
231     ok(hbmpso != hbmp,"\n");
232 
233     DeleteObject(hbmpso);
234     ret = GetObjectW(hbmpso, sizeof(bm), &bm);
235     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
236 
237     hbmpsoc = ClearBitmapAttributes(hbmpso, 1);
238     ok(hbmpsoc != 0, "should get pattern bitmap\n");
239     ok(GetBitmapAttributes(hbmp) == 0,"\n");
240     ok(hbmpsoc == hbmp,"\n");
241 
242     DeleteObject(hbmp);
243     ret = GetObjectW(hbmp, sizeof(bm), &bm);
244     ok(ret == 0, "GetObject returned %d\n", ret);
245 }
246 
247 START_TEST(CreateBitmap)
248 {
249     Test_CreateBitmap_Params();
250     Test_CreateBitmap();
251     Test_BitmapAttributes();
252 }
253