From 3d642460e756a57824db8d7d127206ac8f154d15 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sun, 26 Jul 2020 20:29:00 +0100 Subject: [PATCH] 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 | 199 +++++++++++++++++++++++++++++------------ gtk/gtktestatcontext.h | 142 +++++++++++++++-------------- 2 files changed, 215 insertions(+), 126 deletions(-) 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,6 +258,50 @@ gtk_test_accessible_has_relation (GtkAccessible *accessible, return gtk_at_context_has_accessible_relation (context, relation); } +/** + * 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 *res = NULL; + va_list args; + + va_start (args, relation); + + GtkAccessibleValue *check_value = + gtk_accessible_value_collect_for_relation (relation, &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_relation (context, relation); + + 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; +} + void gtk_test_accessible_assertion_message_role (const char *domain, const char *file, @@ -177,69 +309,18 @@ gtk_test_accessible_assertion_message_role (const char *domain, const char *func, const char *expr, GtkAccessible *accessible, - GtkAccessibleRole role) + GtkAccessibleRole expected_role, + GtkAccessibleRole actual_role) { - char *role_name = g_enum_to_string (GTK_TYPE_ACCESSIBLE_ROLE, role); - char *s = g_strdup_printf ("%s:accessible-role == %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), - role_name); + 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); } - -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)); - - g_assertion_message_expr (domain, file, line, func, s); - - g_free (s); -} - -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)); - - g_assertion_message_expr (domain, file, line, func, s); - - g_free (s); -} - -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) -{ - char *s = g_strdup_printf ("%s:accessible-relation == %s", - G_OBJECT_TYPE_NAME (accessible), - gtk_accessible_relation_get_attribute_name (relation)); - - g_assertion_message_expr (domain, file, line, func, s); - - g_free (s); -} diff --git a/gtk/gtktestatcontext.h b/gtk/gtktestatcontext.h index 1d99a7cf1f..99bf57989f 100644 --- a/gtk/gtktestatcontext.h +++ b/gtk/gtktestatcontext.h @@ -36,65 +36,83 @@ G_BEGIN_DECLS * Checks whether a #GtkAccessible implementation has the given @role, * and raises an assertion if the condition is failed. */ -#define gtk_test_accessible_assert_role(accessible,role) G_STMT_START { \ - GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ - GtkAccessibleRole __r = (role); \ - if (gtk_test_accessible_has_role (__a, __r)) ; else \ - gtk_test_accessible_assertion_message_role (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #accessible " == " #role, \ - __a, __r); \ - } G_STMT_END +#define gtk_test_accessible_assert_role(accessible,role) \ +G_STMT_START { \ + GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ + GtkAccessibleRole __r1 = (role); \ + GtkAccessibleRole __r2 = gtk_accessible_get_accessible_role (__a); \ + if (__r1 == __r2) ; else { \ + gtk_test_accessible_assertion_message_role (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + #accessible ".accessible-role == " #role, \ + __a, __r1, __r2); \ + } \ +} G_STMT_END + /** * gtk_test_accessible_assert_property: * @accessible: a #GtkAccessible * @property: a #GtkAccessibleProperty + * @value: the value of @property * - * Checks whether a #GtkAccessible implementation contains the - * given @property, and raises an assertion if the condition is - * failed. + * Checks whether a #GtkAccessible implementation has its accessible + * property set to the expected @value, and raises an assertion if the + * condition is not satisfied. */ -#define gtk_test_accessible_assert_property(accessible,property) G_STMT_START { \ - GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ - GtkAccessibleProperty __p = (property); \ - if (gtk_test_accessible_has_property (__a, __p)) ; else \ - gtk_test_accessible_assertion_message_property (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #accessible " has " #property, \ - __a, __p); \ - } G_STMT_END +#define gtk_test_accessible_assert_property(accessible,property,value) \ +G_STMT_START { \ + GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ + GtkAccessibleProperty __p = (property); \ + char *__value = gtk_test_accessible_check_property (__a, __p, (value)); \ + if (__value == NULL) ; else { \ + char *__msg = g_strdup_printf ("assertion failed: (" #accessible ".accessible-property(" #property ") == " #value ": value = '%s'", __value); \ + g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \ + g_free (__msg); \ + } \ +} G_STMT_END + /** * gtk_test_accessible_assert_relation: * @accessible: a #GtkAccessible * @relation: a #GtkAccessibleRelation + * @value: the expected value of @relation * - * Checks whether a #GtkAccessible implementation contains the - * given @relation, and raises an assertion if the condition is - * failed. + * Checks whether a #GtkAccessible implementation has its accessible + * relation set to the expected @value, and raises an assertion if the + * condition is not satisfied. */ -#define gtk_test_accessible_assert_relation(accessible,relation) G_STMT_START { \ - GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ - GtkAccessibleRelation __r = (relation); \ - if (gtk_test_accessible_has_relation (__a, __r)) ; else \ - gtk_test_accessible_assertion_message_relation (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #accessible " has " #relation, \ - __a, __r); \ - } G_STMT_END +#define gtk_test_accessible_assert_relation(accessible,relation,value) \ +G_STMT_START { \ + GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ + GtkAccessibleRelation __r = (relation); \ + char *__value = gtk_test_accessible_check_relation (__a, __r, (value)); \ + if (__value == NULL); else { \ + char *__msg = g_strdup_printf ("assertion failed: (" #accessible ".accessible-relation(" #relation ") == " #value ": value = '%s'", __value); \ + g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \ + g_free (__msg); \ + } \ +} G_STMT_END + /** * gtk_test_accessible_assert_state: * @accessible: a #GtkAccessible - * @state: a #GtkAccessibleState + * @state: a #GtkAccessibleRelation + * @value: the expected value of @state * - * Checks whether a #GtkAccessible implementation contains the - * given @state, and raises an assertion if the condition is - * failed. + * Checks whether a #GtkAccessible implementation has its accessible + * state set to the expected @value, and raises an assertion if the + * condition is not satisfied. */ -#define gtk_test_accessible_assert_state(accessible,state) G_STMT_START { \ - GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ - GtkAccessibleState __s = (state); \ - if (gtk_test_accessible_has_state (__a, __s)) ; else \ - gtk_test_accessible_assertion_message_state (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ - #accessible " has " #state, \ - __a, __s); \ - } G_STMT_END +#define gtk_test_accessible_assert_state(accessible,state,value) \ +G_STMT_START { \ + GtkAccessible *__a = GTK_ACCESSIBLE (accessible); \ + GtkAccessibleRelation __s = (state); \ + char *__value = gtk_test_accessible_check_state (__a, __s, (value)); \ + if (__value == NULL); else { \ + char *__msg = g_strdup_printf ("assertion failed: (" #accessible ".accessible-state(" #state ") == " #value ": value = '%s'", __value); \ + g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \ + g_free (__msg); \ + } \ +} G_STMT_END GDK_AVAILABLE_IN_ALL gboolean gtk_test_accessible_has_role (GtkAccessible *accessible, @@ -109,6 +127,19 @@ GDK_AVAILABLE_IN_ALL gboolean gtk_test_accessible_has_state (GtkAccessible *accessible, GtkAccessibleState state); +GDK_AVAILABLE_IN_ALL +char * gtk_test_accessible_check_property (GtkAccessible *accessible, + GtkAccessibleProperty property, + ...); +GDK_AVAILABLE_IN_ALL +char * gtk_test_accessible_check_relation (GtkAccessible *accessible, + GtkAccessibleRelation relation, + ...); +GDK_AVAILABLE_IN_ALL +char * gtk_test_accessible_check_state (GtkAccessible *accessible, + GtkAccessibleState state, + ...); + GDK_AVAILABLE_IN_ALL void gtk_test_accessible_assertion_message_role (const char *domain, const char *file, @@ -116,30 +147,7 @@ void gtk_test_accessible_assertion_message_role (const char *doma const char *func, const char *expr, GtkAccessible *accessible, - GtkAccessibleRole role) G_GNUC_NORETURN; -GDK_AVAILABLE_IN_ALL -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) G_GNUC_NORETURN; -GDK_AVAILABLE_IN_ALL -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) G_GNUC_NORETURN; -GDK_AVAILABLE_IN_ALL -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) G_GNUC_NORETURN; + GtkAccessibleRole expected_role, + GtkAccessibleRole actual_role); G_END_DECLS