← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~tomasgroth/openlp/ppt-catch into lp:openlp

 

Tomas Groth has proposed merging lp:~tomasgroth/openlp/ppt-catch into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~tomasgroth/openlp/ppt-catch/+merge/225457

Improve powerpoint error handling.
-- 
https://code.launchpad.net/~tomasgroth/openlp/ppt-catch/+merge/225457
Your team OpenLP Core is requested to review the proposed merge of lp:~tomasgroth/openlp/ppt-catch into lp:openlp.
=== modified file 'openlp/plugins/presentations/lib/powerpointcontroller.py'
--- openlp/plugins/presentations/lib/powerpointcontroller.py	2014-04-12 20:19:22 +0000
+++ openlp/plugins/presentations/lib/powerpointcontroller.py	2014-07-03 11:24:32 +0000
@@ -40,6 +40,8 @@
     import pywintypes
 
 from openlp.core.lib import ScreenList
+from openlp.core.lib.ui import UiStrings, critical_error_message_box, translate
+from openlp.core.common import trace_error_handler
 from .presentationcontroller import PresentationController, PresentationDocument
 
 
@@ -99,7 +101,7 @@
                 if self.process.Presentations.Count > 0:
                     return
                 self.process.Quit()
-            except pywintypes.com_error:
+            except (AttributeError, pywintypes.com_error):
                 pass
             self.process = None
 
@@ -126,16 +128,24 @@
         earlier.
         """
         log.debug('load_presentation')
-        if not self.controller.process or not self.controller.process.Visible:
-            self.controller.start_process()
         try:
+            if not self.controller.process or not self.controller.process.Visible:
+                self.controller.start_process()
             self.controller.process.Presentations.Open(self.file_path, False, False, True)
+            self.presentation = self.controller.process.Presentations(self.controller.process.Presentations.Count)
+            self.create_thumbnails()
+            # Powerpoint 2013 pops up when loading a file, so we minimize it again
+            if self.presentation.Application.Version == u'15.0':
+                try:
+                    self.presentation.Application.WindowState = 2
+                except:
+                    log.error('Failed to minimize main powerpoint window')
+                    trace_error_handler(log)
+            return True
         except pywintypes.com_error:
-            log.debug('PPT open failed')
+            log.error('PPT open failed')
+            trace_error_handler(log)
             return False
-        self.presentation = self.controller.process.Presentations(self.controller.process.Presentations.Count)
-        self.create_thumbnails()
-        return True
 
     def create_thumbnails(self):
         """
@@ -206,23 +216,33 @@
         Unblanks (restores) the presentation.
         """
         log.debug('unblank_screen')
-        self.presentation.SlideShowSettings.Run()
-        self.presentation.SlideShowWindow.View.State = 1
-        self.presentation.SlideShowWindow.Activate()
-        if self.presentation.Application.Version == '14.0':
-            # Unblanking is broken in PowerPoint 2010, need to redisplay
-            slide = self.presentation.SlideShowWindow.View.CurrentShowPosition
-            click = self.presentation.SlideShowWindow.View.GetClickIndex()
-            self.presentation.SlideShowWindow.View.GotoSlide(slide)
-            if click:
-                self.presentation.SlideShowWindow.View.GotoClick(click)
+        try:
+            self.presentation.SlideShowSettings.Run()
+            self.presentation.SlideShowWindow.View.State = 1
+            self.presentation.SlideShowWindow.Activate()
+            if self.presentation.Application.Version == '14.0':
+                # Unblanking is broken in PowerPoint 2010, need to redisplay
+                slide = self.presentation.SlideShowWindow.View.CurrentShowPosition
+                click = self.presentation.SlideShowWindow.View.GetClickIndex()
+                self.presentation.SlideShowWindow.View.GotoSlide(slide)
+                if click:
+                    self.presentation.SlideShowWindow.View.GotoClick(click)
+        except pywintypes.com_error:
+            log.error('COM error while in unblank_screen')
+            trace_error_handler(log)
+            self.show_error_msg()
 
     def blank_screen(self):
         """
         Blanks the screen.
         """
         log.debug('blank_screen')
-        self.presentation.SlideShowWindow.View.State = 3
+        try:
+            self.presentation.SlideShowWindow.View.State = 3
+        except pywintypes.com_error:
+            log.error('COM error while in blank_screen')
+            trace_error_handler(log)
+            self.show_error_msg()
 
     def is_blank(self):
         """
@@ -230,7 +250,12 @@
         """
         log.debug('is_blank')
         if self.is_active():
-            return self.presentation.SlideShowWindow.View.State == 3
+            try:
+                return self.presentation.SlideShowWindow.View.State == 3
+            except pywintypes.com_error:
+                log.error('COM error while in is_blank')
+                trace_error_handler(log)
+                self.show_error_msg()
         else:
             return False
 
@@ -239,7 +264,12 @@
         Stops the current presentation and hides the output.
         """
         log.debug('stop_presentation')
-        self.presentation.SlideShowWindow.View.Exit()
+        try:
+            self.presentation.SlideShowWindow.View.Exit()
+        except pywintypes.com_error:
+            log.error('COM error while in stop_presentation')
+            trace_error_handler(log)
+            self.show_error_msg()
 
     if os.name == 'nt':
         def start_presentation(self):
@@ -259,24 +289,49 @@
             ppt_window = self.presentation.SlideShowSettings.Run()
             if not ppt_window:
                 return
-            ppt_window.Top = size.y() * 72 / dpi
-            ppt_window.Height = size.height() * 72 / dpi
-            ppt_window.Left = size.x() * 72 / dpi
-            ppt_window.Width = size.width() * 72 / dpi
+            try:
+                ppt_window.Top = size.y() * 72 / dpi
+                ppt_window.Height = size.height() * 72 / dpi
+                ppt_window.Left = size.x() * 72 / dpi
+                ppt_window.Width = size.width() * 72 / dpi
+            except AttributeError as e:
+                log.error('AttributeError while in start_presentation')
+                log.error(e)
+            # Powerpoint 2013 pops up when starting a file, so we minimize it again
+            if self.presentation.Application.Version == u'15.0':
+                try:
+                    self.presentation.Application.WindowState = 2
+                except:
+                    log.error('Failed to minimize main powerpoint window')
+                    trace_error_handler(log)
 
     def get_slide_number(self):
         """
         Returns the current slide number.
         """
         log.debug('get_slide_number')
-        return self.presentation.SlideShowWindow.View.CurrentShowPosition
+        ret = 0
+        try:
+            ret = self.presentation.SlideShowWindow.View.CurrentShowPosition
+        except pywintypes.com_error:
+            log.error('COM error while in get_slide_number')
+            trace_error_handler(log)
+            self.show_error_msg()
+        return ret
 
     def get_slide_count(self):
         """
         Returns total number of slides.
         """
         log.debug('get_slide_count')
-        return self.presentation.Slides.Count
+        ret = 0
+        try:
+            ret = self.presentation.Slides.Count
+        except pywintypes.com_error:
+            log.error('COM error while in get_slide_count')
+            trace_error_handler(log)
+            self.show_error_msg()
+        return ret
 
     def goto_slide(self, slide_no):
         """
@@ -285,14 +340,25 @@
         :param slide_no: The slide the text is required for, starting at 1
         """
         log.debug('goto_slide')
-        self.presentation.SlideShowWindow.View.GotoSlide(slide_no)
+        try:
+            self.presentation.SlideShowWindow.View.GotoSlide(slide_no)
+        except pywintypes.com_error:
+            log.error('COM error while in goto_slide')
+            trace_error_handler(log)
+            self.show_error_msg()
 
     def next_step(self):
         """
         Triggers the next effect of slide on the running presentation.
         """
         log.debug('next_step')
-        self.presentation.SlideShowWindow.View.Next()
+        try:
+            self.presentation.SlideShowWindow.View.Next()
+        except pywintypes.com_error:
+            log.error('COM error while in next_step')
+            trace_error_handler(log)
+            self.show_error_msg()
+            return
         if self.get_slide_number() > self.get_slide_count():
             self.previous_step()
 
@@ -301,7 +367,12 @@
         Triggers the previous slide on the running presentation.
         """
         log.debug('previous_step')
-        self.presentation.SlideShowWindow.View.Previous()
+        try:
+            self.presentation.SlideShowWindow.View.Previous()
+        except pywintypes.com_error:
+            log.error('COM error while in previous_step')
+            trace_error_handler(log)
+            self.show_error_msg()
 
     def get_slide_text(self, slide_no):
         """
@@ -319,6 +390,16 @@
         """
         return _get_text_from_shapes(self.presentation.Slides(slide_no).NotesPage.Shapes)
 
+    def show_error_msg(self):
+        """
+        Stop presentation and display an error message.
+        """
+        self.stop_presentation()
+        critical_error_message_box(UiStrings().Error, translate('PresentationPlugin.PowerpointDocument',
+                                                                'An error occurred in the Powerpoint integration '
+                                                                'and the presentation will be stopped. '
+                                                                'Restart the presentation if you wish to present it.'))
+
 
 def _get_text_from_shapes(shapes):
     """

=== added file 'tests/functional/openlp_plugins/presentations/test_powerpointcontroller.py'
--- tests/functional/openlp_plugins/presentations/test_powerpointcontroller.py	1970-01-01 00:00:00 +0000
+++ tests/functional/openlp_plugins/presentations/test_powerpointcontroller.py	2014-07-03 11:24:32 +0000
@@ -0,0 +1,131 @@
+# -*- coding: utf-8 -*-
+# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
+
+###############################################################################
+# OpenLP - Open Source Lyrics Projection                                      #
+# --------------------------------------------------------------------------- #
+# Copyright (c) 2008-2014 Raoul Snyman                                        #
+# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan      #
+# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub,      #
+# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer.   #
+# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru,          #
+# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith,             #
+# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock,              #
+# Frode Woldsund, Martin Zibricky, Patrick Zimmermann                         #
+# --------------------------------------------------------------------------- #
+# This program is free software; you can redistribute it and/or modify it     #
+# under the terms of the GNU General Public License as published by the Free  #
+# Software Foundation; version 2 of the License.                              #
+#                                                                             #
+# 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 General Public License for    #
+# more details.                                                               #
+#                                                                             #
+# You should have received a copy of the GNU General Public License along     #
+# with this program; if not, write to the Free Software Foundation, Inc., 59  #
+# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
+###############################################################################
+"""
+Functional tests to test the PowerPointController class and related methods.
+"""
+import os
+if os.name == 'nt':
+    import pywintypes
+import shutil
+from unittest import TestCase
+from tempfile import mkdtemp
+
+from tests.functional import patch, MagicMock
+from tests.helpers.testmixin import TestMixin
+
+from openlp.plugins.presentations.lib.powerpointcontroller import PowerpointController, PowerpointDocument
+
+
+class TestPowerpointController(TestCase, TestMixin):
+    """
+    Test the PowerpointController Class
+    """
+
+    def setUp(self):
+        """
+        Set up the patches and mocks need for all tests.
+        """
+        self.get_application()
+        self.build_settings()
+        self.mock_plugin = MagicMock()
+        self.temp_folder = mkdtemp()
+        self.mock_plugin.settings_section = self.temp_folder
+
+    def tearDown(self):
+        """
+        Stop the patches
+        """
+        self.destroy_settings()
+        shutil.rmtree(self.temp_folder)
+
+    def constructor_test(self):
+        """
+        Test the Constructor from the PowerpointController
+        """
+        # GIVEN: No presentation controller
+        controller = None
+
+        # WHEN: The presentation controller object is created
+        controller = PowerpointController(plugin=self.mock_plugin)
+
+        # THEN: The name of the presentation controller should be correct
+        self.assertEqual('Powerpoint', controller.name,
+                         'The name of the presentation controller should be correct')
+
+
+class TestPowerpointDocument(TestCase):
+    """
+    Test the PowerpointDocument Class
+    """
+
+    def setUp(self):
+        """
+        Set up the patches and mocks need for all tests.
+        """
+        self.powerpoint_document_stop_presentation_patcher = patch(
+            'openlp.plugins.presentations.lib.powerpointcontroller.PowerpointDocument.stop_presentation')
+        self.presentation_document_get_temp_folder_patcher = patch(
+            'openlp.plugins.presentations.lib.powerpointcontroller.PresentationDocument.get_temp_folder')
+        self.presentation_document_setup_patcher = patch(
+            'openlp.plugins.presentations.lib.powerpointcontroller.PresentationDocument._setup')
+        self.mock_powerpoint_document_stop_presentation = self.powerpoint_document_stop_presentation_patcher.start()
+        self.mock_presentation_document_get_temp_folder = self.presentation_document_get_temp_folder_patcher.start()
+        self.mock_presentation_document_setup = self.presentation_document_setup_patcher.start()
+        self.mock_controller = MagicMock()
+        self.mock_presentation = MagicMock()
+        self.mock_presentation_document_get_temp_folder.return_value = 'temp folder'
+
+    def tearDown(self):
+        """
+        Stop the patches
+        """
+        self.powerpoint_document_stop_presentation_patcher.stop()
+        self.presentation_document_get_temp_folder_patcher.stop()
+        self.presentation_document_setup_patcher.stop()
+
+    def show_error_msg_test(self):
+        """
+        Test the PowerpointDocument.show_error_msg() method gets called on com exception
+        """
+        if os.name == 'nt':
+            # GIVEN: A PowerpointDocument with mocked controller and presentation
+            with patch('openlp.plugins.presentations.lib.powerpointcontroller.critical_error_message_box') as \
+                    mocked_critical_error_message_box:
+                instance = PowerpointDocument(self.mock_controller, self.mock_presentation)
+                instance.presentation = MagicMock()
+                instance.presentation.SlideShowWindow.View.GotoSlide = MagicMock(side_effect=pywintypes.com_error('1'))
+
+                # WHEN: Calling goto_slide which will throw an exception
+                instance.goto_slide(42)
+
+                # THEN: mocked_critical_error_message_box should have been called
+                mocked_critical_error_message_box.assert_called_with('Error', 'An error occurred in the Powerpoint '
+                                                                     'integration and the presentation will be stopped.'
+                                                                     ' Restart the presentation if you wish to '
+                                                                     'present it.')


Follow ups