2014-03-23 10:49:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2017-12-29 09:15:48 +00:00
|
|
|
# Copyright (c) 2008-2018 OpenLP Developers #
|
2014-03-23 10:49:03 +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; 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
Module to test the EditCustomForm.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2016-11-11 21:23:03 +00:00
|
|
|
from unittest.mock import MagicMock, call, patch
|
2014-03-23 10:49:03 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore, QtGui, QtTest, QtWidgets
|
2014-03-23 10:49:03 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
2017-10-23 22:09:57 +00:00
|
|
|
from openlp.core.widgets.edits import SearchEdit, HistoryComboBox
|
2014-03-23 10:49:03 +00:00
|
|
|
from tests.helpers.testmixin import TestMixin
|
|
|
|
|
|
|
|
|
|
|
|
class SearchTypes(object):
|
2014-11-24 21:18:32 +00:00
|
|
|
"""
|
|
|
|
Types of search
|
|
|
|
"""
|
2014-03-23 10:49:03 +00:00
|
|
|
First = 0
|
|
|
|
Second = 1
|
|
|
|
|
|
|
|
|
|
|
|
SECOND_PLACEHOLDER_TEXT = "Second Placeholder Text"
|
|
|
|
SEARCH_TYPES = [(SearchTypes.First, QtGui.QIcon(), "First", "First Placeholder Text"),
|
|
|
|
(SearchTypes.Second, QtGui.QIcon(), "Second", SECOND_PLACEHOLDER_TEXT)]
|
|
|
|
|
|
|
|
|
|
|
|
class TestSearchEdit(TestCase, TestMixin):
|
|
|
|
"""
|
|
|
|
Test the EditCustomForm.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create the UI
|
|
|
|
"""
|
|
|
|
Registry.create()
|
2014-10-22 20:43:05 +00:00
|
|
|
self.setup_application()
|
2015-11-07 00:49:40 +00:00
|
|
|
self.main_window = QtWidgets.QMainWindow()
|
2014-03-23 10:49:03 +00:00
|
|
|
Registry().register('main_window', self.main_window)
|
|
|
|
|
2016-11-11 21:23:03 +00:00
|
|
|
settings_patcher = patch(
|
2017-10-23 22:09:57 +00:00
|
|
|
'openlp.core.widgets.edits.Settings', return_value=MagicMock(**{'value.return_value': SearchTypes.First}))
|
2016-11-11 21:23:03 +00:00
|
|
|
self.addCleanup(settings_patcher.stop)
|
|
|
|
self.mocked_settings = settings_patcher.start()
|
|
|
|
|
2016-11-11 21:28:11 +00:00
|
|
|
self.search_edit = SearchEdit(self.main_window, 'settings_section')
|
2014-03-23 10:49:03 +00:00
|
|
|
# To complete set up we have to set the search types.
|
|
|
|
self.search_edit.set_search_types(SEARCH_TYPES)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Delete all the C++ objects at the end so that we don't have a segfault
|
|
|
|
"""
|
2014-11-26 23:34:48 +00:00
|
|
|
del self.search_edit
|
|
|
|
del self.main_window
|
2014-03-23 10:49:03 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_set_search_types(self):
|
2014-03-23 10:49:03 +00:00
|
|
|
"""
|
|
|
|
Test setting the search types of the search edit.
|
|
|
|
"""
|
|
|
|
# GIVEN: The search edit with the search types set. NOTE: The set_search_types(types) is called in the setUp()
|
|
|
|
# method!
|
|
|
|
|
|
|
|
# WHEN:
|
|
|
|
|
2016-11-11 21:23:03 +00:00
|
|
|
# THEN: The first search type should be the first one in the list. The selected type should be saved in the
|
|
|
|
# settings
|
2017-12-23 09:09:45 +00:00
|
|
|
assert self.search_edit.current_search_type() == SearchTypes.First, \
|
|
|
|
"The first search type should be selected."
|
2017-06-04 09:52:15 +00:00
|
|
|
self.mocked_settings().setValue.assert_called_once_with('settings_section/last used search type', 0)
|
2014-03-23 10:49:03 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_set_current_search_type(self):
|
2014-03-23 10:49:03 +00:00
|
|
|
"""
|
|
|
|
Test if changing the search type works.
|
|
|
|
"""
|
|
|
|
# GIVEN:
|
|
|
|
# WHEN: Change the search type
|
|
|
|
result = self.search_edit.set_current_search_type(SearchTypes.Second)
|
|
|
|
|
|
|
|
# THEN:
|
2017-12-23 09:09:45 +00:00
|
|
|
assert result is True, "The call should return success (True)."
|
|
|
|
assert self.search_edit.current_search_type() == SearchTypes.Second, \
|
|
|
|
"The search type should be SearchTypes.Second"
|
|
|
|
assert self.search_edit.placeholderText() == SECOND_PLACEHOLDER_TEXT, \
|
|
|
|
"The correct placeholder text should be 'Second Placeholder Text'."
|
2016-11-11 21:23:03 +00:00
|
|
|
self.mocked_settings().setValue.assert_has_calls(
|
2017-06-04 09:52:15 +00:00
|
|
|
[call('settings_section/last used search type', 0), call('settings_section/last used search type', 1)])
|
2014-03-23 10:49:03 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_clear_button_visibility(self):
|
2014-03-23 10:49:03 +00:00
|
|
|
"""
|
|
|
|
Test if the clear button is hidden/shown correctly.
|
|
|
|
"""
|
|
|
|
# GIVEN: Everything is left to its defaults (hidden).
|
|
|
|
assert self.search_edit.clear_button.isHidden(), "Pre condition not met. Button should be hidden."
|
|
|
|
|
|
|
|
# WHEN: Type something in the search edit.
|
|
|
|
QtTest.QTest.keyPress(self.search_edit, QtCore.Qt.Key_A)
|
|
|
|
QtTest.QTest.keyRelease(self.search_edit, QtCore.Qt.Key_A)
|
|
|
|
|
|
|
|
# THEN: The clear button should not be hidden any more.
|
|
|
|
assert not self.search_edit.clear_button.isHidden(), "The clear button should be visible."
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_press_clear_button(self):
|
2014-03-23 10:49:03 +00:00
|
|
|
"""
|
|
|
|
Check if the search edit behaves correctly when pressing the clear button.
|
|
|
|
"""
|
|
|
|
# GIVEN: A search edit with text.
|
|
|
|
QtTest.QTest.keyPress(self.search_edit, QtCore.Qt.Key_A)
|
|
|
|
QtTest.QTest.keyRelease(self.search_edit, QtCore.Qt.Key_A)
|
|
|
|
|
|
|
|
# WHEN: Press the clear button.
|
|
|
|
QtTest.QTest.mouseClick(self.search_edit.clear_button, QtCore.Qt.LeftButton)
|
|
|
|
|
|
|
|
# THEN: The search edit text should be cleared and the button be hidden.
|
|
|
|
assert not self.search_edit.text(), "The search edit should not have any text."
|
|
|
|
assert self.search_edit.clear_button.isHidden(), "The clear button should be hidden."
|
2017-10-23 22:09:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestHistoryComboBox(TestCase, TestMixin):
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Some pre-test setup required.
|
|
|
|
"""
|
|
|
|
Registry.create()
|
|
|
|
self.setup_application()
|
|
|
|
self.main_window = QtWidgets.QMainWindow()
|
|
|
|
Registry().register('main_window', self.main_window)
|
|
|
|
self.combo = HistoryComboBox(self.main_window)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Delete all the C++ objects at the end so that we don't have a segfault
|
|
|
|
"""
|
|
|
|
del self.combo
|
|
|
|
del self.main_window
|
|
|
|
|
|
|
|
def test_get_items(self):
|
|
|
|
"""
|
|
|
|
Test the getItems() method
|
|
|
|
"""
|
|
|
|
# GIVEN: The combo.
|
|
|
|
|
|
|
|
# WHEN: Add two items.
|
|
|
|
self.combo.addItem('test1')
|
|
|
|
self.combo.addItem('test2')
|
|
|
|
|
|
|
|
# THEN: The list of items should contain both strings.
|
2017-12-23 09:09:45 +00:00
|
|
|
assert self.combo.getItems() == ['test1', 'test2']
|