summaryrefslogtreecommitdiff
path: root/tests/test_actiongroup.py
blob: a4fb97d8b8b8f00648a1bec0673baa47d1562f74 (plain)
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
# Test for Bug #162874

import gc
import unittest

from common import gtk, gobject


class ActionGroupTest(unittest.TestCase):
    ui0 = '''<ui>
    <menubar name="MenuBar">
      <menu action="File">
        <menuitem action="Quit"/>
      </menu>
      <menu action="Sound">
        <menuitem action="Mute"/>
      </menu>
      <menu action="RadioBand">
        <menuitem action="AM"/>
        <menuitem action="FM"/>
        <menuitem action="SSB"/>
      </menu>
    </menubar>
    <toolbar name="Toolbar">
      <toolitem action="Quit"/>
      <separator/>
      <toolitem action="Mute"/>
      <separator name="sep1"/>
      <placeholder name="RadioBandItems">
        <toolitem action="AM"/>
        <toolitem action="FM"/>
        <toolitem action="SSB"/>
      </placeholder>
    </toolbar>
    </ui>'''

    def build_gui(self):
        # Create the toplevel window
        window = gtk.Window()
        vbox = gtk.VBox()
        window.add(vbox)

        # Create a UIManager instance
        uimanager = gtk.UIManager()
        self.uimanager = uimanager

        # Create the base ActionGroup
        actiongroup = gtk.ActionGroup('UIMergeExampleBase')

        actiongroup.add_actions([('File', None, '_File'),
                                  ('Sound', None, '_Sound'),
                                  ('RadioBand', None, '_Radio Band')])
        uimanager.insert_action_group(actiongroup, 0)

        # Create an ActionGroup
        actiongroup0 = gtk.ActionGroup('UIMergeExample0')

        # Create a ToggleAction, etc.
        actiongroup0.add_toggle_actions([('Mute', None, '_Mute', '<Control>m',
                                          'Mute the volume', self.cb)])

        # Create actions
        actiongroup0.add_actions([('Quit', gtk.STOCK_QUIT, '_Quit me!', None,
                                   'Quit the Program', self.cb)])

        # Create some RadioActions
        actiongroup0.add_radio_actions([('AM', None, '_AM', '<Control>a',
                                         'AM Radio', 0),
                                        ('FM', None, '_FM', '<Control>f',
                                         'FM Radio', 1),
                                        ('SSB', None, '_SSB', '<Control>b',
                                         'SSB Radio', 2),
                                        ], 0, self.cb)

        # Add the actiongroup to the uimanager
        uimanager.insert_action_group(actiongroup0, 1)

        self.ag0 = actiongroup0
        del actiongroup0

        # Add a UI description
        self.merge_id0 = uimanager.add_ui_from_string(self.ui0)

        # Create a MenuBar
        menubar = uimanager.get_widget('/MenuBar')
        vbox.pack_start(menubar, False)

        # Create a Toolbar
        toolbar = uimanager.get_widget('/Toolbar')
        vbox.pack_start(toolbar, False)

        gobject.timeout_add(50, self.timeout_cb)

        gtk.main()
        return

    def timeout_cb(self):
        ag0 = self.ag0
        uimanager = self.uimanager
        del self.ag0, self.uimanager

        uimanager.remove_ui(self.merge_id0)
        uimanager.remove_action_group(ag0)

        gc.collect()             # Clean out unreachable objects

        del ag0
        self.assertEqual(gc.collect(), 1) # Collect just the ActionGroup

        uimanager.ensure_update()
        self.assertEqual(gc.collect(), 6) # Now the GtkActions have lost their last
                                 # GObject reference; they should be collected.
                                 # We have a ToggleAction, an Action and a
                                 # RadioAction, plus self.cb is bound in three
                                 # closures.

        gtk.main_quit()

    def setUp(self):
        gc.collect()

    def testActionGroups(self):
        self.build_gui()

    def cb(self, action):
        return

if __name__ == '__main__':
    unittest.main()