lib: wpsettings: Add _register_callback () API
This commit is contained in:

committed by
Julian Bouzas

parent
8e9d852124
commit
cfd51d9fe2
@@ -36,6 +36,9 @@ struct _WpSettings
|
||||
/* element-type: Rule* */
|
||||
GPtrArray *rules;
|
||||
|
||||
/* element-type: Callback* */
|
||||
GPtrArray *callbacks;
|
||||
|
||||
gchar *metadata_name;
|
||||
WpObjectManager *metadata_om;
|
||||
};
|
||||
@@ -54,6 +57,12 @@ typedef struct
|
||||
WpProperties *actions;
|
||||
} Match;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GClosure *closure;
|
||||
gchar *pattern;
|
||||
} Callback;
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_METADATA_NAME,
|
||||
@@ -67,6 +76,90 @@ wp_settings_init (WpSettings * self)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Subscribes callback for a given setting pattern(a glob-style pattern
|
||||
* matched using g_pattern_match_simple), this allows clients to look
|
||||
* for any changes made in settings through metadata.
|
||||
*
|
||||
* \ingroup wpsettings
|
||||
* \param self the settings object
|
||||
* \param pattern name of the pattern to match the settings with
|
||||
* \param callback (scope async): the callback triggered when the settings
|
||||
* change.
|
||||
* \param user_data data to pass to \a callback
|
||||
* \returns the subscription ID (always greater than 0 for successful
|
||||
* subscriptions)
|
||||
*/
|
||||
guintptr
|
||||
wp_settings_subscribe (WpSettings *self,
|
||||
const gchar *pattern, WpSettingsChangedCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
return wp_settings_subscribe_closure (self, pattern,
|
||||
g_cclosure_new (G_CALLBACK (callback), user_data, NULL));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Subscribes callback for a given setting pattern(a glob-style pattern
|
||||
* matched using g_pattern_match_simple), this allows clients to look
|
||||
* for any changes made in settings through metadata.
|
||||
*
|
||||
* \ingroup wpsettings
|
||||
* \param self the settings object
|
||||
* \param pattern name of the pattern to match the settings with
|
||||
* \param closure (nullable): a GAsyncReadyCallback wrapped in a GClosure
|
||||
* \returns the subscription ID (always greater than 0 for success)
|
||||
*/
|
||||
guintptr
|
||||
wp_settings_subscribe_closure (WpSettings *self, const gchar *pattern,
|
||||
GClosure *closure)
|
||||
{
|
||||
g_return_val_if_fail (self, false);
|
||||
g_return_val_if_fail (pattern, false);
|
||||
g_return_val_if_fail (closure, false);
|
||||
|
||||
Callback *cb = g_slice_new0 (Callback);
|
||||
g_return_val_if_fail (cb, FALSE);
|
||||
|
||||
cb->closure = g_closure_ref (closure);
|
||||
g_closure_sink (closure);
|
||||
if (G_CLOSURE_NEEDS_MARSHAL (closure))
|
||||
g_closure_set_marshal (closure, g_cclosure_marshal_generic);
|
||||
|
||||
cb->pattern = g_strdup (pattern);
|
||||
|
||||
g_ptr_array_add (self->callbacks, cb);
|
||||
|
||||
wp_debug_object (self, "callback(%p) subscribed for pattern(%s)",
|
||||
(void *) cb, pattern);
|
||||
|
||||
return (guintptr) cb;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unsubscribes callback for a given subscription_id.
|
||||
*
|
||||
* \ingroup wpsettings
|
||||
* \param self the settings object
|
||||
* \param subscription_id identifies the callback
|
||||
* \returns TRUE if success, FALSE otherwise
|
||||
*/
|
||||
gboolean
|
||||
wp_settings_unsubscribe (WpSettings *self, guintptr subscription_id)
|
||||
{
|
||||
gboolean ret = false;
|
||||
g_return_val_if_fail (self, false);
|
||||
g_return_val_if_fail (subscription_id, false);
|
||||
|
||||
Callback *cb = (Callback *) subscription_id;
|
||||
|
||||
ret = g_ptr_array_remove (self->callbacks, cb);
|
||||
|
||||
wp_debug_object (self, "callback(%p) unsubscription %s", (void *) cb,
|
||||
(ret)? "succeeded": "failed");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the boolean value of a setting
|
||||
@@ -106,7 +199,7 @@ wp_settings_get_boolean (WpSettings *self, const gchar *setting,
|
||||
*/
|
||||
gboolean
|
||||
wp_settings_get_string (WpSettings *self, const gchar *setting,
|
||||
const char **value)
|
||||
const gchar **value)
|
||||
{
|
||||
g_return_val_if_fail (self, false);
|
||||
g_return_val_if_fail (setting, false);
|
||||
@@ -509,6 +602,48 @@ parse_setting (const gchar *setting, const gchar *value, WpSettings *self)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
on_metadata_changed (WpMetadata *m, guint32 subject,
|
||||
const gchar *setting, const gchar *type, const gchar *new_value, gpointer d)
|
||||
{
|
||||
WpSettings *self = WP_SETTINGS(d);
|
||||
const gchar *old_value = wp_properties_get (self->settings, setting);
|
||||
|
||||
if (!old_value) {
|
||||
wp_info_object (self, "new setting defined \"%s\" = \"%s\"",
|
||||
setting, new_value);
|
||||
} else {
|
||||
wp_info_object (self, "setting \"%s\" new_value changed from \"%s\" ->"
|
||||
" \"%s\"", setting, old_value, new_value);
|
||||
}
|
||||
|
||||
wp_properties_set (self->settings, setting, new_value);
|
||||
|
||||
for (guint i = 0; i < self->callbacks->len; i++) {
|
||||
Callback *cb = g_ptr_array_index (self->callbacks, i);
|
||||
|
||||
if (g_pattern_match_simple (cb->pattern, setting)) {
|
||||
|
||||
GValue values[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
||||
g_value_init (&values[0], G_TYPE_OBJECT);
|
||||
g_value_init (&values[1], G_TYPE_STRING);
|
||||
g_value_init (&values[2], G_TYPE_STRING);
|
||||
|
||||
g_value_set_object (&values[0], self);
|
||||
g_value_set_string (&values[1], setting);
|
||||
g_value_set_string (&values[2], new_value);
|
||||
|
||||
g_closure_invoke (cb->closure, NULL, 3, values, NULL);
|
||||
|
||||
g_value_unset (&values[0]);
|
||||
g_value_unset (&values[1]);
|
||||
g_value_unset (&values[2]);
|
||||
|
||||
wp_debug_object (self, "triggered callback(%p)", cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
on_metadata_added (WpObjectManager *om, WpMetadata *m, gpointer d)
|
||||
{
|
||||
@@ -517,6 +652,10 @@ on_metadata_added (WpObjectManager *om, WpMetadata *m, gpointer d)
|
||||
g_autoptr (WpIterator) it = wp_metadata_new_iterator (WP_METADATA (m), 0);
|
||||
g_auto (GValue) val = G_VALUE_INIT;
|
||||
|
||||
/* Handle the changed signal */
|
||||
g_signal_connect_object (m, "changed", G_CALLBACK (on_metadata_changed),
|
||||
self, 0);
|
||||
|
||||
/* traverse through all settings and rules */
|
||||
for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
|
||||
const gchar *setting, *value;
|
||||
@@ -539,6 +678,13 @@ rule_unref (Rule * self)
|
||||
g_clear_pointer (&self->matches, g_ptr_array_unref);
|
||||
}
|
||||
|
||||
static void
|
||||
callback_unref (Callback * self)
|
||||
{
|
||||
g_free (self->pattern);
|
||||
g_clear_pointer (&self->closure, g_closure_unref);
|
||||
}
|
||||
|
||||
static void
|
||||
wp_settings_activate_execute_step (WpObject * object,
|
||||
WpFeatureActivationTransition * transition, guint step,
|
||||
@@ -555,6 +701,9 @@ wp_settings_activate_execute_step (WpObject * object,
|
||||
self->rules = g_ptr_array_new_with_free_func
|
||||
((GDestroyNotify) rule_unref);
|
||||
|
||||
self->callbacks = g_ptr_array_new_with_free_func
|
||||
((GDestroyNotify) callback_unref);
|
||||
|
||||
self->metadata_om = wp_object_manager_new ();
|
||||
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA,
|
||||
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s",
|
||||
@@ -581,9 +730,11 @@ wp_settings_deactivate (WpObject * object, WpObjectFeatures features)
|
||||
{
|
||||
WpSettings *self = WP_SETTINGS (object);
|
||||
|
||||
wp_debug_object (self, "%s", self->metadata_name);
|
||||
g_free (self->metadata_name);
|
||||
g_clear_object (&self->metadata_om);
|
||||
g_clear_pointer (&self->rules, g_ptr_array_unref);
|
||||
g_clear_pointer (&self->callbacks, g_ptr_array_unref);
|
||||
g_clear_pointer (&self->settings, wp_properties_unref);
|
||||
|
||||
wp_object_update_features (WP_OBJECT (self), 0, WP_OBJECT_FEATURES_ALL);
|
||||
|
@@ -35,13 +35,38 @@ WP_API
|
||||
WpSettings * wp_settings_get_instance (WpCore * core,
|
||||
const gchar *metadata_name);
|
||||
|
||||
/*!
|
||||
* \brief callback conveying the changed setting and its string(raw) value
|
||||
*
|
||||
* \ingroup wpsettings
|
||||
* \param obj the wpsettings object
|
||||
* \param setting the changed setting
|
||||
* \param raw_value string value of the the changed setting
|
||||
* \param user_data data passed in the \a wp_settings_subscribe
|
||||
*/
|
||||
typedef void (*WpSettingsChangedCallback) (WpSettings *obj,
|
||||
const gchar *setting, const gchar *raw_value, gpointer user_data);
|
||||
|
||||
WP_API
|
||||
guintptr wp_settings_subscribe (WpSettings *self,
|
||||
const gchar *pattern, WpSettingsChangedCallback callback,
|
||||
gpointer user_data);
|
||||
|
||||
WP_API
|
||||
guintptr wp_settings_subscribe_closure (WpSettings *self,
|
||||
const gchar *pattern, GClosure * closure);
|
||||
|
||||
WP_API
|
||||
gboolean wp_settings_unsubscribe (WpSettings *self,
|
||||
guintptr subscription_id);
|
||||
|
||||
WP_API
|
||||
gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting,
|
||||
gboolean *value);
|
||||
|
||||
WP_API
|
||||
gboolean wp_settings_get_string (WpSettings *self, const gchar *setting,
|
||||
const char **value);
|
||||
const gchar **value);
|
||||
|
||||
WP_API
|
||||
gboolean wp_settings_get_int (WpSettings *self, const gchar *setting,
|
||||
|
@@ -1593,12 +1593,52 @@ apply_rule (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
subscribe (lua_State *L)
|
||||
{
|
||||
const gchar *pattern = luaL_checkstring (L, 1);
|
||||
const gchar *m = NULL;
|
||||
guintptr sub_id = 0;
|
||||
|
||||
GClosure * closure = wplua_function_to_closure (L, -1);
|
||||
|
||||
if (lua_type (L, 2) == LUA_TSTRING)
|
||||
m = luaL_checkstring (L, 2);
|
||||
|
||||
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m);
|
||||
if (s)
|
||||
sub_id = wp_settings_subscribe_closure (s, pattern, closure);
|
||||
|
||||
lua_pushinteger (L, sub_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
unsubscribe (lua_State *L)
|
||||
{
|
||||
guintptr sub_id = luaL_checkinteger (L, 1);
|
||||
const gchar *m = NULL;
|
||||
gboolean ret = FALSE;
|
||||
if (lua_type (L, 2) == LUA_TSTRING)
|
||||
m = luaL_checkstring (L, 2);
|
||||
|
||||
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m);
|
||||
if (s)
|
||||
ret = wp_settings_unsubscribe (s, sub_id);
|
||||
|
||||
lua_pushboolean (L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg settings_methods[] = {
|
||||
{ "get_boolean", get_boolean },
|
||||
{ "apply_rule", apply_rule },
|
||||
{ "get_string", get_string },
|
||||
{ "get_int", get_int },
|
||||
{ "get_float", get_float },
|
||||
{ "apply_rule", apply_rule },
|
||||
{ "subscribe", subscribe },
|
||||
{ "unsubscribe", unsubscribe },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
|
@@ -160,8 +160,11 @@ do_parse_settings (void *data, const char *location,
|
||||
int len = 0;
|
||||
|
||||
g_value_unset (&item);
|
||||
if (!wp_iterator_next (iter, &item))
|
||||
if (!wp_iterator_next (iter, &item)) {
|
||||
wp_warning ("It is likely that the JSON syntax is incorrect,"
|
||||
" check key value pair formatting");
|
||||
break;
|
||||
}
|
||||
j = g_value_get_boxed (&item);
|
||||
|
||||
value = wp_spa_json_parse_string (j);
|
||||
|
@@ -11,6 +11,13 @@
|
||||
* tests the loading & parsing of JSON conf file(pls check the .conf that is
|
||||
* loaded), metadata updates, wpsetttings object creation and its API.
|
||||
*/
|
||||
typedef enum {
|
||||
BOOLEAN,
|
||||
INTEGER,
|
||||
STRING,
|
||||
FLOAT
|
||||
} SettingType;
|
||||
|
||||
typedef struct {
|
||||
WpBaseTestFixture base;
|
||||
|
||||
@@ -20,6 +27,11 @@ typedef struct {
|
||||
WpMetadata *metadata;
|
||||
|
||||
WpSettings *s;
|
||||
|
||||
gchar *triggered_setting;
|
||||
gchar *triggered_setting_value;
|
||||
gboolean triggered_callback;
|
||||
SettingType setting_type;
|
||||
} TestSettingsFixture;
|
||||
|
||||
static void
|
||||
@@ -253,11 +265,11 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
|
||||
gboolean value = 0;
|
||||
|
||||
/* test settings _get_boolean */
|
||||
g_assert_false (wp_settings_get_boolean (s, "test-property-undefined",
|
||||
g_assert_false (wp_settings_get_boolean (s, "test-setting-undefined",
|
||||
&value));
|
||||
g_assert_true (wp_settings_get_boolean (s, "test-property1", &value));
|
||||
g_assert_true (wp_settings_get_boolean (s, "test-setting1", &value));
|
||||
g_assert_false (value);
|
||||
g_assert_true (wp_settings_get_boolean (s, "test-property2", &value));
|
||||
g_assert_true (wp_settings_get_boolean (s, "test-setting2", &value));
|
||||
g_assert_true (value);
|
||||
}
|
||||
|
||||
@@ -265,24 +277,24 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
|
||||
gint64 value = 0;
|
||||
|
||||
/* _get_int () */
|
||||
g_assert_false (wp_settings_get_int (s, "test-property-undefined",
|
||||
g_assert_false (wp_settings_get_int (s, "test-setting-undefined",
|
||||
&value));
|
||||
|
||||
g_assert_true (wp_settings_get_int (s, "test-property3-int", &value));
|
||||
g_assert_true (wp_settings_get_int (s, "test-setting3-int", &value));
|
||||
g_assert_cmpint (value, ==, -20);
|
||||
|
||||
g_assert_true (wp_settings_get_int (s, "test-property4-max-int", &value));
|
||||
g_assert_true (wp_settings_get_int (s, "test-setting4-max-int", &value));
|
||||
g_assert_cmpint (value, ==, G_MAXINT64);
|
||||
|
||||
g_assert_true (wp_settings_get_int (s, "test-property4-min-int", &value));
|
||||
g_assert_true (wp_settings_get_int (s, "test-setting4-min-int", &value));
|
||||
g_assert_cmpint (value, ==, G_MININT64);
|
||||
|
||||
value = 0;
|
||||
g_assert_true (wp_settings_get_int (s, "test-property4-max-int-one-more",
|
||||
g_assert_true (wp_settings_get_int (s, "test-setting4-max-int-one-more",
|
||||
&value));
|
||||
g_assert_cmpint (value, ==, 0);
|
||||
|
||||
g_assert_true (wp_settings_get_int (s, "test-property4-min-int-one-less",
|
||||
g_assert_true (wp_settings_get_int (s, "test-setting4-min-int-one-less",
|
||||
&value));
|
||||
g_assert_cmpint (value, ==, 0);
|
||||
}
|
||||
@@ -290,26 +302,26 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
|
||||
{
|
||||
const gchar *value = NULL;
|
||||
/* _get_string () */
|
||||
g_assert_false (wp_settings_get_string (s, "test-property-undefined",
|
||||
g_assert_false (wp_settings_get_string (s, "test-setting-undefined",
|
||||
&value));
|
||||
|
||||
g_assert_true (wp_settings_get_string (s, "test-property4-string",
|
||||
g_assert_true (wp_settings_get_string (s, "test-setting4-string",
|
||||
&value));
|
||||
g_assert_cmpstr (value, ==, "blahblah");
|
||||
|
||||
g_assert_true (wp_settings_get_string (s, "test-property2",
|
||||
g_assert_true (wp_settings_get_string (s, "test-setting2",
|
||||
&value));
|
||||
g_assert_cmpstr (value, ==, "true");
|
||||
|
||||
g_assert_true (wp_settings_get_string (s, "test-property3-int",
|
||||
g_assert_true (wp_settings_get_string (s, "test-setting3-int",
|
||||
&value));
|
||||
g_assert_cmpstr (value, ==, "-20");
|
||||
|
||||
g_assert_true (wp_settings_get_string (s, "test-prop1-json",
|
||||
g_assert_true (wp_settings_get_string (s, "test-setting-json",
|
||||
&value));
|
||||
g_assert_cmpstr (value, ==, "[ a b c ]");
|
||||
|
||||
g_assert_true (wp_settings_get_string (s, "test-prop-strings",
|
||||
g_assert_true (wp_settings_get_string (s, "test-setting-strings",
|
||||
&value));
|
||||
g_assert_cmpstr (value, ==, "[\"test1\", \"test 2\", \"test three\", \"test-four\"]");
|
||||
|
||||
@@ -352,15 +364,15 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
|
||||
{
|
||||
gfloat value = 0.0;
|
||||
/* _get_float () */
|
||||
g_assert_false (wp_settings_get_float (s, "test-property-undefined",
|
||||
g_assert_false (wp_settings_get_float (s, "test-setting-undefined",
|
||||
&value));
|
||||
|
||||
g_assert_true (wp_settings_get_float (s, "test-property-float1",
|
||||
g_assert_true (wp_settings_get_float (s, "test-setting-float1",
|
||||
&value));
|
||||
|
||||
g_assert_cmpfloat_with_epsilon (value, 3.14, 0.001);
|
||||
|
||||
g_assert_true (wp_settings_get_float (s, "test-property-float2",
|
||||
g_assert_true (wp_settings_get_float (s, "test-setting-float2",
|
||||
&value));
|
||||
g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001);
|
||||
}
|
||||
@@ -555,6 +567,100 @@ test_rules (TestSettingsFixture *self, gconstpointer data)
|
||||
}
|
||||
}
|
||||
|
||||
void wp_settings_changed_callback (WpSettings *obj, const gchar *setting,
|
||||
const gchar *raw_value, gpointer user_data)
|
||||
{
|
||||
TestSettingsFixture *self = user_data;
|
||||
g_assert_cmpstr (setting, ==, self->triggered_setting);
|
||||
self->triggered_callback = true;
|
||||
|
||||
if (self->setting_type == BOOLEAN) {
|
||||
gboolean value = false;
|
||||
wp_settings_get_boolean (self->s, setting, &value);
|
||||
g_assert_cmpint (value, ==, spa_atob (self->triggered_setting_value));
|
||||
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);
|
||||
|
||||
}else if (self->setting_type == INTEGER) {
|
||||
gint64 value = 0;
|
||||
wp_settings_get_int (self->s, setting, &value);
|
||||
gint64 svalue = 0;
|
||||
spa_atoi64 (self->triggered_setting_value, &svalue, 0);
|
||||
g_assert_cmpint (value, ==, svalue);
|
||||
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);
|
||||
|
||||
} else if (self->setting_type == STRING) {
|
||||
const gchar *value = NULL;
|
||||
wp_settings_get_string (self->s, setting, &value);
|
||||
g_assert_cmpstr (value, ==, self->triggered_setting_value);
|
||||
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_callbacks (TestSettingsFixture *self, gconstpointer data)
|
||||
{
|
||||
WpSettings *s = self->s;
|
||||
guintptr sub_id;
|
||||
|
||||
/* register callback */
|
||||
sub_id = wp_settings_subscribe (s, "test*",
|
||||
wp_settings_changed_callback, (gpointer)self);
|
||||
|
||||
{
|
||||
self->triggered_setting = "test-setting1";
|
||||
self->triggered_setting_value = "true";
|
||||
self->triggered_callback = false;
|
||||
self->setting_type = BOOLEAN;
|
||||
wp_metadata_set (self->metadata, 0, self->triggered_setting,
|
||||
"Spa:String:JSON", self->triggered_setting_value);
|
||||
g_assert_cmpint (self->triggered_callback, ==, true);
|
||||
}
|
||||
|
||||
{
|
||||
self->triggered_setting = "test-setting1";
|
||||
self->triggered_setting_value = "true";
|
||||
self->setting_type = BOOLEAN;
|
||||
self->triggered_callback = false;
|
||||
wp_metadata_set (self->metadata, 0, self->triggered_setting,
|
||||
"Spa:String:JSON", self->triggered_setting_value);
|
||||
g_assert_cmpint (self->triggered_callback, ==, false);
|
||||
}
|
||||
|
||||
{
|
||||
self->triggered_setting = "test-setting3-int";
|
||||
self->setting_type = INTEGER;
|
||||
self->triggered_setting_value = "99";
|
||||
self->triggered_callback = true;
|
||||
wp_metadata_set (self->metadata, 0, self->triggered_setting,
|
||||
"Spa:String:JSON", self->triggered_setting_value);
|
||||
g_assert_cmpint (self->triggered_callback, ==, true);
|
||||
}
|
||||
|
||||
{
|
||||
self->triggered_setting = "test-setting4-string";
|
||||
self->setting_type = STRING;
|
||||
self->triggered_setting_value = "lets not blabber";
|
||||
self->triggered_callback = true;
|
||||
wp_metadata_set (self->metadata, 0, self->triggered_setting,
|
||||
"Spa:String:JSON", self->triggered_setting_value);
|
||||
g_assert_cmpint (self->triggered_callback, ==, true);
|
||||
}
|
||||
{
|
||||
self->triggered_setting = "test-setting4-string";
|
||||
self->setting_type = STRING;
|
||||
self->triggered_setting_value = "lets blabber";
|
||||
self->triggered_callback = false;
|
||||
g_assert_cmpint (wp_settings_unsubscribe (s, sub_id), ==,
|
||||
true);
|
||||
g_assert_cmpint (wp_settings_unsubscribe (s, (sub_id-1)), ==,
|
||||
false);
|
||||
wp_metadata_set (self->metadata, 0, self->triggered_setting,
|
||||
"Spa:String:JSON", self->triggered_setting_value);
|
||||
g_assert_cmpint (self->triggered_callback, ==, false);
|
||||
}
|
||||
}
|
||||
|
||||
gint
|
||||
main (gint argc, gchar *argv[])
|
||||
{
|
||||
@@ -573,6 +679,8 @@ main (gint argc, gchar *argv[])
|
||||
test_wpsettings_setup, test_wpsettings, test_wpsettings_teardown);
|
||||
g_test_add ("/wp/settings/wpsettings-creation-rules", TestSettingsFixture, NULL,
|
||||
test_wpsettings_setup, test_rules, test_wpsettings_teardown);
|
||||
g_test_add ("/wp/settings/wpsettings-callbacks", TestSettingsFixture, NULL,
|
||||
test_wpsettings_setup, test_callbacks, test_wpsettings_teardown);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
@@ -64,14 +64,14 @@ context.modules = [
|
||||
]
|
||||
|
||||
wireplumber.settings = {
|
||||
test-property1 = "false"
|
||||
test-property2 = "true"
|
||||
test-property3-int = "-20"
|
||||
test-property4-max-int = "9223372036854775807"
|
||||
test-property4-max-int-one-more = "9223372036854775808"
|
||||
test-property4-min-int = -9223372036854775808
|
||||
test-property4-min-int-one-less = -9223372036854775809
|
||||
test-property4-string = "blahblah"
|
||||
test-setting1 = "false"
|
||||
test-setting2 = "true"
|
||||
test-setting3-int = "-20"
|
||||
test-setting4-max-int = "9223372036854775807"
|
||||
test-setting4-max-int-one-more = "9223372036854775808"
|
||||
test-setting4-min-int = -9223372036854775808
|
||||
test-setting4-min-int-one-less = -9223372036854775809
|
||||
test-setting4-string = "blahblah"
|
||||
rule_one = [
|
||||
{
|
||||
matches = [
|
||||
@@ -129,8 +129,8 @@ wireplumber.settings = {
|
||||
}
|
||||
}
|
||||
]
|
||||
test-property-float1 = "3.14"
|
||||
test-property-float2 = "0.4"
|
||||
test-setting-float1 = "3.14"
|
||||
test-setting-float2 = "0.4"
|
||||
rule_three = [
|
||||
{
|
||||
matches = [
|
||||
@@ -167,6 +167,6 @@ wireplumber.settings = {
|
||||
}
|
||||
}
|
||||
]
|
||||
test-prop1-json = "[ a b c ]"
|
||||
test-prop-strings = ["test1", "test 2", "test three", "test-four"]
|
||||
test-setting-json = "[ a b c ]"
|
||||
test-setting-strings = ["test1", "test 2", "test three", "test-four"]
|
||||
}
|
||||
|
@@ -2,59 +2,62 @@
|
||||
-- tests the lua API of WpSettings, this file tests the settings present in
|
||||
-- .conf file that is loaded.
|
||||
|
||||
Script.async_activation = true
|
||||
|
||||
-- test settings _get_boolean ()
|
||||
local value = Settings.get_boolean ("test-property1", "test-settings")
|
||||
|
||||
local value = Settings.get_boolean ("test-setting1", "test-settings")
|
||||
assert (value == false)
|
||||
|
||||
value = Settings.get_boolean ("test-property2", "test-settings")
|
||||
value = Settings.get_boolean ("test-setting2", "test-settings")
|
||||
assert ("boolean" == type (value))
|
||||
assert (value == true)
|
||||
|
||||
value = Settings.get_boolean ("test-property1")
|
||||
value = Settings.get_boolean ("test-setting1")
|
||||
assert (value == nil)
|
||||
|
||||
value = Settings.get_boolean ("test-property-undefined",
|
||||
value = Settings.get_boolean ("test-setting-undefined",
|
||||
"test-settings")
|
||||
assert (value == nil)
|
||||
|
||||
|
||||
-- test settings _get_int ()
|
||||
value = Settings.get_int ("test-property-undefined", "test-settings")
|
||||
value = Settings.get_int ("test-setting-undefined", "test-settings")
|
||||
assert (value == nil)
|
||||
|
||||
value = Settings.get_int ("test-property3-int", "test-settings")
|
||||
value = Settings.get_int ("test-setting3-int", "test-settings")
|
||||
assert ("number" == type (value))
|
||||
assert (value == -20)
|
||||
|
||||
value = Settings.get_int ("test-property4-max-int", "test-settings")
|
||||
value = Settings.get_int ("test-setting4-max-int", "test-settings")
|
||||
assert (value == 9223372036854775807)
|
||||
|
||||
value = Settings.get_int ("test-property4-min-int", "test-settings")
|
||||
value = Settings.get_int ("test-setting4-min-int", "test-settings")
|
||||
assert (value == -9223372036854775808)
|
||||
|
||||
value = Settings.get_int ("test-property4-max-int-one-more",
|
||||
value = Settings.get_int ("test-setting4-max-int-one-more",
|
||||
"test-settings")
|
||||
assert (value == 0)
|
||||
|
||||
value = Settings.get_int ("test-property4-min-int-one-less",
|
||||
value = Settings.get_int ("test-setting4-min-int-one-less",
|
||||
"test-settings")
|
||||
assert (value == 0)
|
||||
|
||||
-- test settings _get_string ()
|
||||
value = Settings.get_string ("test-property-undefined", "test-settings")
|
||||
value = Settings.get_string ("test-setting-undefined", "test-settings")
|
||||
assert (value == nil)
|
||||
|
||||
value = Settings.get_string ("test-property4-string", "test-settings")
|
||||
value = Settings.get_string ("test-setting4-string", "test-settings")
|
||||
assert ("string" == type (value))
|
||||
assert (value == "blahblah")
|
||||
|
||||
value = Settings.get_string ("test-property3-int", "test-settings")
|
||||
value = Settings.get_string ("test-setting3-int", "test-settings")
|
||||
assert (value == "-20")
|
||||
|
||||
value = Settings.get_string ("test-prop1-json", "test-settings")
|
||||
value = Settings.get_string ("test-setting-json", "test-settings")
|
||||
assert (value == "[ a b c ]")
|
||||
|
||||
value = Settings.get_string ("test-prop-strings", "test-settings")
|
||||
value = Settings.get_string ("test-setting-strings", "test-settings")
|
||||
assert (value == "[\"test1\", \"test 2\", \"test three\", \"test-four\"]")
|
||||
json = Json.Raw (value)
|
||||
assert (json:is_array())
|
||||
@@ -69,14 +72,14 @@ assert (json:get_data() ==
|
||||
"[\"test1\", \"test 2\", \"test three\", \"test-four\"]")
|
||||
|
||||
-- test settings _get_float ()
|
||||
value = Settings.get_float ("test-property-undefined", "test-settings")
|
||||
value = Settings.get_float ("test-setting-undefined", "test-settings")
|
||||
assert (value == nil)
|
||||
|
||||
value = Settings.get_float ("test-property-float1", "test-settings")
|
||||
value = Settings.get_float ("test-setting-float1", "test-settings")
|
||||
assert ("number" == type (value))
|
||||
assert ((value - 3.14) < 0.00001)
|
||||
|
||||
value = Settings.get_float ("test-property-float2", "test-settings")
|
||||
value = Settings.get_float ("test-setting-float2", "test-settings")
|
||||
assert ((value - 0.4) < 0.00001)
|
||||
|
||||
|
||||
@@ -190,3 +193,98 @@ assert (applied == true)
|
||||
assert (ap["prop.electrical.conductivity"] == "true")
|
||||
assert (ap["prop.state"] == "solid")
|
||||
assert (ap["prop.example"] == "ferrous")
|
||||
|
||||
-- test callbacks
|
||||
|
||||
metadata_om = ObjectManager {
|
||||
Interest {
|
||||
type = "metadata",
|
||||
Constraint { "metadata.name", "=", "test-settings" },
|
||||
}
|
||||
}
|
||||
|
||||
metadata_om:activate()
|
||||
|
||||
local setting
|
||||
local setting_value
|
||||
local callback
|
||||
local setting_type
|
||||
local finish_activation
|
||||
|
||||
function callback (obj, s, rawvalue)
|
||||
if (setting_type == "boolean") then
|
||||
assert (s == setting)
|
||||
callback = true
|
||||
assert (rawvalue == tostring(setting_value))
|
||||
assert ((setting_value and true or false) ==
|
||||
Settings.get_boolean (s, "test-settings"))
|
||||
|
||||
elseif (setting_type == "integer") then
|
||||
assert (s == setting)
|
||||
callback = true
|
||||
assert (rawvalue == tostring(setting_value))
|
||||
assert (setting_value ==
|
||||
Settings.get_int (s, "test-settings"))
|
||||
|
||||
elseif (setting_type == "string") then
|
||||
assert (s == setting)
|
||||
callback = true
|
||||
assert (rawvalue == setting_value)
|
||||
assert (setting_value ==
|
||||
Settings.get_string (s, "test-settings"))
|
||||
end
|
||||
|
||||
if (finish_activation) then
|
||||
assert (Settings.unsubscribe (sub_id, "test-settings"))
|
||||
assert (not Settings.unsubscribe (sub_id-1, "test-settings"))
|
||||
Script:finish_activation()
|
||||
end
|
||||
end
|
||||
|
||||
sub_id = Settings.subscribe ("test*", "test-settings", callback)
|
||||
|
||||
metadata_om:connect("objects-changed", function (om)
|
||||
local metadata = om:lookup()
|
||||
|
||||
if (not metadata) then
|
||||
return
|
||||
end
|
||||
|
||||
-- test #1
|
||||
setting = "test-setting1"
|
||||
setting_value = true
|
||||
callback = false
|
||||
setting_type = "boolean"
|
||||
|
||||
metadata:set(0, setting, "Spa:String:JSON", tostring(setting_value))
|
||||
assert (callback)
|
||||
|
||||
-- test #2
|
||||
setting = "test-setting1"
|
||||
setting_value = true
|
||||
callback = false
|
||||
setting_type = "boolean"
|
||||
|
||||
metadata:set(0, setting, "Spa:String:JSON", tostring(setting_value))
|
||||
assert (not callback)
|
||||
|
||||
-- test #3
|
||||
setting = "test-setting3-int"
|
||||
setting_value = 99
|
||||
callback = false
|
||||
setting_type = "integer"
|
||||
|
||||
metadata:set(0, setting, "Spa:String:JSON", setting_value)
|
||||
assert (callback)
|
||||
|
||||
-- test #4
|
||||
setting = "test-setting4-string"
|
||||
setting_value = "lets not blabber"
|
||||
callback = false
|
||||
setting_type = "string"
|
||||
|
||||
finish_activation = true
|
||||
metadata:set(0, setting, "Spa:String:JSON", setting_value)
|
||||
assert (callback)
|
||||
|
||||
end)
|
||||
|
Reference in New Issue
Block a user