~fmos/zim/features

« back to all changes in this revision

Viewing changes to tests/gui.py

  • Committer: Fabian Moser
  • Date: 2011-01-08 18:26:21 UTC
  • mfrom: (297.1.32 pyzim-trunk)
  • Revision ID: e-mail@fabianmoser.at-20110108182621-5k8ymh4s9b7lnfkf
Merged main branch and updated sidepane registration for the tag tree plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from tests import TestCase, get_test_notebook, create_tmp_dir, MockObject
 
3
 
 
4
from zim.errors import Error
 
5
from zim.notebook import Path
 
6
from zim.fs import File, Dir
 
7
 
 
8
import zim.gui
 
9
 
 
10
 
 
11
 
 
12
class TestDialogs(TestCase):
 
13
 
 
14
        slowTest = True
 
15
 
 
16
        def runTest(self):
 
17
                '''Test input/output of various dialogs'''
 
18
                tmp_dir = create_tmp_dir('gui_testdialogs')
 
19
                ui = MockUI('Test:foo:bar')
 
20
 
 
21
                ## Jump To Page dialog
 
22
                for name, path in (
 
23
                        (':foo', ':foo'),
 
24
                        ('foo', ':Test:foo'),
 
25
                        ('baz', ':Test:foo:baz'),
 
26
                        ('+baz', ':Test:foo:bar:baz'),
 
27
                ):
 
28
                        dialog = zim.gui.OpenPageDialog(ui)
 
29
                        dialog.form.widgets['page'].set_text(name)
 
30
                        dialog.assert_response_ok()
 
31
                        self.assertEqual(ui.mock_calls[-1], ('open_page', Path(path)))
 
32
 
 
33
                ## New Page dialog
 
34
                for name, path in (
 
35
                        (':new', ':new'),
 
36
                        ('foo:new', ':Test:foo:new'),
 
37
                        ('new', ':Test:foo:new'),
 
38
                        ('+new', ':Test:foo:bar:new'),
 
39
                ):
 
40
                        dialog = zim.gui.NewPageDialog(ui)
 
41
                        dialog.form.widgets['page'].set_text(name)
 
42
                        dialog.assert_response_ok()
 
43
                        self.assertEqual(ui.mock_calls[-1], ('save_page',))
 
44
                        self.assertEqual(ui.mock_calls[-2], ('open_page', Path(path)))
 
45
                        page = ui.notebook.get_page(Path(path))
 
46
                        self.assertTrue(page.exists())
 
47
                        page.modified = False # HACK so we can clean up
 
48
                        ui.notebook.delete_page(page)
 
49
 
 
50
                for name, path in (
 
51
                        (':new', ':Test:foo:bar:new'),
 
52
                        ('foo:new', ':Test:foo:bar:foo:new'),
 
53
                        ('new', ':Test:foo:bar:new'),
 
54
                        ('+new', ':Test:foo:bar:new'),
 
55
                ):
 
56
                        dialog = zim.gui.NewPageDialog(ui, subpage=True)
 
57
                        dialog.form.widgets['page'].set_text(name)
 
58
                        dialog.assert_response_ok()
 
59
                        self.assertEqual(ui.mock_calls[-1], ('save_page',))
 
60
                        self.assertEqual(ui.mock_calls[-2], ('open_page', Path(path)))
 
61
                        page = ui.notebook.get_page(Path(path))
 
62
                        self.assertTrue(page.exists())
 
63
                        page.modified = False # HACK so we can clean up
 
64
                        ui.notebook.delete_page(page)
 
65
 
 
66
                dialog = zim.gui.NewPageDialog(ui)
 
67
                dialog.form.widgets['page'].set_text(':Test:foo')
 
68
                self.assertRaises(Error, dialog.assert_response_ok)
 
69
 
 
70
                ## Save Copy dialog
 
71
                file = File((tmp_dir, 'save_copy.txt'))
 
72
                self.assertFalse(file.exists())
 
73
                dialog = zim.gui.SaveCopyDialog(ui)
 
74
                dialog.set_file(file)
 
75
                #~ dialog.assert_response_ok()
 
76
                #~ self.assertTrue(file.exists())
 
77
 
 
78
                ## Import Page dialog
 
79
                file = File((tmp_dir, 'import_page.txt'))
 
80
                file.write('test 123\n')
 
81
                self.assertTrue(file.exists())
 
82
                ui = MockUI()
 
83
                dialog = zim.gui.ImportPageDialog(ui)
 
84
                dialog.set_file(file)
 
85
                #~ dialog.assert_response_ok()
 
86
                #~ self.assertEqual(ui.mock_calls[-1], ('open_page', Path(':import_page')))
 
87
                #~ self.assertTrue(ui.notebook.get_page(':import_page').exists())
 
88
 
 
89
                ## Move Page dialog
 
90
                # Can't test much here except for the call to do_move_page
 
91
                ui.mock_method('do_move_page', True)
 
92
 
 
93
                dialog = zim.gui.MovePageDialog(ui, path=Path('Test:foo:bar'))
 
94
                self.assertTrue(dialog.form['update'])
 
95
                self.assertTrue(dialog.form.widgets['update'].get_sensitive())
 
96
                dialog.form['parent'] = Path('New')
 
97
                dialog.assert_response_ok()
 
98
                self.assertEqual(ui.mock_calls[-1],
 
99
                        ('do_move_page', Path('Test:foo:bar'), Path('New:bar'), True))
 
100
 
 
101
                dialog = zim.gui.MovePageDialog(ui, path=Path('New:bar'))
 
102
                self.assertFalse(dialog.form['update'])
 
103
                self.assertFalse(dialog.form.widgets['update'].get_sensitive())
 
104
                dialog.form['parent'] = Path('foo')
 
105
                dialog.assert_response_ok()
 
106
                self.assertEqual(ui.mock_calls[-1],
 
107
                        ('do_move_page', Path('New:bar'), Path('foo:bar'), False))
 
108
 
 
109
                ## Rename Page dialog
 
110
                # Can't test much here except for the call to do_rename_page
 
111
                ui.mock_method('do_rename_page', True)
 
112
 
 
113
                dialog = zim.gui.RenamePageDialog(ui, path=Path('Test:foo:bar'))
 
114
                self.assertTrue(dialog.form['update'])
 
115
                self.assertTrue(dialog.form.widgets['update'].get_sensitive())
 
116
                self.assertTrue(dialog.form['head'])
 
117
                self.assertTrue(dialog.form.widgets['head'].get_sensitive())
 
118
                dialog.form['name'] = 'New'
 
119
                dialog.assert_response_ok()
 
120
                self.assertEqual(ui.mock_calls[-1],
 
121
                        ('do_rename_page', Path('Test:foo:bar'), 'New', True, True))
 
122
 
 
123
                dialog = zim.gui.RenamePageDialog(ui, path=Path('New:bar'))
 
124
                self.assertFalse(dialog.form['update'])
 
125
                self.assertFalse(dialog.form.widgets['update'].get_sensitive())
 
126
                self.assertFalse(dialog.form['head'])
 
127
                self.assertFalse(dialog.form.widgets['head'].get_sensitive())
 
128
                dialog.form['name'] = 'New'
 
129
                dialog.assert_response_ok()
 
130
                self.assertEqual(ui.mock_calls[-1],
 
131
                        ('do_rename_page', Path('New:bar'), 'New', False, False))
 
132
 
 
133
                ## Delete Page dialog
 
134
                # just check inputs are OK - skip output
 
135
                dialog = zim.gui.DeletePageDialog(ui, path=Path('Test:foo:bar'))
 
136
                self.assertTrue(dialog.links_checkbox.get_active())
 
137
                self.assertTrue(dialog.links_checkbox.get_sensitive())
 
138
 
 
139
                dialog = zim.gui.DeletePageDialog(ui, path=Path('New'))
 
140
                self.assertFalse(dialog.links_checkbox.get_active())
 
141
                self.assertFalse(dialog.links_checkbox.get_sensitive())
 
142
 
 
143
                dialog.destroy()
 
144
 
 
145
                ## Attach File dialog
 
146
                file = File((tmp_dir, 'file_to_be_attached'))
 
147
                file.write('Test 1 2 3\n')
 
148
                newfile = File((tmp_dir, 'attachments', 'Test', 'foo', 'file_to_be_attached'))
 
149
                self.assertTrue(file.exists())
 
150
                self.assertFalse(newfile.exists())
 
151
 
 
152
                store = ui.notebook.get_store(Path(':'))
 
153
                store.dir = Dir((tmp_dir, 'attachments')) # Fake dir based notebook
 
154
                dialog = zim.gui.AttachFileDialog(ui, path=Path('Test:foo'))
 
155
                dialog.set_file(file)
 
156
                #~ dialog.assert_response_ok()
 
157
 
 
158
                #~ self.assertTrue(file.exists()) # No move or delete happened
 
159
                #~ self.assertTrue(newfile.exists())
 
160
                #~ self.assertTrue(newfile.compare(file))
 
161
                del store.dir
 
162
 
 
163
                ## Search dialog
 
164
                from zim.gui.searchdialog import SearchDialog
 
165
                ui = MockUI()
 
166
                ui.notebook = get_test_notebook()
 
167
                dialog = SearchDialog(ui)
 
168
                dialog.query_entry.set_text('Foo')
 
169
                dialog.query_entry.activate()
 
170
                model = dialog.results_treeview.get_model()
 
171
                self.assertTrue(len(model) > 3)
 
172
 
 
173
                ui.mainwindow = MockObject()
 
174
                ui.mainwindow.pageview = MockObject()
 
175
                col = dialog.results_treeview.get_column(0)
 
176
                dialog.results_treeview.row_activated((0,), col)
 
177
 
 
178
                ## New Application dialog
 
179
                from zim.gui.applications import NewApplicationDialog
 
180
                ui = MockUI()
 
181
                dialog = NewApplicationDialog(ui, mimetype='text/plain')
 
182
                dialog.form['name'] = 'Foo'
 
183
                dialog.form['exec'] = 'foo %f'
 
184
                app = dialog.assert_response_ok()
 
185
                self.assertEqual(app.name, 'Foo')
 
186
 
 
187
                dialog = NewApplicationDialog(ui, type='web_browser')
 
188
                dialog.form['name'] = 'Foo'
 
189
                dialog.form['exec'] = 'foo %f'
 
190
                app = dialog.assert_response_ok()
 
191
                self.assertEqual(app.name, 'Foo')
 
192
 
 
193
                ## Custom tool dialog
 
194
                from zim.gui.customtools import CustomToolManagerDialog
 
195
                dialog = CustomToolManagerDialog(ui)
 
196
                properties = {
 
197
                        'Name': 'Foo',
 
198
                        'Comment': 'Test Foo',
 
199
                        'X-Zim-ExecTool': 'foo %u',
 
200
                        'X-Zim-ReadOnly': False,
 
201
                        'X-Zim-ShowInToolBar': False,
 
202
 
 
203
                }
 
204
                dialog.manager.create(**properties)
 
205
                dialog.listview.refresh()
 
206
                dialog.destroy()
 
207
 
 
208
                ## Edit custom tool dialog
 
209
                from zim.gui.customtools import EditCustomToolDialog
 
210
                dialog = EditCustomToolDialog(ui)
 
211
                input = {
 
212
                        'Name': 'Foo',
 
213
                        'Comment': 'Test Foo',
 
214
                        'X-Zim-ExecTool': 'foo %u',
 
215
                        'X-Zim-ReadOnly': False,
 
216
                        'X-Zim-ShowInToolBar': False,
 
217
 
 
218
                }
 
219
                dialog.form.update(input)
 
220
                output = dialog.assert_response_ok()
 
221
                input['Icon'] = None
 
222
                self.assertEqual(output, input)
 
223
 
 
224
                ## Properties dialog
 
225
                from zim.gui.propertiesdialog import PropertiesDialog
 
226
                ui = MockUI()
 
227
                dialog = PropertiesDialog(ui)
 
228
                dialog.assert_response_ok()
 
229
 
 
230
 
 
231
class MockUI(MockObject):
 
232
 
 
233
        def __init__(self, page=None):
 
234
                MockObject.__init__(self)
 
235
 
 
236
                if page and not isinstance(page, Path):
 
237
                        self.page = Path(page)
 
238
                else:
 
239
                        self.page = page
 
240
 
 
241
                self.mainwindow = None
 
242
                self.notebook = get_test_notebook()