| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2022 Collabora Ltd. |
| 4 |
* @author Ashok Sidipotu <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
#include "../common/base-test-fixture.h" |
| 9 |
|
| 10 |
typedef struct { |
| 11 |
WpBaseTestFixture base; |
| 12 |
|
| 13 |
WpProperties *loaded_settings; |
| 14 |
WpProperties *loaded_schema; |
| 15 |
|
| 16 |
WpImplMetadata *metadata; |
| 17 |
WpImplMetadata *metadata_schema; |
| 18 |
WpImplMetadata *metadata_persistent; |
| 19 |
|
| 20 |
WpSettings *settings; |
| 21 |
|
| 22 |
gchar *triggered_setting; |
| 23 |
WpSpaJson *triggered_setting_value; |
| 24 |
gboolean triggered_callback; |
| 25 |
} TestSettingsFixture; |
| 26 |
|
| 27 |
static void |
| 28 |
test_conf_file_setup (TestSettingsFixture *self, gconstpointer user_data) |
| 29 |
{ |
| 30 |
self->base.conf_file = g_strdup_printf ("%s/settings/wireplumber.conf", |
| 31 |
g_getenv ("G_TEST_SRCDIR")); |
| 32 |
|
| 33 |
wp_base_test_fixture_setup (&self->base, WP_BASE_TEST_FLAG_CLIENT_CORE); |
| 34 |
} |
| 35 |
|
| 36 |
static void |
| 37 |
test_conf_file_teardown (TestSettingsFixture *self, gconstpointer user_data) |
| 38 |
{ |
| 39 |
wp_base_test_fixture_teardown (&self->base); |
| 40 |
} |
| 41 |
|
| 42 |
static WpProperties * |
| 43 |
do_parse_section (WpSpaJson *json) |
| 44 |
{ |
| 45 |
g_autoptr (WpProperties) settings = wp_properties_new_empty (); |
| 46 |
g_autoptr (WpIterator) iter = wp_spa_json_new_iterator (json); |
| 47 |
g_auto (GValue) item = G_VALUE_INIT; |
| 48 |
|
| 49 |
if (!wp_spa_json_is_object (json)) |
| 50 |
return NULL; |
| 51 |
|
| 52 |
while (wp_iterator_next (iter, &item)) { |
| 53 |
WpSpaJson *j = g_value_get_boxed (&item); |
| 54 |
g_autofree gchar *name = wp_spa_json_parse_string (j); |
| 55 |
g_autofree gchar *value = NULL; |
| 56 |
|
| 57 |
g_value_unset (&item); |
| 58 |
g_assert_true (wp_iterator_next (iter, &item)); |
| 59 |
j = g_value_get_boxed (&item); |
| 60 |
|
| 61 |
value = wp_spa_json_to_string (j); |
| 62 |
g_value_unset (&item); |
| 63 |
|
| 64 |
if (name && value) |
| 65 |
wp_properties_set (settings, name, value); |
| 66 |
} |
| 67 |
|
| 68 |
return g_steal_pointer (&settings); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data) |
| 73 |
{ |
| 74 |
test_conf_file_setup (self, user_data); |
| 75 |
|
| 76 |
g_autoptr (WpConf) conf = wp_core_get_conf (self->base.core); |
| 77 |
g_assert_nonnull (conf); |
| 78 |
|
| 79 |
{ |
| 80 |
g_autoptr (WpSpaJson) json = wp_conf_get_section (conf, |
| 81 |
"wireplumber.settings"); |
| 82 |
g_assert_nonnull (json); |
| 83 |
self->loaded_settings = do_parse_section (json); |
| 84 |
g_assert_nonnull (self->loaded_settings); |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
g_autoptr (WpSpaJson) json = wp_conf_get_section (conf, |
| 89 |
"wireplumber.settings.schema"); |
| 90 |
self->loaded_schema = do_parse_section (json); |
| 91 |
g_assert_nonnull (self->loaded_schema); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
test_parsing_teardown (TestSettingsFixture *self, gconstpointer user_data) |
| 97 |
{ |
| 98 |
g_clear_pointer (&self->loaded_settings, wp_properties_unref); |
| 99 |
g_clear_pointer (&self->loaded_schema, wp_properties_unref); |
| 100 |
|
| 101 |
test_conf_file_teardown (self, user_data); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
on_metadata_persistent_activated (WpMetadata * m, GAsyncResult * res, |
| 106 |
gpointer user_data) |
| 107 |
{ |
| 108 |
TestSettingsFixture *self = user_data; |
| 109 |
|
| 110 |
g_assert_true (wp_object_activate_finish (WP_OBJECT (m), res, NULL)); |
| 111 |
|
| 112 |
g_main_loop_quit (self->base.loop); |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
on_metadata_schema_activated (WpMetadata * m, GAsyncResult * res, |
| 117 |
gpointer user_data) |
| 118 |
{ |
| 119 |
TestSettingsFixture *self = user_data; |
| 120 |
g_autoptr (WpIterator) it = NULL; |
| 121 |
g_auto (GValue) item = G_VALUE_INIT; |
| 122 |
|
| 123 |
g_assert_true (wp_object_activate_finish (WP_OBJECT (m), res, NULL)); |
| 124 |
|
| 125 |
for (it = wp_properties_new_iterator (self->loaded_schema); |
| 126 |
wp_iterator_next (it, &item); |
| 127 |
g_value_unset (&item)) { |
| 128 |
WpPropertiesItem *pi = g_value_get_boxed (&item); |
| 129 |
const gchar *key = wp_properties_item_get_key (pi); |
| 130 |
const gchar *value = wp_properties_item_get_value (pi); |
| 131 |
|
| 132 |
wp_metadata_set (m, 0, key, "Spa:String:JSON", value); |
| 133 |
} |
| 134 |
|
| 135 |
self->metadata_persistent = wp_impl_metadata_new_full (self->base.core, |
| 136 |
WP_SETTINGS_PERSISTENT_METADATA_NAME_PREFIX "sm-settings", NULL); |
| 137 |
|
| 138 |
wp_object_activate (WP_OBJECT (self->metadata_persistent), |
| 139 |
WP_OBJECT_FEATURES_ALL, |
| 140 |
NULL, |
| 141 |
(GAsyncReadyCallback)on_metadata_persistent_activated, |
| 142 |
self); |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
on_metadata_activated (WpMetadata * m, GAsyncResult * res, gpointer user_data) |
| 147 |
{ |
| 148 |
TestSettingsFixture *self = user_data; |
| 149 |
g_autoptr (WpIterator) it = NULL; |
| 150 |
g_auto (GValue) item = G_VALUE_INIT; |
| 151 |
|
| 152 |
g_assert_true (wp_object_activate_finish (WP_OBJECT (m), res, NULL)); |
| 153 |
|
| 154 |
for (it = wp_properties_new_iterator (self->loaded_settings); |
| 155 |
wp_iterator_next (it, &item); |
| 156 |
g_value_unset (&item)) { |
| 157 |
WpPropertiesItem *pi = g_value_get_boxed (&item); |
| 158 |
const gchar *key = wp_properties_item_get_key (pi); |
| 159 |
const gchar *value = wp_properties_item_get_value (pi); |
| 160 |
|
| 161 |
wp_metadata_set (m, 0, key, "Spa:String:JSON", value); |
| 162 |
} |
| 163 |
|
| 164 |
self->metadata_schema = wp_impl_metadata_new_full (self->base.core, |
| 165 |
WP_SETTINGS_SCHEMA_METADATA_NAME_PREFIX "sm-settings", NULL); |
| 166 |
|
| 167 |
wp_object_activate (WP_OBJECT (self->metadata_schema), |
| 168 |
WP_OBJECT_FEATURES_ALL, |
| 169 |
NULL, |
| 170 |
(GAsyncReadyCallback)on_metadata_schema_activated, |
| 171 |
self); |
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
test_metadata_setup (TestSettingsFixture *self, gconstpointer user_data) |
| 176 |
{ |
| 177 |
test_parsing_setup (self, user_data); |
| 178 |
|
| 179 |
self->metadata = wp_impl_metadata_new_full (self->base.core, "sm-settings", |
| 180 |
NULL); |
| 181 |
|
| 182 |
wp_object_activate (WP_OBJECT (self->metadata), |
| 183 |
WP_OBJECT_FEATURES_ALL, |
| 184 |
NULL, |
| 185 |
(GAsyncReadyCallback)on_metadata_activated, |
| 186 |
self); |
| 187 |
|
| 188 |
g_main_loop_run (self->base.loop); |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
test_metadata_teardown (TestSettingsFixture *self, gconstpointer user_data) |
| 193 |
{ |
| 194 |
test_parsing_teardown (self, user_data); |
| 195 |
|
| 196 |
g_clear_object (&self->metadata); |
| 197 |
g_clear_object (&self->metadata_schema); |
| 198 |
g_clear_object (&self->metadata_persistent); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
on_settings_ready (WpSettings *s, GAsyncResult *res, gpointer data) |
| 203 |
{ |
| 204 |
TestSettingsFixture *self = data; |
| 205 |
|
| 206 |
g_assert_true (wp_object_activate_finish (WP_OBJECT (s), res, NULL)); |
| 207 |
|
| 208 |
wp_core_register_object (self->base.core, g_object_ref (s)); |
| 209 |
|
| 210 |
g_main_loop_quit (self->base.loop); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
test_settings_setup (TestSettingsFixture *self, gconstpointer user_data) |
| 215 |
{ |
| 216 |
test_metadata_setup (self, user_data); |
| 217 |
|
| 218 |
self->settings = wp_settings_new (self->base.core, "sm-settings"); |
| 219 |
wp_object_activate (WP_OBJECT (self->settings), |
| 220 |
WP_OBJECT_FEATURES_ALL, |
| 221 |
NULL, |
| 222 |
(GAsyncReadyCallback)on_settings_ready, |
| 223 |
self); |
| 224 |
g_main_loop_run (self->base.loop); |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
test_settings_teardown (TestSettingsFixture *self, gconstpointer user_data) |
| 229 |
{ |
| 230 |
test_metadata_teardown (self, user_data); |
| 231 |
g_clear_object (&self->settings); |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
test_basic (TestSettingsFixture *self, gconstpointer data) |
| 236 |
{ |
| 237 |
/* Find */ |
| 238 |
{ |
| 239 |
g_autoptr (WpSettings) s1 = |
| 240 |
wp_settings_find (self->base.core, NULL); |
| 241 |
g_autoptr (WpSettings) s2 = |
| 242 |
wp_settings_find (self->base.core, "sm-settings"); |
| 243 |
g_autoptr (WpSettings) s3 = |
| 244 |
wp_settings_find (self->base.core, "blah-blah"); |
| 245 |
|
| 246 |
g_assert_true (self->settings == s1); |
| 247 |
g_assert_true (s1 == s2); |
| 248 |
g_assert_false (s1 == s3); |
| 249 |
g_assert_null (s3); |
| 250 |
|
| 251 |
g_autoptr (WpSettings) s4 = wp_settings_find (self->base.core, NULL); |
| 252 |
g_auto (GValue) value = G_VALUE_INIT; |
| 253 |
g_object_get_property (G_OBJECT(s4), "metadata-name", &value); |
| 254 |
g_assert_cmpstr (g_value_get_string (&value), ==, "sm-settings"); |
| 255 |
} |
| 256 |
|
| 257 |
/* Iterator */ |
| 258 |
{ |
| 259 |
g_autoptr (WpProperties) settings = wp_properties_new_empty (); |
| 260 |
g_autoptr (WpIterator) it = wp_settings_new_iterator (self->settings); |
| 261 |
g_auto (GValue) val = G_VALUE_INIT; |
| 262 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 263 |
WpSettingsItem *si = g_value_get_boxed (&val); |
| 264 |
const gchar *key = wp_settings_item_get_key (si); |
| 265 |
g_autoptr (WpSpaJson) value = wp_settings_item_get_value (si); |
| 266 |
wp_properties_set (settings, key, wp_spa_json_get_data (value)); |
| 267 |
} |
| 268 |
g_assert_true (wp_properties_matches (self->loaded_settings, settings)); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
test_get_set_save_reset_delete (TestSettingsFixture *self, gconstpointer data) |
| 274 |
{ |
| 275 |
WpSettings *s = self->settings; |
| 276 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 277 |
const gchar *desc = NULL; |
| 278 |
const gchar *name = NULL; |
| 279 |
g_autoptr (WpSpaJson) def = NULL; |
| 280 |
g_autoptr (WpSpaJson) min = NULL; |
| 281 |
g_autoptr (WpSpaJson) max = NULL; |
| 282 |
g_autoptr (WpSpaJson) j = NULL; |
| 283 |
|
| 284 |
/* Undefined */ |
| 285 |
{ |
| 286 |
spec = wp_settings_get_spec (s, "test-setting-undefined"); |
| 287 |
g_assert_null (spec); |
| 288 |
|
| 289 |
j = wp_settings_get (s, "test-setting-undefined"); |
| 290 |
g_assert_null (j); |
| 291 |
|
| 292 |
j = wp_spa_json_new_null (); |
| 293 |
g_assert_false (wp_settings_set (s, "test-setting-undefined", j)); |
| 294 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 295 |
} |
| 296 |
|
| 297 |
/* Boolean */ |
| 298 |
{ |
| 299 |
gboolean value = FALSE; |
| 300 |
|
| 301 |
spec = wp_settings_get_spec (s, "test-setting-bool"); |
| 302 |
name = wp_settings_spec_get_name (spec); |
| 303 |
g_assert_nonnull (name); |
| 304 |
g_assert_cmpstr (name, ==, "test-setting-bool name"); |
| 305 |
desc = wp_settings_spec_get_description (spec); |
| 306 |
g_assert_nonnull (desc); |
| 307 |
g_assert_cmpstr (desc, ==, "test-setting-bool description"); |
| 308 |
g_assert_true ( |
| 309 |
wp_settings_spec_get_value_type (spec) == WP_SETTINGS_SPEC_TYPE_BOOL); |
| 310 |
def = wp_settings_spec_get_default_value (spec); |
| 311 |
g_assert_nonnull (def); |
| 312 |
g_assert_true (wp_spa_json_parse_boolean (def, &value)); |
| 313 |
g_assert_false (value); |
| 314 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 315 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 316 |
|
| 317 |
j = wp_settings_get (s, "test-setting-bool"); |
| 318 |
g_assert_nonnull (j); |
| 319 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 320 |
g_assert_true (value); |
| 321 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 322 |
|
| 323 |
j = wp_spa_json_new_boolean (FALSE); |
| 324 |
g_assert_true (wp_settings_set (s, "test-setting-bool", j)); |
| 325 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 326 |
j = wp_settings_get (s, "test-setting-bool"); |
| 327 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 328 |
g_assert_false (value); |
| 329 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 330 |
|
| 331 |
j = wp_spa_json_new_boolean (TRUE); |
| 332 |
g_assert_true (wp_settings_set (s, "test-setting-bool", j)); |
| 333 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 334 |
j = wp_settings_get (s, "test-setting-bool"); |
| 335 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 336 |
g_assert_true (value); |
| 337 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 338 |
|
| 339 |
j = wp_spa_json_new_int (1); |
| 340 |
g_assert_false (wp_settings_set (s, "test-setting-bool", j)); |
| 341 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 342 |
j = wp_settings_get (s, "test-setting-bool"); |
| 343 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 344 |
g_assert_true (value); |
| 345 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 346 |
|
| 347 |
j = wp_settings_get_saved (s, "test-setting-bool"); |
| 348 |
g_assert_null (j); |
| 349 |
g_assert_true (wp_settings_save (s, "test-setting-bool")); |
| 350 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 351 |
j = wp_settings_get_saved (s, "test-setting-bool"); |
| 352 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 353 |
g_assert_true (value); |
| 354 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 355 |
g_assert_true (wp_settings_delete (s, "test-setting-bool")); |
| 356 |
j = wp_settings_get_saved (s, "test-setting-bool"); |
| 357 |
g_assert_null (j); |
| 358 |
g_assert_true (wp_settings_reset (s, "test-setting-bool")); |
| 359 |
j = wp_settings_get (s, "test-setting-bool"); |
| 360 |
g_assert_true (wp_spa_json_parse_boolean (j, &value)); |
| 361 |
g_assert_false (value); |
| 362 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 363 |
} |
| 364 |
|
| 365 |
/* Int */ |
| 366 |
{ |
| 367 |
gint value = 0; |
| 368 |
|
| 369 |
spec = wp_settings_get_spec (s, "test-setting-int"); |
| 370 |
name = wp_settings_spec_get_name (spec); |
| 371 |
g_assert_nonnull (name); |
| 372 |
g_assert_cmpstr (name, ==, "test-setting-int name"); |
| 373 |
desc = wp_settings_spec_get_description (spec); |
| 374 |
g_assert_nonnull (desc); |
| 375 |
g_assert_cmpstr (desc, ==, "test-setting-int description"); |
| 376 |
g_assert_true ( |
| 377 |
wp_settings_spec_get_value_type (spec) == WP_SETTINGS_SPEC_TYPE_INT); |
| 378 |
def = wp_settings_spec_get_default_value (spec); |
| 379 |
min = wp_settings_spec_get_min_value (spec); |
| 380 |
max = wp_settings_spec_get_max_value (spec); |
| 381 |
g_assert_nonnull (def); |
| 382 |
g_assert_nonnull (min); |
| 383 |
g_assert_nonnull (max); |
| 384 |
g_assert_true (wp_spa_json_parse_int (def, &value)); |
| 385 |
g_assert_cmpint (value, ==, 0); |
| 386 |
g_assert_true (wp_spa_json_parse_int (min, &value)); |
| 387 |
g_assert_cmpint (value, ==, -100); |
| 388 |
g_assert_true (wp_spa_json_parse_int (max, &value)); |
| 389 |
g_assert_cmpint (value, ==, 100); |
| 390 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 391 |
g_clear_pointer (&min, wp_spa_json_unref); |
| 392 |
g_clear_pointer (&max, wp_spa_json_unref); |
| 393 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 394 |
|
| 395 |
j = wp_settings_get (s, "test-setting-int"); |
| 396 |
g_assert_nonnull (j); |
| 397 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 398 |
g_assert_cmpint (value, ==, -20); |
| 399 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 400 |
|
| 401 |
j = wp_spa_json_new_int (3); |
| 402 |
g_assert_true (wp_settings_set (s, "test-setting-int", j)); |
| 403 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 404 |
j = wp_settings_get (s, "test-setting-int"); |
| 405 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 406 |
g_assert_cmpint (value, ==, 3); |
| 407 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 408 |
|
| 409 |
j = wp_spa_json_new_int (1000); |
| 410 |
g_assert_false (wp_settings_set (s, "test-setting-int", j)); |
| 411 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 412 |
j = wp_settings_get (s, "test-setting-int"); |
| 413 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 414 |
g_assert_cmpint (value, ==, 3); |
| 415 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 416 |
|
| 417 |
j = wp_spa_json_new_int (-1000); |
| 418 |
g_assert_false (wp_settings_set (s, "test-setting-int", j)); |
| 419 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 420 |
j = wp_settings_get (s, "test-setting-int"); |
| 421 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 422 |
g_assert_cmpint (value, ==, 3); |
| 423 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 424 |
|
| 425 |
j = wp_spa_json_new_float (50.5); |
| 426 |
g_assert_false (wp_settings_set (s, "test-setting-int", j)); |
| 427 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 428 |
j = wp_settings_get (s, "test-setting-int"); |
| 429 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 430 |
g_assert_cmpint (value, ==, 3); |
| 431 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 432 |
|
| 433 |
j = wp_settings_get_saved (s, "test-setting-int"); |
| 434 |
g_assert_null (j); |
| 435 |
g_assert_true (wp_settings_save (s, "test-setting-int")); |
| 436 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 437 |
j = wp_settings_get_saved (s, "test-setting-int"); |
| 438 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 439 |
g_assert_cmpint (value, ==, 3); |
| 440 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 441 |
g_assert_true (wp_settings_delete (s, "test-setting-int")); |
| 442 |
j = wp_settings_get_saved (s, "test-setting-int"); |
| 443 |
g_assert_null (j); |
| 444 |
g_assert_true (wp_settings_reset (s, "test-setting-int")); |
| 445 |
j = wp_settings_get (s, "test-setting-int"); |
| 446 |
g_assert_true (wp_spa_json_parse_int (j, &value)); |
| 447 |
g_assert_cmpint (value, ==, 0); |
| 448 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 449 |
} |
| 450 |
|
| 451 |
/* Float */ |
| 452 |
{ |
| 453 |
gfloat value = 0.0; |
| 454 |
|
| 455 |
spec = wp_settings_get_spec (s, "test-setting-float"); |
| 456 |
name = wp_settings_spec_get_name (spec); |
| 457 |
g_assert_nonnull (name); |
| 458 |
g_assert_cmpstr (name, ==, "test-setting-float name"); |
| 459 |
desc = wp_settings_spec_get_description (spec); |
| 460 |
g_assert_nonnull (desc); |
| 461 |
g_assert_cmpstr (desc, ==, "test-setting-float description"); |
| 462 |
g_assert_true ( |
| 463 |
wp_settings_spec_get_value_type (spec) == WP_SETTINGS_SPEC_TYPE_FLOAT); |
| 464 |
def = wp_settings_spec_get_default_value (spec); |
| 465 |
min = wp_settings_spec_get_min_value (spec); |
| 466 |
max = wp_settings_spec_get_max_value (spec); |
| 467 |
g_assert_nonnull (def); |
| 468 |
g_assert_nonnull (min); |
| 469 |
g_assert_nonnull (max); |
| 470 |
g_assert_true (wp_spa_json_parse_float (def, &value)); |
| 471 |
g_assert_cmpfloat_with_epsilon (value, 0.0, 0.001); |
| 472 |
g_assert_true (wp_spa_json_parse_float (min, &value)); |
| 473 |
g_assert_cmpfloat_with_epsilon (value, -100.0, 0.001); |
| 474 |
g_assert_true (wp_spa_json_parse_float (max, &value)); |
| 475 |
g_assert_cmpfloat_with_epsilon (value, 100.0, 0.001); |
| 476 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 477 |
g_clear_pointer (&min, wp_spa_json_unref); |
| 478 |
g_clear_pointer (&max, wp_spa_json_unref); |
| 479 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 480 |
|
| 481 |
j = wp_settings_get (s, "test-setting-float"); |
| 482 |
g_assert_nonnull (j); |
| 483 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 484 |
g_assert_cmpfloat_with_epsilon (value, 3.14, 0.001); |
| 485 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 486 |
|
| 487 |
j = wp_spa_json_new_float (99.5); |
| 488 |
g_assert_true (wp_settings_set (s, "test-setting-float", j)); |
| 489 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 490 |
j = wp_settings_get (s, "test-setting-float"); |
| 491 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 492 |
g_assert_cmpfloat_with_epsilon (value, 99.5, 0.001); |
| 493 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 494 |
|
| 495 |
j = wp_spa_json_new_float (150.5); |
| 496 |
g_assert_false (wp_settings_set (s, "test-setting-float", j)); |
| 497 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 498 |
j = wp_settings_get (s, "test-setting-float"); |
| 499 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 500 |
g_assert_cmpfloat_with_epsilon (value, 99.5, 0.001); |
| 501 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 502 |
|
| 503 |
j = wp_spa_json_new_float (-150.5); |
| 504 |
g_assert_false (wp_settings_set (s, "test-setting-float", j)); |
| 505 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 506 |
j = wp_settings_get (s, "test-setting-float"); |
| 507 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 508 |
g_assert_cmpfloat_with_epsilon (value, 99.5, 0.001); |
| 509 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 510 |
|
| 511 |
j = wp_spa_json_new_int (50); |
| 512 |
g_assert_false (wp_settings_set (s, "test-setting-float", j)); |
| 513 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 514 |
j = wp_settings_get (s, "test-setting-float"); |
| 515 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 516 |
g_assert_cmpfloat_with_epsilon (value, 99.5, 0.001); |
| 517 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 518 |
|
| 519 |
j = wp_settings_get_saved (s, "test-setting-float"); |
| 520 |
g_assert_null (j); |
| 521 |
g_assert_true (wp_settings_save (s, "test-setting-float")); |
| 522 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 523 |
j = wp_settings_get_saved (s, "test-setting-float"); |
| 524 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 525 |
g_assert_cmpfloat_with_epsilon (value, 99.5, 0.001); |
| 526 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 527 |
g_assert_true (wp_settings_delete (s, "test-setting-float")); |
| 528 |
j = wp_settings_get_saved (s, "test-setting-float"); |
| 529 |
g_assert_null (j); |
| 530 |
g_assert_true (wp_settings_reset (s, "test-setting-float")); |
| 531 |
j = wp_settings_get (s, "test-setting-float"); |
| 532 |
g_assert_true (wp_spa_json_parse_float (j, &value)); |
| 533 |
g_assert_cmpfloat_with_epsilon (value, 0.0, 0.001); |
| 534 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 535 |
} |
| 536 |
|
| 537 |
/* String */ |
| 538 |
{ |
| 539 |
g_autofree gchar *value = NULL; |
| 540 |
|
| 541 |
{ |
| 542 |
spec = wp_settings_get_spec (s, "test-setting-string"); |
| 543 |
name = wp_settings_spec_get_name (spec); |
| 544 |
g_assert_nonnull (name); |
| 545 |
g_assert_cmpstr (name, ==, "test-setting-string name"); |
| 546 |
desc = wp_settings_spec_get_description (spec); |
| 547 |
g_assert_nonnull (desc); |
| 548 |
g_assert_cmpstr (desc, ==, "test-setting-string description"); |
| 549 |
g_assert_true (wp_settings_spec_get_value_type (spec) == |
| 550 |
WP_SETTINGS_SPEC_TYPE_STRING); |
| 551 |
def = wp_settings_spec_get_default_value (spec); |
| 552 |
g_assert_nonnull (def); |
| 553 |
value = wp_spa_json_parse_string (def); |
| 554 |
g_assert_nonnull (value); |
| 555 |
g_assert_cmpstr (value, ==, "default"); |
| 556 |
g_clear_pointer (&value, g_free); |
| 557 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 558 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 559 |
|
| 560 |
j = wp_settings_get (s, "test-setting-string"); |
| 561 |
g_assert_nonnull (j); |
| 562 |
value = wp_spa_json_parse_string (j); |
| 563 |
g_assert_nonnull (value); |
| 564 |
g_assert_cmpstr (value, ==, "blahblah"); |
| 565 |
g_clear_pointer (&value, g_free); |
| 566 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 567 |
|
| 568 |
j = wp_spa_json_new_string ("new-string-value"); |
| 569 |
g_assert_true (wp_settings_set (s, "test-setting-string", j)); |
| 570 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 571 |
j = wp_settings_get (s, "test-setting-string"); |
| 572 |
value = wp_spa_json_parse_string (j); |
| 573 |
g_assert_nonnull (value); |
| 574 |
g_assert_cmpstr (value, ==, "new-string-value"); |
| 575 |
g_clear_pointer (&value, g_free); |
| 576 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 577 |
|
| 578 |
j = wp_spa_json_new_int (99); |
| 579 |
g_assert_false (wp_settings_set (s, "test-setting-string", j)); |
| 580 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 581 |
j = wp_settings_get (s, "test-setting-string"); |
| 582 |
value = wp_spa_json_parse_string (j); |
| 583 |
g_assert_nonnull (value); |
| 584 |
g_assert_cmpstr (value, ==, "new-string-value"); |
| 585 |
g_clear_pointer (&value, g_free); |
| 586 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 587 |
|
| 588 |
j = wp_spa_json_new_string ("99"); |
| 589 |
g_assert_true (wp_settings_set (s, "test-setting-string", j)); |
| 590 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 591 |
j = wp_settings_get (s, "test-setting-string"); |
| 592 |
value = wp_spa_json_parse_string (j); |
| 593 |
g_assert_nonnull (value); |
| 594 |
g_assert_cmpstr (value, ==, "99"); |
| 595 |
g_clear_pointer (&value, g_free); |
| 596 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 597 |
|
| 598 |
j = wp_settings_get_saved (s, "test-setting-string"); |
| 599 |
g_assert_null (j); |
| 600 |
g_assert_true (wp_settings_save (s, "test-setting-string")); |
| 601 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 602 |
j = wp_settings_get_saved (s, "test-setting-string"); |
| 603 |
value = wp_spa_json_parse_string (j); |
| 604 |
g_assert_nonnull (value); |
| 605 |
g_assert_cmpstr (value, ==, "99"); |
| 606 |
g_clear_pointer (&value, g_free); |
| 607 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 608 |
g_assert_true (wp_settings_delete (s, "test-setting-string")); |
| 609 |
j = wp_settings_get_saved (s, "test-setting-string"); |
| 610 |
g_assert_null (j); |
| 611 |
g_assert_true (wp_settings_reset (s, "test-setting-string")); |
| 612 |
j = wp_settings_get (s, "test-setting-string"); |
| 613 |
value = wp_spa_json_parse_string (j); |
| 614 |
g_assert_nonnull (value); |
| 615 |
g_assert_cmpstr (value, ==, "default"); |
| 616 |
g_clear_pointer (&value, g_free); |
| 617 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 618 |
} |
| 619 |
|
| 620 |
{ |
| 621 |
spec = wp_settings_get_spec (s, "test-setting-string"); |
| 622 |
name = wp_settings_spec_get_name (spec); |
| 623 |
g_assert_nonnull (name); |
| 624 |
g_assert_cmpstr (name, ==, "test-setting-string name"); |
| 625 |
desc = wp_settings_spec_get_description (spec); |
| 626 |
g_assert_nonnull (desc); |
| 627 |
g_assert_cmpstr (desc, ==, "test-setting-string description"); |
| 628 |
g_assert_true (wp_settings_spec_get_value_type (spec) == |
| 629 |
WP_SETTINGS_SPEC_TYPE_STRING); |
| 630 |
def = wp_settings_spec_get_default_value (spec); |
| 631 |
g_assert_nonnull (def); |
| 632 |
value = wp_spa_json_parse_string (def); |
| 633 |
g_assert_nonnull (value); |
| 634 |
g_assert_cmpstr (value, ==, "default"); |
| 635 |
g_clear_pointer (&value, g_free); |
| 636 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 637 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 638 |
|
| 639 |
j = wp_settings_get (s, "test-setting-string2"); |
| 640 |
g_assert_nonnull (j); |
| 641 |
value = wp_spa_json_parse_string (j); |
| 642 |
g_assert_nonnull (value); |
| 643 |
g_assert_cmpstr (value, ==, "a string with \"quotes\""); |
| 644 |
g_clear_pointer (&value, g_free); |
| 645 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 646 |
|
| 647 |
j = wp_spa_json_new_string ("a new string with \"quotes\""); |
| 648 |
g_assert_true (wp_settings_set (s, "test-setting-string2", j)); |
| 649 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 650 |
j = wp_settings_get (s, "test-setting-string2"); |
| 651 |
value = wp_spa_json_parse_string (j); |
| 652 |
g_assert_nonnull (value); |
| 653 |
g_assert_cmpstr (value, ==, "a new string with \"quotes\""); |
| 654 |
g_clear_pointer (&value, g_free); |
| 655 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 656 |
|
| 657 |
j = wp_spa_json_new_float (99.5); |
| 658 |
g_assert_false (wp_settings_set (s, "test-setting-string2", j)); |
| 659 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 660 |
j = wp_settings_get (s, "test-setting-string2"); |
| 661 |
value = wp_spa_json_parse_string (j); |
| 662 |
g_assert_nonnull (value); |
| 663 |
g_assert_cmpstr (value, ==, "a new string with \"quotes\""); |
| 664 |
g_clear_pointer (&value, g_free); |
| 665 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 666 |
|
| 667 |
j = wp_spa_json_new_string ("99.5"); |
| 668 |
g_assert_true (wp_settings_set (s, "test-setting-string2", j)); |
| 669 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 670 |
j = wp_settings_get (s, "test-setting-string2"); |
| 671 |
value = wp_spa_json_parse_string (j); |
| 672 |
g_assert_nonnull (value); |
| 673 |
g_assert_cmpstr (value, ==, "99.5"); |
| 674 |
g_clear_pointer (&value, g_free); |
| 675 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 676 |
|
| 677 |
j = wp_settings_get_saved (s, "test-setting-string2"); |
| 678 |
g_assert_null (j); |
| 679 |
g_assert_true (wp_settings_save (s, "test-setting-string2")); |
| 680 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 681 |
j = wp_settings_get_saved (s, "test-setting-string2"); |
| 682 |
value = wp_spa_json_parse_string (j); |
| 683 |
g_assert_nonnull (value); |
| 684 |
g_assert_cmpstr (value, ==, "99.5"); |
| 685 |
g_clear_pointer (&value, g_free); |
| 686 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 687 |
g_assert_true (wp_settings_delete (s, "test-setting-string2")); |
| 688 |
j = wp_settings_get_saved (s, "test-setting-string2"); |
| 689 |
g_assert_null (j); |
| 690 |
g_assert_true (wp_settings_reset (s, "test-setting-string2")); |
| 691 |
j = wp_settings_get (s, "test-setting-string2"); |
| 692 |
value = wp_spa_json_parse_string (j); |
| 693 |
g_assert_nonnull (value); |
| 694 |
g_assert_cmpstr (value, ==, "default"); |
| 695 |
g_clear_pointer (&value, g_free); |
| 696 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
/* Array */ |
| 701 |
{ |
| 702 |
{ |
| 703 |
spec = wp_settings_get_spec (s, "test-setting-array"); |
| 704 |
name = wp_settings_spec_get_name (spec); |
| 705 |
g_assert_nonnull (name); |
| 706 |
g_assert_cmpstr (name, ==, "test-setting-array name"); |
| 707 |
desc = wp_settings_spec_get_description (spec); |
| 708 |
g_assert_nonnull (desc); |
| 709 |
g_assert_cmpstr (desc, ==, "test-setting-array description"); |
| 710 |
g_assert_true (wp_settings_spec_get_value_type (spec) == |
| 711 |
WP_SETTINGS_SPEC_TYPE_ARRAY); |
| 712 |
def = wp_settings_spec_get_default_value (spec); |
| 713 |
g_assert_nonnull (def); |
| 714 |
g_assert_cmpstr (wp_spa_json_get_data (def), ==, "[]"); |
| 715 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 716 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 717 |
|
| 718 |
j = wp_settings_get (s, "test-setting-array"); |
| 719 |
g_assert_nonnull (j); |
| 720 |
g_assert_true (wp_spa_json_is_array (j)); |
| 721 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[1, 2, 3]"); |
| 722 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 723 |
|
| 724 |
j = wp_spa_json_new_array ("i", 4, "i", 5, NULL); |
| 725 |
g_assert_true (wp_settings_set (s, "test-setting-array", j)); |
| 726 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 727 |
j = wp_settings_get (s, "test-setting-array"); |
| 728 |
g_assert_nonnull (j); |
| 729 |
g_assert_true (wp_spa_json_is_array (j)); |
| 730 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[4, 5]"); |
| 731 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 732 |
|
| 733 |
j = wp_spa_json_new_int (6); |
| 734 |
g_assert_false (wp_settings_set (s, "test-setting-array", j)); |
| 735 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 736 |
j = wp_settings_get (s, "test-setting-array"); |
| 737 |
g_assert_nonnull (j); |
| 738 |
g_assert_true (wp_spa_json_is_array (j)); |
| 739 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[4, 5]"); |
| 740 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 741 |
|
| 742 |
j = wp_settings_get_saved (s, "test-setting-array"); |
| 743 |
g_assert_null (j); |
| 744 |
g_assert_true (wp_settings_save (s, "test-setting-array")); |
| 745 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 746 |
j = wp_settings_get_saved (s, "test-setting-array"); |
| 747 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[4, 5]"); |
| 748 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 749 |
g_assert_true (wp_settings_delete (s, "test-setting-array")); |
| 750 |
j = wp_settings_get_saved (s, "test-setting-array"); |
| 751 |
g_assert_null (j); |
| 752 |
g_assert_true (wp_settings_reset (s, "test-setting-array")); |
| 753 |
j = wp_settings_get (s, "test-setting-array"); |
| 754 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[]"); |
| 755 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 756 |
} |
| 757 |
|
| 758 |
{ |
| 759 |
spec = wp_settings_get_spec (s, "test-setting-array2"); |
| 760 |
name = wp_settings_spec_get_name (spec); |
| 761 |
g_assert_nonnull (name); |
| 762 |
g_assert_cmpstr (name, ==, "test-setting-array2 name"); |
| 763 |
desc = wp_settings_spec_get_description (spec); |
| 764 |
g_assert_nonnull (desc); |
| 765 |
g_assert_cmpstr (desc, ==, "test-setting-array2 description"); |
| 766 |
g_assert_true (wp_settings_spec_get_value_type (spec) == |
| 767 |
WP_SETTINGS_SPEC_TYPE_ARRAY); |
| 768 |
def = wp_settings_spec_get_default_value (spec); |
| 769 |
g_assert_nonnull (def); |
| 770 |
g_assert_cmpstr (wp_spa_json_get_data (def), ==, "[]"); |
| 771 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 772 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 773 |
|
| 774 |
j = wp_settings_get (s, "test-setting-array2"); |
| 775 |
g_assert_nonnull (j); |
| 776 |
g_assert_true (wp_spa_json_is_array (j)); |
| 777 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, |
| 778 |
"[\"test1\", \"test 2\", \"test three\", \"test-four\"]"); |
| 779 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 780 |
|
| 781 |
j = wp_spa_json_new_array ("s", "foo", "s", "bar", NULL); |
| 782 |
g_assert_true (wp_settings_set (s, "test-setting-array2", j)); |
| 783 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 784 |
j = wp_settings_get (s, "test-setting-array2"); |
| 785 |
g_assert_nonnull (j); |
| 786 |
g_assert_true (wp_spa_json_is_array (j)); |
| 787 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[\"foo\", \"bar\"]"); |
| 788 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 789 |
|
| 790 |
j = wp_spa_json_new_string ("value"); |
| 791 |
g_assert_false (wp_settings_set (s, "test-setting-array2", j)); |
| 792 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 793 |
j = wp_settings_get (s, "test-setting-array2"); |
| 794 |
g_assert_nonnull (j); |
| 795 |
g_assert_true (wp_spa_json_is_array (j)); |
| 796 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[\"foo\", \"bar\"]"); |
| 797 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 798 |
|
| 799 |
j = wp_settings_get_saved (s, "test-setting-array2"); |
| 800 |
g_assert_null (j); |
| 801 |
g_assert_true (wp_settings_save (s, "test-setting-array2")); |
| 802 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 803 |
j = wp_settings_get_saved (s, "test-setting-array2"); |
| 804 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[\"foo\", \"bar\"]"); |
| 805 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 806 |
g_assert_true (wp_settings_delete (s, "test-setting-array2")); |
| 807 |
j = wp_settings_get_saved (s, "test-setting-array2"); |
| 808 |
g_assert_null (j); |
| 809 |
g_assert_true (wp_settings_reset (s, "test-setting-array2")); |
| 810 |
j = wp_settings_get (s, "test-setting-array2"); |
| 811 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "[]"); |
| 812 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
/* Object */ |
| 817 |
{ |
| 818 |
spec = wp_settings_get_spec (s, "test-setting-object"); |
| 819 |
name = wp_settings_spec_get_name (spec); |
| 820 |
g_assert_nonnull (name); |
| 821 |
g_assert_cmpstr (name, ==, "test-setting-object name"); |
| 822 |
desc = wp_settings_spec_get_description (spec); |
| 823 |
g_assert_nonnull (desc); |
| 824 |
g_assert_cmpstr (desc, ==, "test-setting-object description"); |
| 825 |
g_assert_true (wp_settings_spec_get_value_type (spec) == |
| 826 |
WP_SETTINGS_SPEC_TYPE_OBJECT); |
| 827 |
def = wp_settings_spec_get_default_value (spec); |
| 828 |
g_assert_nonnull (def); |
| 829 |
g_assert_cmpstr (wp_spa_json_get_data (def), ==, "{}"); |
| 830 |
g_clear_pointer (&def, wp_spa_json_unref); |
| 831 |
g_clear_pointer (&spec, wp_settings_spec_unref); |
| 832 |
|
| 833 |
j = wp_settings_get (s, "test-setting-object"); |
| 834 |
g_assert_nonnull (j); |
| 835 |
g_assert_true (wp_spa_json_is_object (j)); |
| 836 |
g_assert_cmpstr (wp_spa_json_get_data(j), ==, |
| 837 |
"{ key1: \"value\", key2: 2, key3: true }"); |
| 838 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 839 |
|
| 840 |
j = wp_spa_json_new_object ( |
| 841 |
"key1", "s", "new-value", |
| 842 |
"key2", "i", 5, |
| 843 |
"key4", "b", FALSE, |
| 844 |
NULL); |
| 845 |
g_assert_true (wp_settings_set (s, "test-setting-object", j)); |
| 846 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 847 |
j = wp_settings_get (s, "test-setting-object"); |
| 848 |
g_assert_nonnull (j); |
| 849 |
g_assert_true (wp_spa_json_is_object (j)); |
| 850 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, |
| 851 |
"{\"key1\":\"new-value\", \"key2\":5, \"key4\":false}"); |
| 852 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 853 |
|
| 854 |
j = wp_spa_json_new_string ("value"); |
| 855 |
g_assert_false (wp_settings_set (s, "test-setting-object", j)); |
| 856 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 857 |
j = wp_settings_get (s, "test-setting-object"); |
| 858 |
g_assert_nonnull (j); |
| 859 |
g_assert_true (wp_spa_json_is_object (j)); |
| 860 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, |
| 861 |
"{\"key1\":\"new-value\", \"key2\":5, \"key4\":false}"); |
| 862 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 863 |
|
| 864 |
j = wp_settings_get_saved (s, "test-setting-object"); |
| 865 |
g_assert_null (j); |
| 866 |
g_assert_true (wp_settings_save (s, "test-setting-object")); |
| 867 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 868 |
j = wp_settings_get_saved (s, "test-setting-object"); |
| 869 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, |
| 870 |
"{\"key1\":\"new-value\", \"key2\":5, \"key4\":false}"); |
| 871 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 872 |
g_assert_true (wp_settings_delete (s, "test-setting-object")); |
| 873 |
j = wp_settings_get_saved (s, "test-setting-object"); |
| 874 |
g_assert_null (j); |
| 875 |
g_assert_true (wp_settings_reset (s, "test-setting-object")); |
| 876 |
j = wp_settings_get (s, "test-setting-object"); |
| 877 |
g_assert_cmpstr (wp_spa_json_get_data (j), ==, "{}"); |
| 878 |
g_clear_pointer (&j, wp_spa_json_unref); |
| 879 |
} |
| 880 |
|
| 881 |
/* Name omitted */ |
| 882 |
{ |
| 883 |
spec = wp_settings_get_spec (s, "test-setting-no-name"); |
| 884 |
name = wp_settings_spec_get_name (spec); |
| 885 |
g_assert_null (name); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
static void |
| 890 |
test_save_reset_delete_all (TestSettingsFixture *self, gconstpointer data) |
| 891 |
{ |
| 892 |
WpSettings *s = self->settings; |
| 893 |
|
| 894 |
/* Check settings are the same as the loaded ones, and not saved */ |
| 895 |
{ |
| 896 |
g_autoptr (WpProperties) props = wp_properties_new_empty (); |
| 897 |
g_autoptr (WpIterator) it = wp_settings_new_iterator (s); |
| 898 |
g_auto (GValue) val = G_VALUE_INIT; |
| 899 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 900 |
WpSettingsItem *si = g_value_get_boxed (&val); |
| 901 |
const gchar *key = wp_settings_item_get_key (si); |
| 902 |
g_autoptr (WpSpaJson) value = wp_settings_item_get_value (si); |
| 903 |
g_autoptr (WpSpaJson) saved = wp_settings_get_saved (s, key); |
| 904 |
g_assert_null (saved); |
| 905 |
wp_properties_set (props, key, wp_spa_json_get_data (value)); |
| 906 |
} |
| 907 |
g_assert_true (wp_properties_matches (self->loaded_settings, props)); |
| 908 |
} |
| 909 |
|
| 910 |
/* Reset all settings to default value */ |
| 911 |
wp_settings_reset_all (self->settings); |
| 912 |
|
| 913 |
/* Check all settings are set to their default values */ |
| 914 |
{ |
| 915 |
gint n_settings = 0; |
| 916 |
g_autoptr (WpIterator) it = wp_settings_new_iterator (s); |
| 917 |
g_auto (GValue) val = G_VALUE_INIT; |
| 918 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 919 |
WpSettingsItem *si = g_value_get_boxed (&val); |
| 920 |
const gchar *key = wp_settings_item_get_key (si); |
| 921 |
g_autoptr (WpSpaJson) value = wp_settings_item_get_value (si); |
| 922 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 923 |
g_autoptr (WpSpaJson) def = NULL; |
| 924 |
spec = wp_settings_get_spec (s, key); |
| 925 |
g_assert_nonnull (spec); |
| 926 |
def = wp_settings_spec_get_default_value (spec); |
| 927 |
g_assert_cmpstr (wp_spa_json_get_data (def), ==, |
| 928 |
wp_spa_json_get_data (value)); |
| 929 |
n_settings++; |
| 930 |
} |
| 931 |
g_assert_cmpint (n_settings, ==, |
| 932 |
wp_properties_get_count (self->loaded_settings)); |
| 933 |
} |
| 934 |
|
| 935 |
/* Save all settings */ |
| 936 |
wp_settings_save_all (self->settings); |
| 937 |
|
| 938 |
/* Check all settings are saved */ |
| 939 |
{ |
| 940 |
g_autoptr (WpIterator) it = wp_settings_new_iterator (s); |
| 941 |
g_auto (GValue) val = G_VALUE_INIT; |
| 942 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 943 |
WpSettingsItem *si = g_value_get_boxed (&val); |
| 944 |
const gchar *key = wp_settings_item_get_key (si); |
| 945 |
g_autoptr (WpSpaJson) saved = wp_settings_get_saved (s, key); |
| 946 |
g_assert_nonnull (saved); |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
/* Delete all saved settings */ |
| 951 |
wp_settings_delete_all (self->settings); |
| 952 |
|
| 953 |
/* Check all settings are not saved anymore */ |
| 954 |
{ |
| 955 |
g_autoptr (WpIterator) it = wp_settings_new_iterator (s); |
| 956 |
g_auto (GValue) val = G_VALUE_INIT; |
| 957 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 958 |
WpSettingsItem *si = g_value_get_boxed (&val); |
| 959 |
const gchar *key = wp_settings_item_get_key (si); |
| 960 |
g_autoptr (WpSpaJson) saved = wp_settings_get_saved (s, key); |
| 961 |
g_assert_null (saved); |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
static void |
| 967 |
wp_settings_changed_callback (WpSettings *s, const gchar *key, |
| 968 |
WpSpaJson *value, gpointer user_data) |
| 969 |
{ |
| 970 |
TestSettingsFixture *self = user_data; |
| 971 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 972 |
|
| 973 |
g_assert_cmpstr (key, ==, self->triggered_setting); |
| 974 |
g_assert_nonnull (value); |
| 975 |
|
| 976 |
self->triggered_callback = TRUE; |
| 977 |
|
| 978 |
spec = wp_settings_get_spec (s, key); |
| 979 |
g_assert_nonnull (spec); |
| 980 |
|
| 981 |
switch (wp_settings_spec_get_value_type (spec)) { |
| 982 |
case WP_SETTINGS_SPEC_TYPE_BOOL: { |
| 983 |
gboolean val = FALSE, expected = FALSE; |
| 984 |
g_assert_true (wp_spa_json_parse_boolean (value, &val)); |
| 985 |
g_assert_true (wp_spa_json_parse_boolean (self->triggered_setting_value, |
| 986 |
&expected)); |
| 987 |
g_assert_true (val == expected); |
| 988 |
break; |
| 989 |
} |
| 990 |
case WP_SETTINGS_SPEC_TYPE_INT: { |
| 991 |
gint val = 0, expected = 0; |
| 992 |
g_assert_true (wp_spa_json_parse_int (value, &val)); |
| 993 |
g_assert_true (wp_spa_json_parse_int (self->triggered_setting_value, |
| 994 |
&expected)); |
| 995 |
g_assert_cmpint (val, ==, expected); |
| 996 |
break; |
| 997 |
} |
| 998 |
case WP_SETTINGS_SPEC_TYPE_FLOAT: { |
| 999 |
gfloat val = 0.0, expected = 0.0; |
| 1000 |
g_assert_true (wp_spa_json_parse_float (value, &val)); |
| 1001 |
g_assert_true (wp_spa_json_parse_float (self->triggered_setting_value, |
| 1002 |
&expected)); |
| 1003 |
g_assert_cmpfloat_with_epsilon (val, expected, 0.001); |
| 1004 |
break; |
| 1005 |
} |
| 1006 |
case WP_SETTINGS_SPEC_TYPE_STRING: { |
| 1007 |
g_autofree gchar *val = wp_spa_json_parse_string (value); |
| 1008 |
g_autofree gchar *expected = wp_spa_json_parse_string ( |
| 1009 |
self->triggered_setting_value); |
| 1010 |
g_assert_nonnull (value); |
| 1011 |
g_assert_nonnull (expected); |
| 1012 |
g_assert_cmpstr (val, ==, expected); |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
case WP_SETTINGS_SPEC_TYPE_ARRAY: { |
| 1016 |
g_assert_cmpstr (wp_spa_json_get_data (value), ==, |
| 1017 |
wp_spa_json_get_data (self->triggered_setting_value)); |
| 1018 |
break; |
| 1019 |
} |
| 1020 |
case WP_SETTINGS_SPEC_TYPE_OBJECT: { |
| 1021 |
g_assert_cmpstr (wp_spa_json_get_data (value), ==, |
| 1022 |
wp_spa_json_get_data (self->triggered_setting_value)); |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
default: |
| 1026 |
g_assert_not_reached (); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
static void |
| 1031 |
test_subscribe_unsibscribe (TestSettingsFixture *self, gconstpointer data) |
| 1032 |
{ |
| 1033 |
WpSettings *s = self->settings; |
| 1034 |
g_autoptr (WpSpaJson) json = NULL; |
| 1035 |
guintptr sub_id; |
| 1036 |
|
| 1037 |
/* Boolean */ |
| 1038 |
{ |
| 1039 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1040 |
wp_settings_changed_callback, (gpointer)self); |
| 1041 |
|
| 1042 |
json = wp_spa_json_new_boolean (FALSE); |
| 1043 |
self->triggered_setting = "test-setting-bool"; |
| 1044 |
self->triggered_setting_value = json; |
| 1045 |
self->triggered_callback = FALSE; |
| 1046 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1047 |
g_assert_true (self->triggered_callback); |
| 1048 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1049 |
|
| 1050 |
wp_settings_unsubscribe (s, sub_id); |
| 1051 |
|
| 1052 |
json = wp_spa_json_new_boolean (TRUE); |
| 1053 |
self->triggered_setting = "test-setting-bool"; |
| 1054 |
self->triggered_setting_value = json; |
| 1055 |
self->triggered_callback = FALSE; |
| 1056 |
wp_settings_set (s, self->triggered_setting, json); |
| 1057 |
g_assert_false (self->triggered_callback); |
| 1058 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* Int */ |
| 1062 |
{ |
| 1063 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1064 |
wp_settings_changed_callback, (gpointer)self); |
| 1065 |
|
| 1066 |
json = wp_spa_json_new_int (99); |
| 1067 |
self->triggered_setting = "test-setting-int"; |
| 1068 |
self->triggered_setting_value = json; |
| 1069 |
self->triggered_callback = FALSE; |
| 1070 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1071 |
g_assert_true (self->triggered_callback); |
| 1072 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1073 |
|
| 1074 |
wp_settings_unsubscribe (s, sub_id); |
| 1075 |
|
| 1076 |
json = wp_spa_json_new_int (90); |
| 1077 |
self->triggered_setting = "test-setting-int"; |
| 1078 |
self->triggered_setting_value = json; |
| 1079 |
self->triggered_callback = FALSE; |
| 1080 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1081 |
g_assert_false (self->triggered_callback); |
| 1082 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* Float */ |
| 1086 |
{ |
| 1087 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1088 |
wp_settings_changed_callback, (gpointer)self); |
| 1089 |
|
| 1090 |
json = wp_spa_json_new_float (45.5); |
| 1091 |
self->triggered_setting = "test-setting-float"; |
| 1092 |
self->triggered_setting_value = json; |
| 1093 |
self->triggered_callback = FALSE; |
| 1094 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1095 |
g_assert_true (self->triggered_callback); |
| 1096 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1097 |
|
| 1098 |
wp_settings_unsubscribe (s, sub_id); |
| 1099 |
|
| 1100 |
json = wp_spa_json_new_float (30.5); |
| 1101 |
self->triggered_setting = "test-setting-float"; |
| 1102 |
self->triggered_setting_value = json; |
| 1103 |
self->triggered_callback = FALSE; |
| 1104 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1105 |
g_assert_false (self->triggered_callback); |
| 1106 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* String */ |
| 1110 |
{ |
| 1111 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1112 |
wp_settings_changed_callback, (gpointer)self); |
| 1113 |
|
| 1114 |
json = wp_spa_json_new_string ("lets not blabber"); |
| 1115 |
self->triggered_setting = "test-setting-string"; |
| 1116 |
self->triggered_setting_value = json; |
| 1117 |
self->triggered_callback = FALSE; |
| 1118 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1119 |
g_assert_true (self->triggered_callback); |
| 1120 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1121 |
|
| 1122 |
wp_settings_unsubscribe (s, sub_id); |
| 1123 |
|
| 1124 |
json = wp_spa_json_new_string ("lets blabber"); |
| 1125 |
self->triggered_setting = "test-setting-string"; |
| 1126 |
self->triggered_setting_value = json; |
| 1127 |
self->triggered_callback = FALSE; |
| 1128 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1129 |
g_assert_false (self->triggered_callback); |
| 1130 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
/* Array */ |
| 1135 |
{ |
| 1136 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1137 |
wp_settings_changed_callback, (gpointer)self); |
| 1138 |
|
| 1139 |
json = wp_spa_json_new_array ("i", 4, "i", 5, NULL); |
| 1140 |
self->triggered_setting = "test-setting-array"; |
| 1141 |
self->triggered_setting_value = json; |
| 1142 |
self->triggered_callback = FALSE; |
| 1143 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1144 |
g_assert_true (self->triggered_callback); |
| 1145 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1146 |
|
| 1147 |
wp_settings_unsubscribe (s, sub_id); |
| 1148 |
|
| 1149 |
json = wp_spa_json_new_array ("i", 6, "i", 7, NULL); |
| 1150 |
self->triggered_setting = "test-setting-array"; |
| 1151 |
self->triggered_setting_value = json; |
| 1152 |
self->triggered_callback = FALSE; |
| 1153 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1154 |
g_assert_false (self->triggered_callback); |
| 1155 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* Object */ |
| 1159 |
{ |
| 1160 |
sub_id = wp_settings_subscribe (s, "test*", |
| 1161 |
wp_settings_changed_callback, (gpointer)self); |
| 1162 |
|
| 1163 |
json = wp_spa_json_new_object ( |
| 1164 |
"key1", "s", "value", |
| 1165 |
"key2", "i", 3, |
| 1166 |
"key4", "b", TRUE, |
| 1167 |
NULL); |
| 1168 |
self->triggered_setting = "test-setting-object"; |
| 1169 |
self->triggered_setting_value = json; |
| 1170 |
self->triggered_callback = FALSE; |
| 1171 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1172 |
g_assert_true (self->triggered_callback); |
| 1173 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1174 |
|
| 1175 |
wp_settings_unsubscribe (s, sub_id); |
| 1176 |
|
| 1177 |
json = wp_spa_json_new_object ( |
| 1178 |
"key1", "f", 1.2, |
| 1179 |
NULL); |
| 1180 |
self->triggered_setting = "test-setting-object"; |
| 1181 |
self->triggered_setting_value = json; |
| 1182 |
self->triggered_callback = FALSE; |
| 1183 |
g_assert_true (wp_settings_set (s, self->triggered_setting, json)); |
| 1184 |
g_assert_false (self->triggered_callback); |
| 1185 |
g_clear_pointer (&json, wp_spa_json_unref); |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
gint |
| 1190 |
main (gint argc, gchar *argv[]) |
| 1191 |
{ |
| 1192 |
g_test_init (&argc, &argv, NULL); |
| 1193 |
wp_init (WP_INIT_ALL); |
| 1194 |
|
| 1195 |
g_test_add ("/wp/settings/basic", TestSettingsFixture, NULL, |
| 1196 |
test_settings_setup, test_basic, test_settings_teardown); |
| 1197 |
g_test_add ("/wp/settings/get-set-save-reset-delete", TestSettingsFixture, NULL, |
| 1198 |
test_settings_setup, test_get_set_save_reset_delete, test_settings_teardown); |
| 1199 |
g_test_add ("/wp/settings/save-reset-delete-all", TestSettingsFixture, NULL, |
| 1200 |
test_settings_setup, test_save_reset_delete_all, test_settings_teardown); |
| 1201 |
g_test_add ("/wp/settings/subscribe-unsubscribe", TestSettingsFixture, NULL, |
| 1202 |
test_settings_setup, test_subscribe_unsibscribe, test_settings_teardown); |
| 1203 |
|
| 1204 |
return g_test_run (); |
| 1205 |
} |