From 3d642460e756a57824db8d7d127206ac8f154d15 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sun, 26 Jul 2020 20:29:00 +0100 Subject: a11y: Simplify the test API We don't need as many functions to print out the property, relation, and state of an accessible. Additionally, we should allow comparing the accessible attributes with an expected value, and print out the real accessible value if they do not match. --- gtk/gtktestatcontext.c | 191 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 136 insertions(+), 55 deletions(-) (limited to 'gtk/gtktestatcontext.c') diff --git a/gtk/gtktestatcontext.c b/gtk/gtktestatcontext.c index a88b7af3fa..f6c401b339 100644 --- a/gtk/gtktestatcontext.c +++ b/gtk/gtktestatcontext.c @@ -140,6 +140,50 @@ gtk_test_accessible_has_property (GtkAccessible *accessible, return gtk_at_context_has_accessible_property (context, property); } +/** + * gtk_test_accessible_check_property: + * @accessible: a #GtkAccessible + * @property: a #GtkAccessibleProperty + * @...: the expected value of @property + * + * Checks whether the accessible @property of @accessible is set to + * a specific value. + * + * Returns: (transfer full): the value of the accessible property + */ +char * +gtk_test_accessible_check_property (GtkAccessible *accessible, + GtkAccessibleProperty property, + ...) +{ + char *res = NULL; + va_list args; + + va_start (args, property); + + GtkAccessibleValue *check_value = + gtk_accessible_value_collect_for_property (property, &args); + + va_end (args); + + if (check_value == NULL) + return g_strdup ("undefined"); + + GtkATContext *context = gtk_accessible_get_at_context (accessible); + GtkAccessibleValue *real_value = + gtk_at_context_get_accessible_property (context, property); + + if (gtk_accessible_value_equal (check_value, real_value)) + goto out; + + res = gtk_accessible_value_to_string (real_value); + +out: + gtk_accessible_value_unref (check_value); + + return res; +} + gboolean gtk_test_accessible_has_state (GtkAccessible *accessible, GtkAccessibleState state) @@ -155,6 +199,50 @@ gtk_test_accessible_has_state (GtkAccessible *accessible, return gtk_at_context_has_accessible_state (context, state); } +/** + * gtk_test_accessible_check_state: + * @accessible: a #GtkAccessible + * @state: a #GtkAccessibleState + * @...: the expected value of @state + * + * Checks whether the accessible @state of @accessible is set to + * a specific value. + * + * Returns: (transfer full): the value of the accessible state + */ +char * +gtk_test_accessible_check_state (GtkAccessible *accessible, + GtkAccessibleState state, + ...) +{ + char *res = NULL; + va_list args; + + va_start (args, state); + + GtkAccessibleValue *check_value = + gtk_accessible_value_collect_for_state (state, &args); + + va_end (args); + + if (check_value == NULL) + return g_strdup ("undefined"); + + GtkATContext *context = gtk_accessible_get_at_context (accessible); + GtkAccessibleValue *real_value = + gtk_at_context_get_accessible_state (context, state); + + if (gtk_accessible_value_equal (check_value, real_value)) + goto out; + + res = gtk_accessible_value_to_string (real_value); + +out: + gtk_accessible_value_unref (check_value); + + return res; +} + gboolean gtk_test_accessible_has_relation (GtkAccessible *accessible, GtkAccessibleRelation relation) @@ -170,76 +258,69 @@ gtk_test_accessible_has_relation (GtkAccessible *accessible, return gtk_at_context_has_accessible_relation (context, relation); } -void -gtk_test_accessible_assertion_message_role (const char *domain, - const char *file, - int line, - const char *func, - const char *expr, - GtkAccessible *accessible, - GtkAccessibleRole role) +/** + * gtk_test_accessible_check_relation: + * @accessible: a #GtkAccessible + * @relation: a #GtkAccessibleRelation + * @...: the expected value of @relation + * + * Checks whether the accessible @relation of @accessible is set to + * a specific value. + * + * Returns: (transfer full): the value of the accessible relation + */ +char * +gtk_test_accessible_check_relation (GtkAccessible *accessible, + GtkAccessibleRelation relation, + ...) { - char *role_name = g_enum_to_string (GTK_TYPE_ACCESSIBLE_ROLE, role); - char *s = g_strdup_printf ("%s:accessible-role == %s", - G_OBJECT_TYPE_NAME (accessible), - role_name); + char *res = NULL; + va_list args; - g_assertion_message_expr (domain, file, line, func, s); + va_start (args, relation); - g_free (role_name); - g_free (s); -} + GtkAccessibleValue *check_value = + gtk_accessible_value_collect_for_relation (relation, &args); -void -gtk_test_accessible_assertion_message_property (const char *domain, - const char *file, - int line, - const char *func, - const char *expr, - GtkAccessible *accessible, - GtkAccessibleProperty property) -{ - char *s = g_strdup_printf ("%s:accessible-property == %s", - G_OBJECT_TYPE_NAME (accessible), - gtk_accessible_property_get_attribute_name (property)); + va_end (args); - g_assertion_message_expr (domain, file, line, func, s); + if (check_value == NULL) + return g_strdup ("undefined"); - g_free (s); -} + GtkATContext *context = gtk_accessible_get_at_context (accessible); + GtkAccessibleValue *real_value = + gtk_at_context_get_accessible_relation (context, relation); -void -gtk_test_accessible_assertion_message_state (const char *domain, - const char *file, - int line, - const char *func, - const char *expr, - GtkAccessible *accessible, - GtkAccessibleState state) -{ - char *s = g_strdup_printf ("%s:accessible-state == %s", - G_OBJECT_TYPE_NAME (accessible), - gtk_accessible_state_get_attribute_name (state)); + if (gtk_accessible_value_equal (check_value, real_value)) + goto out; - g_assertion_message_expr (domain, file, line, func, s); + res = gtk_accessible_value_to_string (real_value); - g_free (s); +out: + gtk_accessible_value_unref (check_value); + + return res; } void -gtk_test_accessible_assertion_message_relation (const char *domain, - const char *file, - int line, - const char *func, - const char *expr, - GtkAccessible *accessible, - GtkAccessibleRelation relation) +gtk_test_accessible_assertion_message_role (const char *domain, + const char *file, + int line, + const char *func, + const char *expr, + GtkAccessible *accessible, + GtkAccessibleRole expected_role, + GtkAccessibleRole actual_role) { - char *s = g_strdup_printf ("%s:accessible-relation == %s", + char *role_name = g_enum_to_string (GTK_TYPE_ACCESSIBLE_ROLE, actual_role); + char *s = g_strdup_printf ("assertion failed: (%s): %s.accessible-role = %s (%d)", + expr, G_OBJECT_TYPE_NAME (accessible), - gtk_accessible_relation_get_attribute_name (relation)); + role_name, + actual_role); - g_assertion_message_expr (domain, file, line, func, s); + g_assertion_message (domain, file, line, func, s); + g_free (role_name); g_free (s); } -- cgit v1.2.1