2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tests / pyatspi / accessibletest.py
1 import dbus
2 import gobject
3 import os.path
4
5 from xml.dom import minidom
6 import os
7
8 from pasytest import PasyTest as _PasyTest
9
10 import pyatspi
11 from pyatspi import StateSet
12
13 st = [pyatspi.STATE_MULTI_LINE,
14       pyatspi.STATE_MODAL,
15       pyatspi.STATE_INDETERMINATE,
16       pyatspi.STATE_SUPPORTS_AUTOCOMPLETION,
17       pyatspi.STATE_VERTICAL,]
18
19 def _createNode(accessible, parentElement):
20         e = minidom.Element("accessible")
21
22         e.attributes["name"] = accessible.name
23         e.attributes["role"] = str(int(accessible.getRole()))
24         e.attributes["description"] = accessible.description
25
26         for i in range(0, accessible.childCount):
27                 _createNode(accessible.getChildAtIndex(i), e)
28
29         parentElement.appendChild(e)
30
31 class AccessibleTest(_PasyTest):
32
33         __tests__ = ["setup",
34                      "test_name",
35                      "test_getChildAtIndex",
36                      "test_isEqual",
37                      "test_getApplication",
38                      "test_getAttributes",
39                      "test_parent",
40                      "test_getIndexInParent",
41                      "test_getLocalizedRoleName",
42                      "test_getRelationSet",
43                      "test_getRole",
44                      "test_getRoleName",
45                      "test_getState",
46                      "test_childCount",
47                      "test_description",
48                      "test_tree",
49                      "teardown",
50                      ]
51
52         def __init__(self, bus, path):
53                 _PasyTest.__init__(self, "Accessible", False)
54                 self._bus = bus
55                 self._path = path
56
57         def setup(self, test):
58                 self._registry = pyatspi.Registry()
59                 print self._path
60                 self._desktop = self._registry.getDesktop(0)
61
62         def test_name(self, test):
63                 root = self._desktop.getChildAtIndex(0)
64                 test.assertEqual(root.name, "main", "Expected name - \"main\". Recieved - \"%s\"" % (root.name,))
65
66         def test_getChildAtIndex(self, test):
67                 root = self._desktop.getChildAtIndex(0)
68                 a = root.getChildAtIndex(0)
69                 test.assertEqual(a.name, "gnome-settings-daemon",
70                                          "Expected name - \"gnome-settings-daemon\". Recieved - \"%s\"" % (a.name,))
71                 b = root.getChildAtIndex(1)
72                 test.assertEqual(b.name, "gnome-panel",
73                                          "Expected name - \"gnome-panel\". Recieved - \"%s\"" % (b.name,))
74                 c = root.getChildAtIndex(2)
75                 test.assertEqual(c.name, "nautilus",
76                                          "Expected name - \"nautilus\". Recieved - \"%s\"" % (c.name,))
77                 
78         def test_isEqual(self, test):
79                 root = self._desktop.getChildAtIndex(0)
80
81                 a = root.getChildAtIndex(1)
82                 if not a.isEqual(a):
83                         test.fail("Same accessible found unequal to self")
84
85                 b = root.getChildAtIndex(1)
86                 if not a.isEqual(b):
87                         test.fail("Similar accessibles found unequal")
88                 if not b.isEqual(a):
89                         test.fail("Similar accessibles found unequal")
90
91                 c = root.getChildAtIndex(2)
92                 if c.isEqual(a):
93                         test.fail("Different accessibles found equal")
94                 if a.isEqual(c):
95                         test.fail("Different accessibles found equal")
96
97         def test_getApplication(self, test):
98                 root = self._desktop.getChildAtIndex(0)
99                 application = root.getApplication()
100                 if not root.isEqual(application):
101                         test.fail("Root accessible does not provide itself as its Application")
102
103                 a = root.getChildAtIndex(1)
104                 application = a.getApplication()
105                 if not root.isEqual(application):
106                         test.fail("Child accessible does not provide the root as its Application")
107
108
109         def test_getAttributes(self, test):
110                 root = self._desktop.getChildAtIndex(0)
111                 attr = root.getAttributes()
112                 res = ["foo:bar", "baz:qux", "quux:corge"]
113                 attr.sort()
114                 res.sort()
115                 test.assertEqual(attr, res, "Attributes expected %s, recieved %s" % (attr, res))
116
117         def test_parent(self, test):
118                 root = self._desktop.getChildAtIndex(0)
119
120                 a = root.getChildAtIndex(1)
121                 pa = a.parent
122                 if not root.isEqual(pa):
123                         test.fail("Child does not correctly report its parent")
124
125         def test_getIndexInParent(self, test):
126                 root = self._desktop.getChildAtIndex(0)
127
128                 for i in range(0, root.childCount):
129                         child = root.getChildAtIndex(i)
130                         test.assertEqual(i, child.getIndexInParent(), "Childs index in parent reported incorrectly")
131
132         def test_getLocalizedRoleName(self, test):
133                 root = self._desktop.getChildAtIndex(0)
134
135                 ans = "window"
136                 res = root.getLocalizedRoleName()
137                 test.assertEqual(ans, res,
138                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
139
140                 a = root.getChildAtIndex(1)
141                 a = a.getChildAtIndex(0)
142                 ans = "html container"
143                 res = a.getLocalizedRoleName()
144                 test.assertEqual(ans, res,
145                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
146
147         def test_getRelationSet(self, test):
148                 root = self._desktop.getChildAtIndex(0)
149                 # Complete test of Relation interface is separate
150                 rset = root.getRelationSet()
151
152         def test_getRole(self, test):
153                 root = self._desktop.getChildAtIndex(0)
154                 test.assertEqual(root.getRole(), 69,
155                                  "Expected role - \"69\". Recieved - \"%d\"" % (int(root.getRole()),))
156
157         def test_getRoleName(self, test):
158                 root = self._desktop.getChildAtIndex(0)
159
160                 ans = "window"
161                 res = root.getRoleName()
162                 test.assertEqual(ans, res,
163                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
164
165                 a = root.getChildAtIndex(1)
166                 a = a.getChildAtIndex(0)
167                 ans = "html container"
168                 res = a.getRoleName()
169                 test.assertEqual(ans, res,
170                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
171
172         def test_getState(self, test):
173                 root = self._desktop.getChildAtIndex(0)
174                 state = root.getState()
175                 res = StateSet(*st)
176                 if not res.equals(state):
177                         test.fail("States not reported correctly")
178
179         def test_childCount(self, test):
180                 root = self._desktop.getChildAtIndex(0)
181                 test.assertEqual(root.childCount, 11,
182                                  "Expected role - \"11\". Recieved - \"%d\"" % (root.childCount,))
183
184         def test_description(self, test):
185                 root = self._desktop.getChildAtIndex(0)
186                 description = "The main accessible object, root of the accessible tree"
187                 test.assertEqual(root.description, description,
188                                  "Expected description - \"%s\". Recieved - \"%s\"" % (description, root.description,))
189
190         def test_tree(self, test):
191                 """
192                 This is a mild stress test for the 
193                 methods:
194
195                 getChildAtIndex
196                 
197                 And the attributes:
198
199                 name
200                 description
201
202                 It checks a tree of these values is correctly
203                 passed from Application to AT.
204                 """
205                 root = self._desktop.getChildAtIndex(0)
206
207                 doc = minidom.Document()
208                 _createNode(root, doc)
209                 answer = doc.toprettyxml()
210
211                 correct = os.path.join(os.environ["TEST_DATA_DIRECTORY"],
212                                         "accessible-test-results.xml")
213                 file = open(correct)
214                 cstring = file.read()
215                 
216                 test.assertEqual(answer, cstring, "Object tree not passed correctly")
217
218         def teardown(self, test):
219                 pass