diff --git a/qutebrowser/test/stubs.py b/qutebrowser/test/stubs.py index e5055e587..869351916 100644 --- a/qutebrowser/test/stubs.py +++ b/qutebrowser/test/stubs.py @@ -82,7 +82,7 @@ class FakeWebElement: """A stub for QWebElement.""" def __init__(self, geometry=None, frame=None, null=False, visibility='', - display='', attributes=None): + display='', attributes=None, tagname=None, classes=None): """Constructor. Args: @@ -92,6 +92,8 @@ class FakeWebElement: visibility: The CSS visibility style property calue. display: The CSS display style property calue. attributes: Boolean HTML attributes to be added. + tagname: The tag name. + classes: HTML classes to be added. Raise: ValueError if element is not null and geometry/frame are not given. @@ -99,9 +101,11 @@ class FakeWebElement: self.geometry = Mock(return_value=geometry) self.webFrame = Mock(return_value=frame) self.isNull = Mock(return_value=null) + self.tagName = Mock(return_value=tagname) self._visibility = visibility self._display = display self._attributes = attributes + self._classes = classes def styleProperty(self, name, strategy): """Return the CSS style property named name. @@ -130,6 +134,22 @@ class FakeWebElement: else: return name in self._attributes + def attribute(self, name): + """Get the attribute named name.""" + if self._attributes is None: + return '' + try: + return self._attributes[name] + except KeyError: + return '' + + def classes(self): + """Get the classes of the object.""" + if self._classes is not None: + return self._classes.split(' ') + else: + return [] + class FakeWebFrame: diff --git a/qutebrowser/test/utils/test_webelem.py b/qutebrowser/test/utils/test_webelem.py index ce06bfecf..bc44c947a 100644 --- a/qutebrowser/test/utils/test_webelem.py +++ b/qutebrowser/test/utils/test_webelem.py @@ -25,7 +25,7 @@ from PyQt5.QtCore import QRect, QPoint import qutebrowser.utils.webelem as webelem from qutebrowser.test.stubs import (FakeWebElement, FakeWebFrame, - FakeChildrenFrame) + FakeChildrenFrame, ConfigStub) class IsVisibleInvalidTests(unittest.TestCase): @@ -296,5 +296,199 @@ class GetChildFramesTests(unittest.TestCase): frame.childFrames.assert_called_once_with() +class IsEditableTests(unittest.TestCase): + + """Tests for is_editable.""" + + def setUp(self): + webelem.config = None + + def test_input_plain(self): + """Test with plain input element.""" + elem = FakeWebElement(tagname='input') + self.assertTrue(webelem.is_editable(elem)) + + def test_input_text(self): + """Test with text input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'text'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_text_caps(self): + """Test with text input element with caps attributes.""" + elem = FakeWebElement(tagname='INPUT', attributes={'TYPE': 'TEXT'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_email(self): + """Test with email input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'email'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_url(self): + """Test with url input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'url'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_tel(self): + """Test with tel input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'tel'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_number(self): + """Test with number input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'number'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_password(self): + """Test with password input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'password'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_search(self): + """Test with search input element.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'search'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_input_button(self): + """Button should not be editable.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'button'}) + self.assertFalse(webelem.is_editable(elem)) + + def test_input_checkbox(self): + """Checkbox should not be editable.""" + elem = FakeWebElement(tagname='input', attributes={'type': 'checkbox'}) + self.assertFalse(webelem.is_editable(elem)) + + def test_textarea(self): + """Test textarea element.""" + elem = FakeWebElement(tagname='textarea') + self.assertTrue(webelem.is_editable(elem)) + + def test_select(self): + """Test selectbox.""" + elem = FakeWebElement(tagname='select') + self.assertTrue(webelem.is_editable(elem)) + + def test_input_disabled(self): + """Test disabled input element.""" + elem = FakeWebElement(tagname='input', attributes={'disabled': None}) + self.assertFalse(webelem.is_editable(elem)) + + def test_input_readonly(self): + """Test readonly input element.""" + elem = FakeWebElement(tagname='input', attributes={'readonly': None}) + self.assertFalse(webelem.is_editable(elem)) + + def test_textarea_disabled(self): + """Test disabled textarea element.""" + elem = FakeWebElement(tagname='textarea', + attributes={'disabled': None}) + self.assertFalse(webelem.is_editable(elem)) + + def test_textarea_readonly(self): + """Test readonly textarea element.""" + elem = FakeWebElement(tagname='textarea', + attributes={'readonly': None}) + self.assertFalse(webelem.is_editable(elem)) + + def test_embed_true(self): + """Test embed-element with insert-mode-on-plugins true.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': True}}) + elem = FakeWebElement(tagname='embed') + self.assertTrue(webelem.is_editable(elem)) + + def test_applet_true(self): + """Test applet-element with insert-mode-on-plugins true.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': True}}) + elem = FakeWebElement(tagname='applet') + self.assertTrue(webelem.is_editable(elem)) + + def test_embed_false(self): + """Test embed-element with insert-mode-on-plugins false.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': False}}) + elem = FakeWebElement(tagname='embed') + self.assertFalse(webelem.is_editable(elem)) + + def test_applet_false(self): + """Test applet-element with insert-mode-on-plugins false.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': False}}) + elem = FakeWebElement(tagname='applet') + self.assertFalse(webelem.is_editable(elem)) + + def test_object_no_type(self): + """Test object-element without type.""" + elem = FakeWebElement(tagname='object') + self.assertFalse(webelem.is_editable(elem)) + + def test_object_image(self): + """Test object-element with image type.""" + elem = FakeWebElement(tagname='object', + attributes={'type': 'image/gif'}) + self.assertFalse(webelem.is_editable(elem)) + + def test_object_application(self): + """Test object-element with application type.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': True}}) + elem = FakeWebElement(tagname='object', + attributes={'type': 'application/foo'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_object_application_false(self): + """Test object-element with application type but not ...-on-plugins.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': False}}) + elem = FakeWebElement(tagname='object', + attributes={'type': 'application/foo'}) + self.assertFalse(webelem.is_editable(elem)) + + def test_object_classid(self): + """Test object-element with classid.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': True}}) + elem = FakeWebElement(tagname='object', + attributes={'type': 'foo', 'classid': 'foo'}) + self.assertTrue(webelem.is_editable(elem)) + + def test_object_classid_false(self): + """Test object-element with classid but not insert-mode-on-plugins.""" + webelem.config = ConfigStub({'input': + {'insert-mode-on-plugins': False}}) + elem = FakeWebElement(tagname='object', + attributes={'type': 'foo', 'classid': 'foo'}) + self.assertFalse(webelem.is_editable(elem)) + + def test_div_empty(self): + """Test div-element without class.""" + elem = FakeWebElement(tagname='div') + self.assertFalse(webelem.is_editable(elem)) + + def test_div_noneditable(self): + """Test div-element with non-editableclass.""" + elem = FakeWebElement(tagname='div', classes='foo-kix-bar') + self.assertFalse(webelem.is_editable(elem)) + + def test_div_xik(self): + """Test div-element with xik class.""" + elem = FakeWebElement(tagname='div', classes='foo kix-foo') + self.assertTrue(webelem.is_editable(elem)) + + def test_div_xik_caps(self): + """Test div-element with xik class in caps. + + This tests if classes are case sensitive as they should. + """ + elem = FakeWebElement(tagname='div', classes='KIX-FOO') + self.assertFalse(webelem.is_editable(elem)) + + def test_div_codemirror(self): + """Test div-element with codemirror class.""" + elem = FakeWebElement(tagname='div', classes='foo CodeMirror-foo') + self.assertTrue(webelem.is_editable(elem)) + + if __name__ == '__main__': unittest.main()