[pygi] fix up Builder override, add new override methods, and add unit tests



commit 9f34d120845d936b04546a5cea599ec67e9181a7
Author: John (J5) Palmieri <johnp redhat com>
Date:   Mon May 24 16:16:50 2010 -0400

    fix up Builder override, add new override methods, and add unit tests
    
    * check for flags when connecting signals now that we get gi GObject types
    * override the add_from_string and add_objects_from string overrides so
      that you don't have to pass in the length of the buffer
    * add test that loads objects from strings and connects them to signals

 gi/overrides/Gtk.py     |   19 +++++++++++-
 tests/test_overrides.py |   72 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 86 insertions(+), 5 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index ebbbfc4..38b0dfa 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -207,8 +207,7 @@ class Builder(Gtk.Builder):
             if not callable(handler):
                 raise TypeError('Handler %s is not a method or function' % handler_name)
 
-            # TODO: we need to support bitfields
-            after = False #flags and GObject.ConnectFlags.AFTER
+            after = flags or GObject.ConnectFlags.AFTER
             if connect_obj is not None:
                 if after:
                     gobj.connect_object_after(signal_name, handler, connect_obj)
@@ -223,6 +222,22 @@ class Builder(Gtk.Builder):
         self.connect_signals_full(_full_callback,
                                   obj_or_map);
 
+    def add_from_string(self, buffer):
+        if not isinstance(buffer, basestring):
+            raise TypeError('buffer must be a string')
+
+        length = len(buffer)
+
+        return Gtk.Builder.add_from_string(self, buffer, length)
+
+    def add_objects_from_string(self, buffer, object_ids):
+        if not isinstance(buffer, basestring):
+            raise TypeError('buffer must be a string')
+
+        length = len(buffer)
+
+        return Gtk.Builder.add_objects_from_string(self, buffer, length, object_ids)
+
 Builder = override(Builder)
 
 __all__ = ['ActionGroup', 'Builder', 'UIManager']
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 7f70311..f13ba94 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -10,6 +10,7 @@ import gobject
 import sys
 sys.path.insert(0, "../")
 
+from gi.repository import GObject
 from gi.repository import Gdk
 from gi.repository import Gtk
 import gi.overrides as overrides
@@ -34,8 +35,8 @@ class TestGtk(unittest.TestCase):
 """
 )
         menubar = ui.get_widget("/menubar1")
-        self.assertEquals(type(menubar), Gtk.MenuBar) 
-        
+        self.assertEquals(type(menubar), Gtk.MenuBar)
+
     def test_actiongroup(self):
         self.assertEquals(Gtk.ActionGroup, overrides.Gtk.ActionGroup)
         action_group = Gtk.ActionGroup (name = 'TestActionGroup')
@@ -53,7 +54,7 @@ class TestGtk(unittest.TestCase):
         action_group.add_radio_actions([
             ('test-radio-action1', None, 'Test Radio Action 1'),
             ('test-radio-action2', Gtk.STOCK_COPY, 'Test Radio Action 2')], 1, None)
-        
+
         expected_results = (('test-action1', Gtk.Action),
                             ('test-action2', Gtk.Action),
                             ('test-toggle-action1', Gtk.ToggleAction),
@@ -65,3 +66,68 @@ class TestGtk(unittest.TestCase):
             a = (action.get_name(), type(action))
             self.assertEquals(a,cmp)
 
+    def test_builder(self):
+        self.assertEquals(Gtk.Builder, overrides.Gtk.Builder)
+
+        class SignalTest(GObject.GObject):
+            __gtype_name__ = "GIOverrideSignalTest"
+            __gsignals__ = {
+                "test-signal": (gobject.SIGNAL_RUN_FIRST,
+                                gobject.TYPE_NONE,
+                                []),
+            }
+
+
+        class SignalCheck:
+            def __init__(self):
+                self.sentinel = 0
+
+            def on_signal_1(self, *args):
+                self.sentinel += 1
+
+            def on_signal_3(self, *args):
+                self.sentinel += 3
+
+        signal_checker = SignalCheck()
+        builder = Gtk.Builder()
+
+        # add object1 to the builder
+        builder.add_from_string(
+"""
+<interface>
+  <object class="GIOverrideSignalTest" id="object1">
+      <signal name="test-signal" handler="on_signal_1" />
+  </object>
+</interface>
+""")
+
+        # only add object3 to the builder
+        builder.add_objects_from_string(
+"""
+<interface>
+  <object class="GIOverrideSignalTest" id="object2">
+      <signal name="test-signal" handler="on_signal_2" />
+  </object>
+  <object class="GIOverrideSignalTest" id="object3">
+      <signal name="test-signal" handler="on_signal_3" />
+  </object>
+  <object class="GIOverrideSignalTest" id="object4">
+      <signal name="test-signal" handler="on_signal_4" />
+  </object>
+</interface>
+
+""",
+            ['object3'])
+
+        # hook up signals
+        builder.connect_signals(signal_checker)
+
+        # call their notify signals and check sentinel
+        objects = builder.get_objects()
+        self.assertEquals(len(objects), 2)
+        for obj in objects:
+            obj.emit('test-signal')
+
+        self.assertEquals(signal_checker.sentinel, 4)
+
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]