Stop using gtk_main_quit

Stop using gtk_main and gtk_main_quit in tests and
examples. These APIs are on the way out.
This commit is contained in:
Matthias Clasen 2020-02-09 20:06:58 -05:00
parent 7eb889c7aa
commit c789711652
9 changed files with 326 additions and 114 deletions

View File

@ -232,7 +232,7 @@ shoot_one (WidgetInfo *info)
if (g_list_find (toplevels, info) == NULL)
{
g_warning ("Widget not found in queue");
gtk_main_quit ();
exit (1);
}
window = gtk_native_get_surface (GTK_NATIVE (info->window));
@ -259,7 +259,7 @@ shoot_one (WidgetInfo *info)
/* remove from the queue and try to load up another */
toplevels = g_list_remove (toplevels, info);
if (toplevels == NULL)
gtk_main_quit ();
exit (0);
else
queue_show ();

View File

@ -8,6 +8,16 @@ print_hello (GtkWidget *widget,
g_print ("Hello World\n");
}
static void
quit_cb (GtkWidget *widget, gpointer data)
{
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
}
int
main (int argc,
char *argv[])
@ -15,6 +25,7 @@ main (int argc,
GtkBuilder *builder;
GObject *window;
GObject *button;
gboolean done = FALSE;
#ifdef GTK_SRCDIR
g_chdir (GTK_SRCDIR);
@ -28,7 +39,7 @@ main (int argc,
/* Connect signal handlers to the constructed widgets. */
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
button = gtk_builder_get_object (builder, "button1");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
@ -37,9 +48,12 @@ main (int argc,
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
button = gtk_builder_get_object (builder, "quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (button, "clicked", G_CALLBACK (quit_cb), &done);
gtk_main ();
gtk_widget_show (GTK_WIDGET (window));
while (!done)
g_main_context_iteration (NULL, TRUE);
return 0;
}

View File

@ -1410,7 +1410,6 @@ test_accelerators (void)
" <object class=\"GtkWindow\" id=\"window1\">"
" <child>"
" <object class=\"GtkTreeView\" id=\"treeview1\">"
" <signal name=\"cursor-changed\" handler=\"gtk_main_quit\"/>"
" </object>"
" </child>"
" </object>"
@ -1989,47 +1988,6 @@ test_add_objects (void)
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 (GTK_WIDGET (obj));
}
}
gtk_main ();
g_object_unref (builder);
}
static void
test_message_area (void)
{
@ -2519,12 +2477,6 @@ 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);

View File

@ -150,13 +150,6 @@ test_simple_row (void)
lc = gtk_layout_manager_get_layout_child (layout, GTK_WIDGET (child3));
gtk_grid_layout_child_set_left_attach (GTK_GRID_LAYOUT_CHILD (lc), 2);
#if 0
gtk_widget_show (window);
g_timeout_add (1000, (GSourceFunc)gtk_main_quit, NULL);
gtk_main ();
#endif
gtk_layout_manager_measure (layout,
parent,
GTK_ORIENTATION_HORIZONTAL,
@ -250,13 +243,6 @@ test_simple_column (void)
lc = gtk_layout_manager_get_layout_child (layout, GTK_WIDGET (child3));
gtk_grid_layout_child_set_top_attach (GTK_GRID_LAYOUT_CHILD (lc), 2);
#if 0
gtk_widget_show (window);
g_timeout_add (1000, (GSourceFunc)gtk_main_quit, NULL);
gtk_main ();
#endif
gtk_layout_manager_measure (layout,
parent,
GTK_ORIENTATION_HORIZONTAL,
@ -379,13 +365,6 @@ test_spans (void)
gtk_grid_layout_child_set_top_attach (GTK_GRID_LAYOUT_CHILD (lc), 1);
gtk_grid_layout_child_set_left_attach (GTK_GRID_LAYOUT_CHILD (lc), 2);
#if 0
gtk_widget_show (window);
g_timeout_add (1000, (GSourceFunc)gtk_main_quit, NULL);
gtk_main ();
#endif
gtk_layout_manager_measure (layout,
parent,
GTK_ORIENTATION_HORIZONTAL,
@ -502,13 +481,6 @@ test_homogeneous (void)
gtk_grid_layout_child_set_top_attach (GTK_GRID_LAYOUT_CHILD (lc), 1);
gtk_grid_layout_child_set_left_attach (GTK_GRID_LAYOUT_CHILD (lc), 1);
#if 0
gtk_widget_show (window);
g_timeout_add (1000, (GSourceFunc)gtk_main_quit, NULL);
gtk_main ();
#endif
gtk_layout_manager_measure (layout,
parent,
GTK_ORIENTATION_HORIZONTAL,

View File

@ -31,7 +31,11 @@ static gboolean finalized = FALSE;
static gboolean
main_loop_quit_cb (gpointer data)
{
gtk_main_quit ();
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
g_assert (finalized);
return FALSE;
@ -51,6 +55,7 @@ test_finalize_object (gconstpointer data)
{
GType test_type = GPOINTER_TO_SIZE (data);
GObject *object;
gboolean done;
if (g_str_equal (g_type_name (test_type), "GdkClipboard"))
object = g_object_new (test_type, "display", gdk_display_get_default (), NULL);
@ -97,8 +102,10 @@ test_finalize_object (gconstpointer data)
g_object_unref (object);
/* Even if the object did finalize, it may have left some dangerous stuff in the GMainContext */
g_timeout_add (50, main_loop_quit_cb, NULL);
gtk_main();
done = FALSE;
g_timeout_add (50, main_loop_quit_cb, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
}
static gboolean

View File

@ -24,7 +24,11 @@ tickle (gpointer data)
static gboolean
stop (gpointer data)
{
gtk_main_quit ();
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
return G_SOURCE_REMOVE;
}
@ -35,6 +39,7 @@ test_show_popover (void)
GtkWidget *window;
GtkWidget *button;
GtkWidget *popover;
gboolean done;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
button = gtk_menu_button_new ();
@ -47,9 +52,11 @@ test_show_popover (void)
g_timeout_add (1000, pop_up, popover);
g_timeout_add (2000, tickle, popover);
g_timeout_add (3000, stop, NULL);
done = FALSE;
g_timeout_add (3000, stop, &done);
gtk_main ();
while (!done)
g_main_context_iteration (NULL, TRUE);
}
int

View File

@ -25,7 +25,11 @@
static gboolean
main_loop_quit_cb (gpointer data)
{
gtk_main_quit ();
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
return FALSE;
}
@ -176,6 +180,7 @@ static void
test_app_chooser_dialog_basic (void)
{
GtkWidget *widget;
gboolean done;
widget = gtk_app_chooser_dialog_new_for_content_type (NULL, 0, "text/plain");
g_assert (GTK_IS_APP_CHOOSER_DIALOG (widget));
@ -184,8 +189,10 @@ test_app_chooser_dialog_basic (void)
* the main context then app_chooser_online_get_default_ready_cb()
* will be eventually called and segfault.
*/
g_timeout_add (500, main_loop_quit_cb, NULL);
gtk_main();
done = FALSE;
g_timeout_add (500, main_loop_quit_cb, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_widget_destroy (widget);
}
@ -243,6 +250,7 @@ static void
test_file_chooser_dialog_basic (void)
{
GtkWidget *widget;
gboolean done;
g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
@ -252,8 +260,10 @@ test_file_chooser_dialog_basic (void)
NULL);
g_assert (GTK_IS_FILE_CHOOSER_DIALOG (widget));
g_timeout_add (100, main_loop_quit_cb, NULL);
gtk_main();
done = FALSE;
g_timeout_add (100, main_loop_quit_cb, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_widget_destroy (widget);
}

212
testsuite/gtk/testsorter.c Normal file
View File

@ -0,0 +1,212 @@
#include <locale.h>
#include <gtk/gtk.h>
static GQuark number_quark;
static guint
get (GListModel *model,
guint position)
{
GObject *object = g_list_model_get_item (model, position);
g_assert (object != NULL);
g_object_unref (object);
return GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark));
}
static char *
get_string (gpointer object)
{
return g_strdup_printf ("%u", GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark)));
}
static void
append_digit (GString *s,
guint digit)
{
static char *names[10] = { NULL, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
if (digit == 0)
return;
g_assert (digit < 10);
if (s->len)
g_string_append_c (s, ' ');
g_string_append (s, names[digit]);
}
static void
append_below_thousand (GString *s,
guint n)
{
if (n >= 100)
{
append_digit (s, n / 100);
g_string_append (s, " hundred");
n %= 100;
}
if (n >= 20)
{
const char *names[10] = { NULL, NULL, "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (s->len)
g_string_append_c (s, ' ');
g_string_append (s, names [n / 10]);
n %= 10;
}
if (n >= 10)
{
const char *names[10] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
if (s->len)
g_string_append_c (s, ' ');
g_string_append (s, names [n - 10]);
}
else
{
append_digit (s, n);
}
}
static char *
get_spelled_out (gpointer object)
{
guint n = GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark));
GString *s;
g_assert (n < 1000000);
if (n == 0)
return g_strdup ("Zero");
s = g_string_new (NULL);
if (n >= 1000)
{
append_below_thousand (s, n / 1000);
g_string_append (s, " thousand");
n %= 1000;
}
append_below_thousand (s, n);
/* Capitalize first letter so we can do case-sensitive matching */
s->str[0] = g_ascii_toupper (s->str[0]);
return g_string_free (s, FALSE);
}
static char *
model_to_string (GListModel *model)
{
GString *string = g_string_new (NULL);
guint i;
for (i = 0; i < g_list_model_get_n_items (model); i++)
{
if (i > 0)
g_string_append (string, " ");
g_string_append_printf (string, "%u", get (model, i));
}
return g_string_free (string, FALSE);
}
static GListStore *
new_store (guint start,
guint end,
guint step);
static void
add (GListStore *store,
guint number)
{
GObject *object;
/* 0 cannot be differentiated from NULL, so don't use it */
g_assert (number != 0);
object = g_object_new (G_TYPE_OBJECT, NULL);
g_object_set_qdata (object, number_quark, GUINT_TO_POINTER (number));
g_list_store_append (store, object);
g_object_unref (object);
}
#define assert_model(model, expected) G_STMT_START{ \
char *s = model_to_string (G_LIST_MODEL (model)); \
if (!g_str_equal (s, expected)) \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#model " == " #expected, s, "==", expected); \
g_free (s); \
}G_STMT_END
static GListStore *
new_empty_store (void)
{
return g_list_store_new (G_TYPE_OBJECT);
}
static GListStore *
new_store (guint start,
guint end,
guint step)
{
GListStore *store = new_empty_store ();
guint i;
for (i = start; i <= end; i += step)
add (store, i);
return store;
}
static GtkFilterListModel *
new_model (guint size,
GtkFilter *filter)
{
GtkFilterListModel *result;
result = gtk_filter_list_model_new (G_LIST_MODEL (new_store (1, size, 1)), filter);
return result;
}
static int
sort_numbers (gpointer item1,
gpointer item2,
gpointer data)
{
guint n1 = GPOINTER_TO_UINT (g_object_get_qdata (item1, number_quark));
guint n2 = GPOINTER_TO_UINT (g_object_get_qdata (item2, number_quark));
}
static void
test_simple (void)
{
GtkSortListModel *model;
GtkSorter *sorter;
sorter = gtk_custom_sorter_new (sort_numbers, NULL);
model = new_model (20, filter);
shuffle (model);
g_object_unref (filter);
assert_model (model, "3 6 9 12 15 18");
g_object_unref (model);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
setlocale (LC_ALL, "C");
number_quark = g_quark_from_static_string ("Like a trashcan fire in a prison cell.");
g_test_add_func ("/sorter/simple", test_simple);
return g_test_run ();
}

View File

@ -10,7 +10,11 @@ static gboolean interactive = FALSE;
static gboolean
stop_main (gpointer data)
{
gtk_main_quit ();
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
return G_SOURCE_REMOVE;
}
@ -40,9 +44,17 @@ on_draw (GtkDrawingArea *da,
}
static gboolean
on_keypress (GtkEventControllerKey *key)
on_keypress (GtkEventControllerKey *key,
guint keyval,
guint keycode,
GdkModifierType state,
gpointer data)
{
gtk_main_quit ();
gboolean *done = data;
*done = TRUE;
g_main_context_wakeup (NULL);
return GDK_EVENT_PROPAGATE;
}
@ -53,12 +65,13 @@ test_default_size (void)
GtkWidget *window;
GtkWidget *da;
gint w, h;
gboolean done;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
if (interactive)
{
GtkEventController *controller = gtk_event_controller_key_new ();
g_signal_connect (controller, "key-pressed", G_CALLBACK (on_keypress), window);
g_signal_connect (controller, "key-pressed", G_CALLBACK (on_keypress), &done);
gtk_widget_add_controller (window, controller);
}
@ -85,9 +98,11 @@ test_default_size (void)
gtk_widget_show (window);
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, NULL);
gtk_main ();
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
/* check that the window and its content actually gets the right size */
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
@ -105,9 +120,11 @@ test_default_size (void)
g_assert_cmpint (w, ==, 100);
g_assert_cmpint (h, ==, 600);
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, NULL);
gtk_main ();
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
g_assert_cmpint (w, ==, 300);
@ -117,9 +134,11 @@ test_default_size (void)
gtk_widget_hide (window);
gtk_widget_show (window);
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, NULL);
gtk_main ();
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
g_assert_cmpint (w, ==, 300);
@ -134,6 +153,7 @@ test_resize (void)
GtkWidget *window;
GtkWidget *da;
gint w, h;
gboolean done;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
if (interactive)
@ -160,9 +180,11 @@ test_resize (void)
gtk_widget_show (window);
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, NULL);
gtk_main ();
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
/* test that resize before show works */
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
@ -175,9 +197,11 @@ test_resize (void)
*/
gtk_window_resize (GTK_WINDOW (window), 200, 400);
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, NULL);
gtk_main ();
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
g_assert_cmpint (w, ==, 200);
@ -191,6 +215,7 @@ test_resize_popup (void)
{
GtkWidget *window;
gint w, h;
gboolean done;
/* testcase for the dnd window */
window = gtk_window_new (GTK_WINDOW_POPUP);
@ -202,8 +227,11 @@ test_resize_popup (void)
gtk_widget_show (window);
g_timeout_add (200, stop_main, NULL);
gtk_main ();
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
g_assert_cmpint (w, ==, 1);
@ -217,6 +245,7 @@ test_show_hide (void)
{
GtkWidget *window;
gint w, h, w1, h1;
gboolean done;
/*http://bugzilla.gnome.org/show_bug.cgi?id=696882 */
@ -226,15 +255,21 @@ test_show_hide (void)
gtk_widget_show (window);
g_timeout_add (100, stop_main, NULL);
gtk_main ();
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
gtk_widget_hide (window);
g_timeout_add (100, stop_main, NULL);
gtk_main ();
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
g_assert_cmpint (w, ==, w1);
@ -242,8 +277,11 @@ test_show_hide (void)
gtk_widget_show (window);
g_timeout_add (100, stop_main, NULL);
gtk_main ();
done = FALSE;
if (!interactive)
g_timeout_add (200, stop_main, &done);
while (!done)
g_main_context_iteration (NULL, TRUE);
gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
g_assert_cmpint (w, ==, w1);