[gtksourceview/gtksourceview-4-8] tests: allow openin file with test-widget



commit b8b548c977f6236c0de1d8e230cfe495ecb32e18
Author: Christian Hergert <chergert redhat com>
Date:   Tue Dec 1 10:54:06 2020 -0800

    tests: allow openin file with test-widget
    
    This is already on the GTK 4 branch, and it is helpful here too when
    testing things for GTK 3.

 tests/test-widget.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
---
diff --git a/tests/test-widget.c b/tests/test-widget.c
index fcae46c9..d3eff04d 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 const char *cmd_filename;
+
 static void
 remove_all_marks (GtkSourceBuffer *buffer)
 {
@@ -257,18 +259,15 @@ end:
 }
 
 static void
-open_file (TestWidget  *self,
-          const gchar *filename)
+open_file (TestWidget *self,
+          GFile      *location)
 {
-       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);
 
        loader = gtk_source_file_loader_new (self->priv->buffer,
                                             self->priv->file);
@@ -496,16 +495,16 @@ open_button_clicked_cb (TestWidget *self)
 
        if (response == GTK_RESPONSE_OK)
        {
-               gchar *filename;
+               GFile *file;
 
-               filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
+               file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
 
-               if (filename != NULL)
+               if (file != NULL)
                {
                        g_free (last_dir);
                        last_dir = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (chooser));
-                       open_file (self, filename);
-                       g_free (filename);
+                       open_file (self, file);
+                       g_object_unref (file);
                }
        }
 
@@ -1001,6 +1000,7 @@ static void
 test_widget_init (TestWidget *self)
 {
        GtkSourceSpaceDrawer *space_drawer;
+       GFile *file;
 
        self->priv = test_widget_get_instance_private (self);
 
@@ -1081,7 +1081,13 @@ test_widget_init (TestWidget *self)
                                space_drawer, "enable-matrix",
                                G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
-       open_file (self, TOP_SRCDIR "/gtksourceview/gtksourcebuffer.c");
+       if (cmd_filename)
+               file = g_file_new_for_commandline_arg (cmd_filename);
+       else
+               file = g_file_new_for_path (TOP_SRCDIR "/gtksourceview/gtksourcebuffer.c");
+
+       open_file (self, file);
+       g_object_unref (file);
 }
 
 static TestWidget *
@@ -1099,6 +1105,11 @@ main (int argc, char *argv[])
        gtk_init (&argc, &argv);
        gtk_source_init ();
 
+       if (argc == 2 && g_file_test (argv[1], G_FILE_TEST_IS_REGULAR))
+       {
+               cmd_filename = argv[1];
+       }
+
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size (GTK_WINDOW (window), 900, 600);
 


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