tests: Remove gtk_dialog_run()

Either use the "response" signal for dialogs that are already modal, or
use an explicit nested loop for tests that rely on the response id being
available in sequence.
This commit is contained in:
Emmanuele Bassi 2020-04-30 15:32:38 +01:00
parent 96856527e6
commit 2090dbb27d
6 changed files with 148 additions and 96 deletions

View File

@ -496,22 +496,9 @@ activate_preview (GSimpleAction *action,
} }
static void static void
activate_save_as (GSimpleAction *action, on_save_response (GtkWidget *dialog,
GVariant *parameter, int response)
gpointer user_data)
{ {
GtkWidget *dialog;
gint response;
dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window),
GTK_FILE_CHOOSER_ACTION_SAVE,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Save", GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK) if (response == GTK_RESPONSE_OK)
{ {
GFile *save_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); GFile *save_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
@ -522,6 +509,28 @@ activate_save_as (GSimpleAction *action,
gtk_window_destroy (GTK_WINDOW (dialog)); gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void
activate_save_as (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window),
GTK_FILE_CHOOSER_ACTION_SAVE,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Save", GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_widget_show (dialog);
g_signal_connect (dialog, "response",
G_CALLBACK (on_save_response),
NULL);
}
static void static void
activate_save (GSimpleAction *action, activate_save (GSimpleAction *action,
GVariant *parameter, GVariant *parameter,
@ -534,22 +543,9 @@ activate_save (GSimpleAction *action,
} }
static void static void
activate_open (GSimpleAction *action, on_open_response (GtkWidget *dialog,
GVariant *parameter, int response)
gpointer user_data)
{ {
GtkWidget *dialog;
gint response;
dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window),
GTK_FILE_CHOOSER_ACTION_OPEN,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Open", GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK) if (response == GTK_RESPONSE_OK)
{ {
GFile *open_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); GFile *open_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
@ -560,6 +556,28 @@ activate_open (GSimpleAction *action,
gtk_window_destroy (GTK_WINDOW (dialog)); gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void
activate_open (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window),
GTK_FILE_CHOOSER_ACTION_OPEN,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Open", GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_widget_show (dialog);
g_signal_connect (dialog, "response",
G_CALLBACK (on_open_response),
NULL);
}
static void static void
activate_new (GSimpleAction *action, activate_new (GSimpleAction *action,
GVariant *parameter, GVariant *parameter,

View File

@ -125,12 +125,32 @@ display_dialog (void)
gtk_widget_show (dialog); gtk_widget_show (dialog);
} }
static void
on_open_response (GtkWidget *file_chooser,
int response)
{
if (response == GTK_RESPONSE_ACCEPT)
{
char *path;
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser));
path = g_file_get_path (file);
gtk_button_set_label (GTK_BUTTON (file_l), path);
g_free (path);
}
gtk_window_destroy (GTK_WINDOW (file_chooser));
gtk_widget_set_sensitive (open, TRUE);
}
static void static void
button_clicked (GtkButton *b, button_clicked (GtkButton *b,
gpointer user_data) gpointer user_data)
{ {
GtkWidget *w; GtkWidget *w;
gchar *path;
w = gtk_file_chooser_dialog_new ("Select file", w = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (toplevel), GTK_WINDOW (toplevel),
@ -139,16 +159,13 @@ button_clicked (GtkButton *b,
"_Open", GTK_RESPONSE_ACCEPT, "_Open", GTK_RESPONSE_ACCEPT,
NULL); NULL);
gtk_dialog_run (GTK_DIALOG (w)); gtk_window_set_modal (GTK_WINDOW (w), TRUE);
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (w));
path = g_file_get_path (file);
gtk_button_set_label (GTK_BUTTON (file_l), path);
gtk_window_destroy (GTK_WINDOW (w)); g_signal_connect (w, "response",
G_CALLBACK (on_open_response),
NULL);
gtk_widget_set_sensitive (open, TRUE); gtk_window_present (GTK_WINDOW (w));
g_free (path);
} }
static void static void
@ -180,7 +197,7 @@ main (int argc, char **argv)
w1, 0, 0, 1, 1); w1, 0, 0, 1, 1);
file_l = gtk_button_new (); file_l = gtk_button_new ();
path = g_build_filename (g_get_current_dir (), "apple-red.png", NULL); path = g_build_filename (GTK_SRCDIR, "apple-red.png", NULL);
file = g_file_new_for_path (path); file = g_file_new_for_path (path);
gtk_button_set_label (GTK_BUTTON (file_l), path); gtk_button_set_label (GTK_BUTTON (file_l), path);
g_free (path); g_free (path);

View File

@ -191,7 +191,7 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
GtkWidget *content_area; GtkWidget *content_area;
GtkWidget *dialog; GtkWidget *window;
GtkWidget *scrolled_window; GtkWidget *scrolled_window;
GtkWidget *hbox; GtkWidget *hbox;
GtkWidget *button_vbox; GtkWidget *button_vbox;
@ -206,13 +206,12 @@ main (int argc, char **argv)
model = gtk_list_store_new (1, G_TYPE_STRING); model = gtk_list_store_new (1, G_TYPE_STRING);
contents = g_array_new (FALSE, FALSE, sizeof (char)); contents = g_array_new (FALSE, FALSE, sizeof (char));
dialog = gtk_dialog_new_with_buttons ("GtkComboBox model changes",
NULL, 0,
"_Close", GTK_RESPONSE_CLOSE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); window = gtk_window_new ();
gtk_window_set_title (GTK_WINDOW (window), "ComboBox Change");
content_area = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_window_set_child (GTK_WINDOW (window), content_area);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12); hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
gtk_box_append (GTK_BOX (content_area), hbox); gtk_box_append (GTK_BOX (content_area), hbox);
@ -243,7 +242,7 @@ main (int argc, char **argv)
button_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8); button_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
gtk_box_append (GTK_BOX (hbox), button_vbox); gtk_box_append (GTK_BOX (hbox), button_vbox);
gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300); gtk_window_set_default_size (GTK_WINDOW (window), 500, 300);
button = gtk_button_new_with_label ("Insert"); button = gtk_button_new_with_label ("Insert");
gtk_box_append (GTK_BOX (button_vbox), button); gtk_box_append (GTK_BOX (button_vbox), button);
@ -261,8 +260,24 @@ main (int argc, char **argv)
gtk_box_append (GTK_BOX (button_vbox), button); gtk_box_append (GTK_BOX (button_vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (on_animate), NULL); g_signal_connect (button, "clicked", G_CALLBACK (on_animate), NULL);
gtk_widget_show (dialog); GtkWidget *close_button = gtk_button_new_with_mnemonic ("_Close");
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_set_hexpand (close_button, TRUE);
gtk_box_append (GTK_BOX (content_area), close_button);
gtk_widget_show (window);
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_signal_connect_swapped (close_button, "clicked",
G_CALLBACK (gtk_window_destroy),
window);
g_signal_connect_swapped (window, "destroy",
G_CALLBACK (g_main_loop_quit),
loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
return 0; return 0;
} }

View File

@ -16,8 +16,8 @@ show_message_dialog1 (GtkWindow *parent)
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"Unhandled error message: SSH program unexpectedly exited"); "Unhandled error message: SSH program unexpectedly exited");
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_window_present (GTK_WINDOW (dialog));
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
} }
static void static void
@ -33,8 +33,8 @@ show_message_dialog1a (GtkWindow *parent)
GTK_BUTTONS_OK, GTK_BUTTONS_OK,
"The system network services are not compatible with this version.")); "The system network services are not compatible with this version."));
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_window_present (GTK_WINDOW (dialog));
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
} }
static void static void
@ -56,8 +56,8 @@ show_message_dialog2 (GtkWindow *parent)
"Empty Wastebasket", GTK_RESPONSE_OK, "Empty Wastebasket", GTK_RESPONSE_OK,
NULL); NULL);
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_window_present (GTK_WINDOW (dialog));
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
} }
static void static void
@ -66,9 +66,8 @@ show_color_chooser (GtkWindow *parent)
GtkWidget *dialog; GtkWidget *dialog;
dialog = gtk_color_chooser_dialog_new ("Builtin", parent); dialog = gtk_color_chooser_dialog_new ("Builtin", parent);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -81,8 +80,8 @@ show_color_chooser_generic (GtkWindow *parent)
"transient-for", parent, "transient-for", parent,
NULL); NULL);
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_window_present (GTK_WINDOW (dialog));
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
} }
static void static void
@ -119,9 +118,8 @@ show_dialog (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -135,9 +133,8 @@ show_dialog_with_header (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -152,9 +149,8 @@ show_dialog_with_buttons (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -169,9 +165,8 @@ show_dialog_with_header_buttons (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -183,9 +178,8 @@ show_dialog_with_header_buttons2 (GtkWindow *parent)
builder = gtk_builder_new_from_file ("dialog.ui"); builder = gtk_builder_new_from_file ("dialog.ui");
dialog = (GtkWidget *)gtk_builder_get_object (builder, "dialog"); dialog = (GtkWidget *)gtk_builder_get_object (builder, "dialog");
g_object_unref (builder); g_object_unref (builder);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
typedef struct { typedef struct {
@ -231,9 +225,8 @@ show_dialog_from_template (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
static void static void
@ -252,9 +245,8 @@ show_dialog_flex_template (GtkWindow *parent)
NULL); NULL);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
typedef struct { typedef struct {
@ -305,9 +297,8 @@ show_dialog_from_template_with_header (GtkWindow *parent)
add_buttons (dialog); add_buttons (dialog);
add_content (dialog); add_content (dialog);
gtk_window_present (GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog)); g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
gtk_window_destroy (GTK_WINDOW (dialog));
} }
int int

View File

@ -156,8 +156,10 @@ set_current_folder (GtkFileChooser *chooser,
GTK_BUTTONS_CLOSE, GTK_BUTTONS_CLOSE,
"Could not set the folder to %s", "Could not set the folder to %s",
name); name);
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_show (dialog);
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response",
G_CALLBACK (gtk_window_destroy),
NULL);
} }
g_object_unref (file); g_object_unref (file);
} }
@ -191,8 +193,10 @@ set_filename (GtkFileChooser *chooser,
GTK_BUTTONS_CLOSE, GTK_BUTTONS_CLOSE,
"Could not select %s", "Could not select %s",
name); name);
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_show (dialog);
gtk_window_destroy (GTK_WINDOW (dialog)); g_signal_connect (dialog, "response",
G_CALLBACK (gtk_window_destroy),
NULL);
} }
g_object_unref (file); g_object_unref (file);
} }

View File

@ -65,10 +65,10 @@ show_dialog (void)
gtk_widget_realize (dialog); gtk_widget_realize (dialog);
g_signal_connect (gtk_native_get_surface (GTK_NATIVE (dialog)), "size-changed", g_signal_connect (gtk_native_get_surface (GTK_NATIVE (dialog)), "size-changed",
G_CALLBACK (size_changed_cb), label); G_CALLBACK (size_changed_cb), label);
g_signal_connect (dialog, "response",
gtk_dialog_run (GTK_DIALOG (dialog)); G_CALLBACK (gtk_window_destroy),
NULL);
gtk_window_destroy (GTK_WINDOW (dialog)); gtk_widget_show (dialog);
} }
static void static void
@ -127,6 +127,16 @@ create_window (void)
gtk_grid_attach (GTK_GRID (grid), button, 2, 4, 1, 1); gtk_grid_attach (GTK_GRID (grid), button, 2, 4, 1, 1);
gtk_widget_show (window); gtk_widget_show (window);
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_signal_connect_swapped (window, "destroy",
G_CALLBACK (g_main_loop_quit),
loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
} }
int int
@ -136,8 +146,5 @@ main (int argc, char *argv[])
create_window (); create_window ();
while (TRUE)
g_main_context_iteration (NULL, TRUE);
return 0; return 0;
} }