def testAttachFileResolveExistingFile(self): folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) file = folder.file('Attachment.abc') file.write('Test ABC\n') attach_folder = self.notebook.get_attachments_dir(self.page) conflict_file = attach_folder.file('Attachment.abc') conflict_file.write('Conflict\n') def attach_file(dialog): dialog.set_file(file) dialog.assert_response_ok() def resolve_conflict(dialog): dialog.set_input(name='NewName.abc') dialog.assert_response_ok() with tests.DialogContext(attach_file, resolve_conflict): self.uiactions.attach_file() attach_file = attach_folder.file('NewName.abc') self.assertTrue(attach_file.exists()) self.assertEqual(attach_file.read(), file.read()) self.assertEqual(conflict_file.read(), 'Conflict\n')
def testDeletePageWithTrash(self): self.assertTrue(self.page.exists()) with tests.DialogContext(): # fails if dialog shown self.uiactions.delete_page() self.assertFalse(self.page.exists())
def testEditPageSource(self): from zim.gui.widgets import MessageDialog from zim.newfs import LocalFile from zim.gui.applications import ApplicationManager oldtext = self.page.dump('plain') # trick page into caching content signals = tests.SignalLogger(self.page) def edit_page(cmd): file = LocalFile(cmd[-1]) self.assertEqual(file, self.page.source_file) file.write('New text\n') manager = ApplicationManager() entry = manager.create('text/plain', 'test', 'test') manager.set_default_application('text/plain', entry) with tests.ApplicationContext(edit_page): with tests.DialogContext(MessageDialog): self.uiactions.edit_page_source() newtext = self.page.dump('plain') self.assertEqual(signals['storage-changed'], [(True, )]) # boolean for external change self.assertNotEqual(oldtext, newtext)
def testShowRecentChangesDialog(self): def use_recent_changes(dialog): # Check view model = dialog.treeview.get_model() pages = set(r[0] for r in model) self.assertEqual(pages, {'Test', 'ExistingPage'}) # TODO: how can we check rendering of date column ? # Check live update page = self.notebook.get_page(Path('NewPage')) page.parse('wiki', 'TEst 123') self.notebook.store_page(page) pages = set(r[0] for r in model) self.assertEqual(pages, {'NewPage', 'Test', 'ExistingPage'}) # Check opening a page col = dialog.treeview.get_column(0) dialog.treeview.row_activated(Gtk.TreePath((0, )), col) with tests.DialogContext(use_recent_changes): self.uiactions.show_recent_changes() self.assertEqual(self.navigation.opened, Path('NewPage'))
def runTest(self): window = tests.MockObject() window.pageview = setUpPageView() window.ui = tests.MockObject() window.ui.uimanager = tests.MockObject() window.ui.uistate = ConfigDict() window.ui.mainwindow = window # XXX plugin = TableEditorPlugin() extension = MainWindowExtension(plugin, window) with tests.DialogContext(self.checkInsertTableDialog): extension.insert_table() tree = window.pageview.get_parsetree() #~ print tree.tostring() obj = tree.find('table') self.assertTrue(obj.attrib['aligns'] == 'left') self.assertTrue(obj.attrib['wraps'] == '0') # Parses tree to a table object tabledata = tree.tostring().replace("<?xml version='1.0' encoding='utf-8'?>", '')\ .replace('<zim-tree>', '').replace('</zim-tree>', '')\ .replace('<td> </td>', '<td>text</td>') table = plugin.create_table({'type': 'table'}, ElementTree.fromstring(tabledata)) self.assertTrue(isinstance(table, TableViewObject))
def testTrashPage(self): self.assertTrue(self.page.exists()) with tests.DialogContext(TrashPageDialog): self.uiactions.delete_page() self.assertFalse(self.page.exists())
def testExceptionHandlerWithGtk(self): def error_dialog_with_trace(dialog): self.assertIsInstance(dialog, ErrorDialog) self.assertTrue(dialog.showing_trace) def error_dialog_without_trace(dialog): self.assertIsInstance(dialog, ErrorDialog) self.assertFalse(dialog.showing_trace) zim.errors.set_use_gtk(True) try: self.assertTrue(zim.errors.use_gtk_errordialog) with tests.DialogContext( error_dialog_with_trace, error_dialog_with_trace, error_dialog_without_trace, error_dialog_without_trace, ): with tests.LoggingFilter(logger='zim.gui', message='Running ErrorDialog'): self.testExceptionHandler() except: zim.errors.set_use_gtk(False) raise else: zim.errors.set_use_gtk(False) self.assertFalse(zim.errors.use_gtk_errordialog)
def testEditCustomTool(self): config = ConfigManager() manager = CustomToolManager(config) mytool = { 'Name': 'Edit', 'Comment': 'Test Edit', 'X-Zim-ExecTool': 'edit %f', } manager.create(**mytool) self.assertIsNotNone(manager.get_tool('Edit')) def edit_tool(dialog): dialog.set_input(Comment='Editted Comment') dialog.assert_response_ok() dialog = CustomToolManagerDialog(None, config) #model = dialog.listview.get_model() #self.assertIn('Edit', [r[CustomToolList.NAME_COL] for r in model]) with tests.DialogContext(edit_tool): dialog.listview.select_by_name('Edit') dialog.on_edit() dialog.assert_response_ok() tool = manager.get_tool('Edit') self.assertEqual(tool.comment, 'Editted Comment')
def testAddCustomTool(self): config = ConfigManager() manager = CustomToolManager(config) mytool = { 'Name': 'Add', 'Comment': 'Test Add', 'X-Zim-ExecTool': 'add %f', 'X-Zim-ReadOnly': False, 'X-Zim-ShowInToolBar': False, 'X-Zim-ReplaceSelection': False, } self.assertIsNone(manager.get_tool('Add')) def add_tool(dialog): dialog.set_input(**mytool) dialog.assert_response_ok() dialog = CustomToolManagerDialog(None, config) with tests.DialogContext(add_tool): dialog.on_add() dialog.assert_response_ok() # XXX listview intialization fails in test ??? #model = dialog.listview.get_model() #self.assertIn('Add', [r[CustomToolList.NAME_COL] for r in model]) tool = manager.get_tool('Add') self.assertIsNotNone(tool) for key, value in list(mytool.items()): self.assertEqual(tool['Desktop Entry'][key], value)
def runTest(self): from zim.gui.mainwindow import MainWindow ## Without argument should prompt def testAddNotebookDialog(dialog): self.assertIn(dialog.__class__.__name__, ('AddNotebookDialog', 'NotebookDialog')) cmd = GuiCommand('gui') with tests.DialogContext(testAddNotebookDialog): cmd.run( ) # Exits without running due to no notebook given in dialog ### Try again with argument folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) folder.touch() cmd = GuiCommand('gui') cmd.parse_options(folder.path) with tests.WindowContext(MainWindow): with tests.LoggingFilter('zim', 'Exception while loading plugin:'): window = cmd.run() self.addCleanup(window.destroy) self.assertEqual(window.__class__.__name__, 'MainWindow') self.assertEqual(window.notebook.uri, folder.uri) self.assertGreaterEqual( len(ConfigManager.preferences['General']['plugins']), 3) self.assertGreaterEqual(len(window.pageview.__zim_extension_objects__), 3) with tests.WindowContext(MainWindow): window2 = cmd.run() self.assertIs(window2, window)
def testOpenAnotherNotebook(self): from zim.gui.notebookdialog import NotebookDialog def check_dialog_shown(dialog): assert isinstance(dialog, NotebookDialog) with tests.DialogContext(check_dialog_shown): self.uiactions.show_open_notebook()
def testOpenNewWindow(self): self.uiactions.widget = setUpMainWindow(self.notebook) def pagewindow(window): window.pageview.page == self.page with tests.DialogContext(pagewindow): self.uiactions.open_new_window()
def testShowExport(self): from zim.gui.exportdialog import ExportDialog def check_dialog_shown(dialog): assert isinstance(dialog, ExportDialog) with tests.DialogContext(check_dialog_shown): self.uiactions.show_export()
def testDeletePage(self): self.notebook.config['Notebook']['disable_trash'] = True self.assertTrue(self.page.exists()) with tests.DialogContext(DeletePageDialog): self.uiactions.delete_page() self.assertFalse(self.page.exists())
def testShowPreferencesDialog(self): from zim.gui.preferencesdialog import PreferencesDialog from zim.plugins import PluginManager self.uiactions.widget = Gtk.Window() with tests.DialogContext(PreferencesDialog): self.uiactions.show_preferences()
def testOpenDocumentRootNotDefined(self): from zim.gui.widgets import ErrorDialog self.assertIsNone(self.notebook.document_root) with tests.LoggingFilter('zim', 'No document root defined'): with tests.ApplicationContext(): with tests.DialogContext(ErrorDialog): self.uiactions.open_document_root()
def testCreateNewPageFailsForExistingPage(self): from zim.notebook import PageExistsError def open_new_page(dialog): dialog.set_input(page='ExistingPage') self.assertRaises(PageExistsError, dialog.assert_response_ok) with tests.DialogContext(open_new_page): self.uiactions.new_page()
def testRenamePageFailsForExistingPage(self): from zim.notebook import PageExistsError def renamepage(dialog): dialog.set_input(name='ExistingPage') self.assertRaises(PageExistsError, dialog.do_response_ok) with tests.DialogContext(renamepage): self.uiactions.move_page()
def testRenamePageSameNameInvalidInput(self): self.assertEqual(self.page.basename, 'Test') def renamepage(dialog): dialog.set_input(name='Test') self.assertFalse(dialog.do_response_ok()) with tests.DialogContext(renamepage): self.uiactions.move_page()
def testRenamePageNonExistingPageFails(self): from zim.notebook import PageNotFoundError page = self.notebook.get_page(Path('NonExistingPage')) def renamepage(dialog): dialog.set_input(name='NewName') self.assertRaises(PageNotFoundError, dialog.do_response_ok) with tests.DialogContext(renamepage): self.uiactions.rename_page(page)
def testOpenAttachmentsFolderExisting(self): folder = self.notebook.get_attachments_dir(self.page) folder.touch() def open_folder(cmd): self.assertEqual(cmd[-1], folder.path) with tests.ApplicationContext(open_folder): with tests.DialogContext(): self.uiactions.open_attachments_folder()
def testMovePageNonExistingPageFails(self): from zim.notebook import PageNotFoundError page = self.notebook.get_page(Path('NonExistingPage')) def movepage(dialog): dialog.set_input(parent='NewParent') self.assertRaises(PageNotFoundError, dialog.do_response_ok) with tests.DialogContext(movepage): self.uiactions.move_page(page)
def testDeletePageWithTrashUpdateLinks(self): referrer = self.notebook.get_page(Path('Referrer')) referrer.parse('wiki', 'Test [[Test]]\n') self.notebook.store_page(referrer) with tests.DialogContext(): # fails if dialog shown self.uiactions.delete_page() self.assertFalse(self.page.exists()) self.assertEqual(referrer.dump('wiki'), ['Test Test\n'])
def testDeletePageWithoutTrashCancel(self): self.notebook.config['Notebook']['disable_trash'] = True def do_delete(dialog): dialog.do_response_cancel() with tests.DialogContext(do_delete): self.uiactions.delete_page() self.assertTrue(self.page.exists())
def testTrashPageCancel(self): self.assertTrue(self.page.exists()) def cancel_delete(dialog): assert isinstance(dialog, TrashPageDialog) dialog.do_response_cancel() with tests.DialogContext(cancel_delete): self.uiactions.delete_page() self.assertTrue(self.page.exists())
def testMovePageSameParentInvalidInput(self): page = self.notebook.get_page(Path('SomeParent:MyPage')) page.parse('wiki', 'test 123\n') self.notebook.store_page(page) def movepage(dialog): dialog.set_input(parent='SomeParent') self.assertFalse(dialog.do_response_ok()) with tests.DialogContext(movepage): self.uiactions.move_page(page)
def testChangeTable(self): attrib = {'aligns': 'normal,normal', 'wraps': '0,0'} header = ['h1', 'h2'] rows = [['t1', 't2'],] obj = TableViewObject(attrib, header, rows, {}) widget = obj.get_widget() with tests.DialogContext(self.checkUpdateTableDialog): widget.on_change_columns(None) self.assertTrue(isinstance(widget.treeview, gtk.TreeView))
def testOpenNewWindowWithPage(self): self.uiactions.widget = setUpMainWindow(self.notebook) page = self.notebook.get_page(Path('OtherPage')) self.assertNotEqual(page, self.page) def pagewindow(window): window.pageview.page == page with tests.DialogContext(pagewindow): self.uiactions.open_new_window(page)
def testOpenDocumentRoot(self): from zim.gui.widgets import ErrorDialog self.notebook.document_root = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) self.notebook.document_root.touch() def open_folder(cmd): self.assertEqual(cmd[-1], self.notebook.document_root.path) with tests.ApplicationContext(open_folder): with tests.DialogContext(): self.uiactions.open_document_root()
def testAddNotebook(dialog): self.assertTrue(isinstance(dialog, NotebookDialog)) with tests.DialogContext(doAddNotebook): dialog.do_add_notebook() dialog.combobox.set_active(0) selection = dialog.treeview.get_selection() selection.select_path((1, )) # select newly added notebook dialog.assert_response_ok()