mshtml: Added IHTMLDOMNode::insertBefore implementation.
[wine] / dlls / user32 / tests / text.c
1 /*
2  * DrawText tests
3  *
4  * Copyright (c) 2004 Zach Gorman
5  * Copyright 2007 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23
24 #include "wine/test.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
29
30
31 static void test_DrawTextCalcRect(void)
32 {
33     HWND hwnd;
34     HDC hdc;
35     HFONT hFont, hOldFont;
36     LOGFONTA lf;
37     static CHAR text[] = "Example text for testing DrawText in "
38       "MM_HIENGLISH mode";
39     static WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
40         's','t','r','i','n','g','\0'};
41     static CHAR emptystring[] = "";
42     static WCHAR emptystringW[] = { 0 };
43     INT textlen, textheight;
44     RECT rect = { 0, 0, 100, 0 };
45     BOOL ret;
46
47     /* Initialization */
48     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
49                            0, 0, 200, 200, 0, 0, 0, NULL);
50     ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
51     hdc = GetDC(hwnd);
52     ok(hdc != 0, "GetDC error %u\n", GetLastError());
53     trace("hdc %p\n", hdc);
54     textlen = lstrlenA(text);
55
56     /* LOGFONT initialization */
57     memset(&lf, 0, sizeof(lf));
58     lf.lfCharSet = ANSI_CHARSET;
59     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
60     lf.lfWeight = FW_DONTCARE;
61     lf.lfHeight = 0; /* mapping mode dependent */
62     lf.lfQuality = DEFAULT_QUALITY;
63     lstrcpyA(lf.lfFaceName, "Arial");
64
65     /* DrawText in MM_HIENGLISH with DT_CALCRECT */
66     SetMapMode(hdc, MM_HIENGLISH);
67     lf.lfHeight = 100 * 9 / 72; /* 9 point */
68     hFont = CreateFontIndirectA(&lf);
69     ok(hFont != 0, "CreateFontIndirectA error %u\n",
70        GetLastError());
71     hOldFont = SelectObject(hdc, hFont);
72
73     textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
74        DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
75        DT_NOPREFIX);
76     ok( textheight, "DrawTextA error %u\n", GetLastError());
77
78     trace("MM_HIENGLISH rect.bottom %d\n", rect.bottom);
79     todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
80        "DT_CALCRECT should return a negative rectangle bottom. "
81        "(bot=%d)\n", rect.bottom);
82
83     SelectObject(hdc, hOldFont);
84     ret = DeleteObject(hFont);
85     ok( ret, "DeleteObject error %u\n", GetLastError());
86
87
88     /* DrawText in MM_TEXT with DT_CALCRECT */
89     SetMapMode(hdc, MM_TEXT);
90     lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
91        LOGPIXELSY), 72); /* 9 point */
92     hFont = CreateFontIndirectA(&lf);
93     ok(hFont != 0, "CreateFontIndirectA error %u\n",
94        GetLastError());
95     hOldFont = SelectObject(hdc, hFont);
96
97     textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
98        DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
99        DT_NOPREFIX);
100     ok( textheight, "DrawTextA error %u\n", GetLastError());
101
102     trace("MM_TEXT rect.bottom %d\n", rect.bottom);
103     ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
104        "should return a positive rectangle bottom. (bot=%d)\n",
105        rect.bottom);
106
107     /* empty or null text should in some cases calc an empty rectangle */
108     /* note: testing the function's return value is useless, it differs
109      * ( 0 or 1) on every Windows version I tried */
110     SetRect( &rect, 10,10, 100, 100);
111     textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT, NULL );
112     ok( !(rect.left == rect.right && rect.bottom == rect.top),
113         "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
114     SetRect( &rect, 10,10, 100, 100);
115     SetLastError( 0);
116     textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT, NULL );
117     ok( (rect.left == rect.right && rect.bottom == rect.top),
118         "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
119     SetRect( &rect, 10,10, 100, 100);
120     SetLastError( 0);
121     textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
122     ok( (rect.left == rect.right && rect.bottom == rect.top),
123         "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
124     SetRect( &rect, 10,10, 100, 100);
125     textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
126     ok( !(rect.left == rect.right && rect.bottom == rect.top),
127         "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
128
129     /* Wide char versions */
130     SetRect( &rect, 10,10, 100, 100);
131     SetLastError( 0);
132     textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT, NULL );
133     if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
134         ok( !(rect.left == rect.right && rect.bottom == rect.top),
135             "rectangle should NOT be empty got %d,%d-%d,%d\n",
136             rect.left, rect.top, rect.right, rect.bottom );
137         SetRect( &rect, 10,10, 100, 100);
138         textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
139         ok( (rect.left == rect.right && rect.bottom == rect.top),
140             "rectangle should be empty got %d,%d-%d,%d\n",
141             rect.left, rect.top, rect.right, rect.bottom );
142         if (0) {
143             SetRect( &rect, 10,10, 100, 100);
144             /* Crashes on NT4 */
145             textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
146             ok( !(rect.left == rect.right && rect.bottom == rect.top),
147                 "rectangle should NOT be empty got %d,%d-%d,%d\n",
148                 rect.left, rect.top, rect.right, rect.bottom );
149         }
150         SetRect( &rect, 10,10, 100, 100);
151         textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
152         ok( !(rect.left == rect.right && rect.bottom == rect.top),
153             "rectangle should NOT be empty got %d,%d-%d,%d\n",
154             rect.left, rect.top, rect.right, rect.bottom );
155     }
156
157     /* More test cases from bug 12226 */
158     SetRect(&rect, 0, 0, 0, 0);
159     textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
160     todo_wine ok(textheight, "DrawTextA error %u\n", GetLastError());
161     ok(0 == rect.left, "expected 0, got %d\n", rect.left);
162     ok(0 == rect.right, "expected 0, got %d\n", rect.right);
163     ok(0 == rect.top, "expected 0, got %d\n", rect.top);
164     todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
165
166     SetRect(&rect, 0, 0, 0, 0);
167     textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
168     if (!textheight && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
169     {
170         win_skip( "DrawTextW not implemented\n" );
171     }
172     else
173     {
174         todo_wine ok(textheight, "DrawTextW error %u\n", GetLastError());
175         ok(0 == rect.left, "expected 0, got %d\n", rect.left);
176         ok(0 == rect.right, "expected 0, got %d\n", rect.right);
177         ok(0 == rect.top, "expected 0, got %d\n", rect.top);
178         todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
179     }
180
181     SelectObject(hdc, hOldFont);
182     ret = DeleteObject(hFont);
183     ok( ret, "DeleteObject error %u\n", GetLastError());
184
185     /* Clean up */
186     ret = ReleaseDC(hwnd, hdc);
187     ok( ret, "ReleaseDC error %u\n", GetLastError());
188     ret = DestroyWindow(hwnd);
189     ok( ret, "DestroyWindow error %u\n", GetLastError());
190 }
191
192 /* replace tabs by \t */
193 static void strfmt( const char *str, char *strout)
194 {
195     unsigned int i,j ;
196     for(i=0,j=0;i<=strlen(str);i++,j++)
197         if((strout[j]=str[i])=='\t') {
198             strout[j++]='\\';
199             strout[j]='t';
200         }
201 }
202
203
204 #define TABTEST( tabval, tabcount, string, _exp) \
205 { int i,x_act, x_exp; char strdisp[64];\
206     for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
207     extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
208     strfmt( string, strdisp); \
209  /*   trace( "Extent is %08lx\n", extent); */\
210     x_act = LOWORD( extent); \
211     x_exp = (_exp); \
212     ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
213         strdisp, x_act, x_exp, tabval, tabcount); \
214 } \
215
216
217 static void test_TabbedText(void)
218 {
219     HWND hwnd;
220     HDC hdc;
221     BOOL ret;
222     TEXTMETRICA tm;
223     DWORD extent;
224     INT tabs[8], cx, cy, tab, tabcount,t,align;
225
226     /* Initialization */
227     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
228                            0, 0, 200, 200, 0, 0, 0, NULL);
229     ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
230     hdc = GetDC(hwnd);
231     ok(hdc != 0, "GetDC error %u\n", GetLastError());
232
233     ret = GetTextMetricsA( hdc, &tm);
234     ok( ret, "GetTextMetrics error %u\n", GetLastError());
235
236     extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
237     cx = LOWORD( extent);
238     cy = HIWORD( extent);
239     trace( "cx is %d cy is %d\n", cx, cy);
240
241     align=1;
242     for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
243                                catch the one off errors */
244         tab =  (cx *4 + t);
245         /* test the special case tabcount =1 and the general array (80 of tabs */
246         for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
247             TABTEST( align * tab, tabcount, "\t", tab)
248             TABTEST( align * tab, tabcount, "xxx\t", tab)
249             TABTEST( align * tab, tabcount, "\tx", tab+cx)
250             TABTEST( align * tab, tabcount, "\t\t", tab*2)
251             TABTEST( align * tab, tabcount, "\tx\t", tab*2)
252             TABTEST( align * tab, tabcount, "x\tx", tab+cx)
253             TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
254             TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
255             TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
256             TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
257         }
258     }
259     align=-1;
260     for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
261                                catch the one off errors */
262         tab =  (cx *4 + t);
263         /* test the special case tabcount =1 and the general array (8) of tabs */
264         for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
265             TABTEST( align * tab, tabcount, "\t", tab)
266             TABTEST( align * tab, tabcount, "xxx\t", tab)
267             TABTEST( align * tab, tabcount, "\tx", tab)
268             TABTEST( align * tab, tabcount, "\t\t", tab*2)
269             TABTEST( align * tab, tabcount, "\tx\t", tab*2)
270             TABTEST( align * tab, tabcount, "x\tx", tab)
271             TABTEST( align * tab, tabcount, "xx\tx", tab)
272             TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
273             TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
274             TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
275         }
276     }
277
278     ReleaseDC( hwnd, hdc );
279     DestroyWindow( hwnd );
280 }
281
282 static void test_DrawState(void)
283 {
284     static const char text[] = "Sample text string";
285     HWND hwnd;
286     HDC hdc;
287     BOOL ret;
288
289     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
290                            0, 0, 200, 200, 0, 0, 0, NULL);
291     assert(hwnd);
292
293     hdc = GetDC(hwnd);
294     assert(hdc);
295
296     SetLastError(0xdeadbeef);
297     ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
298                     0, 0, 10, 10, DST_TEXT);
299     ok(ret, "DrawState error %u\n", GetLastError());
300
301     SetLastError(0xdeadbeef);
302     ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
303                     0, 0, 10, 10, DST_TEXT);
304     ok(ret, "DrawState error %u\n", GetLastError());
305
306     SetLastError(0xdeadbeef);
307     ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
308                     0, 0, 10, 10, DST_TEXT);
309     ok(!ret, "DrawState succeeded\n");
310     ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
311
312     SetLastError(0xdeadbeef);
313     ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
314                     0, 0, 10, 10, DST_TEXT);
315     ok(!ret, "DrawState succeeded\n");
316     ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
317
318     ReleaseDC(hwnd, hdc);
319     DestroyWindow(hwnd);
320 }
321
322 START_TEST(text)
323 {
324     test_TabbedText();
325     test_DrawTextCalcRect();
326     test_DrawState();
327 }