Skip to content

Commit

Permalink
Merge from 3.x: PR #6333
Browse files Browse the repository at this point in the history
Fixes #5813
  • Loading branch information
ccordoba12 committed Feb 3, 2018
2 parents 5851a3f + 1292810 commit 1c84364
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
33 changes: 27 additions & 6 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
#
# -----------------------------------------------------------------------------
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
#
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
# -----------------------------------------------------------------------------

"""
Tests for the main window
Expand All @@ -23,15 +25,16 @@
from numpy.testing import assert_array_equal
import pytest
from qtpy import PYQT4, PYQT5, PYQT_VERSION
from qtpy.QtCore import Qt, QTimer
from qtpy.QtCore import Qt, QTimer, QEvent
from qtpy.QtTest import QTest
from qtpy.QtWidgets import QApplication, QFileDialog, QLineEdit
from qtpy.QtWidgets import QApplication, QFileDialog, QLineEdit, QTabBar

from spyder import __trouble_url__
from spyder.app.mainwindow import MainWindow # Tests fail without this import
from spyder.app import start
from spyder.config.base import get_home_dir
from spyder.config.main import CONF
from spyder.widgets.dock import TabFilter
from spyder.plugins.runconfig import RunConfiguration
from spyder.py3compat import PY2, to_text_string
from spyder.utils.ipython.kernelspec import SpyderKernelSpec
Expand Down Expand Up @@ -163,7 +166,7 @@ def close_window():
@pytest.mark.use_introspection
@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt' or (not PY2 and PYQT_VERSION < "5.9.0"),
reason="It times out on AppVeyor and fails on PY3 and PyQt 5.6")
reason="Times out on AppVeyor and fails on PY3/PyQt 5.6")
@pytest.mark.timeout(timeout=45, method='thread')
def test_calltip(main_window, qtbot):
"""Test that the calltip in editor is hidden when matching ')' is found."""
Expand All @@ -181,7 +184,7 @@ def test_calltip(main_window, qtbot):

qtbot.keyPress(code_editor, Qt.Key_ParenLeft, delay=3000)
qtbot.keyPress(code_editor, Qt.Key_A, delay=1000)
qtbot.waitUntil(lambda: calltip.isVisible(), timeout=1000)
qtbot.waitUntil(lambda: calltip.isVisible(), timeout=3000)

qtbot.keyPress(code_editor, Qt.Key_ParenRight, delay=1000)
qtbot.keyPress(code_editor, Qt.Key_Space)
Expand Down Expand Up @@ -1224,5 +1227,23 @@ def test_troubleshooting_menu_item_and_url(monkeypatch):
raise


@flaky(max_runs=3)
@pytest.mark.slow
def test_tabfilter_typeerror_full(main_window):
"""Test for #5813 ; event filter handles None indicies when moving tabs."""
MockEvent = MagicMock()
MockEvent.return_value.type.return_value = QEvent.MouseMove
MockEvent.return_value.pos.return_value = 0
mockEvent_instance = MockEvent()

test_tabbar = main_window.findChildren(QTabBar)[0]
test_tabfilter = TabFilter(test_tabbar, main_window)
test_tabfilter.from_index = None
test_tabfilter.moving = True

assert test_tabfilter.eventFilter(None, mockEvent_instance)
assert mockEvent_instance.pos.call_count == 1


if __name__ == "__main__":
pytest.main()
52 changes: 52 additions & 0 deletions spyder/plugins/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright © Spyder Project Contributors
#
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
# -----------------------------------------------------------------------------

"""Tests for the base plugin classes ('__init__.py')."""

# Standard library imports
try:
from unittest.mock import Mock, MagicMock
except ImportError:
from mock import Mock, MagicMock # Python 2

# 3rd party imports
import pytest
from qtpy.QtCore import QEvent

# Local imports
from spyder.widgets.dock import TabFilter


# =============================================================================
# Tests
# =============================================================================
def test_tabfilter_typeerror_simple():
"""Test for #5813 ; event filter handles None indicies when moving tabs."""
MockEvent = MagicMock()
MockEvent.return_value.type.return_value = QEvent.MouseMove
MockEvent.return_value.pos.return_value = 0
mockEvent_instance = MockEvent()

MockTabBar = MagicMock()
MockTabBar.return_value.tabAt.return_value = 0
mockTabBar_instance = MockTabBar()

MockMainWindow = Mock()
mockMainWindow_instance = MockMainWindow()

test_tabfilter = TabFilter(mockTabBar_instance, mockMainWindow_instance)
test_tabfilter.from_index = None
test_tabfilter.moving = True

assert test_tabfilter.eventFilter(None, mockEvent_instance)
assert mockEvent_instance.pos.call_count == 1
assert mockTabBar_instance.tabAt.call_count == 1


if __name__ == "__main__":
pytest.main()
11 changes: 7 additions & 4 deletions spyder/widgets/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _fix_cursor(self, from_index, to_index):
new_pos = self.dock_tabbar.mapToGlobal(QPoint(x + delta, y))
cursor.setPos(new_pos)

def eventFilter(self, obj, event):
def eventFilter(self, obj, event):
"""Filter mouse press events.
Events that are captured and not propagated return True. Events that
Expand All @@ -81,7 +81,10 @@ def eventFilter(self, obj, event):
self.tab_pressed(event)
return False
if event_type == QEvent.MouseMove:
self.tab_moved(event)
try:
self.tab_moved(event)
except TypeError:
pass
return True
if event_type == QEvent.MouseButtonRelease:
self.tab_released(event)
Expand Down Expand Up @@ -112,11 +115,11 @@ def tab_moved(self, event):
QApplication.setOverrideCursor(Qt.ClosedHandCursor)
self.moving = True

if self.to_index == -1:
if self.to_index in (-1, None):
self.to_index = self.from_index

from_index, to_index = self.from_index, self.to_index
if from_index != to_index and from_index != -1 and to_index != -1:
if from_index not in (to_index, -1, None):
self.move_tab(from_index, to_index)
self._fix_cursor(from_index, to_index)
self.from_index = to_index
Expand Down
5 changes: 3 additions & 2 deletions spyder/widgets/tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ def test_tab_moves_focus_from_search_to_replace(editor_find_replace_bot):
assert finder.replace_text.hasFocus()


@flaky(max_runs=3)
@pytest.mark.skipif(platform.startswith('linux'), reason="Fails on Linux.")
def test_tab_copies_find_to_replace(editor_find_replace_bot):
"""Check that text in the find box is copied to the replace box on tab
Expand All @@ -536,8 +537,8 @@ def test_tab_copies_find_to_replace(editor_find_replace_bot):
finder.show_replace()
finder.search_text.setFocus()
finder.search_text.set_current_text('This is some test text!')
qtbot.keyPress(finder.search_text, Qt.Key_Tab)
qtbot.wait(100)
qtbot.keyClick(finder.search_text, Qt.Key_Tab)
qtbot.wait(500)
assert finder.replace_text.currentText() == 'This is some test text!'


Expand Down

0 comments on commit 1c84364

Please sign in to comment.