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