| 1 |
#include <gio/gio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "gdbus-tests.h" |
| 7 |
#include "gdbus-sessionbus.h" |
| 8 |
|
| 9 |
#if 0 |
| 10 |
/* These tests are racy -- there is no guarantee about the order of data |
| 11 |
* arriving over D-Bus. |
| 12 |
* |
| 13 |
* They're also a bit ridiculous -- GApplication was never meant to be |
| 14 |
* abused in this way... |
| 15 |
* |
| 16 |
* We need new tests. |
| 17 |
*/ |
| 18 |
static gint outstanding_watches; |
| 19 |
static GMainLoop *main_loop; |
| 20 |
|
| 21 |
typedef struct |
| 22 |
{ |
| 23 |
gchar *expected_stdout; |
| 24 |
gint stdout_pipe; |
| 25 |
gchar *expected_stderr; |
| 26 |
gint stderr_pipe; |
| 27 |
} ChildData; |
| 28 |
|
| 29 |
static void |
| 30 |
check_data (gint fd, const gchar *expected) |
| 31 |
{ |
| 32 |
gssize len, actual; |
| 33 |
gchar *buffer; |
| 34 |
|
| 35 |
len = strlen (expected); |
| 36 |
buffer = g_alloca (len + 100); |
| 37 |
actual = read (fd, buffer, len + 100); |
| 38 |
|
| 39 |
g_assert_cmpint (actual, >=, 0); |
| 40 |
|
| 41 |
if (actual != len || |
| 42 |
memcmp (buffer, expected, len) != 0) |
| 43 |
{ |
| 44 |
buffer[MIN(len + 100, actual)] = '\0'; |
| 45 |
|
| 46 |
g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n", |
| 47 |
expected, |
| 48 |
(actual > len) ? "truncated" : "full", buffer); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
child_quit (GPid pid, |
| 54 |
gint status, |
| 55 |
gpointer data) |
| 56 |
{ |
| 57 |
ChildData *child = data; |
| 58 |
|
| 59 |
g_assert_cmpint (status, ==, 0); |
| 60 |
|
| 61 |
if (--outstanding_watches == 0) |
| 62 |
g_main_loop_quit (main_loop); |
| 63 |
|
| 64 |
check_data (child->stdout_pipe, child->expected_stdout); |
| 65 |
close (child->stdout_pipe); |
| 66 |
g_free (child->expected_stdout); |
| 67 |
|
| 68 |
if (child->expected_stderr) |
| 69 |
{ |
| 70 |
check_data (child->stderr_pipe, child->expected_stderr); |
| 71 |
close (child->stderr_pipe); |
| 72 |
g_free (child->expected_stderr); |
| 73 |
} |
| 74 |
|
| 75 |
g_slice_free (ChildData, child); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
spawn (const gchar *expected_stdout, |
| 80 |
const gchar *expected_stderr, |
| 81 |
const gchar *first_arg, |
| 82 |
...) |
| 83 |
{ |
| 84 |
GError *error = NULL; |
| 85 |
const gchar *arg; |
| 86 |
GPtrArray *array; |
| 87 |
ChildData *data; |
| 88 |
gchar **args; |
| 89 |
va_list ap; |
| 90 |
GPid pid; |
| 91 |
GPollFD fd; |
| 92 |
gchar **env; |
| 93 |
|
| 94 |
va_start (ap, first_arg); |
| 95 |
array = g_ptr_array_new (); |
| 96 |
g_ptr_array_add (array, g_test_build_filename (G_TEST_BUILT, "basic-application", NULL)); |
| 97 |
for (arg = first_arg; arg; arg = va_arg (ap, const gchar *)) |
| 98 |
g_ptr_array_add (array, g_strdup (arg)); |
| 99 |
g_ptr_array_add (array, NULL); |
| 100 |
args = (gchar **) g_ptr_array_free (array, FALSE); |
| 101 |
va_end (ap); |
| 102 |
|
| 103 |
env = g_environ_setenv (g_get_environ (), "TEST", "1", TRUE); |
| 104 |
|
| 105 |
data = g_slice_new (ChildData); |
| 106 |
data->expected_stdout = g_strdup (expected_stdout); |
| 107 |
data->expected_stderr = g_strdup (expected_stderr); |
| 108 |
|
| 109 |
g_spawn_async_with_pipes (NULL, args, env, |
| 110 |
G_SPAWN_DO_NOT_REAP_CHILD, |
| 111 |
NULL, NULL, &pid, NULL, |
| 112 |
&data->stdout_pipe, |
| 113 |
expected_stderr ? &data->stderr_pipe : NULL, |
| 114 |
&error); |
| 115 |
g_assert_no_error (error); |
| 116 |
|
| 117 |
g_strfreev (env); |
| 118 |
|
| 119 |
g_child_watch_add (pid, child_quit, data); |
| 120 |
outstanding_watches++; |
| 121 |
|
| 122 |
/* we block until the children write to stdout to make sure |
| 123 |
* they have started, as they need to be executed in order; |
| 124 |
* see https://bugzilla.gnome.org/show_bug.cgi?id=664627 |
| 125 |
*/ |
| 126 |
fd.fd = data->stdout_pipe; |
| 127 |
fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; |
| 128 |
g_poll (&fd, 1, -1); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
basic (void) |
| 133 |
{ |
| 134 |
GDBusConnection *c; |
| 135 |
|
| 136 |
g_assert (outstanding_watches == 0); |
| 137 |
|
| 138 |
session_bus_up (); |
| 139 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 140 |
|
| 141 |
main_loop = g_main_loop_new (NULL, 0); |
| 142 |
|
| 143 |
/* spawn the main instance */ |
| 144 |
spawn ("activated\n" |
| 145 |
"open file:///a file:///b\n" |
| 146 |
"exit status: 0\n", NULL, |
| 147 |
"./app", NULL); |
| 148 |
|
| 149 |
/* send it some files */ |
| 150 |
spawn ("exit status: 0\n", NULL, |
| 151 |
"./app", "/a", "/b", NULL); |
| 152 |
|
| 153 |
g_main_loop_run (main_loop); |
| 154 |
|
| 155 |
g_object_unref (c); |
| 156 |
session_bus_down (); |
| 157 |
|
| 158 |
g_main_loop_unref (main_loop); |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
test_remote_command_line (void) |
| 163 |
{ |
| 164 |
GDBusConnection *c; |
| 165 |
GFile *file; |
| 166 |
gchar *replies; |
| 167 |
gchar *cwd; |
| 168 |
|
| 169 |
g_assert (outstanding_watches == 0); |
| 170 |
|
| 171 |
session_bus_up (); |
| 172 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 173 |
|
| 174 |
main_loop = g_main_loop_new (NULL, 0); |
| 175 |
|
| 176 |
file = g_file_new_for_commandline_arg ("foo"); |
| 177 |
cwd = g_get_current_dir (); |
| 178 |
|
| 179 |
replies = g_strconcat ("got ./cmd 0\n", |
| 180 |
"got ./cmd 1\n", |
| 181 |
"cmdline ./cmd echo --abc -d\n", |
| 182 |
"environment TEST=1\n", |
| 183 |
"getenv TEST=1\n", |
| 184 |
"file ", g_file_get_path (file), "\n", |
| 185 |
"properties ok\n", |
| 186 |
"cwd ", cwd, "\n", |
| 187 |
"busy\n", |
| 188 |
"idle\n", |
| 189 |
"stdin ok\n", |
| 190 |
"exit status: 0\n", |
| 191 |
NULL); |
| 192 |
g_object_unref (file); |
| 193 |
|
| 194 |
/* spawn the main instance */ |
| 195 |
spawn (replies, NULL, |
| 196 |
"./cmd", NULL); |
| 197 |
|
| 198 |
g_free (replies); |
| 199 |
|
| 200 |
/* send it a few commandlines */ |
| 201 |
spawn ("exit status: 0\n", NULL, |
| 202 |
"./cmd", NULL); |
| 203 |
|
| 204 |
spawn ("exit status: 0\n", NULL, |
| 205 |
"./cmd", "echo", "--abc", "-d", NULL); |
| 206 |
|
| 207 |
spawn ("exit status: 0\n", NULL, |
| 208 |
"./cmd", "env", NULL); |
| 209 |
|
| 210 |
spawn ("exit status: 0\n", NULL, |
| 211 |
"./cmd", "getenv", NULL); |
| 212 |
|
| 213 |
spawn ("print test\n" |
| 214 |
"exit status: 0\n", NULL, |
| 215 |
"./cmd", "print", "test", NULL); |
| 216 |
|
| 217 |
spawn ("exit status: 0\n", "printerr test\n", |
| 218 |
"./cmd", "printerr", "test", NULL); |
| 219 |
|
| 220 |
spawn ("exit status: 0\n", NULL, |
| 221 |
"./cmd", "file", "foo", NULL); |
| 222 |
|
| 223 |
spawn ("exit status: 0\n", NULL, |
| 224 |
"./cmd", "properties", NULL); |
| 225 |
|
| 226 |
spawn ("exit status: 0\n", NULL, |
| 227 |
"./cmd", "cwd", NULL); |
| 228 |
|
| 229 |
spawn ("exit status: 0\n", NULL, |
| 230 |
"./cmd", "busy", NULL); |
| 231 |
|
| 232 |
spawn ("exit status: 0\n", NULL, |
| 233 |
"./cmd", "idle", NULL); |
| 234 |
|
| 235 |
spawn ("exit status: 0\n", NULL, |
| 236 |
"./cmd", "stdin", NULL); |
| 237 |
|
| 238 |
g_main_loop_run (main_loop); |
| 239 |
|
| 240 |
g_object_unref (c); |
| 241 |
session_bus_down (); |
| 242 |
|
| 243 |
g_main_loop_unref (main_loop); |
| 244 |
} |
| 245 |
|
| 246 |
static void |
| 247 |
test_remote_actions (void) |
| 248 |
{ |
| 249 |
GDBusConnection *c; |
| 250 |
|
| 251 |
g_assert (outstanding_watches == 0); |
| 252 |
|
| 253 |
session_bus_up (); |
| 254 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 255 |
|
| 256 |
main_loop = g_main_loop_new (NULL, 0); |
| 257 |
|
| 258 |
/* spawn the main instance */ |
| 259 |
spawn ("got ./cmd 0\n" |
| 260 |
"activate action1\n" |
| 261 |
"change action2 1\n" |
| 262 |
"exit status: 0\n", NULL, |
| 263 |
"./cmd", NULL); |
| 264 |
|
| 265 |
spawn ("actions quit new action1 action2\n" |
| 266 |
"exit status: 0\n", NULL, |
| 267 |
"./actions", "list", NULL); |
| 268 |
|
| 269 |
spawn ("exit status: 0\n", NULL, |
| 270 |
"./actions", "activate", NULL); |
| 271 |
|
| 272 |
spawn ("exit status: 0\n", NULL, |
| 273 |
"./actions", "set-state", NULL); |
| 274 |
|
| 275 |
g_main_loop_run (main_loop); |
| 276 |
|
| 277 |
g_object_unref (c); |
| 278 |
session_bus_down (); |
| 279 |
|
| 280 |
g_main_loop_unref (main_loop); |
| 281 |
} |
| 282 |
#endif |
| 283 |
|
| 284 |
#if 0 |
| 285 |
/* Now that we register non-unique apps on the bus we need to fix the |
| 286 |
* following test not to assume that it's safe to create multiple instances |
| 287 |
* of the same app in one process. |
| 288 |
* |
| 289 |
* See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that |
| 290 |
* introduced this problem. |
| 291 |
*/ |
| 292 |
|
| 293 |
static GApplication *recently_activated; |
| 294 |
static GMainLoop *loop; |
| 295 |
|
| 296 |
static void |
| 297 |
nonunique_activate (GApplication *application) |
| 298 |
{ |
| 299 |
recently_activated = application; |
| 300 |
|
| 301 |
if (loop != NULL) |
| 302 |
g_main_loop_quit (loop); |
| 303 |
} |
| 304 |
|
| 305 |
static GApplication * |
| 306 |
make_app (gboolean non_unique) |
| 307 |
{ |
| 308 |
GApplication *app; |
| 309 |
gboolean ok; |
| 310 |
|
| 311 |
app = g_application_new ("org.gtk.Test-Application", |
| 312 |
non_unique ? G_APPLICATION_NON_UNIQUE : 0); |
| 313 |
g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL); |
| 314 |
ok = g_application_register (app, NULL, NULL); |
| 315 |
if (!ok) |
| 316 |
{ |
| 317 |
g_object_unref (app); |
| 318 |
return NULL; |
| 319 |
} |
| 320 |
|
| 321 |
g_application_activate (app); |
| 322 |
|
| 323 |
return app; |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
test_nonunique (void) |
| 328 |
{ |
| 329 |
GApplication *first, *second, *third, *fourth; |
| 330 |
|
| 331 |
session_bus_up (); |
| 332 |
|
| 333 |
first = make_app (TRUE); |
| 334 |
/* non-remote because it is non-unique */ |
| 335 |
g_assert (!g_application_get_is_remote (first)); |
| 336 |
g_assert (recently_activated == first); |
| 337 |
recently_activated = NULL; |
| 338 |
|
| 339 |
second = make_app (FALSE); |
| 340 |
/* non-remote because it is first */ |
| 341 |
g_assert (!g_application_get_is_remote (second)); |
| 342 |
g_assert (recently_activated == second); |
| 343 |
recently_activated = NULL; |
| 344 |
|
| 345 |
third = make_app (TRUE); |
| 346 |
/* non-remote because it is non-unique */ |
| 347 |
g_assert (!g_application_get_is_remote (third)); |
| 348 |
g_assert (recently_activated == third); |
| 349 |
recently_activated = NULL; |
| 350 |
|
| 351 |
fourth = make_app (FALSE); |
| 352 |
/* should have failed to register due to being |
| 353 |
* unable to register the object paths |
| 354 |
*/ |
| 355 |
g_assert (fourth == NULL); |
| 356 |
g_assert (recently_activated == NULL); |
| 357 |
|
| 358 |
g_object_unref (first); |
| 359 |
g_object_unref (second); |
| 360 |
g_object_unref (third); |
| 361 |
|
| 362 |
session_bus_down (); |
| 363 |
} |
| 364 |
#endif |
| 365 |
|
| 366 |
static void |
| 367 |
properties (void) |
| 368 |
{ |
| 369 |
GDBusConnection *c; |
| 370 |
GObject *app; |
| 371 |
gchar *id; |
| 372 |
GApplicationFlags flags; |
| 373 |
gboolean registered; |
| 374 |
guint timeout; |
| 375 |
gboolean remote; |
| 376 |
gboolean ret; |
| 377 |
GError *error = NULL; |
| 378 |
|
| 379 |
session_bus_up (); |
| 380 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 381 |
|
| 382 |
app = g_object_new (G_TYPE_APPLICATION, |
| 383 |
"application-id", "org.gtk.TestApplication", |
| 384 |
NULL); |
| 385 |
|
| 386 |
g_object_get (app, |
| 387 |
"application-id", &id, |
| 388 |
"flags", &flags, |
| 389 |
"is-registered", ®istered, |
| 390 |
"inactivity-timeout", &timeout, |
| 391 |
NULL); |
| 392 |
|
| 393 |
g_assert_cmpstr (id, ==, "org.gtk.TestApplication"); |
| 394 |
g_assert_cmpint (flags, ==, G_APPLICATION_DEFAULT_FLAGS); |
| 395 |
g_assert (!registered); |
| 396 |
g_assert_cmpint (timeout, ==, 0); |
| 397 |
|
| 398 |
ret = g_application_register (G_APPLICATION (app), NULL, &error); |
| 399 |
g_assert (ret); |
| 400 |
g_assert_no_error (error); |
| 401 |
|
| 402 |
g_object_get (app, |
| 403 |
"is-registered", ®istered, |
| 404 |
"is-remote", &remote, |
| 405 |
NULL); |
| 406 |
|
| 407 |
g_assert (registered); |
| 408 |
g_assert (!remote); |
| 409 |
|
| 410 |
g_object_set (app, |
| 411 |
"inactivity-timeout", 1000, |
| 412 |
NULL); |
| 413 |
|
| 414 |
g_application_quit (G_APPLICATION (app)); |
| 415 |
|
| 416 |
g_object_unref (c); |
| 417 |
g_object_unref (app); |
| 418 |
g_free (id); |
| 419 |
|
| 420 |
session_bus_down (); |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
appid (void) |
| 425 |
{ |
| 426 |
gchar *id; |
| 427 |
|
| 428 |
g_assert_false (g_application_id_is_valid ("")); |
| 429 |
g_assert_false (g_application_id_is_valid (".")); |
| 430 |
g_assert_false (g_application_id_is_valid ("a")); |
| 431 |
g_assert_false (g_application_id_is_valid ("abc")); |
| 432 |
g_assert_false (g_application_id_is_valid (".abc")); |
| 433 |
g_assert_false (g_application_id_is_valid ("abc.")); |
| 434 |
g_assert_false (g_application_id_is_valid ("a..b")); |
| 435 |
g_assert_false (g_application_id_is_valid ("a/b")); |
| 436 |
g_assert_false (g_application_id_is_valid ("a\nb")); |
| 437 |
g_assert_false (g_application_id_is_valid ("a\nb")); |
| 438 |
g_assert_false (g_application_id_is_valid ("emoji_picker")); |
| 439 |
g_assert_false (g_application_id_is_valid ("emoji-picker")); |
| 440 |
g_assert_false (g_application_id_is_valid ("emojipicker")); |
| 441 |
g_assert_false (g_application_id_is_valid ("my.Terminal.0123")); |
| 442 |
id = g_new0 (gchar, 261); |
| 443 |
memset (id, 'a', 260); |
| 444 |
id[1] = '.'; |
| 445 |
id[260] = 0; |
| 446 |
g_assert_false (g_application_id_is_valid (id)); |
| 447 |
g_free (id); |
| 448 |
|
| 449 |
g_assert_true (g_application_id_is_valid ("a.b")); |
| 450 |
g_assert_true (g_application_id_is_valid ("A.B")); |
| 451 |
g_assert_true (g_application_id_is_valid ("A-.B")); |
| 452 |
g_assert_true (g_application_id_is_valid ("a_b.c-d")); |
| 453 |
g_assert_true (g_application_id_is_valid ("_a.b")); |
| 454 |
g_assert_true (g_application_id_is_valid ("-a.b")); |
| 455 |
g_assert_true (g_application_id_is_valid ("org.gnome.SessionManager")); |
| 456 |
g_assert_true (g_application_id_is_valid ("my.Terminal._0123")); |
| 457 |
g_assert_true (g_application_id_is_valid ("com.example.MyApp")); |
| 458 |
g_assert_true (g_application_id_is_valid ("com.example.internal_apps.Calculator")); |
| 459 |
g_assert_true (g_application_id_is_valid ("org._7_zip.Archiver")); |
| 460 |
} |
| 461 |
|
| 462 |
static gboolean nodbus_activated; |
| 463 |
|
| 464 |
static gboolean |
| 465 |
release_app (gpointer user_data) |
| 466 |
{ |
| 467 |
g_application_release (user_data); |
| 468 |
return G_SOURCE_REMOVE; |
| 469 |
} |
| 470 |
|
| 471 |
static void |
| 472 |
nodbus_activate (GApplication *app) |
| 473 |
{ |
| 474 |
nodbus_activated = TRUE; |
| 475 |
g_application_hold (app); |
| 476 |
|
| 477 |
g_assert (g_application_get_dbus_connection (app) == NULL); |
| 478 |
g_assert (g_application_get_dbus_object_path (app) == NULL); |
| 479 |
|
| 480 |
g_idle_add (release_app, app); |
| 481 |
} |
| 482 |
|
| 483 |
static void |
| 484 |
test_nodbus (void) |
| 485 |
{ |
| 486 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 487 |
gchar *argv[] = { binpath, NULL }; |
| 488 |
GApplication *app; |
| 489 |
|
| 490 |
app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_DEFAULT_FLAGS); |
| 491 |
g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL); |
| 492 |
g_application_run (app, 1, argv); |
| 493 |
g_object_unref (app); |
| 494 |
|
| 495 |
g_assert (nodbus_activated); |
| 496 |
g_free (binpath); |
| 497 |
} |
| 498 |
|
| 499 |
static gboolean noappid_activated; |
| 500 |
|
| 501 |
static void |
| 502 |
noappid_activate (GApplication *app) |
| 503 |
{ |
| 504 |
noappid_activated = TRUE; |
| 505 |
g_application_hold (app); |
| 506 |
|
| 507 |
g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE); |
| 508 |
|
| 509 |
g_idle_add (release_app, app); |
| 510 |
} |
| 511 |
|
| 512 |
/* test that no appid -> non-unique */ |
| 513 |
static void |
| 514 |
test_noappid (void) |
| 515 |
{ |
| 516 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 517 |
gchar *argv[] = { binpath, NULL }; |
| 518 |
GApplication *app; |
| 519 |
|
| 520 |
app = g_application_new (NULL, G_APPLICATION_DEFAULT_FLAGS); |
| 521 |
g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL); |
| 522 |
g_application_run (app, 1, argv); |
| 523 |
g_object_unref (app); |
| 524 |
|
| 525 |
g_assert (noappid_activated); |
| 526 |
g_free (binpath); |
| 527 |
} |
| 528 |
|
| 529 |
static gboolean activated; |
| 530 |
static gboolean quitted; |
| 531 |
|
| 532 |
static gboolean |
| 533 |
quit_app (gpointer user_data) |
| 534 |
{ |
| 535 |
quitted = TRUE; |
| 536 |
g_application_quit (user_data); |
| 537 |
return G_SOURCE_REMOVE; |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
quit_activate (GApplication *app) |
| 542 |
{ |
| 543 |
activated = TRUE; |
| 544 |
g_application_hold (app); |
| 545 |
|
| 546 |
g_assert (g_application_get_dbus_connection (app) != NULL); |
| 547 |
g_assert (g_application_get_dbus_object_path (app) != NULL); |
| 548 |
|
| 549 |
g_idle_add (quit_app, app); |
| 550 |
} |
| 551 |
|
| 552 |
static void |
| 553 |
test_quit (void) |
| 554 |
{ |
| 555 |
GDBusConnection *c; |
| 556 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 557 |
gchar *argv[] = { binpath, NULL }; |
| 558 |
GApplication *app; |
| 559 |
|
| 560 |
session_bus_up (); |
| 561 |
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 562 |
|
| 563 |
app = g_application_new ("org.gtk.Unimportant", |
| 564 |
G_APPLICATION_DEFAULT_FLAGS); |
| 565 |
activated = FALSE; |
| 566 |
quitted = FALSE; |
| 567 |
g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL); |
| 568 |
g_application_run (app, 1, argv); |
| 569 |
g_object_unref (app); |
| 570 |
g_object_unref (c); |
| 571 |
|
| 572 |
g_assert (activated); |
| 573 |
g_assert (quitted); |
| 574 |
|
| 575 |
session_bus_down (); |
| 576 |
g_free (binpath); |
| 577 |
} |
| 578 |
|
| 579 |
typedef struct |
| 580 |
{ |
| 581 |
gboolean shutdown; |
| 582 |
GParamSpec *notify_spec; /* (owned) (nullable) */ |
| 583 |
} RegisteredData; |
| 584 |
|
| 585 |
static void |
| 586 |
on_registered_shutdown (GApplication *app, |
| 587 |
gpointer user_data) |
| 588 |
{ |
| 589 |
RegisteredData *registered_data = user_data; |
| 590 |
|
| 591 |
registered_data->shutdown = TRUE; |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
on_registered_notify (GApplication *app, |
| 596 |
GParamSpec *spec, |
| 597 |
gpointer user_data) |
| 598 |
{ |
| 599 |
RegisteredData *registered_data = user_data; |
| 600 |
registered_data->notify_spec = g_param_spec_ref (spec); |
| 601 |
|
| 602 |
if (g_application_get_is_registered (app)) |
| 603 |
g_assert_false (registered_data->shutdown); |
| 604 |
else |
| 605 |
g_assert_true (registered_data->shutdown); |
| 606 |
} |
| 607 |
|
| 608 |
static void |
| 609 |
test_registered (void) |
| 610 |
{ |
| 611 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 612 |
gchar *argv[] = { binpath, NULL }; |
| 613 |
RegisteredData registered_data = { FALSE, NULL }; |
| 614 |
GApplication *app; |
| 615 |
|
| 616 |
app = g_application_new (NULL, G_APPLICATION_DEFAULT_FLAGS); |
| 617 |
g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL); |
| 618 |
g_signal_connect (app, "shutdown", G_CALLBACK (on_registered_shutdown), ®istered_data); |
| 619 |
g_signal_connect (app, "notify::is-registered", G_CALLBACK (on_registered_notify), ®istered_data); |
| 620 |
|
| 621 |
g_assert_null (registered_data.notify_spec); |
| 622 |
|
| 623 |
g_assert_true (g_application_register (app, NULL, NULL)); |
| 624 |
g_assert_true (g_application_get_is_registered (app)); |
| 625 |
|
| 626 |
g_assert_nonnull (registered_data.notify_spec); |
| 627 |
g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered"); |
| 628 |
g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref); |
| 629 |
|
| 630 |
g_assert_false (registered_data.shutdown); |
| 631 |
|
| 632 |
g_application_run (app, 1, argv); |
| 633 |
|
| 634 |
g_assert_true (registered_data.shutdown); |
| 635 |
g_assert_false (g_application_get_is_registered (app)); |
| 636 |
g_assert_nonnull (registered_data.notify_spec); |
| 637 |
g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered"); |
| 638 |
g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref); |
| 639 |
|
| 640 |
/* Register it again */ |
| 641 |
registered_data.shutdown = FALSE; |
| 642 |
g_assert_true (g_application_register (app, NULL, NULL)); |
| 643 |
g_assert_true (g_application_get_is_registered (app)); |
| 644 |
g_assert_nonnull (registered_data.notify_spec); |
| 645 |
g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered"); |
| 646 |
g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref); |
| 647 |
g_assert_false (registered_data.shutdown); |
| 648 |
|
| 649 |
g_object_unref (app); |
| 650 |
|
| 651 |
g_free (binpath); |
| 652 |
} |
| 653 |
|
| 654 |
static void |
| 655 |
on_activate (GApplication *app) |
| 656 |
{ |
| 657 |
gchar **actions; |
| 658 |
GAction *action; |
| 659 |
GVariant *state; |
| 660 |
|
| 661 |
g_assert (!g_application_get_is_remote (app)); |
| 662 |
|
| 663 |
actions = g_action_group_list_actions (G_ACTION_GROUP (app)); |
| 664 |
g_assert (g_strv_length (actions) == 0); |
| 665 |
g_strfreev (actions); |
| 666 |
|
| 667 |
action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE)); |
| 668 |
g_action_map_add_action (G_ACTION_MAP (app), action); |
| 669 |
|
| 670 |
actions = g_action_group_list_actions (G_ACTION_GROUP (app)); |
| 671 |
g_assert (g_strv_length (actions) == 1); |
| 672 |
g_strfreev (actions); |
| 673 |
|
| 674 |
g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE)); |
| 675 |
state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test"); |
| 676 |
g_assert (g_variant_get_boolean (state) == TRUE); |
| 677 |
|
| 678 |
action = g_action_map_lookup_action (G_ACTION_MAP (app), "test"); |
| 679 |
g_assert (action != NULL); |
| 680 |
|
| 681 |
g_action_map_remove_action (G_ACTION_MAP (app), "test"); |
| 682 |
|
| 683 |
actions = g_action_group_list_actions (G_ACTION_GROUP (app)); |
| 684 |
g_assert (g_strv_length (actions) == 0); |
| 685 |
g_strfreev (actions); |
| 686 |
} |
| 687 |
|
| 688 |
static void |
| 689 |
test_local_actions (void) |
| 690 |
{ |
| 691 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 692 |
gchar *argv[] = { binpath, NULL }; |
| 693 |
GApplication *app; |
| 694 |
|
| 695 |
app = g_application_new ("org.gtk.Unimportant", |
| 696 |
G_APPLICATION_DEFAULT_FLAGS); |
| 697 |
g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); |
| 698 |
g_application_run (app, 1, argv); |
| 699 |
g_object_unref (app); |
| 700 |
g_free (binpath); |
| 701 |
} |
| 702 |
|
| 703 |
typedef GApplication TestLocCmdApp; |
| 704 |
typedef GApplicationClass TestLocCmdAppClass; |
| 705 |
|
| 706 |
static GType test_loc_cmd_app_get_type (void); |
| 707 |
G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION) |
| 708 |
|
| 709 |
static void |
| 710 |
test_loc_cmd_app_init (TestLocCmdApp *app) |
| 711 |
{ |
| 712 |
} |
| 713 |
|
| 714 |
static void |
| 715 |
test_loc_cmd_app_startup (GApplication *app) |
| 716 |
{ |
| 717 |
g_assert_not_reached (); |
| 718 |
} |
| 719 |
|
| 720 |
static void |
| 721 |
test_loc_cmd_app_shutdown (GApplication *app) |
| 722 |
{ |
| 723 |
g_assert_not_reached (); |
| 724 |
} |
| 725 |
|
| 726 |
static gboolean |
| 727 |
test_loc_cmd_app_local_command_line (GApplication *application, |
| 728 |
gchar ***arguments, |
| 729 |
gint *exit_status) |
| 730 |
{ |
| 731 |
return TRUE; |
| 732 |
} |
| 733 |
|
| 734 |
static void |
| 735 |
test_loc_cmd_app_class_init (TestLocCmdAppClass *klass) |
| 736 |
{ |
| 737 |
G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup; |
| 738 |
G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown; |
| 739 |
G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line; |
| 740 |
} |
| 741 |
|
| 742 |
static void |
| 743 |
test_local_command_line (void) |
| 744 |
{ |
| 745 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 746 |
gchar *argv[] = { binpath, "-invalid", NULL }; |
| 747 |
GApplication *app; |
| 748 |
|
| 749 |
app = g_object_new (test_loc_cmd_app_get_type (), |
| 750 |
"application-id", "org.gtk.Unimportant", |
| 751 |
"flags", G_APPLICATION_DEFAULT_FLAGS, |
| 752 |
NULL); |
| 753 |
g_application_run (app, 1, argv); |
| 754 |
g_object_unref (app); |
| 755 |
g_free (binpath); |
| 756 |
} |
| 757 |
|
| 758 |
static void |
| 759 |
test_resource_path (void) |
| 760 |
{ |
| 761 |
GApplication *app; |
| 762 |
|
| 763 |
app = g_application_new ("x.y.z", 0); |
| 764 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z"); |
| 765 |
|
| 766 |
/* this should not change anything */ |
| 767 |
g_application_set_application_id (app, "a.b.c"); |
| 768 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z"); |
| 769 |
|
| 770 |
/* but this should... */ |
| 771 |
g_application_set_resource_base_path (app, "/x"); |
| 772 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x"); |
| 773 |
|
| 774 |
/* ... and this */ |
| 775 |
g_application_set_resource_base_path (app, NULL); |
| 776 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL); |
| 777 |
|
| 778 |
g_object_unref (app); |
| 779 |
|
| 780 |
/* Make sure that overriding at construction time works properly */ |
| 781 |
app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", "/a", NULL); |
| 782 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/a"); |
| 783 |
g_object_unref (app); |
| 784 |
|
| 785 |
/* ... particularly if we override to NULL */ |
| 786 |
app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", NULL, NULL); |
| 787 |
g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL); |
| 788 |
g_object_unref (app); |
| 789 |
} |
| 790 |
|
| 791 |
static gint |
| 792 |
test_help_command_line (GApplication *app, |
| 793 |
GApplicationCommandLine *command_line, |
| 794 |
gpointer user_data) |
| 795 |
{ |
| 796 |
gboolean *called = user_data; |
| 797 |
|
| 798 |
*called = TRUE; |
| 799 |
|
| 800 |
return 0; |
| 801 |
} |
| 802 |
|
| 803 |
/* Test whether --help is handled when HANDLES_COMMND_LINE is set and |
| 804 |
* options have been added. |
| 805 |
*/ |
| 806 |
static void |
| 807 |
test_help (void) |
| 808 |
{ |
| 809 |
if (g_test_subprocess ()) |
| 810 |
{ |
| 811 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 812 |
gchar *argv[] = { binpath, "--help", NULL }; |
| 813 |
GApplication *app; |
| 814 |
gboolean called = FALSE; |
| 815 |
int status; |
| 816 |
|
| 817 |
app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE); |
| 818 |
g_application_add_main_option (app, "foo", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 819 |
g_signal_connect (app, "command-line", G_CALLBACK (test_help_command_line), &called); |
| 820 |
|
| 821 |
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv); |
| 822 |
g_assert (called == TRUE); |
| 823 |
g_assert_cmpint (status, ==, 0); |
| 824 |
|
| 825 |
g_object_unref (app); |
| 826 |
g_free (binpath); |
| 827 |
return; |
| 828 |
} |
| 829 |
|
| 830 |
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT); |
| 831 |
g_test_trap_assert_passed (); |
| 832 |
g_test_trap_assert_stdout ("*Application options*"); |
| 833 |
} |
| 834 |
|
| 835 |
static gint |
| 836 |
command_line_done_callback (GApplication *app, |
| 837 |
GApplicationCommandLine *command_line, |
| 838 |
gpointer user_data) |
| 839 |
{ |
| 840 |
gboolean *called = user_data; |
| 841 |
|
| 842 |
*called = TRUE; |
| 843 |
|
| 844 |
g_application_command_line_set_exit_status (command_line, 42); |
| 845 |
g_application_command_line_done (command_line); |
| 846 |
|
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
/* Test whether 'command-line' handler return value is ignored |
| 851 |
* after g_application_command_line_done() |
| 852 |
*/ |
| 853 |
static void |
| 854 |
test_command_line_done (void) |
| 855 |
{ |
| 856 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 857 |
const gchar *const argv[] = { binpath, "arg", NULL }; |
| 858 |
GApplication *app; |
| 859 |
gboolean called = FALSE; |
| 860 |
int status; |
| 861 |
gulong command_line_id; |
| 862 |
|
| 863 |
app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE); |
| 864 |
command_line_id = g_signal_connect (app, "command-line", G_CALLBACK (command_line_done_callback), &called); |
| 865 |
|
| 866 |
status = g_application_run (app, G_N_ELEMENTS (argv) - 1, (gchar **) argv); |
| 867 |
|
| 868 |
g_signal_handler_disconnect (app, command_line_id); |
| 869 |
g_object_unref (app); |
| 870 |
g_free (binpath); |
| 871 |
|
| 872 |
g_assert_true (called); |
| 873 |
g_assert_cmpint (status, ==, 42); |
| 874 |
} |
| 875 |
|
| 876 |
static void |
| 877 |
test_busy (void) |
| 878 |
{ |
| 879 |
GApplication *app; |
| 880 |
|
| 881 |
/* use GSimpleAction to bind to the busy state, because it's easy to |
| 882 |
* create and has an easily modifiable boolean property */ |
| 883 |
GSimpleAction *action1; |
| 884 |
GSimpleAction *action2; |
| 885 |
|
| 886 |
session_bus_up (); |
| 887 |
|
| 888 |
app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_NON_UNIQUE); |
| 889 |
g_assert (g_application_register (app, NULL, NULL)); |
| 890 |
|
| 891 |
g_assert (!g_application_get_is_busy (app)); |
| 892 |
g_application_mark_busy (app); |
| 893 |
g_assert (g_application_get_is_busy (app)); |
| 894 |
g_application_unmark_busy (app); |
| 895 |
g_assert (!g_application_get_is_busy (app)); |
| 896 |
|
| 897 |
action1 = g_simple_action_new ("action", NULL); |
| 898 |
g_application_bind_busy_property (app, action1, "enabled"); |
| 899 |
g_assert (g_application_get_is_busy (app)); |
| 900 |
|
| 901 |
g_simple_action_set_enabled (action1, FALSE); |
| 902 |
g_assert (!g_application_get_is_busy (app)); |
| 903 |
|
| 904 |
g_application_mark_busy (app); |
| 905 |
g_assert (g_application_get_is_busy (app)); |
| 906 |
|
| 907 |
action2 = g_simple_action_new ("action", NULL); |
| 908 |
g_application_bind_busy_property (app, action2, "enabled"); |
| 909 |
g_assert (g_application_get_is_busy (app)); |
| 910 |
|
| 911 |
g_application_unmark_busy (app); |
| 912 |
g_assert (g_application_get_is_busy (app)); |
| 913 |
|
| 914 |
g_object_unref (action2); |
| 915 |
g_assert (!g_application_get_is_busy (app)); |
| 916 |
|
| 917 |
g_simple_action_set_enabled (action1, TRUE); |
| 918 |
g_assert (g_application_get_is_busy (app)); |
| 919 |
|
| 920 |
g_application_mark_busy (app); |
| 921 |
g_assert (g_application_get_is_busy (app)); |
| 922 |
|
| 923 |
g_application_unbind_busy_property (app, action1, "enabled"); |
| 924 |
g_assert (g_application_get_is_busy (app)); |
| 925 |
|
| 926 |
g_application_unmark_busy (app); |
| 927 |
g_assert (!g_application_get_is_busy (app)); |
| 928 |
|
| 929 |
g_object_unref (action1); |
| 930 |
g_object_unref (app); |
| 931 |
|
| 932 |
session_bus_down (); |
| 933 |
} |
| 934 |
|
| 935 |
/* |
| 936 |
* Test that handle-local-options works as expected |
| 937 |
*/ |
| 938 |
|
| 939 |
static gint |
| 940 |
test_local_options (GApplication *app, |
| 941 |
GVariantDict *options, |
| 942 |
gpointer data) |
| 943 |
{ |
| 944 |
gboolean *called = data; |
| 945 |
|
| 946 |
*called = TRUE; |
| 947 |
|
| 948 |
if (g_variant_dict_contains (options, "success")) |
| 949 |
return 0; |
| 950 |
else if (g_variant_dict_contains (options, "failure")) |
| 951 |
return 1; |
| 952 |
else |
| 953 |
return -1; |
| 954 |
} |
| 955 |
|
| 956 |
static gint |
| 957 |
second_handler (GApplication *app, |
| 958 |
GVariantDict *options, |
| 959 |
gpointer data) |
| 960 |
{ |
| 961 |
gboolean *called = data; |
| 962 |
|
| 963 |
*called = TRUE; |
| 964 |
|
| 965 |
return 2; |
| 966 |
} |
| 967 |
|
| 968 |
static void |
| 969 |
test_handle_local_options_success (void) |
| 970 |
{ |
| 971 |
if (g_test_subprocess ()) |
| 972 |
{ |
| 973 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 974 |
gchar *argv[] = { binpath, "--success", NULL }; |
| 975 |
GApplication *app; |
| 976 |
gboolean called = FALSE; |
| 977 |
gboolean called2 = FALSE; |
| 978 |
int status; |
| 979 |
|
| 980 |
app = g_application_new ("org.gtk.TestApplication", 0); |
| 981 |
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 982 |
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 983 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called); |
| 984 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2); |
| 985 |
|
| 986 |
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv); |
| 987 |
g_assert (called); |
| 988 |
g_assert (!called2); |
| 989 |
g_assert_cmpint (status, ==, 0); |
| 990 |
|
| 991 |
g_object_unref (app); |
| 992 |
g_free (binpath); |
| 993 |
return; |
| 994 |
} |
| 995 |
|
| 996 |
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR); |
| 997 |
g_test_trap_assert_passed (); |
| 998 |
} |
| 999 |
|
| 1000 |
static void |
| 1001 |
test_handle_local_options_failure (void) |
| 1002 |
{ |
| 1003 |
if (g_test_subprocess ()) |
| 1004 |
{ |
| 1005 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 1006 |
gchar *argv[] = { binpath, "--failure", NULL }; |
| 1007 |
GApplication *app; |
| 1008 |
gboolean called = FALSE; |
| 1009 |
gboolean called2 = FALSE; |
| 1010 |
int status; |
| 1011 |
|
| 1012 |
app = g_application_new ("org.gtk.TestApplication", 0); |
| 1013 |
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 1014 |
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 1015 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called); |
| 1016 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2); |
| 1017 |
|
| 1018 |
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv); |
| 1019 |
g_assert (called); |
| 1020 |
g_assert (!called2); |
| 1021 |
g_assert_cmpint (status, ==, 1); |
| 1022 |
|
| 1023 |
g_object_unref (app); |
| 1024 |
g_free (binpath); |
| 1025 |
return; |
| 1026 |
} |
| 1027 |
|
| 1028 |
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR); |
| 1029 |
g_test_trap_assert_passed (); |
| 1030 |
} |
| 1031 |
|
| 1032 |
static void |
| 1033 |
test_handle_local_options_passthrough (void) |
| 1034 |
{ |
| 1035 |
if (g_test_subprocess ()) |
| 1036 |
{ |
| 1037 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 1038 |
gchar *argv[] = { binpath, NULL }; |
| 1039 |
GApplication *app; |
| 1040 |
gboolean called = FALSE; |
| 1041 |
gboolean called2 = FALSE; |
| 1042 |
int status; |
| 1043 |
|
| 1044 |
app = g_application_new ("org.gtk.TestApplication", 0); |
| 1045 |
g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 1046 |
g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", ""); |
| 1047 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called); |
| 1048 |
g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2); |
| 1049 |
|
| 1050 |
status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv); |
| 1051 |
g_assert (called); |
| 1052 |
g_assert (called2); |
| 1053 |
g_assert_cmpint (status, ==, 2); |
| 1054 |
|
| 1055 |
g_object_unref (app); |
| 1056 |
g_free (binpath); |
| 1057 |
return; |
| 1058 |
} |
| 1059 |
|
| 1060 |
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR); |
| 1061 |
g_test_trap_assert_passed (); |
| 1062 |
} |
| 1063 |
|
| 1064 |
static void |
| 1065 |
test_api (void) |
| 1066 |
{ |
| 1067 |
GApplication *app; |
| 1068 |
GSimpleAction *action; |
| 1069 |
|
| 1070 |
app = g_application_new ("org.gtk.TestApplication", 0); |
| 1071 |
|
| 1072 |
/* add an action without a name */ |
| 1073 |
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*assertion*failed*"); |
| 1074 |
action = g_simple_action_new (NULL, NULL); |
| 1075 |
g_assert (action == NULL); |
| 1076 |
g_test_assert_expected_messages (); |
| 1077 |
|
| 1078 |
/* also, gapplication shouldn't accept actions without names */ |
| 1079 |
action = g_object_new (G_TYPE_SIMPLE_ACTION, NULL); |
| 1080 |
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*action has no name*"); |
| 1081 |
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); |
| 1082 |
g_test_assert_expected_messages (); |
| 1083 |
|
| 1084 |
g_object_unref (action); |
| 1085 |
g_object_unref (app); |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* Check that G_APPLICATION_ALLOW_REPLACEMENT works. To do so, we launch |
| 1089 |
* a GApplication in this process that allows replacement, and then |
| 1090 |
* launch a subprocess with --gapplication-replace. We have to do our |
| 1091 |
* own async version of g_test_trap_subprocess() here since we need |
| 1092 |
* the main process to keep spinning its mainloop. |
| 1093 |
*/ |
| 1094 |
|
| 1095 |
static gboolean |
| 1096 |
name_was_lost (GApplication *app, |
| 1097 |
gboolean *called) |
| 1098 |
{ |
| 1099 |
*called = TRUE; |
| 1100 |
g_application_quit (app); |
| 1101 |
return TRUE; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static void |
| 1105 |
startup_in_subprocess (GApplication *app, |
| 1106 |
gboolean *called) |
| 1107 |
{ |
| 1108 |
*called = TRUE; |
| 1109 |
} |
| 1110 |
|
| 1111 |
typedef struct |
| 1112 |
{ |
| 1113 |
gboolean allow_replacement; |
| 1114 |
GSubprocess *subprocess; |
| 1115 |
GApplication *app; /* (not owned) */ |
| 1116 |
guint timeout_id; |
| 1117 |
} TestReplaceData; |
| 1118 |
|
| 1119 |
static void |
| 1120 |
startup_cb (GApplication *app, |
| 1121 |
TestReplaceData *data) |
| 1122 |
{ |
| 1123 |
const char *argv[] = { NULL, "--verbose", "--quiet", "-p", NULL, "--GTestSubprocess", NULL }; |
| 1124 |
GSubprocessLauncher *launcher; |
| 1125 |
GError *local_error = NULL; |
| 1126 |
|
| 1127 |
g_application_hold (app); |
| 1128 |
|
| 1129 |
argv[0] = g_get_prgname (); |
| 1130 |
|
| 1131 |
if (data->allow_replacement) |
| 1132 |
argv[4] = "/gapplication/replace"; |
| 1133 |
else |
| 1134 |
argv[4] = "/gapplication/no-replace"; |
| 1135 |
|
| 1136 |
/* Now that we are the primary instance, launch our replacement. |
| 1137 |
* We inherit the environment to share the test session bus. |
| 1138 |
*/ |
| 1139 |
g_test_message ("launching subprocess"); |
| 1140 |
|
| 1141 |
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE); |
| 1142 |
g_subprocess_launcher_set_environ (launcher, NULL); |
| 1143 |
data->subprocess = g_subprocess_launcher_spawnv (launcher, argv, &local_error); |
| 1144 |
g_assert_no_error (local_error); |
| 1145 |
g_object_unref (launcher); |
| 1146 |
|
| 1147 |
if (!data->allow_replacement) |
| 1148 |
{ |
| 1149 |
/* make sure we exit after a bit, if the subprocess is not replacing us */ |
| 1150 |
g_application_set_inactivity_timeout (app, 500); |
| 1151 |
g_application_release (app); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
static void |
| 1156 |
activate (gpointer data) |
| 1157 |
{ |
| 1158 |
/* GApplication complains if we don't connect to ::activate */ |
| 1159 |
} |
| 1160 |
|
| 1161 |
static void |
| 1162 |
quit_already (gpointer user_data) |
| 1163 |
{ |
| 1164 |
TestReplaceData *data = user_data; |
| 1165 |
|
| 1166 |
g_application_quit (data->app); |
| 1167 |
data->timeout_id = 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static void |
| 1171 |
test_replace (gconstpointer data) |
| 1172 |
{ |
| 1173 |
gboolean allow = GPOINTER_TO_INT (data); |
| 1174 |
|
| 1175 |
if (g_test_subprocess ()) |
| 1176 |
{ |
| 1177 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 1178 |
char *argv[] = { binpath, "--gapplication-replace", NULL }; |
| 1179 |
GApplication *app; |
| 1180 |
gboolean startup = FALSE; |
| 1181 |
|
| 1182 |
app = g_application_new ("org.gtk.TestApplication.Replace", G_APPLICATION_ALLOW_REPLACEMENT); |
| 1183 |
g_signal_connect (app, "startup", G_CALLBACK (startup_in_subprocess), &startup); |
| 1184 |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); |
| 1185 |
|
| 1186 |
g_application_run (app, G_N_ELEMENTS (argv) - 1, argv); |
| 1187 |
|
| 1188 |
if (allow) |
| 1189 |
g_assert_true (startup); |
| 1190 |
else |
| 1191 |
g_assert_false (startup); |
| 1192 |
|
| 1193 |
g_object_unref (app); |
| 1194 |
g_free (binpath); |
| 1195 |
} |
| 1196 |
else |
| 1197 |
{ |
| 1198 |
char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL); |
| 1199 |
gchar *argv[] = { binpath, NULL }; |
| 1200 |
GApplication *app; |
| 1201 |
gboolean name_lost = FALSE; |
| 1202 |
TestReplaceData data; |
| 1203 |
GTestDBus *bus; |
| 1204 |
|
| 1205 |
data.allow_replacement = allow; |
| 1206 |
data.subprocess = NULL; |
| 1207 |
data.timeout_id = 0; |
| 1208 |
|
| 1209 |
bus = g_test_dbus_new (0); |
| 1210 |
g_test_dbus_up (bus); |
| 1211 |
|
| 1212 |
app = data.app = g_application_new ("org.gtk.TestApplication.Replace", allow ? G_APPLICATION_ALLOW_REPLACEMENT : G_APPLICATION_DEFAULT_FLAGS); |
| 1213 |
g_application_set_inactivity_timeout (app, 500); |
| 1214 |
g_signal_connect (app, "name-lost", G_CALLBACK (name_was_lost), &name_lost); |
| 1215 |
g_signal_connect (app, "startup", G_CALLBACK (startup_cb), &data); |
| 1216 |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); |
| 1217 |
|
| 1218 |
if (!allow) |
| 1219 |
data.timeout_id = g_timeout_add_seconds_once (1, quit_already, &data); |
| 1220 |
|
| 1221 |
g_application_run (app, G_N_ELEMENTS (argv) - 1, argv); |
| 1222 |
|
| 1223 |
g_assert_nonnull (data.subprocess); |
| 1224 |
if (allow) |
| 1225 |
g_assert_true (name_lost); |
| 1226 |
else |
| 1227 |
g_assert_false (name_lost); |
| 1228 |
|
| 1229 |
g_clear_handle_id (&data.timeout_id, g_source_remove); |
| 1230 |
g_object_unref (app); |
| 1231 |
g_free (binpath); |
| 1232 |
|
| 1233 |
g_subprocess_wait (data.subprocess, NULL, NULL); |
| 1234 |
g_clear_object (&data.subprocess); |
| 1235 |
|
| 1236 |
g_test_dbus_down (bus); |
| 1237 |
g_object_unref (bus); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
static void |
| 1242 |
dbus_activate_cb (GApplication *app, |
| 1243 |
gpointer user_data) |
| 1244 |
{ |
| 1245 |
guint *n_activations = user_data; |
| 1246 |
|
| 1247 |
*n_activations = *n_activations + 1; |
| 1248 |
g_main_context_wakeup (NULL); |
| 1249 |
} |
| 1250 |
|
| 1251 |
static void dbus_startup_reply_cb (GObject *source_object, |
| 1252 |
GAsyncResult *result, |
| 1253 |
gpointer user_data); |
| 1254 |
static gboolean dbus_startup_reply_idle_cb (gpointer user_data); |
| 1255 |
|
| 1256 |
static void |
| 1257 |
dbus_startup_cb (GApplication *app, |
| 1258 |
gpointer user_data) |
| 1259 |
{ |
| 1260 |
GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 1261 |
GDBusMessage *message = G_DBUS_MESSAGE (user_data); |
| 1262 |
|
| 1263 |
g_assert_nonnull (connection); |
| 1264 |
|
| 1265 |
g_dbus_connection_send_message_with_reply (connection, message, |
| 1266 |
G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, |
| 1267 |
NULL, NULL, |
| 1268 |
dbus_startup_reply_cb, g_object_ref (app)); |
| 1269 |
|
| 1270 |
g_clear_object (&connection); |
| 1271 |
} |
| 1272 |
|
| 1273 |
static void |
| 1274 |
dbus_startup_reply_cb (GObject *source_object, |
| 1275 |
GAsyncResult *result, |
| 1276 |
gpointer user_data) |
| 1277 |
{ |
| 1278 |
GDBusConnection *connection = G_DBUS_CONNECTION (source_object); |
| 1279 |
GApplication *app = G_APPLICATION (user_data); |
| 1280 |
GDBusMessage *reply = NULL; |
| 1281 |
GError *local_error = NULL; |
| 1282 |
|
| 1283 |
reply = g_dbus_connection_send_message_with_reply_finish (connection, result, &local_error); |
| 1284 |
g_assert_no_error (local_error); |
| 1285 |
|
| 1286 |
g_object_set_data_full (G_OBJECT (app), "dbus-command-line-reply", g_steal_pointer (&reply), g_object_unref); |
| 1287 |
|
| 1288 |
/* Release the app in an idle callback, so there’s time to process other |
| 1289 |
* pending sources first. */ |
| 1290 |
g_idle_add_full (G_PRIORITY_LOW, dbus_startup_reply_idle_cb, g_steal_pointer (&app), g_object_unref); |
| 1291 |
} |
| 1292 |
|
| 1293 |
static gboolean |
| 1294 |
dbus_startup_reply_idle_cb (gpointer user_data) |
| 1295 |
{ |
| 1296 |
GApplication *app = G_APPLICATION (user_data); |
| 1297 |
|
| 1298 |
g_application_release (app); |
| 1299 |
|
| 1300 |
return G_SOURCE_REMOVE; |
| 1301 |
} |
| 1302 |
|
| 1303 |
static void |
| 1304 |
test_dbus_activate (void) |
| 1305 |
{ |
| 1306 |
GTestDBus *bus = NULL; |
| 1307 |
GVariantBuilder builder; |
| 1308 |
GDBusMessage *message = NULL; |
| 1309 |
GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */ |
| 1310 |
gsize i; |
| 1311 |
|
| 1312 |
g_test_summary ("Test that calling the Activate D-Bus method works"); |
| 1313 |
|
| 1314 |
/* Try various different messages */ |
| 1315 |
messages = g_ptr_array_new_with_free_func (g_object_unref); |
| 1316 |
|
| 1317 |
/* Via org.gtk.Application */ |
| 1318 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate", |
| 1319 |
"/org/gtk/TestApplication/Activate", |
| 1320 |
"org.gtk.Application", |
| 1321 |
"Activate"); |
| 1322 |
g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL)); |
| 1323 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1324 |
|
| 1325 |
/* Via org.freedesktop.Application */ |
| 1326 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate", |
| 1327 |
"/org/gtk/TestApplication/Activate", |
| 1328 |
"org.freedesktop.Application", |
| 1329 |
"Activate"); |
| 1330 |
g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL)); |
| 1331 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1332 |
|
| 1333 |
/* With some platform data */ |
| 1334 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| 1335 |
g_variant_builder_add (&builder, "{sv}", "cwd", g_variant_new_bytestring ("/home/henry")); |
| 1336 |
|
| 1337 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate", |
| 1338 |
"/org/gtk/TestApplication/Activate", |
| 1339 |
"org.gtk.Application", |
| 1340 |
"Activate"); |
| 1341 |
g_dbus_message_set_body (message, g_variant_new ("(a{sv})", &builder)); |
| 1342 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1343 |
|
| 1344 |
/* Try each message */ |
| 1345 |
bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 1346 |
g_test_dbus_up (bus); |
| 1347 |
|
| 1348 |
for (i = 0; i < messages->len; i++) |
| 1349 |
{ |
| 1350 |
GApplication *app = NULL; |
| 1351 |
gulong activate_id, startup_id; |
| 1352 |
guint n_activations = 0; |
| 1353 |
|
| 1354 |
g_test_message ("Message %" G_GSIZE_FORMAT, i); |
| 1355 |
|
| 1356 |
app = g_application_new ("org.gtk.TestApplication.Activate", G_APPLICATION_DEFAULT_FLAGS); |
| 1357 |
activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_cb), &n_activations); |
| 1358 |
startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]); |
| 1359 |
|
| 1360 |
g_application_hold (app); |
| 1361 |
g_application_run (app, 0, NULL); |
| 1362 |
|
| 1363 |
/* It’ll be activated once as normal, and once due to the D-Bus call */ |
| 1364 |
g_assert_cmpuint (n_activations, ==, 2); |
| 1365 |
|
| 1366 |
g_signal_handler_disconnect (app, startup_id); |
| 1367 |
g_signal_handler_disconnect (app, activate_id); |
| 1368 |
g_clear_object (&app); |
| 1369 |
} |
| 1370 |
|
| 1371 |
g_ptr_array_unref (messages); |
| 1372 |
|
| 1373 |
g_test_dbus_down (bus); |
| 1374 |
g_clear_object (&bus); |
| 1375 |
} |
| 1376 |
|
| 1377 |
static void |
| 1378 |
dbus_activate_noop_cb (GApplication *app, |
| 1379 |
gpointer user_data) |
| 1380 |
{ |
| 1381 |
/* noop */ |
| 1382 |
} |
| 1383 |
|
| 1384 |
static void |
| 1385 |
dbus_open_cb (GApplication *app, |
| 1386 |
gpointer files, |
| 1387 |
int n_files, |
| 1388 |
char *hint, |
| 1389 |
gpointer user_data) |
| 1390 |
{ |
| 1391 |
guint *n_opens = user_data; |
| 1392 |
|
| 1393 |
*n_opens = *n_opens + 1; |
| 1394 |
g_main_context_wakeup (NULL); |
| 1395 |
} |
| 1396 |
|
| 1397 |
static void |
| 1398 |
test_dbus_open (void) |
| 1399 |
{ |
| 1400 |
GTestDBus *bus = NULL; |
| 1401 |
GVariantBuilder builder, builder2; |
| 1402 |
GDBusMessage *message = NULL; |
| 1403 |
GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */ |
| 1404 |
gsize i; |
| 1405 |
|
| 1406 |
g_test_summary ("Test that calling the Open D-Bus method works"); |
| 1407 |
|
| 1408 |
/* Try various different messages */ |
| 1409 |
messages = g_ptr_array_new_with_free_func (g_object_unref); |
| 1410 |
|
| 1411 |
/* Via org.gtk.Application */ |
| 1412 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); |
| 1413 |
g_variant_builder_add (&builder, "s", "file:///home/henry/test"); |
| 1414 |
|
| 1415 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open", |
| 1416 |
"/org/gtk/TestApplication/Open", |
| 1417 |
"org.gtk.Application", |
| 1418 |
"Open"); |
| 1419 |
g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", &builder, "hint", NULL)); |
| 1420 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1421 |
|
| 1422 |
/* Via org.freedesktop.Application (which has no hint parameter) */ |
| 1423 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); |
| 1424 |
g_variant_builder_add (&builder, "s", "file:///home/henry/test"); |
| 1425 |
|
| 1426 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open", |
| 1427 |
"/org/gtk/TestApplication/Open", |
| 1428 |
"org.freedesktop.Application", |
| 1429 |
"Open"); |
| 1430 |
g_dbus_message_set_body (message, g_variant_new ("(asa{sv})", &builder, NULL)); |
| 1431 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1432 |
|
| 1433 |
/* With some platform data and more than one file */ |
| 1434 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); |
| 1435 |
g_variant_builder_add (&builder, "s", "file:///home/henry/test"); |
| 1436 |
g_variant_builder_add (&builder, "s", "file:///home/henry/test2"); |
| 1437 |
|
| 1438 |
g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}")); |
| 1439 |
g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_bytestring ("/home/henry")); |
| 1440 |
|
| 1441 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open", |
| 1442 |
"/org/gtk/TestApplication/Open", |
| 1443 |
"org.gtk.Application", |
| 1444 |
"Open"); |
| 1445 |
g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", &builder, "", &builder2)); |
| 1446 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1447 |
|
| 1448 |
/* No files */ |
| 1449 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open", |
| 1450 |
"/org/gtk/TestApplication/Open", |
| 1451 |
"org.gtk.Application", |
| 1452 |
"Open"); |
| 1453 |
g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", NULL, "", NULL)); |
| 1454 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1455 |
|
| 1456 |
/* Try each message */ |
| 1457 |
bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 1458 |
g_test_dbus_up (bus); |
| 1459 |
|
| 1460 |
for (i = 0; i < messages->len; i++) |
| 1461 |
{ |
| 1462 |
GApplication *app = NULL; |
| 1463 |
gulong activate_id, open_id, startup_id; |
| 1464 |
guint n_opens = 0; |
| 1465 |
|
| 1466 |
g_test_message ("Message %" G_GSIZE_FORMAT, i); |
| 1467 |
|
| 1468 |
app = g_application_new ("org.gtk.TestApplication.Open", G_APPLICATION_HANDLES_OPEN); |
| 1469 |
activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL); |
| 1470 |
open_id = g_signal_connect (app, "open", G_CALLBACK (dbus_open_cb), &n_opens); |
| 1471 |
startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]); |
| 1472 |
|
| 1473 |
g_application_hold (app); |
| 1474 |
g_application_run (app, 0, NULL); |
| 1475 |
|
| 1476 |
g_assert_cmpuint (n_opens, ==, 1); |
| 1477 |
|
| 1478 |
g_signal_handler_disconnect (app, startup_id); |
| 1479 |
g_signal_handler_disconnect (app, open_id); |
| 1480 |
g_signal_handler_disconnect (app, activate_id); |
| 1481 |
g_clear_object (&app); |
| 1482 |
} |
| 1483 |
|
| 1484 |
g_ptr_array_unref (messages); |
| 1485 |
|
| 1486 |
g_test_dbus_down (bus); |
| 1487 |
g_clear_object (&bus); |
| 1488 |
} |
| 1489 |
|
| 1490 |
static void |
| 1491 |
dbus_command_line_cb (GApplication *app, |
| 1492 |
GApplicationCommandLine *command_line, |
| 1493 |
gpointer user_data) |
| 1494 |
{ |
| 1495 |
guint *n_command_lines = user_data; |
| 1496 |
|
| 1497 |
*n_command_lines = *n_command_lines + 1; |
| 1498 |
g_main_context_wakeup (NULL); |
| 1499 |
} |
| 1500 |
|
| 1501 |
static void |
| 1502 |
test_dbus_command_line (void) |
| 1503 |
{ |
| 1504 |
GTestDBus *bus = NULL; |
| 1505 |
GVariantBuilder builder, builder2; |
| 1506 |
GDBusMessage *message = NULL; |
| 1507 |
GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */ |
| 1508 |
gsize i; |
| 1509 |
|
| 1510 |
g_test_summary ("Test that calling the CommandLine D-Bus method works"); |
| 1511 |
|
| 1512 |
/* Try various different messages */ |
| 1513 |
messages = g_ptr_array_new_with_free_func (g_object_unref); |
| 1514 |
|
| 1515 |
/* Via org.gtk.Application */ |
| 1516 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay")); |
| 1517 |
g_variant_builder_add (&builder, "^ay", "test-program"); |
| 1518 |
g_variant_builder_add (&builder, "^ay", "--open"); |
| 1519 |
g_variant_builder_add (&builder, "^ay", "/path/to/something"); |
| 1520 |
|
| 1521 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine", |
| 1522 |
"/org/gtk/TestApplication/CommandLine", |
| 1523 |
"org.gtk.Application", |
| 1524 |
"CommandLine"); |
| 1525 |
g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})", |
| 1526 |
"/my/org/gtk/private/CommandLine", |
| 1527 |
&builder, NULL)); |
| 1528 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1529 |
|
| 1530 |
/* With platform data */ |
| 1531 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay")); |
| 1532 |
g_variant_builder_add (&builder, "^ay", "test-program"); |
| 1533 |
g_variant_builder_add (&builder, "^ay", "--open"); |
| 1534 |
g_variant_builder_add (&builder, "^ay", "/path/to/something"); |
| 1535 |
|
| 1536 |
g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}")); |
| 1537 |
g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_bytestring ("/home")); |
| 1538 |
g_variant_builder_add_parsed (&builder2, "{'environ', <@aay [ b'HOME=/home/bloop', b'PATH=/blah']>}"); |
| 1539 |
g_variant_builder_add_parsed (&builder2, "{'options', <{'a': <@u 32>, 'b': <'bloop'>}>}"); |
| 1540 |
|
| 1541 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine", |
| 1542 |
"/org/gtk/TestApplication/CommandLine", |
| 1543 |
"org.gtk.Application", |
| 1544 |
"CommandLine"); |
| 1545 |
g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})", |
| 1546 |
"/my/org/gtk/private/CommandLine", |
| 1547 |
&builder, &builder2)); |
| 1548 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1549 |
|
| 1550 |
/* With invalid typed platform data */ |
| 1551 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay")); |
| 1552 |
g_variant_builder_add (&builder, "^ay", "test-program"); |
| 1553 |
g_variant_builder_add (&builder, "^ay", "--open"); |
| 1554 |
g_variant_builder_add (&builder, "^ay", "/path/to/something"); |
| 1555 |
|
| 1556 |
g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}")); |
| 1557 |
g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_string ("/home should be a bytestring")); |
| 1558 |
g_variant_builder_add_parsed (&builder2, "{'environ', <['HOME=should be a bytestring', 'PATH=this also']>}"); |
| 1559 |
g_variant_builder_add_parsed (&builder2, "{'options', <['should be a', 'dict']>}"); |
| 1560 |
|
| 1561 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine", |
| 1562 |
"/org/gtk/TestApplication/CommandLine", |
| 1563 |
"org.gtk.Application", |
| 1564 |
"CommandLine"); |
| 1565 |
g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})", |
| 1566 |
"/my/org/gtk/private/CommandLine", |
| 1567 |
&builder, &builder2)); |
| 1568 |
g_ptr_array_add (messages, g_steal_pointer (&message)); |
| 1569 |
|
| 1570 |
/* Try each message */ |
| 1571 |
bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 1572 |
g_test_dbus_up (bus); |
| 1573 |
|
| 1574 |
for (i = 0; i < messages->len; i++) |
| 1575 |
{ |
| 1576 |
GApplication *app = NULL; |
| 1577 |
gulong activate_id, command_line_id, startup_id; |
| 1578 |
guint n_command_lines = 0; |
| 1579 |
|
| 1580 |
g_test_message ("Message %" G_GSIZE_FORMAT, i); |
| 1581 |
|
| 1582 |
app = g_application_new ("org.gtk.TestApplication.CommandLine", G_APPLICATION_HANDLES_COMMAND_LINE); |
| 1583 |
activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL); |
| 1584 |
command_line_id = g_signal_connect (app, "command-line", G_CALLBACK (dbus_command_line_cb), &n_command_lines); |
| 1585 |
startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]); |
| 1586 |
|
| 1587 |
g_application_hold (app); |
| 1588 |
g_application_run (app, 0, NULL); |
| 1589 |
|
| 1590 |
/* It’s called once for handling the local command line on startup, and again |
| 1591 |
* for the D-Bus call */ |
| 1592 |
g_assert_cmpuint (n_command_lines, ==, 2); |
| 1593 |
|
| 1594 |
g_signal_handler_disconnect (app, startup_id); |
| 1595 |
g_signal_handler_disconnect (app, command_line_id); |
| 1596 |
g_signal_handler_disconnect (app, activate_id); |
| 1597 |
g_clear_object (&app); |
| 1598 |
} |
| 1599 |
|
| 1600 |
g_ptr_array_unref (messages); |
| 1601 |
|
| 1602 |
g_test_dbus_down (bus); |
| 1603 |
g_clear_object (&bus); |
| 1604 |
} |
| 1605 |
|
| 1606 |
static gint |
| 1607 |
dbus_command_line_done_cb (GApplication *app, |
| 1608 |
GApplicationCommandLine *command_line, |
| 1609 |
gpointer user_data) |
| 1610 |
{ |
| 1611 |
guint *n_command_lines = user_data; |
| 1612 |
|
| 1613 |
*n_command_lines = *n_command_lines + 1; |
| 1614 |
|
| 1615 |
if (*n_command_lines == 1) |
| 1616 |
return 0; |
| 1617 |
|
| 1618 |
g_object_set_data_full (G_OBJECT (app), "command-line", g_object_ref (command_line), g_object_unref); |
| 1619 |
|
| 1620 |
g_application_command_line_set_exit_status (command_line, 42); |
| 1621 |
g_application_command_line_done (command_line); |
| 1622 |
|
| 1623 |
return 1; /* ignored - after g_application_command_line_done () */ |
| 1624 |
} |
| 1625 |
|
| 1626 |
static void |
| 1627 |
test_dbus_command_line_done (void) |
| 1628 |
{ |
| 1629 |
GTestDBus *bus = NULL; |
| 1630 |
GVariantBuilder builder; |
| 1631 |
GDBusMessage *message = NULL; |
| 1632 |
GDBusMessage *reply = NULL; |
| 1633 |
GApplication *app = NULL; |
| 1634 |
guint n_command_lines = 0; |
| 1635 |
gint exit_status; |
| 1636 |
|
| 1637 |
g_test_summary ("Test that GDBusCommandLine.done() works"); |
| 1638 |
|
| 1639 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay")); |
| 1640 |
g_variant_builder_add (&builder, "^ay", "test-program"); |
| 1641 |
g_variant_builder_add (&builder, "^ay", "/path/to/something"); |
| 1642 |
|
| 1643 |
message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine", |
| 1644 |
"/org/gtk/TestApplication/CommandLine", |
| 1645 |
"org.gtk.Application", |
| 1646 |
"CommandLine"); |
| 1647 |
g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})", |
| 1648 |
"/my/org/gtk/private/CommandLine", |
| 1649 |
&builder, NULL)); |
| 1650 |
|
| 1651 |
bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 1652 |
g_test_dbus_up (bus); |
| 1653 |
|
| 1654 |
app = g_application_new ("org.gtk.TestApplication.CommandLine", G_APPLICATION_HANDLES_COMMAND_LINE); |
| 1655 |
g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL); |
| 1656 |
g_signal_connect (app, "command-line", G_CALLBACK (dbus_command_line_done_cb), &n_command_lines); |
| 1657 |
g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), message); |
| 1658 |
|
| 1659 |
g_application_hold (app); |
| 1660 |
exit_status = g_application_run (app, 0, NULL); |
| 1661 |
|
| 1662 |
g_assert_cmpuint (n_command_lines, ==, 2); |
| 1663 |
g_assert_cmpint (exit_status, ==, 0); |
| 1664 |
|
| 1665 |
reply = g_object_get_data (G_OBJECT (app), "dbus-command-line-reply"); |
| 1666 |
g_variant_get (g_dbus_message_get_body (reply), "(i)", &exit_status); |
| 1667 |
g_assert_cmpint (exit_status, ==, 42); |
| 1668 |
|
| 1669 |
g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_activate_noop_cb), NULL); |
| 1670 |
g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_command_line_done_cb), &n_command_lines); |
| 1671 |
g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_startup_cb), message); |
| 1672 |
|
| 1673 |
g_clear_object (&app); |
| 1674 |
g_clear_object (&message); |
| 1675 |
|
| 1676 |
g_test_dbus_down (bus); |
| 1677 |
g_clear_object (&bus); |
| 1678 |
} |
| 1679 |
|
| 1680 |
static void |
| 1681 |
dbus_activate_action_cb (GSimpleAction *action, |
| 1682 |
GVariant *parameter, |
| 1683 |
gpointer user_data) |
| 1684 |
{ |
| 1685 |
guint *n_activations = user_data; |
| 1686 |
|
| 1687 |
*n_activations = *n_activations + 1; |
| 1688 |
g_main_context_wakeup (NULL); |
| 1689 |
} |
| 1690 |
|
| 1691 |
static void |
| 1692 |
test_dbus_activate_action (void) |
| 1693 |
{ |
| 1694 |
GTestDBus *bus = NULL; |
| 1695 |
GVariantBuilder builder; |
| 1696 |
struct |
| 1697 |
{ |
| 1698 |
GDBusMessage *message; /* (not nullable) (owned) */ |
| 1699 |
guint n_expected_activations; |
| 1700 |
} messages[6]; |
| 1701 |
gsize i; |
| 1702 |
|
| 1703 |
g_test_summary ("Test that calling the ActivateAction D-Bus method works"); |
| 1704 |
|
| 1705 |
/* Action without parameter */ |
| 1706 |
messages[0].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1707 |
"/org/gtk/TestApplication/ActivateAction", |
| 1708 |
"org.freedesktop.Application", |
| 1709 |
"ActivateAction"); |
| 1710 |
g_dbus_message_set_body (messages[0].message, g_variant_new ("(sava{sv})", "undo", NULL, NULL)); |
| 1711 |
messages[0].n_expected_activations = 1; |
| 1712 |
|
| 1713 |
/* Action with parameter */ |
| 1714 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("av")); |
| 1715 |
g_variant_builder_add (&builder, "v", g_variant_new_string ("spanish")); |
| 1716 |
|
| 1717 |
messages[1].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1718 |
"/org/gtk/TestApplication/ActivateAction", |
| 1719 |
"org.freedesktop.Application", |
| 1720 |
"ActivateAction"); |
| 1721 |
g_dbus_message_set_body (messages[1].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL)); |
| 1722 |
messages[1].n_expected_activations = 1; |
| 1723 |
|
| 1724 |
/* Action with unexpected parameter */ |
| 1725 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("av")); |
| 1726 |
g_variant_builder_add (&builder, "v", g_variant_new_string ("should not be passed")); |
| 1727 |
|
| 1728 |
messages[2].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1729 |
"/org/gtk/TestApplication/ActivateAction", |
| 1730 |
"org.freedesktop.Application", |
| 1731 |
"ActivateAction"); |
| 1732 |
g_dbus_message_set_body (messages[2].message, g_variant_new ("(sava{sv})", "undo", &builder, NULL)); |
| 1733 |
messages[2].n_expected_activations = 0; |
| 1734 |
|
| 1735 |
/* Action without required parameter */ |
| 1736 |
messages[3].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1737 |
"/org/gtk/TestApplication/ActivateAction", |
| 1738 |
"org.freedesktop.Application", |
| 1739 |
"ActivateAction"); |
| 1740 |
g_dbus_message_set_body (messages[3].message, g_variant_new ("(sava{sv})", "lang", NULL, NULL)); |
| 1741 |
messages[3].n_expected_activations = 0; |
| 1742 |
|
| 1743 |
/* Action with wrong parameter type */ |
| 1744 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("av")); |
| 1745 |
g_variant_builder_add (&builder, "v", g_variant_new_uint32 (42)); |
| 1746 |
|
| 1747 |
messages[4].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1748 |
"/org/gtk/TestApplication/ActivateAction", |
| 1749 |
"org.freedesktop.Application", |
| 1750 |
"ActivateAction"); |
| 1751 |
g_dbus_message_set_body (messages[4].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL)); |
| 1752 |
messages[4].n_expected_activations = 0; |
| 1753 |
|
| 1754 |
/* Nonexistent action */ |
| 1755 |
messages[5].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction", |
| 1756 |
"/org/gtk/TestApplication/ActivateAction", |
| 1757 |
"org.freedesktop.Application", |
| 1758 |
"ActivateAction"); |
| 1759 |
g_dbus_message_set_body (messages[5].message, g_variant_new ("(sava{sv})", "nonexistent", NULL, NULL)); |
| 1760 |
messages[5].n_expected_activations = 0; |
| 1761 |
|
| 1762 |
/* Try each message */ |
| 1763 |
bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 1764 |
g_test_dbus_up (bus); |
| 1765 |
|
| 1766 |
for (i = 0; i < G_N_ELEMENTS (messages); i++) |
| 1767 |
{ |
| 1768 |
GApplication *app = NULL; |
| 1769 |
gulong activate_id, startup_id; |
| 1770 |
const GActionEntry entries[] = |
| 1771 |
{ |
| 1772 |
{ "undo", dbus_activate_action_cb, NULL, NULL, NULL, { 0 } }, |
| 1773 |
{ "lang", dbus_activate_action_cb, "s", "'latin'", NULL, { 0 } }, |
| 1774 |
}; |
| 1775 |
guint n_activations = 0; |
| 1776 |
|
| 1777 |
g_test_message ("Message %" G_GSIZE_FORMAT, i); |
| 1778 |
|
| 1779 |
app = g_application_new ("org.gtk.TestApplication.ActivateAction", G_APPLICATION_DEFAULT_FLAGS); |
| 1780 |
activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL); |
| 1781 |
startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages[i].message); |
| 1782 |
|
| 1783 |
/* Export some actions. */ |
| 1784 |
g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), &n_activations); |
| 1785 |
|
| 1786 |
g_application_hold (app); |
| 1787 |
g_application_run (app, 0, NULL); |
| 1788 |
|
| 1789 |
g_assert_cmpuint (n_activations, ==, messages[i].n_expected_activations); |
| 1790 |
|
| 1791 |
g_signal_handler_disconnect (app, startup_id); |
| 1792 |
g_signal_handler_disconnect (app, activate_id); |
| 1793 |
g_clear_object (&app); |
| 1794 |
g_clear_object (&messages[i].message); |
| 1795 |
} |
| 1796 |
|
| 1797 |
g_test_dbus_down (bus); |
| 1798 |
g_clear_object (&bus); |
| 1799 |
} |
| 1800 |
|
| 1801 |
int |
| 1802 |
main (int argc, char **argv) |
| 1803 |
{ |
| 1804 |
g_setenv ("LC_ALL", "C", TRUE); |
| 1805 |
|
| 1806 |
g_log_writer_default_set_use_stderr (TRUE); |
| 1807 |
|
| 1808 |
g_test_init (&argc, &argv, NULL); |
| 1809 |
|
| 1810 |
if (!g_test_subprocess ()) |
| 1811 |
g_test_dbus_unset (); |
| 1812 |
|
| 1813 |
g_test_add_func ("/gapplication/no-dbus", test_nodbus); |
| 1814 |
/* g_test_add_func ("/gapplication/basic", basic); */ |
| 1815 |
g_test_add_func ("/gapplication/no-appid", test_noappid); |
| 1816 |
/* g_test_add_func ("/gapplication/non-unique", test_nonunique); */ |
| 1817 |
g_test_add_func ("/gapplication/properties", properties); |
| 1818 |
g_test_add_func ("/gapplication/app-id", appid); |
| 1819 |
g_test_add_func ("/gapplication/quit", test_quit); |
| 1820 |
g_test_add_func ("/gapplication/registered", test_registered); |
| 1821 |
g_test_add_func ("/gapplication/local-actions", test_local_actions); |
| 1822 |
/* g_test_add_func ("/gapplication/remote-actions", test_remote_actions); */ |
| 1823 |
g_test_add_func ("/gapplication/local-command-line", test_local_command_line); |
| 1824 |
/* g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line); */ |
| 1825 |
g_test_add_func ("/gapplication/resource-path", test_resource_path); |
| 1826 |
g_test_add_func ("/gapplication/test-help", test_help); |
| 1827 |
g_test_add_func ("/gapplication/command-line-done", test_command_line_done); |
| 1828 |
g_test_add_func ("/gapplication/test-busy", test_busy); |
| 1829 |
g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success); |
| 1830 |
g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure); |
| 1831 |
g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough); |
| 1832 |
g_test_add_func ("/gapplication/api", test_api); |
| 1833 |
g_test_add_data_func ("/gapplication/replace", GINT_TO_POINTER (TRUE), test_replace); |
| 1834 |
g_test_add_data_func ("/gapplication/no-replace", GINT_TO_POINTER (FALSE), test_replace); |
| 1835 |
g_test_add_func ("/gapplication/dbus/activate", test_dbus_activate); |
| 1836 |
g_test_add_func ("/gapplication/dbus/open", test_dbus_open); |
| 1837 |
g_test_add_func ("/gapplication/dbus/command-line", test_dbus_command_line); |
| 1838 |
g_test_add_func ("/gapplication/dbus/command-line-done", test_dbus_command_line_done); |
| 1839 |
g_test_add_func ("/gapplication/dbus/activate-action", test_dbus_activate_action); |
| 1840 |
|
| 1841 |
return g_test_run (); |
| 1842 |
} |