openlp/tests/interfaces/openlp_core_ui/test_projectorsourceform.py

156 lines
6.4 KiB
Python
Raw Normal View History

# -*- 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 #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
"""
2016-01-07 21:36:43 +00:00
:mod: `tests.interfaces.openlp_core_ui.test_projectorsourceform` module
2016-01-07 21:36:43 +00:00
Tests for the Projector Source Select form.
"""
2016-01-07 21:36:43 +00:00
import os
import time
from unittest import TestCase
from unittest.mock import patch
2016-01-07 21:36:43 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5.QtWidgets import QDialog
from tests.helpers.testmixin import TestMixin
2015-01-19 04:24:45 +00:00
from tests.resources.projector.data import TEST_DB, TEST1_DATA
from openlp.core.common import Registry
2016-01-07 21:36:43 +00:00
from openlp.core.lib.projector.db import ProjectorDB, Projector
2015-01-19 04:24:45 +00:00
from openlp.core.lib.projector.constants import PJLINK_DEFAULT_CODES, PJLINK_DEFAULT_SOURCES
from openlp.core.ui.projector.sourceselectform import source_group, SourceSelectSingle
def build_source_dict():
"""
Builds a source dictionary to verify source_group returns a valid dictionary of dictionary items
:returns: dictionary of valid PJLink source codes grouped by PJLink source group
"""
test_group = {}
for group in PJLINK_DEFAULT_SOURCES:
test_group[group] = {}
for key in PJLINK_DEFAULT_CODES:
test_group[key[0]][key] = PJLINK_DEFAULT_CODES[key]
return test_group
class ProjectorSourceFormTest(TestCase, TestMixin):
"""
Test class for the Projector Source Select form module
"""
2015-01-19 04:24:45 +00:00
@patch('openlp.core.lib.projector.db.init_url')
def setUp(self, mocked_init_url):
"""
Set up anything necessary for all tests
"""
2016-01-07 21:36:43 +00:00
mocked_init_url.return_value = 'sqlite:///{}'.format(TEST_DB)
2015-01-19 04:24:45 +00:00
self.build_settings()
self.setup_application()
Registry.create()
# Do not try to recreate if we've already been created from a previous test
if not hasattr(self, 'projectordb'):
self.projectordb = ProjectorDB()
# Retrieve/create a database record
2016-01-07 21:36:43 +00:00
self.projector = self.projectordb.get_projector_by_ip(TEST1_DATA['ip'])
2015-01-19 04:24:45 +00:00
if not self.projector:
2016-01-07 21:36:43 +00:00
self.projectordb.add_projector(projector=Projector(**TEST1_DATA))
self.projector = self.projectordb.get_projector_by_ip(TEST1_DATA['ip'])
2015-01-19 04:24:45 +00:00
self.projector.dbid = self.projector.id
self.projector.db_item = self.projector
def tearDown(self):
"""
Close database session.
Delete all C++ objects at end so we don't segfault.
"""
self.projectordb.session.close()
del self.projectordb
del self.projector
2016-01-15 19:41:14 +00:00
retries = 0
2016-01-15 19:14:24 +00:00
while retries < 5:
try:
if os.path.exists(TEST_DB):
os.unlink(TEST_DB)
break
except:
time.sleep(1)
retries += 1
2015-01-19 04:24:45 +00:00
self.destroy_settings()
def source_dict_test(self):
"""
Test that source list dict returned from sourceselectform module is a valid dict with proper entries
"""
# GIVEN: A list of inputs
codes = []
for item in PJLINK_DEFAULT_CODES.keys():
codes.append(item)
codes.sort()
# WHEN: projector.sourceselectform.source_select() is called
check = source_group(codes, PJLINK_DEFAULT_CODES)
# THEN: return dictionary should match test dictionary
self.assertEquals(check, build_source_dict(),
"Source group dictionary should match test dictionary")
2015-01-19 04:24:45 +00:00
2015-11-07 00:49:40 +00:00
@patch.object(QDialog, 'exec')
2015-01-19 04:24:45 +00:00
def source_select_edit_button_test(self, mocked_qdialog):
"""
Test source select form edit has Ok, Cancel, Reset, and Revert buttons
"""
# GIVEN: Initial setup and mocks
self.projector.source_available = ['11', ]
self.projector.source = '11'
# WHEN we create a source select widget and set edit=True
select_form = SourceSelectSingle(parent=None, projectordb=self.projectordb)
select_form.edit = True
2015-11-07 00:49:40 +00:00
select_form.exec(projector=self.projector)
2015-01-19 04:24:45 +00:00
projector = select_form.projector
# THEN: Verify all 4 buttons are available
self.assertEquals(len(select_form.button_box.buttons()), 4,
'SourceSelect dialog box should have "OK", "Cancel" '
'"Rest", and "Revert" buttons available')
2015-11-07 00:49:40 +00:00
@patch.object(QDialog, 'exec')
2015-01-19 04:24:45 +00:00
def source_select_noedit_button_test(self, mocked_qdialog):
"""
Test source select form view has OK and Cancel buttons only
"""
# GIVEN: Initial setup and mocks
self.projector.source_available = ['11', ]
self.projector.source = '11'
# WHEN we create a source select widget and set edit=False
select_form = SourceSelectSingle(parent=None, projectordb=self.projectordb)
select_form.edit = False
2015-11-07 00:49:40 +00:00
select_form.exec(projector=self.projector)
2015-01-19 04:24:45 +00:00
projector = select_form.projector
# THEN: Verify only 2 buttons are available
self.assertEquals(len(select_form.button_box.buttons()), 2,
'SourceSelect dialog box should only have "OK" '
'and "Cancel" buttons available')