Add tests for GApplication::handle-local-options

This commit is contained in:
Matthias Clasen 2015-07-26 21:39:53 -04:00
parent 243d740c04
commit c885d42751

View File

@ -808,6 +808,135 @@ test_busy (void)
session_bus_down ();
}
/*
* Test that handle-local-options works as expected
*/
static gint
test_local_options (GApplication *app,
GVariantDict *options,
gpointer data)
{
gboolean *called = data;
*called = TRUE;
if (g_variant_dict_contains (options, "success"))
return 0;
else if (g_variant_dict_contains (options, "failure"))
return 1;
else
return -1;
}
static gint
second_handler (GApplication *app,
GVariantDict *options,
gpointer data)
{
gboolean *called = data;
*called = TRUE;
return 2;
}
static void
test_handle_local_options_success (void)
{
if (g_test_subprocess ())
{
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
gchar *argv[] = { binpath, "--success", NULL };
GApplication *app;
gboolean called = FALSE;
gboolean called2 = FALSE;
int status;
app = g_application_new ("org.gtk.TestApplication", 0);
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
g_assert (called);
g_assert (!called2);
g_assert_cmpint (status, ==, 0);
g_object_unref (app);
g_free (binpath);
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
g_test_trap_assert_passed ();
}
static void
test_handle_local_options_failure (void)
{
if (g_test_subprocess ())
{
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
gchar *argv[] = { binpath, "--failure", NULL };
GApplication *app;
gboolean called = FALSE;
gboolean called2 = FALSE;
int status;
app = g_application_new ("org.gtk.TestApplication", 0);
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
g_assert (called);
g_assert (!called2);
g_assert_cmpint (status, ==, 1);
g_object_unref (app);
g_free (binpath);
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
g_test_trap_assert_passed ();
}
static void
test_handle_local_options_passthrough (void)
{
if (g_test_subprocess ())
{
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
gchar *argv[] = { binpath, NULL };
GApplication *app;
gboolean called = FALSE;
gboolean called2 = FALSE;
int status;
app = g_application_new ("org.gtk.TestApplication", 0);
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
g_assert (called);
g_assert (called2);
g_assert_cmpint (status, ==, 2);
g_object_unref (app);
g_free (binpath);
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
g_test_trap_assert_passed ();
}
int
main (int argc, char **argv)
{
@ -829,6 +958,9 @@ main (int argc, char **argv)
g_test_add_func ("/gapplication/resource-path", test_resource_path);
g_test_add_func ("/gapplication/test-help", test_help);
g_test_add_func ("/gapplication/test-busy", test_busy);
g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success);
g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure);
g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough);
return g_test_run ();
}