1import unittest
2
3from common import gtk, gobject
4
5class MyContainer(gtk.Container):
6    __gtype_name__ = 'MyContainer'
7    def __init__(self):
8        gtk.Container.__init__(self)
9        self.children = []
10        self.props = {}
11
12    def do_add(self, child):
13        child.set_parent(self)
14        self.children.append(child)
15        self.props[child] = '' # we just have one child property
16
17    def do_remove(self, child):
18        widget_was_visible = child.flags() & gtk.VISIBLE
19        child.unparent()
20        self.children.remove(child)
21        del self.props[child]
22
23        if widget_was_visible:
24            self.queue_resize()
25
26    def do_forall(self, internal, callback, data):
27        for child in self.children:
28            callback(child, data)
29
30    def do_set_child_property(self, child, property_id, value, pspec):
31        if pspec.name == 'dumb-prop':
32            self.props[child] = value
33
34    def do_get_child_property(self, child, property_id, pspec):
35        if pspec.name == 'dumb-prop':
36            return self.props[child]
37
38MyContainer.install_child_property(1,
39                                   ('dumb_prop',
40                                    gobject.TYPE_STRING,
41                                    'Dumb Prop',
42                                    'Dumb Property for testing purposes',
43                                    '', gobject.PARAM_READWRITE))
44
45class MyAlignment(gtk.Alignment):
46
47    def __init__(self, caption):
48        self.caption = gtk.Label()
49        self.caption.set_markup('<b>%s</b>' % gobject.markup_escape_text(caption))
50
51        gtk.Alignment.__init__(self)
52
53        self.caption.set_parent(self)
54
55    def do_forall(self, include_internals, callback, callback_data):
56        callback(self.caption, callback_data)
57        gtk.Alignment.do_forall(self, include_internals, callback, callback_data)
58
59# FIXME: Why is it needed?
60gobject.type_register(MyAlignment)
61
62class FocusContainer(gtk.HBox):
63
64    # Just call super.
65    def do_set_focus_child(self, widget):
66        gtk.HBox.do_set_focus_child(self, widget)
67
68gobject.type_register(FocusContainer)
69
70class FocusWindow(gtk.Window):
71
72    # Just call super.
73    def do_set_focus(self, focus):
74        gtk.Window.do_set_focus(self, focus)
75
76gobject.type_register(FocusWindow)
77
78class ContainerTest(unittest.TestCase):
79
80    def testChildProperties(self):
81        obj = MyContainer()
82        label = gtk.Label()
83        obj.add(label)
84        v = 'dumb value'
85        obj.child_set_property(label, 'dumb_prop', v)
86        self.assertEqual(v, obj.child_get_property(label, 'dumb_prop'))
87
88    def testSuperclassForAll(self):
89        alignment = MyAlignment('test')
90        label = gtk.Label('foo')
91        alignment.add(label)
92        alignment.show_all()
93
94        self.assert_(label.flags() & gtk.VISIBLE)
95        self.assert_(alignment.caption.flags() & gtk.VISIBLE)
96
97    def testFocusSignalHandlers(self):
98        button = gtk.Button()
99        container = FocusContainer()
100        window = FocusWindow()
101        container.add(button)
102        window.add(container)
103        window.show_all()
104
105        button.grab_focus()
106        self.assert_(button.is_focus())
107
108        window.set_focus(None)
109        self.assert_(window.get_focus() is None)
110
111        window.set_focus(button)
112        self.assert_(window.get_focus() == button)
113
114        window.destroy()
115
116if __name__ == '__main__':
117    unittest.main()
118