Improved coverage of dialogs tools, and import document feature

This commit is contained in:
Veronica K. B. Olsen
2020-09-28 20:29:52 +02:00
parent fc096ddb0b
commit 678a31855f
5 changed files with 77 additions and 53 deletions
+12 -17
View File
@@ -594,15 +594,12 @@ class GuiMain(QMainWindow):
return False return False
if not self.docEditor.isEmpty(): if not self.docEditor.isEmpty():
if self.mainConf.showGUI: msgBox = QMessageBox()
msgBox = QMessageBox() msgRes = msgBox.question(self, "Import Document", (
msgRes = msgBox.question(self, "Import Document", ( "Importing the file will overwrite the current content of the document. "
"Importing the file will overwrite the current content of the document. " "Do you want to proceed?"
"Do you want to proceed?" ))
)) if msgRes != QMessageBox.Yes:
if msgRes != QMessageBox.Yes:
return False
else:
return False return False
self.docEditor.replaceText(theText) self.docEditor.replaceText(theText)
@@ -612,18 +609,16 @@ class GuiMain(QMainWindow):
def mergeDocuments(self): def mergeDocuments(self):
"""Merge multiple documents to one single new document. """Merge multiple documents to one single new document.
""" """
if self.mainConf.showGUI: dlgMerge = GuiDocMerge(self, self.theProject)
dlgMerge = GuiDocMerge(self, self.theProject) dlgMerge.exec_()
dlgMerge.exec_() return
return True
def splitDocument(self): def splitDocument(self):
"""Split a single document into multiple documents. """Split a single document into multiple documents.
""" """
if self.mainConf.showGUI: dlgSplit = GuiDocSplit(self, self.theProject)
dlgSplit = GuiDocSplit(self, self.theProject) dlgSplit.exec_()
dlgSplit.exec_() return
return True
def passDocumentAction(self, theAction): def passDocumentAction(self, theAction):
"""Pass on document action theAction to the document viewer if """Pass on document action theAction to the document viewer if
-27
View File
@@ -178,30 +178,3 @@ def nwOldProj(nwTemp):
if path.isdir(oldProjDir): if path.isdir(oldProjDir):
shutil.rmtree(oldProjDir) shutil.rmtree(oldProjDir)
return return
##
# Monkey Patch Dialogs
##
@pytest.fixture(scope="function")
def mnkQtDialogs(monkeypatch, nwTemp):
"""Mock Qt dialog functions to prevent GUI blocking while testing.
"""
monkeypatch.setattr(
QFileDialog, "getExistingDirectory", lambda *args, **kwargs: nwTemp
)
monkeypatch.setattr(
QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes
)
monkeypatch.setattr(
QMessageBox, "information", lambda *args, **kwargs: None
)
monkeypatch.setattr(
QMessageBox, "warning", lambda *args, **kwargs: None
)
monkeypatch.setattr(
QMessageBox, "critical", lambda *args, **kwargs: None
)
return
+24 -6
View File
@@ -14,7 +14,7 @@ from os import path
from PyQt5.QtCore import Qt, QItemSelectionModel from PyQt5.QtCore import Qt, QItemSelectionModel
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialogButtonBox, QTreeWidgetItem, QListWidgetItem, QDialog, QAction, QDialogButtonBox, QTreeWidgetItem, QListWidgetItem, QDialog, QAction,
QMessageBox QMessageBox, QFileDialog
) )
from nw.gui import ( from nw.gui import (
@@ -547,7 +547,7 @@ def testBuildTool(qtbot, nwTempBuild, nwLipsum, nwRef, nwTemp):
nwGUI.closeMain() nwGUI.closeMain()
@pytest.mark.gui @pytest.mark.gui
def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp): def testMergeSplitTools(qtbot, monkeypatch, nwTempGUI, nwLipsum, nwRef, nwTemp):
nwGUI = nw.main(["--testmode", "--config=%s" % nwLipsum, "--data=%s" % nwTemp]) nwGUI = nw.main(["--testmode", "--config=%s" % nwLipsum, "--data=%s" % nwTemp])
qtbot.addWidget(nwGUI) qtbot.addWidget(nwGUI)
@@ -562,7 +562,13 @@ def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp):
assert nwGUI.treeView.setSelectedHandle("45e6b01ca35c1") assert nwGUI.treeView.setSelectedHandle("45e6b01ca35c1")
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
nwMerge = GuiDocMerge(nwGUI, nwGUI.theProject) monkeypatch.setattr(GuiDocMerge, "exec_", lambda *args: None)
nwGUI.mainMenu.aMergeDocs.activate(QAction.Trigger)
qtbot.waitUntil(lambda: getGuiItem("GuiDocMerge") is not None, timeout=1000)
nwMerge = getGuiItem("GuiDocMerge")
assert isinstance(nwMerge, GuiDocMerge)
nwMerge.show()
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
nwMerge._doMerge() nwMerge._doMerge()
@@ -579,8 +585,16 @@ def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp):
# Split By Chapter # Split By Chapter
assert nwGUI.treeView.setSelectedHandle("73475cb40a568") assert nwGUI.treeView.setSelectedHandle("73475cb40a568")
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
nwSplit = GuiDocSplit(nwGUI, nwGUI.theProject)
monkeypatch.setattr(GuiDocSplit, "exec_", lambda *args: None)
nwGUI.mainMenu.aSplitDoc.activate(QAction.Trigger)
qtbot.waitUntil(lambda: getGuiItem("GuiDocSplit") is not None, timeout=1000)
nwSplit = getGuiItem("GuiDocSplit")
assert isinstance(nwSplit, GuiDocSplit)
nwSplit.show()
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
nwSplit.splitLevel.setCurrentIndex(1) nwSplit.splitLevel.setCurrentIndex(1)
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
@@ -863,7 +877,7 @@ def testLoadProject(qtbot, monkeypatch, nwMinimal, nwTemp):
nwGUI.closeMain() nwGUI.closeMain()
@pytest.mark.gui @pytest.mark.gui
def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef, tmpConf): def testPreferences(qtbot, monkeypatch, nwMinimal, nwTemp, nwRef, tmpConf):
nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp]) nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp])
qtbot.addWidget(nwGUI) qtbot.addWidget(nwGUI)
nwGUI.show() nwGUI.show()
@@ -872,6 +886,8 @@ def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef,
assert nwGUI.openProject(nwMinimal) assert nwGUI.openProject(nwMinimal)
monkeypatch.setattr(QMessageBox, "information", lambda *args, **kwargs: None)
monkeypatch.setattr(GuiPreferences, "exec_", lambda *args: None) monkeypatch.setattr(GuiPreferences, "exec_", lambda *args: None)
monkeypatch.setattr(GuiPreferences, "result", lambda *args: QDialog.Accepted) monkeypatch.setattr(GuiPreferences, "result", lambda *args: QDialog.Accepted)
nwGUI.mainMenu.aPreferences.activate(QAction.Trigger) nwGUI.mainMenu.aPreferences.activate(QAction.Trigger)
@@ -883,6 +899,7 @@ def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef,
# Override Config # Override Config
tmpConf.confPath = nwMinimal tmpConf.confPath = nwMinimal
tmpConf.showGUI = False
nwGUI.mainConf = tmpConf nwGUI.mainConf = tmpConf
nwPrefs.mainConf = tmpConf nwPrefs.mainConf = tmpConf
nwPrefs.tabGeneral.mainConf = tmpConf nwPrefs.tabGeneral.mainConf = tmpConf
@@ -1047,13 +1064,14 @@ def testQuotesDialog(qtbot, nwMinimal, nwTemp):
nwGUI.close() nwGUI.close()
@pytest.mark.gui @pytest.mark.gui
def testDialogsOpenClose(qtbot, mnkQtDialogs, nwMinimal, nwTemp): def testDialogsOpenClose(qtbot, monkeypatch, nwMinimal, nwTemp):
nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp, nwMinimal]) nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp, nwMinimal])
qtbot.addWidget(nwGUI) qtbot.addWidget(nwGUI)
nwGUI.show() nwGUI.show()
qtbot.waitForWindowShown(nwGUI) qtbot.waitForWindowShown(nwGUI)
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *args, **kwargs: nwTemp)
assert nwGUI.selectProjectPath() == nwTemp assert nwGUI.selectProjectPath() == nwTemp
# qtbot.stopForInteraction() # qtbot.stopForInteraction()
+40 -2
View File
@@ -12,7 +12,9 @@ from nwtools import cmpFiles
from os import path from os import path
from PyQt5.QtCore import Qt, QUrl, QPoint, QItemSelectionModel from PyQt5.QtCore import Qt, QUrl, QPoint, QItemSelectionModel
from PyQt5.QtGui import QTextCursor, QColor, QPixmap, QIcon from PyQt5.QtGui import QTextCursor, QColor, QPixmap, QIcon
from PyQt5.QtWidgets import qApp, QAction, QTreeWidgetItem, QStyle from PyQt5.QtWidgets import (
qApp, QAction, QTreeWidgetItem, QStyle, QFileDialog, QMessageBox
)
from nw.constants import ( from nw.constants import (
nwItemType, nwItemClass, nwUnicode, nwOutline, nwDocAction, nwDocInsert nwItemType, nwItemClass, nwUnicode, nwOutline, nwDocAction, nwDocInsert
@@ -999,7 +1001,7 @@ def testContextMenu(qtbot, nwLipsum, nwTemp):
nwGUI.close() nwGUI.close()
@pytest.mark.gui @pytest.mark.gui
def testInsertMenu(qtbot, nwFuncTemp, nwTemp): def testInsertMenu(qtbot, monkeypatch, nwFuncTemp, nwTemp):
nwGUI = nw.main(["--testmode", "--config=%s" % nwFuncTemp, "--data=%s" % nwTemp]) nwGUI = nw.main(["--testmode", "--config=%s" % nwFuncTemp, "--data=%s" % nwTemp])
qtbot.addWidget(nwGUI) qtbot.addWidget(nwGUI)
nwGUI.show() nwGUI.show()
@@ -1085,6 +1087,42 @@ def testInsertMenu(qtbot, nwFuncTemp, nwTemp):
assert nwGUI.docEditor.getText() == " " assert nwGUI.docEditor.getText() == " "
nwGUI.docEditor.clear() nwGUI.docEditor.clear()
# Insert text from file
nwGUI.closeDocument()
# First, with no path
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: [])
assert not nwGUI.importDocument()
# Then with a path, but an invalid one
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: [" "])
assert not nwGUI.importDocument()
# Then a valid path, but bot a file that exists
theFile = path.join(nwTemp, "import.txt")
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: [theFile])
assert not nwGUI.importDocument()
# Create the file and try again, but with no target document open
with open(theFile, mode="w+", encoding="utf8") as outFile:
outFile.write("Foo")
assert not nwGUI.importDocument()
# Open the document from before, and add some text to it
nwGUI.openDocument("0e17daca5f3e1")
nwGUI.docEditor.setText("Bar")
assert nwGUI.docEditor.getText() == "Bar"
# The document isn't empty, so the message box should pop
monkeypatch.setattr(QMessageBox, "question", lambda *args, **kwargs: QMessageBox.No)
assert not nwGUI.importDocument()
assert nwGUI.docEditor.getText() == "Bar"
# Finally, accept the replaced text, this time we use the menu entry to trigger it
monkeypatch.setattr(QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes)
nwGUI.mainMenu.aImportFile.activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Foo"
# qtbot.stopForInteraction() # qtbot.stopForInteraction()
nwGUI.closeMain() nwGUI.closeMain()
nwGUI.close() nwGUI.close()
+1 -1
View File
@@ -580,7 +580,7 @@ def testOrphanedFiles(nwDummy, nwLipsum):
assert theProject.closeProject() assert theProject.closeProject()
@pytest.mark.project @pytest.mark.project
def testOldProject(nwDummy, nwOldProj, mnkQtDialogs): def testOldProject(nwDummy, nwOldProj):
theProject = NWProject(nwDummy) theProject = NWProject(nwDummy)
theProject.mainConf.showGUI = False theProject.mainConf.showGUI = False