100% coverage for browser.webelem.

This commit is contained in:
Florian Bruhin 2015-08-10 19:37:16 +02:00
parent 9cabd4828c
commit c8679d6544
4 changed files with 87 additions and 4 deletions

View File

@ -339,8 +339,6 @@ def get_child_frames(startframe):
def focus_elem(frame):
"""Get the focused element in a web frame.
FIXME: Add tests.
Args:
frame: The QWebFrame to search in.
"""

View File

@ -37,6 +37,7 @@ PERFECT_FILES = [
'qutebrowser/browser/tabhistory.py',
'qutebrowser/browser/http.py',
'qutebrowser/browser/rfc6266.py',
'qutebrowser/browser/webelem.py',
'qutebrowser/misc/readline.py',
'qutebrowser/misc/split.py',

View File

@ -30,8 +30,9 @@ import os.path
import hypothesis
import hypothesis.strategies
from PyQt5.QtCore import PYQT_VERSION, QRect, QPoint
from PyQt5.QtCore import PYQT_VERSION, QRect, QPoint, Qt, QTimer, QEventLoop
from PyQt5.QtWebKit import QWebElement
from PyQt5.QtWebKitWidgets import QWebView
import pytest
from qutebrowser.browser import webelem
@ -334,6 +335,14 @@ class TestIsVisible:
def frame(self, stubs):
return stubs.FakeWebFrame(QRect(0, 0, 100, 100))
def test_invalid_frame_geometry(self, stubs):
"""Test with an invalid frame geometry."""
rect = QRect(0, 0, 0, 0)
assert not rect.isValid()
frame = stubs.FakeWebFrame(rect)
elem = get_webelem(QRect(0, 0, 10, 10), frame)
assert not elem.is_visible(frame)
def test_invalid_invisible(self, frame):
"""Test elements with an invalid geometry which are invisible."""
elem = get_webelem(QRect(0, 0, 0, 0), frame)
@ -460,6 +469,67 @@ class TestIsVisibleIframe:
assert not objects.elems[2].is_visible(objects.frame)
assert objects.elems[3].is_visible(objects.frame)
@pytest.fixture
def invalid_objects(self, stubs):
"""Set up the following base situation:
0, 0 300, 0
##############################
# #
0,10 # iframe 100,10 #
#********** #
#* e * elems[0]: 10, 10 in iframe (visible)
#* * #
#* * #
#********** #
0,110 #. .100,110 #
#. . #
#. e . elems[2]: 20,150 in iframe (not visible)
#.......... #
##############################
300, 0 300, 300
Returns an Objects namedtuple with frame/iframe/elems attributes.
"""
frame = stubs.FakeWebFrame(QRect(0, 0, 300, 300))
iframe = stubs.FakeWebFrame(QRect(0, 10, 100, 100), parent=frame)
assert frame.geometry().contains(iframe.geometry())
elems = [
get_webelem(QRect(10, 10, 0, 0), iframe),
get_webelem(QRect(20, 150, 0, 0), iframe),
]
for e in elems:
assert not e.geometry().isValid()
return self.Objects(frame=frame, iframe=iframe, elems=elems)
def test_invalid_visible(self, invalid_objects):
"""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 = invalid_objects.elems[0]
assert elem.is_visible(invalid_objects.frame)
def test_invalid_invisible(self, invalid_objects):
"""Test elements with an invalid geometry which are invisible."""
assert not invalid_objects.elems[1].is_visible(invalid_objects.frame)
def test_focus_element(stubs):
"""Test getting focus element with a fake frame/element.
Testing this with a real webpage is almost impossible because the window
and the element would have focus, which is hard to achieve consistently in
a test.
"""
frame = stubs.FakeWebFrame(QRect(0, 0, 100, 100))
elem = get_webelem()
frame.focus_elem = elem._elem
assert webelem.focus_elem(frame)._elem is elem._elem
class TestRectOnView:

View File

@ -42,7 +42,11 @@ class FakeKeyEvent:
class FakeWebFrame:
"""A stub for QWebFrame."""
"""A stub for QWebFrame.
Attributes:
focus_elem: The 'focused' element.
"""
def __init__(self, geometry, scroll=None, parent=None):
"""Constructor.
@ -57,6 +61,16 @@ class FakeWebFrame:
self.geometry = mock.Mock(return_value=geometry)
self.scrollPosition = mock.Mock(return_value=scroll)
self.parentFrame = mock.Mock(return_value=parent)
self.focus_elem = None
def findFirstElement(self, selector):
if selector == '*:focus':
if self.focus_elem is not None:
return self.focus_elem
else:
raise Exception("Trying to get focus element but it's unset!")
else:
raise Exception("Unknown selector {!r}!".format(selector))
class FakeChildrenFrame: