inetcomm/tests: Add tests for IMimeInternational_ConvertString.
[wine.git] / dlls / inetcomm / tests / mimeintl.c
blobe7cdd87a28b546faa557ff6641893571b2e6ec22
1 /*
2 * MimeInternational tests
4 * Copyright 2008 Huw Davies
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.
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.
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
21 #define COBJMACROS
22 #define NONAMELESSUNION
24 #include "windows.h"
25 #include "ole2.h"
26 #include "ocidl.h"
28 #include "mimeole.h"
30 #include "initguid.h"
31 #include "mlang.h"
33 #include <stdio.h>
34 #include <assert.h>
36 #include "wine/test.h"
38 static void test_create(void)
40 IMimeInternational *internat, *internat2;
41 HRESULT hr;
42 ULONG ref;
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);
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);
55 IMimeInternational_Release(internat);
58 static inline HRESULT get_mlang(IMultiLanguage **ml)
60 return CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
61 &IID_IMultiLanguage, (void **)ml);
64 static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
66 DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
67 BSTR bstr = SysAllocStringLen(NULL, len - 1);
68 HRESULT hr;
69 IMultiLanguage *ml;
71 MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);
73 hr = get_mlang(&ml);
75 if(SUCCEEDED(hr))
77 hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
78 IMultiLanguage_Release(ml);
80 SysFreeString(bstr);
81 if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
82 return hr;
85 static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
87 HRESULT hr;
88 IMultiLanguage *ml;
90 hr = get_mlang(&ml);
92 if(SUCCEEDED(hr))
94 hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
95 IMultiLanguage_Release(ml);
97 return hr;
100 static HRESULT mlang_getcsetinfo_from_cp(UINT cp, CHARSETTYPE charset_type, MIMECSETINFO *mlang_info)
102 MIMECPINFO mlang_cp_info;
103 WCHAR *charset_name;
104 HRESULT hr;
105 IMultiLanguage *ml;
107 hr = mlang_getcodepageinfo(cp, &mlang_cp_info);
108 if(FAILED(hr)) return hr;
110 switch(charset_type)
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;
123 hr = get_mlang(&ml);
125 if(SUCCEEDED(hr))
127 hr = IMultiLanguage_GetCharsetInfo(ml, charset_name, mlang_info);
128 IMultiLanguage_Release(ml);
130 return hr;
133 static void test_charset(void)
135 IMimeInternational *internat;
136 HRESULT hr;
137 HCHARSET hcs, hcs_windows_1252, hcs_windows_1251;
138 INETCSETINFO cs_info;
139 MIMECSETINFO mlang_cs_info;
141 hr = MimeOleGetInternat(&internat);
142 ok(hr == S_OK, "ret %08x\n", hr);
144 hr = IMimeInternational_FindCharset(internat, "non-existent", &hcs);
145 ok(hr == MIME_E_NOT_FOUND, "got %08x\n", hr);
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");
153 hr = IMimeInternational_FindCharset(internat, "windows-1251", &hcs_windows_1251);
154 ok(hr == S_OK, "got %08x\n", hr);
155 ok(hcs_windows_1252 != hcs_windows_1251, "got the same hcharset for the different names\n");
157 hr = IMimeInternational_GetCharsetInfo(internat, hcs_windows_1252, &cs_info);
158 ok(hr == S_OK, "got %08x\n", hr);
160 hr = mlang_getcsetinfo("windows-1252", &mlang_cs_info);
161 ok(hr == S_OK, "got %08x\n", hr);
162 ok(cs_info.cpiWindows == mlang_cs_info.uiCodePage, "cpiWindows %d while mlang uiCodePage %d\n",
163 cs_info.cpiWindows, mlang_cs_info.uiCodePage);
164 ok(cs_info.cpiInternet == mlang_cs_info.uiInternetEncoding, "cpiInternet %d while mlang uiInternetEncoding %d\n",
165 cs_info.cpiInternet, mlang_cs_info.uiInternetEncoding);
166 ok(cs_info.hCharset == hcs_windows_1252, "hCharset doesn't match requested\n");
167 ok(!strcmp(cs_info.szName, "windows-1252"), "szName doesn't match requested\n");
169 hr = IMimeInternational_GetCodePageCharset(internat, 1252, CHARSET_BODY, &hcs);
170 ok(hr == S_OK, "got %08x\n", hr);
171 hr = IMimeInternational_GetCharsetInfo(internat, hcs, &cs_info);
172 ok(hr == S_OK, "got %08x\n", hr);
174 hr = mlang_getcsetinfo_from_cp(1252, CHARSET_BODY, &mlang_cs_info);
175 ok(hr == S_OK, "got %08x\n", hr);
176 ok(cs_info.cpiWindows == mlang_cs_info.uiCodePage, "cpiWindows %d while mlang uiCodePage %d\n",
177 cs_info.cpiWindows, mlang_cs_info.uiCodePage);
178 ok(cs_info.cpiInternet == mlang_cs_info.uiInternetEncoding, "cpiInternet %d while mlang uiInternetEncoding %d\n",
179 cs_info.cpiInternet, mlang_cs_info.uiInternetEncoding);
181 IMimeInternational_Release(internat);
184 static void test_defaultcharset(void)
186 IMimeInternational *internat;
187 HRESULT hr;
188 HCHARSET hcs_default, hcs, hcs_windows_1251;
190 hr = MimeOleGetInternat(&internat);
191 ok(hr == S_OK, "ret %08x\n", hr);
193 hr = IMimeInternational_GetDefaultCharset(internat, &hcs_default);
194 ok(hr == S_OK, "ret %08x\n", hr);
195 hr = IMimeInternational_GetCodePageCharset(internat, GetACP(), CHARSET_BODY, &hcs);
196 ok(hr == S_OK, "ret %08x\n", hr);
197 ok(hcs_default == hcs, "Unexpected default charset\n");
199 hr = IMimeInternational_FindCharset(internat, "windows-1251", &hcs_windows_1251);
200 ok(hr == S_OK, "got %08x\n", hr);
201 hr = IMimeInternational_SetDefaultCharset(internat, hcs_windows_1251);
202 ok(hr == S_OK, "ret %08x\n", hr);
203 hr = IMimeInternational_GetDefaultCharset(internat, &hcs);
204 ok(hr == S_OK, "ret %08x\n", hr);
205 ok(hcs == hcs_windows_1251, "didn't retrieve recently set default\n");
206 /* Set the old default back again */
207 hr = IMimeInternational_SetDefaultCharset(internat, hcs_default);
208 ok(hr == S_OK, "ret %08x\n", hr);
210 IMimeInternational_Release(internat);
213 static void test_convert(void)
215 IMimeInternational *internat;
216 HRESULT hr;
217 BLOB src, dst;
218 ULONG read;
219 PROPVARIANT prop_in, prop_out;
220 static char test_string[] = "test string";
221 static WCHAR test_stringW[] = {'t','e','s','t',' ','s','t','r','i','n','g',0};
223 hr = MimeOleGetInternat(&internat);
224 ok(hr == S_OK, "ret %08x\n", hr);
226 src.pBlobData = (BYTE*)test_string;
227 src.cbSize = sizeof(test_string);
228 hr = IMimeInternational_ConvertBuffer(internat, 1252, 28591, &src, &dst, &read);
229 ok(hr == S_OK, "ret %08x\n", hr);
230 ok(read == sizeof(test_string), "got %d\n", read);
231 ok(dst.cbSize == sizeof(test_string), "got %d\n", dst.cbSize);
232 CoTaskMemFree(dst.pBlobData);
234 src.cbSize = 2;
235 hr = IMimeInternational_ConvertBuffer(internat, 1252, 28591, &src, &dst, &read);
236 ok(hr == S_OK, "ret %08x\n", hr);
237 ok(read == 2, "got %d\n", read);
238 ok(dst.cbSize == 2, "got %d\n", dst.cbSize);
239 CoTaskMemFree(dst.pBlobData);
241 prop_in.vt = VT_LPWSTR;
242 prop_in.u.pwszVal = test_stringW;
243 hr = IMimeInternational_ConvertString(internat, CP_UNICODE, 1252, &prop_in, &prop_out);
244 ok(hr == S_OK, "ret %08x\n", hr);
245 ok(prop_out.vt == VT_LPSTR, "got %d\n", prop_out.vt);
246 ok(!strcmp(prop_out.u.pszVal, test_string), "got %s\n", prop_out.u.pszVal);
247 PropVariantClear(&prop_out);
249 /* If in.vt is VT_LPWSTR, ignore cpiSrc */
250 prop_in.vt = VT_LPWSTR;
251 prop_in.u.pwszVal = test_stringW;
252 hr = IMimeInternational_ConvertString(internat, 28591, 1252, &prop_in, &prop_out);
253 ok(hr == S_OK, "ret %08x\n", hr);
254 ok(prop_out.vt == VT_LPSTR, "got %d\n", prop_out.vt);
255 ok(!strcmp(prop_out.u.pszVal, test_string), "got %s\n", prop_out.u.pszVal);
256 PropVariantClear(&prop_out);
258 prop_in.vt = VT_LPSTR;
259 prop_in.u.pszVal = test_string;
260 hr = IMimeInternational_ConvertString(internat, 28591, CP_UNICODE, &prop_in, &prop_out);
261 ok(hr == S_OK, "ret %08x\n", hr);
262 ok(prop_out.vt == VT_LPWSTR, "got %d\n", prop_out.vt);
263 ok(!lstrcmpW(prop_out.u.pwszVal, test_stringW), "mismatched strings\n");
264 PropVariantClear(&prop_out);
266 /* If in.vt is VT_LPSTR and cpiSrc is CP_UNICODE, use another multibyte codepage (probably GetACP()) */
267 prop_in.vt = VT_LPSTR;
268 prop_in.u.pszVal = test_string;
269 hr = IMimeInternational_ConvertString(internat, CP_UNICODE, CP_UNICODE, &prop_in, &prop_out);
270 ok(hr == S_OK, "ret %08x\n", hr);
271 ok(prop_out.vt == VT_LPWSTR, "got %d\n", prop_out.vt);
272 ok(!lstrcmpW(prop_out.u.pwszVal, test_stringW), "mismatched strings\n");
273 PropVariantClear(&prop_out);
275 IMimeInternational_Release(internat);
278 START_TEST(mimeintl)
280 OleInitialize(NULL);
281 test_create();
282 test_charset();
283 test_defaultcharset();
284 test_convert();
285 OleUninitialize();