File: test-calls-manager.c

package info (click to toggle)
phosh 0.53.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,936 kB
  • sloc: ansic: 83,383; xml: 3,981; python: 717; sh: 449; makefile: 34; lisp: 22; javascript: 6
file content (212 lines) | stat: -rw-r--r-- 6,054 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
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2021 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#include "shell-priv.h"

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

#define BUS_NAME "org.gnome.Calls"
#define OBJECT_PATH "/org/gnome/Calls"

#define POP_TIMEOUT 50000000

typedef struct {
  GDBusObjectManagerServer *object_manager;
  gboolean bus_acquired;
  guint    bus_id;
} CallsMock;


static void
on_bus_acquired (GDBusConnection *connection,
                 const char      *name,
                 gpointer         user_data)
{
  CallsMock * mock = user_data;

  g_test_message ("Owned %s, creating object manager at %s", name, OBJECT_PATH);

  mock->object_manager = g_dbus_object_manager_server_new (OBJECT_PATH);
  g_dbus_object_manager_server_set_connection (mock->object_manager, connection);
  mock->bus_acquired = TRUE;
}


static void
calls_mock_dispose (CallsMock *self)
{
  g_clear_handle_id (&self->bus_id,  g_bus_unown_name);
  g_clear_object (&self->object_manager);
  g_free (self);
}


static CallsMock *
calls_mock_new (void)
{
  return g_new0 (CallsMock, 1);
}


static void
calls_mock_export (CallsMock *mock)
{
  mock->bus_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                                 BUS_NAME,
                                 G_BUS_NAME_OWNER_FLAGS_NONE,
                                 on_bus_acquired,
                                 NULL,
                                 NULL,
                                 mock,
                                 NULL);
  g_assert_true (mock->bus_id > 0);
}


/* t1: test_phosh_calls_present */

static void
t1_on_calls_manager_present (PhoshCallsManager *manager,
                             GParamSpec        *pspec,
                             gpointer           user_data)
{
  PhoshCallsManager *cm = phosh_shell_get_calls_manager (phosh_shell_get_default ());
  gboolean present;

  g_assert_true (PHOSH_IS_CALLS_MANAGER (manager));

  present = phosh_calls_manager_get_present (cm);
  g_test_message ("Object manager present: %d", present);
  g_assert_true (present);

  g_main_loop_quit ((GMainLoop *) user_data);
}


static void
test_phosh_calls_present (PhoshTestFullShellFixture *fixture, gconstpointer unused)
{
  guint notify_id;
  CallsMock *mock;
  g_autoptr (GMainLoop) loop = NULL;
  PhoshCallsManager *cm;

  /* Wait until comp/shell are up */
  g_assert_nonnull (g_async_queue_timeout_pop (fixture->queue, POP_TIMEOUT));

  loop = g_main_loop_new (NULL, FALSE);
  cm = phosh_shell_get_calls_manager (phosh_shell_get_default ());
  notify_id = g_signal_connect (cm,
                                "notify::present",
                                G_CALLBACK (t1_on_calls_manager_present),
                                loop);

  mock = calls_mock_new ();
  calls_mock_export (mock);
  /* Run mainloop, main loop will be quit in notify::present callback */
  g_main_loop_run (loop);
  g_assert_true (mock->bus_acquired);
  g_signal_handler_disconnect (cm, notify_id);

  calls_mock_dispose (mock);
}


/* t2: test_phosh_calls_incoming */

static void
t2_on_active_call_changed (PhoshCallsManager *manager,
                           GParamSpec        *pspec,
                           gpointer           user_data)
{
  g_assert_true (PHOSH_IS_CALLS_MANAGER (manager));

  g_test_message ("Active call state changed");

  g_main_loop_quit ((GMainLoop *) user_data);
}


static void
t2_on_calls_manager_present (PhoshCallsManager *manager,
                             GParamSpec        *pspec,
                             gpointer           user_data)
{
  PhoshCallsManager *cm = phosh_shell_get_calls_manager (phosh_shell_get_default ());
  gboolean present;
  g_autoptr (PhoshDBusObjectSkeleton) object = NULL;
  g_autoptr (PhoshDBusCallsCall) iface = NULL;
  CallsMock *calls_mock = user_data;

  g_assert_true (PHOSH_IS_CALLS_MANAGER (manager));

  present = phosh_calls_manager_get_present (cm);
  g_test_message ("Object manager present: %d", present);
  g_assert_true (present);

  object = phosh_dbus_object_skeleton_new (OBJECT_PATH "/Call/1");
  iface = phosh_dbus_calls_call_skeleton_new ();
  phosh_dbus_calls_call_set_state (iface, PHOSH_CALL_STATE_ACTIVE);
  g_dbus_object_skeleton_add_interface (G_DBUS_OBJECT_SKELETON (object),
                                        G_DBUS_INTERFACE_SKELETON (iface));
  g_dbus_object_manager_server_export (calls_mock->object_manager, G_DBUS_OBJECT_SKELETON (object));
}


static void
test_phosh_calls_incoming (PhoshTestFullShellFixture *fixture, gconstpointer unused)
{
  CallsMock *mock;
  g_autoptr (GMainLoop) loop = NULL;
  PhoshCallsManager *cm;
  guint id_present, id_active;

  /* Wait until comp/shell are up */
  g_assert_nonnull (g_async_queue_timeout_pop (fixture->queue, POP_TIMEOUT));

  loop = g_main_loop_new (NULL, FALSE);
  mock = calls_mock_new ();

  cm = phosh_shell_get_calls_manager (phosh_shell_get_default ());
  id_present = g_signal_connect (cm,
                           "notify::present",
                           G_CALLBACK (t2_on_calls_manager_present),
                           mock);

  id_active = g_signal_connect (cm,
                                "notify::active-call",
                                G_CALLBACK (t2_on_active_call_changed),
                                loop);
  calls_mock_export (mock);

  /* Run mainloop, main loop will be quit in active-call callback */
  g_main_loop_run (loop);
  g_assert_true (mock->bus_acquired);

  g_signal_handler_disconnect (cm, id_present);
  g_signal_handler_disconnect (cm, id_active);

  calls_mock_dispose (mock);
}


int
main (int argc, char *argv[])
{
  g_autoptr (PhoshTestFullShellFixtureCfg) cfg = NULL;

  g_test_init (&argc, &argv, NULL);

  cfg = phosh_test_full_shell_fixture_cfg_new ("phosh-calls-manager");

  PHOSH_FULL_SHELL_TEST_ADD ("/phosh/dbus/calls-manager/present", cfg, test_phosh_calls_present);
  PHOSH_FULL_SHELL_TEST_ADD ("/phosh/dbus/calls-manager/incoming", cfg, test_phosh_calls_incoming);

  return g_test_run ();
}