Shifted common test code into setup.

This commit is contained in:
Ian Knight 2016-03-24 00:44:37 +10:30
parent a98e62ed5d
commit 1667363abd

View File

@ -37,11 +37,28 @@ class TestListPreviewWidget(TestCase):
"""
Mock out stuff for all the tests
"""
# Mock self.parent().width()
self.parent_patcher = patch('openlp.core.ui.listpreviewwidget.ListPreviewWidget.parent')
self.mocked_parent = self.parent_patcher.start()
self.mocked_parent.width.return_value = 100
self.addCleanup(self.parent_patcher.stop)
# Mock Settings().value()
self.Settings_patcher = patch('openlp.core.ui.listpreviewwidget.Settings')
self.mocked_Settings = self.Settings_patcher.start()
self.mocked_Settings_obj = MagicMock()
self.mocked_Settings_obj.value.return_value = None
self.mocked_Settings.return_value = self.mocked_Settings_obj
self.addCleanup(self.Settings_patcher.stop)
# Mock self.viewport().width()
self.viewport_patcher = patch('openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
self.mocked_viewport = self.viewport_patcher.start()
self.mocked_viewport_obj = MagicMock()
self.mocked_viewport_obj.width.return_value = 200
self.mocked_viewport.return_value = self.mocked_viewport_obj
self.addCleanup(self.viewport_patcher.stop)
def new_list_preview_widget_test(self):
"""
Test that creating an instance of ListPreviewWidget works
@ -55,12 +72,9 @@ class TestListPreviewWidget(TestCase):
self.assertIsNotNone(list_preview_widget, 'The ListPreviewWidget object should not be None')
self.assertEquals(list_preview_widget.screen_ratio, 1, 'Should not be called')
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
def replace_recalculate_layout_test_text(self, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def replace_recalculate_layout_test_text(self, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." enabled, txt slides unchanged in replace_service_item & __recalc...
"""
@ -68,13 +82,9 @@ class TestListPreviewWidget(TestCase):
# a text ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 100
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 100
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock text service item
service_item = MagicMock()
service_item.is_text.return_value = True
@ -84,7 +94,7 @@ class TestListPreviewWidget(TestCase):
list_preview_widget = ListPreviewWidget(None, 1)
list_preview_widget.replace_service_item(service_item, 200, 0)
# Change viewport width before forcing a resize
mocked_viewport_obj.width.return_value = 400
self.mocked_viewport_obj.width.return_value = 400
# WHEN: __recalculate_layout() is called (via resizeEvent)
list_preview_widget.resizeEvent(None)
@ -94,12 +104,9 @@ class TestListPreviewWidget(TestCase):
self.assertEquals(mocked_resizeRowsToContents.call_count, 2, 'Should be called')
self.assertEquals(mocked_setRowHeight.call_count, 0, 'Should not be called')
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
def replace_recalculate_layout_test_img(self, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def replace_recalculate_layout_test_img(self, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." disabled, img slides unchanged in replace_service_item & __recalc...
"""
@ -107,13 +114,9 @@ class TestListPreviewWidget(TestCase):
# an image ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 0
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 0
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock image service item
service_item = MagicMock()
service_item.is_text.return_value = False
@ -123,7 +126,7 @@ class TestListPreviewWidget(TestCase):
list_preview_widget = ListPreviewWidget(None, 1)
list_preview_widget.replace_service_item(service_item, 200, 0)
# Change viewport width before forcing a resize
mocked_viewport_obj.width.return_value = 400
self.mocked_viewport_obj.width.return_value = 400
# WHEN: __recalculate_layout() is called (via resizeEvent)
list_preview_widget.resizeEvent(None)
@ -135,26 +138,19 @@ class TestListPreviewWidget(TestCase):
calls = [call(0, 200), call(1, 200), call(0, 400), call(1, 400)]
mocked_setRowHeight.assert_has_calls(calls)
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
def replace_recalculate_layout_test_img_max(self, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def replace_recalculate_layout_test_img_max(self, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." enabled, img slides resized in replace_service_item & __recalc...
"""
# GIVEN: A setting to adjust "Max height for non-text slides in slide controller",
# an image ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 100
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 100
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock image service item
service_item = MagicMock()
service_item.is_text.return_value = False
@ -164,7 +160,7 @@ class TestListPreviewWidget(TestCase):
list_preview_widget = ListPreviewWidget(None, 1)
list_preview_widget.replace_service_item(service_item, 200, 0)
# Change viewport width before forcing a resize
mocked_viewport_obj.width.return_value = 400
self.mocked_viewport_obj.width.return_value = 400
# WHEN: __recalculate_layout() is called (via resizeEvent)
list_preview_widget.resizeEvent(None)
@ -176,13 +172,10 @@ class TestListPreviewWidget(TestCase):
calls = [call(0, 100), call(1, 100), call(0, 100), call(1, 100)]
mocked_setRowHeight.assert_has_calls(calls)
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.cellWidget')
def row_resized_test_text(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def row_resized_test_text(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." enabled, text-based slides not affected in row_resized.
"""
@ -190,13 +183,9 @@ class TestListPreviewWidget(TestCase):
# a text ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 100
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 100
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock text service item
service_item = MagicMock()
service_item.is_text.return_value = True
@ -217,13 +206,10 @@ class TestListPreviewWidget(TestCase):
# THEN: self.cellWidget(row, 0).children()[1].setMaximumWidth() should not be called
self.assertEquals(mocked_cellWidget_child.setMaximumWidth.call_count, 0, 'Should not be called')
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.cellWidget')
def row_resized_test_img(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def row_resized_test_img(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." disabled, image-based slides not affected in row_resized.
"""
@ -231,13 +217,9 @@ class TestListPreviewWidget(TestCase):
# an image ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 0
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 0
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock image service item
service_item = MagicMock()
service_item.is_text.return_value = False
@ -258,13 +240,10 @@ class TestListPreviewWidget(TestCase):
# THEN: self.cellWidget(row, 0).children()[1].setMaximumWidth() should not be called
self.assertEquals(mocked_cellWidget_child.setMaximumWidth.call_count, 0, 'Should not be called')
@patch(u'openlp.core.ui.listpreviewwidget.Settings')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.viewport')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.setRowHeight')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.cellWidget')
def row_resized_test_img_max(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents,
mocked_viewport, mocked_Settings):
def row_resized_test_img_max(self, mocked_cellWidget, mocked_setRowHeight, mocked_resizeRowsToContents):
"""
Test if "Max height for non-text slides..." enabled, image-based slides are scaled in row_resized.
"""
@ -272,13 +251,9 @@ class TestListPreviewWidget(TestCase):
# an image ServiceItem and a ListPreviewWidget.
# Mock Settings().value('advanced/slide max height')
mocked_Settings_obj = MagicMock()
mocked_Settings_obj.value.return_value = 100
mocked_Settings.return_value = mocked_Settings_obj
self.mocked_Settings_obj.value.return_value = 100
# Mock self.viewport().width()
mocked_viewport_obj = MagicMock()
mocked_viewport_obj.width.return_value = 200
mocked_viewport.return_value = mocked_viewport_obj
self.mocked_viewport_obj.width.return_value = 200
# Mock image service item
service_item = MagicMock()
service_item.is_text.return_value = False