def testToCWidget(self):
        '''Test Tabel Of Contents plugin'''
        notebook = self.setUpNotebook()
        window = setUpMainWindow(notebook)
        pageview = window.pageview

        widget = ToCWidget(pageview, ellipsis=False)

        def get_tree():
            # Count number of rows in TreeModel
            model = widget.treeview.get_model()
            rows = []

            def c(model, path, iter):
                rows.append((len(path), model[iter][TEXT_COL]))

            model.foreach(c)
            return rows

        page = notebook.get_page(Path('Test'))
        page.parse(
            'wiki', '''\
====== Foo ======

===== bar =====

line below could be mistaken for heading of the same name..

baz

sfsfsfsd

===== baz =====

sdfsdfsd

==== A ====

==== B ====

==== C ====

===== dus =====

sdfsdf

''')
        notebook.store_page(page)
        #~ print page.get_parsetree().tostring()

        with_h1 = [
            (1, 'Foo'),
            (2, 'bar'),
            (2, 'baz'),
            (3, 'A'),
            (3, 'B'),
            (3, 'C'),
            (2, 'dus'),
        ]
        without_h1 = [
            (1, 'bar'),
            (1, 'baz'),
            (2, 'A'),
            (2, 'B'),
            (2, 'C'),
            (1, 'dus'),
        ]

        # Test basic usage - click some headings
        window.open_page(page)
        widget.on_page_changed(window, page)
        self.assertEqual(get_tree(), without_h1)
        widget.on_store_page(notebook, page)
        self.assertEqual(get_tree(), without_h1)

        widget.set_preferences(show_h1=True, include_hr=True, fontsize=10)
        self.assertEqual(get_tree(), with_h1)
        widget.set_preferences(show_h1=False, include_hr=True, fontsize=10)
        self.assertEqual(get_tree(), without_h1)

        column = widget.treeview.get_column(0)
        model = widget.treeview.get_model()

        def activate_row(m, path, i):
            #~ print(">>>", path)
            widget.treeview.row_activated(path, column)
            # TODO assert something here

            widget.select_section(pageview.textview.get_buffer(), path)

            menu = Gtk.Menu()
            widget.treeview.get_selection().select_path(path)
            widget.on_populate_popup(widget.treeview, menu)
            # TODO assert something here
            widget.treeview.get_selection().unselect_path(path)

        model.foreach(activate_row)

        # Test promote / demote
        pageview.set_readonly(False)
        wanted = [
            (1, 'bar'),
            (2, 'baz'),
            (3, 'A'),
            (3, 'B'),
            (3, 'C'),
            (1, 'dus'),
        ]

        widget.treeview.get_selection().unselect_all()
        widget.treeview.get_selection().select_path((1, ))  # "baz"
        self.assertFalse(widget.on_promote())
        self.assertTrue(widget.on_demote())
        self.assertEqual(get_tree(), wanted)

        widget.treeview.expand_all()
        widget.treeview.get_selection().unselect_all()
        widget.treeview.get_selection().select_path((0, 0))  # "baz"
        self.assertFalse(widget.on_demote())
        self.assertTrue(widget.on_promote())
        self.assertEqual(get_tree(), without_h1)

        # Test promote / demote multiple selected
        wanted = [
            (1, 'bar'),
            (2, 'baz'),  # (1,)	demote 1 -> 2
            (3, 'A'),  # (1,0) demote 2 -> 3
            (3, 'B'),  # (1,1) demote 2 -> 3
            (3, 'C'),  # (1,2)	demote 2 -> 3
            (2, 'dus'),  # (2,)	demote 1 -> 2
        ]

        widget.treeview.expand_all()
        widget.treeview.get_selection().unselect_all()
        for path in (
            (1, ),
            (1, 0),
            (1, 1),
            (1, 2),
            (2, )  # "baz" -> "dus"
        ):
            widget.treeview.get_selection().select_path(path)
        self.assertFalse(widget.on_promote())
        self.assertTrue(widget.on_demote())
        self.assertEqual(get_tree(), wanted)

        widget.treeview.expand_all()
        widget.treeview.get_selection().unselect_all()
        for path in (
            (0, 0),
            (0, 0, 0),
            (0, 0, 1),
            (0, 0, 2),
            (0, 1)  # "baz" -> "dus"
        ):
            widget.treeview.get_selection().select_path(path)
        self.assertFalse(widget.on_demote())
        self.assertTrue(widget.on_promote())
        self.assertEqual(get_tree(), without_h1)

        # Test empty page
        emptypage = tests.MockObject(return_values={'get_parsetree': None})
        widget.on_page_changed(window, emptypage)
        self.assertEqual(get_tree(), [])
        widget.on_store_page(notebook, emptypage)
        self.assertEqual(get_tree(), [])

        # Test some more pages - any errors ?
        for path in notebook.pages.walk():
            page = notebook.get_page(path)
            widget.on_page_changed(window, page)
            widget.on_store_page(notebook, page)
 def setUp(self):
     self.plugin = PluginManager.load_plugin('toolbar')
     self.window = setUpMainWindow(self.setUpNotebook())
     self.extension = find_extension(self.window.pageview,
                                     ToolBarMainWindowExtension)
	def runTest(self):
		plugin = PluginManager.load_plugin('versioncontrol')

		dir = get_tmp_dir('versioncontrol_TestMainWindowExtension')
		notebook = self.setUpNotebook(
			mock=tests.MOCK_ALWAYS_REAL,
			content=('Test',),
			folder=LocalFolder(dir.path)
		)
		mainwindow = setUpMainWindow(notebook)

		notebook_ext = find_extension(notebook, NotebookExtension)
		window_ext = find_extension(mainwindow, VersionControlMainWindowExtension)

		op = ongoing_operation(notebook)
		assert op is None # check no opperation ongoing

		## init & save version
		self.assertIsNone(notebook_ext.vcs)

		def init(dialog):
			self.assertIsInstance(dialog, VersionControlInitDialog)
			choice = dialog.combobox.get_active_text()
			self.assertTrue(choice and not choice.isspace())
			dialog.assert_response_ok()

		with tests.DialogContext(init, SaveVersionDialog):
			window_ext.save_version()

		self.assertIsNotNone(notebook_ext.vcs)

		self.assertFalse(notebook_ext.vcs.is_modified())

		## save version again
		page = notebook.get_page(Path('Foo'))
		page.parse('wiki', 'foo!')
		notebook.store_page(page)

		self.assertTrue(notebook_ext.vcs.is_modified())

		with tests.DialogContext(SaveVersionDialog):
			window_ext.save_version()

		self.assertFalse(notebook_ext.vcs.is_modified())

		## show versions
		with tests.DialogContext(VersionsDialog):
			window_ext.show_versions()

		## auto-save
		plugin.preferences['autosave'] = True

		page = notebook.get_page(Path('Fooooo'))
		page.parse('wiki', 'foo!')
		notebook.store_page(page)

		self.assertTrue(notebook_ext.vcs.is_modified())
		mainwindow.emit('close')
		self.assertFalse(notebook_ext.vcs.is_modified())

		tests.gtk_process_events()
		assert ongoing_operation(notebook) is None
	def setUp(self):
		plugin = LineSorterPlugin()
		window = setUpMainWindow(self.setUpNotebook())
		self.extension = LineSorterPageViewExtension(plugin, window.pageview)
		self.buffer = window.pageview.textview.get_buffer()
	def runTest(self):
		plugin = PluginManager.load_plugin('insertsymbol')

		mainwindow = setUpMainWindow(self.setUpNotebook(content={'Test': '\n'}), path='Test')
		pageview = mainwindow.pageview
		textview = pageview.textview
		buffer = textview.get_buffer()

		# Widget needs to be realized
		pageview.realize()
		textview.realize()

		# insert on end-of-word with space
		press(textview, '\\alpha ')
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, ALPHA + ' \n')

		# Check undo - first undo replace, then the insert space
		pageview.undo()
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, '\\alpha \n')
		pageview.undo()
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, '\\alpha\n') # no trailing space

		# insert on end-of-word with ;
		buffer.clear()
		press(textview, r'\alpha;')
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, ALPHA) # no trailing space

		# no insert in code or pre section
		buffer.clear()
		pageview.toggle_format(VERBATIM)
		press(textview, r'\alpha ')
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, r'\alpha ') # no replace

		# no insert in code or pre section at end-of-line
		buffer.clear()
		pageview.toggle_format(VERBATIM)
		press(textview, '\\alpha\n')
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, '\\alpha\n') # no replace

		# test dialog
		def check_dialog(dialog):
			self.assertIsInstance(dialog, InsertSymbolDialog)
			dialog.iconview.item_activated(Gtk.TreePath((9,))) # path for 10th item in symbol list
			dialog.iconview.item_activated(Gtk.TreePath((10,))) # path for 11th item in symbol list
			dialog.iconview.item_activated(Gtk.TreePath((11,))) # path for 12th item in symbol list
			dialog.assert_response_ok()

		buffer.clear()
		pageview_ext = find_extension(pageview, InsertSymbolPageViewExtension)
		with tests.DialogContext(check_dialog):
			pageview_ext.insert_symbol()
		start, end = buffer.get_bounds()
		text = start.get_text(end)
		self.assertEqual(text, EACUTE + ECIRC + EGRAVE)
示例#6
0
 def runTest(self):
     plugin = PageIndexPlugin()
     window = setUpMainWindow(self.setUpNotebook())
     plugin.extend(window.pageview)
     extension = find_extension(window.pageview, PageIndexPageViewExtension)
     self.assertIsNotNone(extension)
示例#7
0
 def runTest(self):
     plugin = PluginManager.load_plugin('pageindex')
     window = setUpMainWindow(self.setUpNotebook())
     extension = find_extension(window.pageview, PageIndexPageViewExtension)
     self.assertIsNotNone(extension)
    def _test_generator(self):
        plugin = self.pluginklass()

        attachment_dir = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL)
        notebook = self.setUpNotebook()
        notebook.get_attachments_dir = lambda *a: attachment_dir
        mainwindow = setUpMainWindow(notebook)

        extensionklass = plugin.extension_classes['PageView']
        self.assertTrue(
            issubclass(extensionklass, ImageGeneratorPageViewExtensionBase))

        extension = extensionklass(plugin, mainwindow.pageview)

        generator = extension.build_generator()
        self.assertIsInstance(generator, ImageGeneratorClass)

        # Check properties
        self.assertIsNotNone(generator.object_type)
        self.assertIsNotNone(generator.scriptname)
        self.assertIsNotNone(generator.imagename)

        # Input OK
        generator = self.generatorklass(plugin)
        generator.cleanup()  # ensure files did not yet exist
        imagefile, logfile = generator.generate_image(self.validinput)
        self.assertTrue(imagefile.exists())
        if generator.uses_log_file:
            self.assertTrue(logfile.exists())
        else:
            self.assertIsNone(logfile)

        # Cleanup
        generator.cleanup()
        self.assertFalse(imagefile.exists())
        if generator.uses_log_file:
            self.assertFalse(logfile.exists())

        # Input NOK
        if self.invalidinput is not None:
            generator = self.generatorklass(plugin)
            imagefile, logfile = generator.generate_image(self.invalidinput)
            self.assertIsNone(imagefile)
            if generator.uses_log_file:
                self.assertTrue(logfile.exists())
            else:
                self.assertIsNone(logfile)

        # Dialog OK
        dialog = self.dialogklass(mainwindow.pageview, '<title>', generator)
        dialog.set_text(self.validinput)
        dialog.assert_response_ok()

        # Dialog NOK
        if self.invalidinput is not None:

            def ok_store(dialog):
                # Click OK in the "Store Anyway" question dialog
                dialog.do_response(Gtk.ResponseType.YES)

            with tests.LoggingFilter('zim.gui.pageview', 'No such image:'):
                with tests.DialogContext(ok_store):
                    dialog = self.dialogklass(mainwindow.pageview, '<title>',
                                              generator)
                    dialog.set_text(self.invalidinput)
                    dialog.assert_response_ok()