2019-11-26 17:49:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
2022-02-06 09:10:17 +00:00
|
|
|
# Copyright (c) 2008-2022 OpenLP Developers #
|
2019-11-26 17:49:41 +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/>. #
|
|
|
|
##########################################################################
|
|
|
|
"""
|
|
|
|
All the tests
|
|
|
|
"""
|
|
|
|
import os
|
2020-01-04 21:24:32 +00:00
|
|
|
import sys
|
2020-03-19 20:04:28 +00:00
|
|
|
import shutil
|
|
|
|
from tempfile import mkdtemp
|
2019-11-26 17:49:41 +00:00
|
|
|
from tempfile import mkstemp
|
2019-12-04 20:01:02 +00:00
|
|
|
from unittest.mock import MagicMock
|
2019-11-26 17:49:41 +00:00
|
|
|
|
|
|
|
import pytest
|
2020-02-07 19:53:52 +00:00
|
|
|
from pytestqt.qt_compat import qt_api
|
|
|
|
|
2020-01-04 21:24:32 +00:00
|
|
|
from PyQt5 import QtCore, QtWidgets # noqa
|
|
|
|
sys.modules['PyQt5.QtWebEngineWidgets'] = MagicMock()
|
2019-11-26 17:49:41 +00:00
|
|
|
|
2020-01-04 21:24:32 +00:00
|
|
|
from openlp.core.app import OpenLP
|
2020-01-18 21:00:13 +00:00
|
|
|
from openlp.core.state import State
|
2019-11-26 17:49:41 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
|
|
|
from openlp.core.common.settings import Settings
|
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2020-02-07 19:53:52 +00:00
|
|
|
def qapp(qtbot):
|
2019-11-26 17:49:41 +00:00
|
|
|
"""An instance of QApplication"""
|
2021-06-18 05:19:44 +00:00
|
|
|
# Newer versions of pytest-qt have QApplication in the widgets module.
|
|
|
|
# Catch attribute error if the widgets module is missing and instantiate QApplication the old way.
|
|
|
|
try:
|
|
|
|
qt_api.QtWidgets.QApplication.instance()
|
|
|
|
except AttributeError:
|
|
|
|
qt_api.QApplication.instance()
|
2020-01-04 21:24:32 +00:00
|
|
|
app = OpenLP()
|
|
|
|
yield app
|
|
|
|
del app
|
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2020-01-04 21:24:32 +00:00
|
|
|
def mocked_qapp():
|
|
|
|
"""A mocked instance of QApplication"""
|
|
|
|
app = MagicMock()
|
2019-11-26 17:49:41 +00:00
|
|
|
yield app
|
|
|
|
del app
|
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2019-12-04 20:01:02 +00:00
|
|
|
def registry():
|
|
|
|
"""An instance of the Registry"""
|
2020-03-04 06:06:47 +00:00
|
|
|
yield Registry.create()
|
|
|
|
Registry._instances = {}
|
2019-12-04 20:01:02 +00:00
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2019-12-04 20:01:02 +00:00
|
|
|
def settings(qapp, registry):
|
2019-11-26 17:49:41 +00:00
|
|
|
"""A Settings() instance"""
|
|
|
|
fd, ini_file = mkstemp('.ini')
|
|
|
|
Settings.set_filename(ini_file)
|
|
|
|
Settings().setDefaultFormat(QtCore.QSettings.IniFormat)
|
|
|
|
# Needed on windows to make sure a Settings object is available during the tests
|
|
|
|
sets = Settings()
|
|
|
|
sets.setValue('themes/global theme', 'my_theme')
|
2019-12-04 20:01:02 +00:00
|
|
|
Registry().register('settings', sets)
|
2020-04-02 18:48:27 +00:00
|
|
|
Registry().register('settings_thread', sets)
|
2020-02-18 08:08:39 +00:00
|
|
|
Registry().register('application', qapp)
|
2020-02-09 20:24:05 +00:00
|
|
|
qapp.settings = sets
|
2019-11-26 17:49:41 +00:00
|
|
|
yield sets
|
|
|
|
del sets
|
2020-04-02 18:48:27 +00:00
|
|
|
Registry().remove('settings')
|
|
|
|
Registry().remove('settings_thread')
|
2019-11-26 17:49:41 +00:00
|
|
|
os.close(fd)
|
|
|
|
os.unlink(Settings().fileName())
|
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2020-03-04 06:06:47 +00:00
|
|
|
def mock_settings(qapp, registry):
|
2019-12-04 20:01:02 +00:00
|
|
|
"""A Mock Settings() instance"""
|
|
|
|
# Create and register a mock settings object to work with
|
2020-02-15 21:04:17 +00:00
|
|
|
mk_settings = MagicMock()
|
|
|
|
Registry().register('settings', mk_settings)
|
2020-03-04 06:06:47 +00:00
|
|
|
Registry().register('application', qapp)
|
2020-04-02 18:48:27 +00:00
|
|
|
Registry().register('settings_thread', mk_settings)
|
2020-02-15 21:04:17 +00:00
|
|
|
yield mk_settings
|
2019-12-04 20:01:02 +00:00
|
|
|
Registry().remove('settings')
|
2020-04-02 18:48:27 +00:00
|
|
|
Registry().remove('settings_thread')
|
2020-02-15 21:04:17 +00:00
|
|
|
del mk_settings
|
2020-01-18 21:00:13 +00:00
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture
|
2020-01-18 21:00:13 +00:00
|
|
|
def state():
|
2020-03-04 06:06:47 +00:00
|
|
|
yield State().load_settings()
|
|
|
|
State._instances = {}
|
2020-03-11 21:53:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def state_media(state):
|
|
|
|
State().add_service("media", 0)
|
|
|
|
State().update_pre_conditions("media", True)
|
|
|
|
State().flush_preconditions()
|
2020-03-19 20:04:28 +00:00
|
|
|
|
|
|
|
|
2020-12-22 04:41:57 +00:00
|
|
|
@pytest.fixture()
|
2020-04-09 21:55:27 +00:00
|
|
|
def temp_folder():
|
2020-03-19 20:04:28 +00:00
|
|
|
t_folder = mkdtemp(prefix='openlp_')
|
|
|
|
yield t_folder
|
|
|
|
shutil.rmtree(t_folder, ignore_errors=True)
|