2008-08-22 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.Registry(self._path)
59                 self._desktop = self._registry.getDesktop(0)
60
61         def test_name(self, test):
62                 root = self._desktop.getChildAtIndex(0)
63                 test.assertEqual(root.name, "main", "Expected name - \"main\". Recieved - \"%s\"" % (root.name,))
64
65         def test_getChildAtIndex(self, test):
66                 root = self._desktop.getChildAtIndex(0)
67                 a = root.getChildAtIndex(0)
68                 test.assertEqual(a.name, "gnome-settings-daemon",
69                                          "Expected name - \"gnome-settings-daemon\". Recieved - \"%s\"" % (a.name,))
70                 b = root.getChildAtIndex(1)
71                 test.assertEqual(b.name, "gnome-panel",
72                                          "Expected name - \"gnome-panel\". Recieved - \"%s\"" % (b.name,))
73                 c = root.getChildAtIndex(2)
74                 test.assertEqual(c.name, "nautilus",
75                                          "Expected name - \"nautilus\". Recieved - \"%s\"" % (c.name,))
76                 
77         def test_isEqual(self, test):
78                 root = self._desktop.getChildAtIndex(0)
79
80                 a = root.getChildAtIndex(1)
81                 if not a.isEqual(a):
82                         test.fail("Same accessible found unequal to self")
83
84                 b = root.getChildAtIndex(1)
85                 if not a.isEqual(b):
86                         test.fail("Similar accessibles found unequal")
87                 if not b.isEqual(a):
88                         test.fail("Similar accessibles found unequal")
89
90                 c = root.getChildAtIndex(2)
91                 if c.isEqual(a):
92                         test.fail("Different accessibles found equal")
93                 if a.isEqual(c):
94                         test.fail("Different accessibles found equal")
95
96         def test_getApplication(self, test):
97                 root = self._desktop.getChildAtIndex(0)
98                 application = root.getApplication()
99                 if not root.isEqual(application):
100                         test.fail("Root accessible does not provide itself as its Application")
101
102                 a = root.getChildAtIndex(1)
103                 application = a.getApplication()
104                 if not root.isEqual(application):
105                         test.fail("Child accessible does not provide the root as its Application")
106
107
108         def test_getAttributes(self, test):
109                 root = self._desktop.getChildAtIndex(0)
110                 attr = root.getAttributes()
111                 res = ["foo:bar", "baz:qux", "quux:corge"]
112                 test.assertEqual(attr, res, "Attributes expected %s, recieved %s" % (attr, res))
113
114
115         def test_parent(self, test):
116                 root = self._desktop.getChildAtIndex(0)
117
118                 a = root.getChildAtIndex(1)
119                 pa = a.parent
120                 if not root.isEqual(pa):
121                         test.fail("Child does not correctly report its parent")
122
123         def test_getIndexInParent(self, test):
124                 root = self._desktop.getChildAtIndex(0)
125
126                 for i in range(0, root.childCount):
127                         child = root.getChildAtIndex(i)
128                         test.assertEqual(i, child.getIndexInParent(), "Childs index in parent reported incorrectly")
129
130         def test_getLocalizedRoleName(self, test):
131                 root = self._desktop.getChildAtIndex(0)
132
133                 ans = "window"
134                 res = root.getLocalizedRoleName()
135                 test.assertEqual(ans, res,
136                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
137
138                 a = root.getChildAtIndex(1)
139                 a = a.getChildAtIndex(0)
140                 ans = "html container"
141                 res = a.getLocalizedRoleName()
142                 test.assertEqual(ans, res,
143                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
144
145         def test_getRelationSet(self, test):
146                 root = self._desktop.getChildAtIndex(0)
147                 # Complete test of Relation interface is separate
148                 rset = root.getRelationSet()
149
150         def test_getRole(self, test):
151                 root = self._desktop.getChildAtIndex(0)
152                 test.assertEqual(root.getRole(), 69,
153                                  "Expected role - \"69\". Recieved - \"%d\"" % (root.getRole(),))
154
155         def test_getRoleName(self, test):
156                 root = self._desktop.getChildAtIndex(0)
157
158                 ans = "window"
159                 res = root.getRoleName()
160                 test.assertEqual(ans, res,
161                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
162
163                 a = root.getChildAtIndex(1)
164                 a = a.getChildAtIndex(0)
165                 ans = "html container"
166                 res = a.getRoleName()
167                 test.assertEqual(ans, res,
168                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
169
170         def test_getState(self, test):
171                 root = self._desktop.getChildAtIndex(0)
172                 state = root.getState()
173                 res = StateSet(*st)
174                 if not res.equals(state):
175                         test.fail("States not reported correctly")
176
177         def test_childCount(self, test):
178                 root = self._desktop.getChildAtIndex(0)
179                 test.assertEqual(root.childCount, 11,
180                                  "Expected role - \"11\". Recieved - \"%d\"" % (root.childCount,))
181
182         def test_description(self, test):
183                 root = self._desktop.getChildAtIndex(0)
184                 description = "The main accessible object, root of the accessible tree"
185                 test.assertEqual(root.description, description,
186                                  "Expected description - \"%s\". Recieved - \"%s\"" % (description, root.description,))
187
188         def test_tree(self, test):
189                 """
190                 This is a mild stress test for the 
191                 methods:
192
193                 getChildAtIndex
194                 
195                 And the attributes:
196
197                 name
198                 description
199
200                 It checks a tree of these values is correctly
201                 passed from Application to AT.
202                 """
203                 root = self._desktop.getChildAtIndex(0)
204
205                 doc = minidom.Document()
206                 _createNode(root, doc)
207                 answer = doc.toprettyxml()
208
209                 correct = os.path.join(os.environ["TEST_DATA_DIRECTORY"],
210                                         "accessible-test-results.xml")
211                 file = open(correct)
212                 cstring = file.read()
213                 
214                 test.assertEqual(answer, cstring, "Object tree not passed correctly")
215
216         def teardown(self, test):
217                 pass