dbbfd6b056f7406d52dcd564db97741a8650c4d3
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / automation / tests / unit / test.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 chrome.test.runWithModuleSystem(function(moduleSystem) {
6   window.AutomationRootNode =
7       moduleSystem.require('automationNode').AutomationRootNode;
8   window.privates = moduleSystem.privates;
9   // Unused.
10   window.automationUtil = function() {};
11   window.automationUtil.storeTreeCallback = function() {};
12 });
13
14 var assertEq = chrome.test.assertEq;
15 var assertFalse = chrome.test.assertFalse;
16 var assertTrue = chrome.test.assertTrue;
17 var assertIsDef = function(obj) {
18   assertTrue(obj !== null && obj !== undefined);
19 }
20 var assertIsNotDef = function(obj) {
21   assertTrue(obj === null || obj === undefined);
22 }
23 var succeed = chrome.test.succeed;
24
25 var tests = [
26   function testAutomationRootNode() {
27     var root = new AutomationRootNode();
28     assertTrue(root.isRootNode);
29
30     succeed();
31   },
32
33   function testAriaRelationshipAttributes() {
34     var data = {
35       'eventType': 'loadComplete',
36           'processID': 17, 'routingID': 2, 'targetID': 1,
37       'update': { 'nodeIdToClear': 0, 'nodes': [
38         {
39           'id': 1, 'role': 'rootWebArea',
40           'boolAttributes': {'docLoaded': true},
41           'childIds': [5, 6, 7, 8, 9, 10, 11]
42         },
43         {
44           'id': 11,        'role': 'div',
45           'childIds': [],
46           'htmlAttributes': {'id': 'target' }
47         },
48         {
49           'id': 5, 'role': 'div',
50           'childIds': [],
51           'htmlAttributes': {'aria-activedescendant': 'target',
52                              'id': 'activedescendant'},
53           'intAttributes': {'activedescendantId': 11},
54         },
55         {
56           'id': 6, 'role': 'div',
57           'childIds': [],
58           'htmlAttributes': {'aria-controls': 'target', 'id': 'controlledBy'},
59           'intlistAttributes': {'controlsIds': [11]},
60         },
61         {
62           'id': 7, 'role': 'div',
63           'childIds': [],
64           'htmlAttributes': {'aria-describedby': 'target', 'id': 'describedBy'},
65           'intlistAttributes': {'describedbyIds': [11, 6]},
66         },
67         {
68           'id': 8, 'role': 'div',
69           'childIds': [],
70           'htmlAttributes': {'aria-flowto': 'target', 'id': 'flowTo'},
71           'intlistAttributes': {'flowtoIds': [11]},
72         },
73         {
74           'id': 9, 'role': 'div',
75           'childIds': [],
76           'htmlAttributes': {'aria-labelledby': 'target', 'id': 'labelledBy'},
77           'intlistAttributes': {'labelledbyIds': [11]}
78         },
79         {
80           'id': 10, 'role': 'div',
81           'childIds': [],
82           'htmlAttributes': {'aria-owns': 'target', 'id': 'owns'},
83           'intlistAttributes': {'ownsIds': [11]},
84         }
85       ]}
86     }
87
88     var tree = new AutomationRootNode();
89     privates(tree).impl.onAccessibilityEvent(data);
90
91     var activedescendant = tree.firstChild();
92     assertIsDef(activedescendant);
93     assertEq('activedescendant', activedescendant.attributes.id);
94     assertEq(
95         'target',
96         activedescendant.attributes['aria-activedescendant'].attributes.id);
97     assertIsNotDef(activedescendant.attributes.activedescendantId);
98
99     var controlledBy = activedescendant.nextSibling();
100     assertIsDef(controlledBy);
101     assertEq('controlledBy', controlledBy.attributes.id);
102     assertEq('target',
103              controlledBy.attributes['aria-controls'][0].attributes.id);
104     assertEq(1, controlledBy.attributes['aria-controls'].length);
105     assertIsNotDef(controlledBy.attributes.controlledbyIds);
106
107     var describedBy = controlledBy.nextSibling();
108     assertIsDef(describedBy);
109     assertEq('describedBy', describedBy.attributes.id);
110     assertEq('target',
111              describedBy.attributes['aria-describedby'][0].attributes.id);
112     assertEq('controlledBy',
113              describedBy.attributes['aria-describedby'][1].attributes.id);
114     assertEq(2, describedBy.attributes['aria-describedby'].length);
115     assertIsNotDef(describedBy.attributes.describedbyIds);
116
117     var flowTo = describedBy.nextSibling();
118     assertIsDef(flowTo);
119     assertEq('flowTo', flowTo.attributes.id);
120     assertEq('target',
121              flowTo.attributes['aria-flowto'][0].attributes.id);
122     assertEq(1, flowTo.attributes['aria-flowto'].length);
123     assertIsNotDef(flowTo.attributes.flowtoIds);
124
125     var labelledBy = flowTo.nextSibling();
126     assertIsDef(labelledBy);
127     assertEq('labelledBy', labelledBy.attributes.id);
128     assertEq('target',
129              labelledBy.attributes['aria-labelledby'][0].attributes.id);
130     assertEq(1, labelledBy.attributes['aria-labelledby'].length);
131     assertIsNotDef(labelledBy.attributes.labelledbyIds);
132
133     var owns = labelledBy.nextSibling();
134     assertIsDef(owns);
135     assertEq('owns', owns.attributes.id);
136     assertEq('target', owns.attributes['aria-owns'][0].attributes.id);
137     assertEq(1, owns.attributes['aria-owns'].length);
138     assertIsNotDef(owns.attributes.ownsIds);
139
140     succeed();
141   },
142
143   function testCannotSetAttribute() {
144     var update =
145         {
146           'nodeIdToClear': 0, 'nodes': [
147             {
148               'id': 1, 'role': 'rootWebArea',
149               'boolAttributes': {'docLoaded': true},
150               'childIds': [11]
151             },
152             {
153               'id': 11, 'role': 'button',
154               'stringAttributes': {'name': 'foo'},
155               'childIds': []
156             }]
157         }
158
159     var tree = new AutomationRootNode();
160     assertTrue(privates(tree).impl.unserialize(update));
161     var button = tree.firstChild();
162     assertEq('button', button.role);
163     assertEq('foo', button.attributes.name);
164     button.attributes.name = 'bar';
165     assertEq('foo', button.attributes.name);
166
167     succeed();
168   },
169
170   function testBadUpdateInvalidChildIds() {
171     var update =
172         {
173           'nodeIdToClear': 0, 'nodes': [
174             {
175               'id': 1, 'role': 'rootWebArea',
176               'boolAttributes': {'docLoaded': true},
177               'childIds': [5, 6, 7, 8, 9, 10, 11]
178             }]
179         }
180
181     var tree = new AutomationRootNode();
182
183     // This is a bad update because the root references non existent child ids.
184     assertFalse(privates(tree).impl.unserialize(update));
185
186     succeed();
187   },
188
189   function testMultipleUpdateNameChanged() {
190     var update =
191         {
192           'nodeIdToClear': 0, 'nodes': [
193             {
194               'id': 1, 'role': 'rootWebArea',
195               'boolAttributes': {'docLoaded': true},
196               'childIds': [11]
197             },
198             {
199               'id': 11, 'role': 'button',
200               'stringAttributes': {'name': 'foo'},
201               'childIds': []
202             }]
203         }
204
205     // First, setup the initial tree.
206     var tree = new AutomationRootNode();
207     assertTrue(privates(tree).impl.unserialize(update));
208     var button = tree.firstChild();
209     assertEq('button', button.role);
210     assertEq('foo', button.attributes.name);
211
212     // Now, apply an update that changes the button's name.
213     // Remove the root since the native serializer stops at the LCA.
214     update.nodes.splice(0, 1);
215     update.nodes[0].stringAttributes.name = 'bar';
216
217     // Make sure the name changes.
218     assertTrue(privates(tree).impl.unserialize(update));
219     assertEq('bar', button.attributes.name);
220
221     succeed();
222   }
223 ];
224
225 chrome.test.runTests(tests);