2013-11-28 06:19:19 +01:00
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <gstdio.h>
|
|
|
|
|
2017-08-18 10:53:23 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar *applications_dir;
|
|
|
|
} Fixture;
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup (Fixture *fixture,
|
|
|
|
gconstpointer user_data)
|
|
|
|
{
|
2018-11-30 19:09:56 +01:00
|
|
|
fixture->applications_dir = g_build_filename (g_get_user_data_dir (), "applications", NULL);
|
|
|
|
g_assert_cmpint (g_mkdir_with_parents (fixture->applications_dir, 0755), ==, 0);
|
2017-08-18 10:53:23 +02:00
|
|
|
|
2018-11-30 19:09:56 +01:00
|
|
|
g_test_message ("Using data directory: %s", g_get_user_data_dir ());
|
2017-08-18 10:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
teardown (Fixture *fixture,
|
|
|
|
gconstpointer user_data)
|
|
|
|
{
|
|
|
|
g_assert_cmpint (g_rmdir (fixture->applications_dir), ==, 0);
|
|
|
|
g_clear_pointer (&fixture->applications_dir, g_free);
|
|
|
|
}
|
|
|
|
|
2013-11-28 06:19:19 +01:00
|
|
|
static gboolean
|
|
|
|
create_app (gpointer data)
|
|
|
|
{
|
|
|
|
const gchar *path = data;
|
|
|
|
GError *error = NULL;
|
|
|
|
const gchar *contents =
|
|
|
|
"[Desktop Entry]\n"
|
|
|
|
"Name=Application\n"
|
|
|
|
"Version=1.0\n"
|
|
|
|
"Type=Application\n"
|
|
|
|
"Exec=true\n";
|
|
|
|
|
2015-05-13 04:43:32 +02:00
|
|
|
g_file_set_contents (path, contents, -1, &error);
|
2013-11-28 06:19:19 +01:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2015-06-30 18:13:49 +02:00
|
|
|
static void
|
2013-11-28 06:19:19 +01:00
|
|
|
delete_app (gpointer data)
|
|
|
|
{
|
|
|
|
const gchar *path = data;
|
|
|
|
|
2015-05-13 04:43:32 +02:00
|
|
|
g_remove (path);
|
2013-11-28 06:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean changed_fired;
|
|
|
|
|
|
|
|
static void
|
|
|
|
changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop)
|
|
|
|
{
|
|
|
|
changed_fired = TRUE;
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
quit_loop (gpointer data)
|
|
|
|
{
|
|
|
|
GMainLoop *loop = data;
|
|
|
|
|
2015-06-30 18:13:49 +02:00
|
|
|
if (g_main_loop_is_running (loop))
|
|
|
|
g_main_loop_quit (loop);
|
2013-11-28 06:19:19 +01:00
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-08-18 10:53:23 +02:00
|
|
|
test_app_monitor (Fixture *fixture,
|
|
|
|
gconstpointer user_data)
|
2013-11-28 06:19:19 +01:00
|
|
|
{
|
2017-08-18 10:53:23 +02:00
|
|
|
gchar *app_path;
|
2013-11-28 06:19:19 +01:00
|
|
|
GAppInfoMonitor *monitor;
|
|
|
|
GMainLoop *loop;
|
|
|
|
|
2017-08-18 10:53:23 +02:00
|
|
|
app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);
|
2015-05-13 04:43:32 +02:00
|
|
|
|
2013-11-28 06:19:19 +01:00
|
|
|
/* FIXME: this shouldn't be required */
|
|
|
|
g_list_free_full (g_app_info_get_all (), g_object_unref);
|
|
|
|
|
|
|
|
monitor = g_app_info_monitor_get ();
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
|
|
|
|
|
2015-05-13 04:43:32 +02:00
|
|
|
g_idle_add (create_app, app_path);
|
2013-11-28 06:19:19 +01:00
|
|
|
g_timeout_add_seconds (3, quit_loop, loop);
|
|
|
|
|
|
|
|
g_main_loop_run (loop);
|
|
|
|
g_assert (changed_fired);
|
|
|
|
changed_fired = FALSE;
|
|
|
|
|
|
|
|
/* FIXME: this shouldn't be required */
|
|
|
|
g_list_free_full (g_app_info_get_all (), g_object_unref);
|
|
|
|
|
|
|
|
g_timeout_add_seconds (3, quit_loop, loop);
|
|
|
|
|
2016-04-26 20:29:46 +02:00
|
|
|
delete_app (app_path);
|
2015-06-30 18:13:49 +02:00
|
|
|
|
2013-11-28 06:19:19 +01:00
|
|
|
g_main_loop_run (loop);
|
2015-06-30 18:13:49 +02:00
|
|
|
|
2013-11-28 06:19:19 +01:00
|
|
|
g_assert (changed_fired);
|
|
|
|
|
|
|
|
g_main_loop_unref (loop);
|
2015-05-13 04:43:32 +02:00
|
|
|
g_remove (app_path);
|
2013-11-28 06:19:19 +01:00
|
|
|
|
|
|
|
g_object_unref (monitor);
|
2015-06-23 12:55:28 +02:00
|
|
|
|
2015-05-13 04:43:32 +02:00
|
|
|
g_free (app_path);
|
2013-11-28 06:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2018-11-30 19:09:56 +01:00
|
|
|
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
|
2013-11-28 06:19:19 +01:00
|
|
|
|
2017-08-18 10:53:23 +02:00
|
|
|
g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);
|
2013-11-28 06:19:19 +01:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|