File: testlib-full-shell.c

package info (click to toggle)
phog 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,268 kB
  • sloc: ansic: 14,194; xml: 2,542; sh: 78; python: 16; makefile: 10
file content (199 lines) | stat: -rw-r--r-- 4,918 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2020 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#include "testlib.h"
#include "testlib-full-shell.h"

#include "log.h"
#include "shell.h"

#include <handy.h>

GPid comp_pid;


static gboolean
stop_shell (gpointer unused)
{
  g_debug ("Stopping shell");
  gtk_main_quit ();

  return G_SOURCE_REMOVE;
}


static void kill_compositor (int signum)
{
  kill(comp_pid, SIGTERM);
}


static gpointer
phog_test_full_shell_thread (gpointer data)
{
  PhogShell *shell;
  GLogLevelFlags flags;
  PhogTestFullShellFixture *fixture = (PhogTestFullShellFixture *)data;

  /* compositor setup in thread since this invokes gdk already */
  fixture->state = phog_test_compositor_new (FALSE);
  /* We assume only one compositor running at a given time */
  comp_pid = fixture->state->pid;

  signal(SIGTRAP, kill_compositor);

  gtk_init (NULL, NULL);
  hdy_init ();

  phog_log_set_log_domains (fixture->log_domains);

  /* Drop warnings from the fatal log mask since there's plenty
   * when running without recommended DBus services */
  flags = g_log_set_always_fatal (0);
  g_log_set_always_fatal (flags & ~G_LOG_LEVEL_WARNING);

  shell = phog_shell_get_default ();
  g_assert_true (PHOG_IS_SHELL (shell));

  g_assert_false (phog_shell_is_startup_finished (shell));

  /* Process events to startup shell */
  while (g_main_context_pending (NULL))
    g_main_context_iteration (NULL, FALSE);

  g_assert_true (phog_shell_is_startup_finished (shell));

  g_async_queue_push (fixture->queue, (gpointer)TRUE);

  gtk_main ();

  g_assert_finalize_object (shell);
  phog_test_compositor_free (fixture->state);

  /* Process events to tear down compositor */
  while (g_main_context_pending (NULL))
    g_main_context_iteration (NULL, FALSE);

  phog_log_set_log_domains (NULL);
  return NULL;
}

PhogTestFullShellFixtureCfg *
phog_test_full_shell_fixture_cfg_new (const char *display, const char *log_domains)
{
  PhogTestFullShellFixtureCfg *self = g_new0 (PhogTestFullShellFixtureCfg, 1);

  if (display)
    self->display = g_strdup (display);

  if (log_domains)
    self->log_domains = g_strdup (log_domains);

  return self;
}

void
phog_test_full_shell_fixture_cfg_dispose (PhogTestFullShellFixtureCfg *self)
{
  g_clear_pointer (&self->display, g_free);
  g_clear_pointer (&self->log_domains, g_free);

  g_free (self);
}

static void
phog_test_remove_tree (GFile *file)
{
  g_autoptr (GError) err = NULL;
  g_autoptr (GFileEnumerator) enumerator = NULL;

  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME,
                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                          NULL, NULL);

  while (enumerator != NULL) {
    GFile *child;
    gboolean ret;

    ret = g_file_enumerator_iterate (enumerator, NULL, &child, NULL, &err);
    g_assert_no_error (err);
    g_assert_true (ret);

    if (child == NULL)
      break;

    phog_test_remove_tree (child);
  }

  g_assert_true (g_file_delete (file, NULL, &err));
  g_assert_no_error (err);
}

/**
 * phog_test_full_shell_setup:
 * @fixture: Test fixture
 * @data: Data for test setup
 *
 * Sets up a test environment with compositor, own DBus session bus and running shell object. This
 * function is meant to be used with g_test_add().
 */
void
phog_test_full_shell_setup (PhogTestFullShellFixture *fixture, gconstpointer data)
{
  const PhogTestFullShellFixtureCfg *cfg = data;
  g_autoptr (GError) err = NULL;

  fixture->bus = g_test_dbus_new (G_TEST_DBUS_NONE);

  g_test_dbus_up (fixture->bus);

  g_setenv ("NO_AT_BRIDGE", "1", TRUE);

  fixture->tmpdir = g_dir_make_tmp ("phog-test-comp.XXXXXX", &err);
  g_assert_no_error (err);

  g_setenv ("XDG_RUNTIME_DIR", fixture->tmpdir, TRUE);
  /* Display for wlroots X11 backend */
  if (cfg->display)
    g_setenv ("DISPLAY", cfg->display, TRUE);

  if (cfg->log_domains)
    fixture->log_domains = g_strdup (cfg->log_domains);

  /* Run shell in a thread so we can sync call to the DBus interfaces */
  fixture->queue = g_async_queue_new ();
  fixture->comp_and_shell = g_thread_new ("comp-and-shell-thread", phog_test_full_shell_thread, fixture);
}

/**
 * phog_test_full_shell_teardown:
 * @fixture: Test fixture
 * @data: Data for test setup
 *
 * Tears down the test environment that was setup with
 * phog_test_full_shell_setup(). This function is meant to be used
 * with g_test_add().
 */
void
phog_test_full_shell_teardown (PhogTestFullShellFixture *fixture, gconstpointer unused)
{
  g_autoptr (GFile) file = g_file_new_for_path (fixture->tmpdir);

  gdk_threads_add_idle (stop_shell, NULL);
  g_thread_join (fixture->comp_and_shell);
  g_async_queue_unref (fixture->queue);

  g_test_dbus_down (fixture->bus);

  signal (SIGTRAP, SIG_DFL);

  phog_test_remove_tree (file);
  g_free (fixture->tmpdir);

  g_free (fixture->log_domains);
}