wintrust: Use a helper function to get a signer's cert info from a message.
[wine] / dlls / user32 / tests / combo.c
1 /* Unit test suite for combo boxes.
2  *
3  * Copyright 2007 Mikolaj Zalewski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define STRICT
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
27
28 #include "wine/test.h"
29
30 #define COMBO_ID 1995
31
32 static HWND hMainWnd;
33
34 #define expect_eq(expr, value, type, fmt); { type val = expr; ok(val == (value), #expr " expected " #fmt " got " #fmt "\n", (value), val); }
35 #define expect_rect(r, _left, _top, _right, _bottom) ok(r.left == _left && r.top == _top && \
36     r.bottom == _bottom && r.right == _right, "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", \
37     r.left, r.top, r.right, r.bottom, _left, _top, _right, _bottom);
38
39 static HWND build_combo(DWORD style)
40 {
41     return CreateWindow("ComboBox", "Combo", WS_VISIBLE|WS_CHILD|style, 5, 5, 100, 100, hMainWnd, (HMENU)COMBO_ID, NULL, 0);
42 }
43
44 static int font_height(HFONT hFont)
45 {
46     TEXTMETRIC tm;
47     HFONT hFontOld;
48     HDC hDC;
49
50     hDC = CreateCompatibleDC(NULL);
51     hFontOld = SelectObject(hDC, hFont);
52     GetTextMetrics(hDC, &tm);
53     SelectObject(hDC, hFontOld);
54     DeleteDC(hDC);
55
56     return tm.tmHeight;
57 }
58
59 static INT CALLBACK is_font_installed_proc(const LOGFONT *elf, const TEXTMETRIC *tm, DWORD type, LPARAM lParam)
60 {
61     return 0;
62 }
63
64 static int is_font_installed(const char *name)
65 {
66     HDC hdc = GetDC(NULL);
67     BOOL ret = !EnumFontFamilies(hdc, name, is_font_installed_proc, 0);
68     ReleaseDC(NULL, hdc);
69     return ret;
70 }
71
72 static void test_setfont(DWORD style)
73 {
74     HWND hCombo = build_combo(style);
75     HFONT hFont1 = CreateFont(10, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Marlett");
76     HFONT hFont2 = CreateFont(8, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Marlett");
77     RECT r;
78     int i;
79
80     trace("Style %x\n", style);
81     GetClientRect(hCombo, &r);
82     expect_rect(r, 0, 0, 100, 24);
83     SendMessageA(hCombo, CB_GETDROPPEDCONTROLRECT, 0, (LPARAM)&r);
84     MapWindowPoints(HWND_DESKTOP, hMainWnd, (LPPOINT)&r, 2);
85     todo_wine expect_rect(r, 5, 5, 105, 105);
86
87     if (!is_font_installed("Marlett"))
88     {
89         skip("Marlett font not available\n");
90         DestroyWindow(hCombo);
91         DeleteObject(hFont1);
92         DeleteObject(hFont2);
93         return;
94     }
95
96     if (font_height(hFont1) == 10 && font_height(hFont2) == 8)
97     {
98         SendMessage(hCombo, WM_SETFONT, (WPARAM)hFont1, FALSE);
99         GetClientRect(hCombo, &r);
100         expect_rect(r, 0, 0, 100, 18);
101         SendMessageA(hCombo, CB_GETDROPPEDCONTROLRECT, 0, (LPARAM)&r);
102         MapWindowPoints(HWND_DESKTOP, hMainWnd, (LPPOINT)&r, 2);
103         todo_wine expect_rect(r, 5, 5, 105, 99);
104
105         SendMessage(hCombo, WM_SETFONT, (WPARAM)hFont2, FALSE);
106         GetClientRect(hCombo, &r);
107         expect_rect(r, 0, 0, 100, 16);
108         SendMessageA(hCombo, CB_GETDROPPEDCONTROLRECT, 0, (LPARAM)&r);
109         MapWindowPoints(HWND_DESKTOP, hMainWnd, (LPPOINT)&r, 2);
110         todo_wine expect_rect(r, 5, 5, 105, 97);
111
112         SendMessage(hCombo, WM_SETFONT, (WPARAM)hFont1, FALSE);
113         GetClientRect(hCombo, &r);
114         expect_rect(r, 0, 0, 100, 18);
115         SendMessageA(hCombo, CB_GETDROPPEDCONTROLRECT, 0, (LPARAM)&r);
116         MapWindowPoints(HWND_DESKTOP, hMainWnd, (LPPOINT)&r, 2);
117         todo_wine expect_rect(r, 5, 5, 105, 99);
118     }
119     else
120         skip("Invalid Marlett font heights\n");
121
122     for (i = 1; i < 30; i++)
123     {
124         HFONT hFont = CreateFont(i, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Marlett");
125         int height = font_height(hFont);
126
127         SendMessage(hCombo, WM_SETFONT, (WPARAM)hFont, FALSE);
128         GetClientRect(hCombo, &r);
129         expect_eq(r.bottom - r.top, height + 8, int, "%d");
130         SendMessage(hCombo, WM_SETFONT, 0, FALSE);
131         DeleteObject(hFont);
132     }
133
134     DestroyWindow(hCombo);
135     DeleteObject(hFont1);
136     DeleteObject(hFont2);
137 }
138
139 static LRESULT (CALLBACK *old_parent_proc)(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
140 static LPCSTR expected_edit_text;
141 static LPCSTR expected_list_text;
142
143 static LRESULT CALLBACK parent_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
144 {
145     switch (msg)
146     {
147     case WM_COMMAND:
148         switch (wparam)
149         {
150             case MAKEWPARAM(COMBO_ID, CBN_SELCHANGE):
151             {
152                 HWND hCombo = (HWND)lparam;
153                 int idx;
154                 char list[20], edit[20];
155
156                 memset(list, 0, sizeof(list));
157                 memset(edit, 0, sizeof(edit));
158
159                 idx = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
160                 SendMessage(hCombo, CB_GETLBTEXT, idx, (LPARAM)list);
161                 SendMessage(hCombo, WM_GETTEXT, sizeof(edit), (LPARAM)edit);
162
163                 ok(!strcmp(edit, expected_edit_text), "edit: got %s, expected %s\n",
164                    edit, expected_edit_text);
165                 ok(!strcmp(list, expected_list_text), "list: got %s, expected %s\n",
166                    list, expected_list_text);
167             }
168             break;
169         }
170         break;
171     }
172
173     return CallWindowProc(old_parent_proc, hwnd, msg, wparam, lparam);
174 }
175
176 static void test_selection(DWORD style, const char * const text[],
177                            const int *edit, const int *list)
178 {
179     INT idx;
180     HWND hCombo;
181
182     hCombo = build_combo(style);
183
184     SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)text[0]);
185     SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)text[1]);
186     SendMessage(hCombo, CB_SETCURSEL, -1, 0);
187
188     old_parent_proc = (void *)SetWindowLongPtr(hMainWnd, GWLP_WNDPROC, (ULONG_PTR)parent_wnd_proc);
189
190     idx = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
191     ok(idx == -1, "expected selection -1, got %d\n", idx);
192
193     expected_list_text = text[list[0]];
194     expected_edit_text = text[edit[0]];
195     SendMessage(hCombo, WM_KEYDOWN, VK_DOWN, 0);
196
197     expected_list_text = text[list[1]];
198     expected_edit_text = text[edit[1]];
199     SendMessage(hCombo, WM_KEYDOWN, VK_DOWN, 0);
200
201     expected_list_text = text[list[2]];
202     expected_edit_text = text[edit[2]];
203     SendMessage(hCombo, WM_KEYDOWN, VK_UP, 0);
204
205     SetWindowLongPtr(hMainWnd, GWLP_WNDPROC, (ULONG_PTR)old_parent_proc);
206     DestroyWindow(hCombo);
207 }
208
209 static void test_CBN_SELCHANGE(void)
210 {
211     static const char * const text[] = { "alpha", "beta", "" };
212     static const int sel_1[] = { 2, 0, 1 };
213     static const int sel_2[] = { 0, 1, 0 };
214
215     test_selection(CBS_SIMPLE, text, sel_1, sel_2);
216     test_selection(CBS_DROPDOWN, text, sel_1, sel_2);
217     test_selection(CBS_DROPDOWNLIST, text, sel_2, sel_2);
218 }
219
220 START_TEST(combo)
221 {
222     hMainWnd = CreateWindow("static", "Test", WS_OVERLAPPEDWINDOW, 10, 10, 300, 300, NULL, NULL, NULL, 0);
223     ShowWindow(hMainWnd, SW_SHOW);
224
225     test_setfont(CBS_DROPDOWN);
226     test_setfont(CBS_DROPDOWNLIST);
227     test_CBN_SELCHANGE();
228
229     DestroyWindow(hMainWnd);
230 }