/* buildertest.c * Copyright (C) 2006-2007 Async Open Source * Authors: Johan Dahlin * Henrique Romano * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library. If not, see . */ #include #include #include #include #include #include /* exported for GtkBuilder */ void signal_normal (GtkWindow *window, GParamSpec *spec); void signal_after (GtkWindow *window, GParamSpec *spec); void signal_object (GtkButton *button, GParamSpec *spec); void signal_object_after (GtkButton *button, GParamSpec *spec); void signal_first (GtkButton *button, GParamSpec *spec); void signal_second (GtkButton *button, GParamSpec *spec); void signal_extra (GtkButton *button, GParamSpec *spec); void signal_extra2 (GtkButton *button, GParamSpec *spec); /* Copied from gtkiconfactory.c; keep in sync! */ struct _GtkIconSet { guint ref_count; GSList *sources; GSList *cache; guint cache_size; guint cache_serial; }; static GtkBuilder * builder_new_from_string (const gchar *buffer, gsize length, const gchar *domain) { GtkBuilder *builder; GError *error = NULL; builder = gtk_builder_new (); if (domain) gtk_builder_set_translation_domain (builder, domain); gtk_builder_add_from_string (builder, buffer, length, &error); if (error) { g_print ("ERROR: %s", error->message); g_error_free (error); } return builder; } static void test_parser (void) { GtkBuilder *builder; GError *error; builder = gtk_builder_new (); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_UNHANDLED_TAG)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ATTRIBUTE)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TAG)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TAG)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_DUPLICATE_ID)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_PROPERTY)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, "", -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_SIGNAL)); g_error_free (error); g_object_unref (builder); } static int normal = 0; static int after = 0; static int object = 0; static int object_after = 0; void /* exported for GtkBuilder */ signal_normal (GtkWindow *window, GParamSpec *spec) { g_assert (GTK_IS_WINDOW (window)); g_assert (normal == 0); g_assert (after == 0); normal++; } void /* exported for GtkBuilder */ signal_after (GtkWindow *window, GParamSpec *spec) { g_assert (GTK_IS_WINDOW (window)); g_assert (normal == 1); g_assert (after == 0); after++; } void /* exported for GtkBuilder */ signal_object (GtkButton *button, GParamSpec *spec) { g_assert (GTK_IS_BUTTON (button)); g_assert (object == 0); g_assert (object_after == 0); object++; } void /* exported for GtkBuilder */ signal_object_after (GtkButton *button, GParamSpec *spec) { g_assert (GTK_IS_BUTTON (button)); g_assert (object == 1); g_assert (object_after == 0); object_after++; } void /* exported for GtkBuilder */ signal_first (GtkButton *button, GParamSpec *spec) { g_assert (normal == 0); normal = 10; } void /* exported for GtkBuilder */ signal_second (GtkButton *button, GParamSpec *spec) { g_assert (normal == 10); normal = 20; } void /* exported for GtkBuilder */ signal_extra (GtkButton *button, GParamSpec *spec) { g_assert (normal == 20); normal = 30; } void /* exported for GtkBuilder */ signal_extra2 (GtkButton *button, GParamSpec *spec) { g_assert (normal == 30); normal = 40; } static void test_connect_signals (void) { GtkBuilder *builder; GObject *window; const gchar buffer[] = "" " " " " " " " " " " " " " " ""; const gchar buffer_order[] = "" " " " " " " " " ""; const gchar buffer_extra[] = "" " " " " " " ""; const gchar buffer_extra2[] = "" " " " " " " ""; const gchar buffer_after_child[] = "" " " " " " " " " " " " " ""; builder = builder_new_from_string (buffer, -1, NULL); gtk_builder_connect_signals (builder, NULL); window = gtk_builder_get_object (builder, "window1"); gtk_window_set_title (GTK_WINDOW (window), "test"); g_assert_cmpint (normal, ==, 1); g_assert_cmpint (after, ==, 1); g_assert_cmpint (object, ==, 1); g_assert_cmpint (object_after, ==, 1); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); builder = builder_new_from_string (buffer_order, -1, NULL); gtk_builder_connect_signals (builder, NULL); window = gtk_builder_get_object (builder, "window1"); normal = 0; gtk_window_set_title (GTK_WINDOW (window), "test"); g_assert (normal == 20); gtk_widget_destroy (GTK_WIDGET (window)); gtk_builder_add_from_string (builder, buffer_extra, strlen (buffer_extra), NULL); gtk_builder_add_from_string (builder, buffer_extra2, strlen (buffer_extra2), NULL); gtk_builder_connect_signals (builder, NULL); window = gtk_builder_get_object (builder, "window2"); gtk_window_set_title (GTK_WINDOW (window), "test"); g_assert (normal == 30); gtk_widget_destroy (GTK_WIDGET (window)); window = gtk_builder_get_object (builder, "window3"); gtk_window_set_title (GTK_WINDOW (window), "test"); g_assert (normal == 40); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); /* new test, reset globals */ after = 0; normal = 0; builder = builder_new_from_string (buffer_after_child, -1, NULL); window = gtk_builder_get_object (builder, "window1"); gtk_builder_connect_signals (builder, NULL); gtk_window_set_title (GTK_WINDOW (window), "test"); g_assert (normal == 1); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_uimanager_simple (void) { GtkBuilder *builder; GObject *window, *uimgr, *menubar; GObject *menu, *label; GList *children; const gchar buffer[] = "" " " ""; const gchar buffer2[] = "" " " " " " " " " " " " _File" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; builder = builder_new_from_string (buffer, -1, NULL); uimgr = gtk_builder_get_object (builder, "uimgr1"); G_GNUC_BEGIN_IGNORE_DEPRECATIONS; g_assert (GTK_IS_UI_MANAGER (uimgr)); G_GNUC_END_IGNORE_DEPRECATIONS; g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); menubar = gtk_builder_get_object (builder, "menubar1"); g_assert (GTK_IS_MENU_BAR (menubar)); children = gtk_container_get_children (GTK_CONTAINER (menubar)); menu = children->data; g_assert (GTK_IS_MENU_ITEM (menu)); g_assert (strcmp (gtk_widget_get_name (GTK_WIDGET (menu)), "file") == 0); g_list_free (children); label = G_OBJECT (gtk_bin_get_child (GTK_BIN (menu))); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_text (GTK_LABEL (label)), "File") == 0); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_domain (void) { GtkBuilder *builder; const gchar buffer1[] = ""; const gchar buffer2[] = ""; const gchar *domain; builder = builder_new_from_string (buffer1, -1, NULL); domain = gtk_builder_get_translation_domain (builder); g_assert (domain == NULL); g_object_unref (builder); builder = builder_new_from_string (buffer1, -1, "domain-1"); domain = gtk_builder_get_translation_domain (builder); g_assert (domain); g_assert (strcmp (domain, "domain-1") == 0); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); domain = gtk_builder_get_translation_domain (builder); g_assert (domain == NULL); g_object_unref (builder); } #if 0 static void test_translation (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " File" " " " " " " ""; GtkLabel *window, *label; setlocale (LC_ALL, "sv_SE"); textdomain ("builder"); bindtextdomain ("builder", "tests"); builder = builder_new_from_string (buffer, -1, NULL); label = GTK_LABEL (gtk_builder_get_object (builder, "label")); g_assert (strcmp (gtk_label_get_text (label), "Arkiv") == 0); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } #endif static void test_sizegroup (void) { GtkBuilder * builder; const gchar buffer1[] = "" " " " GTK_SIZE_GROUP_HORIZONTAL" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; const gchar buffer2[] = "" " " " GTK_SIZE_GROUP_HORIZONTAL" " " " " " " ""; const gchar buffer3[] = "" " " " GTK_SIZE_GROUP_HORIZONTAL" " " " " " " " " " " " " " GTK_SIZE_GROUP_HORIZONTAL" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; GObject *sizegroup; GSList *widgets; builder = builder_new_from_string (buffer1, -1, NULL); sizegroup = gtk_builder_get_object (builder, "sizegroup1"); widgets = gtk_size_group_get_widgets (GTK_SIZE_GROUP (sizegroup)); g_assert (g_slist_length (widgets) == 2); g_slist_free (widgets); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); sizegroup = gtk_builder_get_object (builder, "sizegroup1"); widgets = gtk_size_group_get_widgets (GTK_SIZE_GROUP (sizegroup)); g_assert (g_slist_length (widgets) == 0); g_slist_free (widgets); g_object_unref (builder); builder = builder_new_from_string (buffer3, -1, NULL); sizegroup = gtk_builder_get_object (builder, "sizegroup1"); widgets = gtk_size_group_get_widgets (GTK_SIZE_GROUP (sizegroup)); g_assert (g_slist_length (widgets) == 2); g_slist_free (widgets); sizegroup = gtk_builder_get_object (builder, "sizegroup2"); widgets = gtk_size_group_get_widgets (GTK_SIZE_GROUP (sizegroup)); g_assert (g_slist_length (widgets) == 2); g_slist_free (widgets); #if 0 { GObject *window; window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); } #endif g_object_unref (builder); } static void test_list_store (void) { const gchar buffer1[] = "" " " " " " " " " " " " " ""; const char buffer2[] = "" " " " " " " " " " " " " " " " " " John" " Doe" " 25" " " " " " Johan" " Dole" " 50" " " " " " " ""; const char buffer3[] = "" " " " " " " " " " " " " " " " " " Doe" " John" " 25" " " " " " 50" " Dole" " Johan" " " " " " 19" " " " " " " ""; GtkBuilder *builder; GObject *store; GtkTreeIter iter; gchar *surname, *lastname; int age; builder = builder_new_from_string (buffer1, -1, NULL); store = gtk_builder_get_object (builder, "liststore1"); g_assert (gtk_tree_model_get_n_columns (GTK_TREE_MODEL (store)) == 2); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 1) == G_TYPE_UINT); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); store = gtk_builder_get_object (builder, "liststore1"); g_assert (gtk_tree_model_get_n_columns (GTK_TREE_MODEL (store)) == 3); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 1) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 2) == G_TYPE_INT); g_assert (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter) == TRUE); gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &surname, 1, &lastname, 2, &age, -1); g_assert (surname != NULL); g_assert (strcmp (surname, "John") == 0); g_free (surname); g_assert (lastname != NULL); g_assert (strcmp (lastname, "Doe") == 0); g_free (lastname); g_assert (age == 25); g_assert (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter) == TRUE); gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &surname, 1, &lastname, 2, &age, -1); g_assert (surname != NULL); g_assert (strcmp (surname, "Johan") == 0); g_free (surname); g_assert (lastname != NULL); g_assert (strcmp (lastname, "Dole") == 0); g_free (lastname); g_assert (age == 50); g_assert (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter) == FALSE); g_object_unref (builder); builder = builder_new_from_string (buffer3, -1, NULL); store = gtk_builder_get_object (builder, "liststore1"); g_assert (gtk_tree_model_get_n_columns (GTK_TREE_MODEL (store)) == 3); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 1) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 2) == G_TYPE_INT); g_assert (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter) == TRUE); gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &surname, 1, &lastname, 2, &age, -1); g_assert (surname != NULL); g_assert (strcmp (surname, "John") == 0); g_free (surname); g_assert (lastname != NULL); g_assert (strcmp (lastname, "Doe") == 0); g_free (lastname); g_assert (age == 25); g_assert (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter) == TRUE); gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &surname, 1, &lastname, 2, &age, -1); g_assert (surname != NULL); g_assert (strcmp (surname, "Johan") == 0); g_free (surname); g_assert (lastname != NULL); g_assert (strcmp (lastname, "Dole") == 0); g_free (lastname); g_assert (age == 50); g_assert (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter) == TRUE); gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &surname, 1, &lastname, 2, &age, -1); g_assert (surname == NULL); g_assert (lastname == NULL); g_assert (age == 19); g_assert (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter) == FALSE); g_object_unref (builder); } static void test_tree_store (void) { const gchar buffer[] = "" " " " " " " " " " " " " ""; GtkBuilder *builder; GObject *store; builder = builder_new_from_string (buffer, -1, NULL); store = gtk_builder_get_object (builder, "treestore1"); g_assert (gtk_tree_model_get_n_columns (GTK_TREE_MODEL (store)) == 2); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0) == G_TYPE_STRING); g_assert (gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 1) == G_TYPE_UINT); g_object_unref (builder); } static void test_types (void) { const gchar buffer[] = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; const gchar buffer2[] = "" " " ""; const gchar buffer3[] = "" " " ""; GtkBuilder *builder; GObject *window; GError *error; builder = builder_new_from_string (buffer, -1, NULL); gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "dialog"))); gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "window"))); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); window = gtk_builder_get_object (builder, "window"); g_assert (GTK_IS_WINDOW (window)); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); error = NULL; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer3, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION)); g_error_free (error); g_object_unref (builder); } static void test_spin_button (void) { GtkBuilder *builder; const gchar buffer[] = "" "" "0" "10" "2" "3" "0" "1" "" "" "True" "adjustment1" "" ""; GObject *obj; GtkAdjustment *adjustment; gdouble value; builder = builder_new_from_string (buffer, -1, NULL); obj = gtk_builder_get_object (builder, "spinbutton1"); g_assert (GTK_IS_SPIN_BUTTON (obj)); adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (obj)); g_assert (GTK_IS_ADJUSTMENT (adjustment)); g_object_get (adjustment, "value", &value, NULL); g_assert (value == 1); g_object_get (adjustment, "lower", &value, NULL); g_assert (value == 0); g_object_get (adjustment, "upper", &value, NULL); g_assert (value == 10); g_object_get (adjustment, "step-increment", &value, NULL); g_assert (value == 2); g_object_get (adjustment, "page-increment", &value, NULL); g_assert (value == 3); g_object_get (adjustment, "page-size", &value, NULL); g_assert (value == 0); g_object_unref (builder); } static void test_notebook (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " label1" " " " " " " " " " tab_label1" " " " " " " " " " label2" " " " " " " " " " tab_label2" " " " " " " ""; GObject *notebook; GtkWidget *label; builder = builder_new_from_string (buffer, -1, NULL); notebook = gtk_builder_get_object (builder, "notebook1"); g_assert (notebook != NULL); g_assert (gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook)) == 2); label = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 0); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_label (GTK_LABEL (label)), "label1") == 0); label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (notebook), label); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_label (GTK_LABEL (label)), "tab_label1") == 0); label = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 1); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_label (GTK_LABEL (label)), "label2") == 0); label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (notebook), label); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_label (GTK_LABEL (label)), "tab_label2") == 0); g_object_unref (builder); } static void test_construct_only_property (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " GTK_WINDOW_POPUP" " " ""; const gchar buffer2[] = "" " " " " " tagtable1" " " ""; GObject *widget, *tagtable, *textbuffer; GtkWindowType type; builder = builder_new_from_string (buffer, -1, NULL); widget = gtk_builder_get_object (builder, "window1"); g_object_get (widget, "type", &type, NULL); g_assert (type == GTK_WINDOW_POPUP); gtk_widget_destroy (GTK_WIDGET (widget)); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); textbuffer = gtk_builder_get_object (builder, "textbuffer1"); g_assert (textbuffer != NULL); g_object_get (textbuffer, "tag-table", &tagtable, NULL); g_assert (tagtable == gtk_builder_get_object (builder, "tagtable1")); g_object_unref (tagtable); g_object_unref (builder); } static void test_object_properties (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " 10" " " " " " spinbutton1" " " " " " " " " " " " " " " " " ""; const gchar buffer2[] = "" " " ""; GObject *label, *spinbutton, *window; builder = builder_new_from_string (buffer, -1, NULL); label = gtk_builder_get_object (builder, "label1"); g_assert (label != NULL); spinbutton = gtk_builder_get_object (builder, "spinbutton1"); g_assert (spinbutton != NULL); g_assert (spinbutton == (GObject*)gtk_label_get_mnemonic_widget (GTK_LABEL (label))); gtk_builder_add_from_string (builder, buffer2, -1, NULL); window = gtk_builder_get_object (builder, "window2"); g_assert (window != NULL); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_children (void) { GtkBuilder * builder; GtkWidget *content_area, *dialog_action_area; GList *children; const gchar buffer1[] = "" " " " " " " " Hello" " " " " " " ""; const gchar buffer2[] = "" " " " 1" " " " " " 10" " " " " " 20" " " " " " " " " " " ""; GObject *window, *button; GObject *dialog, *vbox, *action_area; builder = builder_new_from_string (buffer1, -1, NULL); window = gtk_builder_get_object (builder, "window1"); g_assert (window != NULL); g_assert (GTK_IS_WINDOW (window)); button = gtk_builder_get_object (builder, "button1"); g_assert (button != NULL); g_assert (GTK_IS_BUTTON (button)); g_assert (gtk_widget_get_parent (GTK_WIDGET(button)) != NULL); g_assert (strcmp (gtk_buildable_get_name (GTK_BUILDABLE (gtk_widget_get_parent (GTK_WIDGET (button)))), "window1") == 0); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); dialog = gtk_builder_get_object (builder, "dialog1"); g_assert (dialog != NULL); g_assert (GTK_IS_DIALOG (dialog)); children = gtk_container_get_children (GTK_CONTAINER (dialog)); g_assert_cmpint (g_list_length (children), ==, 2); g_list_free (children); vbox = gtk_builder_get_object (builder, "dialog1-vbox"); content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); g_assert (vbox != NULL); g_assert (GTK_IS_BOX (vbox)); g_assert (gtk_orientable_get_orientation (GTK_ORIENTABLE (vbox)) == GTK_ORIENTATION_VERTICAL); g_assert (strcmp (gtk_buildable_get_name (GTK_BUILDABLE (gtk_widget_get_parent (GTK_WIDGET (vbox)))), "dialog1") == 0); g_assert (gtk_container_get_border_width (GTK_CONTAINER (vbox)) == 10); g_assert (strcmp (gtk_buildable_get_name (GTK_BUILDABLE (content_area)), "dialog1-vbox") == 0); action_area = gtk_builder_get_object (builder, "dialog1-action_area"); G_GNUC_BEGIN_IGNORE_DEPRECATIONS dialog_action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog)); G_GNUC_END_IGNORE_DEPRECATIONS g_assert (action_area != NULL); g_assert (GTK_IS_BUTTON_BOX (action_area)); g_assert (gtk_orientable_get_orientation (GTK_ORIENTABLE (action_area)) == GTK_ORIENTATION_HORIZONTAL); g_assert (gtk_widget_get_parent (GTK_WIDGET (action_area)) != NULL); g_assert (gtk_container_get_border_width (GTK_CONTAINER (action_area)) == 20); g_assert (dialog_action_area != NULL); g_assert (gtk_buildable_get_name (GTK_BUILDABLE (action_area)) != NULL); g_assert (strcmp (gtk_buildable_get_name (GTK_BUILDABLE (dialog_action_area)), "dialog1-action_area") == 0); gtk_widget_destroy (GTK_WIDGET (dialog)); g_object_unref (builder); } static void test_child_properties (void) { GtkBuilder * builder; const gchar buffer1[] = "" " " " " " " " " " start" " " " " " " " " " " " end" " " " " " " ""; GObject *label, *vbox; GtkPackType pack_type; builder = builder_new_from_string (buffer1, -1, NULL); vbox = gtk_builder_get_object (builder, "vbox1"); g_assert (GTK_IS_BOX (vbox)); label = gtk_builder_get_object (builder, "label1"); g_assert (GTK_IS_LABEL (label)); gtk_container_child_get (GTK_CONTAINER (vbox), GTK_WIDGET (label), "pack-type", &pack_type, NULL); g_assert (pack_type == GTK_PACK_START); label = gtk_builder_get_object (builder, "label2"); g_assert (GTK_IS_LABEL (label)); gtk_container_child_get (GTK_CONTAINER (vbox), GTK_WIDGET (label), "pack-type", &pack_type, NULL); g_assert (pack_type == GTK_PACK_END); g_object_unref (builder); } static void test_treeview_column (void) { GtkBuilder *builder; const gchar buffer[] = "" "" " " " " " " " " " " " " " John" " 25" " " " " "" "" " " " " " True" " liststore1" " " " " " Test" " " " " " " " 1" " " " " " " " " " " " " " Number" " " " " " " " 0" " " " " " " " " " " " " "" ""; GObject *window, *treeview; GtkTreeViewColumn *column; GList *renderers; GObject *renderer; builder = builder_new_from_string (buffer, -1, NULL); treeview = gtk_builder_get_object (builder, "treeview1"); g_assert (treeview); g_assert (GTK_IS_TREE_VIEW (treeview)); column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 0); g_assert (GTK_IS_TREE_VIEW_COLUMN (column)); g_assert (strcmp (gtk_tree_view_column_get_title (column), "Test") == 0); renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column)); g_assert (g_list_length (renderers) == 1); renderer = g_list_nth_data (renderers, 0); g_assert (renderer); g_assert (GTK_IS_CELL_RENDERER_TEXT (renderer)); g_list_free (renderers); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_icon_view (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " " " " " " " " " test" " " " " " " " " " " " " " liststore1" " 0" " 1" " True" " " " " " " " 0" " " " " " " " " " " ""; GObject *window, *iconview; builder = builder_new_from_string (buffer, -1, NULL); iconview = gtk_builder_get_object (builder, "iconview1"); g_assert (iconview); g_assert (GTK_IS_ICON_VIEW (iconview)); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_combo_box (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " " " " " " " " " 1" " Foo" " " " " " 2" " Bar" " " " " " " " " " " " " " liststore1" " True" " " " " " " " 0" " " " " " " " " " " " 1" " " " " " " " " " " ""; GObject *window, *combobox; builder = builder_new_from_string (buffer, -1, NULL); combobox = gtk_builder_get_object (builder, "combobox1"); g_assert (combobox); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } #if 0 static void test_combo_box_entry (void) { GtkBuilder *builder; const gchar buffer[] = "" " " " " " " " " " " " " " " " 1" " Foo" " " " " " 2" " Bar" " " " " " " " " " " " " " liststore1" " True" " True" " " " " " " " 0" " " " " " " " " " " " 1" " " " " " " " " " " ""; GObject *window, *combobox, *renderer; gchar *text; builder = builder_new_from_string (buffer, -1, NULL); combobox = gtk_builder_get_object (builder, "comboboxentry1"); g_assert (combobox); renderer = gtk_builder_get_object (builder, "renderer2"); g_assert (renderer); g_object_get (renderer, "text", &text, NULL); g_assert (text); g_assert (strcmp (text, "Bar") == 0); g_free (text); renderer = gtk_builder_get_object (builder, "renderer1"); g_assert (renderer); g_object_get (renderer, "text", &text, NULL); g_assert (text); g_assert (strcmp (text, "2") == 0); g_free (text); window = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } #endif static void test_cell_view (void) { GtkBuilder *builder; const gchar *buffer = "" " " " " " " " " " " " " " test" " " " " " " " " " " " " " True" " liststore1" " " " " " " " " " 0" " " " " " " " " " " ""; GObject *cellview; GObject *model, *window; GtkTreePath *path; GList *renderers; builder = builder_new_from_string (buffer, -1, NULL); cellview = gtk_builder_get_object (builder, "cellview1"); g_assert (builder); g_assert (cellview); g_assert (GTK_IS_CELL_VIEW (cellview)); g_object_get (cellview, "model", &model, NULL); g_assert (model); g_assert (GTK_IS_TREE_MODEL (model)); g_object_unref (model); path = gtk_tree_path_new_first (); gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cellview), path); renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cellview)); g_assert (renderers); g_assert (g_list_length (renderers) == 1); window = gtk_builder_get_object (builder, "window1"); g_assert (window); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_dialog (void) { GtkBuilder * builder; const gchar buffer1[] = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " button_ok" " button_cancel" " " " " ""; GObject *dialog1; GObject *button_ok; GObject *button_cancel; builder = builder_new_from_string (buffer1, -1, NULL); dialog1 = gtk_builder_get_object (builder, "dialog1"); button_ok = gtk_builder_get_object (builder, "button_ok"); g_assert (gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog1), GTK_WIDGET (button_ok)) == 3); button_cancel = gtk_builder_get_object (builder, "button_cancel"); g_assert (gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog1), GTK_WIDGET (button_cancel)) == -5); gtk_widget_destroy (GTK_WIDGET (dialog1)); g_object_unref (builder); } static void test_message_dialog (void) { GtkBuilder * builder; const gchar buffer1[] = "" " " " " " " " " " " " " " " " " " " ""; GObject *dialog1; GObject *expander; builder = builder_new_from_string (buffer1, -1, NULL); dialog1 = gtk_builder_get_object (builder, "dialog1"); expander = gtk_builder_get_object (builder, "expander"); g_assert (GTK_IS_EXPANDER (expander)); g_assert (gtk_widget_get_parent (GTK_WIDGET (expander)) == gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog1))); gtk_widget_destroy (GTK_WIDGET (dialog1)); g_object_unref (builder); } static void test_accelerators (void) { GtkBuilder *builder; const gchar *buffer = "" " " " " " " " " " " " " " " ""; const gchar *buffer2 = "" " " " " " " " " " " " " " " " " ""; GObject *window1; GSList *accel_groups; GObject *accel_group; builder = builder_new_from_string (buffer, -1, NULL); window1 = gtk_builder_get_object (builder, "window1"); g_assert (window1); g_assert (GTK_IS_WINDOW (window1)); accel_groups = gtk_accel_groups_from_object (window1); g_assert (g_slist_length (accel_groups) == 1); accel_group = g_slist_nth_data (accel_groups, 0); g_assert (accel_group); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); window1 = gtk_builder_get_object (builder, "window1"); g_assert (window1); g_assert (GTK_IS_WINDOW (window1)); accel_groups = gtk_accel_groups_from_object (window1); g_assert (g_slist_length (accel_groups) == 1); accel_group = g_slist_nth_data (accel_groups, 0); g_assert (accel_group); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); } static void test_widget (void) { const gchar *buffer = "" " " " " " " " True" " True" " " " " " " ""; const gchar *buffer2 = "" " " " " " " " True" " True" " " " " " " ""; const gchar *buffer3 = "" " " " " " " " " " " " " " " " A Label" " " " " " " " " " " " " " " " " " " " " " " " Sniff" " " " " " " " " " " " " ""; GtkBuilder *builder; GObject *window1, *button1, *label1; AtkObject *accessible; AtkRelationSet *relation_set; AtkRelation *relation; char *name; builder = builder_new_from_string (buffer, -1, NULL); button1 = gtk_builder_get_object (builder, "button1"); #if 0 g_assert (gtk_widget_has_focus (GTK_WIDGET (button1))); #endif window1 = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); button1 = gtk_builder_get_object (builder, "button1"); g_assert (gtk_widget_get_receives_default (GTK_WIDGET (button1))); g_object_unref (builder); builder = builder_new_from_string (buffer3, -1, NULL); window1 = gtk_builder_get_object (builder, "window1"); label1 = gtk_builder_get_object (builder, "label1"); accessible = gtk_widget_get_accessible (GTK_WIDGET (label1)); relation_set = atk_object_ref_relation_set (accessible); g_return_if_fail (atk_relation_set_get_n_relations (relation_set) == 1); relation = atk_relation_set_get_relation (relation_set, 0); g_return_if_fail (relation != NULL); g_return_if_fail (ATK_IS_RELATION (relation)); g_return_if_fail (atk_relation_get_relation_type (relation) != ATK_RELATION_LABELLED_BY); g_object_unref (relation_set); g_object_get (G_OBJECT (accessible), "accessible-name", &name, NULL); g_return_if_fail (strcmp (name, "A Label") == 0); g_free (name); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); } static void test_window (void) { const gchar *buffer1 = "" " " " " " " ""; const gchar *buffer2 = "" " " " " ""; GtkBuilder *builder; GObject *window1; gchar *title; builder = builder_new_from_string (buffer1, -1, NULL); window1 = gtk_builder_get_object (builder, "window1"); g_object_get (window1, "title", &title, NULL); g_assert (strcmp (title, "") == 0); g_free (title); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); window1 = gtk_builder_get_object (builder, "window1"); gtk_widget_destroy (GTK_WIDGET (window1)); g_object_unref (builder); } static void test_value_from_string (void) { GValue value = G_VALUE_INIT; GError *error = NULL; GtkBuilder *builder; builder = gtk_builder_new (); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_STRING, "test", &value, &error)); g_assert (G_VALUE_HOLDS_STRING (&value)); g_assert (strcmp (g_value_get_string (&value), "test") == 0); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "true", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == TRUE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "false", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == FALSE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "yes", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == TRUE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "no", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == FALSE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "0", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == FALSE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "1", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == TRUE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "tRuE", &value, &error)); g_assert (G_VALUE_HOLDS_BOOLEAN (&value)); g_assert (g_value_get_boolean (&value) == TRUE); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "blaurgh", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "yess", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "trueee", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, "", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_INT, "12345", &value, &error)); g_assert (G_VALUE_HOLDS_INT (&value)); g_assert (g_value_get_int (&value) == 12345); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_LONG, "9912345", &value, &error)); g_assert (G_VALUE_HOLDS_LONG (&value)); g_assert (g_value_get_long (&value) == 9912345); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_UINT, "2345", &value, &error)); g_assert (G_VALUE_HOLDS_UINT (&value)); g_assert (g_value_get_uint (&value) == 2345); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_INT64, "-2345", &value, &error)); g_assert (G_VALUE_HOLDS_INT64 (&value)); g_assert (g_value_get_int64 (&value) == -2345); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_UINT64, "2345", &value, &error)); g_assert (G_VALUE_HOLDS_UINT64 (&value)); g_assert (g_value_get_uint64 (&value) == 2345); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_FLOAT, "1.454", &value, &error)); g_assert (G_VALUE_HOLDS_FLOAT (&value)); g_assert (fabs (g_value_get_float (&value) - 1.454) < 0.00001); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_FLOAT, "abc", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, G_TYPE_INT, "/-+,abc", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, GTK_TYPE_WINDOW_TYPE, "toplevel", &value, &error) == TRUE); g_assert (G_VALUE_HOLDS_ENUM (&value)); g_assert (g_value_get_enum (&value) == GTK_WINDOW_TOPLEVEL); g_value_unset (&value); g_assert (gtk_builder_value_from_string_type (builder, GTK_TYPE_WINDOW_TYPE, "sliff", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_assert (gtk_builder_value_from_string_type (builder, GTK_TYPE_WINDOW_TYPE, "foobar", &value, &error) == FALSE); g_value_unset (&value); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE)); g_error_free (error); error = NULL; g_object_unref (builder); } static gboolean model_freed = FALSE; static void model_weakref (gpointer data, GObject *model) { model_freed = TRUE; } static void test_reference_counting (void) { GtkBuilder *builder; const gchar buffer1[] = "" " " " " " " " " " " " liststore1" " " " " " " ""; const gchar buffer2[] = "" " " " " " " " " " start" " " " " " " ""; GObject *window, *treeview, *model; builder = builder_new_from_string (buffer1, -1, NULL); window = gtk_builder_get_object (builder, "window1"); treeview = gtk_builder_get_object (builder, "treeview1"); model = gtk_builder_get_object (builder, "liststore1"); g_object_unref (builder); g_object_weak_ref (model, (GWeakNotify)model_weakref, NULL); g_assert (model_freed == FALSE); gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), NULL); g_assert (model_freed == TRUE); gtk_widget_destroy (GTK_WIDGET (window)); builder = builder_new_from_string (buffer2, -1, NULL); g_object_unref (builder); } static void test_icon_factory (void) { GtkBuilder *builder; const gchar buffer1[] = "" " " " " " " " " " " ""; const gchar buffer2[] = "" " " " " " " " " " " " " ""; #if 0 const gchar buffer3[] = "" " " " " " " ""; const gchar buffer4[] = "" " " " " " " " " " " ""; const gchar buffer5[] = "" " " " " " " " " " " ""; GError *error = NULL; #endif GObject *factory; GtkIconSet *icon_set; GtkIconSource *icon_source; GtkWidget *image; G_GNUC_BEGIN_IGNORE_DEPRECATIONS; builder = builder_new_from_string (buffer1, -1, NULL); factory = gtk_builder_get_object (builder, "iconfactory1"); g_assert (factory != NULL); icon_set = gtk_icon_factory_lookup (GTK_ICON_FACTORY (factory), "apple-red"); g_assert (icon_set != NULL); gtk_icon_factory_add_default (GTK_ICON_FACTORY (factory)); G_GNUC_BEGIN_IGNORE_DEPRECATIONS; image = gtk_image_new_from_stock ("apple-red", GTK_ICON_SIZE_BUTTON); G_GNUC_END_IGNORE_DEPRECATIONS; g_assert (image != NULL); g_object_ref_sink (image); g_object_unref (image); g_object_unref (builder); builder = builder_new_from_string (buffer2, -1, NULL); factory = gtk_builder_get_object (builder, "iconfactory1"); g_assert (factory != NULL); icon_set = gtk_icon_factory_lookup (GTK_ICON_FACTORY (factory), "sliff"); g_assert (icon_set != NULL); g_assert (g_slist_length (icon_set->sources) == 2); icon_source = icon_set->sources->data; g_assert (gtk_icon_source_get_direction (icon_source) == GTK_TEXT_DIR_RTL); g_assert (gtk_icon_source_get_state (icon_source) == GTK_STATE_ACTIVE); g_assert (gtk_icon_source_get_size (icon_source) == GTK_ICON_SIZE_MENU); g_assert (g_str_has_suffix (gtk_icon_source_get_filename (icon_source), "sloff.png")); icon_source = icon_set->sources->next->data; g_assert (gtk_icon_source_get_direction (icon_source) == GTK_TEXT_DIR_LTR); g_assert (gtk_icon_source_get_state (icon_source) == GTK_STATE_SELECTED); g_assert (gtk_icon_source_get_size (icon_source) == GTK_ICON_SIZE_DND); g_assert (g_str_has_suffix (gtk_icon_source_get_filename (icon_source), "slurf.png")); g_object_unref (builder); #if 0 error = NULL; gtk_builder_add_from_string (builder, buffer3, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TAG)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, buffer4, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TAG)); g_error_free (error); error = NULL; gtk_builder_add_from_string (builder, buffer5, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ATTRIBUTE)); g_error_free (error); #endif G_GNUC_BEGIN_IGNORE_DEPRECATIONS; } typedef struct { gboolean weight; gboolean foreground; gboolean underline; gboolean size; gboolean font_desc; gboolean language; } FoundAttrs; static gboolean filter_pango_attrs (PangoAttribute *attr, gpointer data) { FoundAttrs *found = (FoundAttrs *)data; if (attr->klass->type == PANGO_ATTR_WEIGHT) found->weight = TRUE; else if (attr->klass->type == PANGO_ATTR_FOREGROUND) found->foreground = TRUE; else if (attr->klass->type == PANGO_ATTR_UNDERLINE) found->underline = TRUE; /* Make sure optional start/end properties are working */ else if (attr->klass->type == PANGO_ATTR_SIZE && attr->start_index == 5 && attr->end_index == 10) found->size = TRUE; else if (attr->klass->type == PANGO_ATTR_FONT_DESC) found->font_desc = TRUE; else if (attr->klass->type == PANGO_ATTR_LANGUAGE) found->language = TRUE; return TRUE; } static void test_pango_attributes (void) { GtkBuilder *builder; FoundAttrs found = { 0, }; const gchar buffer[] = "" " " " " " " " " " " " " " " " " " " " " ""; const gchar err_buffer1[] = "" " " " " " " " " " " ""; const gchar err_buffer2[] = "" " " " " " " " " " " ""; GObject *label; GError *error = NULL; PangoAttrList *attrs, *filtered; /* Test attributes are set */ builder = builder_new_from_string (buffer, -1, NULL); label = gtk_builder_get_object (builder, "label1"); g_assert (label != NULL); attrs = gtk_label_get_attributes (GTK_LABEL (label)); g_assert (attrs != NULL); filtered = pango_attr_list_filter (attrs, filter_pango_attrs, &found); g_assert (filtered); pango_attr_list_unref (filtered); g_assert (found.weight); g_assert (found.foreground); g_assert (found.underline); g_assert (found.size); g_assert (found.language); g_assert (found.font_desc); g_object_unref (builder); /* Test errors are set */ builder = gtk_builder_new (); gtk_builder_add_from_string (builder, err_buffer1, -1, &error); label = gtk_builder_get_object (builder, "label1"); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_MISSING_ATTRIBUTE)); g_object_unref (builder); g_error_free (error); error = NULL; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, err_buffer2, -1, &error); label = gtk_builder_get_object (builder, "label1"); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ATTRIBUTE)); g_object_unref (builder); g_error_free (error); } static void test_requires (void) { GtkBuilder *builder; GError *error = NULL; gchar *buffer; const gchar buffer_fmt[] = "" " " ""; buffer = g_strdup_printf (buffer_fmt, GTK_MAJOR_VERSION, GTK_MINOR_VERSION + 1); builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_VERSION_MISMATCH)); g_object_unref (builder); g_error_free (error); g_free (buffer); } static void test_add_objects (void) { GtkBuilder *builder; GError *error; gint ret; GObject *obj; GtkUIManager *manager; GtkWidget *menubar; GObject *menu, *label; GList *children; gchar *objects[2] = {"mainbox", NULL}; gchar *objects2[3] = {"mainbox", "window2", NULL}; gchar *objects3[3] = {"uimgr1", "menubar1"}; gchar *objects4[2] = {"uimgr1", NULL}; const gchar buffer[] = "" " " " " " " " True" " " " " " True" " first label" " " " " " " " " " True" " second label" " " " " " 1" " " " " " " " " " " " " " " " " " second label" " " " " " " ""; const gchar buffer2[] = "" " " " " " " " " " " " _File" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; error = NULL; builder = gtk_builder_new (); ret = gtk_builder_add_objects_from_string (builder, buffer, -1, objects, &error); g_assert (ret); g_assert (error == NULL); obj = gtk_builder_get_object (builder, "window"); g_assert (obj == NULL); obj = gtk_builder_get_object (builder, "window2"); g_assert (obj == NULL); obj = gtk_builder_get_object (builder, "mainbox"); g_assert (GTK_IS_WIDGET (obj)); g_object_unref (builder); error = NULL; builder = gtk_builder_new (); ret = gtk_builder_add_objects_from_string (builder, buffer, -1, objects2, &error); g_assert (ret); g_assert (error == NULL); obj = gtk_builder_get_object (builder, "window"); g_assert (obj == NULL); obj = gtk_builder_get_object (builder, "window2"); g_assert (GTK_IS_WINDOW (obj)); gtk_widget_destroy (GTK_WIDGET (obj)); obj = gtk_builder_get_object (builder, "mainbox"); g_assert (GTK_IS_WIDGET (obj)); g_object_unref (builder); /* test cherry picking a ui manager and menubar that depends on it */ error = NULL; builder = gtk_builder_new (); ret = gtk_builder_add_objects_from_string (builder, buffer2, -1, objects3, &error); g_assert (ret); obj = gtk_builder_get_object (builder, "uimgr1"); g_assert (GTK_IS_UI_MANAGER (obj)); obj = gtk_builder_get_object (builder, "file"); g_assert (GTK_IS_ACTION (obj)); obj = gtk_builder_get_object (builder, "menubar1"); g_assert (GTK_IS_MENU_BAR (obj)); menubar = GTK_WIDGET (obj); children = gtk_container_get_children (GTK_CONTAINER (menubar)); menu = children->data; g_assert (menu != NULL); g_assert (GTK_IS_MENU_ITEM (menu)); g_assert (strcmp (gtk_widget_get_name (GTK_WIDGET (menu)), "file") == 0); g_list_free (children); label = G_OBJECT (gtk_bin_get_child (GTK_BIN (menu))); g_assert (label != NULL); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_text (GTK_LABEL (label)), "File") == 0); g_object_unref (builder); /* test cherry picking just the ui manager */ error = NULL; builder = gtk_builder_new (); ret = gtk_builder_add_objects_from_string (builder, buffer2, -1, objects4, &error); g_assert (ret); obj = gtk_builder_get_object (builder, "uimgr1"); g_assert (GTK_IS_UI_MANAGER (obj)); manager = GTK_UI_MANAGER (obj); obj = gtk_builder_get_object (builder, "file"); g_assert (GTK_IS_ACTION (obj)); menubar = gtk_ui_manager_get_widget (manager, "/menubar1"); g_assert (GTK_IS_MENU_BAR (menubar)); children = gtk_container_get_children (GTK_CONTAINER (menubar)); menu = children->data; g_assert (menu != NULL); g_assert (GTK_IS_MENU_ITEM (menu)); g_assert (strcmp (gtk_widget_get_name (GTK_WIDGET (menu)), "file") == 0); g_list_free (children); label = G_OBJECT (gtk_bin_get_child (GTK_BIN (menu))); g_assert (label != NULL); g_assert (GTK_IS_LABEL (label)); g_assert (strcmp (gtk_label_get_text (GTK_LABEL (label)), "File") == 0); g_object_unref (builder); } static GtkWidget * get_parent_menubar (GtkWidget *menuitem) { GtkMenuShell *menu_shell; GtkWidget *attach = NULL; menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (menuitem)); g_assert (GTK_IS_MENU_SHELL (menu_shell)); while (menu_shell && !GTK_IS_MENU_BAR (menu_shell)) { if (GTK_IS_MENU (menu_shell) && (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL) menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach)); else menu_shell = NULL; } return menu_shell ? GTK_WIDGET (menu_shell) : NULL; } static void test_menus (void) { const gchar *buffer = "" " " " " " " " " " " " " " True" " vertical" " " " " " True" " " " " " True" " _File" " True" " " " " " True" " " " " " gtk-new" " True" " True" " accelgroup1" " " " " " " " " " " " " " " " " " " " " " " "" ""; const gchar *buffer1 = "" " " " " " " " " " " " " " True" " vertical" " " " " " True" " " " " " True" " " " " " True" " a label" " " " " " " " " " " " " " " " " " " "" ""; GtkBuilder *builder; GtkWidget *child; GtkWidget *window, *item; GtkAccelGroup *accel_group; GtkWidget *item_accel_label, *sample_accel_label, *sample_menu_item, *custom; /* Check that the item has the correct accel label string set */ builder = builder_new_from_string (buffer, -1, NULL); window = (GtkWidget *)gtk_builder_get_object (builder, "window1"); item = (GtkWidget *)gtk_builder_get_object (builder, "imagemenuitem1"); accel_group = (GtkAccelGroup *)gtk_builder_get_object (builder, "accelgroup1"); gtk_widget_show_all (window); G_GNUC_BEGIN_IGNORE_DEPRECATIONS; sample_menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_NEW, accel_group); G_GNUC_END_IGNORE_DEPRECATIONS; child = gtk_bin_get_child (GTK_BIN (sample_menu_item)); g_assert (child); g_assert (GTK_IS_ACCEL_LABEL (child)); sample_accel_label = child; gtk_widget_show (sample_accel_label); child = gtk_bin_get_child (GTK_BIN (item)); g_assert (child); g_assert (GTK_IS_ACCEL_LABEL (child)); item_accel_label = child; gtk_accel_label_refetch (GTK_ACCEL_LABEL (sample_accel_label)); gtk_accel_label_refetch (GTK_ACCEL_LABEL (item_accel_label)); g_assert (gtk_label_get_text (GTK_LABEL (sample_accel_label)) != NULL); g_assert (gtk_label_get_text (GTK_LABEL (item_accel_label)) != NULL); g_assert (strcmp (gtk_label_get_text (GTK_LABEL (item_accel_label)), gtk_label_get_text (GTK_LABEL (sample_accel_label))) == 0); /* Check the menu hierarchy worked here */ g_assert (get_parent_menubar (item)); gtk_widget_destroy (GTK_WIDGET (window)); gtk_widget_destroy (sample_menu_item); g_object_unref (builder); /* Check that we can add alien children to menu items via normal * GtkContainer apis. */ builder = builder_new_from_string (buffer1, -1, NULL); window = (GtkWidget *)gtk_builder_get_object (builder, "window1"); item = (GtkWidget *)gtk_builder_get_object (builder, "imagemenuitem1"); custom = (GtkWidget *)gtk_builder_get_object (builder, "custom1"); g_assert (gtk_widget_get_parent (custom) == item); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } static void test_file (const gchar *filename) { GtkBuilder *builder; GError *error = NULL; GSList *l, *objects; builder = gtk_builder_new (); if (!gtk_builder_add_from_file (builder, filename, &error)) { g_error ("%s", error->message); g_error_free (error); return; } objects = gtk_builder_get_objects (builder); for (l = objects; l; l = l->next) { GObject *obj = (GObject*)l->data; if (GTK_IS_DIALOG (obj)) { g_print ("Running dialog %s.\n", gtk_widget_get_name (GTK_WIDGET (obj))); gtk_dialog_run (GTK_DIALOG (obj)); } else if (GTK_IS_WINDOW (obj)) { g_signal_connect (obj, "destroy", G_CALLBACK (gtk_main_quit), NULL); g_print ("Showing %s.\n", gtk_widget_get_name (GTK_WIDGET (obj))); gtk_widget_show_all (GTK_WIDGET (obj)); } } gtk_main (); g_object_unref (builder); } static void test_message_area (void) { GtkBuilder *builder; GObject *obj, *obj1; const gchar buffer[] = "" " " " " " " " " " " " Message" " " " " " False" " " " " " " " " " " " " " " " " " gtk-ok" " yes" " " " " " " " " " " " button_ok" " " " " ""; builder = builder_new_from_string (buffer, -1, NULL); obj = gtk_builder_get_object (builder, "infobar1"); g_assert (GTK_IS_INFO_BAR (obj)); obj1 = gtk_builder_get_object (builder, "content"); g_assert (GTK_IS_LABEL (obj1)); obj1 = gtk_builder_get_object (builder, "button_ok"); g_assert (GTK_IS_BUTTON (obj1)); g_object_unref (builder); } static void test_gmenu (void) { GtkBuilder *builder; GObject *obj, *obj1; const gchar buffer[] = "" " " " " " " "
" " " " Undo" " undo" " " " " " Redo" " redo" " " "
" "
" "
" " Copy & Paste" " " " Cut" " cut" " " " " " Copy" " copy" " " " " " Paste" " paste" " " "
" " " " " " Bold" " bold" " " " " " Language" " " " Latin" " lang" " 'latin'" " " " " " Greek" " lang" " 'greek'" " " " " " Urdu" " lang" " 'urdu'" " " " " " " "
" "
"; builder = builder_new_from_string (buffer, -1, NULL); obj = gtk_builder_get_object (builder, "window"); g_assert (GTK_IS_WINDOW (obj)); obj1 = gtk_builder_get_object (builder, "edit-menu"); g_assert (G_IS_MENU_MODEL (obj1)); obj1 = gtk_builder_get_object (builder, "blargh"); g_assert (G_IS_MENU_MODEL (obj1)); g_object_unref (builder); } static void test_level_bar (void) { GtkBuilder *builder; GError *error = NULL; GObject *obj, *obj1; const gchar buffer1[] = "" " " " " " " " 4.70" " 2" " 5" " " " " " " " " " " " " " " " " ""; const gchar buffer2[] = "" " " " " " " " " " " ""; const gchar buffer3[] = "" " " " " " " " " " " " " " " ""; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer1, -1, &error); g_assert (error == NULL); obj = gtk_builder_get_object (builder, "window"); g_assert (GTK_IS_WINDOW (obj)); obj1 = gtk_builder_get_object (builder, "levelbar"); g_assert (GTK_IS_LEVEL_BAR (obj1)); g_object_unref (builder); error = NULL; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer2, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ATTRIBUTE)); g_error_free (error); g_object_unref (builder); error = NULL; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer3, -1, &error); g_assert (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_UNHANDLED_TAG)); g_error_free (error); g_object_unref (builder); } static GObject *external_object = NULL, *external_object_swapped = NULL; void on_button_clicked (GtkButton *button, GObject *data) { external_object = data; } void on_button_clicked_swapped (GObject *data, GtkButton *button) { external_object_swapped = data; } static void test_expose_object (void) { GtkBuilder *builder; GError *error = NULL; GtkWidget *image; GObject *obj; const gchar buffer[] = "" " " " external_image" " " " " " " ""; image = gtk_image_new (); builder = gtk_builder_new (); gtk_builder_expose_object (builder, "external_image", G_OBJECT (image)); gtk_builder_expose_object (builder, "builder", G_OBJECT (builder)); gtk_builder_add_from_string (builder, buffer, -1, &error); g_assert (error == NULL); obj = gtk_builder_get_object (builder, "button"); g_assert (GTK_IS_BUTTON (obj)); g_assert (gtk_button_get_image (GTK_BUTTON (obj)) == image); /* Connect signals and fake clicked event */ gtk_builder_connect_signals (builder, NULL); gtk_button_clicked (GTK_BUTTON (obj)); g_assert (external_object == G_OBJECT (builder)); g_assert (external_object_swapped == G_OBJECT (builder)); g_object_unref (builder); g_object_unref (image); } static void test_no_ids (void) { GtkBuilder *builder; GError *error = NULL; GObject *obj; const gchar buffer[] = "" " " " " " " " " " " " Message" " " " " " False" " " " " " " " " " " " " " " " " " gtk-ok" " yes" " " " " " " " " " " " button_ok" " " " " ""; builder = gtk_builder_new (); gtk_builder_add_from_string (builder, buffer, -1, &error); g_assert (error == NULL); obj = gtk_builder_get_object (builder, "button_ok"); g_assert (GTK_IS_BUTTON (obj)); g_object_unref (builder); } static void test_property_bindings (void) { const gchar *buffer = "" " " " " " " " True" " vertical" " " " " " false" " " " " " " " " " false" " " " " " " " " " " " " " " " " " " " " ""; GtkBuilder *builder; GObject *checkbutton, *button, *button2, *window; builder = builder_new_from_string (buffer, -1, NULL); checkbutton = gtk_builder_get_object (builder, "checkbutton"); g_assert (GTK_IS_CHECK_BUTTON (checkbutton)); g_assert (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton))); button = gtk_builder_get_object (builder, "button"); g_assert (GTK_IS_BUTTON (button)); g_assert (!gtk_widget_get_sensitive (GTK_WIDGET (button))); button2 = gtk_builder_get_object (builder, "button2"); g_assert (GTK_IS_BUTTON (button2)); g_assert (gtk_widget_get_sensitive (GTK_WIDGET (button2))); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), TRUE); g_assert (gtk_widget_get_sensitive (GTK_WIDGET (button))); g_assert (gtk_widget_get_sensitive (GTK_WIDGET (button2))); window = gtk_builder_get_object (builder, "window"); gtk_widget_destroy (GTK_WIDGET (window)); g_object_unref (builder); } #define MY_GTK_GRID_TEMPLATE "\ \n\ \n\ \n" #define MY_TYPE_GTK_GRID (my_gtk_grid_get_type ()) #define MY_IS_GTK_GRID(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MY_TYPE_GTK_GRID)) typedef struct { GtkGridClass parent_class; } MyGtkGridClass; typedef struct { GtkLabel *label; } MyGtkGridPrivate; typedef struct { GtkGrid parent_instance; GtkLabel *label; MyGtkGridPrivate *priv; } MyGtkGrid; G_DEFINE_TYPE_WITH_PRIVATE (MyGtkGrid, my_gtk_grid, GTK_TYPE_GRID); static void my_gtk_grid_init (MyGtkGrid *grid) { grid->priv = my_gtk_grid_get_instance_private (grid); gtk_widget_init_template (GTK_WIDGET (grid)); } static void my_gtk_grid_class_init (MyGtkGridClass *klass) { GBytes *template = g_bytes_new_static (MY_GTK_GRID_TEMPLATE, strlen (MY_GTK_GRID_TEMPLATE)); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); gtk_widget_class_set_template (widget_class, template); gtk_widget_class_bind_template_child (widget_class, MyGtkGrid, label); gtk_widget_class_bind_template_child_private (widget_class, MyGtkGrid, label); } static void test_template () { MyGtkGrid *my_gtk_grid; /* make sure the type we are trying to register does not exist */ g_assert (!g_type_from_name ("MyGtkGrid")); /* create the template object */ my_gtk_grid = g_object_new (MY_TYPE_GTK_GRID, NULL); /* Check everything is fine */ g_assert (g_type_from_name ("MyGtkGrid")); g_assert (MY_IS_GTK_GRID (my_gtk_grid)); g_assert (my_gtk_grid->label == my_gtk_grid->priv->label); g_assert (GTK_IS_LABEL (my_gtk_grid->label)); g_assert (GTK_IS_LABEL (my_gtk_grid->priv->label)); } void on_cellrenderertoggle1_toggled (GtkCellRendererToggle *cell) { } static void test_anaconda_signal (void) { GtkBuilder *builder; const gchar buffer[] = "" "" "" " " " " " " " " " " " " " " " " " False" " " " " " True" " True" " liststore1" " " " " " " " " " " " column" " " " " " " " " " " " 0" " " " " " " " " " " " " " " ""; builder = builder_new_from_string (buffer, -1, NULL); gtk_builder_connect_signals (builder, NULL); g_object_unref (builder); } int main (int argc, char **argv) { /* initialize test program */ gtk_test_init (&argc, &argv); if (argc > 1) { test_file (argv[1]); return 0; } g_test_add_func ("/Builder/Parser", test_parser); g_test_add_func ("/Builder/Types", test_types); g_test_add_func ("/Builder/Construct-Only Properties", test_construct_only_property); g_test_add_func ("/Builder/Children", test_children); g_test_add_func ("/Builder/Child Properties", test_child_properties); g_test_add_func ("/Builder/Object Properties", test_object_properties); g_test_add_func ("/Builder/Notebook", test_notebook); g_test_add_func ("/Builder/Domain", test_domain); g_test_add_func ("/Builder/Signal Autoconnect", test_connect_signals); g_test_add_func ("/Builder/UIManager Simple", test_uimanager_simple); g_test_add_func ("/Builder/Spin Button", test_spin_button); g_test_add_func ("/Builder/SizeGroup", test_sizegroup); g_test_add_func ("/Builder/ListStore", test_list_store); g_test_add_func ("/Builder/TreeStore", test_tree_store); g_test_add_func ("/Builder/TreeView Column", test_treeview_column); g_test_add_func ("/Builder/IconView", test_icon_view); g_test_add_func ("/Builder/ComboBox", test_combo_box); #if 0 g_test_add_func ("/Builder/ComboBox Entry", test_combo_box_entry); #endif g_test_add_func ("/Builder/CellView", test_cell_view); g_test_add_func ("/Builder/Dialog", test_dialog); g_test_add_func ("/Builder/Accelerators", test_accelerators); g_test_add_func ("/Builder/Widget", test_widget); g_test_add_func ("/Builder/Value From String", test_value_from_string); g_test_add_func ("/Builder/Reference Counting", test_reference_counting); g_test_add_func ("/Builder/Window", test_window); g_test_add_func ("/Builder/IconFactory", test_icon_factory); g_test_add_func ("/Builder/PangoAttributes", test_pango_attributes); g_test_add_func ("/Builder/Requires", test_requires); g_test_add_func ("/Builder/AddObjects", test_add_objects); g_test_add_func ("/Builder/Menus", test_menus); g_test_add_func ("/Builder/MessageArea", test_message_area); g_test_add_func ("/Builder/MessageDialog", test_message_dialog); g_test_add_func ("/Builder/GMenu", test_gmenu); g_test_add_func ("/Builder/LevelBar", test_level_bar); g_test_add_func ("/Builder/Expose Object", test_expose_object); g_test_add_func ("/Builder/Template", test_template); g_test_add_func ("/Builder/No IDs", test_no_ids); g_test_add_func ("/Builder/Property Bindings", test_property_bindings); g_test_add_func ("/Builder/anaconda-signal", test_anaconda_signal); return g_test_run(); }