Remove redundant check.
[wine] / dlls / urlmon / tests / misc.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define COBJMACROS
20
21 #include <wine/test.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28
29 static void test_CreateFormatEnum(void)
30 {
31     IEnumFORMATETC *fenum = NULL, *fenum2 = NULL;
32     FORMATETC fetc[5];
33     ULONG ul;
34     HRESULT hres;
35
36     static DVTARGETDEVICE dev = {sizeof(dev),0,0,0,0,{0}};
37     static FORMATETC formatetc[] = {
38         {0,&dev,0,0,0},
39         {0,&dev,0,1,0},
40         {0,NULL,0,2,0},
41         {0,NULL,0,3,0},
42         {0,NULL,0,4,0}
43     };
44
45     hres = CreateFormatEnumerator(0, formatetc, &fenum);
46     ok(hres == E_FAIL, "CreateFormatEnumerator failed: %08lx, expected E_FAIL\n", hres);
47     hres = CreateFormatEnumerator(0, formatetc, NULL);
48     ok(hres == E_INVALIDARG, "CreateFormatEnumerator failed: %08lx, expected E_INVALIDARG\n", hres);
49     hres = CreateFormatEnumerator(5, formatetc, NULL);
50     ok(hres == E_INVALIDARG, "CreateFormatEnumerator failed: %08lx, expected E_INVALIDARG\n", hres);
51
52
53     hres = CreateFormatEnumerator(5, formatetc, &fenum);
54     ok(hres == S_OK, "CreateFormatEnumerator failed: %08lx\n", hres);
55     if(FAILED(hres))
56         return;
57
58     hres = IEnumFORMATETC_Next(fenum, 2, NULL, &ul);
59     ok(hres == E_INVALIDARG, "Next failed: %08lx, expected E_INVALIDARG\n", hres);
60     ul = 100;
61     hres = IEnumFORMATETC_Next(fenum, 0, fetc, &ul);
62     ok(hres == S_OK, "Next failed: %08lx\n", hres);
63     ok(ul == 0, "ul=%ld, expected 0\n", ul);
64
65     hres = IEnumFORMATETC_Next(fenum, 2, fetc, &ul);
66     ok(hres == S_OK, "Next failed: %08lx\n", hres);
67     ok(fetc[0].lindex == 0, "fetc[0].lindex=%ld, expected 0\n", fetc[0].lindex);
68     ok(fetc[1].lindex == 1, "fetc[1].lindex=%ld, expected 1\n", fetc[1].lindex);
69     ok(fetc[0].ptd == &dev, "fetc[0].ptd=%p, expected %p\n", fetc[0].ptd, &dev);
70     ok(ul == 2, "ul=%ld, expected 2\n", ul);
71
72     hres = IEnumFORMATETC_Skip(fenum, 1);
73     ok(hres == S_OK, "Skip failed: %08lx\n", hres);
74
75     hres = IEnumFORMATETC_Next(fenum, 4, fetc, &ul);
76     ok(hres == S_FALSE, "Next failed: %08lx, expected S_FALSE\n", hres);
77     ok(fetc[0].lindex == 3, "fetc[0].lindex=%ld, expected 3\n", fetc[0].lindex);
78     ok(fetc[1].lindex == 4, "fetc[1].lindex=%ld, expected 4\n", fetc[1].lindex);
79     ok(fetc[0].ptd == NULL, "fetc[0].ptd=%p, expected NULL\n", fetc[0].ptd);
80     ok(ul == 2, "ul=%ld, expected 2\n", ul);
81
82     hres = IEnumFORMATETC_Next(fenum, 4, fetc, &ul);
83     ok(hres == S_FALSE, "Next failed: %08lx, expected S_FALSE\n", hres);
84     ok(ul == 0, "ul=%ld, expected 0\n", ul);
85     ul = 100;
86     hres = IEnumFORMATETC_Next(fenum, 0, fetc, &ul);
87     ok(hres == S_OK, "Next failed: %08lx\n", hres);
88     ok(ul == 0, "ul=%ld, expected 0\n", ul);
89
90     hres = IEnumFORMATETC_Skip(fenum, 3);
91     ok(hres == S_FALSE, "Skip failed: %08lx, expected S_FALSE\n", hres);
92
93     hres = IEnumFORMATETC_Reset(fenum);
94     ok(hres == S_OK, "Reset failed: %08lx\n", hres);
95
96     hres = IEnumFORMATETC_Next(fenum, 5, fetc, NULL);
97     ok(hres == S_OK, "Next failed: %08lx\n", hres);
98     ok(fetc[0].lindex == 0, "fetc[0].lindex=%ld, expected 0\n", fetc[0].lindex);
99
100     hres = IEnumFORMATETC_Reset(fenum);
101     ok(hres == S_OK, "Reset failed: %08lx\n", hres);
102
103     hres = IEnumFORMATETC_Skip(fenum, 2);
104     ok(hres == S_OK, "Skip failed: %08lx\n", hres);
105
106     hres = IEnumFORMATETC_Clone(fenum, NULL);
107     ok(hres == E_INVALIDARG, "Clone failed: %08lx, expected E_INVALIDARG\n", hres);
108
109     hres = IEnumFORMATETC_Clone(fenum, &fenum2);
110     ok(hres == S_OK, "Clone failed: %08lx\n", hres);
111
112     if(SUCCEEDED(hres)) {
113         ok(fenum != fenum2, "fenum == fenum2\n");
114
115         hres = IEnumFORMATETC_Next(fenum2, 2, fetc, &ul);
116         ok(hres == S_OK, "Next failed: %08lx\n", hres);
117         ok(fetc[0].lindex == 2, "fetc[0].lindex=%ld, expected 2\n", fetc[0].lindex);
118
119         IEnumFORMATETC_Release(fenum2);
120     }
121
122     hres = IEnumFORMATETC_Next(fenum, 2, fetc, &ul);
123     ok(hres == S_OK, "Next failed: %08lx\n", hres);
124     ok(fetc[0].lindex == 2, "fetc[0].lindex=%ld, expected 2\n", fetc[0].lindex);
125
126     hres = IEnumFORMATETC_Skip(fenum, 1);
127     ok(hres == S_OK, "Skip failed: %08lx\n", hres);
128     
129     IEnumFORMATETC_Release(fenum);
130 }
131
132 static void test_RegisterFormatEnumerator(void)
133 {
134     IBindCtx *bctx = NULL;
135     IEnumFORMATETC *format = NULL, *format2 = NULL;
136     IUnknown *unk = NULL;
137     HRESULT hres;
138
139     static FORMATETC formatetc = {0,NULL,0,0,0};
140     static WCHAR wszEnumFORMATETC[] =
141         {'_','E','n','u','m','F','O','R','M','A','T','E','T','C','_',0};
142
143     CreateBindCtx(0, &bctx);
144
145     hres = CreateFormatEnumerator(1, &formatetc, &format);
146     ok(hres == S_OK, "CreateFormatEnumerator failed: %08lx\n", hres);
147     if(FAILED(hres))
148         return;
149
150     hres = RegisterFormatEnumerator(NULL, format, 0);
151     ok(hres == E_INVALIDARG,
152             "RegisterFormatEnumerator failed: %08lx, expected E_INVALIDARG\n", hres);
153     hres = RegisterFormatEnumerator(bctx, NULL, 0);
154     ok(hres == E_INVALIDARG,
155             "RegisterFormatEnumerator failed: %08lx, expected E_INVALIDARG\n", hres);
156
157     hres = RegisterFormatEnumerator(bctx, format, 0);
158     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08lx\n", hres);
159
160     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
161     ok(hres == S_OK, "GetObjectParam failed: %08lx\n", hres);
162     ok(unk == (IUnknown*)format, "unk != format\n");
163
164     hres = RevokeFormatEnumerator(NULL, format);
165     ok(hres == E_INVALIDARG,
166             "RevokeFormatEnumerator failed: %08lx, expected E_INVALIDARG\n", hres);
167
168     hres = RevokeFormatEnumerator(bctx, format);
169     ok(hres == S_OK, "RevokeFormatEnumerator failed: %08lx\n", hres);
170
171     hres = RevokeFormatEnumerator(bctx, format);
172     ok(hres == E_FAIL, "RevokeFormatEnumerator failed: %08lx, expected E_FAIL\n", hres);
173
174     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
175     ok(hres == E_FAIL, "GetObjectParam failed: %08lx, expected E_FAIL\n", hres);
176
177     hres = RegisterFormatEnumerator(bctx, format, 0);
178     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08lx\n", hres);
179
180     hres = CreateFormatEnumerator(1, &formatetc, &format2);
181     ok(hres == S_OK, "CreateFormatEnumerator failed: %08lx\n", hres);
182
183     if(SUCCEEDED(hres)) {
184         hres = RevokeFormatEnumerator(bctx, format);
185         ok(hres == S_OK, "RevokeFormatEnumerator failed: %08lx\n", hres);
186
187         IEnumFORMATETC_Release(format2);
188     }
189
190     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
191     ok(hres == E_FAIL, "GetObjectParam failed: %08lx, expected E_FAIL\n", hres);
192
193     IEnumFORMATETC_Release(format);
194
195     hres = RegisterFormatEnumerator(bctx, format, 0);
196     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08lx\n", hres);
197     hres = RevokeFormatEnumerator(bctx, NULL);
198     ok(hres == S_OK, "RevokeFormatEnumerator failed: %08lx\n", hres);
199     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
200     ok(hres == E_FAIL, "GetObjectParam failed: %08lx, expected E_FAIL\n", hres);
201
202     IBindCtx_Release(bctx);
203 }
204
205 static const WCHAR url1[] = {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',
206         '/','b','l','a','n','k','.','h','t','m',0};
207 static const WCHAR url2[] = {'i','n','d','e','x','.','h','t','m',0};
208 static const WCHAR url3[] = {'f','i','l','e',':','c',':','\\','I','n','d','e','x','.','h','t','m',0};
209 static const WCHAR url4[] = {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e',
210         '%','2','E','j','p','g',0};
211
212 static const WCHAR url4e[] = {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e',
213         '.','j','p','g',0};
214
215 static const WCHAR wszRes[] = {'r','e','s',0};
216 static const WCHAR wszFile[] = {'f','i','l','e',0};
217 static const WCHAR wszEmpty[] = {0};
218
219 struct parse_test {
220     LPCWSTR url;
221     LPCWSTR encoded_url;
222     LPCWSTR schema;
223 };
224
225 static const struct parse_test parse_tests[] = {
226     {url1,  url1,   wszRes},
227     {url2,  url2,   wszEmpty},
228     {url3,  url3,   wszFile},
229     {url4,  url4e,  wszFile}
230 };
231
232 static void test_CoInternetParseUrl(void)
233 {
234     HRESULT hres;
235     DWORD size;
236     int i;
237
238     static WCHAR buf[4096];
239
240     memset(buf, 0xf0, sizeof(buf));
241     hres = CoInternetParseUrl(parse_tests[0].url, PARSE_SCHEMA, 0, buf,
242             3, &size, 0);
243     ok(hres == E_POINTER, "schema failed: %08lx, expected E_POINTER\n", hres);
244
245     for(i=0; i < sizeof(parse_tests)/sizeof(parse_tests[0]); i++) {
246         memset(buf, 0xf0, sizeof(buf));
247         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_ENCODE, 0, buf,
248                 sizeof(buf)/sizeof(WCHAR), &size, 0);
249         ok(hres == S_OK, "[%d] encoding failed: %08lx\n", i, hres);
250         ok(size == lstrlenW(parse_tests[i].encoded_url), "[%d] wrong size\n", i);
251         ok(!lstrcmpW(parse_tests[i].encoded_url, buf), "[%d] wrong encoded url\n", i);
252
253         memset(buf, 0xf0, sizeof(buf));
254         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_SCHEMA, 0, buf,
255                 sizeof(buf)/sizeof(WCHAR), &size, 0);
256         ok(hres == S_OK, "[%d] schema failed: %08lx\n", i, hres);
257         ok(size == lstrlenW(parse_tests[i].schema), "[%d] wrong size\n", i);
258         ok(!lstrcmpW(parse_tests[i].schema, buf), "[%d] wrong schema\n", i);
259     }
260 }
261
262 START_TEST(misc)
263 {
264     test_CreateFormatEnum();
265     test_RegisterFormatEnumerator();
266     test_CoInternetParseUrl();
267 }