blob: b516b2a16876c9ea9d69321603fcce15871e9afb [file] [log] [blame]
<!DOCTYPE HTML>
<html>
<body>
<script src="../resources/js-test.js"></script>
<div id="container">
<div>
<label id="label1" for="control1">Label 1</label>
<input id="control1" type="text">
</div>
<div>
<label id="label2">
Label 2
<input id="control2" type="text">
</label>
</div>
<div>
<label id="label3">Label 3</label>
<input id="control3" type="text">
</div>
<div>
<input id="control4" type="text">
</div>
<div>
<label id="label5">
Label 5
</label>
<input id="control5" type="text">
</div>
<div>
<label id="label6b" for="control6">Label 6b</label>
<label id="label6c" for="control6">Label 6c</label>
<input id="control6" type="text">
</div>
</div>
<div id="console"></div>
<script>
description("This tests that titleUIElement works correctly even when things change dynamically.");
if (window.testRunner && window.accessibilityController) {
function hasTitleUIElement(axElement) {
var label1 = accessibilityController.accessibleElementById("label1");
var titleUIElement = axElement.titleUIElement();
if (titleUIElement == null)
return false;
return titleUIElement.role == label1.role;
}
function createLabelWithIdAndForAttribute(id, forAttributeValue) {
var labelElement = document.createElement("label");
labelElement.id = id;
labelElement.setAttribute("for", forAttributeValue);
labelElement.innerText = "Label for " + forAttributeValue;
return labelElement;
}
function reparentNodeIntoContainer(node, container) {
node.parentElement.removeChild(node);
container.appendChild(node);
}
function axElement(id) {
return accessibilityController.accessibleElementById(id);
}
shouldBe("axElement('control1').titleUIElement().isEqual(axElement('label1'))", "true");
shouldBe("axElement('control2').titleUIElement().isEqual(axElement('label2'))", "true");
// Test changing the "for" attribute dynamically.
shouldBe("hasTitleUIElement(axElement('control3'))", "false");
shouldBe("document.getElementById('label3').setAttribute('for', 'control3'); axElement('control3').titleUIElement().isEqual(axElement('label3'))", "true");
// Test unattached label element that's subsequently attached.
var label4Element = document.createElement("label");
label4Element.id = "label4";
label4Element.setAttribute("for", "control4");
label4Element.innerText = "Label 4";
shouldBe("var label4Element = createLabelWithIdAndForAttribute('label4', 'control4'); hasTitleUIElement(axElement('control4'))", "false");
shouldBe("document.getElementById('container').appendChild(label4Element); hasTitleUIElement(axElement('control4'))", "true");
shouldBe("axElement('control4').titleUIElement().isEqual(axElement('label4'))", "true");
// Test what happens when the label is detached.
shouldBe("label4Element.parentElement.removeChild(label4Element); hasTitleUIElement(axElement('control4'))", "false");
// Test label that gets a control reparented into it.
shouldBe("hasTitleUIElement(axElement('control5'))", "false");
shouldBe("reparentNodeIntoContainer(document.getElementById('control5'), document.getElementById('label5')); axElement('control5').titleUIElement() != null", "true");
shouldBe("axElement('control5').titleUIElement().isEqual(axElement('label5'))", "true");
// Make sure the first label is returned, even as labels are added and removed.
shouldBe("axElement('control6').titleUIElement().isEqual(axElement('label6b'))", "true");
shouldBe("newLabel6Element = createLabelWithIdAndForAttribute('label6a', 'control6'); document.body.insertBefore(newLabel6Element, document.body.firstChild); axElement('control6').titleUIElement().isEqual(axElement('label6a'))", "true");
shouldBe("document.body.removeChild(newLabel6Element); axElement('control6').titleUIElement().isEqual(axElement('label6b'))", "true");
document.getElementById('container').style.display = 'none';
}
</script>
</body>
</html>