~gtalent/openlp/openlp

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core/test_init.py

  • Committer: Gary Talent
  • Date: 2017-03-29 21:40:54 UTC
  • mfrom: (2695.1.35 openlp)
  • Revision ID: gtalent2@gmail.com-20170329214054-62r3rqvv3c0raagb
Update to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
###############################################################################
5
5
# OpenLP - Open Source Lyrics Projection                                      #
6
6
# --------------------------------------------------------------------------- #
7
 
# Copyright (c) 2008-2016 OpenLP Developers                                   #
 
7
# Copyright (c) 2008-2017 OpenLP Developers                                   #
8
8
# --------------------------------------------------------------------------- #
9
9
# This program is free software; you can redistribute it and/or modify it     #
10
10
# under the terms of the GNU General Public License as published by the Free  #
19
19
# with this program; if not, write to the Free Software Foundation, Inc., 59  #
20
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
21
21
###############################################################################
22
 
 
23
22
import sys
24
 
from unittest import TestCase
25
 
 
26
 
from openlp.core import parse_options
27
 
from tests.helpers.testmixin import TestMixin
28
 
 
29
 
 
30
 
class TestInitFunctions(TestMixin, TestCase):
 
23
from unittest import TestCase, skip
 
24
from unittest.mock import MagicMock, patch
 
25
 
 
26
from PyQt5 import QtWidgets
 
27
 
 
28
from openlp.core import OpenLP, parse_options
 
29
 
 
30
 
 
31
class TestInitFunctions(TestCase):
31
32
 
32
33
    def test_parse_options_basic(self):
33
34
        """
116
117
 
117
118
    def test_parse_options_file_and_debug(self):
118
119
        """
119
 
        Test the parse options process works with a file
120
 
 
 
120
        Test the parse options process works with a file and the debug log level
121
121
        """
122
122
        # GIVEN: a a set of system arguments.
123
123
        sys.argv[1:] = ['-l debug', 'dummy_temp']
130
130
        self.assertFalse(args.portable, 'The portable flag should be set to false')
131
131
        self.assertEquals(args.style, None, 'There are no style flags to be processed')
132
132
        self.assertEquals(args.rargs, 'dummy_temp', 'The service file should not be blank')
 
133
 
 
134
 
 
135
@skip('Figure out why this is causing a segfault')
 
136
class TestOpenLP(TestCase):
 
137
    """
 
138
    Test the OpenLP app class
 
139
    """
 
140
    @patch('openlp.core.QtWidgets.QApplication.exec')
 
141
    def test_exec(self, mocked_exec):
 
142
        """
 
143
        Test the exec method
 
144
        """
 
145
        # GIVEN: An app
 
146
        app = OpenLP([])
 
147
        app.shared_memory = MagicMock()
 
148
        mocked_exec.return_value = False
 
149
 
 
150
        # WHEN: exec() is called
 
151
        result = app.exec()
 
152
 
 
153
        # THEN: The right things should be called
 
154
        assert app.is_event_loop_active is True
 
155
        mocked_exec.assert_called_once_with()
 
156
        app.shared_memory.detach.assert_called_once_with()
 
157
        assert result is False
 
158
 
 
159
    @patch('openlp.core.QtCore.QSharedMemory')
 
160
    def test_is_already_running_not_running(self, MockedSharedMemory):
 
161
        """
 
162
        Test the is_already_running() method when OpenLP is NOT running
 
163
        """
 
164
        # GIVEN: An OpenLP app and some mocks
 
165
        mocked_shared_memory = MagicMock()
 
166
        mocked_shared_memory.attach.return_value = False
 
167
        MockedSharedMemory.return_value = mocked_shared_memory
 
168
        app = OpenLP([])
 
169
 
 
170
        # WHEN: is_already_running() is called
 
171
        result = app.is_already_running()
 
172
 
 
173
        # THEN: The result should be false
 
174
        MockedSharedMemory.assert_called_once_with('OpenLP')
 
175
        mocked_shared_memory.attach.assert_called_once_with()
 
176
        mocked_shared_memory.create.assert_called_once_with(1)
 
177
        assert result is False
 
178
 
 
179
    @patch('openlp.core.QtWidgets.QMessageBox.critical')
 
180
    @patch('openlp.core.QtWidgets.QMessageBox.StandardButtons')
 
181
    @patch('openlp.core.QtCore.QSharedMemory')
 
182
    def test_is_already_running_is_running_continue(self, MockedSharedMemory, MockedStandardButtons, mocked_critical):
 
183
        """
 
184
        Test the is_already_running() method when OpenLP IS running and the user chooses to continue
 
185
        """
 
186
        # GIVEN: An OpenLP app and some mocks
 
187
        mocked_shared_memory = MagicMock()
 
188
        mocked_shared_memory.attach.return_value = True
 
189
        MockedSharedMemory.return_value = mocked_shared_memory
 
190
        MockedStandardButtons.return_value = 0
 
191
        mocked_critical.return_value = QtWidgets.QMessageBox.Yes
 
192
        app = OpenLP([])
 
193
 
 
194
        # WHEN: is_already_running() is called
 
195
        result = app.is_already_running()
 
196
 
 
197
        # THEN: The result should be false
 
198
        MockedSharedMemory.assert_called_once_with('OpenLP')
 
199
        mocked_shared_memory.attach.assert_called_once_with()
 
200
        MockedStandardButtons.assert_called_once_with(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
 
201
        mocked_critical.assert_called_once_with(None, 'Error', 'OpenLP is already running. Do you wish to continue?', 0)
 
202
        assert result is False
 
203
 
 
204
    @patch('openlp.core.QtWidgets.QMessageBox.critical')
 
205
    @patch('openlp.core.QtWidgets.QMessageBox.StandardButtons')
 
206
    @patch('openlp.core.QtCore.QSharedMemory')
 
207
    def test_is_already_running_is_running_stop(self, MockedSharedMemory, MockedStandardButtons, mocked_critical):
 
208
        """
 
209
        Test the is_already_running() method when OpenLP IS running and the user chooses to stop
 
210
        """
 
211
        # GIVEN: An OpenLP app and some mocks
 
212
        mocked_shared_memory = MagicMock()
 
213
        mocked_shared_memory.attach.return_value = True
 
214
        MockedSharedMemory.return_value = mocked_shared_memory
 
215
        MockedStandardButtons.return_value = 0
 
216
        mocked_critical.return_value = QtWidgets.QMessageBox.No
 
217
        app = OpenLP([])
 
218
 
 
219
        # WHEN: is_already_running() is called
 
220
        result = app.is_already_running()
 
221
 
 
222
        # THEN: The result should be false
 
223
        MockedSharedMemory.assert_called_once_with('OpenLP')
 
224
        mocked_shared_memory.attach.assert_called_once_with()
 
225
        MockedStandardButtons.assert_called_once_with(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
 
226
        mocked_critical.assert_called_once_with(None, 'Error', 'OpenLP is already running. Do you wish to continue?', 0)
 
227
        assert result is True