Clean up test_webelem
This commit is contained in:
parent
1c4aa67813
commit
80d4068ad5
@ -31,8 +31,25 @@ import qutebrowser.utils.webelem as webelem
|
||||
|
||||
class FakeWebElement:
|
||||
|
||||
"""A stub for QWebElement."""
|
||||
|
||||
def __init__(self, geometry=None, frame=None, null=False, visibility='',
|
||||
display=''):
|
||||
"""Constructor.
|
||||
|
||||
Args:
|
||||
geometry: The geometry of the QWebElement as QRect.
|
||||
frame: The QWebFrame the element is in.
|
||||
null: Whether the element is null or not.
|
||||
visibility: The CSS visibility style property calue.
|
||||
display: The CSS display style property calue.
|
||||
|
||||
Raise:
|
||||
ValueError if element is not null and geometry/frame are not given.
|
||||
"""
|
||||
if (not null) and (geometry is None or frame is None):
|
||||
raise ValueError("geometry and frame have to be set if element "
|
||||
"is not null!")
|
||||
self.geometry = Mock(return_value=geometry)
|
||||
self.webFrame = Mock(return_value=frame)
|
||||
self.isNull = Mock(return_value=null)
|
||||
@ -40,6 +57,14 @@ class FakeWebElement:
|
||||
self._display = display
|
||||
|
||||
def styleProperty(self, name, strategy):
|
||||
"""Return the CSS style property named name.
|
||||
|
||||
Only display/visibility and ComputedStyle are simulated.
|
||||
|
||||
Raise:
|
||||
ValueError if strategy is not ComputedStyle or name is not
|
||||
visibility/display.
|
||||
"""
|
||||
if strategy != QWebElement.ComputedStyle:
|
||||
raise ValueError("styleProperty called with strategy != "
|
||||
"ComputedStyle ({})!".format(strategy))
|
||||
@ -54,7 +79,18 @@ class FakeWebElement:
|
||||
|
||||
class FakeWebFrame:
|
||||
|
||||
def __init__(self, geometry, scroll, parent=None):
|
||||
"""A stub for QWebFrame."""
|
||||
|
||||
def __init__(self, geometry, scroll=None, parent=None):
|
||||
"""Constructor.
|
||||
|
||||
Args:
|
||||
geometry: The geometry of the frame as QRect.
|
||||
scroll: The scroll position as QPoint.
|
||||
parent: The parent frame.
|
||||
"""
|
||||
if scroll is None:
|
||||
scroll = QPoint(0, 0)
|
||||
self.geometry = Mock(return_value=geometry)
|
||||
self.scrollPosition = Mock(return_value=scroll)
|
||||
self.parentFrame = Mock(return_value=parent)
|
||||
@ -62,10 +98,17 @@ class FakeWebFrame:
|
||||
|
||||
class IsVisibleInvalidTests(TestCase):
|
||||
|
||||
"""Tests for is_visible with invalid elements."""
|
||||
|
||||
def setUp(self):
|
||||
self.frame = FakeWebFrame(QRect(0, 0, 100, 100), scroll=QPoint(0, 0))
|
||||
self.frame = FakeWebFrame(QRect(0, 0, 100, 100))
|
||||
|
||||
def test_nullelem(self):
|
||||
"""Passing an element with isNull() == True.
|
||||
|
||||
geometry() and webFrame() should not be called, and ValueError should
|
||||
be raised.
|
||||
"""
|
||||
elem = FakeWebElement(null=True)
|
||||
with self.assertRaises(ValueError):
|
||||
webelem.is_visible(elem, self.frame)
|
||||
@ -74,63 +117,85 @@ class IsVisibleInvalidTests(TestCase):
|
||||
self.assertFalse(elem.webFrame.called)
|
||||
|
||||
def test_invalid_invisible(self):
|
||||
elem = FakeWebElement(geometry=QRect(0, 0, 0, 0), frame=self.frame)
|
||||
"""Test elements with an invalid geometry which are invisible."""
|
||||
elem = FakeWebElement(QRect(0, 0, 0, 0), self.frame)
|
||||
self.assertFalse(elem.geometry().isValid())
|
||||
self.assertEqual(elem.geometry().x(), 0)
|
||||
self.assertFalse(webelem.is_visible(elem, self.frame))
|
||||
|
||||
def test_invalid_visible(self):
|
||||
elem = FakeWebElement(geometry=QRect(10, 10, 0, 0), frame=self.frame)
|
||||
"""Test elements with an invalid geometry which are visible.
|
||||
|
||||
This seems to happen sometimes in the real world, with real elements
|
||||
which *are* visible, but don't have a valid geometry.
|
||||
"""
|
||||
elem = FakeWebElement(QRect(10, 10, 0, 0), self.frame)
|
||||
self.assertFalse(elem.geometry().isValid())
|
||||
self.assertTrue(webelem.is_visible(elem, self.frame))
|
||||
|
||||
|
||||
class IsVisibleScrollTests(TestCase):
|
||||
|
||||
"""Tests for is_visible when the frame is scrolled."""
|
||||
|
||||
def setUp(self):
|
||||
self.frame = FakeWebFrame(QRect(0, 0, 100, 100), scroll=QPoint(10, 10))
|
||||
|
||||
def test_invisible(self):
|
||||
elem = FakeWebElement(geometry=QRect(5, 5, 4, 4), frame=self.frame)
|
||||
elem = FakeWebElement(QRect(5, 5, 4, 4), self.frame)
|
||||
self.assertFalse(webelem.is_visible(elem, self.frame))
|
||||
|
||||
def test_visible(self):
|
||||
elem = FakeWebElement(geometry=QRect(10, 10, 1, 1), frame=self.frame)
|
||||
elem = FakeWebElement(QRect(10, 10, 1, 1), self.frame)
|
||||
self.assertTrue(webelem.is_visible(elem, self.frame))
|
||||
|
||||
|
||||
class IsVisibleCssTests(TestCase):
|
||||
|
||||
"""Tests for is_visible with CSS attributes."""
|
||||
|
||||
def setUp(self):
|
||||
self.frame = FakeWebFrame(QRect(0, 0, 100, 100), scroll=QPoint(0, 0))
|
||||
self.frame = FakeWebFrame(QRect(0, 0, 100, 100))
|
||||
|
||||
def test_visibility_visible(self):
|
||||
elem = FakeWebElement(geometry=QRect(0, 0, 10, 10), frame=self.frame,
|
||||
"""Check that elements with "visibility = visible" are visible."""
|
||||
elem = FakeWebElement(QRect(0, 0, 10, 10), self.frame,
|
||||
visibility='visible')
|
||||
self.assertTrue(webelem.is_visible(elem, self.frame))
|
||||
|
||||
def test_visibility_hidden(self):
|
||||
elem = FakeWebElement(geometry=QRect(0, 0, 10, 10), frame=self.frame,
|
||||
"""Check that elements with "visibility = hidden" are not visible."""
|
||||
elem = FakeWebElement(QRect(0, 0, 10, 10), self.frame,
|
||||
visibility='hidden')
|
||||
self.assertFalse(webelem.is_visible(elem, self.frame))
|
||||
|
||||
def test_display_inline(self):
|
||||
elem = FakeWebElement(geometry=QRect(0, 0, 10, 10), frame=self.frame,
|
||||
"""Check that elements with "display = inline" are visible."""
|
||||
elem = FakeWebElement(QRect(0, 0, 10, 10), self.frame,
|
||||
display='inline')
|
||||
self.assertTrue(webelem.is_visible(elem, self.frame))
|
||||
|
||||
def test_display_none(self):
|
||||
elem = FakeWebElement(geometry=QRect(0, 0, 10, 10), frame=self.frame,
|
||||
display='none')
|
||||
"""Check that elements with "display = none" are not visible."""
|
||||
elem = FakeWebElement(QRect(0, 0, 10, 10), self.frame, display='none')
|
||||
self.assertFalse(webelem.is_visible(elem, self.frame))
|
||||
|
||||
|
||||
class JavascriptEscapeTests(TestCase):
|
||||
|
||||
"""Check javascript_escape."""
|
||||
|
||||
STRINGS = [
|
||||
('foo\\bar', r'foo\\bar'),
|
||||
('foo\nbar', r'foo\nbar'),
|
||||
('foo\tbar', r'foo\tbar'),
|
||||
("foo'bar", r"foo\'bar"),
|
||||
('foo"bar', r'foo\"bar'),
|
||||
]
|
||||
|
||||
def test_escape(self):
|
||||
self.assertEqual(
|
||||
webelem.javascript_escape('one\\two\nthree\tfour\'five"six'),
|
||||
r"""one\\two\nthree\tfour\'five\"six""")
|
||||
for before, after in self.STRINGS:
|
||||
self.assertEqual(webelem.javascript_escape(before), after)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user