Fix broken test

This commit is contained in:
Veronica Berglyd Olsen
2023-11-17 07:55:43 +01:00
parent f81c9aa319
commit 1098354fca
+54 -52
View File
@@ -30,6 +30,7 @@ from PyQt5.QtWidgets import qApp, QAction
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwDocAction from novelwriter.enum import nwDocAction
from novelwriter.core.tohtml import ToHtml from novelwriter.core.tohtml import ToHtml
from novelwriter.gui.docviewer import GuiDocViewer
@pytest.mark.gui @pytest.mark.gui
@@ -37,6 +38,7 @@ def testGuiViewer_Main(qtbot, monkeypatch, nwGUI, prjLipsum):
"""Test the document viewer.""" """Test the document viewer."""
# Open project # Open project
assert nwGUI.openProject(prjLipsum) assert nwGUI.openProject(prjLipsum)
docViewer: GuiDocViewer = nwGUI.docViewer
# Rebuild the index # Rebuild the index
nwGUI.mainMenu.aRebuildIndex.activate(QAction.Trigger) nwGUI.mainMenu.aRebuildIndex.activate(QAction.Trigger)
@@ -47,40 +49,40 @@ def testGuiViewer_Main(qtbot, monkeypatch, nwGUI, prjLipsum):
nwGUI.projView.setSelectedHandle("88243afbe5ed8") nwGUI.projView.setSelectedHandle("88243afbe5ed8")
# Middle-click the selected item # Middle-click the selected item
theItem = nwGUI.projView.projTree._getTreeItem("88243afbe5ed8") item = nwGUI.projView.projTree._getTreeItem("88243afbe5ed8")
theRect = nwGUI.projView.projTree.visualItemRect(theItem) rect = nwGUI.projView.projTree.visualItemRect(item)
qtbot.mouseClick(nwGUI.projView.projTree.viewport(), Qt.MidButton, pos=theRect.center()) qtbot.mouseClick(nwGUI.projView.projTree.viewport(), Qt.MidButton, pos=rect.center())
assert nwGUI.docViewer.docHandle == "88243afbe5ed8" assert docViewer.docHandle == "88243afbe5ed8"
# Reload the text # Reload the text
origText = nwGUI.docViewer.toPlainText() origText = docViewer.toPlainText()
nwGUI.docViewer.setPlainText("Oops, all gone!") docViewer.setPlainText("Oops, all gone!")
nwGUI.docViewer.docHeader._refreshDocument() docViewer.docHeader._refreshDocument()
assert nwGUI.docViewer.toPlainText() == origText assert docViewer.toPlainText() == origText
# Select word # Select word
theCursor = nwGUI.docViewer.textCursor() cursor = docViewer.textCursor()
theCursor.setPosition(100) cursor.setPosition(100)
nwGUI.docViewer.setTextCursor(theCursor) docViewer.setTextCursor(cursor)
nwGUI.docViewer._makeSelection(QTextCursor.WordUnderCursor) docViewer._makeSelection(QTextCursor.WordUnderCursor)
qClip = qApp.clipboard() qClip = qApp.clipboard()
qClip.clear() qClip.clear()
# Cut # Cut
assert nwGUI.docViewer.docAction(nwDocAction.CUT) is True assert docViewer.docAction(nwDocAction.CUT) is True
assert qClip.text() == "laoreet" assert qClip.text() == "laoreet"
qClip.clear() qClip.clear()
# Copy # Copy
assert nwGUI.docViewer.docAction(nwDocAction.COPY) is True assert docViewer.docAction(nwDocAction.COPY) is True
assert qClip.text() == "laoreet" assert qClip.text() == "laoreet"
qClip.clear() qClip.clear()
# Select Paragraph # Select Paragraph
assert nwGUI.docViewer.docAction(nwDocAction.SEL_PARA) is True assert docViewer.docAction(nwDocAction.SEL_PARA) is True
theCursor = nwGUI.docViewer.textCursor() cursor = docViewer.textCursor()
assert theCursor.selectedText() == ( assert cursor.selectedText() == (
"Synopsis: Aenean ut placerat velit. Etiam laoreet ullamcorper risus, " "Synopsis: Aenean ut placerat velit. Etiam laoreet ullamcorper risus, "
"eget lobortis enim scelerisque non. Suspendisse id maximus nunc, et " "eget lobortis enim scelerisque non. Suspendisse id maximus nunc, et "
"mollis sapien. Curabitur vel semper sapien, non pulvinar dolor. " "mollis sapien. Curabitur vel semper sapien, non pulvinar dolor. "
@@ -88,74 +90,74 @@ def testGuiViewer_Main(qtbot, monkeypatch, nwGUI, prjLipsum):
) )
# Select All # Select All
assert nwGUI.docViewer.docAction(nwDocAction.SEL_ALL) is True assert docViewer.docAction(nwDocAction.SEL_ALL) is True
theCursor = nwGUI.docViewer.textCursor() cursor = docViewer.textCursor()
assert len(theCursor.selectedText()) == 3061 assert len(cursor.selectedText()) == 3061
# Other actions # Other actions
assert nwGUI.docViewer.docAction(nwDocAction.NO_ACTION) is False assert docViewer.docAction(nwDocAction.NO_ACTION) is False
# Close document # Close document
nwGUI.docViewer.docHeader._closeDocument() docViewer.docHeader._closeDocument()
assert nwGUI.docViewer.docHandle is None assert docViewer.docHandle is None
# Action on no document # Action on no document
assert nwGUI.docViewer.docAction(nwDocAction.COPY) is False assert docViewer.docAction(nwDocAction.COPY) is False
# Open again via menu # Open again via menu
assert nwGUI.projView.setSelectedHandle("88243afbe5ed8") assert nwGUI.projView.setSelectedHandle("88243afbe5ed8")
nwGUI.mainMenu.aViewDoc.activate(QAction.Trigger) nwGUI.mainMenu.aViewDoc.activate(QAction.Trigger)
# Select "Bod" link # Select "Bod" link
theCursor = nwGUI.docViewer.textCursor() cursor = docViewer.textCursor()
theCursor.setPosition(27) cursor.setPosition(27)
nwGUI.docViewer.setTextCursor(theCursor) docViewer.setTextCursor(cursor)
nwGUI.docViewer._makeSelection(QTextCursor.WordUnderCursor) docViewer._makeSelection(QTextCursor.WordUnderCursor)
theRect = nwGUI.docViewer.cursorRect() rect = docViewer.cursorRect()
nwGUI.docViewer._linkClicked(QUrl("#char=Bod")) docViewer._linkClicked(QUrl("#char=Bod"))
assert nwGUI.docViewer.docHandle == "4c4f28287af27" assert docViewer.docHandle == "4c4f28287af27"
# Click mouse nav buttons # Click mouse nav buttons
qtbot.mouseClick(nwGUI.docViewer.viewport(), Qt.BackButton, pos=theRect.center(), delay=100) qtbot.mouseClick(docViewer.viewport(), Qt.BackButton, pos=rect.center(), delay=100)
assert nwGUI.docViewer.docHandle == "88243afbe5ed8" assert docViewer.docHandle == "88243afbe5ed8"
qtbot.mouseClick(nwGUI.docViewer.viewport(), Qt.ForwardButton, pos=theRect.center(), delay=100) qtbot.mouseClick(docViewer.viewport(), Qt.ForwardButton, pos=rect.center(), delay=100)
assert nwGUI.docViewer.docHandle == "4c4f28287af27" assert docViewer.docHandle == "4c4f28287af27"
# Scroll bar default on empty document # Scroll bar default on empty document
nwGUI.docViewer.clear() docViewer.clear()
assert nwGUI.docViewer.scrollPosition == 0 assert docViewer.scrollPosition == 0
nwGUI.docViewer.reloadText() docViewer.reloadText()
# Change document title # Change document title
nwItem = SHARED.project.tree["4c4f28287af27"] nwItem = SHARED.project.tree["4c4f28287af27"]
nwItem.setName("Test Title") nwItem.setName("Test Title") # type: ignore
assert nwItem.itemName == "Test Title" assert nwItem.itemName == "Test Title" # type: ignore
nwGUI.docViewer.updateDocInfo("4c4f28287af27") docViewer.updateDocInfo("4c4f28287af27")
assert nwGUI.docViewer.docHeader.theTitle.text() == "Characters Test Title" assert docViewer.docHeader.docTitle.text() == "Characters \u203a Test Title"
# Title without full path # Title without full path
CONFIG.showFullPath = False CONFIG.showFullPath = False
nwGUI.docViewer.updateDocInfo("4c4f28287af27") docViewer.updateDocInfo("4c4f28287af27")
assert nwGUI.docViewer.docHeader.theTitle.text() == "Test Title" assert docViewer.docHeader.docTitle.text() == "Test Title"
CONFIG.showFullPath = True CONFIG.showFullPath = True
# Document footer show/hide synopsis # Document footer show/hide synopsis
assert nwGUI.viewDocument("f96ec11c6a3da") is True assert nwGUI.viewDocument("f96ec11c6a3da") is True
assert len(nwGUI.docViewer.toPlainText()) == 4315 assert len(docViewer.toPlainText()) == 4315
nwGUI.docViewer.docFooter._doToggleSynopsis(False) docViewer.docFooter._doToggleSynopsis(False)
assert len(nwGUI.docViewer.toPlainText()) == 4099 assert len(docViewer.toPlainText()) == 4099
# Document footer show/hide comments # Document footer show/hide comments
assert nwGUI.viewDocument("846352075de7d") is True assert nwGUI.viewDocument("846352075de7d") is True
assert len(nwGUI.docViewer.toPlainText()) == 675 assert len(docViewer.toPlainText()) == 675
nwGUI.docViewer.docFooter._doToggleComments(False) docViewer.docFooter._doToggleComments(False)
assert len(nwGUI.docViewer.toPlainText()) == 635 assert len(docViewer.toPlainText()) == 635
# Crash the HTML rendering # Crash the HTML rendering
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr(ToHtml, "doConvert", causeException) mp.setattr(ToHtml, "doConvert", causeException)
assert nwGUI.docViewer.loadText("846352075de7d") is False assert docViewer.loadText("846352075de7d") is False
assert nwGUI.docViewer.toPlainText() == "An error occurred while generating the preview." assert docViewer.toPlainText() == "An error occurred while generating the preview."
# qtbot.stop() # qtbot.stop()