| // Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| var group; |
| var h1; |
| var p1; |
| var link; |
| var main; |
| var p2; |
| var p3; |
| var okButton; |
| var cancelButton; |
| |
| function initializeNodes(rootNode) { |
| group = rootNode.firstChild; |
| assertEq(RoleType.GROUP, group.role); |
| |
| h1 = group.firstChild; |
| assertEq(RoleType.HEADING, h1.role); |
| assertEq(1, h1.hierarchicalLevel); |
| |
| p1 = group.lastChild; |
| assertEq(RoleType.PARAGRAPH, p1.role); |
| |
| link = p1.children[1]; |
| assertEq(RoleType.LINK, link.role); |
| |
| main = rootNode.children[1]; |
| assertEq(RoleType.MAIN, main.role); |
| |
| p2 = main.firstChild; |
| assertEq(RoleType.PARAGRAPH, p2.role); |
| |
| p3 = main.lastChild; |
| assertEq(RoleType.PARAGRAPH, p3.role); |
| |
| okButton = rootNode.children[2]; |
| assertEq(RoleType.BUTTON, okButton.role); |
| assertEq('Ok', okButton.name); |
| assertTrue(StateType.DISABLED in okButton.state); |
| assertTrue(okButton.state.disabled); |
| |
| cancelButton = rootNode.children[3]; |
| assertEq(RoleType.BUTTON, cancelButton.role); |
| assertEq('Cancel', cancelButton.name); |
| assertFalse(StateType.DISABLED in cancelButton.state); |
| } |
| |
| var allTests = [ |
| function testFindByRole() { |
| initializeNodes(rootNode); |
| |
| // Should find the only instance of this role. |
| assertEq(h1, rootNode.find({role: RoleType.HEADING})); |
| assertEq([h1], rootNode.findAll({role: RoleType.HEADING})); |
| |
| // find should find first instance only. |
| assertEq(okButton, rootNode.find({role: RoleType.BUTTON})); |
| assertEq(p1, rootNode.find({role: RoleType.PARAGRAPH})); |
| |
| // findAll should find all instances. |
| assertEq( |
| [okButton, cancelButton], rootNode.findAll({role: RoleType.BUTTON})); |
| assertEq([p1, p2, p3], rootNode.findAll({role: RoleType.PARAGRAPH})); |
| |
| // No instances: find should return null; findAll should return empty array. |
| assertEq(null, rootNode.find({role: RoleType.CHECKBOX})); |
| assertEq([], rootNode.findAll({role: RoleType.CHECKBOX})); |
| |
| // Calling from node should search only its subtree. |
| assertEq(p1, group.find({role: RoleType.PARAGRAPH})); |
| assertEq(p2, main.find({role: RoleType.PARAGRAPH})); |
| assertEq([p2, p3], main.findAll({role: RoleType.PARAGRAPH})); |
| |
| chrome.test.succeed(); |
| }, |
| |
| function testFindByStates() { |
| initializeNodes(rootNode); |
| |
| // Find all focusable elements (disabled button is not focusable). |
| assertEq(link, rootNode.find({state: {focusable: true}})); |
| assertEq( |
| [link, cancelButton], rootNode.findAll({state: {focusable: true}})); |
| |
| // Find disabled buttons. |
| assertEq( |
| okButton, |
| rootNode.find({role: RoleType.BUTTON, state: {disabled: true}})); |
| assertEq( |
| [okButton], |
| rootNode.findAll({role: RoleType.BUTTON, state: {disabled: true}})); |
| |
| // Find enabled buttons. |
| assertEq( |
| cancelButton, |
| rootNode.find({role: RoleType.BUTTON, state: {disabled: false}})); |
| assertEq( |
| [cancelButton], |
| rootNode.findAll({role: RoleType.BUTTON, state: {disabled: false}})); |
| chrome.test.succeed(); |
| }, |
| |
| function testFindByAttribute() { |
| initializeNodes(rootNode); |
| |
| // Find by name attribute. |
| assertEq(okButton, rootNode.find({attributes: {name: 'Ok'}})); |
| assertEq(cancelButton, rootNode.find({attributes: {name: 'Cancel'}})); |
| |
| // String attributes must be exact match unless a regex is used. |
| assertEq(null, rootNode.find({attributes: {name: 'Canc'}})); |
| assertEq(null, rootNode.find({attributes: {name: 'ok'}})); |
| |
| // Find by value attribute - regexp. |
| var query = {attributes: {name: /relationship/}}; |
| assertEq(p2, rootNode.find(query).parent); |
| |
| // Find by role and hierarchicalLevel attribute. |
| assertEq( |
| h1, rootNode.find( |
| {role: RoleType.HEADING, attributes: {hierarchicalLevel: 1}})); |
| assertEq( |
| [], rootNode.findAll( |
| {role: RoleType.HEADING, attributes: {hierarchicalLevel: 2}})); |
| |
| // Searching for an attribute which no element has fails. |
| assertEq(null, rootNode.find({attributes: {charisma: 12}})); |
| |
| // Searching for an attribute value of the wrong type fails (even if the |
| // value is equivalent). |
| assertEq( |
| null, |
| rootNode.find( |
| {role: RoleType.HEADING, attributes: {hierarchicalLevel: true}})); |
| |
| chrome.test.succeed(); |
| }, |
| |
| function testMatches() { |
| initializeNodes(rootNode); |
| assertTrue( |
| h1.matches({role: RoleType.HEADING}), |
| 'h1 should match RoleType.HEADING'); |
| assertTrue( |
| h1.matches( |
| {role: RoleType.HEADING, attributes: {hierarchicalLevel: 1}}), |
| 'h1 should match RoleType.HEADING and hierarchicalLevel: 1'); |
| assertFalse( |
| h1.matches({ |
| role: RoleType.HEADING, |
| state: {focusable: true}, |
| attributes: {hierarchicalLevel: 1} |
| }), |
| 'h1 should not match focusable: true'); |
| assertTrue( |
| h1.matches({ |
| role: RoleType.HEADING, |
| state: {focusable: false}, |
| attributes: {hierarchicalLevel: 1} |
| }), |
| 'h1 should match focusable: false'); |
| |
| var p2StaticText = p2.firstChild; |
| assertTrue( |
| p2StaticText.matches( |
| {role: RoleType.STATIC_TEXT, attributes: {name: /relationship/}}), |
| 'p2StaticText should match name: /relationship/ (regex match)'); |
| assertFalse( |
| p2StaticText.matches( |
| {role: RoleType.STATIC_TEXT, attributes: {name: 'relationship'}}), |
| 'p2 should not match name: \'relationship'); |
| |
| chrome.test.succeed(); |
| } |
| ]; |
| |
| setUpAndRunTests(allTests, 'complex.html'); |