[pitivi] tests: Reuse code for creating a main loop



commit f50117e011273a329eed97dcaed2c9dab73f9f1f
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Sun Apr 3 00:52:06 2016 +0200

    tests: Reuse code for creating a main loop
    
    Differential Revision: https://phabricator.freedesktop.org/D854

 tests/common.py             |   41 ++++++++++++++++++++++++-
 tests/test_mainwindow.py    |    3 +-
 tests/test_media_library.py |    6 +--
 tests/test_project.py       |   71 +++++++++++++++----------------------------
 4 files changed, 68 insertions(+), 53 deletions(-)
---
diff --git a/tests/common.py b/tests/common.py
index 5e9efcb..2bb2619 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -1,14 +1,32 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2015, Thibault Saunier <tsaunier gnome org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301, USA.
 """
 A collection of objects to use for testing
 """
 import gc
 import os
-import sys
 import tempfile
 import unittest
 from unittest import mock
 
 from gi.repository import Gdk
+from gi.repository import GLib
 from gi.repository import Gst
 from gi.repository import Gtk
 
@@ -59,6 +77,27 @@ def getPitiviMock(settings=None, proxyingStrategy=ProxyingStrategy.NOTHING,
     return app
 
 
+def create_main_loop():
+    mainloop = GLib.MainLoop()
+    timed_out = False
+
+    def quit_cb(unused):
+        timed_out = True
+        mainloop.quit()
+
+    def run(timeout_seconds=5):
+        source = GLib.timeout_source_new_seconds(timeout_seconds)
+        source.set_callback(quit_cb)
+        source.attach()
+        GLib.MainLoop.run(mainloop)
+        source.destroy()
+        if timed_out:
+            raise Exception("Timed out after %s seconds" % timeout_seconds)
+
+    mainloop.run = run
+    return mainloop
+
+
 class TestCase(unittest.TestCase, Loggable):
     _tracked_types = (Gst.MiniObject, Gst.Element, Gst.Pad, Gst.Caps)
 
diff --git a/tests/test_mainwindow.py b/tests/test_mainwindow.py
index 030bf1b..0ed1c6e 100644
--- a/tests/test_mainwindow.py
+++ b/tests/test_mainwindow.py
@@ -23,7 +23,6 @@ import os
 from unittest import mock
 
 from gi.repository import GES
-from gi.repository import GLib
 from gi.repository import Gtk
 
 from pitivi.mainwindow import PitiviMainWindow
@@ -59,7 +58,7 @@ class TestMainWindow(common.TestCase):
         mainwindow.destroy()
 
     def __loading_failure(self, has_proxy):
-        mainloop = GLib.MainLoop.new(None, False)
+        mainloop = common.create_main_loop()
 
         app = common.getPitiviMock(
             settings={"lastProjectFolder": "/tmp",
diff --git a/tests/test_media_library.py b/tests/test_media_library.py
index 16d50c2..dd30eaa 100644
--- a/tests/test_media_library.py
+++ b/tests/test_media_library.py
@@ -17,12 +17,10 @@
 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 # Boston, MA 02110-1301, USA.
 import os
-import tempfile
 from gettext import gettext as _
 from unittest import mock
 
 from gi.repository import GES
-from gi.repository import GLib
 from gi.repository import Gst
 
 from pitivi import medialibrary
@@ -62,7 +60,7 @@ class TestMediaLibrary(common.TestCase):
         # is connected to some assets.
         self.clean()
 
-        self.mainloop = GLib.MainLoop.new(None, False)
+        self.mainloop = common.create_main_loop()
         self.check_no_transcoding = False
         self.app = common.getPitiviMock(settings)
         self.app.project_manager = ProjectManager(self.app)
@@ -80,7 +78,7 @@ class TestMediaLibrary(common.TestCase):
     def projectLoadedCb(self, unused_project, unused_timeline):
         self.mainloop.quit()
 
-    def _progressBarCb(self, progressbar, unused_pspecunused):
+    def _progressBarCb(self, progressbar, unused_pspec):
         if self.check_no_transcoding:
             self.assertTrue(progressbar.props.fraction == 1.0 or
                             progressbar.props.fraction == 0.0,
diff --git a/tests/test_project.py b/tests/test_project.py
index 1fa2aff..83e8de4 100644
--- a/tests/test_project.py
+++ b/tests/test_project.py
@@ -27,7 +27,6 @@ from gi.repository import GES
 from gi.repository import GLib
 from gi.repository import Gst
 
-from pitivi.application import Pitivi
 from pitivi.project import Project
 from pitivi.project import ProjectManager
 from pitivi.utils.misc import uri_is_reachable
@@ -146,18 +145,15 @@ class TestProjectManager(TestCase):
         self.assertEqual("new-project-loaded", name, self.signals)
 
     def testMissingUriForwarded(self):
-        def quit(mainloop):
-            mainloop.quit()
+        mainloop = common.create_main_loop()
 
-        def missingUriCb(self, project, error, clip_asset, mainloop, result):
+        def missingUriCb(self, project, error, clip_asset, result):
             result[0] = True
             mainloop.quit()
 
-        self.mainloop = GLib.MainLoop()
-
         result = [False]
         self.manager.connect(
-            "missing-uri", missingUriCb, self.mainloop, result)
+            "missing-uri", missingUriCb, result)
 
         # Load a project with a missing asset.
         unused, xges_path = tempfile.mkstemp()
@@ -179,8 +175,7 @@ class TestProjectManager(TestCase):
         try:
             self.assertTrue(self.manager.loadProject(uri))
 
-            GLib.timeout_add_seconds(5, quit, self.mainloop)
-            self.mainloop.run()
+            mainloop.run()
             self.assertTrue(result[0], "missing not missing")
         finally:
             os.remove(xges_path)
@@ -318,28 +313,23 @@ class TestProjectManager(TestCase):
 
 class TestProjectLoading(common.TestCase):
 
-    def setUp(self):
-        self.mainloop = GLib.MainLoop()
-
     def tearDown(self):
         pass
 
     def testLoadedCallback(self):
-        def loaded(project, timeline, mainloop, result):
-            result[0] = True
-            mainloop.quit()
+        mainloop = common.create_main_loop()
 
-        def quit(mainloop):
+        def loaded(project, timeline, result):
+            result[0] = True
             mainloop.quit()
 
         # Create a blank project and save it.
         project = _createRealProject(name="noname")
         result = [False]
-        project.connect("loaded", loaded, self.mainloop, result)
+        project.connect("loaded", loaded, result)
 
         self.assertTrue(project.createTimeline())
-        GLib.timeout_add_seconds(5, quit, self.mainloop)
-        self.mainloop.run()
+        mainloop.run()
         self.assertTrue(
             result[0], "Blank project creation failed to trigger signal: loaded")
 
@@ -352,19 +342,20 @@ class TestProjectLoading(common.TestCase):
             project2 = _createRealProject()
             self.assertTrue(project2.createTimeline())
             result = [False]
-            project2.connect("loaded", loaded, self.mainloop, result)
-            GLib.timeout_add_seconds(5, quit, self.mainloop)
-            self.mainloop.run()
+            project2.connect("loaded", loaded, result)
+            mainloop.run()
             self.assertTrue(
                 result[0], "Blank project loading failed to trigger signal: loaded")
         finally:
             os.remove(xges_path)
 
     def testAssetAddingRemovingAdding(self):
+        mainloop = common.create_main_loop()
+
         def loadingProgressCb(project, progress, estimated_time,
                               self, result, uris):
 
-            def readd(mainloop, result, uris):
+            def readd(result, uris):
                 project.addUris(uris)
                 result[2] = True
                 mainloop.quit()
@@ -377,9 +368,9 @@ class TestProjectLoading(common.TestCase):
             self.assertEqual(len(assets), 1)
             asset = assets[0]
             project.remove_asset(asset)
-            GLib.idle_add(readd, self.mainloop, result, uris)
+            GLib.idle_add(readd, result, uris)
 
-        def loadedCb(project, timeline, mainloop, result, uris):
+        def loadedCb(project, timeline, result, uris):
             result[0] = True
             project.addUris(uris)
 
@@ -387,17 +378,13 @@ class TestProjectLoading(common.TestCase):
         project = _createRealProject()
         result = [False, False, False]
         uris = [common.getSampleUri("tears_of_steel.webm")]
-        project.connect("loaded", loadedCb, self.mainloop, result, uris)
+        project.connect("loaded", loadedCb, result, uris)
         project.connect("asset-loading-progress",
                         loadingProgressCb, self,
                         result, uris)
 
-        def quit(mainloop):
-            mainloop.quit()
-
         self.assertTrue(project.createTimeline())
-        GLib.timeout_add_seconds(5, quit, self.mainloop)
-        self.mainloop.run()
+        mainloop.run()
         self.assertTrue(
             result[0], "Project creation failed to trigger signal: loaded")
         self.assertTrue(
@@ -408,12 +395,6 @@ class TestProjectLoading(common.TestCase):
 
 class TestProjectSettings(common.TestCase):
 
-    def setUp(self):
-        self.mainloop = GLib.MainLoop()
-
-    def tearDown(self):
-        pass
-
     def testAudio(self):
         project = _createRealProject(name="noname")
         project.audiochannels = 2
@@ -433,16 +414,15 @@ class TestProjectSettings(common.TestCase):
         self.assertEqual(Gst.Fraction(2, 7), project.videopar)
 
     def testInitialization(self):
-        def loadedCb(project, timeline, mainloop, uris):
+        mainloop = common.create_main_loop()
+
+        def loadedCb(project, timeline, uris):
             project.addUris(uris)
 
-        def progressCb(project, progress, estimated_time, mainloop):
+        def progressCb(project, progress, estimated_time):
             if progress == 100:
                 mainloop.quit()
 
-        def quit(mainloop):
-            mainloop.quit()
-
         # Create a blank project and add some assets.
         project = _createRealProject()
         self.assertTrue(project._has_default_video_settings)
@@ -451,12 +431,11 @@ class TestProjectSettings(common.TestCase):
         uris = [common.getSampleUri("flat_colour1_640x480.png"),
                 common.getSampleUri("tears_of_steel.webm"),
                 common.getSampleUri("1sec_simpsons_trailer.mp4")]
-        project.connect("loaded", loadedCb, self.mainloop, uris)
-        project.connect("asset-loading-progress", progressCb, self.mainloop)
+        project.connect("loaded", loadedCb, uris)
+        project.connect("asset-loading-progress", progressCb)
 
         self.assertTrue(project.createTimeline())
-        GLib.timeout_add_seconds(5, quit, self.mainloop)
-        self.mainloop.run()
+        mainloop.run()
 
         assets = project.list_assets(GES.UriClip)
         self.assertEqual(3, len(assets), assets)


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