1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
10 sys.path.insert(0, "../")
12 from gi.repository import GObject
13 from gi.repository import Gdk
14 from gi.repository import Gtk
15 import gi.overrides as overrides
17 class TestGdk(unittest.TestCase):
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)
25 class TestGtk(unittest.TestCase):
26 def test_uimanager(self):
27 self.assertEquals(Gtk.UIManager, overrides.Gtk.UIManager)
29 ui.add_ui_from_string(
32 <menubar name="menubar1"></menubar>
36 menubar = ui.get_widget("/menubar1")
37 self.assertEquals(type(menubar), Gtk.MenuBar)
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"
44 def test_action_callback_data(action, user_data):
45 self.assertEquals(user_data, callback_data);
47 def test_radio_action_callback_data(action, current, user_data):
48 self.assertEquals(user_data, callback_data);
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,
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))
73 for action, cmp in zip(action_group.list_actions(), expected_results):
74 a = (action.get_name(), type(action))
75 self.assertEquals(a,cmp)
78 def test_builder(self):
79 self.assertEquals(Gtk.Builder, overrides.Gtk.Builder)
81 class SignalTest(GObject.GObject):
82 __gtype_name__ = "GIOverrideSignalTest"
84 "test-signal": (GObject.SIGNAL_RUN_FIRST,
94 def on_signal_1(self, *args):
97 def on_signal_3(self, *args):
100 signal_checker = SignalCheck()
101 builder = Gtk.Builder()
103 # add object1 to the builder
104 builder.add_from_string(
107 <object class="GIOverrideSignalTest" id="object1">
108 <signal name="test-signal" handler="on_signal_1" />
113 # only add object3 to the builder
114 builder.add_objects_from_string(
117 <object class="GIOverrideSignalTest" id="object2">
118 <signal name="test-signal" handler="on_signal_2" />
120 <object class="GIOverrideSignalTest" id="object3">
121 <signal name="test-signal" handler="on_signal_3" />
123 <object class="GIOverrideSignalTest" id="object4">
124 <signal name="test-signal" handler="on_signal_4" />
132 builder.connect_signals(signal_checker)
134 # call their notify signals and check sentinel
135 objects = builder.get_objects()
136 self.assertEquals(len(objects), 2)
138 obj.emit('test-signal')
140 self.assertEquals(signal_checker.sentinel, 4)
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))
148 dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
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())
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)
164 class TestClass(GObject.GObject):
165 __gtype_name__ = "GIOverrideTreeAPITest"
167 def __init__(self, tester, int_value, string_value):
168 super(TestClass, self).__init__()
170 self.int_value = int_value
171 self.string_value = string_value
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)
178 # FIXME: we should be able to pass strings like 'TestClass'
179 tree_store = Gtk.TreeStore(int, str, TestClass)
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))
186 # walk the tree to see if the values were stored correctly
187 iter = Gtk.TreeIter()
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)
196 iter = Gtk.TreeIter()
198 self.assertEquals(i, 99)
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))
208 # walk the list to see if the values were stored correctly
209 iter = Gtk.TreeIter()
211 has_more = list_store.get_iter_first(iter)
213 i = list_store.get_value(iter, 0)
214 s = list_store.get_value(iter, 1)
215 obj = list_store.get_value(iter, 2)
217 has_more = list_store.iter_next(iter)
219 self.assertEquals(i, 99)
221 # check to see that we can instantiate a TreeViewColumn
222 cell = Gtk.CellRendererText()
223 column = Gtk.TreeViewColumn(title='This is just a test',
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')
233 self.assertEquals(tag.props.name, 'title')
234 self.assertEquals(tag.props.font, 'Sans 18')
236 (start, end) = buffer.get_bounds()
238 buffer.insert(end, 'HelloHello')
239 buffer.insert(end, ' Bob')
241 cursor_iter = end.copy()
242 cursor_iter.backward_chars(9)
243 buffer.place_cursor(cursor_iter)
244 buffer.insert_at_cursor(' Jane ')
246 (start, end) = buffer.get_bounds()
247 text = buffer.get_text(start, end, False)
249 self.assertEquals(text, 'Hello Jane Hello Bob')