1 # -*- coding: utf-8 -*-
2 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
9 sys.path.insert(0, "../")
11 from compathelper import _long, _unicode, _bytes, _unichr
13 from gi.repository import GLib
14 from gi.repository import GObject
15 from gi.repository import Gdk
16 from gi.repository import Gtk
17 from gi.repository import Gio
18 from gi.repository import Pango
19 from gi.repository import GdkPixbuf
20 import gi.overrides as overrides
23 class TestGLib(unittest.TestCase):
25 def test_gvariant_create(self):
28 variant = GLib.Variant('i', 42)
29 self.assertTrue(isinstance(variant, GLib.Variant))
30 self.assertEquals(variant.get_int32(), 42)
32 variant = GLib.Variant('s', '')
33 self.assertTrue(isinstance(variant, GLib.Variant))
34 self.assertEquals(variant.get_string(), '')
36 variant = GLib.Variant('s', 'hello')
37 self.assertTrue(isinstance(variant, GLib.Variant))
38 self.assertEquals(variant.get_string(), 'hello')
41 variant = GLib.Variant('v', GLib.Variant('i', 42))
42 self.assertTrue(isinstance(variant, GLib.Variant))
43 self.assertTrue(isinstance(variant.get_variant(), GLib.Variant))
44 self.assertEqual(variant.get_type_string(), 'v')
45 self.assertEqual(variant.get_variant().get_type_string(), 'i')
46 self.assertEquals(variant.get_variant().get_int32(), 42)
48 variant = GLib.Variant('v', GLib.Variant('v', GLib.Variant('i', 42)))
49 self.assertEqual(variant.get_type_string(), 'v')
50 self.assertEqual(variant.get_variant().get_type_string(), 'v')
51 self.assertEqual(variant.get_variant().get_variant().get_type_string(), 'i')
52 self.assertEquals(variant.get_variant().get_variant().get_int32(), 42)
56 variant = GLib.Variant('()', ())
57 self.assertEqual(variant.get_type_string(), '()')
58 self.assertEquals(variant.n_children(), 0)
60 variant = GLib.Variant('(i)', (3,))
61 self.assertEqual(variant.get_type_string(), '(i)')
62 self.assertTrue(isinstance(variant, GLib.Variant))
63 self.assertEquals(variant.n_children(), 1)
64 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
65 self.assertEquals(variant.get_child_value(0).get_int32(), 3)
67 variant = GLib.Variant('(ss)', ('mec', 'mac'))
68 self.assertEqual(variant.get_type_string(), '(ss)')
69 self.assertTrue(isinstance(variant, GLib.Variant))
70 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
71 self.assertTrue(isinstance(variant.get_child_value(1), GLib.Variant))
72 self.assertEquals(variant.get_child_value(0).get_string(), 'mec')
73 self.assertEquals(variant.get_child_value(1).get_string(), 'mac')
76 variant = GLib.Variant('((si)(ub))', (('hello', -1), (42, True)))
77 self.assertEqual(variant.get_type_string(), '((si)(ub))')
78 self.assertEqual(variant.unpack(), (('hello', -1), (_long(42), True)))
82 variant = GLib.Variant('a{si}', {})
83 self.assertTrue(isinstance(variant, GLib.Variant))
84 self.assertEqual(variant.get_type_string(), 'a{si}')
85 self.assertEquals(variant.n_children(), 0)
87 variant = GLib.Variant('a{si}', {'': 1, 'key1': 2, 'key2': 3})
88 self.assertEqual(variant.get_type_string(), 'a{si}')
89 self.assertTrue(isinstance(variant, GLib.Variant))
90 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
91 self.assertTrue(isinstance(variant.get_child_value(1), GLib.Variant))
92 self.assertTrue(isinstance(variant.get_child_value(2), GLib.Variant))
93 self.assertEqual(variant.unpack(), {'': 1, 'key1': 2, 'key2': 3})
96 variant = GLib.Variant('a{sa{si}}', {})
97 self.assertTrue(isinstance(variant, GLib.Variant))
98 self.assertEqual(variant.get_type_string(), 'a{sa{si}}')
99 self.assertEquals(variant.n_children(), 0)
101 d = {'': {'': 1, 'keyn1': 2},
102 'key1': {'key11': 11, 'key12': 12}}
103 variant = GLib.Variant('a{sa{si}}', d)
104 self.assertEqual(variant.get_type_string(), 'a{sa{si}}')
105 self.assertTrue(isinstance(variant, GLib.Variant))
106 self.assertEqual(variant.unpack(), d)
110 variant = GLib.Variant('ai', [])
111 self.assertEqual(variant.get_type_string(), 'ai')
112 self.assertEquals(variant.n_children(), 0)
114 variant = GLib.Variant('ai', [1, 2])
115 self.assertEqual(variant.get_type_string(), 'ai')
116 self.assertTrue(isinstance(variant, GLib.Variant))
117 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
118 self.assertTrue(isinstance(variant.get_child_value(1), GLib.Variant))
119 self.assertEquals(variant.get_child_value(0).get_int32(), 1)
120 self.assertEquals(variant.get_child_value(1).get_int32(), 2)
122 variant = GLib.Variant('as', [])
123 self.assertEqual(variant.get_type_string(), 'as')
124 self.assertEquals(variant.n_children(), 0)
126 variant = GLib.Variant('as', [''])
127 self.assertEqual(variant.get_type_string(), 'as')
128 self.assertTrue(isinstance(variant, GLib.Variant))
129 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
130 self.assertEquals(variant.get_child_value(0).get_string(), '')
132 variant = GLib.Variant('as', ['hello', 'world'])
133 self.assertEqual(variant.get_type_string(), 'as')
134 self.assertTrue(isinstance(variant, GLib.Variant))
135 self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
136 self.assertTrue(isinstance(variant.get_child_value(1), GLib.Variant))
137 self.assertEquals(variant.get_child_value(0).get_string(), 'hello')
138 self.assertEquals(variant.get_child_value(1).get_string(), 'world')
141 variant = GLib.Variant('aai', [])
142 self.assertEqual(variant.get_type_string(), 'aai')
143 self.assertEquals(variant.n_children(), 0)
145 variant = GLib.Variant('aai', [[]])
146 self.assertEqual(variant.get_type_string(), 'aai')
147 self.assertEquals(variant.n_children(), 1)
148 self.assertEquals(variant.get_child_value(0).n_children(), 0)
150 variant = GLib.Variant('aai', [[1, 2], [3, 4, 5]])
151 self.assertEqual(variant.get_type_string(), 'aai')
152 self.assertEquals(variant.unpack(), [[1, 2], [3, 4, 5]])
158 variant = GLib.Variant('(as)', ([],))
159 self.assertEqual(variant.get_type_string(), '(as)')
160 self.assertEquals(variant.n_children(), 1)
161 self.assertEquals(variant.get_child_value(0).n_children(), 0)
163 variant = GLib.Variant('(as)', ([''],))
164 self.assertEqual(variant.get_type_string(), '(as)')
165 self.assertEquals(variant.n_children(), 1)
166 self.assertEquals(variant.get_child_value(0).n_children(), 1)
167 self.assertEquals(variant.get_child_value(0).get_child_value(0).get_string(), '')
169 variant = GLib.Variant('(as)', (['hello'],))
170 self.assertEqual(variant.get_type_string(), '(as)')
171 self.assertEquals(variant.n_children(), 1)
172 self.assertEquals(variant.get_child_value(0).n_children(), 1)
173 self.assertEquals(variant.get_child_value(0).get_child_value(0).get_string(), 'hello')
175 obj = {'a1': (1, True), 'a2': (2, False)}
176 variant = GLib.Variant('a{s(ib)}', obj)
177 self.assertEqual(variant.get_type_string(), 'a{s(ib)}')
178 self.assertEqual(variant.unpack(), obj)
180 obj = {'a1': (1, GLib.Variant('b', True)), 'a2': (2, GLib.Variant('y', 255))}
181 variant = GLib.Variant('a{s(iv)}', obj)
182 self.assertEqual(variant.get_type_string(), 'a{s(iv)}')
183 self.assertEqual(variant.unpack(), {'a1': (1, True), 'a2': (2, 255)})
185 obj = (1, {'a': {'a1': True, 'a2': False},
190 variant = GLib.Variant('(ia{sa{sb}}s)', obj)
191 self.assertEqual(variant.get_type_string(), '(ia{sa{sb}}s)')
192 self.assertEqual(variant.unpack(), obj)
194 obj = {"frequency": GLib.Variant('t', 738000000),
195 "hierarchy": GLib.Variant('i', 0),
196 "bandwidth": GLib.Variant('x', 8),
197 "code-rate-hp": GLib.Variant('d', 2.0/3.0),
198 "constellation": GLib.Variant('s', "QAM16"),
199 "guard-interval": GLib.Variant('u', 4),}
200 variant = GLib.Variant('a{sv}', obj)
201 self.assertEqual(variant.get_type_string(), 'a{sv}')
202 self.assertEqual(variant.unpack(), {"frequency": 738000000,
205 "code-rate-hp": 2.0/3.0,
206 "constellation": "QAM16",
207 "guard-interval": 4})
209 def test_gvariant_create_errors(self):
211 self.assertRaises(TypeError, GLib.Variant, 'i', 42, 3)
212 self.assertRaises(TypeError, GLib.Variant, '(i)', (42, 3))
214 # not enough arguments
215 self.assertRaises(TypeError, GLib.Variant, '(ii)', (42,))
218 self.assertRaises(TypeError, GLib.Variant, 'i', 'hello')
219 self.assertRaises(TypeError, GLib.Variant, 's', 42)
220 self.assertRaises(TypeError, GLib.Variant, '(ss)', 'mec', 'mac')
222 # unimplemented data type
223 self.assertRaises(NotImplementedError, GLib.Variant, 'Q', 1)
225 def test_gvariant_unpack(self):
227 res = GLib.Variant.new_int32(-42).unpack()
228 self.assertEqual(res, -42)
230 res = GLib.Variant.new_uint64(34359738368).unpack()
231 self.assertEqual(res, 34359738368)
233 res = GLib.Variant.new_boolean(True).unpack()
234 self.assertEqual(res, True)
236 res = GLib.Variant.new_object_path('/foo/Bar').unpack()
237 self.assertEqual(res, '/foo/Bar')
240 res = GLib.Variant('v', GLib.Variant.new_int32(-42)).unpack()
241 self.assertEqual(res, -42)
243 variant = GLib.Variant('v', GLib.Variant('v', GLib.Variant('i', 42)))
244 self.assertEqual(res, -42)
247 res = GLib.Variant.new_tuple(GLib.Variant.new_int32(-1),
248 GLib.Variant.new_string('hello')).unpack()
249 self.assertEqual(res, (-1, 'hello'))
252 vb = GLib.VariantBuilder.new(gi._gi.variant_type_from_string('ai'))
253 vb.add_value(GLib.Variant.new_int32(-1))
254 vb.add_value(GLib.Variant.new_int32(3))
255 res = vb.end().unpack()
256 self.assertEqual(res, [-1, 3])
259 res = GLib.Variant('a{si}', {'key1': 1, 'key2': 2}).unpack()
260 self.assertEqual(res, {'key1': 1, 'key2': 2})
262 def test_gvariant_iteration(self):
264 vb = GLib.VariantBuilder.new(gi._gi.variant_type_from_string('ai'))
265 vb.add_value(GLib.Variant.new_int32(-1))
266 vb.add_value(GLib.Variant.new_int32(3))
269 self.assertEqual(len(v), 2)
270 self.assertEqual(v[0], -1)
271 self.assertEqual(v[1], 3)
272 self.assertEqual(v[-1], 3)
273 self.assertEqual(v[-2], -1)
274 self.assertRaises(IndexError, v.__getitem__, 2)
275 self.assertRaises(IndexError, v.__getitem__, -3)
276 self.assertRaises(ValueError, v.__getitem__, 'a')
279 self.assertEqual([x for x in v], [-1, 3])
280 self.assertEqual(list(v), [-1, 3])
283 v = GLib.Variant.new_tuple(GLib.Variant.new_int32(-1),
284 GLib.Variant.new_string('hello'))
285 self.assertEqual(len(v), 2)
286 self.assertEqual(v[0], -1)
287 self.assertEqual(v[1], 'hello')
288 self.assertEqual(v[-1], 'hello')
289 self.assertEqual(v[-2], -1)
290 self.assertRaises(IndexError, v.__getitem__, 2)
291 self.assertRaises(IndexError, v.__getitem__, -3)
292 self.assertRaises(ValueError, v.__getitem__, 'a')
295 self.assertEqual([x for x in v], [-1, 'hello'])
296 self.assertEqual(tuple(v), (-1, 'hello'))
298 # dictionary index access
299 vsi = GLib.Variant('a{si}', {'key1': 1, 'key2': 2})
300 vis = GLib.Variant('a{is}', {1: 'val1', 5: 'val2'})
302 self.assertEqual(len(vsi), 2)
303 self.assertEqual(vsi['key1'], 1)
304 self.assertEqual(vsi['key2'], 2)
305 self.assertRaises(KeyError, vsi.__getitem__, 'unknown')
307 self.assertEqual(len(vis), 2)
308 self.assertEqual(vis[1], 'val1')
309 self.assertEqual(vis[5], 'val2')
310 self.assertRaises(KeyError, vsi.__getitem__, 3)
312 # dictionary iteration
313 self.assertEqual(set(vsi.keys()), set(['key1', 'key2']))
314 self.assertEqual(set(vis.keys()), set([1, 5]))
316 # string index access
317 v = GLib.Variant('s', 'hello')
318 self.assertEqual(len(v), 5)
319 self.assertEqual(v[0], 'h')
320 self.assertEqual(v[4], 'o')
321 self.assertEqual(v[-1], 'o')
322 self.assertEqual(v[-5], 'h')
323 self.assertRaises(IndexError, v.__getitem__, 5)
324 self.assertRaises(IndexError, v.__getitem__, -6)
327 self.assertEqual([x for x in v], ['h', 'e', 'l', 'l', 'o'])
329 def test_variant_split_signature(self):
330 self.assertEqual(GLib.Variant.split_signature('()'), [])
332 self.assertEqual(GLib.Variant.split_signature('s'), ['s'])
334 self.assertEqual(GLib.Variant.split_signature('as'), ['as'])
336 self.assertEqual(GLib.Variant.split_signature('(s)'), ['s'])
338 self.assertEqual(GLib.Variant.split_signature('(iso)'), ['i', 's', 'o'])
340 self.assertEqual(GLib.Variant.split_signature('(s(ss)i(ii))'),
341 ['s', '(ss)', 'i', '(ii)'])
343 self.assertEqual(GLib.Variant.split_signature('(as)'), ['as'])
345 self.assertEqual(GLib.Variant.split_signature('(s(ss)iaiaasa(ii))'),
346 ['s', '(ss)', 'i', 'ai', 'aas', 'a(ii)'])
348 self.assertEqual(GLib.Variant.split_signature('(a{iv}(ii)((ss)a{s(ss)}))'),
349 ['a{iv}', '(ii)', '((ss)a{s(ss)})'])
351 class TestPango(unittest.TestCase):
353 def test_default_font_description(self):
354 desc = Pango.FontDescription()
355 self.assertEquals(desc.get_variant(), Pango.Variant.NORMAL)
357 def test_font_description(self):
358 desc = Pango.FontDescription('monospace')
359 self.assertEquals(desc.get_family(), 'monospace')
360 self.assertEquals(desc.get_variant(), Pango.Variant.NORMAL)
362 def test_layout(self):
363 self.assertRaises(TypeError, Pango.Layout)
364 context = Pango.Context()
365 layout = Pango.Layout(context)
366 self.assertEquals(layout.get_context(), context)
368 class TestGdk(unittest.TestCase):
370 def test_constructor(self):
371 attribute = Gdk.WindowAttr()
372 attribute.window_type = Gdk.WindowType.CHILD
373 attributes_mask = Gdk.WindowAttributesType.X | \
374 Gdk.WindowAttributesType.Y
375 window = Gdk.Window(None, attribute, attributes_mask)
376 self.assertEquals(window.get_window_type(), Gdk.WindowType.CHILD)
378 def test_color(self):
379 color = Gdk.Color(100, 200, 300)
380 self.assertEquals(color.red, 100)
381 self.assertEquals(color.green, 200)
382 self.assertEquals(color.blue, 300)
383 self.assertEquals(color, Gdk.Color(100, 200, 300))
384 self.assertNotEquals(color, Gdk.Color(1, 2, 3))
387 self.assertEquals(Gdk.RGBA, overrides.Gdk.RGBA)
388 rgba = Gdk.RGBA(0.1, 0.2, 0.3, 0.4)
389 self.assertEquals(rgba, Gdk.RGBA(0.1, 0.2, 0.3, 0.4))
390 self.assertNotEquals(rgba, Gdk.RGBA(0.0, 0.2, 0.3, 0.4))
391 self.assertEquals(rgba.red, 0.1)
392 self.assertEquals(rgba.green, 0.2)
393 self.assertEquals(rgba.blue, 0.3)
394 self.assertEquals(rgba.alpha, 0.4)
396 self.assertEquals(rgba.green, 0.9)
398 def test_event(self):
399 event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
400 self.assertEquals(event.type, Gdk.EventType.CONFIGURE)
401 self.assertEquals(event.send_event, 0)
403 event = Gdk.Event.new(Gdk.EventType.DRAG_MOTION)
404 event.x_root, event.y_root = 0, 5
405 self.assertEquals(event.x_root, 0)
406 self.assertEquals(event.y_root, 5)
409 event.type = Gdk.EventType.SCROLL
410 self.assertRaises(AttributeError, lambda: getattr(event, 'foo_bar'))
412 def test_event_structures(self):
413 def button_press_cb(button, event):
414 self.assertTrue(isinstance(event, Gdk.EventButton))
415 self.assertTrue(event.type == Gdk.EventType.BUTTON_PRESS)
416 self.assertEquals(event.send_event, 0)
417 self.assertEquals(event.get_state(), Gdk.ModifierType.CONTROL_MASK)
418 self.assertEquals(event.get_root_coords(), (2, 5))
421 self.assertEquals(event.get_time(), 12345)
425 b.connect('button-press-event', button_press_cb)
428 Gdk.test_simulate_button(b.get_window(),
431 Gdk.ModifierType.CONTROL_MASK,
432 Gdk.EventType.BUTTON_PRESS)
434 def test_cursor(self):
435 self.assertEquals(Gdk.Cursor, overrides.Gdk.Cursor)
436 c = Gdk.Cursor(Gdk.CursorType.WATCH)
437 self.assertNotEqual(c, None)
438 c = Gdk.Cursor(cursor_type = Gdk.CursorType.WATCH)
439 self.assertNotEqual(c, None)
441 display_manager = Gdk.DisplayManager.get()
442 display = display_manager.get_default_display()
444 test_pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB,
450 c = Gdk.Cursor(display,
454 self.assertNotEqual(c, None)
455 self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3)
457 class TestGtk(unittest.TestCase):
459 def test_container(self):
466 self.assertTrue(label in box)
467 self.assertTrue(label2 in box)
468 self.assertEqual(len(box), 2)
471 self.assertEqual(l, [label, label2])
473 def test_actions(self):
474 self.assertEquals(Gtk.Action, overrides.Gtk.Action)
475 self.assertRaises(TypeError, Gtk.Action)
476 action = Gtk.Action("test", "Test", "Test Action", Gtk.STOCK_COPY)
477 self.assertEquals(action.get_name(), "test")
478 self.assertEquals(action.get_label(), "Test")
479 self.assertEquals(action.get_tooltip(), "Test Action")
480 self.assertEquals(action.get_stock_id(), Gtk.STOCK_COPY)
482 self.assertEquals(Gtk.RadioAction, overrides.Gtk.RadioAction)
483 self.assertRaises(TypeError, Gtk.RadioAction)
484 action = Gtk.RadioAction("test", "Test", "Test Action", Gtk.STOCK_COPY, 1)
485 self.assertEquals(action.get_name(), "test")
486 self.assertEquals(action.get_label(), "Test")
487 self.assertEquals(action.get_tooltip(), "Test Action")
488 self.assertEquals(action.get_stock_id(), Gtk.STOCK_COPY)
489 self.assertEquals(action.get_current_value(), 1)
491 def test_actiongroup(self):
492 self.assertEquals(Gtk.ActionGroup, overrides.Gtk.ActionGroup)
493 self.assertRaises(TypeError, Gtk.ActionGroup)
495 action_group = Gtk.ActionGroup (name = 'TestActionGroup')
496 callback_data = "callback data"
498 def test_action_callback_data(action, user_data):
499 self.assertEquals(user_data, callback_data);
501 def test_radio_action_callback_data(action, current, user_data):
502 self.assertEquals(user_data, callback_data);
504 action_group.add_actions ([
505 ('test-action1', None, 'Test Action 1',
506 None, None, test_action_callback_data),
507 ('test-action2', Gtk.STOCK_COPY, 'Test Action 2',
508 None, None, test_action_callback_data)], callback_data)
509 action_group.add_toggle_actions([
510 ('test-toggle-action1', None, 'Test Toggle Action 1',
511 None, None, test_action_callback_data, False),
512 ('test-toggle-action2', Gtk.STOCK_COPY, 'Test Toggle Action 2',
513 None, None, test_action_callback_data, True)], callback_data)
514 action_group.add_radio_actions([
515 ('test-radio-action1', None, 'Test Radio Action 1'),
516 ('test-radio-action2', Gtk.STOCK_COPY, 'Test Radio Action 2')], 1,
517 test_radio_action_callback_data,
520 expected_results = [('test-action1', Gtk.Action),
521 ('test-action2', Gtk.Action),
522 ('test-toggle-action1', Gtk.ToggleAction),
523 ('test-toggle-action2', Gtk.ToggleAction),
524 ('test-radio-action1', Gtk.RadioAction),
525 ('test-radio-action2', Gtk.RadioAction)]
527 for action in action_group.list_actions():
528 a = (action.get_name(), type(action))
529 self.assertTrue(a in expected_results)
530 expected_results.remove(a)
533 def test_uimanager(self):
534 self.assertEquals(Gtk.UIManager, overrides.Gtk.UIManager)
536 ui.add_ui_from_string(
539 <menubar name="menubar1"></menubar>
543 menubar = ui.get_widget("/menubar1")
544 self.assertEquals(type(menubar), Gtk.MenuBar)
546 ag = Gtk.ActionGroup (name="ag1")
547 ui.insert_action_group(ag)
548 ag2 = Gtk.ActionGroup (name="ag2")
549 ui.insert_action_group(ag2)
550 groups = ui.get_action_groups()
551 self.assertEquals(ag, groups[-2])
552 self.assertEquals(ag2, groups[-1])
554 def test_builder(self):
555 self.assertEquals(Gtk.Builder, overrides.Gtk.Builder)
557 class SignalTest(GObject.GObject):
558 __gtype_name__ = "GIOverrideSignalTest"
560 "test-signal": (GObject.SignalFlags.RUN_FIRST,
570 def on_signal_1(self, *args):
573 def on_signal_3(self, *args):
576 signal_checker = SignalCheck()
577 builder = Gtk.Builder()
579 # add object1 to the builder
580 builder.add_from_string(
583 <object class="GIOverrideSignalTest" id="object1">
584 <signal name="test-signal" handler="on_signal_1" />
589 # only add object3 to the builder
590 builder.add_objects_from_string(
593 <object class="GIOverrideSignalTest" id="object2">
594 <signal name="test-signal" handler="on_signal_2" />
596 <object class="GIOverrideSignalTest" id="object3">
597 <signal name="test-signal" handler="on_signal_3" />
599 <object class="GIOverrideSignalTest" id="object4">
600 <signal name="test-signal" handler="on_signal_4" />
608 builder.connect_signals(signal_checker)
610 # call their notify signals and check sentinel
611 objects = builder.get_objects()
612 self.assertEquals(len(objects), 2)
614 obj.emit('test-signal')
616 self.assertEquals(signal_checker.sentinel, 4)
618 def test_dialogs(self):
619 self.assertEquals(Gtk.Dialog, overrides.Gtk.Dialog)
620 self.assertEquals(Gtk.AboutDialog, overrides.Gtk.AboutDialog)
621 self.assertEquals(Gtk.MessageDialog, overrides.Gtk.MessageDialog)
622 self.assertEquals(Gtk.ColorSelectionDialog, overrides.Gtk.ColorSelectionDialog)
623 self.assertEquals(Gtk.FileChooserDialog, overrides.Gtk.FileChooserDialog)
624 self.assertEquals(Gtk.FontSelectionDialog, overrides.Gtk.FontSelectionDialog)
625 self.assertEquals(Gtk.RecentChooserDialog, overrides.Gtk.RecentChooserDialog)
628 dialog = Gtk.Dialog (title='Foo',
629 flags=Gtk.DialogFlags.MODAL,
630 buttons=('test-button1', 1))
632 dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
634 self.assertEquals('Foo', dialog.get_title())
635 self.assertTrue(dialog.get_modal())
636 button = dialog.get_widget_for_response (1)
637 self.assertEquals('test-button1', button.get_label())
638 button = dialog.get_widget_for_response (2)
639 self.assertEquals('test-button2', button.get_label())
640 button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
641 self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
644 dialog = Gtk.AboutDialog()
647 dialog = Gtk.MessageDialog (title='message dialog test',
648 flags=Gtk.DialogFlags.MODAL,
649 buttons=Gtk.ButtonsType.OK,
650 message_format='dude!')
652 self.assertEquals('message dialog test', dialog.get_title())
653 self.assertTrue(dialog.get_modal())
654 text = dialog.get_property('text')
655 self.assertEquals('dude!', text)
657 dialog.format_secondary_text('2nd text')
658 self.assertEqual(dialog.get_property('secondary-text'), '2nd text')
659 self.assertFalse(dialog.get_property('secondary-use-markup'))
661 dialog.format_secondary_markup('2nd markup')
662 self.assertEqual(dialog.get_property('secondary-text'), '2nd markup')
663 self.assertTrue(dialog.get_property('secondary-use-markup'))
665 # Gtk.ColorSelectionDialog
666 dialog = Gtk.ColorSelectionDialog("color selection dialog test")
667 self.assertEquals('color selection dialog test', dialog.get_title())
669 # Gtk.FileChooserDialog
670 dialog = Gtk.FileChooserDialog (title='file chooser dialog test',
671 buttons=('test-button1', 1),
672 action=Gtk.FileChooserAction.SAVE)
674 dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
675 self.assertEquals('file chooser dialog test', dialog.get_title())
676 button = dialog.get_widget_for_response (1)
677 self.assertEquals('test-button1', button.get_label())
678 button = dialog.get_widget_for_response (2)
679 self.assertEquals('test-button2', button.get_label())
680 button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
681 self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
682 action = dialog.get_property('action')
683 self.assertEquals(Gtk.FileChooserAction.SAVE, action)
686 # Gtk.FontSelectionDialog
687 dialog = Gtk.ColorSelectionDialog("font selection dialog test")
688 self.assertEquals('font selection dialog test', dialog.get_title())
690 # Gtk.RecentChooserDialog
691 test_manager = Gtk.RecentManager()
692 dialog = Gtk.RecentChooserDialog (title='recent chooser dialog test',
693 buttons=('test-button1', 1),
694 manager=test_manager)
696 dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
697 self.assertEquals('recent chooser dialog test', dialog.get_title())
698 button = dialog.get_widget_for_response (1)
699 self.assertEquals('test-button1', button.get_label())
700 button = dialog.get_widget_for_response (2)
701 self.assertEquals('test-button2', button.get_label())
702 button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
703 self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
705 class TestClass(GObject.GObject):
706 __gtype_name__ = "GIOverrideTreeAPITest"
708 def __init__(self, tester, int_value, string_value):
709 super(TestGtk.TestClass, self).__init__()
711 self.int_value = int_value
712 self.string_value = string_value
714 def check(self, int_value, string_value):
715 self.tester.assertEquals(int_value, self.int_value)
716 self.tester.assertEquals(string_value, self.string_value)
718 def test_tree_store(self):
719 self.assertEquals(Gtk.TreeStore, overrides.Gtk.TreeStore)
720 self.assertEquals(Gtk.ListStore, overrides.Gtk.ListStore)
721 self.assertEquals(Gtk.TreeModel, overrides.Gtk.TreeModel)
722 self.assertEquals(Gtk.TreeViewColumn, overrides.Gtk.TreeViewColumn)
724 class TestPyObject(object):
727 test_pyobj = TestPyObject()
728 test_pydict = {1:1, "2":2, "3":"3"}
729 test_pylist = [1,"2", "3"]
730 tree_store = Gtk.TreeStore(int,
733 GObject.TYPE_PYOBJECT,
748 label = 'this is child #%d' % i
749 testobj = TestGtk.TestClass(self, i, label)
750 parent = tree_store.append(parent, (i,
767 parent = tree_store.append(parent)
769 label = 'this is child #%d' % i
770 testobj = TestGtk.TestClass(self, i, label)
771 tree_store.set(parent, 0, i,
781 10, GObject.G_MAXULONG,
782 11, GObject.G_MININT64,
783 12, 0xffffffffffffffff,
787 parent = tree_store.append(parent)
789 label = 'this is child #%d' % i
790 testobj = TestGtk.TestClass(self, i, label)
791 tree_store.set(parent, {0: i,
801 10: GObject.G_MAXULONG,
802 11: GObject.G_MININT64,
803 12: 0xffffffffffffffff,
807 parent = tree_store.append(parent)
809 label = 'this is child #%d' % i
810 testobj = TestGtk.TestClass(self, i, label)
811 tree_store.set(parent, (0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14),
828 # len gets the number of children in the root node
829 # since we kept appending to the previous node
830 # there should only be one child of the root
831 self.assertEquals(len(tree_store), 1)
833 # walk the tree to see if the values were stored correctly
837 treeiter = tree_store.iter_children(parent)
839 i = tree_store.get_value(treeiter, 0)
840 s = tree_store.get_value(treeiter, 1)
841 obj = tree_store.get_value(treeiter, 2)
843 obj2 = tree_store.get_value(treeiter, 3)
844 self.assertEquals(obj, obj2);
846 pyobj = tree_store.get_value(treeiter, 4)
847 self.assertEquals(pyobj, test_pyobj)
848 pydict = tree_store.get_value(treeiter, 5)
849 self.assertEquals(pydict, test_pydict)
850 pylist = tree_store.get_value(treeiter, 6)
851 self.assertEquals(pylist, test_pylist)
853 bool_1 = tree_store.get_value(treeiter, 7)
854 bool_2 = tree_store.get_value(treeiter, 8)
855 self.assertEquals(bool_1, bool_2)
856 self.assertTrue(isinstance(bool_1, bool))
857 self.assertTrue(isinstance(bool_2, bool))
859 uint_ = tree_store.get_value(treeiter, 9)
860 self.assertEquals(uint_, i)
861 ulong_ = tree_store.get_value(treeiter, 10)
862 self.assertEquals(ulong_, GObject.G_MAXULONG)
863 int64_ = tree_store.get_value(treeiter, 11)
864 self.assertEquals(int64_, GObject.G_MININT64)
865 uint64_ = tree_store.get_value(treeiter, 12)
866 self.assertEquals(uint64_, 0xffffffffffffffff)
867 uchar_ = tree_store.get_value(treeiter, 13)
868 self.assertEquals(ord(uchar_), 254)
869 char_ = tree_store.get_value(treeiter, 14)
870 self.assertEquals(char_, 'a')
873 treeiter = tree_store.iter_children(parent)
875 self.assertEquals(i, 99)
877 def test_list_store(self):
878 class TestPyObject(object):
881 test_pyobj = TestPyObject()
882 test_pydict = {1:1, "2":2, "3":"3"}
883 test_pylist = [1,"2", "3"]
885 list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest', object, object, object, bool, bool)
886 for i in range(1, 93):
887 label = 'this is row #%d' % i
888 testobj = TestGtk.TestClass(self, i, label)
889 parent = list_store.append((i,
899 label = _unicode('this is row #93')
900 treeiter = list_store.append()
901 list_store.set_value(treeiter, 0, i)
902 list_store.set_value(treeiter, 1, label)
903 list_store.set_value(treeiter, 2, TestGtk.TestClass(self, i, label))
904 list_store.set_value(treeiter, 3, test_pyobj)
905 list_store.set_value(treeiter, 4, test_pydict)
906 list_store.set_value(treeiter, 5, test_pylist)
907 list_store.set_value(treeiter, 6, 1)
908 list_store.set_value(treeiter, 7, True)
911 label = 'this is row #0'
912 list_store.prepend((0,
914 TestGtk.TestClass(self, 0, label),
921 # test automatic unicode->str conversion
923 label = _unicode('this is row #94')
924 treeiter = list_store.append((i,
926 TestGtk.TestClass(self, i, label),
933 # add sorted items out of order to test insert* apis
934 # also test sending in None to not set a column
936 label = 'this is row #97'
937 treeiter = list_store.append((None,
946 list_store.set_value(treeiter, 0, i)
947 list_store.set_value(treeiter, 1, label)
948 list_store.set_value(treeiter, 2, TestGtk.TestClass(self, i, label))
949 list_store.set_value(treeiter, 4, test_pydict)
950 list_store.set_value(treeiter, 7, True)
954 label = 'this is row #99'
955 list_store.insert(9999, (i,
957 TestGtk.TestClass(self, i, label),
965 label = 'this is row #96'
966 list_store.insert_before(treeiter, (i,
968 TestGtk.TestClass(self, i, label),
976 label = 'this is row #98'
977 list_store.insert_after(treeiter, (i,
979 TestGtk.TestClass(self, i, label),
988 label = 'this is row #95'
989 list_store.insert(95, (i,
991 TestGtk.TestClass(self, i, label),
999 label = 'this is row #100'
1000 treeiter = list_store.append()
1001 list_store.set(treeiter, 1, label,
1003 2, TestGtk.TestClass(self, i, label),
1010 label = 'this is row #101'
1011 treeiter = list_store.append()
1012 list_store.set(treeiter, {1: label,
1014 2: TestGtk.TestClass(self, i, label),
1021 label = 'this is row #102'
1022 treeiter = list_store.append()
1023 list_store.set(treeiter, (1, 0, 2, 3, 4, 5, 6, 7),
1026 TestGtk.TestClass(self, i, label),
1033 self.assertEquals(len(list_store), 103)
1035 # walk the list to see if the values were stored correctly
1037 treeiter = list_store.get_iter_first()
1041 i = list_store.get_value(treeiter, 0)
1042 self.assertEquals(i, counter)
1043 s = list_store.get_value(treeiter, 1)
1044 obj = list_store.get_value(treeiter, 2)
1047 pyobj = list_store.get_value(treeiter, 3)
1048 self.assertEquals(pyobj, test_pyobj)
1049 pydict = list_store.get_value(treeiter, 4)
1050 self.assertEquals(pydict, test_pydict)
1051 pylist = list_store.get_value(treeiter, 5)
1052 self.assertEquals(pylist, test_pylist)
1054 bool_1 = list_store.get_value(treeiter, 6)
1055 bool_2 = list_store.get_value(treeiter, 7)
1056 self.assertEquals(bool_1, bool_2)
1057 self.assertTrue(isinstance(bool_1, bool))
1058 self.assertTrue(isinstance(bool_2, bool))
1060 treeiter = list_store.iter_next(treeiter)
1064 self.assertEquals(i, 102)
1066 def test_tree_path(self):
1068 p2 = Gtk.TreePath.new_first()
1069 self.assertEqual(p1, p2)
1070 self.assertEqual(str(p1), '0')
1071 p1 = Gtk.TreePath(2)
1072 p2 = Gtk.TreePath.new_from_string('2')
1073 self.assertEqual(p1, p2)
1074 self.assertEqual(str(p1), '2')
1075 p1 = Gtk.TreePath('1:2:3')
1076 p2 = Gtk.TreePath.new_from_string('1:2:3')
1077 self.assertEqual(p1, p2)
1078 self.assertEqual(str(p1), '1:2:3')
1079 p1 = Gtk.TreePath((1,2,3))
1080 p2 = Gtk.TreePath.new_from_string('1:2:3')
1081 self.assertEqual(p1, p2)
1082 self.assertEqual(str(p1), '1:2:3')
1083 self.assertTrue(p1 != None)
1084 self.assertFalse(p1 == None)
1085 self.assertTrue(p1 > None)
1086 self.assertTrue(p1 >= None)
1087 self.assertFalse(p1 < None)
1088 self.assertFalse(p1 <= None)
1090 def test_tree_model(self):
1091 tree_store = Gtk.TreeStore(int, str)
1093 self.assertTrue(tree_store)
1094 self.assertEqual(len(tree_store), 0)
1095 self.assertEqual(tree_store.get_iter_first(), None)
1097 def get_by_index(row, col=None):
1099 return tree_store[row][col]
1101 return tree_store[row]
1103 self.assertRaises(TypeError, get_by_index, None)
1104 self.assertRaises(TypeError, get_by_index, "")
1105 self.assertRaises(TypeError, get_by_index, ())
1107 self.assertRaises(IndexError, get_by_index, "0")
1108 self.assertRaises(IndexError, get_by_index, 0)
1109 self.assertRaises(IndexError, get_by_index, (0,))
1111 self.assertRaises(ValueError, tree_store.get_iter, "0")
1112 self.assertRaises(ValueError, tree_store.get_iter, 0)
1113 self.assertRaises(ValueError, tree_store.get_iter, (0,))
1115 self.assertRaises(ValueError, tree_store.get_iter_from_string, "0")
1117 for row in tree_store:
1118 self.fail("Should not be reached")
1120 class DerivedIntType(int):
1123 class DerivedStrType(str):
1126 for i in range(100):
1127 label = 'this is row #%d' % i
1128 parent = tree_store.append(None, (DerivedIntType(i), DerivedStrType(label),))
1129 self.assertNotEquals(parent, None)
1131 label = 'this is child #%d of node #%d' % (j, i)
1132 child = tree_store.append(parent, (j, label,))
1133 self.assertNotEqual(child, None)
1135 self.assertTrue(tree_store)
1136 self.assertEqual(len(tree_store), 100)
1138 for i,row in enumerate(tree_store):
1139 self.assertEqual(row.model, tree_store)
1140 self.assertEqual(row.parent, None)
1142 self.assertEqual(tree_store[i].path, row.path)
1143 self.assertEqual(tree_store[str(i)].path, row.path)
1144 self.assertEqual(tree_store[(i,)].path, row.path)
1146 self.assertEqual(tree_store[i][0], i)
1147 self.assertEqual(tree_store[i][1], "this is row #%d" % i)
1149 aiter = tree_store.get_iter(i)
1150 self.assertEqual(tree_store.get_path(aiter), row.path)
1152 aiter = tree_store.get_iter(str(i))
1153 self.assertEqual(tree_store.get_path(aiter), row.path)
1155 aiter = tree_store.get_iter((i,))
1156 self.assertEqual(tree_store.get_path(aiter), row.path)
1158 self.assertEqual(tree_store.iter_parent(aiter), row.parent)
1160 next = tree_store.iter_next(aiter)
1161 if i < len(tree_store) - 1:
1162 self.assertEqual(tree_store.get_path(next), row.next.path)
1164 self.assertEqual(next, None)
1166 self.assertEqual(tree_store.iter_n_children(row.iter), 20)
1168 child = tree_store.iter_children(row.iter)
1169 for j,childrow in enumerate(row.iterchildren()):
1170 child_path = tree_store.get_path(child)
1171 self.assertEqual(childrow.path, child_path)
1172 self.assertEqual(childrow.parent.path, row.path)
1173 self.assertEqual(childrow.path, tree_store[child].path)
1174 self.assertEqual(childrow.path, tree_store[child_path].path)
1176 self.assertEqual(childrow[0], tree_store[child][0])
1177 self.assertEqual(childrow[0], j)
1178 self.assertEqual(childrow[1], tree_store[child][1])
1179 self.assertEqual(childrow[1], 'this is child #%d of node #%d' % (j, i))
1181 self.assertRaises(IndexError, get_by_index, child, 2)
1183 tree_store[child][1] = 'this was child #%d of node #%d' % (j, i)
1184 self.assertEqual(childrow[1], 'this was child #%d of node #%d' % (j, i))
1186 nth_child = tree_store.iter_nth_child(row.iter, j)
1187 self.assertEqual(childrow.path, tree_store.get_path(nth_child))
1189 childrow2 = tree_store["%d:%d" % (i, j)]
1190 self.assertEqual(childrow.path, childrow2.path)
1192 childrow2 = tree_store[(i, j,)]
1193 self.assertEqual(childrow.path, childrow2.path)
1195 child = tree_store.iter_next(child)
1197 self.assertEqual(childrow.next.path, tree_store.get_path(child))
1199 self.assertEqual(child, childrow.next)
1200 self.assertEqual(child, None)
1202 self.assertEqual(j, 19)
1204 self.assertEqual(i, 99)
1207 for i in range(-1,-100,-1):
1209 self.assertEqual(tree_store[i][0], i_real)
1212 for j in range(-1, -20, -1):
1214 path = (i_real, j_real,)
1216 self.assertEqual(tree_store[path][-2], j_real)
1218 label = 'this was child #%d of node #%d' % (j_real, i_real)
1219 self.assertEqual(tree_store[path][-1], label)
1221 new_label = 'this still is child #%d of node #%d' % (j_real, i_real)
1222 tree_store[path][-1] = new_label
1223 self.assertEqual(tree_store[path][-1], new_label)
1225 self.assertRaises(IndexError, get_by_index, path, -3)
1227 self.assertRaises(IndexError, get_by_index, -101)
1229 last_row = tree_store[99]
1230 self.assertNotEqual(last_row, None)
1232 for i,childrow in enumerate(last_row.iterchildren()):
1234 self.assertTrue(tree_store.remove(childrow.iter))
1236 self.assertFalse(tree_store.remove(childrow.iter))
1238 self.assertEqual(i, 19)
1240 self.assertEqual(tree_store.iter_n_children(last_row.iter), 0)
1241 for childrow in last_row.iterchildren():
1242 self.fail("Should not be reached")
1244 aiter = tree_store.get_iter(10)
1245 self.assertRaises(TypeError, tree_store.get, aiter, 1, 'a')
1246 self.assertRaises(ValueError, tree_store.get, aiter, 1, -1)
1247 self.assertRaises(ValueError, tree_store.get, aiter, 1, 100)
1248 self.assertEqual(tree_store.get(aiter, 0, 1), (10, 'this is row #10'))
1250 def test_tree_model_edit(self):
1251 model = Gtk.ListStore(int, str, float)
1252 model.append([1, "one", -0.1])
1253 model.append([2, "two", -0.2])
1258 self.assertRaises(TypeError, set_row, 3)
1259 self.assertRaises(TypeError, set_row, "three")
1260 self.assertRaises(ValueError, set_row, [])
1261 self.assertRaises(ValueError, set_row, [3, "three"])
1263 model[0] = (3, "three", -0.3)
1265 def test_tree_model_unicode(self):
1266 model = Gtk.ListStore(str)
1268 text = (_unicode("just\na\tstring"),
1269 _unicode("Tr") + _unichr(0xf6) + _unicode("del"),
1270 _unicode("N") + _unichr(0xe1) + _unicode("m") + _unichr(0xe8),
1271 _unichr(0xff) + _unichr(0xdf) + _unicode("er"),
1272 _unichr(0x152) + _unichr(0x180) + _unichr(0x1d25))
1275 model.append([item])
1277 def byte_compare(have, expected):
1278 if sys.version_info < (3, 0):
1279 self.assertTrue(isinstance(have, unicode))
1281 self.assertTrue(isinstance(have, str))
1283 self.assertEqual(len(have), len(expected))
1284 for i in range(len(expected)):
1287 self.assertEqual(a, b)
1288 self.assertEqual(ord(a), ord(b))
1290 for i in range(len(model)):
1291 byte_compare(model[i][0], text[i])
1293 def test_tree_row_slice(self):
1294 model = Gtk.ListStore(int, str, float)
1295 model.append([1, "one", -0.1])
1297 self.assertEqual([1, "one", -0.1], model[0][:])
1298 self.assertEqual([1, "one"], model[0][:2])
1299 self.assertEqual(["one", -0.1], model[0][1:])
1300 self.assertEqual(["one"], model[0][1:-1])
1301 self.assertEqual([1], model[0][:-2])
1302 self.assertEqual([], model[0][5:])
1303 self.assertEqual([1, -0.1], model[0][0:3:2])
1305 model[0][:] = (2, "two", -0.2)
1306 self.assertEqual([2, "two", -0.2], model[0][:])
1308 model[0][:2] = (3, "three")
1309 self.assertEqual([3, "three", -0.2], model[0][:])
1311 model[0][1:] = ("four", -0.4)
1312 self.assertEqual([3, "four", -0.4], model[0][:])
1314 model[0][1:-1] = ("five",)
1315 self.assertEqual([3, "five", -0.4], model[0][:])
1317 model[0][0:3:2] = (6, -0.6)
1318 self.assertEqual([6, "five", -0.6], model[0][:])
1321 model[0][5:] = ("doesn't", "matter",)
1323 self.assertRaises(ValueError, set_row1)
1326 model[0][:1] = (0, "zero", 0)
1328 self.assertRaises(ValueError, set_row2)
1331 model[0][:2] = ("0", 0)
1333 self.assertRaises(ValueError, set_row3)
1335 def test_tree_view_column(self):
1336 cell = Gtk.CellRendererText()
1337 column = Gtk.TreeViewColumn(title='This is just a test',
1342 def test_tree_selection(self):
1343 store = Gtk.ListStore(int, str)
1345 store.append((i, "foo"))
1346 view = Gtk.TreeView()
1347 view.set_model(store)
1348 firstpath = store.get_path(store.get_iter_first())
1349 sel = view.get_selection()
1351 sel.select_path(firstpath)
1352 (m, s) = sel.get_selected()
1353 self.assertEqual(m, store)
1354 self.assertEqual(store.get_path(s), firstpath)
1357 (m, s) = sel.get_selected()
1358 self.assertEqual(m, store)
1359 self.assertEqual(store.get_path(s), firstpath)
1361 sel.select_path("0:0")
1362 (m, s) = sel.get_selected()
1363 self.assertEqual(m, store)
1364 self.assertEqual(store.get_path(s), firstpath)
1366 sel.select_path((0,0))
1367 (m, s) = sel.get_selected()
1368 self.assertEqual(m, store)
1369 self.assertEqual(store.get_path(s), firstpath)
1371 def test_text_buffer(self):
1372 self.assertEquals(Gtk.TextBuffer, overrides.Gtk.TextBuffer)
1373 buffer = Gtk.TextBuffer()
1374 tag = buffer.create_tag ('title', font = 'Sans 18')
1376 self.assertEquals(tag.props.name, 'title')
1377 self.assertEquals(tag.props.font, 'Sans 18')
1379 (start, end) = buffer.get_bounds()
1381 mark = buffer.create_mark(None, start)
1382 self.assertFalse(mark.get_left_gravity())
1384 buffer.set_text('Hello Jane Hello Bob')
1385 (start, end) = buffer.get_bounds()
1386 text = buffer.get_text(start, end, False)
1387 self.assertEquals(text, 'Hello Jane Hello Bob')
1390 (start, end) = buffer.get_bounds()
1391 text = buffer.get_text(start, end, False)
1392 self.assertEquals(text, '')
1394 buffer.insert(end, 'HelloHello')
1395 buffer.insert(end, ' Bob')
1397 cursor_iter = end.copy()
1398 cursor_iter.backward_chars(9)
1399 buffer.place_cursor(cursor_iter)
1400 buffer.insert_at_cursor(' Jane ')
1402 (start, end) = buffer.get_bounds()
1403 text = buffer.get_text(start, end, False)
1404 self.assertEquals(text, 'Hello Jane Hello Bob')
1406 sel = buffer.get_selection_bounds()
1407 self.assertEquals(sel, ())
1408 buffer.select_range(start, end)
1409 sel = buffer.get_selection_bounds()
1410 self.assertTrue(sel[0].equal(start))
1411 self.assertTrue(sel[1].equal(end))
1414 buffer.insert_with_tags(buffer.get_start_iter(), 'HelloHello', tag)
1415 (start, end) = buffer.get_bounds()
1416 self.assertTrue(start.begins_tag(tag))
1417 self.assertTrue(start.has_tag(tag))
1420 buffer.insert_with_tags_by_name(buffer.get_start_iter(), 'HelloHello', 'title')
1421 (start, end) = buffer.get_bounds()
1422 self.assertTrue(start.begins_tag(tag))
1423 self.assertTrue(start.has_tag(tag))
1425 self.assertRaises(ValueError, buffer.insert_with_tags_by_name,
1426 buffer.get_start_iter(), 'HelloHello', 'unknowntag')
1428 def test_text_iter(self):
1429 self.assertEquals(Gtk.TextIter, overrides.Gtk.TextIter)
1430 buffer = Gtk.TextBuffer()
1431 buffer.set_text('Hello Jane Hello Bob')
1432 tag = buffer.create_tag ('title', font = 'Sans 18')
1433 (start, end) = buffer.get_bounds()
1434 start.forward_chars(10)
1435 buffer.apply_tag(tag, start, end)
1436 self.assertTrue(start.begins_tag())
1437 self.assertTrue(end.ends_tag())
1438 self.assertTrue(start.toggles_tag())
1439 self.assertTrue(end.toggles_tag())
1440 start.backward_chars(1)
1441 self.assertFalse(start.begins_tag())
1442 self.assertFalse(start.ends_tag())
1443 self.assertFalse(start.toggles_tag())
1445 def test_buttons(self):
1446 self.assertEquals(Gtk.Button, overrides.Gtk.Button)
1449 button = Gtk.Button()
1450 button = Gtk.Button(stock=Gtk.STOCK_CLOSE)
1451 self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
1452 self.assertTrue(button.get_use_stock())
1453 self.assertTrue(button.get_use_underline())
1455 # test Gtk.LinkButton
1456 self.assertRaises(TypeError, Gtk.LinkButton)
1457 button = Gtk.LinkButton('http://www.gtk.org', 'Gtk')
1458 self.assertEquals('http://www.gtk.org', button.get_uri())
1459 self.assertEquals('Gtk', button.get_label())
1461 def test_inheritance(self):
1462 for name in overrides.Gtk.__all__:
1463 over = getattr(overrides.Gtk, name)
1464 for element in dir(Gtk):
1466 klass = getattr(Gtk, element)
1467 info = klass.__info__
1468 except (NotImplementedError, AttributeError):
1471 # Get all parent classes and interfaces klass inherits from
1472 if isinstance(info, gi.types.ObjectInfo):
1473 classes = list(info.get_interfaces())
1474 parent = info.get_parent()
1475 while parent.get_name() != "Object":
1476 classes.append(parent)
1477 parent = parent.get_parent()
1478 classes = [kl for kl in classes if kl.get_namespace() == "Gtk"]
1483 if kl.get_name() == name:
1484 self.assertTrue(issubclass(klass, over,),
1485 "%r does not inherit from override %r" % (klass, over,))
1487 def test_editable(self):
1488 self.assertEquals(Gtk.Editable, overrides.Gtk.Editable)
1490 # need to use Gtk.Entry because Editable is an interface
1492 pos = entry.insert_text('HeWorld', 0)
1493 self.assertEquals(pos, 7)
1494 pos = entry.insert_text('llo ', 2)
1495 self.assertEquals(pos, 6)
1496 text = entry.get_chars(0, 11)
1497 self.assertEquals('Hello World', text)
1499 def test_label(self):
1500 label = Gtk.Label(label='Hello')
1501 self.assertEquals(label.get_text(), 'Hello')
1503 def adjustment_check(self, adjustment, value=0.0, lower=0.0, upper=0.0,
1504 step_increment=0.0, page_increment=0.0, page_size=0.0):
1505 self.assertEquals(adjustment.get_value(), value)
1506 self.assertEquals(adjustment.get_lower(), lower)
1507 self.assertEquals(adjustment.get_upper(), upper)
1508 self.assertEquals(adjustment.get_step_increment(), step_increment)
1509 self.assertEquals(adjustment.get_page_increment(), page_increment)
1510 self.assertEquals(adjustment.get_page_size(), page_size)
1512 def test_adjustment(self):
1513 adjustment = Gtk.Adjustment(1, 0, 6, 4, 5, 3)
1514 self.adjustment_check(adjustment, 1, 0, 6, 4, 5, 3)
1516 adjustment = Gtk.Adjustment(1, 0, 6, 4, 5)
1517 self.adjustment_check(adjustment, 1, 0, 6, 4, 5)
1519 adjustment = Gtk.Adjustment(1, 0, 6, 4)
1520 self.adjustment_check(adjustment, 1, 0, 6, 4)
1522 adjustment = Gtk.Adjustment(1, 0, 6)
1523 self.adjustment_check(adjustment, 1, 0, 6)
1525 adjustment = Gtk.Adjustment()
1526 self.adjustment_check(adjustment)
1528 adjustment = Gtk.Adjustment(value=1, lower=0, upper=6,
1529 step_increment=4, page_increment=5, page_size=3)
1530 self.adjustment_check(adjustment, 1, 0, 6, 4, 5, 3)
1532 def test_table(self):
1534 self.assertEquals(table.get_size(), (1,1))
1535 self.assertEquals(table.get_homogeneous(), False)
1536 table = Gtk.Table(2, 3)
1537 self.assertEquals(table.get_size(), (2,3))
1538 self.assertEquals(table.get_homogeneous(), False)
1539 table = Gtk.Table(2, 3, True)
1540 self.assertEquals(table.get_size(), (2,3))
1541 self.assertEquals(table.get_homogeneous(), True)
1543 # Test PyGTK interface
1544 table = Gtk.Table(rows=3, columns=2)
1545 self.assertEquals(table.get_size(), (3,2))
1546 # Test using the actual property names
1547 table = Gtk.Table(n_rows=2, n_columns=3, homogeneous=True)
1548 self.assertEquals(table.get_size(), (2,3))
1549 self.assertEquals(table.get_homogeneous(), True)
1551 label = Gtk.Label(label='Hello')
1552 table.attach(label, 0, 1, 0, 1)
1553 self.assertEquals(label, table.get_children()[0])
1555 def test_scrolledwindow(self):
1556 sw = Gtk.ScrolledWindow()
1557 sb = sw.get_hscrollbar()
1558 self.assertEquals(sw.get_hadjustment(), sb.get_adjustment())
1559 sb = sw.get_vscrollbar()
1560 self.assertEquals(sw.get_vadjustment(), sb.get_adjustment())
1562 def test_widget_drag_methods(self):
1563 widget = Gtk.Button()
1565 # here we are not checking functionality, only that the methods exist
1566 # and except the right number of arguments
1568 widget.drag_check_threshold(0, 0, 0, 0)
1570 # drag_dest_ methods
1571 widget.drag_dest_set(Gtk.DestDefaults.DROP, None, Gdk.DragAction.COPY)
1572 widget.drag_dest_add_image_targets()
1573 widget.drag_dest_add_text_targets()
1574 widget.drag_dest_add_uri_targets()
1575 widget.drag_dest_get_track_motion()
1576 widget.drag_dest_set_track_motion(True)
1577 widget.drag_dest_get_target_list()
1578 widget.drag_dest_set_target_list(Gtk.TargetList.new([Gtk.TargetEntry.new('test',0, 0)]))
1579 widget.drag_dest_unset()
1581 widget.drag_highlight()
1582 widget.drag_unhighlight()
1584 # drag_source_ methods
1585 widget.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, None, Gdk.DragAction.MOVE)
1586 widget.drag_source_add_image_targets()
1587 widget.drag_source_add_text_targets()
1588 widget.drag_source_add_uri_targets()
1589 widget.drag_source_set_icon_name("")
1590 widget.drag_source_set_icon_pixbuf(GdkPixbuf.Pixbuf())
1591 widget.drag_source_set_icon_stock("")
1592 widget.drag_source_get_target_list()
1593 widget.drag_source_set_target_list(Gtk.TargetList.new([Gtk.TargetEntry.new('test', 0, 0)]))
1594 widget.drag_source_unset()
1596 # these methods cannot be called because they require a valid drag on
1597 # a real GdkWindow. So we only check that they exist and are callable.
1598 self.assertTrue(hasattr(widget.drag_dest_set_proxy, '__call__'))
1599 self.assertTrue(hasattr(widget.drag_get_data, '__call__'))
1601 class TestGio(unittest.TestCase):
1603 os.environ['GSETTINGS_BACKEND'] = 'memory'
1604 # support a separate build tree, so look in build dir first
1605 os.environ['GSETTINGS_SCHEMA_DIR'] = os.environ.get('TESTS_BUILDDIR',
1606 os.path.dirname(__file__))
1607 self.settings = Gio.Settings('org.gnome.test')
1608 # we change the values in the tests, so set them to predictable start
1610 self.settings.reset('test-string')
1611 self.settings.reset('test-array')
1613 def test_file_enumerator(self):
1614 self.assertEquals(Gio.FileEnumerator, overrides.Gio.FileEnumerator)
1615 f = Gio.file_new_for_path("./")
1618 for info in f.enumerate_children("standard::*", 0, None):
1619 iter_info.append(info.get_name())
1622 enumerator = f.enumerate_children("standard::*", 0, None)
1624 info = enumerator.next_file(None)
1627 next_info.append(info.get_name())
1629 self.assertEquals(iter_info, next_info)
1631 def test_gsettings_native(self):
1632 self.assertTrue('test-array' in self.settings.list_keys())
1635 v = self.settings.get_value('test-boolean')
1636 self.assertEqual(v.get_boolean(), True)
1637 self.assertEqual(self.settings.get_boolean('test-boolean'), True)
1639 v = self.settings.get_value('test-string')
1640 self.assertEqual(v.get_string(), 'Hello')
1641 self.assertEqual(self.settings.get_string('test-string'), 'Hello')
1643 v = self.settings.get_value('test-array')
1644 self.assertEqual(v.unpack(), [1, 2])
1646 v = self.settings.get_value('test-tuple')
1647 self.assertEqual(v.unpack(), (1, 2))
1650 self.settings.set_string('test-string', 'World')
1651 self.assertEqual(self.settings.get_string('test-string'), 'World')
1653 self.settings.set_value('test-string', GLib.Variant('s', 'Goodbye'))
1654 self.assertEqual(self.settings.get_string('test-string'), 'Goodbye')
1656 def test_gsettings_constructor(self):
1657 # default constructor uses path from schema
1658 self.assertEqual(self.settings.get_property('path'), '/tests/')
1660 # optional constructor arguments
1661 with_path = Gio.Settings('org.gnome.nopathtest', path='/mypath/')
1662 self.assertEqual(with_path.get_property('path'), '/mypath/')
1663 self.assertEqual(with_path['np-int'], 42)
1665 def test_gsettings_override(self):
1666 # dictionary interface
1667 self.assertEqual(len(self.settings), 4)
1668 self.assertTrue('test-array' in self.settings)
1669 self.assertTrue('test-array' in self.settings.keys())
1670 self.failIf('nonexisting' in self.settings)
1671 self.failIf(4 in self.settings)
1672 self.assertEqual(bool(self.settings), True)
1675 self.assertEqual(self.settings['test-boolean'], True)
1676 self.assertEqual(self.settings['test-string'], 'Hello')
1677 self.assertEqual(self.settings['test-array'], [1, 2])
1678 self.assertEqual(self.settings['test-tuple'], (1, 2))
1680 self.assertRaises(KeyError, self.settings.__getitem__, 'unknown')
1681 self.assertRaises(KeyError, self.settings.__getitem__, 2)
1684 self.settings['test-string'] = 'Goodbye'
1685 self.assertEqual(self.settings['test-string'], 'Goodbye')
1686 self.settings['test-array'] = [3, 4, 5]
1687 self.assertEqual(self.settings['test-array'], [3, 4, 5])
1689 self.assertRaises(TypeError, self.settings.__setitem__, 'test-string', 1)
1690 self.assertRaises(KeyError, self.settings.__setitem__, 'unknown', 'moo')
1692 def test_gsettings_empty(self):
1693 empty = Gio.Settings('org.gnome.empty', path='/tests/')
1694 self.assertEqual(len(empty), 0)
1695 self.assertEqual(bool(empty), True)
1696 self.assertEqual(empty.keys(), [])