[pitivi] tests: Add test for PreviewWidget



commit 93be716822951676be4cfdf47c2ec808b721847d
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Wed Mar 6 12:16:29 2019 +0100

    tests: Add test for PreviewWidget

 pitivi/dialogs/missingasset.py    |  8 ++--
 pitivi/mediafilespreviewer.py     |  6 +--
 tests/common.py                   | 34 +++++++++++++++
 tests/test_media_library.py       |  3 --
 tests/test_mediafilespreviewer.py | 90 +++++++++++++++++++++++++++++++++++++++
 tests/test_project.py             | 10 -----
 6 files changed, 131 insertions(+), 20 deletions(-)
---
diff --git a/pitivi/dialogs/missingasset.py b/pitivi/dialogs/missingasset.py
index 93bed0cd..ead75bf5 100644
--- a/pitivi/dialogs/missingasset.py
+++ b/pitivi/dialogs/missingasset.py
@@ -90,9 +90,9 @@ class MissingAssetDialog(Gtk.Dialog, Loggable):
         self.get_content_area().pack_start(vbox, False, False, 0)
         vbox.show_all()
 
-        self.__chooser = self.__setup_file_chooser(uri, app.settings)
-        self.get_content_area().pack_start(self.__chooser, True, True, 0)
-        self.__chooser.show()
+        self._chooser = self.__setup_file_chooser(uri, app.settings)
+        self.get_content_area().pack_start(self._chooser, True, True, 0)
+        self._chooser.show()
 
         # If the window is too big, the window manager will resize it so that
         # it fits on the screen.
@@ -128,4 +128,4 @@ class MissingAssetDialog(Gtk.Dialog, Loggable):
         response = self.run()
         if response == Gtk.ResponseType.OK:
             self.log("User chose a new URI for the missing file")
-        return self.__chooser.get_uri()
+        return self._chooser.get_uri()
diff --git a/pitivi/mediafilespreviewer.py b/pitivi/mediafilespreviewer.py
index 6c05e94d..da463860 100644
--- a/pitivi/mediafilespreviewer.py
+++ b/pitivi/mediafilespreviewer.py
@@ -191,10 +191,10 @@ class PreviewWidget(Gtk.Grid, Loggable):
         self.clear_preview()
         self.current_selected_uri = uri
 
-        if not self._discover_sync:
-            GES.UriClipAsset.new(uri, None, self.__asset_loaded_cb)
-        else:
+        if self._discover_sync:
             self._handle_new_asset(uri=uri)
+        else:
+            GES.UriClipAsset.new(uri, None, self.__asset_loaded_cb)
 
     def _handle_new_asset(self, async_result=None, uri=None):
         try:
diff --git a/tests/common.py b/tests/common.py
index 322ac53b..a1b58cce 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -24,6 +24,7 @@ import gc
 import locale
 import os
 import shutil
+import signal
 import sys
 import tempfile
 import traceback
@@ -158,6 +159,29 @@ def create_main_loop():
     return mainloop
 
 
+class OperationTimeout(Exception):
+    pass
+
+
+class checked_operation_duration:
+
+    def __init__(self, seconds, error_message=None):
+        if error_message is None:
+            error_message = "operation timed out after %s seconds" % seconds
+        self.seconds = seconds
+        self.error_message = error_message
+
+    def __handle_sigalrm(self, signum, frame):
+        raise OperationTimeout(self.error_message)
+
+    def __enter__(self):
+        signal.signal(signal.SIGALRM, self.__handle_sigalrm)
+        signal.alarm(self.seconds)
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        signal.alarm(0)
+
+
 class TestCase(unittest.TestCase, Loggable):
     _tracked_types = (Gst.MiniObject, Gst.Element, Gst.Pad, Gst.Caps)
 
@@ -238,6 +262,16 @@ class TestCase(unittest.TestCase, Loggable):
         self._result = result
         unittest.TestCase.run(self, result)
 
+    def create_project_file_from_xges(self, app, xges):
+        unused, xges_path = tempfile.mkstemp(suffix=".xges")
+        proj_uri = Gst.filename_to_uri(os.path.abspath(xges_path))
+        app.project_manager.saveProject(uri=proj_uri)
+
+        with open(xges_path, "w") as f:
+            f.write(xges)
+
+        return proj_uri
+
     def toggle_clip_selection(self, ges_clip, expect_selected):
         """Toggles the selection state of @ges_clip."""
         selected = bool(ges_clip.ui.get_state_flags() & Gtk.StateFlags.SELECTED)
diff --git a/tests/test_media_library.py b/tests/test_media_library.py
index 74bcb541..dc6c51fc 100644
--- a/tests/test_media_library.py
+++ b/tests/test_media_library.py
@@ -18,15 +18,12 @@
 # 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 Gst
 
 from pitivi import medialibrary
 from pitivi.project import ProjectManager
-from pitivi.timeline import timeline
 from pitivi.utils.proxy import ProxyingStrategy
 from tests import common
 
diff --git a/tests/test_mediafilespreviewer.py b/tests/test_mediafilespreviewer.py
new file mode 100644
index 00000000..74a909f4
--- /dev/null
+++ b/tests/test_mediafilespreviewer.py
@@ -0,0 +1,90 @@
+# -*- coding: utf-8 -*-
+# Pitivi video editor
+# Copyright (c) 2019, Alex Băluț <alexandru balut gmail com>
+#
+# 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.
+"""Tests for the mediafilespreviewer module."""
+# pylint: disable=attribute-defined-outside-init,protected-access
+from unittest import mock
+
+from gi.repository import GLib
+
+from pitivi.dialogs.missingasset import MissingAssetDialog
+from pitivi.mediafilespreviewer import PreviewWidget
+from pitivi.utils.proxy import ProxyingStrategy
+from tests import common
+
+
+class PreviewWidgetTest(common.TestCase):
+    """Tests for the PreviewWidget class."""
+
+    def test_select_missing_asset(self):
+        """Exercise the MissingAssetDialog when loading a project."""
+        app = common.create_pitivi(proxyingStrategy=ProxyingStrategy.NOTHING,
+                                   FCpreviewWidth=640,
+                                   FCpreviewHeight=480)
+
+        proj_uri = self.create_project_file_from_xges(app, """<ges version='0.3'>
+            <project properties='properties;' metadatas='metadatas;'>
+                <ressources>
+                    <asset id='file://this/is/a/moved/asset.mp4' extractable-type-name='GESUriClip'
+                        properties='properties, supported-formats=(int)6, duration=(guint64)1228000000;' 
metadatas='metadatas' />
+                </ressources>
+            </project>
+            </ges>""")
+        project_manager = app.project_manager
+
+        # Use a cloned sample so the asset is not in GES's asset cache.
+        # This combination of calls can lead to a mainloop freeze:
+        # - MissingAssetDialog.get_new_uri() through the "missing-uri" signal handler,
+        # - MissingAssetDialog.run() through MissingAssetDialog.get_new_uri(),
+        # - GES.UriClipAsset.request_sync() through PreviewWidget.preview_uri,
+        with common.cloned_sample("1sec_simpsons_trailer.mp4"):
+            asset_uri = common.get_sample_uri("1sec_simpsons_trailer.mp4")
+
+            mainloop = common.create_main_loop()
+
+            def missing_uri_cb(project_manager, project, unused_error, asset):
+                with mock.patch.object(MissingAssetDialog, "set_transient_for"):
+                    mad = MissingAssetDialog(app, asset, asset.get_id())
+                mad._chooser.select_uri(asset_uri)
+                # Close the dialog when idle so get_new_uri does not get stuck.
+                GLib.idle_add(mad.close)
+                uri = mad.get_new_uri()
+                return uri
+
+            project_manager.connect("missing-uri", missing_uri_cb)
+
+            preview_loaded_for_uri = ""
+
+            def preview_uri(preview_widget, uri):
+                nonlocal preview_loaded_for_uri
+                original_preview_uri(preview_widget, uri)
+                # If it gets past the original_preview_uri call, it's all fine!
+                preview_loaded_for_uri = uri
+                mainloop.quit()
+
+            original_preview_uri = PreviewWidget.preview_uri
+            PreviewWidget.preview_uri = preview_uri
+            try:
+                # Our mainloop timeout mechanism cannot be used,
+                # because the mainloop gets blocked.
+                with common.checked_operation_duration(seconds=2):
+                    project_manager.load_project(proj_uri)
+                    mainloop.run()
+                self.assertEqual(preview_loaded_for_uri, asset_uri)
+            finally:
+                PreviewWidget.preview_uri = original_preview_uri
diff --git a/tests/test_project.py b/tests/test_project.py
index c3333b33..2272dd44 100644
--- a/tests/test_project.py
+++ b/tests/test_project.py
@@ -316,16 +316,6 @@ class TestProjectLoading(common.TestCase):
 
         self.assertEqual(len(assets), 1, assets)
 
-    def create_project_file_from_xges(self, app, xges):
-        unused, xges_path = tempfile.mkstemp(suffix=".xges")
-        proj_uri = Gst.filename_to_uri(os.path.abspath(xges_path))
-        app.project_manager.saveProject(uri=proj_uri)
-
-        with open(xges_path, "w") as f:
-            f.write(xges)
-
-        return proj_uri
-
     def load_project_with_missing_proxy(self):
         """Loads a project with missing proxies."""
         uris = [common.get_sample_uri("1sec_simpsons_trailer.mp4")]


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