[pygobject] Add some MainLoop, MainContext, and Source test cases



commit 3dba328010a4ffd9259700ffec95871c7341d491
Author: Martin Pitt <martinpitt gnome org>
Date:   Tue Oct 23 11:59:08 2012 +0200

    Add some MainLoop, MainContext, and Source test cases
    
    These cover the remaining static API and behaviour, so that we have good
    regression tests for converting them to GI.
    
    See https://bugzilla.gnome.org/show_bug.cgi?id=686443

 tests/test_glib.py     |   21 +++++++++++++++++++++
 tests/test_gobject.py  |   11 +++++++++++
 tests/test_mainloop.py |   16 ++++++++++++++++
 tests/test_source.py   |   44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 92 insertions(+), 0 deletions(-)
---
diff --git a/tests/test_glib.py b/tests/test_glib.py
index c073b02..4058fae 100644
--- a/tests/test_glib.py
+++ b/tests/test_glib.py
@@ -64,3 +64,24 @@ https://my.org/q?x=1&y=2
         tm = GLib.get_current_time()
         self.assertTrue(isinstance(tm, float))
         self.assertGreater(tm, 1350000000.0)
+
+    def test_main_loop(self):
+        # note we do not test run() here, as we use this in countless other
+        # tests
+        ml = GLib.MainLoop()
+        self.assertFalse(ml.is_running())
+
+        context = ml.get_context()
+        self.assertFalse(context.pending())
+        self.assertFalse(context.iteration(False))
+
+    def test_main_context(self):
+        # constructor
+        context = GLib.MainContext()
+        self.assertFalse(context.pending())
+        self.assertFalse(context.iteration(False))
+
+        # backwards compatible API
+        context = GLib.main_context_default()
+        self.assertFalse(context.pending())
+        self.assertFalse(context.iteration(False))
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index d67365f..de4fc7a 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -19,6 +19,17 @@ class TestGObjectAPI(unittest.TestCase):
         # GObject formerly exposed a lot of GLib's functions
         self.assertEqual(GObject.markup_escape_text('foo'), 'foo')
 
+    def testMainContextAPI(self):
+        # backwards compatible alias API
+        ml = GObject.MainLoop()
+        self.assertFalse(ml.is_running())
+
+        context = GObject.main_context_default()
+        self.assertTrue(context.pending() in [False, True])
+
+        context = GObject.MainContext()
+        self.assertFalse(context.pending())
+
 
 class TestReferenceCounting(unittest.TestCase):
     def testRegularObject(self):
diff --git a/tests/test_mainloop.py b/tests/test_mainloop.py
index 408a123..3c75342 100644
--- a/tests/test_mainloop.py
+++ b/tests/test_mainloop.py
@@ -79,3 +79,19 @@ class TestMainLoop(unittest.TestCase):
             loop2.quit()
         finally:
             signal.signal(signal.SIGUSR1, orig_handler)
+
+    def test_sigint(self):
+        pid = os.fork()
+        if pid == 0:
+            time.sleep(0.5)
+            os.kill(os.getppid(), signal.SIGINT)
+            os._exit(0)
+
+        loop = GLib.MainLoop()
+        try:
+            loop.run()
+            self.fail('expected KeyboardInterrupt exception')
+        except KeyboardInterrupt:
+            pass
+        self.assertFalse(loop.is_running())
+        os.waitpid(pid, 0)
diff --git a/tests/test_source.py b/tests/test_source.py
index 4c8d24b..98bb0ce 100644
--- a/tests/test_source.py
+++ b/tests/test_source.py
@@ -49,13 +49,17 @@ class TestSource(unittest.TestCase):
         self.setup_timeout(loop)
 
         idle = Idle(loop)
+        self.assertEqual(idle.get_context(), None)
         idle.attach()
+        self.assertEqual(idle.get_context(), GLib.main_context_default())
 
         self.pos = 0
 
         m = MySource()
+        self.assertEqual(m.get_context(), None)
         m.set_callback(self.my_callback, loop)
         m.attach()
+        self.assertEqual(m.get_context(), GLib.main_context_default())
 
         loop.run()
 
@@ -130,6 +134,46 @@ class TestSource(unittest.TestCase):
         self.assertEqual(GLib.source_remove(GObject.G_MAXINT32 + 1), False)
         self.assertEqual(GLib.source_remove(GObject.G_MAXUINT32), False)
 
+    def testRecurseProperty(self):
+        s = GLib.Idle()
+        self.assertTrue(s.can_recurse in [False, True])
+        s.can_recurse = False
+        self.assertFalse(s.can_recurse)
+
+    def testPriority(self):
+        s = GLib.Idle()
+        self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT_IDLE)
+        s.priority = GLib.PRIORITY_HIGH
+        self.assertEqual(s.priority, GLib.PRIORITY_HIGH)
+
+        s = GLib.Idle(GLib.PRIORITY_LOW)
+        self.assertEqual(s.priority, GLib.PRIORITY_LOW)
+
+        s = GLib.Timeout(1, GLib.PRIORITY_LOW)
+        self.assertEqual(s.priority, GLib.PRIORITY_LOW)
+
+        s = GLib.Source()
+        self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT)
+
+    def testGetCurrentTime(self):
+        s = GLib.Idle()
+        time = s.get_current_time()
+        self.assertTrue(isinstance(time, float))
+        # plausibility check, and check magnitude of result
+        self.assertGreater(time, 1300000000.0)
+        self.assertLess(time, 2000000000.0)
+
+    # currently broken with Python 3,
+    # https://bugzilla.gnome.org/show_bug.cgi?id=686443
+    @unittest.expectedFailure
+    def testAddRemovePoll(self):
+        # FIXME: very shallow test, only verifies the API signature
+        pollfd = GLib.PollFD(99, GLib.IO_IN | GLib.IO_HUP)
+        self.assertEqual(pollfd.fd, 99)
+        source = GLib.Source()
+        source.add_poll(pollfd)
+        source.remove_poll(pollfd)
+
 
 class TestTimeout(unittest.TestCase):
     def test504337(self):



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