inetcomm/tests: Add test for IMimeInternational_GetDefaultCharset.
[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
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");
156
157     hr = IMimeInternational_GetCharsetInfo(internat, hcs_windows_1252, &cs_info);
158     ok(hr == S_OK, "got %08x\n", hr);
159
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");
168
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);
173
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);
180
181     IMimeInternational_Release(internat);
182 }
183
184 static void test_defaultcharset(void)
185 {
186     IMimeInternational *internat;
187     HRESULT hr;
188     HCHARSET hcs_default, hcs;
189
190     hr = MimeOleGetInternat(&internat);
191     ok(hr == S_OK, "ret %08x\n", hr);
192
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");
198
199     IMimeInternational_Release(internat);
200 }
201
202 START_TEST(mimeintl)
203 {
204     OleInitialize(NULL);
205     test_create();
206     test_charset();
207     test_defaultcharset();
208     OleUninitialize();
209 }