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