| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "../common/base-test-fixture.h" |
| 10 |
#include <wp/wp.h> |
| 11 |
|
| 12 |
typedef struct { |
| 13 |
WpBaseTestFixture base; |
| 14 |
WpObjectManager *om; |
| 15 |
gboolean disconnected; |
| 16 |
} TestFixture; |
| 17 |
|
| 18 |
static void |
| 19 |
test_core_setup (TestFixture *self, gconstpointer user_data) |
| 20 |
{ |
| 21 |
wp_base_test_fixture_setup (&self->base, WP_BASE_TEST_FLAG_DONT_CONNECT); |
| 22 |
/* remove the "disconnected" handler that fails the test */ |
| 23 |
g_signal_handlers_disconnect_by_data (self->base.core, &self->base); |
| 24 |
self->om = wp_object_manager_new (); |
| 25 |
self->disconnected = FALSE; |
| 26 |
} |
| 27 |
|
| 28 |
static void |
| 29 |
test_core_teardown (TestFixture *self, gconstpointer user_data) |
| 30 |
{ |
| 31 |
g_clear_object (&self->om); |
| 32 |
wp_base_test_fixture_teardown (&self->base); |
| 33 |
} |
| 34 |
|
| 35 |
static void |
| 36 |
expect_connected (WpCore * core, TestFixture * f) |
| 37 |
{ |
| 38 |
g_main_loop_quit (f->base.loop); |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
expect_disconnected (WpCore * core, TestFixture * f) |
| 43 |
{ |
| 44 |
f->disconnected = TRUE; |
| 45 |
g_main_loop_quit (f->base.loop); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
expect_object_added (WpObjectManager *om, WpProxy *proxy, TestFixture *f) |
| 50 |
{ |
| 51 |
g_assert_true (WP_IS_CLIENT (proxy)); |
| 52 |
g_main_loop_quit (f->base.loop); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
test_core_server_disconnected (TestFixture *f, gconstpointer data) |
| 57 |
{ |
| 58 |
g_signal_connect (f->base.core, "connected", |
| 59 |
G_CALLBACK (expect_connected), f); |
| 60 |
g_signal_connect (f->base.core, "disconnected", |
| 61 |
G_CALLBACK (expect_disconnected), f); |
| 62 |
g_signal_connect (f->om, "object-added", |
| 63 |
G_CALLBACK (expect_object_added), f); |
| 64 |
|
| 65 |
wp_object_manager_add_interest (f->om, WP_TYPE_CLIENT, NULL); |
| 66 |
wp_core_install_object_manager (f->base.core, f->om); |
| 67 |
|
| 68 |
/* connect */ |
| 69 |
g_assert_true (wp_core_connect (f->base.core)); |
| 70 |
g_main_loop_run (f->base.loop); |
| 71 |
g_assert_true (wp_core_is_connected (f->base.core)); |
| 72 |
|
| 73 |
/* wait for the object manager to collect the client proxy */ |
| 74 |
g_main_loop_run (f->base.loop); |
| 75 |
g_assert_cmpuint (wp_object_manager_get_n_objects (f->om), ==, 1); |
| 76 |
|
| 77 |
/* destroy the server and wait for the disconnected signal */ |
| 78 |
wp_test_server_teardown (&f->base.server); |
| 79 |
g_main_loop_run (f->base.loop); |
| 80 |
g_assert_true (f->disconnected); |
| 81 |
|
| 82 |
g_assert_false (wp_core_is_connected (f->base.core)); |
| 83 |
g_assert_cmpuint (wp_object_manager_get_n_objects (f->om), ==, 0); |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
test_core_client_disconnected (TestFixture *f, gconstpointer data) |
| 88 |
{ |
| 89 |
g_signal_connect (f->base.core, "connected", |
| 90 |
G_CALLBACK (expect_connected), f); |
| 91 |
g_signal_connect (f->base.core, "disconnected", |
| 92 |
G_CALLBACK (expect_disconnected), f); |
| 93 |
g_signal_connect (f->om, "object-added", |
| 94 |
G_CALLBACK (expect_object_added), f); |
| 95 |
|
| 96 |
wp_object_manager_add_interest (f->om, WP_TYPE_CLIENT, NULL); |
| 97 |
wp_core_install_object_manager (f->base.core, f->om); |
| 98 |
|
| 99 |
/* connect */ |
| 100 |
g_assert_true (wp_core_connect (f->base.core)); |
| 101 |
g_main_loop_run (f->base.loop); |
| 102 |
g_assert_true (wp_core_is_connected (f->base.core)); |
| 103 |
|
| 104 |
/* wait for the object manager to collect the client proxy */ |
| 105 |
g_main_loop_run (f->base.loop); |
| 106 |
g_assert_cmpuint (wp_object_manager_get_n_objects (f->om), ==, 1); |
| 107 |
|
| 108 |
/* disconnect and expect the disconnected signal */ |
| 109 |
wp_core_disconnect (f->base.core); |
| 110 |
g_assert_true (f->disconnected); |
| 111 |
|
| 112 |
g_assert_false (wp_core_is_connected (f->base.core)); |
| 113 |
g_assert_cmpuint (wp_object_manager_get_n_objects (f->om), ==, 0); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
test_core_clone (TestFixture *f, gconstpointer data) |
| 118 |
{ |
| 119 |
g_assert_false (wp_core_is_connected (f->base.core)); |
| 120 |
|
| 121 |
/* clone */ |
| 122 |
g_autoptr (WpCore) clone = wp_core_clone (f->base.core); |
| 123 |
g_assert_nonnull (clone); |
| 124 |
g_assert_false (wp_core_is_connected (clone)); |
| 125 |
|
| 126 |
g_signal_connect (f->base.core, "connected", |
| 127 |
G_CALLBACK (expect_connected), f); |
| 128 |
g_signal_connect (clone, "connected", |
| 129 |
G_CALLBACK (expect_connected), f); |
| 130 |
|
| 131 |
/* connect clone */ |
| 132 |
g_assert_true (wp_core_connect (clone)); |
| 133 |
g_main_loop_run (f->base.loop); |
| 134 |
g_assert_true (wp_core_is_connected (clone)); |
| 135 |
g_assert_false (wp_core_is_connected (f->base.core)); |
| 136 |
|
| 137 |
/* connect core */ |
| 138 |
g_assert_true (wp_core_connect (f->base.core)); |
| 139 |
g_main_loop_run (f->base.loop); |
| 140 |
g_assert_true (wp_core_is_connected (clone)); |
| 141 |
g_assert_true (wp_core_is_connected (f->base.core)); |
| 142 |
|
| 143 |
/* disconnect clone */ |
| 144 |
wp_core_disconnect (clone); |
| 145 |
g_assert_false (wp_core_is_connected (clone)); |
| 146 |
g_assert_true (wp_core_is_connected (f->base.core)); |
| 147 |
|
| 148 |
/* disconnect core */ |
| 149 |
wp_core_disconnect (f->base.core); |
| 150 |
g_assert_false (wp_core_is_connected (f->base.core)); |
| 151 |
g_assert_false (wp_core_is_connected (clone)); |
| 152 |
} |
| 153 |
|
| 154 |
gint |
| 155 |
main (gint argc, gchar *argv[]) |
| 156 |
{ |
| 157 |
g_test_init (&argc, &argv, NULL); |
| 158 |
wp_init (WP_INIT_ALL); |
| 159 |
|
| 160 |
g_test_add ("/wp/core/server-disconnected", TestFixture, NULL, |
| 161 |
test_core_setup, test_core_server_disconnected, test_core_teardown); |
| 162 |
g_test_add ("/wp/core/client-disconnected", TestFixture, NULL, |
| 163 |
test_core_setup, test_core_client_disconnected, test_core_teardown); |
| 164 |
g_test_add ("/wp/core/clone", TestFixture, NULL, |
| 165 |
test_core_setup, test_core_clone, test_core_teardown); |
| 166 |
|
| 167 |
return g_test_run (); |
| 168 |
} |