File: | testsuite/reftests/reftest-snapshot.c |
Warning: | line 219, column 11 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright (C) 2011 Red Hat Inc. |
3 | * |
4 | * Author: |
5 | * Benjamin Otte <otte@gnome.org> |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public |
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
19 | */ |
20 | |
21 | #include "config.h" |
22 | |
23 | #include "reftest-snapshot.h" |
24 | |
25 | #include "reftest-module.h" |
26 | #include "ctk-reftest.h" |
27 | |
28 | #include <string.h> |
29 | |
30 | typedef enum { |
31 | SNAPSHOT_WINDOW, |
32 | SNAPSHOT_DRAW |
33 | } SnapshotMode; |
34 | |
35 | static CtkWidget * |
36 | builder_get_toplevel (CtkBuilder *builder) |
37 | { |
38 | GSList *list, *walk; |
39 | CtkWidget *window = NULL((void*)0); |
40 | |
41 | list = ctk_builder_get_objects (builder); |
42 | for (walk = list; walk; walk = walk->next) |
43 | { |
44 | if (CTK_IS_WINDOW (walk->data)(((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ( (walk->data)); GType __t = ((ctk_window_get_type ())); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })))) && |
45 | ctk_widget_get_parent (walk->data) == NULL((void*)0)) |
46 | { |
47 | window = walk->data; |
48 | break; |
49 | } |
50 | } |
51 | |
52 | g_slist_free (list); |
53 | |
54 | return window; |
55 | } |
56 | |
57 | static gboolean |
58 | quit_when_idle (gpointer loop) |
59 | { |
60 | g_main_loop_quit (loop); |
61 | |
62 | return G_SOURCE_REMOVE(0); |
63 | } |
64 | |
65 | static gint inhibit_count; |
66 | static GMainLoop *loop; |
67 | |
68 | void |
69 | reftest_inhibit_snapshot (void) |
70 | { |
71 | inhibit_count++; |
72 | } |
73 | |
74 | void |
75 | reftest_uninhibit_snapshot (void) |
76 | { |
77 | g_assert (inhibit_count > 0)do { if (inhibit_count > 0) ; else g_assertion_message_expr (((gchar*) 0), "reftest-snapshot.c", 77, ((const char*) (__func__ )), "inhibit_count > 0"); } while (0); |
78 | inhibit_count--; |
79 | |
80 | if (inhibit_count == 0) |
81 | g_idle_add (quit_when_idle, loop); |
82 | } |
83 | |
84 | static void |
85 | check_for_draw (CdkEvent *event, |
86 | gpointer data G_GNUC_UNUSED__attribute__ ((__unused__))) |
87 | { |
88 | if (event->type == CDK_EXPOSE) |
89 | { |
90 | reftest_uninhibit_snapshot (); |
91 | cdk_event_handler_set ((CdkEventFunc) ctk_main_do_event, NULL((void*)0), NULL((void*)0)); |
92 | } |
93 | |
94 | ctk_main_do_event (event); |
95 | } |
96 | |
97 | static cairo_surface_t * |
98 | snapshot_widget (CtkWidget *widget, SnapshotMode mode) |
99 | { |
100 | cairo_surface_t *surface; |
101 | cairo_pattern_t *bg; |
102 | cairo_t *cr; |
103 | |
104 | g_assert (ctk_widget_get_realized (widget))do { if (ctk_widget_get_realized (widget)) ; else g_assertion_message_expr (((gchar*) 0), "reftest-snapshot.c", 104, ((const char*) (__func__ )), "ctk_widget_get_realized (widget)"); } while (0); |
105 | |
106 | loop = g_main_loop_new (NULL((void*)0), FALSE(0)); |
107 | |
108 | /* We wait until the widget is drawn for the first time. |
109 | * We can not wait for a CtkWidget::draw event, because that might not |
110 | * happen if the window is fully obscured by windowed child widgets. |
111 | * Alternatively, we could wait for an expose event on widget's window. |
112 | * Both of these are rather hairy, not sure what's best. |
113 | * |
114 | * We also use an inhibit mechanism, to give module functions a chance |
115 | * to delay the snapshot. |
116 | */ |
117 | reftest_inhibit_snapshot (); |
118 | cdk_event_handler_set (check_for_draw, NULL((void*)0), NULL((void*)0)); |
119 | g_main_loop_run (loop); |
120 | |
121 | surface = cdk_window_create_similar_surface (ctk_widget_get_window (widget), |
122 | CAIRO_CONTENT_COLOR, |
123 | ctk_widget_get_allocated_width (widget), |
124 | ctk_widget_get_allocated_height (widget)); |
125 | |
126 | cr = cairo_create (surface); |
127 | |
128 | switch (mode) |
129 | { |
130 | case SNAPSHOT_WINDOW: |
131 | { |
132 | CdkWindow *window = ctk_widget_get_window (widget); |
133 | if (cdk_window_get_window_type (window) == CDK_WINDOW_TOPLEVEL || |
134 | cdk_window_get_window_type (window) == CDK_WINDOW_FOREIGN) |
135 | { |
136 | /* give the WM/server some time to sync. They need it. |
137 | * Also, do use popups instead of toplevels in your tests |
138 | * whenever you can. |
139 | */ |
140 | cdk_display_sync (cdk_window_get_display (window)); |
141 | g_timeout_add (500, quit_when_idle, loop); |
142 | g_main_loop_run (loop); |
143 | } |
144 | cdk_cairo_set_source_window (cr, window, 0, 0); |
145 | cairo_paint (cr); |
146 | } |
147 | break; |
148 | case SNAPSHOT_DRAW: |
149 | bg = cdk_window_get_background_pattern (ctk_widget_get_window (widget)); |
150 | if (bg) |
151 | { |
152 | cairo_set_source (cr, bg); |
153 | cairo_paint (cr); |
154 | } |
155 | ctk_widget_draw (widget, cr); |
156 | break; |
157 | default: |
158 | g_assert_not_reached()do { g_assertion_message_expr (((gchar*) 0), "reftest-snapshot.c" , 158, ((const char*) (__func__)), ((void*)0)); } while (0); |
159 | break; |
160 | } |
161 | |
162 | cairo_destroy (cr); |
163 | g_main_loop_unref (loop); |
164 | ctk_widget_destroy (widget); |
165 | |
166 | return surface; |
167 | } |
168 | |
169 | static void |
170 | connect_signals (CtkBuilder *builder, |
171 | GObject *object, |
172 | const gchar *signal_name, |
173 | const gchar *handler_name, |
174 | GObject *connect_object, |
175 | GConnectFlags flags, |
176 | gpointer user_data) |
177 | { |
178 | ReftestModule *module; |
179 | const char *directory; |
180 | GCallback func; |
181 | GClosure *closure; |
182 | char **split; |
183 | |
184 | directory = user_data; |
185 | split = g_strsplit (handler_name, ":", -1); |
186 | |
187 | switch (g_strv_length (split)) |
188 | { |
189 | case 1: |
190 | func = ctk_builder_lookup_callback_symbol (builder, split[0]); |
191 | |
192 | if (func) |
193 | { |
194 | module = NULL((void*)0); |
195 | } |
196 | else |
197 | { |
198 | module = reftest_module_new_self (); |
199 | if (module == NULL((void*)0)) |
200 | { |
201 | g_error ("glib compiled without module support."); |
202 | return; |
203 | } |
204 | func = reftest_module_lookup (module, split[0]); |
205 | if (!func) |
206 | { |
207 | g_error ("failed to lookup handler for name '%s' when connecting signals", split[0]); |
208 | return; |
209 | } |
210 | } |
211 | break; |
212 | case 2: |
213 | if (g_getenv ("REFTEST_MODULE_DIR")) |
214 | directory = g_getenv ("REFTEST_MODULE_DIR"); |
215 | module = reftest_module_new (directory, split[0]); |
216 | if (module == NULL((void*)0)) |
217 | { |
218 | g_error ("Could not load module '%s' from '%s' when looking up '%s'", split[0], directory, handler_name); |
219 | return; |
This statement is never executed | |
220 | } |
221 | func = reftest_module_lookup (module, split[1]); |
222 | if (!func) |
223 | { |
224 | g_error ("failed to lookup handler for name '%s' in module '%s'", split[1], split[0]); |
225 | return; |
226 | } |
227 | break; |
228 | default: |
229 | g_error ("Could not connect signal handler named '%s'", handler_name); |
230 | return; |
231 | } |
232 | |
233 | g_strfreev (split); |
234 | |
235 | if (connect_object) |
236 | { |
237 | if (flags & G_CONNECT_SWAPPED) |
238 | closure = g_cclosure_new_object_swap (func, connect_object); |
239 | else |
240 | closure = g_cclosure_new_object (func, connect_object); |
241 | } |
242 | else |
243 | { |
244 | if (flags & G_CONNECT_SWAPPED) |
245 | closure = g_cclosure_new_swap (func, NULL((void*)0), NULL((void*)0)); |
246 | else |
247 | closure = g_cclosure_new (func, NULL((void*)0), NULL((void*)0)); |
248 | } |
249 | |
250 | if (module) |
251 | g_closure_add_finalize_notifier (closure, module, (GClosureNotify) reftest_module_unref); |
252 | |
253 | g_signal_connect_closure (object, signal_name, closure, flags & G_CONNECT_AFTER ? TRUE(!(0)) : FALSE(0)); |
254 | } |
255 | |
256 | cairo_surface_t * |
257 | reftest_snapshot_ui_file (const char *ui_file) |
258 | { |
259 | CtkWidget *window; |
260 | CtkBuilder *builder; |
261 | GError *error = NULL((void*)0); |
262 | char *directory; |
263 | |
264 | directory = g_path_get_dirname (ui_file); |
265 | |
266 | builder = ctk_builder_new (); |
267 | ctk_builder_add_from_file (builder, ui_file, &error); |
268 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "reftest-snapshot.c" , 268, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
269 | ctk_builder_connect_signals_full (builder, connect_signals, directory); |
270 | window = builder_get_toplevel (builder); |
271 | g_object_unref (builder); |
272 | g_free (directory); |
273 | g_assert (window)do { if (window) ; else g_assertion_message_expr (((gchar*) 0 ), "reftest-snapshot.c", 273, ((const char*) (__func__)), "window" ); } while (0); |
274 | |
275 | ctk_widget_show (window); |
276 | |
277 | return snapshot_widget (window, SNAPSHOT_WINDOW); |
278 | } |