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