1/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 * Author: Matthias Clasen
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 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, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <gtk/gtk.h>
20#include <gdk/gdkkeysyms.h>
21#include <stdlib.h>
22#include <string.h>
23#include <math.h>
24
25/* tests related to handling of the cell-area property in
26 * GtkCellLayout implementations
27 */
28
29/* test that we have a cell area after new() */
30static void
31test_iconview_new (void)
32{
33 GtkWidget *view;
34 GtkCellArea *area;
35
36 view = gtk_icon_view_new ();
37
38 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
39 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
40 g_assert_true (gtk_orientable_get_orientation (GTK_ORIENTABLE (area)) == gtk_icon_view_get_item_orientation (GTK_ICON_VIEW (view)));
41
42 g_object_ref_sink (view);
43 g_object_unref (object: view);
44}
45
46/* test that new_with_area() keeps the provided area */
47static void
48test_iconview_new_with_area (void)
49{
50 GtkWidget *view;
51 GtkCellArea *area;
52
53 area = gtk_cell_area_box_new ();
54 view = gtk_icon_view_new_with_area (area);
55 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view)) == area);
56
57 g_object_ref_sink (view);
58 g_object_unref (object: view);
59}
60
61/* test that g_object_new keeps the provided area */
62static void
63test_iconview_object_new (void)
64{
65 GtkWidget *view;
66 GtkCellArea *area;
67
68 area = gtk_cell_area_box_new ();
69 gtk_orientable_set_orientation (GTK_ORIENTABLE (area), orientation: GTK_ORIENTATION_HORIZONTAL);
70 view = g_object_new (GTK_TYPE_ICON_VIEW, first_property_name: "cell-area", area, NULL);
71 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view)) == area);
72 g_assert_true (gtk_orientable_get_orientation (GTK_ORIENTABLE (area)) == gtk_icon_view_get_item_orientation (GTK_ICON_VIEW (view)));
73
74 g_object_ref_sink (view);
75 g_object_unref (object: view);
76}
77
78/* test that we have a cell area after new() */
79static void
80test_combobox_new (void)
81{
82 GtkWidget *view;
83 GtkCellArea *area;
84
85 view = gtk_combo_box_new ();
86
87 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
88 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
89
90 g_object_ref_sink (view);
91 g_object_unref (object: view);
92}
93
94static int subclass_init;
95
96typedef GtkComboBox MyComboBox;
97typedef GtkComboBoxClass MyComboBoxClass;
98
99GType my_combo_box_get_type (void);
100
101G_DEFINE_TYPE (MyComboBox, my_combo_box, GTK_TYPE_COMBO_BOX)
102
103static void
104my_combo_box_class_init (MyComboBoxClass *klass)
105{
106}
107
108static void
109my_combo_box_init (MyComboBox *view)
110{
111 GtkCellArea *area;
112
113 if (subclass_init == 0)
114 {
115 /* do nothing to area */
116 }
117 else if (subclass_init == 1)
118 {
119 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
120 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
121 g_assert_cmpint (gtk_orientable_get_orientation (GTK_ORIENTABLE (area)), ==, GTK_ORIENTATION_HORIZONTAL);
122 gtk_orientable_set_orientation (GTK_ORIENTABLE (area), orientation: GTK_ORIENTATION_VERTICAL);
123 }
124}
125
126/* test that a combobox subclass has an area */
127static void
128test_combobox_subclass0 (void)
129{
130 GtkWidget *view;
131 GtkCellArea *area;
132
133 subclass_init = 0;
134
135 view = g_object_new (object_type: my_combo_box_get_type (), NULL);
136 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
137 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
138 g_assert_cmpint (gtk_orientable_get_orientation (GTK_ORIENTABLE (area)), ==, GTK_ORIENTATION_HORIZONTAL);
139
140 g_object_ref_sink (view);
141 g_object_unref (object: view);
142}
143
144/* test we can access the area in subclass init */
145static void
146test_combobox_subclass2 (void)
147{
148 GtkWidget *view;
149 GtkCellArea *area;
150
151 subclass_init = 1;
152
153 view = g_object_new (object_type: my_combo_box_get_type (), NULL);
154 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
155 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
156 g_assert_cmpint (gtk_orientable_get_orientation (GTK_ORIENTABLE (area)), ==, GTK_ORIENTATION_VERTICAL);
157
158 g_object_ref_sink (view);
159 g_object_unref (object: view);
160}
161
162/* test that we have a cell area after new() */
163static void
164test_cellview_new (void)
165{
166 GtkWidget *view;
167 GtkCellArea *area;
168
169 view = gtk_cell_view_new ();
170
171 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view));
172 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
173
174 g_object_ref_sink (view);
175 g_object_unref (object: view);
176}
177
178/* test that new_with_context() keeps the provided area */
179static void
180test_cellview_new_with_context (void)
181{
182 GtkWidget *view;
183 GtkCellArea *area;
184 GtkCellAreaContext *context;
185
186 area = gtk_cell_area_box_new ();
187 context = gtk_cell_area_create_context (area);
188 view = gtk_cell_view_new_with_context (area, context);
189 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view)) == area);
190
191 g_object_ref_sink (view);
192 g_object_unref (object: view);
193}
194
195/* test that g_object_new keeps the provided area */
196static void
197test_cellview_object_new (void)
198{
199 GtkWidget *view;
200 GtkCellArea *area;
201
202 area = gtk_cell_area_box_new ();
203 gtk_orientable_set_orientation (GTK_ORIENTABLE (area), orientation: GTK_ORIENTATION_HORIZONTAL);
204 view = g_object_new (GTK_TYPE_CELL_VIEW, first_property_name: "cell-area", area, NULL);
205 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (view)) == area);
206
207 g_object_ref_sink (view);
208 g_object_unref (object: view);
209}
210
211/* test that we have a cell area after new() */
212static void
213test_column_new (void)
214{
215 GtkTreeViewColumn *col;
216 GtkCellArea *area;
217
218 col = gtk_tree_view_column_new ();
219
220 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (col));
221 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
222
223 g_object_ref_sink (col);
224 g_object_unref (object: col);
225}
226
227/* test that new_with_area() keeps the provided area */
228static void
229test_column_new_with_area (void)
230{
231 GtkTreeViewColumn *col;
232 GtkCellArea *area;
233
234 area = gtk_cell_area_box_new ();
235 col = gtk_tree_view_column_new_with_area (area);
236 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (col)) == area);
237
238 g_object_ref_sink (col);
239 g_object_unref (object: col);
240}
241
242/* test that g_object_new keeps the provided area */
243static void
244test_column_object_new (void)
245{
246 GtkTreeViewColumn *col;
247 GtkCellArea *area;
248
249 area = gtk_cell_area_box_new ();
250 gtk_orientable_set_orientation (GTK_ORIENTABLE (area), orientation: GTK_ORIENTATION_HORIZONTAL);
251 col = g_object_new (GTK_TYPE_TREE_VIEW_COLUMN, first_property_name: "cell-area", area, NULL);
252 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (col)) == area);
253
254 g_object_ref_sink (col);
255 g_object_unref (object: col);
256}
257
258/* test that we have a cell area after new() */
259static void
260test_completion_new (void)
261{
262 GtkEntryCompletion *c;
263 GtkCellArea *area;
264
265 c = gtk_entry_completion_new ();
266
267 area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (c));
268 g_assert_true (GTK_IS_CELL_AREA_BOX (area));
269
270 g_object_ref_sink (c);
271 g_object_unref (object: c);
272}
273
274/* test that new_with_area() keeps the provided area */
275static void
276test_completion_new_with_area (void)
277{
278 GtkEntryCompletion *c;
279 GtkCellArea *area;
280
281 area = gtk_cell_area_box_new ();
282 c = gtk_entry_completion_new_with_area (area);
283 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (c)) == area);
284
285 g_object_ref_sink (c);
286 g_object_unref (object: c);
287}
288
289/* test that g_object_new keeps the provided area */
290static void
291test_completion_object_new (void)
292{
293 GtkEntryCompletion *c;
294 GtkCellArea *area;
295
296 area = gtk_cell_area_box_new ();
297 gtk_orientable_set_orientation (GTK_ORIENTABLE (area), orientation: GTK_ORIENTATION_HORIZONTAL);
298 c = g_object_new (GTK_TYPE_ENTRY_COMPLETION, first_property_name: "cell-area", area, NULL);
299 g_assert_true (gtk_cell_layout_get_area (GTK_CELL_LAYOUT (c)) == area);
300
301 g_object_ref_sink (c);
302 g_object_unref (object: c);
303}
304
305int
306main (int argc, char *argv[])
307{
308 gtk_test_init (argcp: &argc, argvp: &argv);
309 gtk_test_register_all_types();
310
311 g_test_add_func (testpath: "/tests/iconview-new", test_func: test_iconview_new);
312 g_test_add_func (testpath: "/tests/iconview-new-with-area", test_func: test_iconview_new_with_area);
313 g_test_add_func (testpath: "/tests/iconview-object-new", test_func: test_iconview_object_new);
314
315 g_test_add_func (testpath: "/tests/combobox-new", test_func: test_combobox_new);
316 g_test_add_func (testpath: "/tests/combobox-subclass0", test_func: test_combobox_subclass0);
317 g_test_add_func (testpath: "/tests/combobox-subclass2", test_func: test_combobox_subclass2);
318
319 g_test_add_func (testpath: "/tests/cellview-new", test_func: test_cellview_new);
320 g_test_add_func (testpath: "/tests/cellview-new-with-context", test_func: test_cellview_new_with_context);
321 g_test_add_func (testpath: "/tests/cellview-object-new", test_func: test_cellview_object_new);
322
323 g_test_add_func (testpath: "/tests/column-new", test_func: test_column_new);
324 g_test_add_func (testpath: "/tests/column-new-with-area", test_func: test_column_new_with_area);
325 g_test_add_func (testpath: "/tests/column-object-new", test_func: test_column_object_new);
326
327 g_test_add_func (testpath: "/tests/completion-new", test_func: test_completion_new);
328 g_test_add_func (testpath: "/tests/completion-new-with-area", test_func: test_completion_new_with_area);
329 g_test_add_func (testpath: "/tests/completion-object-new", test_func: test_completion_object_new);
330
331 return g_test_run();
332}
333

source code of gtk/testsuite/gtk/cellarea.c