1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Simon Poirier <simpoir@gmail.com>
# Copyright (C) 2009, Vincent Vinet <vince.vinet@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
Gtk Utils Tests
This module contains all the tests that pertain to the usage of the Tutorius
gtkutils
"""
import unittest
import logging
import gtk, gobject
from sugar.tutorius.gtkutils import find_widget, register_signals_numbered, register_signals, get_children
class SignalCatcher(object):
"""Test class that store arguments received on it's callback method.
Useful for testing callbacks"""
def __init__(self):
"""Constructor"""
self.data = None
def callback(self, *args):
"""Callback function, stores argument list in self.data"""
self.data = args
def disconnect_handlers(hlist):
"""Disconnect handles in handler list. hlist must be a list of
two-tuples (widget, handler_id)"""
for widget, handler in hlist:
try:
widget.handler_disconnect(handler)
except:
pass
class GtkUtilsTests(unittest.TestCase):
def __init__(self,*args):
unittest.TestCase.__init__(self,*args)
self.widgets = {}
self.top = None
def setUp(self):
#create hierarchy
self.top = gtk.Window(type=gtk.WINDOW_TOPLEVEL)
self.top.set_name("Top")
self.widgets["top"] = {"named":"Top","numbered":"0","widget":self.top}
hbox = gtk.HBox()
self.top.add(hbox)
hbox.show()
self.widgets["hbox0"] = {"name":"Top.GtkHBox","numbered":"0.0","widget":hbox}
btn1 = gtk.Button()
btn1.set_name("Button1")
hbox.pack_start(btn1)
btn1.show()
self.widgets["btn1"] = {"name":"Top.GtkHBox.Button1","numbered":"0.0.0","widget":btn1}
btn2 = gtk.Button()
btn2.set_name("Button2")
hbox.pack_start(btn2)
btn2.show()
self.widgets["btn2"] = {"name":"Top.GtkHBox.Button2","numbered":"0.0.1","widget":btn2}
vbox = gtk.VBox()
vbox.set_name("VBox1")
hbox.pack_start(vbox)
hbox.show()
self.widgets["vbox0"] = {"name":"Top.GtkHBox.VBox1","numbered":"0.0.2","widget":vbox}
btn3 = gtk.Button()
btn3.set_name("Button3")
vbox.pack_start(btn3)
btn3.show()
self.widgets["btn3"] = {"name":"Top.GtkHBox.VBox1.Button3","numbered":"0.0.2.0","widget":btn3}
btn4 = gtk.Button()
vbox.pack_start(btn4)
btn4.show()
self.widgets["btn4"] = {"name":"Top.GtkHBox.VBox1.GtkButton","numbered":"0.0.2.1","widget":btn4}
def tearDown(self):
#destroy hierarchy
self.top.destroy()
self.top = None
self.widgets = {}
def test_named(self):
#def register_signals(target, handler, prefix=None, max_depth=None):
s=SignalCatcher()
#Test 0 depth
handler_list = register_signals(self.top, s.callback, max_depth=0)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 1, "register_signals should not have recursed (%d objects registered)" % len(widget_list)
assert widget_list[0] == self.top, "register_signals should have gotten only the top"
disconnect_handlers(handler_list)
#Test 2 depth
handler_list = register_signals(self.top, s.callback, max_depth=2)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 5, "expected %d objects (got %d)" % (len(widget_list), 5)
disconnect_handlers(handler_list)
#Test Infinite depth
handler_list = register_signals(self.top, s.callback, max_depth=None)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 7, "expected %d objects (got %d)" % (len(widget_list), 7)
disconnect_handlers(handler_list)
def test_numbered(self):
s=SignalCatcher()
#def register_signals_numbered(target, handler, prefix="0", max_depth=None):
#Test 0 depth
handler_list = register_signals_numbered(self.top, s.callback, max_depth=0)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 1, "register_signals should not have recursed (%d objects registered)" % len(widget_list)
assert widget_list[0] == self.top, "register_signals should have gotten only the top"
disconnect_handlers(handler_list)
#Test 1 depth
handler_list = register_signals_numbered(self.top, s.callback, max_depth=1)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 2, "expected %d objects (got %d)" % (len(widget_list), 2)
disconnect_handlers(handler_list)
#Test Infinite depth
handler_list = register_signals_numbered(self.top, s.callback, max_depth=None)
#remove duplicates in widget list
widget_list = dict.fromkeys([w for w, h in handler_list]).keys()
assert len(widget_list) == 7, "expected %d objects (got %d)" % (len(widget_list), 7)
disconnect_handlers(handler_list)
def test_find_widget(self):
#Test individual values in the defined widgets
for widget in self.widgets.values():
f = find_widget(self.top, widget["numbered"])
assert f is widget["widget"], "Widget %s found with path %s, expected %s" % (f, widget["numbered"], widget["widget"])
#Test out of index
f = find_widget(self.top, "0.99.1.2")
assert f is self.top, "Should have returned top widget"
def test_register_args_numbered(self):
#Need to check the signal catcher and stuff... grreat
while gtk.events_pending():
gtk.main_iteration(block=False)
def test_register_args_normal(self):
#Need to check the signal catcher and stuff... grreat
while gtk.events_pending():
gtk.main_iteration(block=False)
def test_notwidget(self):
"""Test the get_children function"""
o = object()
res = get_children(o)
assert len(res) == 0, "object has no children"
top_children = get_children(self.top)
expected = [self.widgets["hbox0"]["widget"],]
assert top_children == expected, "expected %s for top's children, got %s" % (str(expected),str(top_children))
if __name__ == "__main__":
unittest.main()
|