| File: | testsuite/a11y/state/state-record.c |
| Warning: | line 379, column 15 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2011 Red Hat Inc. |
| 3 | * |
| 4 | * Author: |
| 5 | * Matthias Clasen <mclasen@redhat.com> |
| 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 | #include <glib/gstdio.h> |
| 23 | #include <ctk/ctk.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #ifdef G_OS_WIN32 |
| 27 | # include <io.h> |
| 28 | #else |
| 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | |
| 32 | static gchar **states; |
| 33 | |
| 34 | static void |
| 35 | record_state_change (AtkObject *accessible, |
| 36 | const gchar *state, |
| 37 | gboolean set, |
| 38 | GString *string) |
| 39 | { |
| 40 | CtkWidget *w; |
| 41 | const gchar *name; |
| 42 | |
| 43 | if (states) |
| 44 | { |
| 45 | gint i; |
| 46 | |
| 47 | for (i = 0; states[i]; i++) |
| 48 | { |
| 49 | if (strcmp (states[i], state) == 0) |
| 50 | break; |
| 51 | } |
| 52 | if (states[i] == NULL((void*)0)) |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | w = ctk_accessible_get_widget (CTK_ACCESSIBLE (accessible)((((CtkAccessible*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((accessible)), ((ctk_accessible_get_type ()))))))); |
| 57 | name = ctk_buildable_get_name (CTK_BUILDABLE (w)((((CtkBuildable*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((w)), ((ctk_buildable_get_type ()))))))); |
| 58 | g_string_append_printf (string, "%s %s %d\n", name, state, set); |
| 59 | } |
| 60 | |
| 61 | static gboolean |
| 62 | stop (gpointer data) |
| 63 | { |
| 64 | GMainLoop *loop = data; |
| 65 | |
| 66 | g_main_loop_quit (loop); |
| 67 | |
| 68 | return G_SOURCE_CONTINUE(!(0)); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | do_action (CtkBuilder *builder, const gchar *action, GString *string) |
| 73 | { |
| 74 | gchar **parts; |
| 75 | gint len; |
| 76 | gint i; |
| 77 | |
| 78 | parts = g_strsplit (action, " ", 0); |
| 79 | len = g_strv_length (parts); |
| 80 | if (len > 0) |
| 81 | { |
| 82 | if (strcmp (parts[0], "record") == 0) |
| 83 | { |
| 84 | for (i = 1; i < len; i++) |
| 85 | { |
| 86 | GObject *o, *a; |
| 87 | |
| 88 | o = ctk_builder_get_object (builder, parts[i]); |
| 89 | if (ATK_IS_OBJECT (o)(((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ( (o)); GType __t = ((atk_object_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; }))))) |
| 90 | a = o; |
| 91 | else if (CTK_IS_WIDGET (o)(((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ( (o)); GType __t = ((ctk_widget_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; }))))) |
| 92 | a = G_OBJECT (ctk_widget_get_accessible (CTK_WIDGET (o)))((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((ctk_widget_get_accessible (((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance*) ((o)), ((ctk_widget_get_type ()))))))))), ( ((GType) ((20) << (2)))))))); |
| 93 | else |
| 94 | g_assert_not_reached ()do { g_assertion_message_expr (((gchar*) 0), "state-record.c" , 94, ((const char*) (__func__)), ((void*)0)); } while (0); |
| 95 | g_signal_connect (a, "state-change", G_CALLBACK (record_state_change), string)g_signal_connect_data ((a), ("state-change"), (((GCallback) ( record_state_change))), (string), ((void*)0), (GConnectFlags) 0); |
| 96 | } |
| 97 | } |
| 98 | else if (strcmp (parts[0], "states") == 0) |
| 99 | { |
| 100 | g_strfreev (states); |
| 101 | states = g_strdupv (parts); |
| 102 | } |
| 103 | else if (strcmp (parts[0], "destroy") == 0) |
| 104 | { |
| 105 | for (i = 1; i < len; i++) |
| 106 | { |
| 107 | GObject *o; |
| 108 | |
| 109 | o = ctk_builder_get_object (builder, parts[i]); |
| 110 | ctk_widget_destroy (CTK_WIDGET (o)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((o)), ((ctk_widget_get_type ()))))))); |
| 111 | } |
| 112 | } |
| 113 | else if (strcmp (parts[0], "show") == 0) |
| 114 | { |
| 115 | GObject *o; |
| 116 | |
| 117 | o = ctk_builder_get_object (builder, parts[1]); |
| 118 | ctk_widget_show_now (CTK_WIDGET (o)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((o)), ((ctk_widget_get_type ()))))))); |
| 119 | } |
| 120 | else if (strcmp (parts[0], "focus") == 0) |
| 121 | { |
| 122 | GObject *o; |
| 123 | |
| 124 | o = ctk_builder_get_object (builder, parts[1]); |
| 125 | ctk_widget_grab_focus (CTK_WIDGET (o)((((CtkWidget*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((o)), ((ctk_widget_get_type ()))))))); |
| 126 | } |
| 127 | else if (strcmp (parts[0], "wait") == 0) |
| 128 | { |
| 129 | GMainLoop *loop; |
| 130 | gulong id; |
| 131 | |
| 132 | loop = g_main_loop_new (NULL((void*)0), FALSE(0)); |
| 133 | id = g_timeout_add (1000, stop, loop); |
| 134 | g_main_loop_run (loop); |
| 135 | g_source_remove (id); |
| 136 | g_main_loop_unref (loop); |
| 137 | } |
| 138 | } |
| 139 | g_free (parts); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | record_events (const gchar *ui_file, |
| 144 | const gchar *in_file, |
| 145 | GString *string) |
| 146 | { |
| 147 | CtkBuilder *builder; |
| 148 | GError *error; |
| 149 | gchar *contents; |
| 150 | gchar **actions; |
| 151 | gint i, len; |
| 152 | |
| 153 | builder = ctk_builder_new (); |
| 154 | error = NULL((void*)0); |
| 155 | ctk_builder_add_from_file (builder, ui_file, &error); |
| 156 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "state-record.c" , 156, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
| 157 | |
| 158 | g_file_get_contents (in_file, &contents, NULL((void*)0), &error); |
| 159 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "state-record.c" , 159, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
| 160 | actions = g_strsplit (contents, "\n", 0); |
| 161 | |
| 162 | len = g_strv_length (actions); |
| 163 | for (i = 0; i < len; i++) |
| 164 | do_action (builder, actions[i], string); |
| 165 | |
| 166 | g_object_unref (builder); |
| 167 | |
| 168 | g_free (contents); |
| 169 | g_strfreev (actions); |
| 170 | } |
| 171 | |
| 172 | static gchar * |
| 173 | get_test_file (const gchar *test_file, |
| 174 | const gchar *extension, |
| 175 | gboolean must_exist) |
| 176 | { |
| 177 | GString *file = g_string_new (NULL((void*)0)); |
| 178 | |
| 179 | if (g_str_has_suffix (test_file, ".ui")(__builtin_constant_p (".ui")? __extension__ ({ const char * const __str = (test_file); const char * const __suffix = (".ui"); gboolean __result = (0); if (__str == ((void*)0) || __suffix == ((void *)0)) __result = (g_str_has_suffix) (__str, __suffix); else { const size_t __str_len = strlen (((__str) + !(__str))); const size_t __suffix_len = strlen (((__suffix) + !(__suffix))); if (__str_len >= __suffix_len) __result = memcmp (__str + __str_len - __suffix_len, ((__suffix) + !(__suffix)), __suffix_len) == 0; } __result; }) : (g_str_has_suffix) (test_file, ".ui") )) |
| 180 | g_string_append_len (file, test_file, strlen (test_file) - strlen (".ui"))g_string_append_len_inline (file, test_file, strlen (test_file ) - strlen (".ui")); |
| 181 | else |
| 182 | g_string_append (file, test_file)(__builtin_constant_p (test_file) ? __extension__ ({ const char * const __val = (test_file); g_string_append_len_inline (file , __val, (__val != ((void*)0)) ? (gssize) strlen (((__val) + ! (__val))) : (gssize) -1); }) : g_string_append_len_inline (file , test_file, (gssize) -1)); |
| 183 | |
| 184 | g_string_append (file, extension)(__builtin_constant_p (extension) ? __extension__ ({ const char * const __val = (extension); g_string_append_len_inline (file , __val, (__val != ((void*)0)) ? (gssize) strlen (((__val) + ! (__val))) : (gssize) -1); }) : g_string_append_len_inline (file , extension, (gssize) -1)); |
| 185 | |
| 186 | if (must_exist && |
| 187 | !g_file_test (file->str, G_FILE_TEST_EXISTS)) |
| 188 | { |
| 189 | g_string_free (file, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) ( (file), ((!(0)))) : g_string_free_and_steal (file)) : (g_string_free ) ((file), ((!(0))))); |
| 190 | return NULL((void*)0); |
| 191 | } |
| 192 | |
| 193 | return g_string_free (file, FALSE)(__builtin_constant_p ((0)) ? (((0)) ? (g_string_free) ((file ), ((0))) : g_string_free_and_steal (file)) : (g_string_free) ((file), ((0)))); |
| 194 | } |
| 195 | |
| 196 | static char * |
| 197 | diff_with_file (const char *file1, |
| 198 | char *text, |
| 199 | gssize len, |
| 200 | GError **error) |
| 201 | { |
| 202 | const char *command[] = { "diff", "-u", file1, NULL((void*)0), NULL((void*)0) }; |
| 203 | char *diff, *tmpfile; |
| 204 | int fd; |
| 205 | |
| 206 | diff = NULL((void*)0); |
| 207 | |
| 208 | if (len < 0) |
| 209 | len = strlen (text); |
| 210 | |
| 211 | /* write the text buffer to a temporary file */ |
| 212 | fd = g_file_open_tmp (NULL((void*)0), &tmpfile, error); |
| 213 | if (fd < 0) |
| 214 | return NULL((void*)0); |
| 215 | |
| 216 | if (write (fd, text, len) != (int) len) |
| 217 | { |
| 218 | close (fd); |
| 219 | g_set_error (error, |
| 220 | G_FILE_ERRORg_file_error_quark (), G_FILE_ERROR_FAILED, |
| 221 | "Could not write data to temporary file '%s'", tmpfile); |
| 222 | goto done; |
| 223 | } |
| 224 | close (fd); |
| 225 | command[3] = tmpfile; |
| 226 | |
| 227 | /* run diff command */ |
| 228 | g_spawn_sync (NULL((void*)0), |
| 229 | (char **) command, |
| 230 | NULL((void*)0), |
| 231 | G_SPAWN_SEARCH_PATH, |
| 232 | NULL((void*)0), NULL((void*)0), |
| 233 | &diff, |
| 234 | NULL((void*)0), NULL((void*)0), |
| 235 | error); |
| 236 | done: |
| 237 | g_unlink (tmpfile); |
| 238 | g_free (tmpfile); |
| 239 | |
| 240 | return diff; |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | test_ui_file (GFile *file) |
| 245 | { |
| 246 | gchar *ui_file, *in_file, *out_file; |
| 247 | GString *record; |
| 248 | GError *error = NULL((void*)0); |
| 249 | |
| 250 | ui_file = g_file_get_path (file); |
| 251 | in_file = get_test_file (ui_file, ".in", TRUE(!(0))); |
| 252 | out_file = get_test_file (ui_file, ".out", TRUE(!(0))); |
| 253 | record = g_string_new (""); |
| 254 | |
| 255 | record_events (ui_file, in_file, record); |
| 256 | |
| 257 | if (out_file) |
| 258 | { |
| 259 | char *diff = diff_with_file (out_file, record->str, record->len, &error); |
| 260 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "state-record.c" , 260, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
| 261 | |
| 262 | if (diff && diff[0]) |
| 263 | { |
| 264 | g_printerr ("Contents don't match expected contents:\n%s", diff); |
| 265 | g_test_fail (); |
| 266 | g_free (diff); |
| 267 | } |
| 268 | } |
| 269 | else if (record->str[0]) |
| 270 | { |
| 271 | g_test_message ("Expected a reference file:\n%s", record->str); |
| 272 | g_test_fail (); |
| 273 | } |
| 274 | |
| 275 | g_string_free (record, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) ( (record), ((!(0)))) : g_string_free_and_steal (record)) : (g_string_free ) ((record), ((!(0))))); |
| 276 | g_free (in_file); |
| 277 | g_free (out_file); |
| 278 | g_free (ui_file); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | add_test_for_file (GFile *file) |
| 283 | { |
| 284 | g_test_add_vtable (g_file_get_path (file), |
| 285 | 0, |
| 286 | g_object_ref (file)((__typeof__ (file)) (g_object_ref) (file)), |
| 287 | (GTestFixtureFunc) NULL((void*)0), |
| 288 | (GTestFixtureFunc) test_ui_file, |
| 289 | (GTestFixtureFunc) g_object_unref); |
| 290 | } |
| 291 | |
| 292 | static int |
| 293 | compare_files (gconstpointer a, gconstpointer b) |
| 294 | { |
| 295 | GFile *file1 = G_FILE (a)((((GFile*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((a)), ((g_file_get_type ())))))); |
| 296 | GFile *file2 = G_FILE (b)((((GFile*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((b)), ((g_file_get_type ())))))); |
| 297 | char *path1, *path2; |
| 298 | int result; |
| 299 | |
| 300 | path1 = g_file_get_path (file1); |
| 301 | path2 = g_file_get_path (file2); |
| 302 | |
| 303 | result = strcmp (path1, path2); |
| 304 | |
| 305 | g_free (path1); |
| 306 | g_free (path2); |
| 307 | |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | static void |
| 312 | add_tests_for_files_in_directory (GFile *dir) |
| 313 | { |
| 314 | GFileEnumerator *enumerator; |
| 315 | GFileInfo *info; |
| 316 | GList *files; |
| 317 | GError *error = NULL((void*)0); |
| 318 | |
| 319 | enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME"standard::name", 0, NULL((void*)0), &error); |
| 320 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "state-record.c" , 320, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
| 321 | files = NULL((void*)0); |
| 322 | |
| 323 | while ((info = g_file_enumerator_next_file (enumerator, NULL((void*)0), &error))) |
| 324 | { |
| 325 | const char *filename; |
| 326 | |
| 327 | filename = g_file_info_get_name (info); |
| 328 | |
| 329 | if (!g_str_has_suffix (filename, ".ui")(__builtin_constant_p (".ui")? __extension__ ({ const char * const __str = (filename); const char * const __suffix = (".ui"); gboolean __result = (0); if (__str == ((void*)0) || __suffix == ((void *)0)) __result = (g_str_has_suffix) (__str, __suffix); else { const size_t __str_len = strlen (((__str) + !(__str))); const size_t __suffix_len = strlen (((__suffix) + !(__suffix))); if (__str_len >= __suffix_len) __result = memcmp (__str + __str_len - __suffix_len, ((__suffix) + !(__suffix)), __suffix_len) == 0; } __result; }) : (g_str_has_suffix) (filename, ".ui") )) |
| 330 | { |
| 331 | g_object_unref (info); |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | files = g_list_prepend (files, g_file_get_child (dir, filename)); |
| 336 | |
| 337 | g_object_unref (info); |
| 338 | } |
| 339 | |
| 340 | g_assert_no_error (error)do { if (error) g_assertion_message_error (((gchar*) 0), "state-record.c" , 340, ((const char*) (__func__)), "error", error, 0, 0); } while (0); |
| 341 | g_object_unref (enumerator); |
| 342 | |
| 343 | files = g_list_sort (files, compare_files); |
| 344 | g_list_foreach (files, (GFunc) add_test_for_file, NULL((void*)0)); |
| 345 | g_list_free_full (files, g_object_unref); |
| 346 | } |
| 347 | |
| 348 | static char *arg_base_dir = NULL((void*)0); |
| 349 | |
| 350 | static const GOptionEntry test_args[] = { |
| 351 | { "directory", 'd', 0, G_OPTION_ARG_FILENAME, &arg_base_dir, |
| 352 | "Directory to run tests from", "DIR" }, |
| 353 | { NULL((void*)0) } |
| 354 | }; |
| 355 | |
| 356 | int |
| 357 | main (int argc, char *argv[]) |
| 358 | { |
| 359 | const gchar *basedir; |
| 360 | GFile *dir; |
| 361 | GError *error = NULL((void*)0); |
| 362 | GOptionContext *context; |
| 363 | |
| 364 | context = g_option_context_new ("- run CTK state tests"); |
| 365 | g_option_context_add_main_entries (context, test_args, NULL((void*)0)); |
| 366 | g_option_context_set_ignore_unknown_options (context, TRUE(!(0))); |
| 367 | |
| 368 | if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 369 | { |
| 370 | g_print ("option parsing failed: %s\n", error->message); |
| 371 | return FALSE(0); |
| 372 | } |
| 373 | |
| 374 | g_option_context_free (context); |
| 375 | |
| 376 | ctk_test_init (&argc, &argv, NULL((void*)0)); |
| 377 | |
| 378 | if (arg_base_dir) |
| 379 | basedir = arg_base_dir; |
This statement is never executed | |
| 380 | else |
| 381 | basedir = g_test_get_dir (G_TEST_DIST); |
| 382 | |
| 383 | dir = g_file_new_for_path (basedir); |
| 384 | add_tests_for_files_in_directory (dir); |
| 385 | g_object_unref (dir); |
| 386 | |
| 387 | return g_test_run (); |
| 388 | } |
| 389 |