1#
2# Copyright 2009 Novell, Inc.
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of the GNU Library General Public
6# License as published by the Free Software Foundation; either
7# version 2 of the License, or (at your option) any later version.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Library General Public License for more details.
13#
14# You should have received a copy of the GNU Library General Public
15# License along with this library; if not, write to the
16# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17# Boston, MA 02110-1301, USA.
18#
19
20import dbus
21from gi.repository import GObject
22import os.path
23
24from xml.dom import minidom
25import os
26
27from pasytest import PasyTest as _PasyTest
28
29import pyatspi
30from pyatspi import StateSet
31
32st = [pyatspi.STATE_MULTI_LINE,
33      pyatspi.STATE_MODAL,
34      pyatspi.STATE_INDETERMINATE,
35      pyatspi.STATE_SUPPORTS_AUTOCOMPLETION,
36      pyatspi.STATE_VERTICAL,]
37
38def _createNode(accessible, parentElement):
39        e = minidom.Element("accessible")
40
41        e.attributes["name"] = accessible.name
42        e.attributes["role"] = str(int(accessible.getRole()))
43        e.attributes["description"] = accessible.description
44
45        for i in range(0, accessible.childCount):
46                _createNode(accessible.getChildAtIndex(i), e)
47
48        parentElement.appendChild(e)
49
50class AccessibleTest(_PasyTest):
51
52        __tests__ = ["setup",
53                     "test_basic",
54                     "test_match_any",
55                     "test_role",
56                     "teardown",
57                     ]
58
59        def __init__(self, bus, path):
60                _PasyTest.__init__(self, "Collection", False)
61                self._bus = bus
62                self._path = path
63
64
65        def setup(self, test):
66                self._registry = pyatspi.Registry()
67                print(self._path)
68                self._desktop = self._registry.getDesktop(0)
69                self._root = pyatspi.findDescendant (self._desktop, lambda x: x.name == "atspi-test-main" and x.getRole() == pyatspi.ROLE_WINDOW)
70
71        def assertObjects(self,test,obj,vars,msg):
72                test.assertEqual(len(obj), len(vars) // 2, msg + " length")
73                for i in range(0, len(vars), 2):
74                        test.assertEqual (vars[i], obj[i//2].name, msg + "name" + "#" + str(i//2))
75                        test.assertEqual(vars[i+1], obj[i//2].getRole(), msg + " role" + "#" + str(i//2))
76
77        # Used to help add new tests
78        def printAsserts(self,obj,msg):
79                print("\t\tself.assertObjects(test,ret,(")
80                for i in range(0,len(obj)):
81                        print("\t\t\t\"" + obj[i].name + "\", " + str(obj[i].getRole()) + ",")
82                print("\t\t), \"", msg, "\")")
83
84        def test_basic(self, test):
85                collection = self._root.queryCollection()
86                stateSet = pyatspi.StateSet()
87                rule = collection.createMatchRule (stateSet.raw(),
88                        collection.MATCH_NONE,
89                [],     # attributes
90                        collection.MATCH_NONE,
91                [],     # role
92                        collection.MATCH_NONE,
93                "",     # interfaces
94                        collection.MATCH_NONE,
95                        False)
96
97                ret = collection.getMatches (rule, collection.SORT_ORDER_CANONICAL, 5, True)
98                self.assertObjects(test,ret,(
99                        "gnome-settings-daemon", 79 ,
100                        "gnome-panel", 79 ,
101                        "Bottom Expanded Edge Panel", 25 ,
102                        "Top Expanded Edge Panel", 25 ,
103                        "nautilus", 79 ,
104                ), " 1 ")
105
106                ret = collection.getMatches (rule, collection.SORT_ORDER_REVERSE_CANONICAL, 5, True)
107                self.assertObjects(test,ret,(
108                        "nautilus", 79,
109                        "Top Expanded Edge Panel", 25,
110                        "Bottom Expanded Edge Panel", 25,
111                        "gnome-panel", 79,
112                        "gnome-settings-daemon", 79,
113                ), " reverse canonical ")
114
115                obj=ret[2]
116                ret = collection.getMatchesTo (obj, rule, collection.SORT_ORDER_REVERSE_CANONICAL, collection.TREE_INORDER, True, 5, True)
117                print("--ret:", len(ret))
118                self.assertObjects(test,ret,(
119                        "gnome-settings-daemon", 79,
120                        "gnome-panel", 79,
121                ), " getMatchesTo ")
122                ret = collection.getMatchesTo (obj, rule, collection.SORT_ORDER_REVERSE_CANONICAL, collection.TREE_INORDER, True, 1, True)
123                self.assertObjects(test,ret,(
124                        "gnome-panel", 79,
125                ), " getMatchesTo w/count=1")
126                ret = collection.getMatchesFrom (obj, rule, collection.SORT_ORDER_REVERSE_CANONICAL, collection.TREE_INORDER, 5, True)
127                self.assertObjects(test,ret,(
128                        "tracker-applet", 79,
129                        "metacity", 79,
130                        "Desktop", 25,
131                        "nautilus", 79,
132                        "Top Expanded Edge Panel", 25,
133                ), " getMatchesFrom ")
134                obj = self._root.getChildAtIndex(1)
135                ret = collection.getMatchesFrom (obj, rule, collection.SORT_ORDER_CANONICAL, collection.TREE_RESTRICT_CHILDREN, 5, True)
136                self.assertObjects(test,ret,(
137                        "Top Expanded Edge Panel", 25,
138                ), " Restrict Children ")
139
140        def test_match_any(self, test):
141                collection = self._root.queryCollection()
142                stateSet = pyatspi.StateSet()
143                rule = collection.createMatchRule (stateSet.raw(),
144                        collection.MATCH_ANY,
145                [],     # attributes
146                        collection.MATCH_ANY,
147                [],     # role
148                        collection.MATCH_ANY,
149                "",     # interfaces
150                        collection.MATCH_NONE,
151                        False)
152
153                ret = collection.getMatches (rule, collection.SORT_ORDER_CANONICAL, 5, True)
154                self.assertObjects(test,ret,(
155                        "gnome-settings-daemon", 79 ,
156                        "gnome-panel", 79 ,
157                        "Bottom Expanded Edge Panel", 25 ,
158                        "Top Expanded Edge Panel", 25 ,
159                        "nautilus", 79 ,
160                ), " 1 ")
161
162        def test_role(self, test):
163                collection = self._root.queryCollection()
164                stateSet = pyatspi.StateSet()
165
166                rule = collection.createMatchRule (stateSet.raw(),
167                        collection.MATCH_NONE,
168                [],     # attributes
169                        collection.MATCH_NONE,
170                [pyatspi.ROLE_RADIO_MENU_ITEM],
171                        collection.MATCH_ANY,
172                "",     # interfaces
173                        collection.MATCH_NONE,
174                        False)
175
176                ret = collection.getMatches (rule, collection.SORT_ORDER_CANONICAL, 5, True)
177                self.assertObjects(test,ret,(
178                        "Activity Indicator", 45,
179                        "Back", 45,
180                        "Forward", 45,
181                        "", 45,
182                        "Reload", 45,
183                ), " role ")
184
185                rule = collection.createMatchRule (stateSet.raw(),
186                        collection.MATCH_NONE,
187                [],     # attributes
188                        collection.MATCH_NONE,
189                [pyatspi.ROLE_ENTRY, pyatspi.ROLE_HTML_CONTAINER],
190                        collection.MATCH_ANY,
191                "",     # interfaces
192                        collection.MATCH_NONE,
193                        False)
194
195                ret = collection.getMatches (rule, collection.SORT_ORDER_CANONICAL, 5, True)
196                self.assertObjects(test,ret,(
197                        "gnome-settings-daemon", 79,
198                        "gnome-panel", 79,
199                        "Bottom Expanded Edge Panel", 25,
200                        "Top Expanded Edge Panel", 25,
201                        "nautilus", 79,
202                        ), " role #2")
203
204        def teardown(self, test):
205                pass
206