usp10: Added some tests.
[wine] / dlls / usp10 / tests / usp10.c
1 /*
2  * Tests for usp10 dll
3  *
4  * Copyright 2006 Jeff Latimer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Notes:
21  * Uniscribe allows for processing of complex scripts such as joining
22  * and filtering characters and bi-directional text with custom line breaks.
23  */
24
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include <wine/test.h>
29 #include <winbase.h>
30 #include <wingdi.h>
31 #include <winuser.h>
32 #include <winerror.h>
33 #include <usp10.h>
34
35 START_TEST(usp10)
36 {
37     HRESULT         hr;
38     int             iMaxProps;
39     const SCRIPT_PROPERTIES **ppSp;
40     HWND            hwnd;
41     HRGN            hrgn;
42     HDC             hdc;
43
44     int             cInChars;
45     int             cMaxItems;
46     SCRIPT_ITEM     pItem[255];
47     int             pcItems;
48     WCHAR           TestItem1[6] = {'T', 'e', 's', 't', '1', 0}; 
49     WCHAR           TestItem2[6] = {'T', 'e', 's', 't', '2', 0}; 
50
51     SCRIPT_CACHE    psc;
52     int             cChars;
53     int             cMaxGlyphs;
54     unsigned short  pwOutGlyphs[256];
55     unsigned short  pwLogClust[256];
56     SCRIPT_VISATTR  psva[256];
57     int             pcGlyphs;
58     int             piAdvance[256];
59     GOFFSET         pGoffset[256];
60     ABC             pABC[256];
61     int             cnt;
62
63     /* We need a valid HDC to drive a lot of Script functions which requires the following    *
64      * to set up for the tests.                                                               */
65     hwnd = CreateWindowExA(0, "static", "", WS_POPUP, 0,0,100,100,
66                            0, 0, 0, NULL);
67     assert(hwnd != 0);
68     ShowWindow(hwnd, SW_SHOW);
69     UpdateWindow(hwnd);
70
71     hrgn = CreateRectRgn(0, 0, 0, 0);
72     assert(hrgn != 0);
73
74     hdc = GetDC(hwnd);                                      /* We now have a hdc             */
75     ok( hdc != NULL, "HDC failed to be created %p\n", hdc);
76
77     /* Start testing usp10 functions                                                         */
78     /* This test determines that the pointer returned by ScriptGetProperties is valid
79      * by checking a known value in the table                                                */
80     hr = ScriptGetProperties(&ppSp, &iMaxProps);
81     trace("number of script properties %d\n", iMaxProps);
82     todo_wine {
83         ok (iMaxProps > 0, "Number of scripts returned should not be 0\n"); 
84         if  (iMaxProps > 0)
85             ok( ppSp[5]->langid == 9, "Langid[5] not = to 9\n"); /* Check a known value to ensure   */
86                                                                  /* ptrs work                       */
87     }
88
89     /* This set of tests are to check that the various edits in ScriptIemize work           */
90     cInChars = 5;                                        /* Length of test without NULL     */
91     cMaxItems = 1;                                       /* Check threshold value           */
92     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
93     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cMaxItems < 2.  Was %d\n",
94         cMaxItems);
95     cInChars = 5;
96     cMaxItems = 255;
97     hr = ScriptItemize(NULL, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
98     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pwcInChars is NULL\n");
99
100     cInChars = 5;
101     cMaxItems = 255;
102     hr = ScriptItemize(TestItem1, 0, cMaxItems, NULL, NULL, pItem, &pcItems);
103     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if cInChars is 0\n");
104
105     cInChars = 5;
106     cMaxItems = 255;
107     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, NULL, &pcItems);
108     ok (hr == E_INVALIDARG, "ScriptItemize should return E_INVALIDARG if pItems is NULL\n");
109
110     /* This is a valid test that will cause parsing to take place                             */
111     cInChars = 5;
112     cMaxItems = 255;
113     hr = ScriptItemize(TestItem1, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
114     ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", (unsigned int) hr);
115     /*  This test is for the interim operation of ScriptItemize where only one SCRIPT_ITEM is *
116      *  returned.                                                                             */
117     todo_wine ok (pcItems > 0, "The number of SCRIPT_ITEMS should be greater than 0\n");
118     if (pcItems > 0)
119         ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
120             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
121             pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
122
123     /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue
124      * ie. ScriptItemize has succeeded and that pItem has been set                            */
125     cInChars = 5;
126     cMaxItems = 255;
127     if (hr == 0) {
128         psc = NULL;                                   /* must be null on first call           */
129         cChars = cInChars;
130         cMaxGlyphs = 4;
131         hr = ScriptShape(NULL, &psc, TestItem1, cChars,
132                          cMaxGlyphs, &pItem[0].a,
133                          pwOutGlyphs, pwLogClust, psva, &pcGlyphs);
134         todo_wine ok (hr == E_OUTOFMEMORY, "If not enoungh output area cChars (%d) is > than CMaxGlyphs (%d) but not E_OUTOFMEMORY\n",
135             cChars, cMaxGlyphs);
136         cMaxGlyphs = 256;
137         hr = ScriptShape(NULL, &psc, TestItem1, cChars,
138                          cMaxGlyphs, &pItem[0].a,
139                          pwOutGlyphs, pwLogClust, psva, &pcGlyphs);
140         todo_wine ok (hr == E_PENDING, "If psc is NULL (%08x) the E_PENDING should be returned\n",
141                       (unsigned int) hr);
142         hr = ScriptShape(hdc, &psc, TestItem1, cChars,
143                          cMaxGlyphs, &pItem[0].a,
144                          pwOutGlyphs, pwLogClust, psva, &pcGlyphs);
145         todo_wine ok (hr == 0, "Should return 0 not (%08x)\n", (unsigned int) hr);
146         todo_wine ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
147         todo_wine ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
148         if (hr ==0) {
149             hr = ScriptPlace(NULL, &psc, pwOutGlyphs, pcGlyphs, psva, &pItem[0].a, piAdvance,
150                              pGoffset, pABC);
151             ok (hr == 0, "Should return 0 not (%08x)\n", (unsigned int) hr);
152         }
153
154         /* This test will check to make sure that SCRIPT_CACHE is reused and that not translation   *
155          * takes place if fNoGlyphIndex is set.                                                     */
156
157         cInChars = 5;
158         cMaxItems = 255;
159         hr = ScriptItemize(TestItem2, cInChars, cMaxItems, NULL, NULL, pItem, &pcItems);
160         ok (hr == 0, "ScriptItemize should return 0, returned %08x\n", (unsigned int) hr);
161         /*  This test is for the intertrim operation of ScriptItemize where only one SCRIPT_ITEM is *
162          *  returned.                                                                               */
163         todo_wine ok (pItem[0].iCharPos == 0 && pItem[1].iCharPos == cInChars,
164                             "Start pos not = 0 (%d) or end pos not = %d (%d)\n",
165                              pItem[0].iCharPos, cInChars, pItem[1].iCharPos);
166         /* It would appear that we have a valid SCRIPT_ANALYSIS and can continue                    */
167         if (hr == 0) {
168              cChars = cInChars;
169              cMaxGlyphs = 256;
170              pItem[0].a.fNoGlyphIndex = 1;                /* say no translate                     */
171              hr = ScriptShape(NULL, &psc, TestItem2, cChars,
172                               cMaxGlyphs, &pItem[0].a,
173                               pwOutGlyphs, pwLogClust, psva, &pcGlyphs);
174              ok (hr != E_PENDING, "If psc should not be NULL (%08x) and the E_PENDING should be returned\n",
175                               (unsigned int) hr);
176              todo_wine ok (hr == 0, "Should return 0 not (%08x)\n", (unsigned int) hr);
177              todo_wine ok (psc != NULL, "psc should not be null and have SCRIPT_CACHE buffer address\n");
178              todo_wine ok (pcGlyphs == cChars, "Chars in (%d) should equal Glyphs out (%d)\n", cChars, pcGlyphs);
179              for (cnt=0; cnt < cChars && TestItem2[cnt] == pwOutGlyphs[cnt]; cnt++) {}
180              todo_wine ok (cnt == cChars, "Translation to place when told not to. WCHAR %d - %04x != %04x\n",
181                            cnt, TestItem2[cnt], pwOutGlyphs[cnt]);
182              if (hr ==0) {
183                  hr = ScriptPlace(NULL, &psc, pwOutGlyphs, pcGlyphs, psva, &pItem[0].a, piAdvance,
184                               pGoffset, pABC);
185                  todo_wine ok (hr == 0, "Should return 0 not (%08x)\n", (unsigned int) hr);
186              }
187         }
188         hr = ScriptFreeCache( &psc);
189         ok (!psc, "psc is not null after ScriptFreeCache\n");
190     }
191     DeleteObject(hrgn);
192     ReleaseDC(hwnd, hdc);
193     DestroyWindow(hwnd);
194     return;
195 }