comctl32: Add implementation of LVS_EX_ONECLICKACTIVATE.
[wine] / dlls / inetcomm / tests / mimeintl.c
1 /*
2  * MimeInternational tests
3  *
4  * Copyright 2008 Huw Davies
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 #define COBJMACROS
22 #define NONAMELESSUNION
23
24 #include "windows.h"
25 #include "ole2.h"
26 #include "ocidl.h"
27
28 #include "mimeole.h"
29
30 #include "initguid.h"
31 #include "mlang.h"
32
33 #include <stdio.h>
34 #include <assert.h>
35
36 #include "wine/test.h"
37
38 static void test_create(void)
39 {
40     IMimeInternational *internat, *internat2;
41     HRESULT hr;
42     ULONG ref;
43
44     hr = MimeOleGetInternat(&internat);
45     ok(hr == S_OK, "ret %08x\n", hr);
46     hr = MimeOleGetInternat(&internat2);
47     ok(hr == S_OK, "ret %08x\n", hr);
48
49     /* Under w2k8 it's no longer a singleton */
50     if(internat == internat2)
51     {
52         /* test to show that the object is a singleton with
53            a reference held by the dll. */
54         ref = IMimeInternational_Release(internat2);
55         ok(ref == 2, "got %d\n", ref);
56
57         ref = IMimeInternational_Release(internat);
58         ok(ref == 1, "got %d\n", ref);
59     }
60     else
61     {
62         ref = IMimeInternational_Release(internat2);
63         ok(ref == 0, "got %d\n", ref);
64
65         ref = IMimeInternational_Release(internat);
66         ok(ref == 0, "got %d\n", ref);
67     }
68
69 }
70
71 static inline HRESULT get_mlang(IMultiLanguage **ml)
72 {
73     return CoCreateInstance(&CLSID_CMultiLanguage, NULL,  CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
74                             &IID_IMultiLanguage, (void **)ml);
75 }
76
77 static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
78 {
79     DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
80     BSTR bstr = SysAllocStringLen(NULL, len - 1);
81     HRESULT hr;
82     IMultiLanguage *ml;
83
84     MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);
85
86     hr = get_mlang(&ml);
87
88     if(SUCCEEDED(hr))
89     {
90         hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
91         IMultiLanguage_Release(ml);
92     }
93     SysFreeString(bstr);
94     if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
95     return hr;
96 }
97
98 static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
99 {
100     HRESULT hr;
101     IMultiLanguage *ml;
102
103     hr = get_mlang(&ml);
104
105     if(SUCCEEDED(hr))
106     {
107         hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
108         IMultiLanguage_Release(ml);
109     }
110     return hr;
111 }
112
113 static HRESULT mlang_getcsetinfo_from_cp(UINT cp, CHARSETTYPE charset_type, MIMECSETINFO *mlang_info)
114 {
115     MIMECPINFO mlang_cp_info;
116     WCHAR *charset_name;
117     HRESULT hr;
118     IMultiLanguage *ml;
119
120     hr = mlang_getcodepageinfo(cp, &mlang_cp_info);
121     if(FAILED(hr)) return hr;
122
123     switch(charset_type)
124     {
125     case CHARSET_BODY:
126         charset_name = mlang_cp_info.wszBodyCharset;
127         break;
128     case CHARSET_HEADER:
129         charset_name = mlang_cp_info.wszHeaderCharset;
130         break;
131     case CHARSET_WEB:
132         charset_name = mlang_cp_info.wszWebCharset;
133         break;
134     }
135
136     hr = get_mlang(&ml);
137
138     if(SUCCEEDED(hr))
139     {
140         hr = IMultiLanguage_GetCharsetInfo(ml, charset_name, mlang_info);
141         IMultiLanguage_Release(ml);
142     }
143     return hr;
144 }
145
146 static void test_charset(void)
147 {
148     IMimeInternational *internat;
149     HRESULT hr;
150     HCHARSET hcs, hcs_windows_1252, hcs_windows_1251;
151     INETCSETINFO cs_info;
152     MIMECSETINFO mlang_cs_info;
153
154     hr = MimeOleGetInternat(&internat);
155     ok(hr == S_OK, "ret %08x\n", hr);
156
157     hr = IMimeInternational_FindCharset(internat, "non-existent", &hcs);
158     ok(hr == MIME_E_NOT_FOUND, "got %08x\n", hr);
159
160     hr = IMimeInternational_FindCharset(internat, "windows-1252", &hcs_windows_1252);
161     ok(hr == S_OK, "got %08x\n", hr);
162     hr = IMimeInternational_FindCharset(internat, "windows-1252", &hcs);
163     ok(hr == S_OK, "got %08x\n", hr);
164     ok(hcs_windows_1252 == hcs, "got different hcharsets for the same name\n");
165     hr = IMimeInternational_FindCharset(internat, "WiNdoWs-1252", &hcs);
166     ok(hr == S_OK, "got %08x\n", hr);
167     ok(hcs_windows_1252 == hcs, "got different hcharsets for the same name\n");
168
169     hr = IMimeInternational_FindCharset(internat, "windows-1251", &hcs_windows_1251);
170     ok(hr == S_OK, "got %08x\n", hr);
171     ok(hcs_windows_1252 != hcs_windows_1251, "got the same hcharset for the different names\n");
172
173     hr = IMimeInternational_GetCharsetInfo(internat, hcs_windows_1252, &cs_info);
174     ok(hr == S_OK, "got %08x\n", hr);
175
176     hr = mlang_getcsetinfo("windows-1252", &mlang_cs_info);
177     ok(hr == S_OK, "got %08x\n", hr);
178     ok(cs_info.cpiWindows == mlang_cs_info.uiCodePage, "cpiWindows %d while mlang uiCodePage %d\n",
179        cs_info.cpiWindows, mlang_cs_info.uiCodePage);
180     ok(cs_info.cpiInternet == mlang_cs_info.uiInternetEncoding, "cpiInternet %d while mlang uiInternetEncoding %d\n",
181        cs_info.cpiInternet, mlang_cs_info.uiInternetEncoding);
182     ok(cs_info.hCharset == hcs_windows_1252, "hCharset doesn't match requested\n");
183     ok(!strcmp(cs_info.szName, "windows-1252"), "szName doesn't match requested\n");
184
185     hr = IMimeInternational_GetCodePageCharset(internat, 1252, CHARSET_BODY, &hcs);
186     ok(hr == S_OK, "got %08x\n", hr);
187     hr = IMimeInternational_GetCharsetInfo(internat, hcs, &cs_info);
188     ok(hr == S_OK, "got %08x\n", hr);
189
190     hr = mlang_getcsetinfo_from_cp(1252, CHARSET_BODY, &mlang_cs_info);
191     ok(hr == S_OK, "got %08x\n", hr);
192     ok(cs_info.cpiWindows == mlang_cs_info.uiCodePage, "cpiWindows %d while mlang uiCodePage %d\n",
193        cs_info.cpiWindows, mlang_cs_info.uiCodePage);
194     ok(cs_info.cpiInternet == mlang_cs_info.uiInternetEncoding, "cpiInternet %d while mlang uiInternetEncoding %d\n",
195        cs_info.cpiInternet, mlang_cs_info.uiInternetEncoding);
196
197     IMimeInternational_Release(internat);
198 }
199
200 static void test_defaultcharset(void)
201 {
202     IMimeInternational *internat;
203     HRESULT hr;
204     HCHARSET hcs_default, hcs, hcs_windows_1251;
205
206     hr = MimeOleGetInternat(&internat);
207     ok(hr == S_OK, "ret %08x\n", hr);
208
209     hr = IMimeInternational_GetDefaultCharset(internat, &hcs_default);
210     ok(hr == S_OK, "ret %08x\n", hr);
211     hr = IMimeInternational_GetCodePageCharset(internat, GetACP(), CHARSET_BODY, &hcs);
212     ok(hr == S_OK, "ret %08x\n", hr);
213     ok(hcs_default == hcs, "Unexpected default charset\n");
214
215     hr = IMimeInternational_FindCharset(internat, "windows-1251", &hcs_windows_1251);
216     ok(hr == S_OK, "got %08x\n", hr);
217     hr = IMimeInternational_SetDefaultCharset(internat, hcs_windows_1251);
218     ok(hr == S_OK, "ret %08x\n", hr);
219     hr = IMimeInternational_GetDefaultCharset(internat, &hcs);
220     ok(hr == S_OK, "ret %08x\n", hr);
221     ok(hcs == hcs_windows_1251, "didn't retrieve recently set default\n");
222     /* Set the old default back again */
223     hr = IMimeInternational_SetDefaultCharset(internat, hcs_default);
224     ok(hr == S_OK, "ret %08x\n", hr);
225
226     IMimeInternational_Release(internat);
227 }
228
229 static void test_convert(void)
230 {
231     IMimeInternational *internat;
232     HRESULT hr;
233     BLOB src, dst;
234     ULONG read;
235     PROPVARIANT prop_in, prop_out;
236     static char test_string[] = "test string";
237     static WCHAR test_stringW[] = {'t','e','s','t',' ','s','t','r','i','n','g',0};
238
239     hr = MimeOleGetInternat(&internat);
240     ok(hr == S_OK, "ret %08x\n", hr);
241
242     src.pBlobData = (BYTE*)test_string;
243     src.cbSize = sizeof(test_string);
244     hr = IMimeInternational_ConvertBuffer(internat, 1252, 28591, &src, &dst, &read);
245     ok(hr == S_OK, "ret %08x\n", hr);
246     ok(read == sizeof(test_string), "got %d\n", read);
247     ok(dst.cbSize == sizeof(test_string), "got %d\n", dst.cbSize);
248     CoTaskMemFree(dst.pBlobData);
249
250     src.cbSize = 2;
251     hr = IMimeInternational_ConvertBuffer(internat, 1252, 28591, &src, &dst, &read);
252     ok(hr == S_OK, "ret %08x\n", hr);
253     ok(read == 2, "got %d\n", read);
254     ok(dst.cbSize == 2, "got %d\n", dst.cbSize);
255     CoTaskMemFree(dst.pBlobData);
256
257     prop_in.vt = VT_LPWSTR;
258     prop_in.u.pwszVal = test_stringW;
259     hr = IMimeInternational_ConvertString(internat, CP_UNICODE, 1252, &prop_in, &prop_out);
260     ok(hr == S_OK, "ret %08x\n", hr);
261     ok(prop_out.vt == VT_LPSTR, "got %d\n", prop_out.vt);
262     ok(!strcmp(prop_out.u.pszVal, test_string), "got %s\n", prop_out.u.pszVal);
263     PropVariantClear(&prop_out);
264
265     /* If in.vt is VT_LPWSTR, ignore cpiSrc */
266     prop_in.vt = VT_LPWSTR;
267     prop_in.u.pwszVal = test_stringW;
268     hr = IMimeInternational_ConvertString(internat, 28591, 1252, &prop_in, &prop_out);
269     ok(hr == S_OK, "ret %08x\n", hr);
270     ok(prop_out.vt == VT_LPSTR, "got %d\n", prop_out.vt);
271     ok(!strcmp(prop_out.u.pszVal, test_string), "got %s\n", prop_out.u.pszVal);
272     PropVariantClear(&prop_out);
273
274     prop_in.vt = VT_LPSTR;
275     prop_in.u.pszVal = test_string;
276     hr = IMimeInternational_ConvertString(internat, 28591, CP_UNICODE, &prop_in, &prop_out);
277     ok(hr == S_OK, "ret %08x\n", hr);
278     ok(prop_out.vt == VT_LPWSTR, "got %d\n", prop_out.vt);
279     ok(!lstrcmpW(prop_out.u.pwszVal, test_stringW), "mismatched strings\n");
280     PropVariantClear(&prop_out);
281
282     /* If in.vt is VT_LPSTR and cpiSrc is CP_UNICODE, use another multibyte codepage (probably GetACP()) */
283     prop_in.vt = VT_LPSTR;
284     prop_in.u.pszVal = test_string;
285     hr = IMimeInternational_ConvertString(internat, CP_UNICODE, CP_UNICODE, &prop_in, &prop_out);
286     ok(hr == S_OK, "ret %08x\n", hr);
287     ok(prop_out.vt == VT_LPWSTR, "got %d\n", prop_out.vt);
288     ok(!lstrcmpW(prop_out.u.pwszVal, test_stringW), "mismatched strings\n");
289     PropVariantClear(&prop_out);
290
291     IMimeInternational_Release(internat);
292 }
293
294 START_TEST(mimeintl)
295 {
296     OleInitialize(NULL);
297     test_create();
298     test_charset();
299     test_defaultcharset();
300     test_convert();
301     OleUninitialize();
302 }