Skip to content

Commit

Permalink
Ensure application quits between tests, provide default dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypw committed Nov 24, 2024
1 parent 0799677 commit 463d6ac
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 56 deletions.
47 changes: 34 additions & 13 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 ();
Expand Down Expand Up @@ -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 ();
Expand All @@ -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 (() => {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
}
Expand Down
11 changes: 10 additions & 1 deletion src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
101 changes: 59 additions & 42 deletions src/tests/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 () {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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':<true>}", "@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':<true>}", "@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':<false>}", "@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':<false>}", "@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':<true>}", "@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':<true>}", "@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':<false>}", "@a{sv} {}", () => {
// var n_windows = (int) application.get_windows ().length ();
// assert_cmpint (n_windows, CompareOperator.EQ, 1);
// });
// });
option ("{'new-window':<false>}", "@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;
Expand Down

0 comments on commit 463d6ac

Please sign in to comment.