Add a test for GtkFileDialog
authorMatthias Clasen <mclasen@redhat.com>
Fri, 6 Jan 2023 04:03:27 +0000 (23:03 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 6 Jan 2023 04:13:58 +0000 (23:13 -0500)
tests/meson.build
tests/testfiledialog.c [new file with mode: 0644]

index 50f8e29a1bf83fabad7b5b03a6fd054c5486499d..10c645dade255c0cf3549dc565a5cae3407d0137 100644 (file)
@@ -117,7 +117,8 @@ gtk_tests = [
   ['testwindowdrag'],
   ['testinhibitshortcuts'],
   ['testzoom'],
-  ['testdatatable', ['frame-stats.c', 'variable.c']]
+  ['testdatatable', ['frame-stats.c', 'variable.c']],
+  ['testfiledialog'],
 ]
 
 if os_unix
diff --git a/tests/testfiledialog.c b/tests/testfiledialog.c
new file mode 100644 (file)
index 0000000..e859d67
--- /dev/null
@@ -0,0 +1,255 @@
+#include <gtk.h>
+
+gboolean done = FALSE;
+
+static void
+open_done (GObject *source,
+           GAsyncResult *result,
+           gpointer data)
+{
+  GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
+  GFile *file;
+  GError *error = NULL;
+
+  file = gtk_file_dialog_open_finish (dialog, result, &error);
+  if (!file)
+    {
+      g_print ("Error: %s %d %s\n", g_quark_to_string (error->domain), error->code, error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_print ("%s\n", g_file_peek_path (file));
+      g_object_unref (file);
+    }
+
+  done = TRUE;
+}
+
+static void
+select_done (GObject *source,
+             GAsyncResult *result,
+             gpointer data)
+{
+  GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
+  GFile *file;
+  GError *error = NULL;
+
+  file = gtk_file_dialog_select_folder_finish (dialog, result, &error);
+  if (!file)
+    {
+      g_print ("Error: %s %d %s\n", g_quark_to_string (error->domain), error->code, error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_print ("%s\n", g_file_peek_path (file));
+      g_object_unref (file);
+    }
+
+  done = TRUE;
+}
+
+static void
+save_done (GObject *source,
+           GAsyncResult *result,
+           gpointer data)
+{
+  GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
+  GFile *file;
+  GError *error = NULL;
+
+  file = gtk_file_dialog_save_finish (dialog, result, &error);
+  if (!file)
+    {
+      g_print ("Error: %s %d %s\n", g_quark_to_string (error->domain), error->code, error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_print ("%s\n", g_file_peek_path (file));
+      g_object_unref (file);
+    }
+
+  done = TRUE;
+}
+
+static void
+open_multiple_done (GObject *source,
+                    GAsyncResult *result,
+                    gpointer data)
+{
+  GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
+  GListModel *model;
+  GError *error = NULL;
+
+  model = gtk_file_dialog_open_multiple_finish (dialog, result, &error);
+  if (!model)
+    {
+      g_print ("Error: %s %d %s\n", g_quark_to_string (error->domain), error->code, error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      for (unsigned int i = 0; i < g_list_model_get_n_items (model); i++)
+        {
+          GFile *file = g_list_model_get_item (model, i);
+          g_print ("%s\n", g_file_peek_path (file));
+          g_object_unref (file);
+        }
+      g_object_unref (model);
+    }
+
+  done = TRUE;
+}
+
+static void
+select_multiple_done (GObject *source,
+                      GAsyncResult *result,
+                      gpointer data)
+{
+  GtkFileDialog *dialog = GTK_FILE_DIALOG (source);
+  GListModel *model;
+  GError *error = NULL;
+
+  model = gtk_file_dialog_select_multiple_folders_finish (dialog, result, &error);
+  if (!model)
+    {
+      g_print ("Error: %s %d %s\n", g_quark_to_string (error->domain), error->code, error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      for (unsigned int i = 0; i < g_list_model_get_n_items (model); i++)
+        {
+          GFile *file = g_list_model_get_item (model, i);
+          g_print ("%s\n", g_file_peek_path (file));
+          g_object_unref (file);
+        }
+      g_object_unref (model);
+    }
+
+  done = TRUE;
+}
+
+static int
+cancel_dialog (gpointer data)
+{
+  GCancellable *cancellable = data;
+
+  g_cancellable_cancel (cancellable);
+
+  return G_SOURCE_REMOVE;
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkFileDialog *dialog;
+  GCancellable *cancellable;
+  char *title = NULL;
+  gboolean modal = TRUE;
+  char **shortcut_folders = NULL;
+  char *initial_folder = NULL;
+  char *initial_name = NULL;
+  char *initial_file = NULL;
+  char *accept_label = NULL;
+  int timeout = -1;
+  GOptionEntry options[] = {
+    { "title", 0, 0, G_OPTION_ARG_STRING, &title, "Title", "TITLE" },
+    { "nonmodal", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &modal, "Non-modal", NULL },
+    { "shortcut-folders", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &shortcut_folders, "Shortcut folders", "FOLDER" },
+    { "initial-folder", 0, 0, G_OPTION_ARG_FILENAME, &initial_folder, "Initial folder", "FOLDER" },
+    { "initial-name", 0, 0, G_OPTION_ARG_STRING, &initial_name, "Initial name", "NAME" },
+    { "initial-file", 0, 0, G_OPTION_ARG_FILENAME, &initial_file, "Initial file", "FILE" },
+    { "accept-label", 0, 0, G_OPTION_ARG_STRING, &accept_label, "Accept label", "LABEL" },
+    { "timeout", 0, 0, G_OPTION_ARG_INT, &timeout, "Timeout", "SECONDS" },
+    { NULL }
+  };
+  char *action = NULL;
+  GOptionContext *context;
+  GError *error = NULL;
+
+  context = g_option_context_new ("ACTION");
+  g_option_context_add_main_entries (context, options, NULL);
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+      g_print ("Failed to parse args: %s\n", error->message);
+      exit (1);
+    }
+
+  action = argv[1];
+
+  gtk_init ();
+
+  dialog = gtk_file_dialog_new ();
+
+  if (title)
+    gtk_file_dialog_set_title (dialog, title);
+  gtk_file_dialog_set_modal (dialog, modal);
+  if (shortcut_folders)
+    {
+      GListStore *store;
+
+      store = g_list_store_new (G_TYPE_FILE);
+      for (int i = 0; shortcut_folders[i]; i++)
+        {
+          GFile *file = g_file_new_for_commandline_arg (shortcut_folders[i]);
+
+          g_list_store_append (store, file);
+          g_object_unref (file);
+        }
+      gtk_file_dialog_set_shortcut_folders (dialog, G_LIST_MODEL (store));
+      g_object_unref (store);
+    }
+  if (initial_folder)
+    {
+      GFile *file = g_file_new_for_commandline_arg (initial_folder);
+      gtk_file_dialog_set_initial_folder (dialog, file);
+      g_object_unref (file);
+    }
+  if (initial_name)
+    gtk_file_dialog_set_initial_name (dialog, initial_name);
+  if (initial_file)
+    {
+      GFile *file = g_file_new_for_commandline_arg (initial_file);
+      gtk_file_dialog_set_initial_file (dialog, file);
+      g_object_unref (file);
+    }
+  if (accept_label)
+    gtk_file_dialog_set_accept_label (dialog, accept_label);
+
+  cancellable = g_cancellable_new ();
+
+  if (timeout > 0)
+    g_timeout_add_seconds (timeout, cancel_dialog, cancellable);
+
+  if (action == NULL)
+    {
+      g_print ("no action\n");
+      exit (1);
+    }
+  else if (strcmp (action, "open") == 0)
+    gtk_file_dialog_open (dialog, NULL, cancellable, open_done, NULL);
+  else if (strcmp (action, "select_folder") == 0)
+    gtk_file_dialog_select_folder (dialog, NULL, cancellable, select_done, NULL);
+  else if (strcmp (action, "save") == 0)
+    gtk_file_dialog_save (dialog, NULL, cancellable, save_done, NULL);
+  else if (strcmp (action, "open_multiple") == 0)
+    gtk_file_dialog_open_multiple (dialog, NULL, cancellable, open_multiple_done, NULL);
+  else if (strcmp (action, "select_multiple_folders") == 0)
+    gtk_file_dialog_select_multiple_folders (dialog, NULL, cancellable, select_multiple_done, NULL);
+  else
+    {
+      g_print ("invalid action: %s\n", action);
+      g_print ("one of open, select_folder, save, open_multiple, select_multiple_folders\n");
+      exit (1);
+    }
+
+  while (!done)
+    g_main_context_iteration (NULL, TRUE);
+
+  g_object_unref (dialog);
+
+  return 0;
+}