Added extra validation checks and matching tests to __recalculate_layout(), row_resized(), replace_service_item() & change_slide()

This commit is contained in:
Ian Knight 2016-04-17 02:05:08 +09:30
parent 665a35b085
commit f8718ee1cb
2 changed files with 22 additions and 10 deletions

View File

@ -87,7 +87,7 @@ class ListPreviewWidget(QtWidgets.QTableWidget, RegistryProperties):
height = self.viewport().width() // self.screen_ratio
max_img_row_height = Settings().value('advanced/slide max height')
# Adjust for row height cap if in use.
if max_img_row_height > 0 and height > max_img_row_height:
if isinstance(max_img_row_height, int) and max_img_row_height > 0 and height > max_img_row_height:
height = max_img_row_height
# Apply new height to slides
for frame_number in range(len(self.service_item.get_frames())):
@ -98,7 +98,8 @@ class ListPreviewWidget(QtWidgets.QTableWidget, RegistryProperties):
Will scale non-image slides.
"""
# Only for non-text slides when row height cap in use
if self.service_item.is_text() or Settings().value('advanced/slide max height') <= 0:
max_img_row_height = Settings().value('advanced/slide max height')
if self.service_item.is_text() or not isinstance(max_img_row_height, int) or max_img_row_height <= 0:
return
# Get and validate label widget containing slide & adjust max width
try:
@ -160,9 +161,9 @@ class ListPreviewWidget(QtWidgets.QTableWidget, RegistryProperties):
pixmap.setDevicePixelRatio(label.devicePixelRatio())
label.setPixmap(pixmap)
slide_height = width // self.screen_ratio
# Setup row height cap if in use.
# Setup and validate row height cap if in use.
max_img_row_height = Settings().value('advanced/slide max height')
if max_img_row_height > 0:
if isinstance(max_img_row_height, int) and max_img_row_height > 0:
if slide_height > max_img_row_height:
slide_height = max_img_row_height
label.setMaximumWidth(max_img_row_height * self.screen_ratio)
@ -199,7 +200,8 @@ class ListPreviewWidget(QtWidgets.QTableWidget, RegistryProperties):
# Check if auto-scroll disabled (None) and validate value as dict containing 'dist' and 'pos'
# 'dist' represents the slide to scroll to relative to the new slide (-1 = previous, 0 = current, 1 = next)
# 'pos' represents the vert position of of the slide (0 = in view, 1 = top, 2 = middle, 3 = bottom)
if not isinstance(autoscrolling, dict) or 'dist' not in autoscrolling or 'pos' not in autoscrolling:
if not (isinstance(autoscrolling, dict) and 'dist' in autoscrolling and 'pos' in autoscrolling and
isinstance(autoscrolling['dist'], int) and isinstance(autoscrolling['pos'], int)):
return
# prevent scrolling past list bounds
scroll_to_slide = slide + autoscrolling['dist']

View File

@ -130,12 +130,14 @@ class TestListPreviewWidget(TestCase):
# WHEN: __recalculate_layout() is called (via resizeEvent)
list_preview_widget.resizeEvent(None)
self.mocked_Settings_obj.value.return_value = None
list_preview_widget.resizeEvent(None)
# THEN: resizeRowsToContents() should not be called, while setRowHeight() should be called
# twice for each slide.
self.assertEquals(mocked_resizeRowsToContents.call_count, 0, 'Should not be called')
self.assertEquals(mocked_setRowHeight.call_count, 4, 'Should be called twice for each slide')
calls = [call(0, 200), call(1, 200), call(0, 400), call(1, 400)]
self.assertEquals(mocked_setRowHeight.call_count, 6, 'Should be called 3 times for each slide')
calls = [call(0, 200), call(1, 200), call(0, 400), call(1, 400), call(0, 400), call(1, 400)]
mocked_setRowHeight.assert_has_calls(calls)
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.resizeRowsToContents')
@ -236,6 +238,8 @@ class TestListPreviewWidget(TestCase):
# WHEN: row_resized() is called
list_preview_widget.row_resized(0, 100, 150)
self.mocked_Settings_obj.value.return_value = None
list_preview_widget.row_resized(0, 100, 150)
# 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')
@ -295,16 +299,20 @@ class TestListPreviewWidget(TestCase):
list_preview_widget.change_slide(0)
self.mocked_Settings_obj.value.return_value = 1
list_preview_widget.change_slide(0)
self.mocked_Settings_obj.value.return_value = {'test': 1}
self.mocked_Settings_obj.value.return_value = {'fail': 1}
list_preview_widget.change_slide(0)
self.mocked_Settings_obj.value.return_value = {'dist': 1, 'test': 1}
self.mocked_Settings_obj.value.return_value = {'dist': 1, 'fail': 1}
list_preview_widget.change_slide(0)
self.mocked_Settings_obj.value.return_value = {'dist': 'fail', 'pos': 1}
list_preview_widget.change_slide(0)
self.mocked_Settings_obj.value.return_value = {'dist': 1, 'pos': 'fail'}
list_preview_widget.change_slide(0)
# THEN: no further functions should be called
self.assertEquals(mocked_slide_count.call_count, 0, 'Should not be called')
self.assertEquals(mocked_item.call_count, 0, 'Should not be called')
self.assertEquals(mocked_scrollToItem.call_count, 0, 'Should not be called')
self.assertEquals(mocked_selectRow.call_count, 0, 'Should not be called')
self.assertEquals(mocked_item.call_count, 0, 'Should not be called')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.selectRow')
@patch(u'openlp.core.ui.listpreviewwidget.ListPreviewWidget.scrollToItem')
@ -332,6 +340,7 @@ class TestListPreviewWidget(TestCase):
self.assertEquals(mocked_slide_count.call_count, 3, 'Should be called')
self.assertEquals(mocked_scrollToItem.call_count, 2, 'Should be called')
self.assertEquals(mocked_selectRow.call_count, 2, 'Should be called')
self.assertEquals(mocked_item.call_count, 2, 'Should be called')
calls = [call(0, 0), call(0, 0)]
mocked_item.assert_has_calls(calls)
@ -363,5 +372,6 @@ class TestListPreviewWidget(TestCase):
self.assertEquals(mocked_slide_count.call_count, 3, 'Should be called')
self.assertEquals(mocked_scrollToItem.call_count, 3, 'Should be called')
self.assertEquals(mocked_selectRow.call_count, 3, 'Should be called')
self.assertEquals(mocked_item.call_count, 3, 'Should be called')
calls = [call(0, 0), call(1, 0), call(2, 0)]
mocked_item.assert_has_calls(calls)