[GTK][a11y] Signal state-changed:selected is not emitted for listbox elements when building with ATSPI
https://bugs.webkit.org/show_bug.cgi?id=233521

Reviewed by Adrian Perez de Castro.

Source/WebCore:

Post a selected change notification for option elements when the state changes.

* accessibility/atspi/AXObjectCacheAtspi.cpp:
(WebCore::AXObjectCache::postPlatformNotification):
* html/HTMLOptionElement.cpp:
(WebCore::HTMLOptionElement::setSelectedState):

Tools:

Add test cases to check that the signal is now emitted as expected.

* TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
(AccessibilityTest::isSelected):
(testAccessibleStateChanged):
(testSelectionListBox):
(testSelectionMenuList):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@286189 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index ff93693..32278e0 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,17 @@
+2021-11-27  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK][a11y] Signal state-changed:selected is not emitted for listbox elements when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=233521
+
+        Reviewed by Adrian Perez de Castro.
+
+        Post a selected change notification for option elements when the state changes.
+
+        * accessibility/atspi/AXObjectCacheAtspi.cpp:
+        (WebCore::AXObjectCache::postPlatformNotification):
+        * html/HTMLOptionElement.cpp:
+        (WebCore::HTMLOptionElement::setSelectedState):
+
 2021-11-27  Antti Koivisto  <antti@apple.com>
 
         [:has() pseudo-class] Invalidation support for adding and removing elements
diff --git a/Source/WebCore/accessibility/atspi/AXObjectCacheAtspi.cpp b/Source/WebCore/accessibility/atspi/AXObjectCacheAtspi.cpp
index 6b81c6f..80fb6e8 100644
--- a/Source/WebCore/accessibility/atspi/AXObjectCacheAtspi.cpp
+++ b/Source/WebCore/accessibility/atspi/AXObjectCacheAtspi.cpp
@@ -80,9 +80,15 @@
             wrapper->stateChanged("checked", coreObject->isChecked());
         break;
     case AXSelectedStateChanged:
-    case AXMenuListItemSelected:
         wrapper->stateChanged("selected", coreObject->isSelected());
         break;
+    case AXMenuListItemSelected: {
+        // Menu list popup items are handled by AXSelectedStateChanged.
+        auto* parent = coreObject->parentObjectUnignored();
+        if (parent && !parent->isMenuListPopup())
+            wrapper->stateChanged("selected", coreObject->isSelected());
+        break;
+    }
     case AXSelectedChildrenChanged:
         wrapper->selectionChanged();
         break;
diff --git a/Source/WebCore/html/HTMLOptionElement.cpp b/Source/WebCore/html/HTMLOptionElement.cpp
index b725de2..077d72a 100644
--- a/Source/WebCore/html/HTMLOptionElement.cpp
+++ b/Source/WebCore/html/HTMLOptionElement.cpp
@@ -27,7 +27,9 @@
 #include "config.h"
 #include "HTMLOptionElement.h"
 
+#include "AXObjectCache.h"
 #include "Document.h"
+#include "DocumentInlines.h"
 #include "ElementAncestorIterator.h"
 #include "HTMLDataListElement.h"
 #include "HTMLNames.h"
@@ -234,6 +236,11 @@
 
     m_isSelected = selected;
     invalidateStyleForSubtree();
+
+#if ENABLE(ACCESSIBILITY) && USE(ATSPI)
+    if (auto* cache = document().existingAXObjectCache())
+        cache->postNotification(this, AXObjectCache::AXSelectedStateChanged);
+#endif
 }
 
 void HTMLOptionElement::childrenChanged(const ChildChange& change)
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 074459b..e64af24 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,18 @@
+2021-11-27  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK][a11y] Signal state-changed:selected is not emitted for listbox elements when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=233521
+
+        Reviewed by Adrian Perez de Castro.
+
+        Add test cases to check that the signal is now emitted as expected.
+
+        * TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
+        (AccessibilityTest::isSelected):
+        (testAccessibleStateChanged):
+        (testSelectionListBox):
+        (testSelectionMenuList):
+
 2021-11-26  Lauro Moura  <lmoura@igalia.com>
 
         [GTK] Gardening API test WebKit.OnDeviceChangedCrash as flaky timeout
diff --git a/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp b/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp
index 5efb443..24671ae 100644
--- a/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp
+++ b/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp
@@ -152,6 +152,12 @@
         return length;
     }
 
+    static bool isSelected(AtspiAccessible* element)
+    {
+        GRefPtr<AtspiStateSet> set = adoptGRef(atspi_accessible_get_state_set(element));
+        return atspi_state_set_contains(set.get(), ATSPI_STATE_SELECTED);
+    }
+
 private:
     AtspiAccessible* m_eventSource { nullptr };
 
@@ -668,13 +674,15 @@
 
 static void testAccessibleStateChanged(AccessibilityTest* test, gconstpointer)
 {
-    test->showInWindow();
+    test->showInWindow(800, 600);
     test->loadHtml(
         "<html>"
         "  <body>"
         "    <input id='check' type='checkbox'/>Checkbox"
         "    <button id='toggle' aria-pressed='true'>Toggle button</button>"
         "    <input id='entry' value='Entry'>"
+        "    <select id='list' size='2'><option>1</option><option>2</option></select>"
+        "    <select id='combo'><option>1</option><option>2</option></select>"
         "  </body>"
         "</html>",
         nullptr);
@@ -689,7 +697,7 @@
 
     auto section = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr));
     g_assert_true(ATSPI_IS_ACCESSIBLE(section.get()));
-    g_assert_cmpint(atspi_accessible_get_child_count(section.get(), nullptr), ==, 3);
+    g_assert_cmpint(atspi_accessible_get_child_count(section.get(), nullptr), ==, 5);
 
     unsigned nextChild = 0;
     auto checkbox = adoptGRef(atspi_accessible_get_child_at_index(section.get(), nextChild++, nullptr));
@@ -741,6 +749,66 @@
     } else
         g_assert_not_reached();
     events = { };
+
+#if USE(ATSPI)
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('list').focus();", nullptr);
+    auto listBox = adoptGRef(atspi_accessible_get_child_at_index(section.get(), nextChild++, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(listBox.get()));
+    auto option1 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(option1.get()));
+    auto option2 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(option2.get()));
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option2.get()));
+    test->startEventMonitor(option1.get(), { "object:state-changed" });
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('list').selectedIndex = 0;", nullptr);
+    events = test->stopEventMonitor(1);
+    g_assert_cmpuint(events.size(), ==, 1);
+    g_assert_cmpstr(events[0]->type, ==, "object:state-changed:selected");
+    g_assert_cmpuint(events[0]->detail1, ==, 1);
+    events = { };
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option2.get()));
+    test->startEventMonitor(option1.get(), { "object:state-changed" });
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('list').selectedIndex = 1;", nullptr);
+    events = test->stopEventMonitor(1);
+    g_assert_cmpuint(events.size(), ==, 1);
+    g_assert_cmpstr(events[0]->type, ==, "object:state-changed:selected");
+    g_assert_cmpuint(events[0]->detail1, ==, 0);
+    events = { };
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
+    g_assert_true(AccessibilityTest::isSelected(option2.get()));
+
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('combo').focus();", nullptr);
+    auto combo = adoptGRef(atspi_accessible_get_child_at_index(section.get(), nextChild++, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(combo.get()));
+    auto menuList = adoptGRef(atspi_accessible_get_child_at_index(combo.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(menuList.get()));
+    option1 = adoptGRef(atspi_accessible_get_child_at_index(menuList.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(option1.get()));
+    option2 = adoptGRef(atspi_accessible_get_child_at_index(menuList.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(option2.get()));
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option2.get()));
+    test->startEventMonitor(option2.get(), { "object:state-changed" });
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('combo').selectedIndex = 1;", nullptr);
+    events = test->stopEventMonitor(1);
+    g_assert_cmpuint(events.size(), ==, 1);
+    g_assert_cmpstr(events[0]->type, ==, "object:state-changed:selected");
+    g_assert_cmpuint(events[0]->detail1, ==, 1);
+    events = { };
+    g_assert_true(AccessibilityTest::isSelected(option2.get()));
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
+    test->startEventMonitor(option2.get(), { "object:state-changed" });
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('combo').selectedIndex = 0", nullptr);
+    events = test->stopEventMonitor(1);
+    g_assert_cmpuint(events.size(), ==, 1);
+    g_assert_cmpstr(events[0]->type, ==, "object:state-changed:selected");
+    g_assert_cmpuint(events[0]->detail1, ==, 0);
+    events = { };
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option2.get()));
+#endif
 }
 
 static void testComponentHitTest(AccessibilityTest* test, gconstpointer)
@@ -2104,6 +2172,7 @@
     g_assert_true(ATSPI_IS_ACCESSIBLE(selectedChild.get()));
     auto option1 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 0, nullptr));
     g_assert_true(selectedChild.get() == option1.get());
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
     test->startEventMonitor(listBox.get(), { "object:selection-changed" });
     g_assert_true(atspi_selection_select_child(ATSPI_SELECTION(listBox.get()), 2, nullptr));
     events = test->stopEventMonitor(1);
@@ -2116,12 +2185,16 @@
     g_assert_true(ATSPI_IS_ACCESSIBLE(selectedChild.get()));
     auto option3 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 2, nullptr));
     g_assert_true(selectedChild.get() == option3.get());
+    g_assert_true(AccessibilityTest::isSelected(option3.get()));
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
     g_assert_false(atspi_selection_deselect_selected_child(ATSPI_SELECTION(listBox.get()), 1, nullptr));
     g_assert_cmpint(atspi_selection_get_n_selected_children(ATSPI_SELECTION(listBox.get()), nullptr), ==, 1);
     g_assert_true(atspi_selection_deselect_selected_child(ATSPI_SELECTION(listBox.get()), 0, nullptr));
     g_assert_cmpint(atspi_selection_get_n_selected_children(ATSPI_SELECTION(listBox.get()), nullptr), ==, 0);
     g_assert_false(atspi_selection_is_child_selected(ATSPI_SELECTION(listBox.get()), 2, nullptr));
     g_assert_false(atspi_selection_select_all(ATSPI_SELECTION(listBox.get()), nullptr));
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option3.get()));
     g_assert_true(atspi_selection_select_child(ATSPI_SELECTION(listBox.get()), 0, nullptr));
     g_assert_cmpint(atspi_selection_get_n_selected_children(ATSPI_SELECTION(listBox.get()), nullptr), ==, 1);
     g_assert_true(atspi_selection_is_child_selected(ATSPI_SELECTION(listBox.get()), 0, nullptr));
@@ -2148,6 +2221,7 @@
     g_assert_true(ATSPI_IS_ACCESSIBLE(selectedChild.get()));
     auto option2 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 1, nullptr));
     g_assert_true(selectedChild.get() == option2.get());
+    g_assert_true(AccessibilityTest::isSelected(option2.get()));
     g_assert_true(atspi_selection_select_child(ATSPI_SELECTION(listBox.get()), 0, nullptr));
     g_assert_cmpint(atspi_selection_get_n_selected_children(ATSPI_SELECTION(listBox.get()), nullptr), ==, 2);
     g_assert_true(atspi_selection_is_child_selected(ATSPI_SELECTION(listBox.get()), 0, nullptr));
@@ -2155,6 +2229,8 @@
     selectedChild = adoptGRef(atspi_selection_get_selected_child(ATSPI_SELECTION(listBox.get()), 0, nullptr));
     option1 = adoptGRef(atspi_accessible_get_child_at_index(listBox.get(), 0, nullptr));
     g_assert_true(selectedChild.get() == option1.get());
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
+    g_assert_true(AccessibilityTest::isSelected(option2.get()));
     selectedChild = adoptGRef(atspi_selection_get_selected_child(ATSPI_SELECTION(listBox.get()), 1, nullptr));
     g_assert_true(selectedChild.get() == option2.get());
     g_assert_true(atspi_selection_deselect_child(ATSPI_SELECTION(listBox.get()), 1, nullptr));
@@ -2211,6 +2287,8 @@
     g_assert_cmpint(atspi_accessible_get_role(panel.get(), nullptr), ==, ATSPI_ROLE_PANEL);
     g_assert_cmpint(atspi_accessible_get_child_count(panel.get(), nullptr), ==, 1);
 
+    test->runJavaScriptAndWaitUntilFinished("document.getElementById('combo').focus();", nullptr);
+
     auto combo = adoptGRef(atspi_accessible_get_child_at_index(panel.get(), 0, nullptr));
     g_assert_true(ATSPI_IS_ACCESSIBLE(combo.get()));
     g_assert_cmpint(atspi_accessible_get_role(combo.get(), nullptr), ==, ATSPI_ROLE_COMBO_BOX);
@@ -2231,6 +2309,9 @@
     g_assert_true(ATSPI_IS_ACCESSIBLE(selectedChild.get()));
     auto option2 = adoptGRef(atspi_accessible_get_child_at_index(menuList.get(), 1, nullptr));
     g_assert_true(selectedChild.get() == option2.get());
+    g_assert_true(AccessibilityTest::isSelected(option2.get()));
+    auto option1 = adoptGRef(atspi_accessible_get_child_at_index(menuList.get(), 0, nullptr));
+    g_assert_false(AccessibilityTest::isSelected(option1.get()));
     g_assert_false(atspi_selection_select_child(ATSPI_SELECTION(menuList.get()), 3, nullptr));
     test->startEventMonitor(menuList.get(), { "object:selection-changed" });
     g_assert_true(atspi_selection_select_child(ATSPI_SELECTION(menuList.get()), 0, nullptr));
@@ -2239,6 +2320,11 @@
     g_assert_cmpstr(events[0]->type, ==, "object:selection-changed");
     events = { };
     g_assert_true(atspi_selection_is_child_selected(ATSPI_SELECTION(menuList.get()), 0, nullptr));
+    selectedChild = adoptGRef(atspi_selection_get_selected_child(ATSPI_SELECTION(menuList.get()), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(selectedChild.get()));
+    g_assert_true(selectedChild.get() == option1.get());
+    g_assert_true(AccessibilityTest::isSelected(option1.get()));
+    g_assert_false(AccessibilityTest::isSelected(option2.get()));
     g_assert_false(atspi_selection_deselect_selected_child(ATSPI_SELECTION(menuList.get()), 0, nullptr));
     g_assert_false(atspi_selection_deselect_child(ATSPI_SELECTION(menuList.get()), 0, nullptr));
     g_assert_false(atspi_selection_select_all(ATSPI_SELECTION(menuList.get()), nullptr));