Exemplo n.º 1
0
class TestSearchEdit(TestCase, TestMixin):
    """
    Test the EditCustomForm.
    """
    def setUp(self):
        """
        Create the UI
        """
        Registry.create()
        self.setup_application()
        self.main_window = QtGui.QMainWindow()
        Registry().register('main_window', self.main_window)

        self.search_edit = SearchEdit(self.main_window)
        # 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
        """
        del self.search_edit
        del self.main_window

    def set_search_types_test(self):
        """
        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:

        # THEN: The first search type should be the first one in the list.
        assert self.search_edit.current_search_type() == SearchTypes.First, "The first search type should be selected."

    def set_current_search_type_test(self):
        """
        Test if changing the search type works.
        """
        # GIVEN:
        # WHEN: Change the search type
        result = self.search_edit.set_current_search_type(SearchTypes.Second)

        # THEN:
        assert result, "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'."

    def clear_button_visibility_test(self):
        """
        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."

    def press_clear_button_test(self):
        """
        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."
Exemplo n.º 2
0
class TestSearchEdit(TestCase, TestMixin):
    """
    Test the EditCustomForm.
    """
    def setUp(self):
        """
        Create the UI
        """
        Registry.create()
        self.setup_application()
        self.main_window = QtWidgets.QMainWindow()
        Registry().register('main_window', self.main_window)

        settings_patcher = patch(
            'openlp.core.lib.searchedit.Settings', return_value=MagicMock(**{'value.return_value': SearchTypes.First}))
        self.addCleanup(settings_patcher.stop)
        self.mocked_settings = settings_patcher.start()

        self.search_edit = SearchEdit(self.main_window, 'settings_section')
        # 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
        """
        del self.search_edit
        del self.main_window

    def test_set_search_types(self):
        """
        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:

        # THEN: The first search type should be the first one in the list. The selected type should be saved in the
        #       settings
        self.assertEqual(self.search_edit.current_search_type(), SearchTypes.First,
                         "The first search type should be selected.")
        self.mocked_settings().setValue.assert_called_once_with('settings_section/last search type', 0)

    def test_set_current_search_type(self):
        """
        Test if changing the search type works.
        """
        # GIVEN:
        # WHEN: Change the search type
        result = self.search_edit.set_current_search_type(SearchTypes.Second)

        # THEN:
        self.assertTrue(result, "The call should return success (True).")
        self.assertEqual(self.search_edit.current_search_type(), SearchTypes.Second,
                         "The search type should be SearchTypes.Second")
        self.assertEqual(self.search_edit.placeholderText(), SECOND_PLACEHOLDER_TEXT,
                         "The correct placeholder text should be 'Second Placeholder Text'.")
        self.mocked_settings().setValue.assert_has_calls(
            [call('settings_section/last search type', 0), call('settings_section/last search type', 1)])

    def test_clear_button_visibility(self):
        """
        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."

    def test_press_clear_button(self):
        """
        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."