File: mainwindow.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (378 lines) | stat: -rw-r--r-- 11,614 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

# Copyright 2009-2017 Jaap Karssenberg <jaap.karssenberg@gmail.com>

from gi.repository.Gtk import ImageMenuItem

import tests

import sys

from zim.notebook import Path
from zim.plugins import PluginManager

from zim.gui.mainwindow import *

is_sensitive = lambda w: w.get_property('sensitive')


def setUpMainWindow(notebook, path='Test'):
	if isinstance(path, str):
		path = Path(path)

	mainwindow = MainWindow(notebook, page=path)
	mainwindow.init_uistate() # XXX
	return mainwindow


class TestHistoryNavigation(tests.TestCase):

	def runTest(self):
		pages = [Path('page1'), Path('page2'), Path('page3'), Path('page4')]
		notebook = self.setUpNotebook(content=pages)

		window = setUpMainWindow(notebook, path=pages[0])
		history = window.history
		historylist = history._history # XXX

		back_action = window.actiongroup.get_action('open_page_back')
		forward_action = window.actiongroup.get_action('open_page_forward')

		# Setup history
		self.assertFalse(is_sensitive(back_action))
		self.assertFalse(is_sensitive(forward_action))
		for i, p in enumerate(pages[1:]):
			window.open_page(p)
			self.assertTrue(is_sensitive(back_action))
			self.assertFalse(is_sensitive(forward_action))

		self.assertEqual([p.name for p in historylist], [p.name for p in pages])
		p = history.get_current()
		self.assertEqual(p.name, pages[-1].name)

		# Navigate backward
		while is_sensitive(back_action):
			window.open_page_back()
			self.assertTrue(is_sensitive(forward_action))

		self.assertEqual([p.name for p in historylist], [p.name for p in pages])
		p = history.get_current()
		self.assertEqual(p.name, pages[0].name)

		# Navigate forward
		while is_sensitive(forward_action):
			window.open_page_forward()
			self.assertTrue(is_sensitive(back_action))

		self.assertEqual([p.name for p in historylist], [p.name for p in pages])
		p = history.get_current()
		self.assertEqual(p.name, pages[-1].name)


class TestUpDownNavigation(tests.TestCase):

	def testUpDown(self):
		pages = (
			'A',
			'A:B',
			'A:B:C',
			'D',
		)
		window = setUpMainWindow(self.setUpNotebook(content=pages), path='A')
		child_action = window.actiongroup.get_action('open_page_child')
		parent_action = window.actiongroup.get_action('open_page_parent')
		historylist = window.history._history # XXX

		while is_sensitive(child_action):
			window.open_page_child()

		self.assertEqual([p.name for p in historylist], ['A', 'A:B', 'A:B:C'])

		while is_sensitive(parent_action):
			window.open_page_parent()

		self.assertEqual([p.name for p in historylist], ['A', 'A:B', 'A:B:C', 'A:B', 'A'])

	def testDownHistory(self):
		pages = (
			'A',
			'A:B',
			'A:C'
		)
		window = setUpMainWindow(self.setUpNotebook(content=pages), path='A')

		self.assertEqual(window.page, Path('A'))
		window.open_page_child()
		self.assertEqual(window.page, Path('A:B'))

		window.open_page(Path('A:C'))
		window.open_page(Path('A'))

		self.assertEqual(window.page, Path('A'))
		window.open_page_child()
		self.assertEqual(window.page, Path('A:C'))


class TestPreviousNextNavigation(tests.TestCase):

	def runTest(self):
		pages = (
			'A',
			'A:B1',
			'A:B2',
			'B',
		)
		window = setUpMainWindow(self.setUpNotebook(content=pages), path='A')
		next_action = window.actiongroup.get_action('open_page_next')
		previous_action = window.actiongroup.get_action('open_page_previous')
		historylist = window.history._history # XXX

		while is_sensitive(next_action):
			window.open_page_next()

		self.assertEqual([p.name for p in historylist], list(pages))

		while is_sensitive(previous_action):
			window.open_page_previous()

		self.assertEqual([p.name for p in historylist], list(pages) + ['A:B2', 'A:B1', 'A'])


class TestActions(tests.TestCase):

	def testJumpToPage(self):
		window = setUpMainWindow(self.setUpNotebook(content=('A', 'B')), path='A')

		def jump(dialog):
			dialog.set_input(page='B')
			dialog.assert_response_ok()

		self.assertEqual(window.page, Path('A'))
		with tests.DialogContext(jump):
			window.show_jump_to()

		self.assertEqual(window.page, Path('B'))

	def testOpenHome(self):
		window = setUpMainWindow(self.setUpNotebook(), path='A')

		self.assertEqual(window.page, Path('A'))
		window.open_page_home()
		self.assertEqual(window.page, Path('Home'))


class TestOpenPageImportTextFile(tests.TestCase):

	def runTest(self):
		# See also uiactions.TestUIActions.testCreateNewPageImportExistingTextFile
		notebook = self.setUpNotebook(content=('A',))
		file = notebook.folder.file('B.txt')
		file.write('Test 123\n') # Not a page, just text !

		window = setUpMainWindow(notebook, path='A')

		def do_import(questiondialog):
			questiondialog.answer_yes()

		with tests.DialogContext(do_import):
			window.open_page(Path('B'))

		self.assertEqual(window.page, Path('B'))
		lines = file.readlines()
		self.assertEqual(lines[0], 'Content-Type: text/x-zim-wiki\n')
		self.assertEqual(lines[-1], 'Test 123\n')


from gi.repository import Gtk

from zim.gui.widgets import WindowSidePaneWidget, LEFT_PANE


class MockSidePaneWidget(Gtk.VBox, WindowSidePaneWidget):
	title = 'MockSidePaneWidget'


class TestTogglingState(tests.TestCase):

	def runTest(self):
		path = Path('Test:foo:bar')
		window = setUpMainWindow(self.setUpNotebook(), path)

		#self.assertTrue(window.uistate['show_menubar'])
		window.toggle_menubar()
		#self.assertFalse(window.uistate['show_menubar'])
		window.toggle_menubar()
		#self.assertTrue(window.uistate['show_menubar'])

		mywidget = MockSidePaneWidget()
		mywidget.show_all()
		window.add_sidepane_widget('Test', mywidget, LEFT_PANE)

		self.assertTrue(window.uistate['left_pane'][0])
		window.toggle_panes()
		self.assertFalse(window.uistate['left_pane'][0])
		window.toggle_panes()
		self.assertTrue(window.uistate['left_pane'][0])

		# ..
		window.toggle_fullscreen()
		window.toggle_fullscreen()


class TestSavingPages(tests.TestCase):

	def setUp(self):
		self.mainwindow = setUpMainWindow(self.setUpNotebook())

	def testSave(self):
		'''Test saving a page from the interface'''
		self.mainwindow.open_page(Path('Non-exsiting:page'))
		self.assertFalse(self.mainwindow.page.exists())
		self.assertIsNone(self.mainwindow.page.get_parsetree())
		self.assertTrue(self.mainwindow.pageview.textview.get_buffer().showing_template)
		self.mainwindow.pageview.save_page()
		self.assertTrue(self.mainwindow.page.exists())
		self.assertIsNotNone(self.mainwindow.page.get_parsetree())
		self.assertFalse(self.mainwindow.pageview.textview.get_buffer().showing_template)

	def testClose(self):
		# Specific bug found when trying to close the page while auto-save
		# in progress, test it here
		self.mainwindow.pageview.textview.get_buffer().insert_at_cursor('...')
		self.mainwindow.pageview._save_page_handler.try_save_page()
		self.assertTrue(self.mainwindow.page.modified)
		self.mainwindow.close()
		self.assertFalse(self.mainwindow.page.modified)

	def testCloseByDestroy(self):
		# Specific bug found when trying to close the page while auto-save
		# in progress, test it here
		self.mainwindow.pageview.textview.get_buffer().insert_at_cursor('...')
		self.mainwindow.pageview._save_page_handler.try_save_page()
		self.assertTrue(self.mainwindow.page.modified)
		self.mainwindow.destroy()
		self.assertFalse(self.mainwindow.page.modified)


class TestMenuDocs(tests.TestCase):

	def setUp(self):
		self.mainwindow = setUpMainWindow(self.setUpNotebook())
		self.manual = tests.ZIM_DATA_FOLDER.file('manual/Help/Menu_Items.txt').read()

	def testAllMenuItemsAreDocumented(self):
		def menuitems(parent, level=0):
			for mi in parent.get_children():
				if isinstance(mi, ImageMenuItem):
					yield mi, level
				sm = mi.get_submenu()
				if sm:
					for mi_sm in menuitems(sm, level+1):
						yield mi_sm

		for menuitem,level in menuitems(self.mainwindow.menubar):
			label = menuitem.get_label().replace('_','').rstrip('.')
			accel_path = menuitem.get_accel_path()
			accel_label = Gtk.accelerator_get_label(
				Gtk.AccelMap().lookup_entry(accel_path).key.accel_key,
				Gtk.AccelMap().lookup_entry(accel_path).key.accel_mods)
			if accel_label:
				if sys.platform == 'darwin':
					accel_label = accel_label.replace('⌘', 'Ctrl+')
					accel_label = accel_label.replace('⇧', 'Shift+')
					accel_label = accel_label.replace('⌥', 'Alt+')
					accel_label = accel_label.replace('←', 'Left')
					accel_label = accel_label.replace('→', 'Right')
					accel_label = accel_label.replace('↑', 'Up')
					accel_label = accel_label.replace('↓', 'Down')
					accel_label = accel_label.replace('⇞', 'Page Up')
					accel_label = accel_label.replace('⇟', 'Page Down')
					accel_label = accel_label.replace('↖', 'Home')

				if '++' in accel_label:
					label += ' <' + accel_label.replace('++', '><+') + '>'
				else:
					label += ' <' + accel_label.replace('+', '><') + '>'


			if level:
				label = '**' + label + '**'
			else:
				label = '===== ' + label + ' ====='
			self.assertTrue(label in self.manual, 'Menu item "{}" not documented'.format(label))


class TestReadOnlyMainWindow(tests.TestCase):

	def runTest(self):
		'''Test switching behavior when page opened with read-only status'''
		notebook = self.setUpNotebook(content=('Test', 'Foo', 'Bar'))
		window = setUpMainWindow(notebook)
		self.assertReadWriteState(window)

		notebook.readonly = True # XXX: should never be assigned liek this in apoplication usage
		notebook._page_cache.clear() # XXX
		window.open_page(Path('Foo'))
		self.assertReadOnlyState(window)

		notebook.readonly = False # XXX: should never be assigned liek this in apoplication usage
		notebook._page_cache.clear() # XXX
		window.open_page(Path('Bar'))
		self.assertReadWriteState(window)

	def assertReadWriteState(self, window):
		for headerbar in (window._headerbar, window._fullscreen_headerbar):
			title = window.get_title()
			self.assertNotIn('[' + _('readonly') + ']', title)

		self.assertTrue(window.toggle_editable.get_sensitive())
		self.assertFalse(window.pageview.readonly)

	def assertReadOnlyState(self, window):
		for headerbar in (window._headerbar, window._fullscreen_headerbar):
			title = window.get_title()
			self.assertIn('[' + _('readonly') + ']', title)

		self.assertFalse(window.toggle_editable.get_sensitive())
		self.assertTrue(window.pageview.readonly)


class TestReadOnlyPageWindow(TestReadOnlyMainWindow):

	def runTest(self):
		notebook = self.setUpNotebook(content=('Test',))
		page = notebook.get_page(Path('Test'))
		window = PageWindow(notebook, page, navigation=None)
		self.assertReadWriteState(window)

		notebook.readonly = True # XXX: should never be assigned liek this in apoplication usage
		notebook._page_cache.clear() # XXX
		page = notebook.get_page(Path('Test'))
		window = PageWindow(notebook, page, navigation=None)
		self.assertReadOnlyState(window)


class TestUIStateInitSave(tests.TestCase):

	def testMainWindow(self):
		notebook = self.setUpNotebook(content=('Test',))
		window = setUpMainWindow(notebook)
		self.doTest(window)

	def testPageWindow(self):
		notebook = self.setUpNotebook(content=('Test',))
		page = notebook.get_page(Path('Test'))
		window = PageWindow(notebook, page, navigation=None)
		self.doTest(window)

	def doTest(self, window):
		from zim.gui.widgets import LEFT_PANE, RIGHT_PANE, TOP_PANE, BOTTOM_PANE

		# Check keys defined in Window.init_uistate()
		window.show_all()
		for key in (LEFT_PANE, RIGHT_PANE, TOP_PANE, BOTTOM_PANE):
			self.assertIn(key, window.uistate)

		# Check Window.save_uistate() is called
		cb = tests.wrap_method_with_logger(window, 'save_uistate')
		window.emit('delete-event', None)
		self.assertTrue(cb.hasBeenCalled)