[gtksourceview/wip/chergert/gsv-gtk4: 144/259] tests: port tests to new GTK API




commit 5be43f7a30345d7382f24ca06890f14c3041522d
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 17 13:06:02 2020 -0700

    tests: port tests to new GTK API
    
    We lost access to various things like gtk_main_* and the default icon
    theme.

 tests/test-completion.c          | 16 ++++++++------
 tests/test-search-performances.c |  7 ++++--
 tests/test-search.c              | 16 +++++++++-----
 tests/test-space-drawing.c       | 13 +++++++----
 tests/test-widget.c              | 48 ++++++++++++++++++++++++----------------
 tests/test-widget.ui             |  5 ++++-
 6 files changed, 66 insertions(+), 39 deletions(-)
---
diff --git a/tests/test-completion.c b/tests/test-completion.c
index 73c821c6..216e8cf0 100644
--- a/tests/test-completion.c
+++ b/tests/test-completion.c
@@ -27,6 +27,7 @@ typedef struct _TestProviderClass TestProviderClass;
 static GtkSourceCompletionWords *word_provider;
 static TestProvider *fixed_provider;
 static TestProvider *random_provider;
+static GMainLoop *main_loop;
 
 struct _TestProvider
 {
@@ -170,9 +171,8 @@ test_provider_init (TestProvider *self)
        GIcon *icon;
        GIcon *emblem_icon;
        GEmblem *emblem;
-       gint scale;
 
-       theme = gtk_icon_theme_get_default ();
+       theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
 
        /* Just use some defaults for icons here. Normally we would create these with
         * the widget to get proper direction, scale, etc.
@@ -405,10 +405,10 @@ create_window (void)
 
        completion = gtk_source_view_get_completion (source_view);
 
-       g_signal_connect (window,
-                         "destroy",
-                         G_CALLBACK (gtk_main_quit),
-                         NULL);
+       g_signal_connect_swapped (window,
+                                 "destroy",
+                                 G_CALLBACK (g_main_loop_quit),
+                                 main_loop);
 
        g_object_bind_property (completion, "remember-info-visibility",
                                remember_info_visibility, "active",
@@ -460,11 +460,13 @@ int
 main (int   argc,
       char *argv[])
 {
+       main_loop = g_main_loop_new (NULL, FALSE);
+
        gtk_init ();
 
        create_window ();
 
-       gtk_main ();
+       g_main_loop_run (main_loop);
 
        /* Not really useful, except for debugging memory leaks. */
        g_object_unref (word_provider);
diff --git a/tests/test-search-performances.c b/tests/test-search-performances.c
index f2a08b36..7052b2ea 100644
--- a/tests/test-search-performances.c
+++ b/tests/test-search-performances.c
@@ -20,6 +20,8 @@
 #include <gtk/gtk.h>
 #include <gtksourceview/gtksource.h>
 
+static GMainLoop *main_loop;
+
 /* This measures the execution times for:
  * - basic search: with gtk_text_iter_forward_search();
  * - "smart" search: the first search with gtk_text_iter_forward_search(), later
@@ -42,7 +44,7 @@ on_notify_search_occurrences_count_cb (GtkSourceSearchContext *search_context,
        g_print ("smart asynchronous search, case sensitive: %lf seconds.\n",
                 g_timer_elapsed (timer, NULL));
 
-       gtk_main_quit ();
+       g_main_loop_quit (main_loop);
 }
 
 int
@@ -279,6 +281,7 @@ main (int argc, char *argv[])
        gtk_source_search_settings_set_regex_enabled (search_settings, FALSE);
        gtk_source_search_settings_set_search_text (search_settings, "foo");
 
-       gtk_main ();
+       main_loop = g_main_loop_new (NULL, FALSE);
+       g_main_loop_run (main_loop);
        return 0;
 }
diff --git a/tests/test-search.c b/tests/test-search.c
index ce352dc2..4083c85d 100644
--- a/tests/test-search.c
+++ b/tests/test-search.c
@@ -59,6 +59,8 @@ GType test_search_get_type (void);
 
 G_DEFINE_TYPE_WITH_PRIVATE (TestSearch, test_search, GTK_TYPE_GRID)
 
+static GMainLoop *main_loop;
+
 static void
 open_file (TestSearch  *search,
           const gchar *filename)
@@ -461,23 +463,25 @@ main (gint argc, gchar *argv[])
        GtkWidget *window;
        TestSearch *search;
 
+       main_loop = g_main_loop_new (NULL, FALSE);
+
        gtk_init ();
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtk_window_new ();
 
        gtk_window_set_default_size (GTK_WINDOW (window), 700, 500);
 
-       g_signal_connect (window,
-                         "destroy",
-                         G_CALLBACK (gtk_main_quit),
-                         NULL);
+       g_signal_connect_swapped (window,
+                                 "destroy",
+                                 G_CALLBACK (g_main_loop_quit),
+                                 main_loop);
 
        search = test_search_new ();
        gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (search));
 
        gtk_widget_show (window);
 
-       gtk_main ();
+       g_main_loop_run (main_loop);
 
        return 0;
 }
diff --git a/tests/test-space-drawing.c b/tests/test-space-drawing.c
index 9ff2f46a..f872267a 100644
--- a/tests/test-space-drawing.c
+++ b/tests/test-space-drawing.c
@@ -21,6 +21,8 @@
 
 #include <gtksourceview/gtksource.h>
 
+static GMainLoop *main_loop;
+
 static void
 fill_buffer (GtkTextBuffer *buffer,
             GtkTextTag    *tag)
@@ -65,9 +67,9 @@ create_window (void)
        GtkTextTag *tag;
        GtkSourceSpaceDrawer *space_drawer;
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtk_window_new ();
        gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
-       g_signal_connect (window, "destroy", gtk_main_quit, NULL);
+       g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_main_loop_quit), main_loop);
 
        hgrid = gtk_grid_new ();
        gtk_orientable_set_orientation (GTK_ORIENTABLE (hgrid), GTK_ORIENTATION_HORIZONTAL);
@@ -75,7 +77,8 @@ create_window (void)
        view = GTK_SOURCE_VIEW (gtk_source_view_new ());
 
        g_object_set (view,
-                     "expand", TRUE,
+                     "hexpand", TRUE,
+                     "vexpand", TRUE,
                      NULL);
 
        gtk_text_view_set_monospace (GTK_TEXT_VIEW (view), TRUE);
@@ -147,11 +150,13 @@ gint
 main (gint    argc,
       gchar **argv)
 {
+       main_loop = g_main_loop_new (NULL, FALSE);
+
        gtk_init ();
 
        create_window ();
 
-       gtk_main ();
+       g_main_loop_run (main_loop);
 
        return 0;
 }
diff --git a/tests/test-widget.c b/tests/test-widget.c
index 45e99b7a..5ab4556a 100644
--- a/tests/test-widget.c
+++ b/tests/test-widget.c
@@ -70,6 +70,8 @@ G_DEFINE_TYPE_WITH_PRIVATE (TestWidget, test_widget, GTK_TYPE_GRID)
 #define MARK_TYPE_1      "one"
 #define MARK_TYPE_2      "two"
 
+static GMainLoop *main_loop;
+
 static void
 remove_all_marks (GtkSourceBuffer *buffer)
 {
@@ -257,18 +259,14 @@ end:
 }
 
 static void
-open_file (TestWidget  *self,
-          const gchar *filename)
+open_file (TestWidget *self,
+           GFile      *file)
 {
-       GFile *location;
        GtkSourceFileLoader *loader;
 
        g_clear_object (&self->priv->file);
        self->priv->file = gtk_source_file_new ();
-
-       location = g_file_new_for_path (filename);
-       gtk_source_file_set_location (self->priv->file, location);
-       g_object_unref (location);
+       gtk_source_file_set_location (self->priv->file, file);
 
        loader = gtk_source_file_loader_new (self->priv->buffer,
                                             self->priv->file);
@@ -488,25 +486,32 @@ open_button_clicked_cb (TestWidget *self)
 
        if (last_dir != NULL && g_path_is_absolute (last_dir))
        {
-               gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
-                                                    last_dir);
+               GFile *folder;
+
+               folder = g_file_new_for_path (last_dir);
+               gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), folder, NULL);
+               g_object_unref (folder);
        }
 
        response = gtk_dialog_run (GTK_DIALOG (chooser));
 
        if (response == GTK_RESPONSE_OK)
        {
-               gchar *filename;
+               GFile *folder;
+               GFile *file;
 
-               filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
+               file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
+               folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (chooser));
 
-               if (filename != NULL)
+               if (file != NULL && folder != NULL)
                {
                        g_free (last_dir);
-                       last_dir = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (chooser));
-                       open_file (self, filename);
-                       g_free (filename);
+                       last_dir = g_file_get_path (folder);
+                       open_file (self, file);
                }
+
+               g_clear_object (&folder);
+               g_clear_object (&file);
        }
 
        gtk_widget_destroy (chooser);
@@ -1006,6 +1011,7 @@ static void
 test_widget_init (TestWidget *self)
 {
        GtkSourceSpaceDrawer *space_drawer;
+       GFile *file;
 
        self->priv = test_widget_get_instance_private (self);
 
@@ -1081,7 +1087,9 @@ test_widget_init (TestWidget *self)
                                space_drawer, "enable-matrix",
                                G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
-       open_file (self, TOP_SRCDIR "/gtksourceview/gtksourcebuffer.c");
+       file = g_file_new_for_path (TOP_SRCDIR "/gtksourceview/gtksourcebuffer.c");
+       open_file (self, file);
+       g_object_unref (file);
 }
 
 static TestWidget *
@@ -1096,20 +1104,22 @@ main (int argc, char *argv[])
        GtkWidget *window;
        TestWidget *test_widget;
 
+       main_loop = g_main_loop_new (NULL, FALSE);
+
        gtk_init ();
        gtk_source_init ();
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtk_window_new ();
        gtk_window_set_default_size (GTK_WINDOW (window), 900, 600);
 
-       g_signal_connect (window, "destroy", gtk_main_quit, NULL);
+       g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_main_loop_quit), main_loop);
 
        test_widget = test_widget_new ();
        gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (test_widget));
 
        gtk_widget_show (window);
 
-       gtk_main ();
+       g_main_loop_run (main_loop);
 
        gtk_source_finalize ();
 
diff --git a/tests/test-widget.ui b/tests/test-widget.ui
index 834e0c1d..0a67816d 100644
--- a/tests/test-widget.ui
+++ b/tests/test-widget.ui
@@ -23,7 +23,10 @@
     <property name="page_increment">10</property>
   </object>
   <template class="TestWidget" parent="GtkGrid">
-    <property name="margin">6</property>
+    <property name="margin-top">6</property>
+    <property name="margin-bottom">6</property>
+    <property name="margin-start">6</property>
+    <property name="margin-end">6</property>
     <property name="column_spacing">4</property>
     <child>
       <object class="GtkGrid" id="grid2">


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]