File: test-shell.c

package info (click to toggle)
phosh 0.51.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,620 kB
  • sloc: ansic: 81,727; xml: 3,903; python: 502; sh: 456; makefile: 34; lisp: 22; javascript: 6
file content (203 lines) | stat: -rw-r--r-- 4,605 bytes parent folder | download | duplicates (2)
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
200
201
202
203
/*
 * Copyright (C) 2021 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#include "testlib-compositor.h"

#include "log.h"
#include "shell-priv.h"
#include "background-manager.h"
#include "phosh-wayland.h"
#include "wall-clock.h"

#include <glib.h>
#include <handy.h>


static void
compositor_setup (PhoshTestCompositorFixture *fixture, gconstpointer unused)
{
  g_setenv ("WLR_HEADLESS_OUTPUTS", "1", TRUE);

  phosh_test_compositor_setup (fixture, NULL);
}


static void
compositor_setup_dualhead (PhoshTestCompositorFixture *fixture, gconstpointer unused)
{
  g_setenv ("WLR_HEADLESS_OUTPUTS", "2", TRUE);

  phosh_test_compositor_setup (fixture, NULL);
}


static void
compositor_teardown (PhoshTestCompositorFixture *fixture, gconstpointer unused)
{
  phosh_test_compositor_teardown (fixture, NULL);
  g_unsetenv("WLR_HEADLESS_OUTPUTS");
}


static gboolean
on_idle (gpointer data)
{
  gtk_main_quit ();

  *(gboolean*)data = TRUE;

  return G_SOURCE_REMOVE;
}


/**
 * phosh_test_get_shell:
 * @saved_flags: Current logging flags
 *
 * Returns:(transfer full): A new shell object
 */
static PhoshShell *
phosh_test_get_shell (GLogLevelFlags *saved_flags)
{
  PhoshShell *shell;
  GLogLevelFlags flags;

  gtk_init (NULL, NULL);
  hdy_init ();

  phosh_log_set_log_domains ("all");

  /* 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 = phosh_shell_new ();
  g_assert_true (PHOSH_IS_SHELL (shell));

  g_assert_false (phosh_shell_get_locked (shell));
  g_assert_false (phosh_shell_is_startup_finished (shell));
  g_assert_true (PHOSH_IS_MONITOR (phosh_shell_get_primary_monitor (shell)));

  if (saved_flags)
    *saved_flags = flags;

  return shell;
}


/**
 * phosh_test_drain_events:
 *
 * Start a new default main loop and drain events. This can
 * be useful to detect lingering async handlers.
 */
static void
phosh_test_drain_events (void)
{
  GMainLoop *loop;

  /* Process all pending events to spot missing cleanups */
  loop = g_main_loop_new (NULL, FALSE);
  while (g_main_context_pending (NULL))
    g_main_context_iteration (NULL, FALSE);
  g_main_loop_unref (loop);
}


static void
on_shell_ready (PhoshShell *shell, gboolean *ready)
{
  *ready = TRUE;
}


static void
test_shell_new (PhoshTestCompositorFixture *fixture, gconstpointer unused)
{
  PhoshShell *shell;
  PhoshMonitorManager *mm;
  GLogLevelFlags flags;
  gboolean success;
  gboolean ready = FALSE;

  shell = phosh_test_get_shell (&flags);
  phosh_shell_set_default (shell);

  g_signal_connect (shell, "ready", G_CALLBACK (on_shell_ready), &ready);

  mm = phosh_shell_get_monitor_manager (shell);
  g_assert_cmpint (phosh_monitor_manager_get_num_monitors (mm), ==, 1);

  g_idle_add (on_idle, &success);
  gtk_main ();

  /* No warnings allowed from here on */
  g_log_set_always_fatal (flags);

  g_assert_true (ready);
  g_assert_true (success);

  g_debug ("Finalizing shell");
  g_assert_finalize_object (shell);

  phosh_test_drain_events ();
}


static void
test_shell_new_two_outputs (PhoshTestCompositorFixture *fixture, gconstpointer unused)
{
  PhoshShell *shell;
  PhoshMonitorManager *mm;
  PhoshBackgroundManager *bm;
  g_autoptr (GList) bgs = NULL;
  GLogLevelFlags flags;
  gboolean success;

  shell = phosh_test_get_shell (&flags);
  phosh_shell_set_default (shell);

  mm = phosh_shell_get_monitor_manager (shell);
  g_assert_cmpint (phosh_monitor_manager_get_num_monitors (mm), ==, 2);

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

  g_idle_add (on_idle, &success);
  gtk_main ();

  g_log_set_always_fatal (flags);
  g_assert_true (success);

  bm = phosh_shell_get_background_manager (shell);
  bgs = phosh_background_manager_get_backgrounds (bm);
  g_assert_cmpint (g_list_length (bgs), ==, 2);

  g_assert_finalize_object (shell);

  phosh_test_drain_events ();
}


int
main (int argc, char *argv[])
{
  g_autoptr (PhoshWallClock) wall_clock = phosh_wall_clock_new ();

  g_test_init (&argc, &argv, NULL);
  phosh_wall_clock_set_default (wall_clock);

  g_test_add ("/phosh/shell/new", PhoshTestCompositorFixture, NULL,
              compositor_setup, test_shell_new, compositor_teardown);
  g_test_add ("/phosh/shell/dualhead", PhoshTestCompositorFixture, NULL,
              compositor_setup_dualhead, test_shell_new_two_outputs, compositor_teardown);

  return g_test_run ();
}