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