setupapi: Improve parameter validation for SetupCreateDiskSpaceListA/W.
[wine] / dlls / winmm / tests / mcicda.c
1 /*
2  * Test MCI CD-ROM access
3  *
4  * Copyright 2010 Jörg Höhle
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22 #include "windows.h"
23 #include "mmsystem.h"
24 #include "wine/test.h"
25
26 typedef union {
27       MCI_STATUS_PARMS     status;
28       MCI_GETDEVCAPS_PARMS caps;
29       MCI_OPEN_PARMS       open;
30       MCI_PLAY_PARMS       play;
31       MCI_SEEK_PARMS       seek;
32       MCI_SAVE_PARMS       save;
33       MCI_GENERIC_PARMS    gen;
34     } MCI_PARMS_UNION;
35
36 extern const char* dbg_mcierr(MCIERROR err); /* from mci.c */
37
38 static BOOL spurious_message(LPMSG msg)
39 {
40   /* WM_DEVICECHANGE 0x0219 appears randomly */
41   if(msg->message != MM_MCINOTIFY) {
42     trace("skipping spurious message %04x\n",msg->message);
43     return TRUE;
44   }
45   return FALSE;
46 }
47
48 /* A single ok() in each code path allows to prefix this with todo_wine */
49 #define test_notification(hwnd, command, type) test_notification_dbg(hwnd, command, type, __LINE__)
50 static void test_notification_dbg(HWND hwnd, const char* command, WPARAM type, int line)
51 {   /* Use type 0 as meaning no message */
52     MSG msg;
53     BOOL seen;
54     do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
55     while(seen && spurious_message(&msg));
56     if(type && !seen) {
57       /* We observe transient delayed notification, mostly on native.
58        * Notification is not always present right when mciSend returns. */
59       trace_(__FILE__,line)("Waiting for delayed notification from %s\n", command);
60       MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
61       seen = PeekMessageA(&msg, hwnd, MM_MCINOTIFY, MM_MCINOTIFY, PM_REMOVE);
62     }
63     if(!seen)
64       ok_(__FILE__,line)(type==0, "Expect message %04lx from %s\n", type, command);
65     else if(msg.hwnd != hwnd)
66         ok_(__FILE__,line)(msg.hwnd == hwnd, "Didn't get the handle to our test window\n");
67     else if(msg.message != MM_MCINOTIFY)
68         ok_(__FILE__,line)(msg.message == MM_MCINOTIFY, "got %04x instead of MM_MCINOTIFY from command %s\n", msg.message, command);
69     else ok_(__FILE__,line)(msg.wParam == type, "got %04lx instead of MCI_NOTIFY_xyz %04lx from command %s\n", msg.wParam, type, command);
70 }
71
72 #define CDFRAMES_PERSEC                 75
73 static DWORD MSF_Add(DWORD d1, DWORD d2)
74 {
75     WORD c, m, s, f;
76     f = MCI_MSF_FRAME(d1)  + MCI_MSF_FRAME(d2);
77     c = f / CDFRAMES_PERSEC;
78     f = f % CDFRAMES_PERSEC;
79     s = MCI_MSF_SECOND(d1) + MCI_MSF_SECOND(d2) + c;
80     c = s / 60;
81     s = s % 60;
82     m = MCI_MSF_MINUTE(d1) + MCI_MSF_MINUTE(d2) + c; /* may be > 60 */
83     return MCI_MAKE_MSF(m,s,f);
84 }
85
86 /* TODO show that shareable flag is not what Wine implements. */
87
88 static void test_play(HWND hwnd)
89 {
90     MCIDEVICEID wDeviceID;
91     MCI_PARMS_UNION parm;
92     MCIERROR err, ok_hw;
93     DWORD numtracks, track, duration;
94     DWORD factor = winetest_interactive ? 3 : 1;
95     char buf[1024];
96     memset(buf, 0, sizeof(buf));
97     parm.gen.dwCallback = (DWORD_PTR)hwnd; /* once to rule them all */
98
99     err = mciSendString("open cdaudio alias c notify shareable", buf, sizeof(buf), hwnd);
100     ok(!err || err == MCIERR_CANNOT_LOAD_DRIVER || err == MCIERR_MUST_USE_SHAREABLE,
101        "mci open cdaudio notify returned %s\n", dbg_mcierr(err));
102     test_notification(hwnd, "open alias notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
103     /* Native returns MUST_USE_SHAREABLE when there's trouble with the hardware
104      * (e.g. unreadable disk) or when Media Player already has the device open,
105      * yet adding that flag does not help get past this error. */
106
107     if(err) {
108         skip("Cannot open any cdaudio device, %s.\n", dbg_mcierr(err));
109         return;
110     }
111     wDeviceID = atoi(buf);
112     ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
113
114     err = mciSendString("capability c has video notify", buf, sizeof(buf), hwnd);
115     ok(!err, "capability video: %s\n", dbg_mcierr(err));
116     if(!err) ok(!strcmp(buf, "false"), "capability video is %s\n", buf);
117     test_notification(hwnd, "capability notify", MCI_NOTIFY_SUCCESSFUL);
118
119     err = mciSendString("capability c can play", buf, sizeof(buf), hwnd);
120     ok(!err, "capability video: %s\n", dbg_mcierr(err));
121     if(!err) ok(!strcmp(buf, "true"), "capability play is %s\n", buf);
122
123     err = mciSendString("capability c", buf, sizeof(buf), NULL);
124     ok(err == MCIERR_MISSING_PARAMETER, "capability nokeyword: %s\n", dbg_mcierr(err));
125
126     parm.caps.dwItem = 0x4001;
127     parm.caps.dwReturn = 0xFEEDABAD;
128     err = mciSendCommand(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
129     ok(err == MCIERR_UNSUPPORTED_FUNCTION, "GETDEVCAPS %x: %s\n", parm.caps.dwItem, dbg_mcierr(err));
130
131     parm.caps.dwItem = MCI_GETDEVCAPS_DEVICE_TYPE;
132     err = mciSendCommand(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
133     ok(!err, "GETDEVCAPS device type: %s\n", dbg_mcierr(err));
134     if(!err) ok( parm.caps.dwReturn == MCI_DEVTYPE_CD_AUDIO, "getdevcaps device type: %u\n", parm.caps.dwReturn);
135
136     err = mciSendCommand(wDeviceID, MCI_RECORD, 0, (DWORD_PTR)&parm);
137     ok(err == MCIERR_UNSUPPORTED_FUNCTION, "MCI_RECORD: %s\n", dbg_mcierr(err));
138
139     /* Wine's MCI_MapMsgAtoW crashes on MCI_SAVE without parm->lpfilename */
140     parm.save.lpfilename = "foo";
141     err = mciSendCommand(wDeviceID, MCI_SAVE, 0, (DWORD_PTR)&parm);
142     ok(err == MCIERR_UNSUPPORTED_FUNCTION, "MCI_SAVE: %s\n", dbg_mcierr(err));
143
144     /* commands from the core set are UNSUPPORTED, others UNRECOGNIZED */
145     err = mciSendCommand(wDeviceID, MCI_STEP, 0, (DWORD_PTR)&parm);
146     ok(err == MCIERR_UNRECOGNIZED_COMMAND, "MCI_STEP: %s\n", dbg_mcierr(err));
147
148     parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
149     parm.status.dwReturn = 0xFEEDABAD;
150     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
151     ok(!err, "STATUS time format: %s\n", dbg_mcierr(err));
152     if(!err) ok(parm.status.dwReturn == MCI_FORMAT_MSF, "status time default format: %ld\n", parm.status.dwReturn);
153
154     /* "CD-Audio" */
155     err = mciSendString("info c product wait notify", buf, sizeof(buf), hwnd);
156     ok(!err, "info product: %s\n", dbg_mcierr(err));
157     test_notification(hwnd, "info notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
158
159     parm.status.dwItem = MCI_STATUS_MEDIA_PRESENT;
160     parm.status.dwReturn = 0xFEEDABAD;
161     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
162     ok(err || parm.status.dwReturn == TRUE || parm.status.dwReturn == FALSE,
163        "STATUS media present: %s\n", dbg_mcierr(err));
164
165     if (parm.status.dwReturn != TRUE) {
166         skip("No CD-ROM in drive.\n");
167         return;
168     }
169
170     parm.status.dwItem = MCI_STATUS_MODE;
171     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
172     ok(!err, "STATUS mode: %s\n", dbg_mcierr(err));
173     switch(parm.status.dwReturn) {
174     case MCI_MODE_NOT_READY:
175         skip("CD-ROM mode not ready (DVD in drive?)\n");
176         return;
177     case MCI_MODE_OPEN: /* should not happen with MEDIA_PRESENT */
178         skip("CD-ROM drive is open\n");
179         /* set door closed may not work. */
180         return;
181     default: /* play/record/seek/pause */
182         ok(parm.status.dwReturn==MCI_MODE_STOP, "STATUS mode is %lx\n", parm.status.dwReturn);
183         /* fall through */
184     case MCI_MODE_STOP: /* normal */
185         break;
186     }
187
188     /* Initial mode is "stopped" with a CD in drive */
189     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
190     ok(!err, "status mode: %s\n", dbg_mcierr(err));
191     if(!err) ok(!strcmp(buf, "stopped"), "status mode is initially %s\n", buf);
192
193     err = mciSendString("status c ready", buf, sizeof(buf), hwnd);
194     ok(!err, "status ready: %s\n", dbg_mcierr(err));
195     if(!err) ok(!strcmp(buf, "true"), "status ready with media is %s\n", buf);
196
197     err = mciSendString("info c product identity", buf, sizeof(buf), hwnd);
198     ok(!err, "info 2flags: %s\n", dbg_mcierr(err)); /* not MCIERR_FLAGS_NOT_COMPATIBLE */
199     /* Precedence rule p>u>i verified experimentally, not tested here. */
200
201     err = mciSendString("info c identity", buf, sizeof(buf), hwnd);
202     ok(!err || err == MCIERR_HARDWARE, "info identity: %s\n", dbg_mcierr(err));
203     /* a blank disk causes MCIERR_HARDWARE and other commands to fail likewise. */
204     ok_hw = err;
205
206     err = mciSendString("info c upc", buf, sizeof(buf), hwnd);
207     ok(err == ok_hw || err == MCIERR_NO_IDENTITY, "info upc: %s\n", dbg_mcierr(err));
208
209     parm.status.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
210     parm.status.dwReturn = 0;
211     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
212     ok(err == ok_hw, "STATUS number of tracks: %s\n", dbg_mcierr(err));
213     numtracks = parm.status.dwReturn;
214     /* cf. MAXIMUM_NUMBER_TRACKS */
215     ok(0 < numtracks && numtracks <= 99, "number of tracks=%ld\n", parm.status.dwReturn);
216
217     err = mciSendString("status c length", buf, sizeof(buf), hwnd);
218     ok(err == ok_hw, "status length: %s\n", dbg_mcierr(err));
219     if(!err) trace("CD length %s\n", buf);
220
221     if(err) { /* MCIERR_HARDWARE when given a blank disk */
222         skip("status length %s (blank disk?)\n", dbg_mcierr(ok_hw));
223         return;
224     }
225
226     /* Linux leaves the drive at some random position,
227      * native initialises to the start position below. */
228     err = mciSendString("status c position", buf, sizeof(buf), hwnd);
229     ok(!err, "status position: %s\n", dbg_mcierr(err));
230     if(!err) todo_wine ok(!strcmp(buf, "00:02:00") || !strcmp(buf, "00:02:33") || !strcmp(buf, "00:03:00"),
231                 "status position initially %s\n", buf);
232     /* 2 seconds is the initial position even with data tracks. */
233
234     err = mciSendString("status c position start notify", buf, sizeof(buf), hwnd);
235     ok(err == ok_hw, "status position start: %s\n", dbg_mcierr(err));
236     if(!err) ok(!strcmp(buf, "00:02:00") || !strcmp(buf, "00:02:33") || !strcmp(buf, "00:03:00"),
237                 "status position start %s\n", buf);
238     test_notification(hwnd, "status notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
239
240     err = mciSendString("status c position start track 1 notify", buf, sizeof(buf), hwnd);
241     ok(err == MCIERR_FLAGS_NOT_COMPATIBLE, "status position start: %s\n", dbg_mcierr(err));
242     test_notification(hwnd, "status 2flags", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
243
244     err = mciSendString("play c from 00:02:00 to 00:01:00 notify", buf, sizeof(buf), hwnd);
245     todo_wine ok(err == MCIERR_OUTOFRANGE, "play 2s to 1s: %s\n", dbg_mcierr(err));
246     test_notification(hwnd, "play 2s to 1s", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
247
248     err = mciSendString("resume c", buf, sizeof(buf), hwnd);
249     ok(err == MCIERR_HARDWARE || /* Win9x */ err == MCIERR_UNSUPPORTED_FUNCTION,
250        "resume without play: %s\n", dbg_mcierr(err)); /* not NONAPPLICABLE_FUNCTION */
251     /* vmware with a .iso (data-only) yields no error on NT/w2k */
252
253     err = mciSendString("seek c wait", buf, sizeof(buf), hwnd);
254     ok(err == MCIERR_MISSING_PARAMETER, "seek noflag: %s\n", dbg_mcierr(err));
255
256     err = mciSendString("seek c to start to end", buf, sizeof(buf), hwnd);
257     ok(err == MCIERR_FLAGS_NOT_COMPATIBLE || broken(!err), "seek to start+end: %s\n", dbg_mcierr(err));
258     /* Win9x only errors out with Seek to start to <position> */
259
260     /* set Wine to a defined position before play */
261     err = mciSendString("seek c to start notify", buf, sizeof(buf), hwnd);
262     ok(!err, "seek to start: %s\n", dbg_mcierr(err));
263     test_notification(hwnd, "seek to start", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
264     /* Win9X Status position / current track then sometimes report the end position / track! */
265
266     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
267     ok(!err, "status mode: %s\n", dbg_mcierr(err));
268     if(!err) ok(!strcmp(buf, "stopped"), "status mode after seek is %s\n", buf);
269
270     /* MCICDA ignores MCI_SET_VIDEO
271      * One xp machine ignored SET_AUDIO, one w2k and one w7 machine honoured it
272      * and simultaneously toggled the mute button in the mixer control panel.
273      * Or does it only depend on the HW, not the OS? */
274     err = mciSendString("set c video audio all on", buf, sizeof(buf), hwnd);
275     ok(!err, "set video/audio: %s\n", dbg_mcierr(err));
276
277     err = mciSendString("set c time format ms", buf, sizeof(buf), hwnd);
278     ok(!err, "set time format ms: %s\n", dbg_mcierr(err));
279
280     memset(buf, 0, sizeof(buf));
281     err = mciSendString("status c position start", buf, sizeof(buf), hwnd);
282     ok(!err, "status position start (ms): %s\n", dbg_mcierr(err));
283     duration = atoi(buf);
284     if(!err) ok(duration > 2000, "status position initially %sms\n", buf);
285     /* 00:02:00 corresponds to 2001 ms, 02:33 -> 2441 etc. */
286
287     err = mciSendString("status c position start track 1", buf, sizeof(buf), hwnd);
288     ok(err == MCIERR_FLAGS_NOT_COMPATIBLE, "status position start+track: %s\n", dbg_mcierr(err));
289
290     err = mciSendString("status c notify wait", buf, sizeof(buf), hwnd);
291     ok(err == MCIERR_MISSING_PARAMETER, "status noflag: %s\n", dbg_mcierr(err));
292
293     err = mciSendString("status c length track 1", buf, sizeof(buf), hwnd);
294     ok(!err, "status length (ms): %s\n", dbg_mcierr(err));
295     if(!err) {
296         trace("track #1 length %sms\n", buf);
297         duration = atoi(buf);
298     } else duration = 2001; /* for the position test below */
299
300     if (0) { /* causes some native systems to return Seek and Play with MCIERR_HARDWARE */
301         /* depending on capability can eject only? */
302         err = mciSendString("set c door closed notify", buf, sizeof(buf), hwnd);
303         ok(!err, "set door closed: %s\n", dbg_mcierr(err));
304         test_notification(hwnd, "door closed", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
305     }
306     /* Changing the disk while the MCI device is open causes the Status
307      * command to report stale data.  Native obviously caches the TOC. */
308
309     /* status type track is localised, strcmp("audio|other") may fail. */
310     parm.status.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
311     parm.status.dwTrack = 1;
312     parm.status.dwReturn = 0xFEEDABAD;
313     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
314     ok(!err, "STATUS type track 1: %s\n", dbg_mcierr(err));
315     ok(parm.status.dwReturn==MCI_CDA_TRACK_OTHER || parm.status.dwReturn==MCI_CDA_TRACK_AUDIO,
316        "unknown track type %lx\n", parm.status.dwReturn);
317
318     if (parm.status.dwReturn == MCI_CDA_TRACK_OTHER) {
319         /* Find an audio track */
320         parm.status.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
321         parm.status.dwTrack = numtracks;
322         parm.status.dwReturn = 0xFEEDABAD;
323         err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
324         ok(!err, "STATUS type track %u: %s\n", numtracks, dbg_mcierr(err));
325         ok(parm.status.dwReturn == MCI_CDA_TRACK_OTHER || parm.status.dwReturn == MCI_CDA_TRACK_AUDIO,
326            "unknown track type %lx\n", parm.status.dwReturn);
327         track = (!err && parm.status.dwReturn == MCI_CDA_TRACK_AUDIO) ? numtracks : 0;
328
329         /* Seek to start (above) skips over data tracks
330          * In case of a data only CD, it seeks to the end of disk, however
331          * another Status position a few seconds later yields MCIERR_HARDWARE. */
332         parm.status.dwItem = MCI_STATUS_POSITION;
333         parm.status.dwReturn = 2000;
334         err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
335         ok(!err || broken(err == MCIERR_HARDWARE), "STATUS position: %s\n", dbg_mcierr(err));
336
337         if(!err && track) ok(parm.status.dwReturn > duration,
338             "Seek did not skip data tracks, position %lums\n", parm.status.dwReturn);
339         /* dwReturn > start + length(#1) may fail because of small position report fluctuation.
340          * On some native systems, status position fluctuates around the target position;
341          * Successive calls return varying positions! */
342
343         err = mciSendString("set c time format msf", buf, sizeof(buf), hwnd);
344         ok(!err, "set time format msf: %s\n", dbg_mcierr(err));
345
346         parm.status.dwItem = MCI_STATUS_LENGTH;
347         parm.status.dwTrack = 1;
348         parm.status.dwReturn = 0xFEEDABAD;
349         err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
350         ok(!err, "STATUS length track %u: %s\n", parm.status.dwTrack, dbg_mcierr(err));
351         duration = parm.status.dwReturn;
352         trace("track #1 length: %02um:%02us:%02uframes\n",
353               MCI_MSF_MINUTE(duration), MCI_MSF_SECOND(duration), MCI_MSF_FRAME(duration));
354         ok(duration>>24==0, "CD length high bits %08X\n", duration);
355
356         /* TODO only with mixed CDs? */
357         /* play track 1 to length silently works with data tracks */
358         parm.play.dwFrom = MCI_MAKE_MSF(0,2,0);
359         parm.play.dwTo = duration; /* omitting 2 seconds from end */
360         err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_TO, (DWORD_PTR)&parm);
361         ok(!err, "PLAY data to %08X: %s\n", duration, dbg_mcierr(err));
362
363         Sleep(1500*factor); /* Time to spin up, hopefully less than track length */
364
365         err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
366         ok(!err, "status mode: %s\n", dbg_mcierr(err));
367         if(!err) ok(!strcmp(buf, "stopped"), "status mode on data is %s\n", buf);
368     } else if (parm.status.dwReturn == MCI_CDA_TRACK_AUDIO) {
369         skip("Got no mixed data+audio CD.\n");
370         track = 1;
371     } else track = 0;
372
373     if (!track) {
374         skip("Found no audio track.\n");
375         return;
376     }
377
378     err = mciSendString("set c time format msf", buf, sizeof(buf), hwnd);
379     ok(!err, "set time format msf: %s\n", dbg_mcierr(err));
380
381     parm.status.dwItem = MCI_STATUS_LENGTH;
382     parm.status.dwTrack = numtracks;
383     parm.status.dwReturn = 0xFEEDABAD;
384     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
385     ok(!err, "STATUS length track %u: %s\n", parm.status.dwTrack, dbg_mcierr(err));
386     duration = parm.status.dwReturn;
387     trace("last track length: %02um:%02us:%02uframes\n",
388           MCI_MSF_MINUTE(duration), MCI_MSF_SECOND(duration), MCI_MSF_FRAME(duration));
389     ok(duration>>24==0, "CD length high bits %08X\n", duration);
390
391     parm.status.dwItem = MCI_STATUS_POSITION;
392     /* dwTrack is still set */
393     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
394     ok(!err, "STATUS position start track %u: %s\n", parm.status.dwTrack, dbg_mcierr(err));
395     trace("last track position: %02um:%02us:%02uframes\n",
396           MCI_MSF_MINUTE(parm.status.dwReturn), MCI_MSF_SECOND(parm.status.dwReturn), MCI_MSF_FRAME(parm.status.dwReturn));
397
398     /* Seek to position + length always works, esp.
399      * for the last track it's NOT the position of the lead-out. */
400     parm.seek.dwTo = MSF_Add(parm.status.dwReturn, duration);
401     err = mciSendCommand(wDeviceID, MCI_SEEK, MCI_TO, (DWORD_PTR)&parm);
402     ok(!err, "SEEK to %08X position last + length: %s\n", parm.seek.dwTo, dbg_mcierr(err));
403
404     parm.seek.dwTo = MSF_Add(parm.seek.dwTo, MCI_MAKE_MSF(0,0,1));
405     err = mciSendCommand(wDeviceID, MCI_SEEK, MCI_TO, (DWORD_PTR)&parm);
406     ok(err == MCIERR_OUTOFRANGE, "SEEK past %08X position last + length: %s\n", parm.seek.dwTo, dbg_mcierr(err));
407
408     err = mciSendString("set c time format tmsf", buf, sizeof(buf), hwnd);
409     ok(!err, "set time format tmsf: %s\n", dbg_mcierr(err));
410
411     parm.play.dwFrom = track;
412     err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_NOTIFY, (DWORD_PTR)&parm);
413     ok(!err, "PLAY from %u notify: %s\n", track, dbg_mcierr(err));
414
415     if(err) {
416         skip("Cannot manage to play track %u.\n", track);
417         return;
418     }
419
420     Sleep(1800*factor); /* Time to spin up, hopefully less than track length */
421
422     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
423     ok(!err, "status mode: %s\n", dbg_mcierr(err));
424     if(!err) ok(!strcmp(buf, "playing"), "status mode during play is %s\n", buf);
425
426     err = mciSendString("pause c", buf, sizeof(buf), hwnd);
427     ok(!err, "pause: %s\n", dbg_mcierr(err));
428
429     test_notification(hwnd, "pause should abort notification", MCI_NOTIFY_ABORTED);
430
431     /* Native returns stopped when paused,
432      * yet the Stop command is different as it would disallow Resume. */
433     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
434     ok(!err, "status mode: %s\n", dbg_mcierr(err));
435     if(!err) todo_wine ok(!strcmp(buf, "stopped"), "status mode while paused is %s\n", buf);
436
437     err = mciSendCommand(wDeviceID, MCI_RESUME, 0, 0);
438     ok(!err || /* Win9x */ err == MCIERR_UNSUPPORTED_FUNCTION,
439        "RESUME without parms: %s\n", dbg_mcierr(err));
440
441     Sleep(1300*factor);
442
443     /* Native continues to play without interruption */
444     err = mciSendCommand(wDeviceID, MCI_PLAY, 0, 0);
445     todo_wine ok(!err, "PLAY without parms: %s\n", dbg_mcierr(err));
446
447     parm.play.dwFrom = MCI_MAKE_TMSF(numtracks,0,1,0);
448     parm.play.dwTo = 1;
449     err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_TO, (DWORD_PTR)&parm);
450     ok(err == MCIERR_OUTOFRANGE, "PLAY: %s\n", dbg_mcierr(err));
451
452     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
453     ok(!err, "status mode: %s\n", dbg_mcierr(err));
454     if(!err) ok(!strcmp(buf, "playing"), "status mode after play is %s\n", buf);
455
456     err = mciSendCommand(wDeviceID, MCI_STOP, MCI_NOTIFY, (DWORD_PTR)&parm);
457     ok(!err, "STOP notify: %s\n", dbg_mcierr(err));
458     test_notification(hwnd, "STOP notify", MCI_NOTIFY_SUCCESSFUL);
459     test_notification(hwnd, "STOP #1", 0);
460
461     parm.play.dwFrom = track;
462     err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_NOTIFY, (DWORD_PTR)&parm);
463     ok(!err, "PLAY from %u notify: %s\n", track, dbg_mcierr(err));
464
465     Sleep(1600*factor);
466
467     parm.seek.dwTo = 1; /* not <track>, to test position below */
468     err = mciSendCommand(wDeviceID, MCI_SEEK, MCI_TO, (DWORD_PTR)&parm);
469     ok(!err, "SEEK to %u notify: %s\n", track, dbg_mcierr(err));
470     /* Note that native's Status position / current track may move the head
471      * and reflect the new position only seconds after issuing the command. */
472
473     /* Seek stops */
474     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
475     ok(!err, "status mode: %s\n", dbg_mcierr(err));
476     if(!err) ok(!strcmp(buf, "stopped"), "status mode after play is %s\n", buf);
477
478     test_notification(hwnd, "Seek aborts Play", MCI_NOTIFY_ABORTED);
479     test_notification(hwnd, "Seek", 0);
480
481     parm.play.dwFrom = track;
482     parm.play.dwTo = MCI_MAKE_TMSF(track,0,0,21); /* 21 frames, subsecond */
483     err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_TO|MCI_NOTIFY, (DWORD_PTR)&parm);
484     ok(!err, "PLAY from %u notify: %s\n", track, dbg_mcierr(err));
485
486     Sleep(2200*factor);
487
488     err = mciSendString("status c mode", buf, sizeof(buf), hwnd);
489     ok(!err, "status mode: %s\n", dbg_mcierr(err));
490     if(!err) ok(!strcmp(buf, "stopped") || broken(!strcmp(buf, "playing")), "status mode after play is %s\n", buf);
491     if(!err && !strcmp(buf, "playing")) trace("status playing after sleep\n");
492
493     /* Playing to end asynchronously sends no notification! */
494     test_notification(hwnd, "PLAY to end", 0);
495
496     err = mciSendString("status c mode notify", buf, sizeof(buf), hwnd);
497     ok(!err, "status mode: %s\n", dbg_mcierr(err));
498     if(!err) ok(!strcmp(buf, "stopped") || broken(!strcmp(buf, "playing")), "status mode after play is %s\n", buf);
499     if(!err && !strcmp(buf, "playing")) trace("status still playing\n");
500     /* Some systems report playing even after Sleep(3900ms) yet the successful
501      * notification tests (not ABORTED) indicates they are finished. */
502
503     test_notification(hwnd, "dangling from PLAY", MCI_NOTIFY_SUPERSEDED);
504     test_notification(hwnd, "status mode", MCI_NOTIFY_SUCCESSFUL);
505
506     err = mciSendString("stop c", buf, sizeof(buf), hwnd);
507     ok(!err, "stop: %s\n", dbg_mcierr(err));
508
509     test_notification(hwnd, "PLAY to end", 0);
510
511     /* length as MSF despite set time format TMSF */
512     parm.status.dwItem = MCI_STATUS_LENGTH;
513     parm.status.dwTrack = numtracks;
514     parm.status.dwReturn = 0xFEEDABAD;
515     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK, (DWORD_PTR)&parm);
516     ok(!err, "STATUS length track %u: %s\n", parm.status.dwTrack, dbg_mcierr(err));
517     ok(duration == parm.status.dwReturn, "length MSF<>TMSF %08lX\n", parm.status.dwReturn);
518
519     /* Play from position start to start+length always works. */
520     /* TODO? also play it using MSF */
521     parm.play.dwFrom = numtracks;
522     parm.play.dwTo = (duration << 8) | numtracks; /* as TMSF */
523     err = mciSendCommand(wDeviceID, MCI_PLAY, MCI_FROM|MCI_TO|MCI_NOTIFY, (DWORD_PTR)&parm);
524     ok(!err, "PLAY (TMSF) from %08X to %08X: %s\n", parm.play.dwFrom, parm.play.dwTo, dbg_mcierr(err));
525
526     Sleep(1400*factor);
527
528     err = mciSendString("status c current track", buf, sizeof(buf), hwnd);
529     ok(!err, "status track: %s\n", dbg_mcierr(err));
530     if(!err) todo_wine ok(numtracks == atoi(buf), "status current track gave %s, expected %u\n", buf, numtracks);
531     /* fails in Wine because SEEK is independent on IOCTL_CDROM_RAW_READ */
532
533     err = mciSendCommand(wDeviceID, MCI_STOP, 0, 0);
534     ok(!err, "STOP: %s\n", dbg_mcierr(err));
535     test_notification(hwnd, "STOP aborts", MCI_NOTIFY_ABORTED);
536     test_notification(hwnd, "STOP final", 0);
537 }
538
539 static void test_openclose(HWND hwnd)
540 {
541     MCIDEVICEID wDeviceID;
542     MCI_PARMS_UNION parm;
543     MCIERROR err;
544     char drive[] = {'a',':','\\','X','\0'};
545
546     /* Bug in native since NT: After OPEN "c" without MCI_OPEN_ALIAS fails with
547      * MCIERR_DEVICE_OPEN, any subsequent OPEN fails with EXTENSION_NOT_FOUND! */
548     parm.open.lpstrAlias = "x"; /* with alias, OPEN "c" behaves normally */
549     parm.open.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
550     parm.open.lpstrElementName = drive;
551     for ( ; strlen(drive); drive[strlen(drive)-1] = 0)
552     for (drive[0] = 'a'; drive[0] <= 'z'; drive[0]++) {
553         err = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT | MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID
554                               | MCI_OPEN_SHAREABLE | MCI_OPEN_ALIAS, (DWORD_PTR)&parm);
555         ok(!err || err == MCIERR_INVALID_FILE, "OPEN %s type: %s\n", drive, dbg_mcierr(err));
556         /* open X:\ fails in Win9x/NT. Only open X: works everywhere. */
557         if(!err) {
558             wDeviceID = parm.open.wDeviceID;
559             trace("ok with %s\n", drive);
560             err = mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0);
561             ok(!err,"mciCommand close returned %s\n", dbg_mcierr(err));
562         }
563     }
564     drive[0] = '\\';
565     err = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID
566                           | MCI_OPEN_SHAREABLE, (DWORD_PTR)&parm);
567     ok(err == MCIERR_INVALID_FILE, "OPEN %s type: %s\n", drive, dbg_mcierr(err));
568     if(!err) mciSendCommand(parm.open.wDeviceID, MCI_CLOSE, 0, 0);
569
570     if (0) {
571         parm.open.lpstrElementName = (LPCSTR)0xDEADBEEF;
572         err = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT|MCI_OPEN_ELEMENT_ID
573                               | MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE, (DWORD_PTR)&parm);
574         todo_wine ok(err == MCIERR_FLAGS_NOT_COMPATIBLE, "OPEN elt_ID: %s\n", dbg_mcierr(err));
575         if(!err) mciSendCommand(parm.open.wDeviceID, MCI_CLOSE, 0, 0);
576     }
577 }
578
579 START_TEST(mcicda)
580 {
581     MCIERROR err;
582     HWND hwnd;
583     hwnd = CreateWindowExA(0, "static", "mcicda test", WS_POPUP, 0,0,100,100,
584                            0, 0, 0, NULL);
585     test_notification(hwnd, "-prior to tests-", 0);
586     test_play(hwnd);
587     test_openclose(hwnd);
588     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_STOP, 0, 0);
589     todo_wine ok(!err || broken(err == MCIERR_HARDWARE /* blank CD or testbot without CD-ROM */),
590        "STOP all returned %s\n", dbg_mcierr(err));
591     err = mciSendString("close all", NULL, 0, hwnd);
592     ok(!err, "final close all returned %s\n", dbg_mcierr(err));
593     test_notification(hwnd, "-tests complete-", 0);
594     DestroyWindow(hwnd);
595 }