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