[chromium] Add a test to WebFrameTest for selectRange and visiblePositionForWindowPoint.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 14 Aug 2012 17:55:03 +0000 (17:55 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 14 Aug 2012 17:55:03 +0000 (17:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=93108

Patch by Iain Merrick <husky@chromium.org> on 2012-08-14
Reviewed by Adam Barth.

Adds a new test WebFrameTest.SelectRange. This tests WebFrameImpl::selectRange,
and also serves to test WebFrameImpl::visiblePositionForWindowPoint as it is
used by selectRange.

The test uses sample files that contain selected text on load. The test uses
WebViewImpl::selectionBounds to get the coordinates, deselects all, then
calls WebFrameImpl::selectRange to attempt to reselect the same text.

Four cases are tested:
- the basic case
- the case where the selected text is offscreen due to a scroll
- the case where the selected text is in an iframe
- the case where the selected text is in an editable element

* tests/WebFrameTest.cpp:
* tests/data/select_range_basic.html: Added.
* tests/data/select_range_editable.html: Added.
* tests/data/select_range_iframe.html: Added.
* tests/data/select_range_scroll.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@125581 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/chromium/ChangeLog
Source/WebKit/chromium/tests/WebFrameTest.cpp
Source/WebKit/chromium/tests/data/select_range_basic.html [new file with mode: 0644]
Source/WebKit/chromium/tests/data/select_range_editable.html [new file with mode: 0644]
Source/WebKit/chromium/tests/data/select_range_iframe.html [new file with mode: 0644]
Source/WebKit/chromium/tests/data/select_range_scroll.html [new file with mode: 0644]

index 8cb9c81..28be4bb 100644 (file)
@@ -1,3 +1,30 @@
+2012-08-14  Iain Merrick  <husky@chromium.org>
+
+        [chromium] Add a test to WebFrameTest for selectRange and visiblePositionForWindowPoint.
+        https://bugs.webkit.org/show_bug.cgi?id=93108
+
+        Reviewed by Adam Barth.
+
+        Adds a new test WebFrameTest.SelectRange. This tests WebFrameImpl::selectRange,
+        and also serves to test WebFrameImpl::visiblePositionForWindowPoint as it is
+        used by selectRange.
+
+        The test uses sample files that contain selected text on load. The test uses
+        WebViewImpl::selectionBounds to get the coordinates, deselects all, then
+        calls WebFrameImpl::selectRange to attempt to reselect the same text.
+
+        Four cases are tested:
+        - the basic case
+        - the case where the selected text is offscreen due to a scroll
+        - the case where the selected text is in an iframe
+        - the case where the selected text is in an editable element
+
+        * tests/WebFrameTest.cpp:
+        * tests/data/select_range_basic.html: Added.
+        * tests/data/select_range_editable.html: Added.
+        * tests/data/select_range_iframe.html: Added.
+        * tests/data/select_range_scroll.html: Added.
+
 2012-08-14  Joshua Bell  <jsbell@chromium.org>
 
         IndexedDB: Pass cursor continue results back in callback
index 0b40c1d..8fc7498 100644 (file)
@@ -958,4 +958,94 @@ TEST_F(WebFrameTest, FindInPageMatchRects)
     webView->close();
 }
 
+static WebView* selectRangeTestCreateWebView(const std::string& url)
+{
+    WebView* webView = FrameTestHelpers::createWebViewAndLoad(url, true);
+    webView->settings()->setDefaultFontSize(12);
+    webView->resize(WebSize(640, 480));
+    return webView;
+}
+
+static WebPoint topLeft(const WebRect& rect)
+{
+    return WebPoint(rect.x, rect.y);
+}
+
+static WebPoint bottomRightMinusOne(const WebRect& rect)
+{
+    // FIXME: If we don't subtract 1 from the x- and y-coordinates of the
+    // selection bounds, selectRange() will select the *next* element. That's
+    // strictly correct, as hit-testing checks the pixel to the lower-right of
+    // the input coordinate, but it's a wart on the API.
+    return WebPoint(rect.x + rect.width - 1, rect.y + rect.height - 1);
+}
+
+static std::string selectionAsString(WebFrame* frame)
+{
+    return std::string(frame->selectionAsText().utf8().data());
+}
+
+TEST_F(WebFrameTest, SelectRange)
+{
+    WebView* webView;
+    WebFrame* frame;
+    WebRect startWebRect;
+    WebRect endWebRect;
+
+    registerMockedHttpURLLoad("select_range_basic.html");
+    registerMockedHttpURLLoad("select_range_scroll.html");
+    registerMockedHttpURLLoad("select_range_iframe.html");
+    registerMockedHttpURLLoad("select_range_editable.html");
+
+    webView = selectRangeTestCreateWebView(m_baseURL + "select_range_basic.html");
+    frame = webView->mainFrame();
+    EXPECT_EQ("Some test text for testing.", selectionAsString(frame));
+    webView->selectionBounds(startWebRect, endWebRect);
+    frame->executeCommand(WebString::fromUTF8("Unselect"));
+    EXPECT_EQ("", selectionAsString(frame));
+    frame->selectRange(topLeft(startWebRect), bottomRightMinusOne(endWebRect));
+    EXPECT_EQ("Some test text for testing.", selectionAsString(frame));
+    webView->close();
+
+    webView = selectRangeTestCreateWebView(m_baseURL + "select_range_scroll.html");
+    frame = webView->mainFrame();
+    EXPECT_EQ("Some offscreen test text for testing.", selectionAsString(frame));
+    webView->selectionBounds(startWebRect, endWebRect);
+    frame->executeCommand(WebString::fromUTF8("Unselect"));
+    EXPECT_EQ("", selectionAsString(frame));
+    frame->selectRange(topLeft(startWebRect), bottomRightMinusOne(endWebRect));
+    EXPECT_EQ("Some offscreen test text for testing.", selectionAsString(frame));
+    webView->close();
+
+    webView = selectRangeTestCreateWebView(m_baseURL + "select_range_iframe.html");
+    frame = webView->mainFrame();
+    WebFrame* subframe = frame->findChildByExpression(WebString::fromUTF8("/html/body/iframe"));
+    EXPECT_EQ("Some test text for testing.", selectionAsString(subframe));
+    webView->selectionBounds(startWebRect, endWebRect);
+    subframe->executeCommand(WebString::fromUTF8("Unselect"));
+    EXPECT_EQ("", selectionAsString(subframe));
+    subframe->selectRange(topLeft(startWebRect), bottomRightMinusOne(endWebRect));
+    EXPECT_EQ("Some test text for testing.", selectionAsString(subframe));
+    webView->close();
+
+    // Select the middle of an editable element, then try to extend the selection to the top of the document.
+    // The selection range should be clipped to the bounds of the editable element.
+    webView = selectRangeTestCreateWebView(m_baseURL + "select_range_editable.html");
+    frame = webView->mainFrame();
+    EXPECT_EQ("This text is initially selected.", selectionAsString(frame));
+    webView->selectionBounds(startWebRect, endWebRect);
+    frame->selectRange(WebPoint(0, 0), bottomRightMinusOne(endWebRect));
+    EXPECT_EQ("16-char header. This text is initially selected.", selectionAsString(frame));
+    webView->close();
+
+    // As above, but extending the selection to the bottom of the document.
+    webView = selectRangeTestCreateWebView(m_baseURL + "select_range_editable.html");
+    frame = webView->mainFrame();
+    EXPECT_EQ("This text is initially selected.", selectionAsString(frame));
+    webView->selectionBounds(startWebRect, endWebRect);
+    frame->selectRange(topLeft(startWebRect), WebPoint(640, 480));
+    EXPECT_EQ("This text is initially selected. 16-char footer.", selectionAsString(frame));
+    webView->close();
+}
+
 } // namespace
diff --git a/Source/WebKit/chromium/tests/data/select_range_basic.html b/Source/WebKit/chromium/tests/data/select_range_basic.html
new file mode 100644 (file)
index 0000000..8ffc3fc
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+<body>
+Some extra text.
+<span id='target'>Some test text for testing.</span>
+Some extra text.
+<script>
+    function select() {
+        var range = document.createRange();
+        range.selectNode(document.getElementById('target'));
+        window.getSelection().addRange(range);
+    }
+    window.onload = select;
+</script>
+</body>
+</html>
+
diff --git a/Source/WebKit/chromium/tests/data/select_range_editable.html b/Source/WebKit/chromium/tests/data/select_range_editable.html
new file mode 100644 (file)
index 0000000..a186979
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<body>
+This text won't be selected because it isn't editable.
+<span id='target' contenteditable="true">16-char header. This text is initially selected. 16-char footer.</span>
+This text won't be selected because it isn't editable.
+<script>
+    function select() {
+        var text = document.getElementById('target').firstChild;
+        var range = document.createRange();
+        range.setStart(text, 16);
+        range.setEnd(text, 48);
+        window.getSelection().addRange(range);
+    }
+    window.onload = select;
+</script>
+</body>
+</html>
+
diff --git a/Source/WebKit/chromium/tests/data/select_range_iframe.html b/Source/WebKit/chromium/tests/data/select_range_iframe.html
new file mode 100644 (file)
index 0000000..f78089a
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<body>
+This is a test case for selecting a range within an iframe.
+<iframe width=400 height=200 id="iframe" src="select_range_basic.html"></iframe>
+<script>
+    document.getElementById('iframe').contentWindow.focus();
+</script>
+</body>
+</html>
+
diff --git a/Source/WebKit/chromium/tests/data/select_range_scroll.html b/Source/WebKit/chromium/tests/data/select_range_scroll.html
new file mode 100644 (file)
index 0000000..a67f189
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+<body>
+<div style='height:3000px'>
+    <span id="target">Some offscreen test text for testing.</span>
+</div>
+<script>
+    var range = document.createRange();
+    range.selectNode(document.getElementById('target'));
+    window.getSelection().addRange(range);
+    window.scrollBy(0,500);
+</script>
+</body>
+</html>
+