diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 5a39a8cb..ed36cfdf 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -423,10 +423,10 @@ class GuiDocEditor(QTextEdit): return True def getCursorPosition(self): - """Find the cursor position in the document. + """Find the cursor position in the document. If the editor has a + selection, return the position of the end of the selection. """ - theCursor = self.textCursor() - return theCursor.position() + return self.textCursor().selectionEnd() def setCursorLine(self, theLine): """Move the cursor to a given line in the document. diff --git a/tests/test_gui.py b/tests/test_gui.py index 55cc46e4..dca5da31 100644 --- a/tests/test_gui.py +++ b/tests/test_gui.py @@ -552,6 +552,7 @@ def testEditFormatMenu(qtbot, nwLipsum, nwTemp): # qtbot.stopForInteraction() nwGUI.closeMain() +@pytest.mark.gui def testContextMenu(qtbot, nwLipsum, nwTemp): nwGUI = nw.main(["--testmode", "--config=%s" % nwLipsum, "--data=%s" % nwTemp]) @@ -726,6 +727,136 @@ def testInsertMenu(qtbot, nwFuncTemp, nwTemp): # qtbot.stopForInteraction() nwGUI.closeMain() +@pytest.mark.gui +def testTextSearch(qtbot, nwLipsum, nwTemp): + + nwGUI = nw.main(["--testmode", "--config=%s" % nwLipsum, "--data=%s" % nwTemp]) + qtbot.addWidget(nwGUI) + nwGUI.show() + qtbot.waitForWindowShown(nwGUI) + qtbot.wait(stepDelay) + + nwGUI.theProject.projTree.setSeed(42) + assert nwGUI.openProject(nwLipsum) + assert nwGUI.openDocument("4c4f28287af27") + origText = nwGUI.docEditor.getText() + qtbot.wait(stepDelay) + + # Select the Word "est" + assert nwGUI.docEditor.setCursorPosition(618) + nwGUI.docEditor._makeSelection(QTextCursor.WordUnderCursor) + theCursor = nwGUI.docEditor.textCursor() + assert theCursor.selectedText() == "est" + + # Activate Search + nwGUI.mainMenu.aFind.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.isVisible() + assert nwGUI.docEditor.docSearch.getSearchText() == "est" + + # Find Next by Menu + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 1272) < 3 + + # Find Next by Button + qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) + assert abs(nwGUI.docEditor.getCursorPosition() - 1486) < 3 + + # Activate Loop Search + nwGUI.docEditor.docSearch.toggleLoop.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.toggleLoop.isChecked() + assert nwGUI.docEditor.docSearch.doLoop + + # Find Next by Menu Search > Find Next + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 620) < 3 + + # Close Search + nwGUI.docEditor.docSearch.cancelSearch.activate(QAction.Trigger) + assert not nwGUI.docEditor.docSearch.isVisible() + assert nwGUI.docEditor.setCursorPosition(15) + + # Toggle Search Again with Header Button + qtbot.mouseClick(nwGUI.docEditor.docHeader.searchButton, Qt.LeftButton, delay=keyDelay) + assert nwGUI.docEditor.docSearch.setSearchText("") + assert nwGUI.docEditor.docSearch.isVisible() + + # Enable RegEx Search + nwGUI.docEditor.docSearch.toggleRegEx.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.toggleRegEx.isChecked() + assert nwGUI.docEditor.docSearch.isRegEx + + # Set Invalid RegEx + assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus[") + qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) + assert nwGUI.docEditor.getCursorPosition() < 3 # No result + + # Set Valid RegEx + assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus") + qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) + assert abs(nwGUI.docEditor.getCursorPosition() - 196) < 3 + + # Find Next and then Prev + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 297) < 3 + nwGUI.mainMenu.aFindPrev.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 196) < 3 + + # Make RegEx Case Sensitive + nwGUI.docEditor.docSearch.toggleCase.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.toggleCase.isChecked() + assert nwGUI.docEditor.docSearch.isCaseSense + + # Find Next (One Result) + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 599) < 3 + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + assert abs(nwGUI.docEditor.getCursorPosition() - 599) < 3 + + # Trigger Replace + nwGUI.mainMenu.aReplace.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.setReplaceText("foo") + + # Disable RegEx Case Sensitive + nwGUI.docEditor.docSearch.toggleCase.activate(QAction.Trigger) + assert not nwGUI.docEditor.docSearch.toggleCase.isChecked() + assert not nwGUI.docEditor.docSearch.isCaseSense + + # Toggle Replace Preserve Case + nwGUI.docEditor.docSearch.toggleMatchCap.activate(QAction.Trigger) + assert nwGUI.docEditor.docSearch.toggleMatchCap.isChecked() + assert nwGUI.docEditor.docSearch.doMatchCap + + # Replace "Sus" with "Foo" via Menu + nwGUI.mainMenu.aReplaceNext.activate(QAction.Trigger) + assert nwGUI.docEditor.getText()[596:607] == "Foopendisse" + + # Find Next to Loop File + nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) + + # Replace "sus" with "foo" via Replace Button + qtbot.mouseClick(nwGUI.docEditor.docSearch.replaceButton, Qt.LeftButton, delay=keyDelay) + assert nwGUI.docEditor.getText()[193:201] == "foocipit" + + # Revert Last Two Replaces + assert nwGUI.docEditor.docAction(nwDocAction.UNDO) + assert nwGUI.docEditor.docAction(nwDocAction.UNDO) + assert nwGUI.docEditor.getText() == origText + + # Disable RegEx Search + nwGUI.docEditor.docSearch.toggleRegEx.activate(QAction.Trigger) + assert not nwGUI.docEditor.docSearch.toggleRegEx.isChecked() + assert not nwGUI.docEditor.docSearch.isRegEx + + # assert nwGUI.docEditor.setCursorPosition(0) + nwGUI.docEditor.docSearch.searchBox.setFocus(True) + assert nwGUI.docEditor.docSearch.cycleFocus(True) + assert nwGUI.docEditor.docSearch.replaceBox.hasFocus() + assert nwGUI.docEditor.docSearch.cycleFocus(True) + assert nwGUI.docEditor.docSearch.searchBox.hasFocus() + + # qtbot.stopForInteraction() + nwGUI.closeMain() + @pytest.mark.gui def testOutline(qtbot, nwLipsum, nwTemp):