Skip to content

Commit

Permalink
Merge pull request #4830 from dalthviz/fixes_issue_4824
Browse files Browse the repository at this point in the history
PR: Fix error when printing source code
  • Loading branch information
ccordoba12 authored Jul 28, 2017
2 parents 7eef7ce + 3bf9b27 commit f0e4d3e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pytest
from qtpy import PYQT5, PYQT_VERSION
from qtpy.QtCore import Qt, QTimer
from qtpy.QtPrintSupport import QAbstractPrintDialog, QPrintDialog
from qtpy.QtTest import QTest
from qtpy.QtWidgets import QApplication, QFileDialog, QLineEdit

Expand Down Expand Up @@ -72,6 +73,15 @@ def open_file_in_editor(main_window, fname, directory=None):
QTest.keyClick(w, Qt.Key_Enter)


def print_file(main_window, result, selection_option=False):
"""Check if the print dialog has the correct option"""
top_level_widgets = QApplication.topLevelWidgets()
for w in top_level_widgets:
if isinstance(w, QPrintDialog):
result.append(selection_option == w.testOption(QAbstractPrintDialog.PrintSelection))
QTest.keyClick(w, Qt.Key_Escape)


def reset_run_code(qtbot, shell, code_editor, nsb):
"""Reset state after a run code test"""
with qtbot.waitSignal(shell.executed):
Expand Down Expand Up @@ -165,6 +175,33 @@ def test_calltip(main_window, qtbot):
main_window.editor.close_file()


@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt',
reason="QtWarningMsg: QPrintDialog: Cannot be used on non-native printer")
def test_print_file(main_window, qtbot, tmpdir):
"""Test print funtionality."""
# ---- Load test file ----
test_file = osp.join(LOCATION, 'script.py')
editor = main_window.editor
editor.load(test_file)
code_editor = editor.get_focus_widget()

# --- Test without selecting text ---
# Set a timer to manipulate the print dialog while it's running
result = []
QTimer.singleShot(2000, lambda: print_file(main_window, result))
editor.print_file()
assert True in result

# --- Test selecting text --
# Select all the text and set a timer to manipulate the print dialog while it's running
result = []
code_editor.selectAll()
QTimer.singleShot(2000, lambda: print_file(main_window, result, selection_option=True))
editor.print_file()
assert True in result


@flaky(max_runs=3)
def test_runconfig_workdir(main_window, qtbot, tmpdir):
"""Test runconfig workdir options."""
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ def print_file(self):
header_font=self.get_plugin_font('printer_header'))
printDialog = QPrintDialog(printer, editor)
if editor.has_selected_text():
printDialog.addEnabledOption(QAbstractPrintDialog.PrintSelection)
printDialog.setOption(QAbstractPrintDialog.PrintSelection, True)
self.redirect_stdio.emit(False)
answer = printDialog.exec_()
self.redirect_stdio.emit(True)
Expand Down

0 comments on commit f0e4d3e

Please sign in to comment.