mapi32/tests: Fix typo.
[wine] / dlls / twain_32 / tests / dsm.c
1 /* Unit test suite for Twain DSM functions
2  *
3  * Copyright 2009 Jeremy White, CodeWeavers, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  *
19  */
20 #include <stdarg.h>
21 #include <assert.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winerror.h"
26 #include "winuser.h"
27 #include "twain.h"
28
29 #include "wine/test.h"
30
31 static DSMENTRYPROC pDSM_Entry;
32
33 static BOOL dsm_RegisterWindowClasses(void)
34 {
35     WNDCLASSA cls;
36
37     cls.style = 0;
38     cls.lpfnWndProc = DefWindowProc;
39     cls.cbClsExtra = 0;
40     cls.cbWndExtra = 0;
41     cls.hInstance = GetModuleHandleA(0);
42     cls.hIcon = 0;
43     cls.hCursor = LoadCursorA(0, IDC_ARROW);
44     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
45     cls.lpszMenuName = NULL;
46     cls.lpszClassName = "TWAIN_dsm_class";
47     if (!RegisterClassA(&cls)) return FALSE;
48
49     return TRUE;
50 }
51
52
53 static void get_condition_code(TW_IDENTITY *appid, TW_IDENTITY *source, TW_STATUS *status)
54 {
55     TW_UINT16 rc;
56     rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_STATUS, MSG_GET, status);
57     ok(rc == TWRC_SUCCESS, "Condition code not available, rc %d\n", rc);
58 }
59
60 static BOOL get_onevalue(TW_HANDLE hcontainer, TW_UINT32 *ret, TW_UINT16 *type)
61 {
62     TW_ONEVALUE *onev;
63     onev = GlobalLock(hcontainer);
64     if (onev)
65     {
66         *ret = onev->Item;
67         if (type)
68             *type = onev->ItemType;
69         GlobalUnlock(hcontainer);
70         return TRUE;
71     }
72     return FALSE;
73 }
74
75 static TW_HANDLE alloc_and_set_onevalue(TW_UINT32 val, TW_UINT16 type)
76 {
77     TW_HANDLE hcontainer;
78     TW_ONEVALUE *onev;
79     hcontainer = GlobalAlloc(0, sizeof(*onev));
80     if (hcontainer)
81     {
82         onev = GlobalLock(hcontainer);
83         if (onev)
84         {
85             onev->ItemType = type;
86             onev->Item = val;
87             GlobalUnlock(hcontainer);
88         }
89         else
90             hcontainer = 0;
91     }
92     return hcontainer;
93 }
94
95 static void check_get(TW_CAPABILITY *pCapability,
96                 TW_UINT32 orig_value, TW_UINT32 default_value, TW_UINT32 *suggested_set_value)
97 {
98     void *p;
99     if (suggested_set_value)
100         *suggested_set_value = orig_value + 1;
101     p = GlobalLock(pCapability->hContainer);
102     if (p)
103     {
104         if (pCapability->ConType == TWON_ONEVALUE)
105         {
106             TW_ONEVALUE *onev = (TW_ONEVALUE *) p;
107             ok(onev->Item == orig_value, "MSG_GET of 0x%x returned 0x%x, expecting 0x%x\n",
108                 pCapability->Cap, onev->Item, orig_value);
109         }
110         else if (pCapability->ConType == TWON_ENUMERATION)
111         {
112             int i;
113             TW_UINT8 *p8;
114             TW_UINT16 *p16;
115             TW_UINT32 *p32;
116             TW_ENUMERATION *enumv = (TW_ENUMERATION *) p;
117             p8 = enumv->ItemList;
118             p16 = (TW_UINT16 *) p8;
119             p32 = (TW_UINT32 *) p8;
120             trace("MSG_GET of 0x%x returned %d items:\n", pCapability->Cap, enumv->NumItems);
121             for (i = 0; i < enumv->NumItems; i++)
122             {
123                 if (enumv->ItemType == TWTY_UINT8 || enumv->ItemType == TWTY_INT8)
124                     trace("  %d: 0x%x\n", i, p8[i]);
125                 if (enumv->ItemType == TWTY_UINT16 || enumv->ItemType == TWTY_INT16)
126                     trace("  %d: 0x%x\n", i, p16[i]);
127                 if (enumv->ItemType == TWTY_UINT32 || enumv->ItemType == TWTY_INT32)
128                     trace("  %d: 0x%x\n", i, p32[i]);
129             }
130             if (enumv->ItemType == TWTY_UINT16 || enumv->ItemType == TWTY_INT16)
131             {
132                 ok(p16[enumv->CurrentIndex] == orig_value,
133                     "Type 0x%x, values from MSG_GET (0x%x) and MSG_GETCURRENT (0x%x) do not match.\n",
134                     pCapability->Cap, p16[enumv->CurrentIndex], orig_value);
135                 ok(p16[enumv->DefaultIndex] == default_value,
136                     "Type 0x%x, values from MSG_GET (0x%x) and MSG_GETDEFAULT (0x%x) do not match.\n",
137                     pCapability->Cap, p16[enumv->DefaultIndex], default_value);
138                 if (suggested_set_value)
139                     *suggested_set_value = p16[(enumv->CurrentIndex + 1) % enumv->NumItems];
140             }
141             if (enumv->ItemType == TWTY_UINT32 || enumv->ItemType == TWTY_INT32)
142             {
143                 ok(p32[enumv->CurrentIndex] == orig_value,
144                     "Type 0x%x, values from MSG_GET (0x%x) and MSG_GETCURRENT (0x%x) do not match.\n",
145                     pCapability->Cap, p32[enumv->CurrentIndex], orig_value);
146                 ok(p32[enumv->DefaultIndex] == default_value,
147                     "Type 0x%x, values from MSG_GET (0x%x) and MSG_GETDEFAULT (0x%x) do not match.\n",
148                     pCapability->Cap, p32[enumv->DefaultIndex], default_value);
149                 if (suggested_set_value)
150                     *suggested_set_value = p32[(enumv->CurrentIndex + 1) % enumv->NumItems];
151             }
152         }
153         else
154             trace("MSG_GET on type 0x%x returned type 0x%x, which we didn't check.\n", pCapability->Cap, pCapability->ConType);
155         GlobalUnlock(pCapability->hContainer);
156     }
157 }
158
159 static void test_onevalue_cap(TW_IDENTITY *appid, TW_IDENTITY *source, TW_UINT16 captype, TW_UINT16 type, TW_INT32 expected_support)
160 {
161     TW_UINT16 rc;
162     TW_UINT16 rtype;
163     TW_STATUS status;
164     TW_CAPABILITY cap;
165     TW_UINT32 orig_value = 0;
166     TW_UINT32 new_value;
167     TW_UINT32 default_value = 0;
168     TW_INT32 actual_support;
169
170     memset(&cap, 0, sizeof(cap));
171     cap.Cap = captype;
172     cap.ConType = TWON_DONTCARE16;
173
174     rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_QUERYSUPPORT, &cap);
175     get_condition_code(appid, source, &status);
176     ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
177             "Error [rc %d|cc %d] doing MSG_QUERYSUPPORT for type 0x%x\n", rc, status.ConditionCode, captype);
178     if (rc != TWRC_SUCCESS)
179         return;
180     ok(get_onevalue(cap.hContainer, (TW_UINT32 *) &actual_support, NULL), "Returned cap.hContainer invalid for QuerySupport on type 0x%x", captype);
181     ok(actual_support == expected_support,
182             "Error:  expected support 0x%x for type 0x%x, got 0x%x\n", expected_support,
183             captype, actual_support);
184
185
186     if (actual_support & TWQC_GETCURRENT)
187     {
188         memset(&cap, 0, sizeof(cap));
189         cap.Cap = captype;
190         cap.ConType = TWON_DONTCARE16;
191
192         rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_GETCURRENT, &cap);
193         get_condition_code(appid, source, &status);
194         ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
195                 "Error [rc %d|cc %d] doing MSG_GETCURRENT for type 0x%x\n", rc, status.ConditionCode, captype);
196         if (rc == TWRC_SUCCESS)
197         {
198             ok(get_onevalue(cap.hContainer, &orig_value, &rtype), "Returned cap.hContainer invalid for GETCURRENT on type 0x%x", captype);
199             ok(rtype == type, "Returned GETCURRENT type 0x%x for cap 0x%x is not expected 0x%x\n", rtype, captype, type);
200             GlobalFree(cap.hContainer);
201         }
202     }
203
204     if (actual_support & TWQC_GETDEFAULT)
205     {
206         memset(&cap, 0, sizeof(cap));
207         cap.Cap = captype;
208         cap.ConType = TWON_DONTCARE16;
209
210         rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_GETDEFAULT, &cap);
211         get_condition_code(appid, source, &status);
212         ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
213                 "Error [rc %d|cc %d] doing MSG_GETDEFAULT for type 0x%x\n", rc, status.ConditionCode, captype);
214         if (rc == TWRC_SUCCESS)
215         {
216             ok(get_onevalue(cap.hContainer, &default_value, &rtype), "Returned cap.hContainer invalid for GETDEFAULT on type 0x%x", captype);
217             ok(rtype == type, "Returned GETDEFAULT type 0x%x for cap 0x%x is not expected 0x%x\n", rtype, captype, type);
218             GlobalFree(cap.hContainer);
219         }
220     }
221
222     new_value = orig_value;
223     if (actual_support & TWQC_GET)
224     {
225         memset(&cap, 0, sizeof(cap));
226         cap.Cap = captype;
227         cap.ConType = TWON_DONTCARE16;
228
229         rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_GET, &cap);
230         get_condition_code(appid, source, &status);
231         ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
232                 "Error [rc %d|cc %d] doing MSG_GET for type 0x%x\n", rc, status.ConditionCode, captype);
233         check_get(&cap, orig_value, default_value, &new_value);
234         if (rc == TWRC_SUCCESS)
235             GlobalFree(cap.hContainer);
236     }
237
238     if (actual_support & TWQC_SET)
239     {
240         memset(&cap, 0, sizeof(cap));
241         cap.Cap = captype;
242         cap.ConType = TWON_ONEVALUE;
243         cap.hContainer = alloc_and_set_onevalue(new_value, type);
244
245         rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_SET, &cap);
246         get_condition_code(appid, source, &status);
247         ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
248                 "Error [rc %d|cc %d] doing MSG_SET for type 0x%x\n", rc, status.ConditionCode, captype);
249         GlobalFree(cap.hContainer);
250     }
251
252     if (actual_support & TWQC_RESET)
253     {
254         memset(&cap, 0, sizeof(cap));
255         cap.Cap = captype;
256         cap.ConType = TWON_DONTCARE16;
257
258         rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_RESET, &cap);
259         get_condition_code(appid, source, &status);
260         ok(rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS,
261                 "Error [rc %d|cc %d] doing MSG_RESET for type 0x%x\n", rc, status.ConditionCode, captype);
262         if (rc == TWRC_SUCCESS)
263             GlobalFree(cap.hContainer);
264     }
265 }
266
267
268 static void test_single_source(TW_IDENTITY *appid, TW_IDENTITY *source)
269 {
270     TW_UINT16 rc;
271     TW_STATUS status;
272     TW_CAPABILITY cap;
273     UINT16 capabilities[CAP_CUSTOMBASE];
274
275     memset(&cap, 0, sizeof(cap));
276     cap.Cap = CAP_SUPPORTEDCAPS;
277     cap.ConType = TWON_DONTCARE16;
278
279     rc = pDSM_Entry(appid, source, DG_CONTROL, DAT_CAPABILITY, MSG_GET, &cap);
280     get_condition_code(appid, source, &status);
281     ok(rc == TWRC_SUCCESS || status.ConditionCode == TWCC_SUCCESS,
282             "Error obtaining CAP_SUPPORTEDCAPS\n");
283
284     memset(capabilities, 0, sizeof(capabilities));
285     if (rc == TWRC_SUCCESS && cap.ConType == TWON_ARRAY)
286     {
287         TW_ARRAY *a;
288         a = GlobalLock(cap.hContainer);
289         if (a)
290         {
291             if (a->ItemType == TWTY_UINT16)
292             {
293                 int i;
294                 UINT16 *u = (UINT16 *) a->ItemList;
295                 trace("%d Capabilities:\n", a->NumItems);
296                 for (i = 0; i < a->NumItems; i++)
297                     if (u[i] < sizeof(capabilities) / sizeof(capabilities[0]))
298                     {
299                         capabilities[u[i]] = 1;
300                         trace("  %d: 0x%x\n", i, u[i]);
301                     }
302             }
303             GlobalUnlock(cap.hContainer);
304         }
305     }
306
307     /* For Twain 1.6, all sources must support: */
308     ok(capabilities[CAP_SUPPORTEDCAPS], "CAP_SUPPORTEDCAPS not supported\n");
309     todo_wine
310     ok(capabilities[CAP_XFERCOUNT], "CAP_XFERCOUNT not supported\n");
311     todo_wine
312     ok(capabilities[CAP_UICONTROLLABLE], "CAP_UICONTROLLABLE not supported\n");
313
314     if (source->SupportedGroups & DG_IMAGE)
315     {
316         /* For Twain 1.6:
317             Sources that supply image information must support DG_CONTROL / DAT_CAPABILITY /
318             MSG_GET, MSG_GETCURRENT, MSG_GETDEFAULT on:
319         */
320         todo_wine
321         ok(capabilities[ICAP_COMPRESSION], "ICAP_COMPRESSION not supported\n");
322         todo_wine
323         ok(capabilities[ICAP_PLANARCHUNKY], "ICAP_PLANARCHUNKY not supported\n");
324         todo_wine
325         ok(capabilities[ICAP_PHYSICALHEIGHT], "ICAP_PHYSICALHEIGHT not supported\n");
326         todo_wine
327         ok(capabilities[ICAP_PHYSICALWIDTH], "ICAP_PHYSICALWIDTH not supported\n");
328         todo_wine
329         ok(capabilities[ICAP_PIXELFLAVOR], "ICAP_PIXELFLAVOR not supported\n");
330
331         /* For Twain 1.6:
332             Sources that supply image information must support DG_CONTROL / DAT_CAPABILITY /
333             MSG_GET, MSG_GETCURRENT, MSG_GETDEFAULT, MSG_RESET and MSG_SET on:
334         */
335         todo_wine
336         ok(capabilities[ICAP_BITDEPTH], "ICAP_BITDEPTH not supported\n");
337         todo_wine
338         ok(capabilities[ICAP_BITORDER], "ICAP_BITORDER not supported\n");
339         todo_wine
340         ok(capabilities[ICAP_PIXELTYPE], "ICAP_PIXELTYPE not supported\n");
341         todo_wine
342         ok(capabilities[ICAP_UNITS], "ICAP_UNITS not supported\n");
343         ok(capabilities[ICAP_XFERMECH], "ICAP_XFERMECH not supported\n");
344         if (capabilities[ICAP_XFERMECH])
345             test_onevalue_cap(appid, source, ICAP_XFERMECH, TWTY_UINT16,
346                 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET);
347         todo_wine
348         ok(capabilities[ICAP_XRESOLUTION], "ICAP_XRESOLUTION not supported\n");
349         todo_wine
350         ok(capabilities[ICAP_YRESOLUTION], "ICAP_YRESOLUTION not supported\n");
351     }
352 }
353
354 static void test_sources(TW_IDENTITY *appid)
355 {
356     TW_UINT16 rc;
357     TW_IDENTITY source;
358     TW_STATUS status;
359     int scannercount = 0;
360
361     memset(&source, 0, sizeof(source));
362     rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_GETFIRST, &source);
363     get_condition_code(appid, NULL, &status);
364     ok( (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS) ||
365         (rc == TWRC_FAILURE && status.ConditionCode == TWCC_NODS),
366             "Get first invalid condition code, rc %d, cc %d\n", rc, status.ConditionCode);
367
368     while (rc == TWRC_SUCCESS)
369     {
370         scannercount++;
371         trace("[Scanner %d|Version %d.%d(%s)|Protocol %d.%d|SupportedGroups 0x%x|Manufacturer %s|Family %s|ProductName %s]\n",
372             scannercount,
373             source.Version.MajorNum, source.Version.MinorNum, source.Version.Info,
374             source.ProtocolMajor, source.ProtocolMinor, source.SupportedGroups,
375             source.Manufacturer, source.ProductFamily, source.ProductName);
376         memset(&source, 0, sizeof(source));
377         rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_GETNEXT, &source);
378         get_condition_code(appid, NULL, &status);
379         ok(rc == TWRC_SUCCESS || rc == TWRC_ENDOFLIST, "Get next source failed, rc %d, cc %d\n", rc, status.ConditionCode);
380     }
381
382     memset(&source, 0, sizeof(source));
383     rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_GETDEFAULT, &source);
384     get_condition_code(appid, NULL, &status);
385     ok( (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS) ||
386         (rc == TWRC_FAILURE && status.ConditionCode == TWCC_NODS),
387             "Get default invalid condition code, rc %d, cc %d\n", rc, status.ConditionCode);
388
389     if (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS)
390     {
391         rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, &source);
392         get_condition_code(appid, NULL, &status);
393
394         if (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS)
395         {
396             rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, &source);
397             get_condition_code(appid, NULL, &status);
398             ok(rc == TWRC_SUCCESS, "Close DS Failed, rc %d, cc %d\n", rc, status.ConditionCode);
399         }
400     }
401
402     if (winetest_interactive)
403     {
404         trace("Interactive, so trying userselect\n");
405         memset(&source, 0, sizeof(source));
406         rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_USERSELECT, &source);
407         get_condition_code(appid, NULL, &status);
408         ok(rc == TWRC_SUCCESS || rc == TWRC_CANCEL, "Userselect failed, rc %d, cc %d\n", rc, status.ConditionCode);
409
410         if (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS)
411         {
412             rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, &source);
413             get_condition_code(appid, NULL, &status);
414             if (rc == TWRC_SUCCESS && status.ConditionCode == TWCC_SUCCESS)
415             {
416                 test_single_source(appid, &source);
417                 rc = pDSM_Entry(appid, NULL, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, &source);
418                 get_condition_code(appid, NULL, &status);
419                 ok(rc == TWRC_SUCCESS, "Close DS Failed, rc %d, cc %d\n", rc, status.ConditionCode);
420             }
421         }
422     }
423
424 }
425
426 START_TEST(dsm)
427 {
428     TW_IDENTITY appid;
429     TW_UINT16 rc;
430     HANDLE hwnd;
431     HMODULE htwain;
432
433     if (!dsm_RegisterWindowClasses()) assert(0);
434
435     htwain = LoadLibraryA("twain_32.dll");
436     if (! htwain)
437     {
438         skip("twain_32.dll not available, skipping tests\n");
439         return;
440     }
441     pDSM_Entry = (void*)GetProcAddress(htwain, "DSM_Entry");
442     ok(pDSM_Entry != NULL, "Unable to GetProcAddress DSM_Entry\n");
443     if (! pDSM_Entry)
444     {
445         skip("DSM_Entry not available, skipping tests\n");
446         return;
447     }
448
449     memset(&appid, 0, sizeof(appid));
450     appid.Version.Language = TWLG_ENGLISH_USA;
451     appid.Version.Country = TWCY_USA;
452     appid.ProtocolMajor = TWON_PROTOCOLMAJOR;
453     appid.ProtocolMinor = TWON_PROTOCOLMINOR;
454     appid.SupportedGroups = DG_CONTROL | DG_IMAGE;
455
456     hwnd = CreateWindow("TWAIN_dsm_class", "Twain Test", 0,
457                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
458                         NULL, NULL, GetModuleHandleA(0), NULL);
459
460     rc = pDSM_Entry(&appid, NULL, DG_CONTROL, DAT_PARENT, MSG_OPENDSM, (TW_MEMREF) &hwnd);
461     ok(rc == TWRC_SUCCESS, "MSG_OPENDSM returned %d\n", rc);
462
463     test_sources(&appid);
464
465     rc = pDSM_Entry(&appid, NULL, DG_CONTROL, DAT_PARENT, MSG_CLOSEDSM, (TW_MEMREF) &hwnd);
466     ok(rc == TWRC_SUCCESS, "MSG_CLOSEDSM returned %d\n", rc);
467
468     DestroyWindow(hwnd);
469     FreeLibrary(htwain);
470 }