|
| 1 | +// Copyright 2025 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as SDK from '../../core/sdk/sdk.js'; |
| 6 | +import {renderElementIntoDOM} from '../../testing/DOMHelpers.js'; |
| 7 | +import {createTarget, stubNoopSettings} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import {describeWithMockConnection} from '../../testing/MockConnection.js'; |
| 9 | + |
| 10 | +import * as Accessibility from './accessibility.js'; |
| 11 | + |
| 12 | +describeWithMockConnection('ARIAAttributesView', () => { |
| 13 | + let target: SDK.Target.Target; |
| 14 | + let domModel: SDK.DOMModel.DOMModel; |
| 15 | + let node: SDK.DOMModel.DOMNode; |
| 16 | + let setAttributeValueSpy: sinon.SinonSpy; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + stubNoopSettings(); |
| 20 | + target = createTarget(); |
| 21 | + domModel = target.model(SDK.DOMModel.DOMModel) as SDK.DOMModel.DOMModel; |
| 22 | + node = new SDK.DOMModel.DOMNode(domModel); |
| 23 | + node.setAttributesPayload(['role', 'checkbox', 'aria-checked', 'true']); |
| 24 | + setAttributeValueSpy = sinon.spy(node, 'setAttributeValue'); |
| 25 | + }); |
| 26 | + |
| 27 | + const modifyAttribute = |
| 28 | + (view: Accessibility.ARIAAttributesView.ARIAAttributesPane, childIndex: number, newValue: string) => { |
| 29 | + const treeOutline = view.getTreeOutlineForTesting(); |
| 30 | + assert.exists(treeOutline); |
| 31 | + const treeElement = |
| 32 | + treeOutline.rootElement().childAt(childIndex) as Accessibility.ARIAAttributesView.ARIAAttributesTreeElement; |
| 33 | + assert.exists(treeElement); |
| 34 | + treeElement.listItemElement.querySelector('span')?.click(); |
| 35 | + |
| 36 | + const prompt = treeElement.getPromptForTesting(); |
| 37 | + assert.exists(prompt); |
| 38 | + const proxyElement = prompt.element(); |
| 39 | + (proxyElement as HTMLElement).textContent = newValue; |
| 40 | + |
| 41 | + proxyElement.dispatchEvent(new FocusEvent('blur')); |
| 42 | + }; |
| 43 | + |
| 44 | + it('can modify an ARIA attribute value', () => { |
| 45 | + const view = new Accessibility.ARIAAttributesView.ARIAAttributesPane(); |
| 46 | + renderElementIntoDOM(view); |
| 47 | + view.setNode(node); |
| 48 | + |
| 49 | + modifyAttribute(view, 1, 'false'); |
| 50 | + |
| 51 | + sinon.assert.calledOnceWithExactly(setAttributeValueSpy, 'aria-checked', 'false'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('can modify an ARIA role', () => { |
| 55 | + const view = new Accessibility.ARIAAttributesView.ARIAAttributesPane(); |
| 56 | + renderElementIntoDOM(view); |
| 57 | + view.setNode(node); |
| 58 | + |
| 59 | + modifyAttribute(view, 0, 'radio'); |
| 60 | + |
| 61 | + sinon.assert.calledOnceWithExactly(setAttributeValueSpy, 'role', 'radio'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('ARIAAttributesTreeElement', () => { |
| 66 | + it('should create a value element with the correct class and text content', () => { |
| 67 | + const value = 'test value'; |
| 68 | + const element = Accessibility.ARIAAttributesView.ARIAAttributesTreeElement.createARIAValueElement(value); |
| 69 | + assert.strictEqual(element.textContent, value); |
| 70 | + assert.isTrue(element.classList.contains('monospace')); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should append a name element with the correct classes and text content', () => { |
| 74 | + const parentPane = {} as Accessibility.ARIAAttributesView.ARIAAttributesPane; |
| 75 | + const attribute = {name: 'aria-label', value: 'test'} as SDK.DOMModel.Attribute; |
| 76 | + const target = {} as SDK.Target.Target; |
| 77 | + const treeElement = new Accessibility.ARIAAttributesView.ARIAAttributesTreeElement(parentPane, attribute, target); |
| 78 | + treeElement.onattach(); |
| 79 | + |
| 80 | + treeElement.appendNameElement('aria-label'); |
| 81 | + |
| 82 | + const nameElement = treeElement.listItemElement.querySelector('.ax-name'); |
| 83 | + assert.exists(nameElement); |
| 84 | + assert.strictEqual(nameElement.textContent, 'aria-label'); |
| 85 | + assert.isTrue(nameElement.classList.contains('monospace')); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should append a value element with the correct text content', () => { |
| 89 | + const parentPane = {} as Accessibility.ARIAAttributesView.ARIAAttributesPane; |
| 90 | + const attribute = {name: 'aria-label', value: 'test'} as SDK.DOMModel.Attribute; |
| 91 | + const target = {} as SDK.Target.Target; |
| 92 | + const treeElement = new Accessibility.ARIAAttributesView.ARIAAttributesTreeElement(parentPane, attribute, target); |
| 93 | + treeElement.onattach(); |
| 94 | + |
| 95 | + treeElement.appendAttributeValueElement('test'); |
| 96 | + |
| 97 | + const valueElement = treeElement.listItemElement.querySelector('span:not(.ax-name):not(.separator)'); |
| 98 | + assert.exists(valueElement); |
| 99 | + assert.strictEqual(valueElement.textContent, 'test'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments