20
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
21
###############################################################################
23
This module contains tests for the openlp.core.ui.lib.pathedit module
23
This module contains tests for the openlp.core.widgets.edits module
26
26
from unittest import TestCase
27
27
from unittest.mock import MagicMock, PropertyMock, patch
29
29
from openlp.core.common.path import Path
30
from openlp.core.ui.lib import PathEdit, PathType
31
from openlp.core.ui.lib.filedialog import FileDialog
30
from openlp.core.widgets.edits import PathEdit
31
from openlp.core.widgets.enums import PathEditType
32
from openlp.core.widgets.dialogs import FileDialog
34
35
class TestPathEdit(TestCase):
36
Test the :class:`~openlp.core.lib.pathedit.PathEdit` class
37
Test the :class:`~openlp.core.widgets.edits.PathEdit` class
39
with patch('openlp.core.ui.lib.pathedit.PathEdit._setup'):
40
with patch('openlp.core.widgets.edits.PathEdit._setup'):
40
41
self.widget = PathEdit()
42
43
def test_path_getter(self):
73
74
# GIVEN: An instance of PathEdit
74
75
# WHEN: Reading the `path` property
75
76
# THEN: The default value should be returned
76
self.assertEqual(self.widget.path_type, PathType.Files)
77
self.assertEqual(self.widget.path_type, PathEditType.Files)
78
79
def test_path_type_setter(self):
83
84
with patch.object(self.widget, 'update_button_tool_tips') as mocked_update_button_tool_tips:
85
86
# WHEN: Writing to a different value than default to the `path_type` property
86
self.widget.path_type = PathType.Directories
87
self.widget.path_type = PathEditType.Directories
88
89
# THEN: The `_path_type` instance variable should be set with the test data and not the default. The
89
90
# update_button_tool_tips should have been called.
90
self.assertEqual(self.widget._path_type, PathType.Directories)
91
self.assertEqual(self.widget._path_type, PathEditType.Directories)
91
92
mocked_update_button_tool_tips.assert_called_once_with()
93
94
def test_update_button_tool_tips_directories(self):
97
98
# GIVEN: An instance of PathEdit with the `path_type` set to `Directories`
98
99
self.widget.browse_button = MagicMock()
99
100
self.widget.revert_button = MagicMock()
100
self.widget._path_type = PathType.Directories
101
self.widget._path_type = PathEditType.Directories
102
103
# WHEN: Calling update_button_tool_tips
103
104
self.widget.update_button_tool_tips()
112
113
# GIVEN: An instance of PathEdit with the `path_type` set to `Files`
113
114
self.widget.browse_button = MagicMock()
114
115
self.widget.revert_button = MagicMock()
115
self.widget._path_type = PathType.Files
116
self.widget._path_type = PathEditType.Files
117
118
# WHEN: Calling update_button_tool_tips
118
119
self.widget.update_button_tool_tips()
120
121
self.widget.browse_button.setToolTip.assert_called_once_with('Browse for file.')
121
122
self.widget.revert_button.setToolTip.assert_called_once_with('Revert to default file.')
123
def test_on_browse_button_clicked_directory(self):
124
@patch('openlp.core.widgets.edits.FileDialog.getExistingDirectory', return_value=None)
125
@patch('openlp.core.widgets.edits.FileDialog.getOpenFileName')
126
def test_on_browse_button_clicked_directory(self, mocked_get_open_file_name, mocked_get_existing_directory):
125
128
Test the `browse_button` `clicked` handler on_browse_button_clicked when the `path_type` is set to Directories.
127
130
# GIVEN: An instance of PathEdit with the `path_type` set to `Directories` and a mocked
128
131
# QFileDialog.getExistingDirectory
129
with patch('openlp.core.ui.lib.pathedit.FileDialog.getExistingDirectory', return_value=None) as \
130
mocked_get_existing_directory, \
131
patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName') as mocked_get_open_file_name:
132
self.widget._path_type = PathType.Directories
133
self.widget._path = Path('test', 'path')
135
# WHEN: Calling on_browse_button_clicked
136
self.widget.on_browse_button_clicked()
138
# THEN: The FileDialog.getExistingDirectory should have been called with the default caption
139
mocked_get_existing_directory.assert_called_once_with(self.widget, 'Select Directory',
140
Path('test', 'path'),
141
FileDialog.ShowDirsOnly)
142
self.assertFalse(mocked_get_open_file_name.called)
132
self.widget._path_type = PathEditType.Directories
133
self.widget._path = Path('test', 'path')
135
# WHEN: Calling on_browse_button_clicked
136
self.widget.on_browse_button_clicked()
138
# THEN: The FileDialog.getExistingDirectory should have been called with the default caption
139
mocked_get_existing_directory.assert_called_once_with(self.widget, 'Select Directory',
140
Path('test', 'path'),
141
FileDialog.ShowDirsOnly)
142
self.assertFalse(mocked_get_open_file_name.called)
144
144
def test_on_browse_button_clicked_directory_custom_caption(self):
149
149
# GIVEN: An instance of PathEdit with the `path_type` set to `Directories` and a mocked
150
150
# QFileDialog.getExistingDirectory with `default_caption` set.
151
with patch('openlp.core.ui.lib.pathedit.FileDialog.getExistingDirectory', return_value=None) as \
151
with patch('openlp.core.widgets.edits.FileDialog.getExistingDirectory', return_value=None) as \
152
152
mocked_get_existing_directory, \
153
patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName') as mocked_get_open_file_name:
154
self.widget._path_type = PathType.Directories
153
patch('openlp.core.widgets.edits.FileDialog.getOpenFileName') as mocked_get_open_file_name:
154
self.widget._path_type = PathEditType.Directories
155
155
self.widget._path = Path('test', 'path')
156
156
self.widget.dialog_caption = 'Directory Caption'
169
169
Test the `browse_button` `clicked` handler on_browse_button_clicked when the `path_type` is set to Files.
171
171
# GIVEN: An instance of PathEdit with the `path_type` set to `Files` and a mocked QFileDialog.getOpenFileName
172
with patch('openlp.core.ui.lib.pathedit.FileDialog.getExistingDirectory') as mocked_get_existing_directory, \
173
patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName', return_value=(None, '')) as \
172
with patch('openlp.core.widgets.edits.FileDialog.getExistingDirectory') as mocked_get_existing_directory, \
173
patch('openlp.core.widgets.edits.FileDialog.getOpenFileName', return_value=(None, '')) as \
174
174
mocked_get_open_file_name:
175
self.widget._path_type = PathType.Files
175
self.widget._path_type = PathEditType.Files
176
176
self.widget._path = Path('test', 'pat.h')
178
178
# WHEN: Calling on_browse_button_clicked
191
191
# GIVEN: An instance of PathEdit with the `path_type` set to `Files` and a mocked QFileDialog.getOpenFileName
192
192
# with `default_caption` set.
193
with patch('openlp.core.ui.lib.pathedit.FileDialog.getExistingDirectory') as mocked_get_existing_directory, \
194
patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName', return_value=(None, '')) as \
193
with patch('openlp.core.widgets.edits.FileDialog.getExistingDirectory') as mocked_get_existing_directory, \
194
patch('openlp.core.widgets.edits.FileDialog.getOpenFileName', return_value=(None, '')) as \
195
195
mocked_get_open_file_name:
196
self.widget._path_type = PathType.Files
196
self.widget._path_type = PathEditType.Files
197
197
self.widget._path = Path('test', 'pat.h')
198
198
self.widget.dialog_caption = 'File Caption'
213
213
# GIVEN: An instance of PathEdit with a mocked QFileDialog.getOpenFileName which returns an empty str for the
215
with patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName', return_value=(None, '')) as \
215
with patch('openlp.core.widgets.edits.FileDialog.getOpenFileName', return_value=(None, '')) as \
216
216
mocked_get_open_file_name:
218
218
# WHEN: Calling on_browse_button_clicked
229
229
# GIVEN: An instance of PathEdit with a mocked QFileDialog.getOpenFileName which returns a str for the file
231
with patch('openlp.core.ui.lib.pathedit.FileDialog.getOpenFileName',
231
with patch('openlp.core.widgets.edits.FileDialog.getOpenFileName',
232
232
return_value=(Path('test', 'pat.h'), '')) as mocked_get_open_file_name, \
233
233
patch.object(self.widget, 'on_new_path'):
272
272
Test `on_new_path` when called with a path that is the same as the existing path.
274
274
# GIVEN: An instance of PathEdit with a test path and mocked `pathChanged` signal
275
with patch('openlp.core.ui.lib.pathedit.PathEdit.path', new_callable=PropertyMock):
275
with patch('openlp.core.widgets.edits.PathEdit.path', new_callable=PropertyMock):
276
276
self.widget._path = Path('/old', 'test', 'pat.h')
277
277
self.widget.pathChanged = MagicMock()
287
287
Test `on_new_path` when called with a path that is the different to the existing path.
289
289
# GIVEN: An instance of PathEdit with a test path and mocked `pathChanged` signal
290
with patch('openlp.core.ui.lib.pathedit.PathEdit.path', new_callable=PropertyMock):
290
with patch('openlp.core.widgets.edits.PathEdit.path', new_callable=PropertyMock):
291
291
self.widget._path = Path('/old', 'test', 'pat.h')
292
292
self.widget.pathChanged = MagicMock()