openlp/tests/functional/openlp_core/projectors/test_projector_pjlink_base.py

142 lines
5.6 KiB
Python
Raw Normal View History

2017-08-06 07:23:26 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2015 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 #
###############################################################################
"""
2017-11-10 11:59:38 +00:00
Package to test the openlp.core.projectors.pjlink base package.
2017-08-06 07:23:26 +00:00
"""
from unittest import TestCase
from unittest.mock import call, patch, MagicMock
2017-12-25 08:44:30 +00:00
from openlp.core.projectors.constants import E_PARAMETER, STATUS_CODE, S_ON, S_CONNECTED, QSOCKET_STATE
from openlp.core.projectors.db import Projector
from openlp.core.projectors.pjlink import PJLink
2017-12-04 00:24:47 +00:00
from tests.resources.projector.data import TEST1_DATA
2017-08-06 07:23:26 +00:00
2017-09-22 12:03:28 +00:00
pjlink_test = PJLink(Projector(**TEST1_DATA), no_poll=True)
2017-08-06 07:23:26 +00:00
class TestPJLinkBase(TestCase):
"""
Tests for the PJLink module
"""
2017-12-04 00:24:47 +00:00
def setUp(self):
'''
TestPJLinkCommands part 2 initialization
'''
self.pjlink_test = PJLink(Projector(**TEST1_DATA), no_poll=True)
def tearDown(self):
'''
TestPJLinkCommands part 2 cleanups
'''
self.pjlink_test = None
2017-08-06 07:23:26 +00:00
@patch.object(pjlink_test, 'change_status')
def test_status_change(self, mock_change_status):
"""
Test process_command call with ERR2 (Parameter) status
"""
# GIVEN: Test object
pjlink = pjlink_test
# WHEN: process_command is called with "ERR2" status from projector
pjlink.process_command('POWR', 'ERR2')
# THEN: change_status should have called change_status with E_UNDEFINED
# as first parameter
mock_change_status.called_with(E_PARAMETER,
'change_status should have been called with "{}"'.format(
2017-12-25 08:44:30 +00:00
STATUS_CODE[E_PARAMETER]))
2017-08-06 07:23:26 +00:00
@patch.object(pjlink_test, 'disconnect_from_host')
def test_socket_abort(self, mock_disconnect):
"""
Test PJLink.socket_abort calls disconnect_from_host
"""
# GIVEN: Test object
pjlink = pjlink_test
# WHEN: Calling socket_abort
pjlink.socket_abort()
# THEN: disconnect_from_host should be called
self.assertTrue(mock_disconnect.called, 'Should have called disconnect_from_host')
def test_poll_loop_not_connected(self):
"""
Test PJLink.poll_loop not connected return
"""
# GIVEN: Test object and mocks
pjlink = pjlink_test
pjlink.state = MagicMock()
pjlink.timer = MagicMock()
pjlink.state.return_value = False
pjlink.ConnectedState = True
# WHEN: PJLink.poll_loop called
pjlink.poll_loop()
# THEN: poll_loop should exit without calling any other method
self.assertFalse(pjlink.timer.called, 'Should have returned without calling any other method')
2017-12-04 00:24:47 +00:00
def test_poll_loop_start(self):
2017-08-06 07:23:26 +00:00
"""
Test PJLink.poll_loop makes correct calls
"""
2017-12-04 00:24:47 +00:00
# GIVEN: Mocks and test data
mock_state = patch.object(self.pjlink_test, 'state').start()
2017-12-25 08:44:30 +00:00
mock_state.return_value = QSOCKET_STATE[S_CONNECTED]
2017-12-04 00:24:47 +00:00
mock_timer = patch.object(self.pjlink_test, 'timer').start()
mock_timer.interval.return_value = 10
mock_send_command = patch.object(self.pjlink_test, 'send_command').start()
pjlink = self.pjlink_test
2017-08-06 07:23:26 +00:00
pjlink.poll_time = 20
pjlink.power = S_ON
pjlink.source_available = None
pjlink.other_info = None
pjlink.manufacturer = None
pjlink.model = None
pjlink.pjlink_name = None
call_list = [
2017-12-04 00:24:47 +00:00
call('POWR'),
call('ERST'),
call('LAMP'),
call('AVMT'),
call('INPT'),
call('INST'),
call('INFO'),
call('INF1'),
call('INF2'),
call('NAME'),
2017-08-06 07:23:26 +00:00
]
# WHEN: PJLink.poll_loop is called
pjlink.poll_loop()
# THEN: proper calls were made to retrieve projector data
# First, call to update the timer with the next interval
2017-12-04 00:24:47 +00:00
self.assertTrue(mock_timer.setInterval.called)
2017-08-06 07:23:26 +00:00
# Next, should have called the timer to start
2017-12-04 00:24:47 +00:00
self.assertTrue(mock_timer.start.called, 'Should have started the timer')
2017-08-06 07:23:26 +00:00
# Finally, should have called send_command with a list of projetctor status checks
mock_send_command.assert_has_calls(call_list, 'Should have queued projector queries')