[gtk/a11y-work: 4/5] a11y: Set an accessible role for GtkExpander




commit 699481bc7dbb15a603f9c9bd8192da33c2c3e18b
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jul 29 21:55:34 2020 -0400

    a11y: Set an accessible role for GtkExpander
    
    Use the button accessible role for GtkExpander
    and set attributes as appropriate.
    
    Update the documentation and add a test.

 docs/reference/gtk/section-accessibility.md |  2 +-
 gtk/gtkexpander.c                           | 23 ++++++++++++
 testsuite/a11y/expander.c                   | 57 +++++++++++++++++++++++++++++
 testsuite/a11y/meson.build                  |  1 +
 4 files changed, 82 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/gtk/section-accessibility.md b/docs/reference/gtk/section-accessibility.md
index d4bc80590c..4c9b158732 100644
--- a/docs/reference/gtk/section-accessibility.md
+++ b/docs/reference/gtk/section-accessibility.md
@@ -46,7 +46,7 @@ Each role name is part of the #GtkAccessibleRole enumeration.
 | Role name | Description | Related GTK widget |
 |-----------|-------------|--------------------|
 | `ALERT` | A message with important information | - |
-| `BUTTON` | A control that performs an action when pressed | #GtkButton, #GtkLinkButton |
+| `BUTTON` | A control that performs an action when pressed | #GtkButton, #GtkLinkButton, #GtkExpander |
 | `CHECKBOX` | A control that has three possible value: `true`, `false`, or `undefined` | #GtkCheckButton |
 | `COLUMNHEADER` | The header of a column in a list or grid | - |
 | `COMBOBOX` | A control that can be expanded to show a list of possible values to select | #GtkComboBox |
diff --git a/gtk/gtkexpander.c b/gtk/gtkexpander.c
index 7993e506d4..6fc389de62 100644
--- a/gtk/gtkexpander.c
+++ b/gtk/gtkexpander.c
@@ -106,6 +106,10 @@
  * GtkExpander has three CSS nodes, the main node with the name expander,
  * a subnode with name title and node below it with name arrow. The arrow of an
  * expander that is showing its child gets the :checked pseudoclass added to it.
+ *
+ * # Accessibility
+ *
+ * GtkExpander uses the #GTK_ACCESSIBLE_ROLE_BUTTON role.
  */
 
 #include "config.h"
@@ -374,6 +378,7 @@ gtk_expander_class_init (GtkExpanderClass *klass)
                   G_TYPE_NONE, 0);
 
   gtk_widget_class_set_css_name (widget_class, I_("expander-widget"));
+  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_BUTTON);
 }
 
 static void
@@ -420,6 +425,11 @@ gtk_expander_init (GtkExpander *expander)
   gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
                                               GTK_PHASE_BUBBLE);
   gtk_widget_add_controller (GTK_WIDGET (expander->title_widget), GTK_EVENT_CONTROLLER (gesture));
+
+  gtk_accessible_update_state (GTK_ACCESSIBLE (expander),
+                               GTK_ACCESSIBLE_STATE_EXPANDED, FALSE,
+                               -1);
+
 }
 
 static GtkBuildableIface *parent_buildable_iface;
@@ -877,6 +887,10 @@ gtk_expander_set_expanded (GtkExpander *expander,
       gtk_expander_resize_toplevel (expander);
     }
 
+  gtk_accessible_update_state (GTK_ACCESSIBLE (expander),
+                               GTK_ACCESSIBLE_STATE_EXPANDED, expanded,
+                               -1);
+
   g_object_notify (G_OBJECT (expander), "expanded");
 }
 
@@ -1164,6 +1178,8 @@ void
 gtk_expander_set_child (GtkExpander *expander,
                         GtkWidget   *child)
 {
+  GList *list = NULL;
+
   g_return_if_fail (GTK_IS_EXPANDER (expander));
   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
 
@@ -1188,6 +1204,13 @@ gtk_expander_set_child (GtkExpander *expander,
         }
     }
 
+  if (expander->child)
+    list = g_list_append (list, expander->child);
+  gtk_accessible_update_relation (GTK_ACCESSIBLE (expander),
+                                  GTK_ACCESSIBLE_RELATION_CONTROLS, list,
+                                  -1);
+  g_list_free (list);
+
   g_object_notify (G_OBJECT (expander), "child");
 }
 
diff --git a/testsuite/a11y/expander.c b/testsuite/a11y/expander.c
new file mode 100644
index 0000000000..169898f796
--- /dev/null
+++ b/testsuite/a11y/expander.c
@@ -0,0 +1,57 @@
+#include <gtk/gtk.h>
+
+static void
+expander_role (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_role (widget, GTK_ACCESSIBLE_ROLE_BUTTON);
+
+  g_object_unref (widget);
+}
+
+static void
+expander_state (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  g_object_ref_sink (widget);
+
+  gtk_test_accessible_assert_state (widget, GTK_ACCESSIBLE_STATE_EXPANDED, FALSE);
+
+  gtk_expander_set_expanded (GTK_EXPANDER (widget), TRUE);
+
+  gtk_test_accessible_assert_state (widget, GTK_ACCESSIBLE_STATE_EXPANDED, TRUE);
+
+  g_object_unref (widget);
+}
+
+static void
+expander_relations (void)
+{
+  GtkWidget *widget = gtk_expander_new ("Hello");
+  GtkWidget *child = gtk_label_new ("Child");
+  GList *list;
+
+  g_object_ref_sink (widget);
+
+  gtk_expander_set_child (GTK_EXPANDER (widget), child);
+
+  list = g_list_append (NULL, child);
+  gtk_test_accessible_assert_relation (widget, GTK_ACCESSIBLE_RELATION_CONTROLS, list);
+  g_list_free (list);
+
+  g_object_unref (widget);
+}
+
+int
+main (int argc, char *argv[])
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/a11y/expander/role", expander_role);
+  g_test_add_func ("/a11y/expander/state", expander_state);
+  g_test_add_func ("/a11y/expander/relations", expander_relations);
+
+  return g_test_run ();
+}
diff --git a/testsuite/a11y/meson.build b/testsuite/a11y/meson.build
index 2c25837dbb..1244a521f8 100644
--- a/testsuite/a11y/meson.build
+++ b/testsuite/a11y/meson.build
@@ -15,6 +15,7 @@ tests = [
   { 'name': 'checkbutton' },
   { 'name': 'dialog' },
   { 'name': 'entry' },
+  { 'name': 'expander' },
   { 'name': 'image' },
   { 'name': 'label' },
   { 'name': 'passwordentry' },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]