2013-09-19 21:02:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# 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, either version 3 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 General Public License for more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License #
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2013-02-16 10:28:37 +00:00
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib.screenlist package.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2018-11-16 04:34:49 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2013-02-16 10:28:37 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore, QtWidgets
|
2013-02-16 10:28:37 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
2018-10-02 04:39:42 +00:00
|
|
|
from openlp.core.display.screens import Screen, ScreenList
|
|
|
|
|
2013-02-16 10:28:37 +00:00
|
|
|
|
2013-02-16 18:14:41 +00:00
|
|
|
SCREEN = {
|
2013-08-31 18:17:38 +00:00
|
|
|
'primary': False,
|
|
|
|
'number': 1,
|
|
|
|
'size': QtCore.QRect(0, 0, 1024, 768)
|
2013-02-16 18:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-16 12:27:13 +00:00
|
|
|
class TestScreenList(TestCase):
|
2013-02-16 10:28:37 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Set up the components need for all tests.
|
|
|
|
"""
|
2013-03-07 12:34:35 +00:00
|
|
|
# Mocked out desktop object
|
|
|
|
self.desktop = MagicMock()
|
2013-08-31 18:17:38 +00:00
|
|
|
self.desktop.primaryScreen.return_value = SCREEN['primary']
|
|
|
|
self.desktop.screenCount.return_value = SCREEN['number']
|
|
|
|
self.desktop.screenGeometry.return_value = SCREEN['size']
|
2013-03-07 12:34:35 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
self.application = QtWidgets.QApplication.instance()
|
2013-02-17 07:54:43 +00:00
|
|
|
Registry.create()
|
2013-08-31 18:17:38 +00:00
|
|
|
self.application.setOrganizationName('OpenLP-tests')
|
|
|
|
self.application.setOrganizationDomain('openlp.org')
|
2013-03-07 12:34:35 +00:00
|
|
|
self.screens = ScreenList.create(self.desktop)
|
2013-02-16 10:28:37 +00:00
|
|
|
|
2013-02-16 15:02:19 +00:00
|
|
|
def tearDown(self):
|
|
|
|
"""
|
2013-02-16 18:14:41 +00:00
|
|
|
Delete QApplication.
|
2013-02-16 15:02:19 +00:00
|
|
|
"""
|
2013-02-17 07:54:43 +00:00
|
|
|
del self.screens
|
2013-02-16 15:02:19 +00:00
|
|
|
del self.application
|
|
|
|
|
2017-12-02 05:31:23 +00:00
|
|
|
def test_current_display_screen(self):
|
|
|
|
"""
|
|
|
|
Test that the "current" property returns the first display screen
|
|
|
|
"""
|
|
|
|
# GIVEN: A new ScreenList object with some screens
|
|
|
|
screen_list = ScreenList()
|
|
|
|
screen_list.screens = [
|
|
|
|
Screen(number=0, geometry=QtCore.QRect(0, 0, 1024, 768), is_primary=True),
|
|
|
|
Screen(number=1, geometry=QtCore.QRect(1024, 0, 1024, 768), is_primary=False, is_display=True)
|
|
|
|
]
|
|
|
|
|
|
|
|
# WHEN: The current property is accessed
|
|
|
|
screen = screen_list.current
|
|
|
|
|
|
|
|
# THEN: It should be the display screen
|
|
|
|
assert screen.number == 1
|
|
|
|
assert screen.geometry == QtCore.QRect(1024, 0, 1024, 768)
|
|
|
|
assert screen.is_primary is False
|
|
|
|
assert screen.is_display is True
|
|
|
|
|
|
|
|
def test_current_primary_screen(self):
|
|
|
|
"""
|
|
|
|
Test that the "current" property returns the first primary screen
|
|
|
|
"""
|
|
|
|
# GIVEN: A new ScreenList object with some screens
|
|
|
|
screen_list = ScreenList()
|
|
|
|
screen_list.screens = [
|
|
|
|
Screen(number=0, geometry=QtCore.QRect(0, 0, 1024, 768), is_primary=True)
|
|
|
|
]
|
|
|
|
|
|
|
|
# WHEN: The current property is accessed
|
|
|
|
screen = screen_list.current
|
|
|
|
|
|
|
|
# THEN: It should be the display screen
|
|
|
|
assert screen.number == 0
|
|
|
|
assert screen.geometry == QtCore.QRect(0, 0, 1024, 768)
|
|
|
|
assert screen.is_primary is True
|
|
|
|
assert screen.is_display is False
|
|
|
|
|
2018-11-16 04:34:49 +00:00
|
|
|
@patch('openlp.core.display.screens.QtWidgets.QApplication.screens')
|
|
|
|
def test_create_screen_list(self, mocked_screens):
|
2013-02-16 10:28:37 +00:00
|
|
|
"""
|
2017-12-02 00:35:21 +00:00
|
|
|
Create the screen list
|
2013-02-16 10:28:37 +00:00
|
|
|
"""
|
2017-12-02 00:35:21 +00:00
|
|
|
# GIVEN: Mocked desktop
|
|
|
|
mocked_desktop = MagicMock()
|
|
|
|
mocked_desktop.screenCount.return_value = 2
|
|
|
|
mocked_desktop.primaryScreen.return_value = 0
|
2018-11-16 04:34:49 +00:00
|
|
|
mocked_screens.return_value = [
|
|
|
|
MagicMock(**{'geometry.return_value': QtCore.QRect(0, 0, 1024, 768)}),
|
|
|
|
MagicMock(**{'geometry.return_value': QtCore.QRect(1024, 0, 1024, 768)})
|
|
|
|
]
|
2013-03-07 12:34:35 +00:00
|
|
|
|
2017-12-02 00:35:21 +00:00
|
|
|
# WHEN: create() is called
|
|
|
|
screen_list = ScreenList.create(mocked_desktop)
|
2013-03-07 12:34:35 +00:00
|
|
|
|
2017-12-02 00:35:21 +00:00
|
|
|
# THEN: The correct screens have been set up
|
|
|
|
assert screen_list.screens[0].number == 0
|
|
|
|
assert screen_list.screens[0].geometry == QtCore.QRect(0, 0, 1024, 768)
|
|
|
|
assert screen_list.screens[0].is_primary is True
|
|
|
|
assert screen_list.screens[1].number == 1
|
|
|
|
assert screen_list.screens[1].geometry == QtCore.QRect(1024, 0, 1024, 768)
|
|
|
|
assert screen_list.screens[1].is_primary is False
|
2019-10-09 08:26:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_screen_from_dict():
|
|
|
|
"""Test that all the correct attributes are set when creating a screen from a dictionary"""
|
|
|
|
# GIVEN: A dictionary of values
|
|
|
|
screen_dict = {
|
|
|
|
'number': 1,
|
|
|
|
'geometry': {'x': 0, 'y': 0, 'width': 1920, 'height': 1080},
|
|
|
|
'custom_geometry': {'x': 10, 'y': 10, 'width': 1900, 'height': 1060},
|
|
|
|
'is_primary': True,
|
|
|
|
'is_display': False
|
|
|
|
}
|
|
|
|
|
|
|
|
# WHEN: A screen object is created from a dictionary
|
|
|
|
screen = Screen.from_dict(screen_dict)
|
|
|
|
|
|
|
|
# THEN: A screen object with the correct attributes is created
|
|
|
|
assert screen.number == 1
|
|
|
|
assert screen.is_primary is True
|
|
|
|
assert screen.is_display is False
|
|
|
|
assert screen.geometry == QtCore.QRect(0, 0, 1920, 1080)
|
|
|
|
assert screen.custom_geometry == QtCore.QRect(10, 10, 1900, 1060)
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_to_dict():
|
|
|
|
"""Test that the correct dictionary is generated"""
|
|
|
|
# GIVEN: A screen object
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), QtCore.QRect(10, 10, 1900, 1060), True, False)
|
|
|
|
|
|
|
|
# WHEN: A dictionary is generated
|
|
|
|
screen_dict = screen.to_dict()
|
|
|
|
|
|
|
|
# THEN: The dictionary should be correct
|
|
|
|
expected_screen = {
|
|
|
|
'number': 1,
|
|
|
|
'geometry': {'x': 0, 'y': 0, 'width': 1920, 'height': 1080},
|
|
|
|
'custom_geometry': {'x': 10, 'y': 10, 'width': 1900, 'height': 1060},
|
|
|
|
'is_primary': True,
|
|
|
|
'is_display': False
|
|
|
|
}
|
|
|
|
assert screen_dict == expected_screen
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_update():
|
|
|
|
"""Test that updating a screen object from a dictionary results in the correct attributes"""
|
|
|
|
# GIVEN: A screen object and a dictionary with updated values
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), is_primary=True, is_display=False)
|
|
|
|
updated_screen = {
|
|
|
|
'geometry': {'x': 0, 'y': 0, 'width': 3840, 'height': 2160},
|
|
|
|
'custom_geometry': {'x': 10, 'y': 10, 'width': 1900, 'height': 1060},
|
|
|
|
'is_primary': False,
|
|
|
|
'is_display': True
|
|
|
|
}
|
|
|
|
|
|
|
|
# WHEN: screen.update() is called
|
|
|
|
screen.update(updated_screen)
|
|
|
|
|
|
|
|
# Then the screen attributes should be correct
|
|
|
|
assert screen.is_primary is False
|
|
|
|
assert screen.is_display is True
|
|
|
|
assert screen.geometry == QtCore.QRect(0, 0, 3840, 2160)
|
|
|
|
assert screen.custom_geometry == QtCore.QRect(10, 10, 1900, 1060)
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_update_bad_geometry():
|
|
|
|
"""Test that updating a screen object from a dictionary results in the correct attributes"""
|
|
|
|
# GIVEN: A screen object and a dictionary with updated values
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), is_primary=True, is_display=False)
|
|
|
|
updated_screen = {
|
|
|
|
'geometry': {'x': 0, 'y': 0, 'x2': 3840, 'y2': 2160},
|
|
|
|
'custom_geometry': {'x': 10, 'y': 10, 'width': 1900, 'height': 1060},
|
|
|
|
'is_primary': False,
|
|
|
|
'is_display': True
|
|
|
|
}
|
|
|
|
|
|
|
|
# WHEN: screen.update() is called
|
|
|
|
screen.update(updated_screen)
|
|
|
|
|
|
|
|
# Then the screen attributes should be correct
|
|
|
|
assert screen.is_primary is False
|
|
|
|
assert screen.is_display is True
|
|
|
|
assert screen.geometry == QtCore.QRect(0, 0, 1920, 1080)
|
|
|
|
assert screen.custom_geometry == QtCore.QRect(10, 10, 1900, 1060)
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_to_str():
|
|
|
|
"""Test that the correct string is generated"""
|
|
|
|
# GIVEN: A screen object
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), QtCore.QRect(10, 10, 1900, 1060), True, False)
|
|
|
|
|
|
|
|
# WHEN: A string is generated
|
|
|
|
screen_str = str(screen)
|
|
|
|
|
|
|
|
# THEN: The string should be correct (screens are 0-based)
|
|
|
|
assert screen_str == 'Screen 2 (primary)'
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_display_geometry():
|
|
|
|
"""Test that the display_geometry property returns the geometry when no custom geometry exists"""
|
|
|
|
# GIVEN: A screen object
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), is_primary=True, is_display=False)
|
|
|
|
|
|
|
|
# WHEN: The display_geometry property is accessed
|
|
|
|
display_geometry = screen.display_geometry
|
|
|
|
|
|
|
|
# THEN: The display geometry is correct
|
|
|
|
assert display_geometry == QtCore.QRect(0, 0, 1920, 1080)
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_display_geometry_custom():
|
|
|
|
"""Test that the display_geometry property returns the custom geometry when it exists"""
|
|
|
|
# GIVEN: A screen object
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), QtCore.QRect(10, 10, 1900, 1060), True, False)
|
|
|
|
|
|
|
|
# WHEN: The display_geometry property is accessed
|
|
|
|
display_geometry = screen.display_geometry
|
|
|
|
|
|
|
|
# THEN: The display geometry is correct
|
|
|
|
assert display_geometry == QtCore.QRect(10, 10, 1900, 1060)
|
|
|
|
|
|
|
|
|
|
|
|
def test_screen_repr():
|
|
|
|
"""Test that the correct screen representation is generated"""
|
|
|
|
# GIVEN: A screen object
|
|
|
|
screen = Screen(1, QtCore.QRect(0, 0, 1920, 1080), QtCore.QRect(10, 10, 1900, 1060), True, False)
|
|
|
|
|
|
|
|
# WHEN: A string is generated
|
|
|
|
screen_str = screen.__repr__()
|
|
|
|
|
|
|
|
# THEN: The string should be correct (screens are 0-based)
|
|
|
|
assert screen_str == '<Screen 2 (primary)>'
|