2015-04-16 01:16:57 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-04-16 01:16:57 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Test widgets in miscwidgets module."""
|
2015-04-20 22:59:35 +02:00
|
|
|
|
2015-04-16 01:16:57 +02:00
|
|
|
from unittest import mock
|
2016-08-03 13:08:25 +02:00
|
|
|
from PyQt5.QtCore import Qt, QSize
|
|
|
|
from PyQt5.QtWidgets import QApplication, QWidget
|
2015-04-16 01:16:57 +02:00
|
|
|
import pytest
|
|
|
|
|
2016-08-03 13:08:25 +02:00
|
|
|
from qutebrowser.misc import miscwidgets
|
2015-04-16 01:16:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCommandLineEdit:
|
2015-04-20 22:59:35 +02:00
|
|
|
|
2015-04-16 01:16:57 +02:00
|
|
|
"""Tests for CommandLineEdit widget."""
|
|
|
|
|
|
|
|
@pytest.yield_fixture
|
|
|
|
def cmd_edit(self, qtbot):
|
2015-04-20 22:59:35 +02:00
|
|
|
"""Fixture to initialize a CommandLineEdit."""
|
2016-08-03 13:08:25 +02:00
|
|
|
cmd_edit = miscwidgets.CommandLineEdit(None)
|
2015-04-16 01:16:57 +02:00
|
|
|
cmd_edit.set_prompt(':')
|
|
|
|
qtbot.add_widget(cmd_edit)
|
|
|
|
assert cmd_edit.text() == ''
|
|
|
|
yield cmd_edit
|
|
|
|
|
2015-04-19 22:13:47 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_clipboard(self, mocker):
|
2015-04-20 22:59:35 +02:00
|
|
|
"""Fixture to mock QApplication.clipboard.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The mocked QClipboard object.
|
2015-04-16 01:16:57 +02:00
|
|
|
"""
|
2015-04-19 22:13:47 +02:00
|
|
|
mocker.patch.object(QApplication, 'clipboard')
|
|
|
|
clipboard = mock.MagicMock()
|
|
|
|
clipboard.supportsSelection.return_value = True
|
|
|
|
QApplication.clipboard.return_value = clipboard
|
|
|
|
return clipboard
|
2015-04-16 01:16:57 +02:00
|
|
|
|
|
|
|
def test_position(self, qtbot, cmd_edit):
|
|
|
|
"""Test cursor position based on the prompt."""
|
|
|
|
qtbot.keyClicks(cmd_edit, ':hello')
|
|
|
|
assert cmd_edit.text() == ':hello'
|
|
|
|
assert cmd_edit.cursorPosition() == len(':hello')
|
|
|
|
|
|
|
|
cmd_edit.home(mark=True)
|
|
|
|
assert cmd_edit.cursorPosition() == len(':hello')
|
|
|
|
qtbot.keyClick(cmd_edit, Qt.Key_Delete)
|
|
|
|
assert cmd_edit.text() == ':'
|
|
|
|
qtbot.keyClick(cmd_edit, Qt.Key_Backspace)
|
|
|
|
assert cmd_edit.text() == ':'
|
|
|
|
|
|
|
|
qtbot.keyClicks(cmd_edit, 'hey again')
|
|
|
|
assert cmd_edit.text() == ':hey again'
|
|
|
|
|
|
|
|
def test_invalid_prompt(self, qtbot, cmd_edit):
|
2015-04-20 22:59:35 +02:00
|
|
|
"""Test preventing of an invalid prompt being entered."""
|
2015-04-16 01:16:57 +02:00
|
|
|
qtbot.keyClicks(cmd_edit, '$hello')
|
|
|
|
assert cmd_edit.text() == ''
|
2016-08-03 13:08:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WrappedWidget(QWidget):
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return QSize(23, 42)
|
|
|
|
|
|
|
|
|
|
|
|
class TestWrapperLayout:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def container(self, qtbot):
|
|
|
|
wrapped = WrappedWidget()
|
|
|
|
parent = QWidget()
|
|
|
|
qtbot.add_widget(wrapped)
|
|
|
|
qtbot.add_widget(parent)
|
|
|
|
miscwidgets.WrapperLayout(wrapped, parent)
|
|
|
|
return parent
|
|
|
|
|
|
|
|
def test_size_hint(self, container):
|
|
|
|
assert container.sizeHint() == QSize(23, 42)
|