| <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
| <html> |
| <head> |
| <script src="../../resources/accessibility-helper.js"></script> |
| <script src="../../resources/js-test.js"></script> |
| </head> |
| <style> |
| .hidden { display: none; } |
| </style> |
| <body> |
| |
| <div id="bg"> |
| <p id="bgContent">Other page content with <a href="#">a dummy focusable element</a></p> |
| <p> |
| <a onclick="toggleDialog(document.getElementById('dialog1'), 'show'); document.getElementById('new').focus(); return false;" |
| href="#" role="button" id="displayBtn"> |
| Display a dialog |
| </a> |
| </p> |
| </div> |
| |
| <div role="dialog" aria-labelledby="dialog1-heading" id="dialog1" class="hidden" tabindex="-1" aria-modal="false"> |
| <h3 id="dialog1-heading">Dialog1 heading</h3> |
| <button id="ok" onclick="toggleDialog(document.getElementById('dialog1'), 'hide');" class="close-button">OK</button> |
| <button onclick="toggleDialog(document.getElementById('dialog2'), 'show');" id="new">New</button> |
| </div> |
| |
| <div role="dialog" aria-labelledby="dialog2-heading" id="dialog2" class="hidden" tabindex="-1"> |
| <h3 id="dialog2-heading">Dialog2 heading</h3> |
| <button id="close" onclick="toggleDialog(document.getElementById('dialog2'), 'hide');" class="close-button">Close</button> |
| </div> |
| |
| <script> |
| var testOutput = "This tests that focus will automatically move to aria-modal dialogs\n\n"; |
| |
| if (window.accessibilityController) { |
| window.jsTestIsAsync = true; |
| |
| if (backgroundAccessible()) |
| testOutput += `PASS: Background is initially accessible.\n`; |
| else |
| testOutput += `FAIL: Background is not initially accessible.\n`; |
| |
| var newBtn, okBtn, closeBtn; |
| // 1. Click the display button, dialog1 shows and focus shouldn't move since we have |
| // javascript code to focus on the "new" button. |
| testOutput += "\nOpening dialog1.\n"; |
| document.getElementById("displayBtn").click(); |
| setTimeout(async () => { |
| await waitFor(() => { |
| newBtn = accessibilityController.accessibleElementById("new"); |
| return newBtn && newBtn.isFocused; |
| }); |
| testOutput += "PASS: #new button is focused.\n"; |
| |
| // 2. Click the new button, dialog2 shows and focus should move to the close button. |
| // Set aria-modal to false on the previous modal object (we shouldn't have two modals in play). |
| testOutput += "\nOpening dialog2.\n"; |
| document.getElementById("new").click(); |
| document.getElementById("dialog1").ariaModal = false; |
| |
| await waitFor(() => { |
| closeBtn = accessibilityController.accessibleElementById("close"); |
| return closeBtn && closeBtn.isFocused; |
| }); |
| testOutput += "PASS: #close button is focused.\n"; |
| |
| // 3. Click the close button, dialog2 closes and focus should go back to the |
| // first focusable child of dialog1, which we now need to add aria-modal back to. |
| testOutput += "\nClosing dialog2 and making dialog1 `ariaModal = true`.\n"; |
| document.getElementById("close").click(); |
| document.getElementById("dialog1").ariaModal = true; |
| |
| await waitFor(() => { |
| okBtn = accessibilityController.accessibleElementById("ok"); |
| return okBtn && okBtn.isFocused; |
| }); |
| testOutput += "PASS: #ok button is focused.\n"; |
| |
| debug(testOutput); |
| finishJSTest(); |
| }); |
| } |
| |
| function backgroundAccessible() { |
| const displayBtn = accessibilityController.accessibleElementById("displayBtn"); |
| const bgContent = accessibilityController.accessibleElementById("bgContent"); |
| if (!displayBtn || !bgContent) |
| return false; |
| |
| return !displayBtn.isIgnored && !bgContent.isIgnored; |
| } |
| |
| function toggleDialog(dialog, sh) { |
| if (sh == "show") { |
| dialog.style.display = "block"; |
| dialog.setAttribute("aria-modal", "true"); |
| } else { |
| dialog.style.display = "none"; |
| dialog.setAttribute("aria-modal", "false"); |
| } |
| } |
| </script> |
| </body> |
| </html> |