From 463d6acc065c5bab496bf328841cf535f31c2a52 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 24 Nov 2024 15:26:52 +0000 Subject: [PATCH] Ensure application quits between tests, provide default dir --- src/Application.vala | 47 +++++++++++---- src/Widgets/TerminalWidget.vala | 11 +++- src/tests/Application.vala | 101 +++++++++++++++++++------------- 3 files changed, 103 insertions(+), 56 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index d1f979cad4..e4ef998eee 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -23,6 +23,8 @@ public class Terminal.Application : Gtk.Application { application_id: "io.elementary.terminal", /* Ensures only one instance runs */ flags: ApplicationFlags.HANDLES_COMMAND_LINE | ApplicationFlags.CAN_OVERRIDE_APP_ID ); + +stdout.printf ("# Application new \n"); } construct { @@ -66,9 +68,16 @@ public class Terminal.Application : Gtk.Application { add_main_option ( "commandline", 'x', 0, OptionArg.FILENAME, _("Run remainder of line as a command in terminal"), "COMMAND" ); +stdout.printf ("# End of application construct \n"); + } + + ~Application () { +stdout.printf ("# App destruct \n"); + } protected override bool local_command_line (ref unowned string[] args, out int exit_status) { +stdout.printf ("# app local commandline \n"); bool show_help = false; for (uint i = 1; args[i] != null; i++) { @@ -123,10 +132,12 @@ public class Terminal.Application : Gtk.Application { } protected override int handle_local_options (VariantDict options) { +stdout.printf ("# App handle local options \n"); unowned string working_directory; unowned string[] args; if (options.lookup ("working-directory", "^&ay", out working_directory)) { +stdout.printf ("# options lookup working directory \n"); if (working_directory != "\0") { Environment.set_current_dir ( Utils.sanitize_path (working_directory, Environment.get_current_dir (), false) @@ -138,6 +149,7 @@ public class Terminal.Application : Gtk.Application { } if (options.lookup (OPTION_REMAINING, "^a&ay", out args)) { +stdout.printf ("# options lookup OPTION_REMAINING \n"); if (commandline != "\0") { commandline += " %s".printf (string.joinv (" ", args)); } else { @@ -149,10 +161,12 @@ public class Terminal.Application : Gtk.Application { options.insert ("commandline", "^&ay", commandline.escape ()); } +stdout.printf ("# return -1 \n"); return -1; } protected override bool dbus_register (DBusConnection connection, string object_path) throws Error { +stdout.printf ("# App dbus register \n"); base.dbus_register (connection, object_path); var dbus = new DBus (); @@ -200,6 +214,7 @@ public class Terminal.Application : Gtk.Application { } protected override void startup () { +stdout.printf ("# App startup \n"); base.startup (); Granite.init (); Adw.init (); @@ -209,18 +224,22 @@ public class Terminal.Application : Gtk.Application { settings_sys = new GLib.Settings ("org.gnome.desktop.interface"); themes = new Themes (); - var provider = new Gtk.CssProvider (); - provider.load_from_resource ("/io/elementary/terminal/Application.css"); - - /* Vte.Terminal itself registers its default styling with the APPLICATION priority: - * https://gitlab.gnome.org/GNOME/vte/blob/0.68.0/src/vtegtk.cc#L844-847 - * To be able to overwrite their styles, we need to use +1. - */ - Gtk.StyleContext.add_provider_for_display ( - Gdk.Display.get_default (), - provider, - Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + 1 - ); + if (!is_testing) { + var provider = new Gtk.CssProvider (); + provider.load_from_resource ("/io/elementary/terminal/Application.css"); + + + /* Vte.Terminal itself registers its default styling with the APPLICATION priority: + * https://gitlab.gnome.org/GNOME/vte/blob/0.68.0/src/vtegtk.cc#L844-847 + * To be able to overwrite their styles, we need to use +1. + */ + Gtk.StyleContext.add_provider_for_display ( + Gdk.Display.get_default (), + provider, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + 1 + ); + } + var new_window_action = new SimpleAction ("new-window", null); new_window_action.activate.connect (() => { @@ -238,7 +257,7 @@ public class Terminal.Application : Gtk.Application { } protected override int command_line (ApplicationCommandLine command_line) { -stdout.printf ("#Entering app commandline \n"); +stdout.printf ("# App commandline \n"); unowned var options = command_line.get_options_dict (); var window = (MainWindow) active_window; var is_first_window = window == null; @@ -303,6 +322,7 @@ stdout.printf ("#presenting window\n"); } protected override void dbus_unregister (DBusConnection connection, string path) { +stdout.printf ("# App dbus unregister \n"); if (dbus_id != 0) { connection.unregister_object (dbus_id); } @@ -311,6 +331,7 @@ stdout.printf ("#presenting window\n"); } public void close () { +stdout.printf ("# App close \n"); foreach (var window in get_windows ()) { window.close (); // if all windows is closed, the main loop will stop automatically. } diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index d4584a0ca5..8775021d6d 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -699,8 +699,17 @@ namespace Terminal { string shell = Application.settings.get_string ("shell"); string?[] envv = null; - if (shell == "") + if (shell == "") { shell = Vte.get_user_shell (); + if (shell == "") { + return; + } + } + + if (dir == "") { + debug ("Using fallback directory"); + dir = "/"; + } envv = { // Export ID so we can identify the terminal for which the process completion is reported diff --git a/src/tests/Application.vala b/src/tests/Application.vala index 83a2b33b45..0cb884fa56 100644 --- a/src/tests/Application.vala +++ b/src/tests/Application.vala @@ -11,6 +11,10 @@ namespace Terminal.Test.Application { delegate void ActivateCallback (); private void setup () { + if (application != null) { + application.quit (); + application = null; + } application = new Terminal.Application () { application_id = "io.elementary.terminal.tests.application", is_testing = true @@ -19,6 +23,9 @@ namespace Terminal.Test.Application { application.shutdown.connect (() => { application.close (); }); + +stdout.printf ("# End of setup\n"); +assert (application is GLib.Application); } private void iterate_context () { @@ -37,26 +44,30 @@ namespace Terminal.Test.Application { private void cli (string[] args, LocalOptionsCallback callback) { setup (); - +stdout.printf ("# cli after setup \n"); application.handle_local_options.connect_after ((dict) => { callback (dict); application.quit (); return 0; }); - +stdout.printf ("# Run application \n"); if (application.run (args) != 0) { GLib.Test.fail (); } +stdout.printf ("# End of cli \n"); } private void option (string? options, string platform_data, CommandLineCallback callback) { ulong oneshot = 0; setup (); -stdout.printf ("#Enter option after setup\n"); +stdout.printf ("# Enter option after setup\n"); + assert (application != null); + assert (application is GLib.Application); +stdout.printf ("# Setup oneshot \n"); oneshot = application.command_line.connect ((nil) => { -stdout.printf ("#oneshot callback\n"); +stdout.printf ("# oneshot callback\n"); application.disconnect (oneshot); -stdout.printf ("#calling commandline with nil\n"); +stdout.printf ("# calling commandline with nil\n"); // Opens primary instance (no options) application.command_line (nil); ApplicationCommandLine? cmdline = null; @@ -80,9 +91,12 @@ stdout.printf ("# quit app\n"); return 0; }); +stdout.printf ("# Run application \n"); if (application.run (null) != 0) { +stdout.printf ("# Test fail \n"); GLib.Test.fail (); } +stdout.printf ("# End of option \n"); } private void action (string name, Variant? @value, ActivateCallback callback) { @@ -118,17 +132,19 @@ stdout.printf ("# quit app\n"); // local command line: any instance from terminal // GLib.Test.add_func ("/application/cli/commandline", () => { - // cli ({ "io.elementary.terminal", "--commandline=true" }, (dict) => { - // assert_true ("commandline" in dict); - // unowned var commandline = dict.lookup_value ("commandline", null).get_bytestring (); - // assert_cmpstr (commandline, CompareOperator.EQ, "true"); - // }); +// stdout.printf ("# Starting cli/commandline\n"); +// cli ({ "io.elementary.terminal", "--commandline=true" }, (dict) => { +// stdout.printf ("# Callback cli 1\n"); +// assert_true ("commandline" in dict); +// unowned var commandline = dict.lookup_value ("commandline", null).get_bytestring (); +// assert_cmpstr (commandline, CompareOperator.EQ, "true"); +// }); - // cli ({ "io.elementary.terminal", "-x", "echo", "-e", "true\tfalse" }, (dict) => { - // assert_true ("commandline" in dict); - // unowned var commandline = dict.lookup_value ("commandline", null).get_bytestring (); - // assert_cmpstr (commandline, CompareOperator.EQ, "echo -e true\\tfalse"); - // }); + // cli ({ "io.elementary.terminal", "-x", "echo", "-e", "true\tfalse" }, (dict) => { + // assert_true ("commandline" in dict); + // unowned var commandline = dict.lookup_value ("commandline", null).get_bytestring (); + // assert_cmpstr (commandline, CompareOperator.EQ, "echo -e true\\tfalse"); + // }); // cli ({ "io.elementary.terminal", "--commandline", "echo", "true" }, (dict) => { // assert_true ("commandline" in dict); @@ -152,48 +168,49 @@ stdout.printf ("# quit app\n"); // primary command line: first instance from terminal. any instance from dbus. GLib.Test.add_func ("/application/command-line/new-tab", () => { +stdout.printf ("# Starting command-line/new-tab\n"); int default_tabs = 0; option (null, "@a{sv} {}", () => { -stdout.printf ("#null option callback\n"); +stdout.printf ("# null option callback\n"); unowned var window = (MainWindow) application.active_window; assert_nonnull (window); default_tabs = (int) window.notebook.n_pages; }); -// option ("{'new-tab':}", "@a{sv} {}", () => { -// stdout.printf ("new tab true callback\n"); -// unowned var window = (MainWindow) application.active_window; -// assert_nonnull (window); -// var n_tabs = (int) window.notebook.n_pages; -// assert_cmpint (n_tabs - default_tabs, CompareOperator.EQ, 1); -// }); + option ("{'new-tab':}", "@a{sv} {}", () => { +stdout.printf ("# new tab true callback\n"); + unowned var window = (MainWindow) application.active_window; + assert_nonnull (window); + var n_tabs = (int) window.notebook.n_pages; + assert_cmpint (n_tabs - default_tabs, CompareOperator.EQ, 1); + }); -// option ("{'new-tab':}", "@a{sv} {}", () => { -// unowned var window = (MainWindow) application.active_window; -// assert_nonnull (window); -// var n_tabs = (int) window.notebook.n_pages; -// assert_cmpint (n_tabs, CompareOperator.EQ, default_tabs); -// }); + option ("{'new-tab':}", "@a{sv} {}", () => { + unowned var window = (MainWindow) application.active_window; + assert_nonnull (window); + var n_tabs = (int) window.notebook.n_pages; + assert_cmpint (n_tabs, CompareOperator.EQ, default_tabs); + }); }); -// GLib.Test.add_func ("/application/command-line/new-window", () => { -// option ("{'new-window':}", "@a{sv} {}", () => { -// stdout.printf ("in callback\n"); -// var n_windows = (int) application.get_windows ().length (); -// stdout.printf ("got n windows %i\n", n_windows); -// assert_cmpint (n_windows, CompareOperator.EQ, 2); -// }); + GLib.Test.add_func ("/application/command-line/new-window", () => { + option ("{'new-window':}", "@a{sv} {}", () => { +stdout.printf ("# in callback\n"); + var n_windows = (int) application.get_windows ().length (); +stdout.printf ("# got n windows %i\n", n_windows); + assert_cmpint (n_windows, CompareOperator.EQ, 2); + }); -// option ("{'new-window':}", "@a{sv} {}", () => { -// var n_windows = (int) application.get_windows ().length (); -// assert_cmpint (n_windows, CompareOperator.EQ, 1); -// }); -// }); + option ("{'new-window':}", "@a{sv} {}", () => { + var n_windows = (int) application.get_windows ().length (); + assert_cmpint (n_windows, CompareOperator.EQ, 1); + }); + }); GLib.Test.add_func ("/application/command-line/execute", () => { int default_tabs = 0; option (null, "@a{sv} {}", () => { -stdout.printf ("#null option callback\n"); +stdout.printf ("# null option callback\n"); unowned var window = (MainWindow) application.active_window; assert_nonnull (window); default_tabs = (int) window.notebook.n_pages;