openlp/tests/functional/openlp_core_ui/test_maindisplay.py

186 lines
8.2 KiB
Python
Raw Normal View History

2013-10-11 15:48:14 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2015-12-31 22:46:06 +00:00
# Copyright (c) 2008-2016 OpenLP Developers #
2013-10-11 15:48:14 +00:00
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
"""
2014-01-11 19:46:31 +00:00
Package to test the openlp.core.ui.slidecontroller package.
2013-10-11 15:48:14 +00:00
"""
from unittest import TestCase, skipUnless
2013-10-11 15:48:14 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore
2013-10-11 15:48:14 +00:00
2015-12-24 19:27:44 +00:00
from openlp.core.common import Registry, is_macosx
2014-01-11 19:46:31 +00:00
from openlp.core.lib import ScreenList
from openlp.core.ui import MainDisplay
from openlp.core.ui.maindisplay import TRANSPARENT_STYLESHEET, OPAQUE_STYLESHEET
2014-01-11 19:46:31 +00:00
from tests.helpers.testmixin import TestMixin
from tests.functional import MagicMock, patch
2014-01-11 19:46:31 +00:00
2015-12-24 19:27:44 +00:00
if is_macosx():
from ctypes import pythonapi, c_void_p, c_char_p, py_object
from sip import voidptr
from objc import objc_object
from AppKit import NSMainMenuWindowLevel, NSWindowCollectionBehaviorManaged
2015-05-26 04:33:55 +00:00
class TestMainDisplay(TestCase, TestMixin):
2013-10-11 15:48:14 +00:00
2014-01-11 19:46:31 +00:00
def setUp(self):
"""
Set up the components need for all tests.
"""
# Mocked out desktop object
self.desktop = MagicMock()
2015-05-25 21:41:18 +00:00
self.desktop.primaryScreen.return_value = 0
self.desktop.screenCount.return_value = 2
self.desktop.screenGeometry.side_effect = lambda x: {0: QtCore.QRect(0, 0, 1024, 768),
1: QtCore.QRect(0, 0, 1024, 768)}[x]
2014-01-11 19:46:31 +00:00
self.screens = ScreenList.create(self.desktop)
Registry.create()
self.registry = Registry()
self.setup_application()
Registry().register('application', self.app)
self.mocked_audio_player = patch('openlp.core.ui.maindisplay.AudioPlayer')
self.mocked_audio_player.start()
2014-01-11 19:46:31 +00:00
def tearDown(self):
"""
Delete QApplication.
"""
self.mocked_audio_player.stop()
2014-01-11 19:46:31 +00:00
del self.screens
def initial_main_display_test(self):
"""
2015-05-25 21:41:18 +00:00
Test the initial Main Display state
2014-01-11 19:46:31 +00:00
"""
2015-05-25 21:41:18 +00:00
# GIVEN: A new SlideController instance.
2014-01-11 19:46:31 +00:00
display = MagicMock()
display.is_live = True
2015-05-25 21:41:18 +00:00
# WHEN: The default controller is built.
2014-01-11 19:46:31 +00:00
main_display = MainDisplay(display)
2015-05-25 21:41:18 +00:00
# THEN: The controller should be a live controller.
2014-01-11 19:46:31 +00:00
self.assertEqual(main_display.is_live, True, 'The main display should be a live controller')
2013-10-11 15:48:14 +00:00
def set_transparency_enabled_test(self):
2013-10-11 15:48:14 +00:00
"""
Test setting the display to be transparent
2013-10-11 15:48:14 +00:00
"""
# GIVEN: An instance of MainDisplay
2013-10-24 18:12:41 +00:00
display = MagicMock()
2013-10-24 20:24:58 +00:00
main_display = MainDisplay(display)
2013-10-11 15:48:14 +00:00
# WHEN: Transparency is enabled
2013-10-24 20:24:58 +00:00
main_display.set_transparency(True)
2013-10-11 15:48:14 +00:00
# THEN: The transparent stylesheet should be used
self.assertEqual(TRANSPARENT_STYLESHEET, main_display.styleSheet(),
'The MainDisplay should use the transparent stylesheet')
2014-04-08 15:15:46 +00:00
self.assertFalse(main_display.autoFillBackground(),
'The MainDisplay should not have autoFillBackground set')
2014-04-08 15:15:46 +00:00
self.assertTrue(main_display.testAttribute(QtCore.Qt.WA_TranslucentBackground),
'The MainDisplay should have a translucent background')
2013-10-24 18:12:41 +00:00
def set_transparency_disabled_test(self):
"""
Test setting the display to be opaque
"""
# GIVEN: An instance of MainDisplay
display = MagicMock()
main_display = MainDisplay(display)
# WHEN: Transparency is disabled
2013-10-24 20:24:58 +00:00
main_display.set_transparency(False)
2013-10-24 18:12:41 +00:00
# THEN: The opaque stylesheet should be used
self.assertEqual(OPAQUE_STYLESHEET, main_display.styleSheet(),
'The MainDisplay should use the opaque stylesheet')
2014-04-08 15:15:46 +00:00
self.assertFalse(main_display.testAttribute(QtCore.Qt.WA_TranslucentBackground),
'The MainDisplay should not have a translucent background')
def css_changed_test(self):
"""
Test that when the CSS changes, the plugins are looped over and given an opportunity to update the CSS
"""
# GIVEN: A mocked list of plugins, a mocked display and a MainDisplay
mocked_songs_plugin = MagicMock()
mocked_bibles_plugin = MagicMock()
mocked_plugin_manager = MagicMock()
mocked_plugin_manager.plugins = [mocked_songs_plugin, mocked_bibles_plugin]
Registry().register('plugin_manager', mocked_plugin_manager)
display = MagicMock()
main_display = MainDisplay(display)
# This is set up dynamically, so we need to mock it out for now
main_display.frame = MagicMock()
# WHEN: The css_changed() method is triggered
main_display.css_changed()
# THEN: The plugins should have each been given an opportunity to add their bit to the CSS
mocked_songs_plugin.refresh_css.assert_called_with(main_display.frame)
mocked_bibles_plugin.refresh_css.assert_called_with(main_display.frame)
2015-05-25 21:41:18 +00:00
2016-01-11 03:27:28 +00:00
@skipUnless(is_macosx(), 'Can only run test on Mac OS X due to pyobjc dependency.')
def macosx_display_window_flags_state_test(self):
2015-05-25 21:41:18 +00:00
"""
2015-12-24 19:27:44 +00:00
Test that on Mac OS X we set the proper window flags
2015-05-25 21:41:18 +00:00
"""
2015-12-24 19:27:44 +00:00
# GIVEN: A new SlideController instance on Mac OS X.
self.screens.set_current_display(0)
2015-05-25 21:41:18 +00:00
display = MagicMock()
# WHEN: The default controller is built.
main_display = MainDisplay(display)
2015-12-24 19:27:44 +00:00
# THEN: The window flags should be the same as those needed on Mac OS X.
self.assertEqual(QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint,
2015-05-25 21:41:18 +00:00
main_display.windowFlags(),
2015-12-24 19:27:44 +00:00
'The window flags should be Qt.Window, and Qt.FramelessWindowHint.')
2015-05-25 21:41:18 +00:00
@skipUnless(is_macosx(), 'Can only run test on Mac OS X due to pyobjc dependency.')
2015-12-24 19:27:44 +00:00
def macosx_display_test(self):
2015-05-25 21:41:18 +00:00
"""
2015-12-24 19:27:44 +00:00
Test display on Mac OS X
2015-05-25 21:41:18 +00:00
"""
2015-12-24 19:27:44 +00:00
# GIVEN: A new SlideController instance on Mac OS X.
2015-05-25 21:41:18 +00:00
self.screens.set_current_display(0)
display = MagicMock()
2015-12-24 19:27:44 +00:00
# WHEN: The default controller is built and a reference to the underlying NSView is stored.
2015-05-25 21:41:18 +00:00
main_display = MainDisplay(display)
2015-12-24 19:27:44 +00:00
try:
nsview_pointer = main_display.winId().ascapsule()
except:
nsview_pointer = voidptr(main_display.winId()).ascapsule()
pythonapi.PyCapsule_SetName.restype = c_void_p
pythonapi.PyCapsule_SetName.argtypes = [py_object, c_char_p]
pythonapi.PyCapsule_SetName(nsview_pointer, c_char_p(b"objc.__object__"))
pyobjc_nsview = objc_object(cobject=nsview_pointer)
# THEN: The window level and collection behavior should be the same as those needed for Mac OS X.
self.assertEqual(pyobjc_nsview.window().level(), NSMainMenuWindowLevel + 2,
'Window level should be NSMainMenuWindowLevel + 2')
self.assertEqual(pyobjc_nsview.window().collectionBehavior(), NSWindowCollectionBehaviorManaged,
'Window collection behavior should be NSWindowCollectionBehaviorManaged')