openlp/tests/functional/openlp_core/api/endpoint/test_controller.py

148 lines
6.1 KiB
Python
Raw Normal View History

2018-02-03 14:41:18 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2020 OpenLP Developers #
2019-04-13 13:00:22 +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, 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/>. #
##########################################################################
2018-11-13 05:42:43 +00:00
# import sys
2018-02-03 14:41:18 +00:00
from unittest import TestCase
2018-11-13 05:42:43 +00:00
from unittest.mock import MagicMock, patch
2018-02-03 14:41:18 +00:00
2018-03-18 18:18:17 +00:00
from PyQt5 import QtCore
from openlp.core.api import app as flask_app
2018-12-02 09:49:30 +00:00
from openlp.core.state import State
2018-10-30 20:12:16 +00:00
# Mock QtWebEngineWidgets
2018-11-13 05:42:43 +00:00
# sys.modules['PyQt5.QtWebEngineWidgets'] = MagicMock()
2018-10-30 20:12:16 +00:00
2018-02-03 14:41:18 +00:00
from openlp.core.common.registry import Registry
2018-03-18 18:18:17 +00:00
from openlp.core.display.screens import ScreenList
from openlp.core.lib.serviceitem import ServiceItem
2018-03-18 18:18:17 +00:00
from tests.utils import convert_file_service_item
from tests.utils.constants import RESOURCE_PATH
2018-10-02 04:39:42 +00:00
2018-12-01 05:52:49 +00:00
TEST_PATH = RESOURCE_PATH / 'service'
2018-03-18 18:18:17 +00:00
SCREEN = {
'primary': False,
'number': 1,
'size': QtCore.QRect(0, 0, 1024, 768)
}
2018-02-03 14:41:18 +00:00
class TestController(TestCase):
"""
Test the Remote plugin deploy functions
"""
def setUp(self):
"""
Setup for tests
"""
Registry.create()
self.registry = Registry()
self.mocked_live_controller = MagicMock()
2018-03-18 18:18:17 +00:00
self.desktop = MagicMock()
self.desktop.primaryScreen.return_value = SCREEN['primary']
self.desktop.screenCount.return_value = SCREEN['number']
self.desktop.screenGeometry.return_value = SCREEN['size']
2018-11-13 05:42:43 +00:00
with patch('openlp.core.display.screens.QtWidgets.QApplication.screens') as mocked_screens:
mocked_screens.return_value = [
MagicMock(**{'geometry.return_value': SCREEN['size']})
]
self.screens = ScreenList.create(self.desktop)
2019-02-13 19:41:10 +00:00
# Mock the renderer and its format_slide method
self.mocked_renderer = MagicMock()
2019-02-13 20:28:10 +00:00
2019-02-13 19:41:10 +00:00
def side_effect_return_arg(arg1, arg2):
return [arg1]
self.mocked_slide_formater = MagicMock(side_effect=side_effect_return_arg)
self.mocked_renderer.format_slide = self.mocked_slide_formater
2018-02-03 14:41:18 +00:00
Registry().register('live_controller', self.mocked_live_controller)
2019-02-13 19:41:10 +00:00
Registry().register('renderer', self.mocked_renderer)
flask_app.config['TESTING'] = True
self.client = flask_app.test_client()
Registry().register('settings', MagicMock(**{'value.return_value': 'english'}))
2018-02-03 14:41:18 +00:00
2018-03-18 18:18:17 +00:00
def test_controller_text_empty(self):
2018-02-03 14:41:18 +00:00
"""
2018-03-18 18:18:17 +00:00
Remote API Tests : test the controller text method can be called with empty service item
2018-02-03 14:41:18 +00:00
"""
# GIVEN: A mocked service with a dummy service item
2018-11-13 05:42:43 +00:00
mocked_service_item = MagicMock()
mocked_service_item.get_frames.return_value = []
mocked_service_item.unique_identifier = 'mock-service-item'
self.mocked_live_controller.service_item = mocked_service_item
2018-02-03 14:41:18 +00:00
# WHEN: I trigger the method
ret = self.client.get('/api/controller/live/text').get_json()
2018-11-13 05:42:43 +00:00
2018-02-03 14:41:18 +00:00
# THEN: I get a basic set of results
2018-11-13 05:42:43 +00:00
assert ret['results']['item'] == 'mock-service-item'
assert len(ret['results']['slides']) == 0
2018-02-23 16:55:17 +00:00
2018-03-18 18:18:17 +00:00
def test_controller_text(self):
"""
Remote API Tests : test the controller text method can be called with a real service item
"""
# GIVEN: A mocked service with a dummy service item
line = convert_file_service_item(TEST_PATH, 'serviceitem_custom_1.osj')
self.mocked_live_controller.service_item = ServiceItem(None)
2019-02-06 20:26:33 +00:00
State().load_settings()
2018-12-02 09:49:30 +00:00
State().add_service("media", 0)
State().update_pre_conditions("media", True)
State().flush_preconditions()
2018-03-18 18:18:17 +00:00
self.mocked_live_controller.service_item.set_from_service(line)
2019-02-13 19:41:10 +00:00
self.mocked_live_controller.service_item._create_slides()
2018-03-18 18:18:17 +00:00
# WHEN: I trigger the method
ret = self.client.get('/api/controller/live/text').get_json()
2018-11-13 05:42:43 +00:00
2018-03-18 18:18:17 +00:00
# THEN: I get a basic set of results
results = ret['results']
assert isinstance(ret, dict)
assert len(results['slides']) == 2
2018-02-23 16:55:17 +00:00
def test_controller_direction_next(self):
"""
Text the live next method is triggered
"""
# GIVEN: A mocked service with a dummy service item
2018-11-13 05:42:43 +00:00
mocked_emit = MagicMock()
self.mocked_live_controller.slidecontroller_live_next.emit = mocked_emit
2018-02-23 16:55:17 +00:00
self.mocked_live_controller.service_item = MagicMock()
2018-11-13 05:42:43 +00:00
2018-02-23 16:55:17 +00:00
# WHEN: I trigger the method
self.client.get('/api/controller/live/next')
2018-02-23 16:55:17 +00:00
# THEN: The correct method is called
2018-11-13 05:42:43 +00:00
mocked_emit.assert_called_once_with()
2018-02-23 16:55:17 +00:00
def test_controller_direction_previous(self):
"""
Text the live next method is triggered
"""
# GIVEN: A mocked service with a dummy service item
2018-11-13 05:42:43 +00:00
mocked_emit = MagicMock()
self.mocked_live_controller.slidecontroller_live_previous.emit = mocked_emit
2018-02-23 16:55:17 +00:00
self.mocked_live_controller.service_item = MagicMock()
2018-11-13 05:42:43 +00:00
2018-02-23 16:55:17 +00:00
# WHEN: I trigger the method
self.client.get('/api/controller/live/previous')
2018-02-23 16:55:17 +00:00
# THEN: The correct method is called
2018-11-13 05:42:43 +00:00
mocked_emit.assert_called_once_with()