1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-12-04 20:07:36 +00:00

tests: Add application shutdown tests

This commit is contained in:
Evangelos Ribeiro Tzaras 2023-01-16 09:19:21 +01:00
parent d901e4c8a6
commit e4bd4580e7
2 changed files with 171 additions and 0 deletions

View file

@ -148,4 +148,26 @@ t = executable('dbus', test_sources, generated_dbus_sources,
)
test('dbus', t, env: test_env)
dbus_run_session = find_program('dbus-run-session')
if dbus_run_session.found ()
test_sources = [ 'test-application.c' ]
t = executable('application', test_sources, calls_resources,
c_args : test_cflags,
link_args: test_link_args,
pie: true,
link_with : [calls_vala, libcalls],
dependencies: calls_deps,
include_directories : [
calls_includes,
]
)
test('application',
dbus_run_session,
args: t.full_path(),
env: test_env,
timeout : 300
)
endif
endif

149
tests/test-application.c Normal file
View file

@ -0,0 +1,149 @@
/*
* Copyright (C) 2023 Purism SPC
*
* SPDX-License-Identifier: GPL-3.0+
*
* Author: Evangelos Ribeiro Tzaras <evangelos.tzaras@puri.sm>
*/
#include "calls-application.h"
#include <glib/gstdio.h>
#include <gtk/gtk.h>
static gboolean
on_idle_quit (gpointer user_data)
{
GApplication *app = user_data;
g_application_quit (app);
return G_SOURCE_REMOVE;
}
static void
test_application_shutdown_daemon (void)
{
CallsApplication *app = calls_application_new ();
char *argv[] = { "test", "--daemon", NULL };
int status;
g_idle_add (on_idle_quit, app);
status = g_application_run (G_APPLICATION (app), 3, argv);
g_assert_cmpint (status, ==, 0);
g_assert_finalize_object (app);
}
static void
test_application_shutdown_no_daemon (void)
{
CallsApplication *app = calls_application_new ();
int status;
g_idle_add (on_idle_quit, app);
status = g_application_run (G_APPLICATION (app), 0, NULL);
g_assert_cmpint (status, ==, 0);
g_assert_finalize_object (app);
}
static void
test_application_shutdown_delayed (void)
{
CallsApplication *app = calls_application_new ();
int status;
g_timeout_add_seconds (5, on_idle_quit, app);
status = g_application_run (G_APPLICATION (app), 0, NULL);
g_assert_cmpint (status, ==, 0);
g_assert_finalize_object (app);
}
static gboolean
on_kill_application (gpointer user_data)
{
gint pid = GPOINTER_TO_INT (user_data);
kill (pid, SIGTERM);
return G_SOURCE_REMOVE;
}
static void
test_application_shutdown_sigterm (void)
{
CallsApplication *app = calls_application_new ();
int status;
int pid = getpid ();
g_idle_add (on_kill_application, GINT_TO_POINTER (pid));
status = g_application_run (G_APPLICATION (app), 0, NULL);
g_assert_cmpint (status, ==, 0);
g_assert_finalize_object (app);
}
static gboolean
dir_contains (const char *dir,
const char *file,
gboolean only_file)
{
g_autoptr (GDir) tmp_dir = g_dir_open (dir, 0, NULL);
const char *tmp_file;
gboolean found = FALSE;
uint n = 0;
while ((tmp_file = g_dir_read_name (tmp_dir))) {
if (g_strcmp0 (tmp_file, file) == 0)
found = TRUE;
n++;
}
if (only_file)
return found && n == 1;
return found;
}
int
main (int argc,
char *argv[])
{
g_autofree char *rec_dir = NULL;
const char *tmp = g_get_tmp_dir ();
int status;
rec_dir = g_build_filename (tmp, "calls-XXXXXX", NULL);
g_assert_nonnull (g_mkdtemp (rec_dir));
g_print ("Setting 'CALLS_RECORD_DIR' to '%s'\n", rec_dir);
g_setenv ("CALLS_RECORD_DIR", rec_dir, TRUE);
gtk_test_init (&argc, &argv, NULL);
g_test_add_func ("/Calls/application/shutdown_daemon", (GTestFunc) test_application_shutdown_daemon);
g_test_add_func ("/Calls/application/shutdown_no_daemon", (GTestFunc) test_application_shutdown_no_daemon);
g_test_add_func ("/Calls/application/shutdown_delayed", (GTestFunc) test_application_shutdown_delayed);
g_test_add_func ("/Calls/application/shutdown_sigterm", (GTestFunc) test_application_shutdown_sigterm);
status = g_test_run ();
/* assert there is no dangling write-ahead-log */
g_assert_true (dir_contains (rec_dir, "records.db", TRUE));
g_rmdir (rec_dir);
return status;
}