From 9f8dd4abf3f756516fd6ebae8dd61f4c1899e505 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 28 Sep 2020 23:40:49 +0200 Subject: [PATCH] Monkeypatched a couple more file dialogs --- nw/gui/custom.py | 2 ++ nw/gui/projload.py | 27 ++++++++--------- nw/gui/writingstats.py | 19 ++++++------ tests/reference/novelwriter_prefs.conf | 2 +- tests/test_dialogs.py | 42 ++++++++++++++++++++------ 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/nw/gui/custom.py b/nw/gui/custom.py index e4e9f0c9..03fc6318 100644 --- a/nw/gui/custom.py +++ b/nw/gui/custom.py @@ -443,6 +443,8 @@ class VerticalTabBar(QTabBar): class QuotesDialog(QDialog): + selectedQuote = "" + def __init__(self, theParent=None, currentQuote="\""): QDialog.__init__(self, parent=theParent) diff --git a/nw/gui/projload.py b/nw/gui/projload.py index c19175be..200d8a93 100644 --- a/nw/gui/projload.py +++ b/nw/gui/projload.py @@ -178,20 +178,19 @@ class GuiProjectLoad(QDialog): """Browse for a folder path. """ logger.verbose("GuiProjectLoad browse button clicked") - if self.mainConf.showGUI: - dlgOpt = QFileDialog.Options() - dlgOpt |= QFileDialog.DontUseNativeDialog - projFile, _ = QFileDialog.getOpenFileName( - self, "Open novelWriter Project", "", - "novelWriter Project File (%s);;All Files (*)" % nwFiles.PROJ_FILE, - options=dlgOpt - ) - if projFile: - thePath = path.abspath(path.dirname(projFile)) - self.selPath.setText(thePath) - self.openPath = thePath - self.openState = self.OPEN_STATE - self.accept() + dlgOpt = QFileDialog.Options() + dlgOpt |= QFileDialog.DontUseNativeDialog + projFile, _ = QFileDialog.getOpenFileName( + self, "Open novelWriter Project", "", + "novelWriter Project File (%s);;All Files (*)" % nwFiles.PROJ_FILE, + options=dlgOpt + ) + if projFile: + thePath = path.abspath(path.dirname(projFile)) + self.selPath.setText(thePath) + self.openPath = thePath + self.openState = self.OPEN_STATE + self.accept() return diff --git a/nw/gui/writingstats.py b/nw/gui/writingstats.py index dd272b8b..4c27520e 100644 --- a/nw/gui/writingstats.py +++ b/nw/gui/writingstats.py @@ -326,16 +326,15 @@ class GuiWritingStats(QDialog): if not path.isdir(saveDir): saveDir = self.mainConf.homePath - if self.mainConf.showGUI: - dlgOpt = QFileDialog.Options() - dlgOpt |= QFileDialog.DontUseNativeDialog - saveTo = QFileDialog.getSaveFileName( - self, "Save Document As", savePath, options=dlgOpt - ) - if saveTo[0]: - savePath = saveTo[0] - else: - return False + dlgOpt = QFileDialog.Options() + dlgOpt |= QFileDialog.DontUseNativeDialog + saveTo = QFileDialog.getSaveFileName( + self, "Save Document As", savePath, options=dlgOpt + ) + if saveTo: + savePath = saveTo[0] + else: + return False self.mainConf.setLastPath(savePath) diff --git a/tests/reference/novelwriter_prefs.conf b/tests/reference/novelwriter_prefs.conf index 6b894f55..608116ea 100644 --- a/tests/reference/novelwriter_prefs.conf +++ b/tests/reference/novelwriter_prefs.conf @@ -49,7 +49,7 @@ highlightquotes = False highlightemph = False [Backup] -backuppath = +backuppath = some/dir backuponclose = True askbeforebackup = True diff --git a/tests/test_dialogs.py b/tests/test_dialogs.py index 464b39bc..0076e181 100644 --- a/tests/test_dialogs.py +++ b/tests/test_dialogs.py @@ -14,7 +14,7 @@ from os import path from PyQt5.QtCore import Qt, QItemSelectionModel from PyQt5.QtWidgets import ( QDialogButtonBox, QTreeWidgetItem, QListWidgetItem, QDialog, QAction, - QMessageBox, QFileDialog + QMessageBox, QFileDialog, QFontDialog ) from nw.gui import ( @@ -198,7 +198,7 @@ def testItemEditor(qtbot, yesToAll, nwFuncTemp, nwTempGUI, nwRef, nwTemp): # qtbot.stopForInteraction() @pytest.mark.gui -def testWritingStatsExport(qtbot, yesToAll, nwFuncTemp, nwTemp): +def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): nwGUI = nw.main(["--testmode", "--config=%s" % nwFuncTemp, "--data=%s" % nwTemp]) qtbot.addWidget(nwGUI) nwGUI.show() @@ -263,6 +263,10 @@ def testWritingStatsExport(qtbot, yesToAll, nwFuncTemp, nwTemp): assert isinstance(sessLog, GuiWritingStats) qtbot.wait(stepDelay) + monkeypatch.setattr(QFileDialog, "getSaveFileName", lambda *args, **kwargs: []) + assert not sessLog._saveData(sessLog.FMT_CSV) + + monkeypatch.setattr(QFileDialog, "getSaveFileName", lambda ss, tt, pp, options: [pp]) assert sessLog._saveData(sessLog.FMT_CSV) qtbot.wait(stepDelay) assert sessLog._saveData(sessLog.FMT_JSON) @@ -913,9 +917,16 @@ def testLoadProject(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp): nwLoad._keyPressDelete() assert nwLoad.listBox.topLevelItemCount() == recentCount - 1 - nwLoad.close() + getFile = path.join(nwMinimal, "nwProject.nwx") + monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwargs: (getFile, None)) + qtbot.mouseClick(nwLoad.browseButton, Qt.LeftButton) + assert nwLoad.openPath == nwMinimal + assert nwLoad.openState == nwLoad.OPEN_STATE # qtbot.stopForInteraction() + + nwLoad.close() nwGUI.closeMain() + nwGUI.close() @pytest.mark.gui def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpConf): @@ -927,8 +938,6 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC assert nwGUI.openProject(nwMinimal) - monkeypatch.setattr(QMessageBox, "information", lambda *args, **kwargs: None) - monkeypatch.setattr(GuiPreferences, "exec_", lambda *args: None) monkeypatch.setattr(GuiPreferences, "result", lambda *args: QDialog.Accepted) nwGUI.mainMenu.aPreferences.activate(QAction.Trigger) @@ -952,7 +961,7 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC qtbot.wait(keyDelay) tabGeneral = nwPrefs.tabGeneral nwPrefs._tabBox.setCurrentWidget(tabGeneral) - tabGeneral.backupPath = nwTemp + tabGeneral.backupPath = "no/where" qtbot.wait(keyDelay) assert not tabGeneral.preferDarkIcons.isChecked() @@ -964,6 +973,16 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC qtbot.mouseClick(tabGeneral.showFullPath, Qt.LeftButton) assert not tabGeneral.showFullPath.isChecked() + # Check Browse button + monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *args, **kwargs: "") + assert not tabGeneral._backupFolder() + monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *args, **kwargs: "some/dir") + qtbot.mouseClick(tabGeneral.backupGetPath, Qt.LeftButton) + + # Check font button + monkeypatch.setattr(QFontDialog, "getFont", lambda font, obj: (font, True)) + qtbot.mouseClick(tabGeneral.fontButton, Qt.LeftButton) + qtbot.wait(keyDelay) assert not tabGeneral.backupOnClose.isChecked() qtbot.mouseClick(tabGeneral.backupOnClose, Qt.LeftButton) @@ -979,6 +998,9 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC tabLayout = nwPrefs.tabLayout nwPrefs._tabBox.setCurrentWidget(tabLayout) + qtbot.wait(keyDelay) + qtbot.mouseClick(tabLayout.fontButton, Qt.LeftButton) + qtbot.wait(keyDelay) tabLayout.textStyleSize.setValue(13) tabLayout.textFlowMax.setValue(700) @@ -1050,12 +1072,14 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC assert not tabAutoRep.autoReplaceDash.isEnabled() assert not tabAutoRep.autoReplaceDots.isEnabled() + monkeypatch.setattr(QuotesDialog, "selectedQuote", "'") + monkeypatch.setattr(QuotesDialog, "exec_", lambda *args: QDialog.Accepted) + qtbot.mouseClick(tabAutoRep.btnDoubleStyleC, Qt.LeftButton) + # Save and Check Config qtbot.mouseClick(nwPrefs.buttonBox.button(QDialogButtonBox.Ok), Qt.LeftButton) assert tmpConf.confChanged - assert tmpConf.backupPath == nwTemp - tmpConf.backupPath = "" tmpConf.lastPath = "" assert nwGUI.mainConf.saveConfig() @@ -1070,7 +1094,7 @@ def testPreferences(qtbot, monkeypatch, yesToAll, nwMinimal, nwTemp, nwRef, tmpC ignoreLines = [ 2, # Timestamp 11, 12, 13, 14, 15, 16, 17, # Window sizes - 7, 25, # Fonts (depends in system default) + 7, 25, # Fonts (depends on system default) ] assert cmpFiles(testConf, refConf, ignoreLines)