mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 15:33:39 +02:00
Add tests for remote actions
This improves the GApplication test coverage.
This commit is contained in:
@@ -1,6 +1,70 @@
|
||||
#include <gio/gio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
new_activated (GSimpleAction *action,
|
||||
GVariant *parameter,
|
||||
gpointer user_data)
|
||||
{
|
||||
GApplication *app = user_data;
|
||||
|
||||
g_application_activate (app);
|
||||
}
|
||||
|
||||
static void
|
||||
quit_activated (GSimpleAction *action,
|
||||
GVariant *parameter,
|
||||
gpointer user_data)
|
||||
{
|
||||
GApplication *app = user_data;
|
||||
|
||||
g_application_quit (app);
|
||||
}
|
||||
|
||||
static void
|
||||
action1_activated (GSimpleAction *action,
|
||||
GVariant *parameter,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_print ("activate action1\n");
|
||||
}
|
||||
|
||||
static void
|
||||
action2_activated (GSimpleAction *action,
|
||||
GVariant *parameter,
|
||||
gpointer user_data)
|
||||
{
|
||||
GVariant *state;
|
||||
|
||||
state = g_action_get_state (G_ACTION (action));
|
||||
g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
|
||||
g_print ("activate action2 %d\n", !g_variant_get_boolean (state));
|
||||
g_variant_unref (state);
|
||||
}
|
||||
|
||||
static void
|
||||
change_action2 (GSimpleAction *action,
|
||||
GVariant *state,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_print ("change action2 %d\n", g_variant_get_boolean (state));
|
||||
}
|
||||
|
||||
static void
|
||||
startup (GApplication *app)
|
||||
{
|
||||
static GActionEntry actions[] = {
|
||||
{ "new", new_activated, NULL, NULL, NULL },
|
||||
{ "quit", quit_activated, NULL, NULL, NULL },
|
||||
{ "action1", action1_activated, NULL, NULL, NULL },
|
||||
{ "action2", action2_activated, "b", "false", change_action2 }
|
||||
};
|
||||
|
||||
g_action_map_add_action_entries (G_ACTION_MAP (app),
|
||||
actions, G_N_ELEMENTS (actions),
|
||||
app);
|
||||
}
|
||||
|
||||
static void
|
||||
activate (GApplication *application)
|
||||
{
|
||||
@@ -134,6 +198,44 @@ command_line (GApplication *application,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
action_cb (gpointer data)
|
||||
{
|
||||
gchar **argv = data;
|
||||
GApplication *app;
|
||||
gchar **actions;
|
||||
gint i;
|
||||
|
||||
if (g_strcmp0 (argv[1], "./actions") == 0)
|
||||
{
|
||||
app = g_application_get_default ();
|
||||
|
||||
if (g_strcmp0 (argv[2], "list") == 0)
|
||||
{
|
||||
g_print ("actions");
|
||||
actions = g_action_group_list_actions (G_ACTION_GROUP (app));
|
||||
for (i = 0; actions[i]; i++)
|
||||
g_print (" %s", actions[i]);
|
||||
g_print ("\n");
|
||||
g_strfreev (actions);
|
||||
}
|
||||
else if (g_strcmp0 (argv[2], "activate") == 0)
|
||||
{
|
||||
g_action_group_activate_action (G_ACTION_GROUP (app),
|
||||
"action1", NULL);
|
||||
}
|
||||
else if (g_strcmp0 (argv[2], "set-state") == 0)
|
||||
{
|
||||
g_action_group_change_action_state (G_ACTION_GROUP (app),
|
||||
"action2",
|
||||
g_variant_new_boolean (TRUE));
|
||||
}
|
||||
}
|
||||
g_application_release (app);
|
||||
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -145,6 +247,7 @@ main (int argc, char **argv)
|
||||
(g_strcmp0 (argv[1], "./cmd") == 0
|
||||
? G_APPLICATION_HANDLES_COMMAND_LINE
|
||||
: G_APPLICATION_HANDLES_OPEN));
|
||||
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
|
||||
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
||||
g_signal_connect (app, "open", G_CALLBACK (open), NULL);
|
||||
g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
|
||||
@@ -153,6 +256,14 @@ main (int argc, char **argv)
|
||||
#else
|
||||
g_application_set_inactivity_timeout (app, 1000);
|
||||
#endif
|
||||
|
||||
if (g_strcmp0 (argv[1], "./actions") == 0)
|
||||
{
|
||||
g_application_set_inactivity_timeout (app, 0);
|
||||
g_application_hold (app);
|
||||
g_idle_add (action_cb, argv);
|
||||
}
|
||||
|
||||
status = g_application_run (app, argc - 1, argv + 1);
|
||||
|
||||
g_object_unref (app);
|
||||
|
Reference in New Issue
Block a user