| 
									
										
										
										
											2023-03-22 14:36:57 +00:00
										 |  |  | /* GLib testing framework examples and tests
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright © 2013 Red Hat, Inc. | 
					
						
							|  |  |  |  * Copyright © 2015, 2017, 2018 Endless Mobile, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Lesser General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							|  |  |  |  * version 2.1 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Lesser General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Lesser General | 
					
						
							|  |  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | #include <gio/gio.h>
 | 
					
						
							|  |  |  | #include <gstdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #if defined (G_OS_UNIX) && !defined (__APPLE__)
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:42:22 +00:00
										 |  |  | #include <gio/gdesktopappinfo.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  | typedef struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gchar *applications_dir; | 
					
						
							|  |  |  | } Fixture; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | setup (Fixture       *fixture, | 
					
						
							|  |  |  |        gconstpointer  user_data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-30 18:09:56 +00:00
										 |  |  |   fixture->applications_dir = g_build_filename (g_get_user_data_dir (), "applications", NULL); | 
					
						
							| 
									
										
										
										
											2020-10-31 12:24:05 +00:00
										 |  |  |   g_assert_no_errno (g_mkdir_with_parents (fixture->applications_dir, 0755)); | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 18:09:56 +00:00
										 |  |  |   g_test_message ("Using data directory: %s", g_get_user_data_dir ()); | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | teardown (Fixture       *fixture, | 
					
						
							|  |  |  |           gconstpointer  user_data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-31 12:24:05 +00:00
										 |  |  |   g_assert_no_errno (g_rmdir (fixture->applications_dir)); | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  |   g_clear_pointer (&fixture->applications_dir, g_free); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #if defined (G_OS_UNIX) && !defined (__APPLE__)
 | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05: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-12 19:43:32 -07:00
										 |  |  |   g_file_set_contents (path, contents, -1, &error); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  |   g_assert_no_error (error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return G_SOURCE_REMOVE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-30 17:13:49 +01:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | delete_app (gpointer data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const gchar *path = data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 19:43:32 -07:00
										 |  |  |   g_remove (path); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  | changed_cb (GAppInfoMonitor *monitor, | 
					
						
							|  |  |  |             gpointer         user_data) | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   gboolean *changed_fired = user_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *changed_fired = TRUE; | 
					
						
							|  |  |  |   g_main_context_wakeup (g_main_context_get_thread_default ()); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gboolean | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  | timeout_cb (gpointer user_data) | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   gboolean *timed_out = user_data; | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   g_assert (!timed_out); | 
					
						
							|  |  |  |   *timed_out = TRUE; | 
					
						
							|  |  |  |   g_main_context_wakeup (g_main_context_get_thread_default ()); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return G_SOURCE_REMOVE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #endif  /* defined (G_OS_UNIX) && !defined (__APPLE__) */
 | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  | test_app_monitor (Fixture       *fixture, | 
					
						
							|  |  |  |                   gconstpointer  user_data) | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #if defined (G_OS_UNIX) && !defined (__APPLE__)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  |   gchar *app_path; | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  |   GAppInfoMonitor *monitor; | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   GMainContext *context = NULL;  /* use the global default main context */ | 
					
						
							|  |  |  |   GSource *timeout_source = NULL; | 
					
						
							| 
									
										
										
										
											2023-03-22 14:42:22 +00:00
										 |  |  |   GDesktopAppInfo *app = NULL; | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   gboolean changed_fired = FALSE; | 
					
						
							|  |  |  |   gboolean timed_out = FALSE; | 
					
						
							| 
									
										
										
										
											2022-01-22 02:32:28 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  |   app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL); | 
					
						
							| 
									
										
										
										
											2015-05-12 19:43:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  |   /* FIXME: this shouldn't be required */ | 
					
						
							|  |  |  |   g_list_free_full (g_app_info_get_all (), g_object_unref); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:58:02 +00:00
										 |  |  |   /* Create an app monitor and check that its ::changed signal is emitted when
 | 
					
						
							|  |  |  |    * a new app is installed. */ | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  |   monitor = g_app_info_monitor_get (); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), &changed_fired); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 19:43:32 -07:00
										 |  |  |   g_idle_add (create_app, app_path); | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   timeout_source = g_timeout_source_new_seconds (3); | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL); | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   g_source_attach (timeout_source, NULL); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   while (!changed_fired && !timed_out) | 
					
						
							|  |  |  |     g_main_context_iteration (context, TRUE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:38:04 +00:00
										 |  |  |   g_assert_true (changed_fired); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  |   changed_fired = FALSE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   g_source_destroy (timeout_source); | 
					
						
							|  |  |  |   g_clear_pointer (&timeout_source, g_source_unref); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:42:22 +00:00
										 |  |  |   /* Check that the app is now queryable. This has the side-effect of re-arming
 | 
					
						
							|  |  |  |    * the #GAppInfoMonitor::changed signal for the next part of the test. */ | 
					
						
							|  |  |  |   app = g_desktop_app_info_new ("app.desktop"); | 
					
						
							|  |  |  |   g_assert_nonnull (app); | 
					
						
							|  |  |  |   g_clear_object (&app); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:58:02 +00:00
										 |  |  |   /* Now check that ::changed is emitted when an app is uninstalled. */ | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   timeout_source = g_timeout_source_new_seconds (3); | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL); | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   g_source_attach (timeout_source, NULL); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 11:29:46 -07:00
										 |  |  |   delete_app (app_path); | 
					
						
							| 
									
										
										
										
											2015-06-30 17:13:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:56:08 +00:00
										 |  |  |   while (!changed_fired && !timed_out) | 
					
						
							|  |  |  |     g_main_context_iteration (context, TRUE); | 
					
						
							| 
									
										
										
										
											2015-06-30 17:13:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:38:04 +00:00
										 |  |  |   g_assert_true (changed_fired); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:50:55 +00:00
										 |  |  |   g_source_destroy (timeout_source); | 
					
						
							|  |  |  |   g_clear_pointer (&timeout_source, g_source_unref); | 
					
						
							| 
									
										
										
										
											2015-05-12 19:43:32 -07:00
										 |  |  |   g_remove (app_path); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   g_object_unref (monitor); | 
					
						
							| 
									
										
										
										
											2015-06-23 06:55:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 19:43:32 -07:00
										 |  |  |   g_free (app_path); | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #elif defined (__APPLE__)
 | 
					
						
							|  |  |  |   g_test_skip (".desktop monitor on macos"); | 
					
						
							|  |  |  | #else  /* if !(defined (G_OS_UNIX) && !defined (__APPLE__)) */
 | 
					
						
							| 
									
										
										
										
											2023-03-22 14:42:22 +00:00
										 |  |  |   g_test_skip (".desktop monitor on win32"); | 
					
						
							| 
									
										
										
										
											2023-04-20 15:57:17 +02:00
										 |  |  | #endif  /* !(defined (G_OS_UNIX) && !defined (__APPLE__)) */
 | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | main (int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-30 18:09:56 +00:00
										 |  |  |   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 09:53:23 +01:00
										 |  |  |   g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown); | 
					
						
							| 
									
										
										
										
											2013-11-28 00:19:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return g_test_run (); | 
					
						
							|  |  |  | } |