File: appmonitor.c

package info (click to toggle)
glib2.0 2.86.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 67,012 kB
  • sloc: ansic: 544,596; python: 9,702; sh: 1,612; xml: 1,482; perl: 1,222; cpp: 535; makefile: 321; javascript: 11
file content (180 lines) | stat: -rw-r--r-- 5,092 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* 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/>.
 */

#include <gio/gio.h>
#include <gstdio.h>

#if defined (G_OS_UNIX) && !defined (__APPLE__)
#include <gio/gdesktopappinfo.h>
#endif

typedef struct
{
  gchar *applications_dir;
} Fixture;

static void
setup (Fixture       *fixture,
       gconstpointer  user_data)
{
  fixture->applications_dir = g_build_filename (g_get_user_data_dir (), "applications", NULL);
  g_assert_no_errno (g_mkdir_with_parents (fixture->applications_dir, 0755));

  g_test_message ("Using data directory: %s", g_get_user_data_dir ());
}

static void
teardown (Fixture       *fixture,
          gconstpointer  user_data)
{
  g_assert_no_errno (g_rmdir (fixture->applications_dir));
  g_clear_pointer (&fixture->applications_dir, g_free);
}

#if defined (G_OS_UNIX) && !defined (__APPLE__)
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";

  g_file_set_contents (path, contents, -1, &error);
  g_assert_no_error (error);

  return G_SOURCE_REMOVE;
}

static void
delete_app (gpointer data)
{
  const gchar *path = data;

  g_remove (path);
}

static void
changed_cb (GAppInfoMonitor *monitor,
            gpointer         user_data)
{
  gboolean *changed_fired = user_data;

  *changed_fired = TRUE;
  g_main_context_wakeup (g_main_context_get_thread_default ());
}

static gboolean
timeout_cb (gpointer user_data)
{
  gboolean *timed_out = user_data;

  g_assert (!*timed_out);
  *timed_out = TRUE;
  g_main_context_wakeup (g_main_context_get_thread_default ());

  return G_SOURCE_REMOVE;
}
#endif  /* defined (G_OS_UNIX) && !defined (__APPLE__) */

static void
test_app_monitor (Fixture       *fixture,
                  gconstpointer  user_data)
{
#if defined (G_OS_UNIX) && !defined (__APPLE__)
  gchar *app_path;
  GAppInfoMonitor *monitor;
  GMainContext *context = NULL;  /* use the global default main context */
  GSource *timeout_source = NULL;
  GDesktopAppInfo *app = NULL;
  gboolean changed_fired = FALSE;
  gboolean timed_out = FALSE;

  app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);

  /* FIXME: this shouldn't be required */
  g_list_free_full (g_app_info_get_all (), g_object_unref);

  /* Create an app monitor and check that its ::changed signal is emitted when
   * a new app is installed. */
  monitor = g_app_info_monitor_get ();

  g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), &changed_fired);

  g_idle_add (create_app, app_path);
  timeout_source = g_timeout_source_new_seconds (3);
  g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL);
  g_source_attach (timeout_source, NULL);

  while (!changed_fired && !timed_out)
    g_main_context_iteration (context, TRUE);

  g_assert_true (changed_fired);
  changed_fired = FALSE;

  g_source_destroy (timeout_source);
  g_clear_pointer (&timeout_source, g_source_unref);

  /* 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);

  /* Now check that ::changed is emitted when an app is uninstalled. */
  timeout_source = g_timeout_source_new_seconds (3);
  g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL);
  g_source_attach (timeout_source, NULL);

  delete_app (app_path);

  while (!changed_fired && !timed_out)
    g_main_context_iteration (context, TRUE);

  g_assert_true (changed_fired);

  g_source_destroy (timeout_source);
  g_clear_pointer (&timeout_source, g_source_unref);
  g_remove (app_path);

  g_object_unref (monitor);

  g_free (app_path);
#elif defined (__APPLE__)
  g_test_skip (".desktop monitor on macos");
#else  /* if !(defined (G_OS_UNIX) && !defined (__APPLE__)) */
  g_test_skip (".desktop monitor on win32");
#endif  /* !(defined (G_OS_UNIX) && !defined (__APPLE__)) */
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);

  g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);

  return g_test_run ();
}