Fix bug #1067251: Correct focus of theme manager's dialogs

- Fix a bug where the line edit in certain dialogs was not focused on show.
- Add a test

bzr-revno: 2177
Fixes: https://launchpad.net/bugs/1067251
This commit is contained in:
Philip Ridout 2013-02-15 21:24:01 +02:00 committed by Raoul Snyman
commit 53beeed257
3 changed files with 98 additions and 4 deletions

View File

@ -34,18 +34,18 @@ from PyQt4 import QtGui
from filerenamedialog import Ui_FileRenameDialog
from openlp.core.lib import translate
from openlp.core.lib import translate, Registry
class FileRenameForm(QtGui.QDialog, Ui_FileRenameDialog):
"""
The file rename dialog
"""
def __init__(self, parent):
def __init__(self):
"""
Constructor
"""
QtGui.QDialog.__init__(self, parent)
QtGui.QDialog.__init__(self, self.main_window)
self.setupUi(self)
def exec_(self, copy=False):
@ -56,4 +56,15 @@ class FileRenameForm(QtGui.QDialog, Ui_FileRenameDialog):
self.setWindowTitle(translate('OpenLP.FileRenameForm', 'File Copy'))
else:
self.setWindowTitle(translate('OpenLP.FileRenameForm', 'File Rename'))
self.fileNameEdit.setFocus()
return QtGui.QDialog.exec_(self)
def _get_main_window(self):
"""
Adds the main window to the class dynamically
"""
if not hasattr(self, u'_main_window'):
self._main_window = Registry().get(u'main_window')
return self._main_window
main_window = property(_get_main_window)

View File

@ -62,7 +62,7 @@ class ThemeManager(QtGui.QWidget):
Registry().register(u'theme_manager', self)
self.settingsSection = u'themes'
self.themeForm = ThemeForm(self)
self.fileRenameForm = FileRenameForm(self)
self.fileRenameForm = FileRenameForm()
# start with the layout
self.layout = QtGui.QVBoxLayout(self)
self.layout.setSpacing(0)

View File

@ -0,0 +1,83 @@
"""
Package to test the openlp.core.ui package.
"""
from unittest import TestCase
from mock import MagicMock, patch
from openlp.core.lib import Registry
from openlp.core.ui import filerenameform
from PyQt4 import QtGui, QtTest
class TestStartFileRenameForm(TestCase):
def setUp(self):
"""
Create the UI
"""
registry = Registry.create()
self.app = QtGui.QApplication([])
self.main_window = QtGui.QMainWindow()
Registry().register(u'main_window', self.main_window)
self.form = filerenameform.FileRenameForm()
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
del self.form
del self.main_window
del self.app
def window_title_test(self):
"""
Test the windowTitle of the FileRenameDialog
"""
# GIVEN: A mocked QDialog.exec_() method
with patch(u'PyQt4.QtGui.QDialog.exec_') as mocked_exec:
# WHEN: The form is executed with no args
self.form.exec_()
# THEN: the window title is set correctly
self.assertEqual(self.form.windowTitle(), u'File Rename', u'The window title should be "File Rename"')
# WHEN: The form is executed with False arg
self.form.exec_(False)
# THEN: the window title is set correctly
self.assertEqual(self.form.windowTitle(), u'File Rename', u'The window title should be "File Rename"')
# WHEN: The form is executed with True arg
self.form.exec_(True)
# THEN: the window title is set correctly
self.assertEqual(self.form.windowTitle(), u'File Copy', u'The window title should be "File Copy"')
def line_edit_focus_test(self):
"""
Regression test for bug1067251
Test that the fileNameEdit setFocus has called with True when executed
"""
# GIVEN: A mocked QDialog.exec_() method and mocked fileNameEdit.setFocus() method.
with patch(u'PyQt4.QtGui.QDialog.exec_') as mocked_exec:
mocked_set_focus = MagicMock()
self.form.fileNameEdit.setFocus = mocked_set_focus
# WHEN: The form is executed
self.form.exec_()
# THEN: the setFocus method of the fileNameEdit has been called with True
mocked_set_focus.assert_called_with()
def file_name_validation_test(self):
"""
Test the fileNameEdit validation
"""
# GIVEN: QLineEdit with a validator set with illegal file name characters.
# WHEN: 'Typing' a string containing invalid file characters.
QtTest.QTest.keyClicks(self.form.fileNameEdit, u'I/n\\v?a*l|i<d> \F[i\l]e" :N+a%me')
# THEN: The text in the QLineEdit should be the same as the input string with the invalid chatacters filtered
# out.
self.assertEqual(self.form.fileNameEdit.text(), u'Invalid File Name')