~raoul-snyman/openlp/dont-test-uno-on-macos

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core/widgets/test_edits.py

  • Committer: Raoul Snyman
  • Date: 2017-10-25 21:18:38 UTC
  • mfrom: (2780.1.2 refactorings-3)
  • Revision ID: raoul@snyman.info-20171025211838-be4wutkihlytvkk3
Merge in refactorings-3

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
21
21
###############################################################################
22
22
"""
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
24
24
"""
25
25
import os
26
26
from unittest import TestCase
27
27
from unittest.mock import MagicMock, PropertyMock, patch
28
28
 
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
32
33
 
33
34
 
34
35
class TestPathEdit(TestCase):
35
36
    """
36
 
    Test the :class:`~openlp.core.lib.pathedit.PathEdit` class
 
37
    Test the :class:`~openlp.core.widgets.edits.PathEdit` class
37
38
    """
38
39
    def setUp(self):
39
 
        with patch('openlp.core.ui.lib.pathedit.PathEdit._setup'):
 
40
        with patch('openlp.core.widgets.edits.PathEdit._setup'):
40
41
            self.widget = PathEdit()
41
42
 
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)
77
78
 
78
79
    def test_path_type_setter(self):
79
80
        """
83
84
        with patch.object(self.widget, 'update_button_tool_tips') as mocked_update_button_tool_tips:
84
85
 
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
87
88
 
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()
92
93
 
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
101
102
 
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
116
117
 
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.')
122
123
 
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):
124
127
        """
125
128
        Test the `browse_button` `clicked` handler on_browse_button_clicked when the `path_type` is set to Directories.
126
129
        """
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')
134
 
 
135
 
            # WHEN: Calling on_browse_button_clicked
136
 
            self.widget.on_browse_button_clicked()
137
 
 
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')
 
134
 
 
135
        # WHEN: Calling on_browse_button_clicked
 
136
        self.widget.on_browse_button_clicked()
 
137
 
 
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)
143
143
 
144
144
    def test_on_browse_button_clicked_directory_custom_caption(self):
145
145
        """
148
148
        """
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'
157
157
 
169
169
        Test the `browse_button` `clicked` handler on_browse_button_clicked when the `path_type` is set to Files.
170
170
        """
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')
177
177
 
178
178
            # WHEN: Calling on_browse_button_clicked
190
190
        """
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'
199
199
 
212
212
        """
213
213
        # GIVEN: An instance of PathEdit with a mocked QFileDialog.getOpenFileName which returns an empty str for the
214
214
        #        file path.
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:
217
217
 
218
218
            # WHEN: Calling on_browse_button_clicked
228
228
        """
229
229
        # GIVEN: An instance of PathEdit with a mocked QFileDialog.getOpenFileName which returns a str for the file
230
230
        #        path.
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'):
234
234
 
272
272
        Test `on_new_path` when called with a path that is the same as the existing path.
273
273
        """
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()
278
278
 
287
287
        Test `on_new_path` when called with a path that is the different to the existing path.
288
288
        """
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()
293
293