Merge branch 'master' of ssh://git.codethink.co.uk/git/atspi-dbus
[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                 test.assertEqual(attr, res, "Attributes expected %s, recieved %s" % (attr, res))
114
115
116         def test_parent(self, test):
117                 root = self._desktop.getChildAtIndex(0)
118
119                 a = root.getChildAtIndex(1)
120                 pa = a.parent
121                 if not root.isEqual(pa):
122                         test.fail("Child does not correctly report its parent")
123
124         def test_getIndexInParent(self, test):
125                 root = self._desktop.getChildAtIndex(0)
126
127                 for i in range(0, root.childCount):
128                         child = root.getChildAtIndex(i)
129                         test.assertEqual(i, child.getIndexInParent(), "Childs index in parent reported incorrectly")
130
131         def test_getLocalizedRoleName(self, test):
132                 root = self._desktop.getChildAtIndex(0)
133
134                 ans = "window"
135                 res = root.getLocalizedRoleName()
136                 test.assertEqual(ans, res,
137                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
138
139                 a = root.getChildAtIndex(1)
140                 a = a.getChildAtIndex(0)
141                 ans = "html container"
142                 res = a.getLocalizedRoleName()
143                 test.assertEqual(ans, res,
144                                  "Expected LocalizedRoleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
145
146         def test_getRelationSet(self, test):
147                 root = self._desktop.getChildAtIndex(0)
148                 # Complete test of Relation interface is separate
149                 rset = root.getRelationSet()
150
151         def test_getRole(self, test):
152                 root = self._desktop.getChildAtIndex(0)
153                 test.assertEqual(root.getRole(), 69,
154                                  "Expected role - \"69\". Recieved - \"%d\"" % (root.getRole(),))
155
156         def test_getRoleName(self, test):
157                 root = self._desktop.getChildAtIndex(0)
158
159                 ans = "window"
160                 res = root.getRoleName()
161                 test.assertEqual(ans, res,
162                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
163
164                 a = root.getChildAtIndex(1)
165                 a = a.getChildAtIndex(0)
166                 ans = "html container"
167                 res = a.getRoleName()
168                 test.assertEqual(ans, res,
169                                  "Expected roleName - \"%s\". Recieved - \"%s\"" % (ans, res,))
170
171         def test_getState(self, test):
172                 root = self._desktop.getChildAtIndex(0)
173                 state = root.getState()
174                 res = StateSet(*st)
175                 if not res.equals(state):
176                         test.fail("States not reported correctly")
177
178         def test_childCount(self, test):
179                 root = self._desktop.getChildAtIndex(0)
180                 test.assertEqual(root.childCount, 11,
181                                  "Expected role - \"11\". Recieved - \"%d\"" % (root.childCount,))
182
183         def test_description(self, test):
184                 root = self._desktop.getChildAtIndex(0)
185                 description = "The main accessible object, root of the accessible tree"
186                 test.assertEqual(root.description, description,
187                                  "Expected description - \"%s\". Recieved - \"%s\"" % (description, root.description,))
188
189         def test_tree(self, test):
190                 """
191                 This is a mild stress test for the 
192                 methods:
193
194                 getChildAtIndex
195                 
196                 And the attributes:
197
198                 name
199                 description
200
201                 It checks a tree of these values is correctly
202                 passed from Application to AT.
203                 """
204                 root = self._desktop.getChildAtIndex(0)
205
206                 doc = minidom.Document()
207                 _createNode(root, doc)
208                 answer = doc.toprettyxml()
209
210                 correct = os.path.join(os.environ["TEST_DATA_DIRECTORY"],
211                                         "accessible-test-results.xml")
212                 file = open(correct)
213                 cstring = file.read()
214                 
215                 test.assertEqual(answer, cstring, "Object tree not passed correctly")
216
217         def teardown(self, test):
218                 pass