Imported Upstream version 2.21.4
[platform/upstream/pygobject2.git] / tests / test_overrides.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
3
4 import pygtk
5 pygtk.require("2.0")
6
7 import unittest
8
9 import sys
10 sys.path.insert(0, "../")
11
12 from gi.repository import GObject
13 from gi.repository import Gdk
14 from gi.repository import Gtk
15 import gi.overrides as overrides
16
17 class TestGdk(unittest.TestCase):
18
19     def test_color(self):
20         color = Gdk.Color(100, 200, 300)
21         self.assertEquals(color.red, 100)
22         self.assertEquals(color.green, 200)
23         self.assertEquals(color.blue, 300)
24
25 class TestGtk(unittest.TestCase):
26     def test_uimanager(self):
27         self.assertEquals(Gtk.UIManager, overrides.Gtk.UIManager)
28         ui = Gtk.UIManager()
29         ui.add_ui_from_string(
30 """
31 <ui>
32     <menubar name="menubar1"></menubar>
33 </ui>
34 """
35 )
36         menubar = ui.get_widget("/menubar1")
37         self.assertEquals(type(menubar), Gtk.MenuBar)
38
39     def test_actiongroup(self):
40         self.assertEquals(Gtk.ActionGroup, overrides.Gtk.ActionGroup)
41         action_group = Gtk.ActionGroup (name = 'TestActionGroup')
42         callback_data = "callback data"
43
44         def test_action_callback_data(action, user_data):
45             self.assertEquals(user_data, callback_data);
46
47         def test_radio_action_callback_data(action, current, user_data):
48             self.assertEquals(user_data, callback_data);
49
50         action_group.add_actions ([
51             ('test-action1', None, 'Test Action 1',
52              None, None, test_action_callback_data),
53             ('test-action2', Gtk.STOCK_COPY, 'Test Action 2',
54               None, None, test_action_callback_data)], callback_data)
55         action_group.add_toggle_actions([
56             ('test-toggle-action1', None, 'Test Toggle Action 1',
57              None, None, test_action_callback_data, False),
58             ('test-toggle-action2', Gtk.STOCK_COPY, 'Test Toggle Action 2',
59               None, None, test_action_callback_data, True)], callback_data)
60         action_group.add_radio_actions([
61             ('test-radio-action1', None, 'Test Radio Action 1'),
62             ('test-radio-action2', Gtk.STOCK_COPY, 'Test Radio Action 2')], 1,
63             test_radio_action_callback_data,
64             callback_data)
65
66         expected_results = (('test-action1', Gtk.Action),
67                             ('test-action2', Gtk.Action),
68                             ('test-toggle-action1', Gtk.ToggleAction),
69                             ('test-toggle-action2', Gtk.ToggleAction),
70                             ('test-radio-action1', Gtk.RadioAction),
71                             ('test-radio-action2', Gtk.RadioAction))
72
73         for action, cmp in zip(action_group.list_actions(), expected_results):
74             a = (action.get_name(), type(action))
75             self.assertEquals(a,cmp)
76             action.activate()
77
78     def test_builder(self):
79         self.assertEquals(Gtk.Builder, overrides.Gtk.Builder)
80
81         class SignalTest(GObject.GObject):
82             __gtype_name__ = "GIOverrideSignalTest"
83             __gsignals__ = {
84                 "test-signal": (GObject.SIGNAL_RUN_FIRST,
85                                 GObject.TYPE_NONE,
86                                 []),
87             }
88
89
90         class SignalCheck:
91             def __init__(self):
92                 self.sentinel = 0
93
94             def on_signal_1(self, *args):
95                 self.sentinel += 1
96
97             def on_signal_3(self, *args):
98                 self.sentinel += 3
99
100         signal_checker = SignalCheck()
101         builder = Gtk.Builder()
102
103         # add object1 to the builder
104         builder.add_from_string(
105 """
106 <interface>
107   <object class="GIOverrideSignalTest" id="object1">
108       <signal name="test-signal" handler="on_signal_1" />
109   </object>
110 </interface>
111 """)
112
113         # only add object3 to the builder
114         builder.add_objects_from_string(
115 """
116 <interface>
117   <object class="GIOverrideSignalTest" id="object2">
118       <signal name="test-signal" handler="on_signal_2" />
119   </object>
120   <object class="GIOverrideSignalTest" id="object3">
121       <signal name="test-signal" handler="on_signal_3" />
122   </object>
123   <object class="GIOverrideSignalTest" id="object4">
124       <signal name="test-signal" handler="on_signal_4" />
125   </object>
126 </interface>
127
128 """,
129             ['object3'])
130
131         # hook up signals
132         builder.connect_signals(signal_checker)
133
134         # call their notify signals and check sentinel
135         objects = builder.get_objects()
136         self.assertEquals(len(objects), 2)
137         for obj in objects:
138             obj.emit('test-signal')
139
140         self.assertEquals(signal_checker.sentinel, 4)
141
142     def test_dialog(self):
143         self.assertEquals(Gtk.Dialog, overrides.Gtk.Dialog)
144         dialog = Gtk.Dialog (title='Foo',
145                              flags=Gtk.DialogFlags.MODAL,
146                              buttons=('test-button1', 1))
147
148         dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
149
150         self.assertEquals('Foo', dialog.get_title())
151         self.assertTrue(dialog.get_modal())
152         button = dialog.get_widget_for_response (1)
153         self.assertEquals('test-button1', button.get_label())
154         button = dialog.get_widget_for_response (2)
155         self.assertEquals('test-button2', button.get_label())
156         button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
157         self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
158
159     def test_tree_api(self):
160         self.assertEquals(Gtk.TreeStore, overrides.Gtk.TreeStore)
161         self.assertEquals(Gtk.ListStore, overrides.Gtk.ListStore)
162         self.assertEquals(Gtk.TreeViewColumn, overrides.Gtk.TreeViewColumn)
163
164         class TestClass(GObject.GObject):
165             __gtype_name__ = "GIOverrideTreeAPITest"
166
167             def __init__(self, tester, int_value, string_value):
168                 super(TestClass, self).__init__()
169                 self.tester = tester
170                 self.int_value = int_value
171                 self.string_value = string_value
172
173             def check(self, int_value, string_value):
174                 self.tester.assertEquals(int_value, self.int_value)
175                 self.tester.assertEquals(string_value, self.string_value)
176
177         # check TreeStore
178         # FIXME: we should be able to pass strings like 'TestClass'
179         tree_store = Gtk.TreeStore(int, str, TestClass)
180         parent = None
181         for i in xrange(100):
182             label = 'this is child #%d' % i
183             testobj = TestClass(self, i, label)
184             parent = tree_store.append(parent, (i, label, testobj))
185
186         # walk the tree to see if the values were stored correctly
187         iter = Gtk.TreeIter()
188         parent = None
189         i = 0
190         while tree_store.iter_children(iter, parent):
191            i = tree_store.get_value(iter, 0)
192            s = tree_store.get_value(iter, 1)
193            obj = tree_store.get_value(iter, 2)
194            obj.check(i, s)
195            parent = iter
196            iter = Gtk.TreeIter()
197
198         self.assertEquals(i, 99)
199
200         # check ListStore
201         # FIXME: we should be able to pass strings like 'TestClass'
202         list_store = Gtk.ListStore(int, str, TestClass)
203         for i in xrange(100):
204             label = 'this is row #%d' % i
205             testobj = TestClass(self, i, label)
206             parent = list_store.append((i, label, testobj))
207
208         # walk the list to see if the values were stored correctly
209         iter = Gtk.TreeIter()
210         i = 0
211         has_more = list_store.get_iter_first(iter)
212         while has_more:
213            i = list_store.get_value(iter, 0)
214            s = list_store.get_value(iter, 1)
215            obj = list_store.get_value(iter, 2)
216            obj.check(i, s)
217            has_more = list_store.iter_next(iter)
218
219         self.assertEquals(i, 99)
220
221         # check to see that we can instantiate a TreeViewColumn
222         cell = Gtk.CellRendererText()
223         column = Gtk.TreeViewColumn(title='This is just a test',
224                                     cell_renderer=cell,
225                                     text=0,
226                                     style=2)
227
228     def test_text_buffer(self):
229         self.assertEquals(Gtk.TextBuffer, overrides.Gtk.TextBuffer)
230         buffer = Gtk.TextBuffer()
231         tag = buffer.create_tag ('title', font = 'Sans 18')
232
233         self.assertEquals(tag.props.name, 'title')
234         self.assertEquals(tag.props.font, 'Sans 18')
235
236         (start, end) = buffer.get_bounds()
237
238         buffer.insert(end, 'HelloHello')
239         buffer.insert(end, ' Bob')
240
241         cursor_iter = end.copy()
242         cursor_iter.backward_chars(9)
243         buffer.place_cursor(cursor_iter)
244         buffer.insert_at_cursor(' Jane ')
245
246         (start, end) = buffer.get_bounds()
247         text = buffer.get_text(start, end, False)
248
249         self.assertEquals(text, 'Hello Jane Hello Bob')