[Chromium] Implement a Layout Test for editing/SurroundingText
authorleandrogracia@chromium.org <leandrogracia@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Jul 2012 00:53:26 +0000 (00:53 +0000)
committerleandrogracia@chromium.org <leandrogracia@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Jul 2012 00:53:26 +0000 (00:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=82461

Reviewed by Ryosuke Niwa.

Source/WebKit/chromium:

Allow passing nodes as arguments for layout test methods.

* public/WebBindings.h:
(WebBindings):
* src/WebBindings.cpp:
(WebKit::getNodeImpl):
(WebKit):
(WebKit::WebBindings::getNode):

Tools:

Add a new method to the layout test controller in order to retrieve the
text surrounding a provided element.

* DumpRenderTree/chromium/LayoutTestController.cpp:
(LayoutTestController::LayoutTestController):
(LayoutTestController::textSurroundingElement):
* DumpRenderTree/chromium/LayoutTestController.h:
(LayoutTestController):

LayoutTests:

Add a new layout test for the surrounding text feature.

* platform/chromium/editing/surrounding-text/surrounding-text-expected.txt: Added.
* platform/chromium/editing/surrounding-text/surrounding-text.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text-expected.txt [new file with mode: 0644]
LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text.html [new file with mode: 0644]
Source/WebKit/chromium/ChangeLog
Source/WebKit/chromium/public/WebBindings.h
Source/WebKit/chromium/src/WebBindings.cpp
Tools/ChangeLog
Tools/DumpRenderTree/chromium/LayoutTestController.cpp
Tools/DumpRenderTree/chromium/LayoutTestController.h

index 98378ee..f6df272 100644 (file)
@@ -1,3 +1,15 @@
+2012-07-02  Leandro Gracia Gil  <leandrogracia@chromium.org>
+
+        [Chromium] Implement a Layout Test for editing/SurroundingText
+        https://bugs.webkit.org/show_bug.cgi?id=82461
+
+        Reviewed by Ryosuke Niwa.
+
+        Add a new layout test for the surrounding text feature.
+
+        * platform/chromium/editing/surrounding-text/surrounding-text-expected.txt: Added.
+        * platform/chromium/editing/surrounding-text/surrounding-text.html: Added.
+
 2012-07-02  Emil A Eklund  <eae@chromium.org>
 
         Unreviewed chromium rebaseline.
diff --git a/LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text-expected.txt b/LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text-expected.txt
new file mode 100644 (file)
index 0000000..912c1c5
--- /dev/null
@@ -0,0 +1,21 @@
+Test the extraction of the text surrounding an element.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 0, 100) is "12345 6789 12345 6789"
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 5, 6) is "89 123"
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 5, 0) is ""
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 5, 1) is "1"
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 6, 2) is "12"
+PASS surroundingText('<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>', 100, 5) is ""
+PASS surroundingText('<select>.</select><div>57th Street and Lake Shore Drive</div> <span>Chicago</span> <span id="here">IL</span> <span>60637</span><select>.</select>', 0, 100) is "57th Street and Lake Shore Drive Chicago IL 60637"
+PASS surroundingText('<fieldset>.</fieldset>12345<button>abc</button><p>6789<br id="here"/>12345</p>6789<textarea>abc</textarea>0123<fieldset>.</fieldset>', 0, 100) is "6789 12345 6789"
+PASS surroundingText('<button>.</button><div id="here">This is <!-- comment --!>a test <' + 'script language="javascript"><' + '/script>example<button>.</button>', 0, 100) is "This is a test example"
+PASS surroundingText('<button>.</button><div id="here">012345678901234567890123456789</div><button>.</button>', 15, 12) is "901234567890"
+PASS surroundingText('<option>.</option>12345<button id="here">test</button><option>.</option>', 0, 100) is ""
+PASS surroundingText('<option>.</option>12345<button>te<span id="here">st</span></button><option>.</option>', 0, 100) is ""
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text.html b/LayoutTests/platform/chromium/editing/surrounding-text/surrounding-text.html
new file mode 100644 (file)
index 0000000..6b7f478
--- /dev/null
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src="../../../../fast/js/resources/js-test-pre.js"></script>
+<div id="test">
+</div>
+<p id="description"></p>
+<div id="console"></div>
+<script>
+description('Test the extraction of the text surrounding an element.');
+
+function surroundingText(html, offset, maxLength) {
+    document.getElementById('test').innerHTML = html;
+
+    var here = document.getElementById('here');
+    if (here == null)
+        throw 'Test case needs an element with id "here"';
+
+    var node = here.hasChildNodes() ? here.firstChild : here.nextSibling;
+    if (node == null)
+        throw 'No node after "here" element';
+
+    var text = window.testRunner.textSurroundingElement(node, offset, maxLength);
+    return text.replace(/\s+/g, ' ').replace(/^\s*|\s*$/g, '');
+}
+
+function run() {
+    if (!window.testRunner)
+        return;
+
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 0, 100)', '12345 6789 12345 6789');
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 6)', '89 123');
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 0)', '');
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 1)', '1');
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 6, 2)', '12');
+    shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 100, 5)', '');
+    shouldBeEqualToString('surroundingText(\'<select>.</select><div>57th Street and Lake Shore Drive</div> <span>Chicago</span> <span id="here">IL</span> <span>60637</span><select>.</select>\', 0, 100)', '57th Street and Lake Shore Drive Chicago IL 60637');
+    shouldBeEqualToString('surroundingText(\'<fieldset>.</fieldset>12345<button>abc</button><p>6789<br id="here"/>12345</p>6789<textarea>abc</textarea>0123<fieldset>.</fieldset>\', 0, 100)', '6789 12345 6789');
+    shouldBeEqualToString('surroundingText(\'<button>.</button><div id="here">This is <!-- comment --!>a test <\' + \'script language="javascript"><\' + \'/script>example<button>.</button>\', 0, 100)', 'This is a test example');
+    shouldBeEqualToString('surroundingText(\'<button>.</button><div id="here">012345678901234567890123456789</div><button>.</button>\', 15, 12)', '901234567890');
+    shouldBeEqualToString('surroundingText(\'<option>.</option>12345<button id="here">test</button><option>.</option>\', 0, 100)', '');
+    shouldBeEqualToString('surroundingText(\'<option>.</option>12345<button>te<span id="here">st</span></button><option>.</option>\', 0, 100)', '');
+
+    document.body.removeChild(document.getElementById('test'));
+    finishJSTest();
+}
+
+window.onload = run;
+window.jsTestIsAsync = true;
+window.successfullyParsed = true;
+</script>
+<script src="../../../../fast/js/resources/js-test-post.js"></script>
+</body>
+</html>
index 702c4b4..53cdcbd 100644 (file)
@@ -1,3 +1,19 @@
+2012-07-02  Leandro Gracia Gil  <leandrogracia@chromium.org>
+
+        [Chromium] Implement a Layout Test for editing/SurroundingText
+        https://bugs.webkit.org/show_bug.cgi?id=82461
+
+        Reviewed by Ryosuke Niwa.
+
+        Allow passing nodes as arguments for layout test methods.
+
+        * public/WebBindings.h:
+        (WebBindings):
+        * src/WebBindings.cpp:
+        (WebKit::getNodeImpl):
+        (WebKit):
+        (WebKit::WebBindings::getNode):
+
 2012-07-02  Benjamin Poulain  <bpoulain@apple.com>
 
         Do not do any logging initialization when logging is disabled
index 34a3205..dc7b45f 100644 (file)
@@ -152,6 +152,10 @@ public:
     // If so, return it as a WebArrayBufferView object.
     WEBKIT_EXPORT static bool getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferView*);
 
+    // Return true (success) if the given npobj is a node.
+    // If so, return that node as a WebNode object.
+    WEBKIT_EXPORT static bool getNode(NPObject* element, WebNode*);
+
     // Return true (success) if the given npobj is an element.
     // If so, return that element as a WebElement object.
     WEBKIT_EXPORT static bool getElement(NPObject* element, WebElement*);
index 232b5b4..b9bbd5d 100644 (file)
@@ -223,6 +223,21 @@ static bool getRangeImpl(NPObject* object, WebRange* webRange)
     return true;
 }
 
+static bool getNodeImpl(NPObject* object, WebNode* webNode)
+{
+    if (!object || (object->_class != npScriptObjectClass))
+        return false;
+
+    V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
+    v8::Handle<v8::Object> v8Object(v8NPObject->v8Object);
+    Node* native = V8Node::HasInstance(v8Object) ? V8Node::toNative(v8Object) : 0;
+    if (!native)
+        return false;
+
+    *webNode = WebNode(native);
+    return true;
+}
+
 static bool getElementImpl(NPObject* object, WebElement* webElement)
 {
     if (!object || (object->_class != npScriptObjectClass))
@@ -328,6 +343,16 @@ bool WebBindings::getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferVi
 #endif
 }
 
+bool WebBindings::getNode(NPObject* node, WebNode* webNode)
+{
+#if USE(V8)
+    return getNodeImpl(node, webNode);
+#else
+    // Not supported on other ports (JSC, etc.).
+    return false;
+#endif
+}
+
 bool WebBindings::getElement(NPObject* element, WebElement* webElement)
 {
 #if USE(V8)
index e772472..0773e3b 100644 (file)
@@ -1,3 +1,19 @@
+2012-07-02  Leandro Gracia Gil  <leandrogracia@chromium.org>
+
+        [Chromium] Implement a Layout Test for editing/SurroundingText
+        https://bugs.webkit.org/show_bug.cgi?id=82461
+
+        Reviewed by Ryosuke Niwa.
+
+        Add a new method to the layout test controller in order to retrieve the
+        text surrounding a provided element.
+
+        * DumpRenderTree/chromium/LayoutTestController.cpp:
+        (LayoutTestController::LayoutTestController):
+        (LayoutTestController::textSurroundingElement):
+        * DumpRenderTree/chromium/LayoutTestController.h:
+        (LayoutTestController):
+
 2012-07-02  Ojan Vafai  <ojan@chromium.org>
 
         Delete unused rebaseline method in gardeningserver.py
index efbf3ff..d5c08ae 100644 (file)
@@ -38,9 +38,7 @@
 #include "TestShell.h"
 #include "WebAnimationController.h"
 #include "WebBindings.h"
-#include "WebWorkerInfo.h"
 #include "WebConsoleMessage.h"
-#include "platform/WebData.h"
 #include "WebDeviceOrientation.h"
 #include "WebDeviceOrientationClientMock.h"
 #include "WebDocument.h"
 #include "WebPrintParams.h"
 #include "WebScriptSource.h"
 #include "WebSecurityPolicy.h"
-#include "platform/WebSerializedScriptValue.h"
 #include "WebSettings.h"
-#include "platform/WebSize.h"
-#include "platform/WebURL.h"
+#include "WebSurroundingText.h"
 #include "WebView.h"
 #include "WebViewHost.h"
+#include "WebWorkerInfo.h"
+#include "platform/WebData.h"
+#include "platform/WebSerializedScriptValue.h"
+#include "platform/WebSize.h"
+#include "platform/WebURL.h"
 #include "v8/include/v8.h"
 #include "webkit/support/webkit_support.h"
 #include <algorithm>
@@ -72,8 +73,8 @@
 #include <cstdlib>
 #include <limits>
 #include <sstream>
-#include <wtf/text/WTFString.h>
 #include <wtf/OwnArrayPtr.h>
+#include <wtf/text/WTFString.h>
 
 #if OS(LINUX) || OS(ANDROID)
 #include "linux/WebFontRendering.h"
@@ -275,7 +276,8 @@ LayoutTestController::LayoutTestController(TestShell* shell)
     bindMethod("setFixedLayoutSize", &LayoutTestController::setFixedLayoutSize);
     bindMethod("selectionAsMarkup", &LayoutTestController::selectionAsMarkup);
     bindMethod("setHasCustomFullScreenBehavior", &LayoutTestController::setHasCustomFullScreenBehavior);
-    
+    bindMethod("textSurroundingElement", &LayoutTestController::textSurroundingElement);
+
     // The fallback method is called when an unknown method is invoked.
     bindFallbackMethod(&LayoutTestController::fallbackMethod);
 
@@ -2343,3 +2345,28 @@ void LayoutTestController::setPointerLockWillFailSynchronously(const CppArgument
     result->setNull();
 }
 #endif
+
+void LayoutTestController::textSurroundingElement(const CppArgumentList& arguments, CppVariant* result)
+{
+    result->setNull();
+    if (arguments.size() < 3 || !arguments[0].isObject() || !arguments[1].isNumber() || !arguments[2].isNumber())
+        return;
+
+    WebNode node;
+    if (!WebBindings::getNode(arguments[0].value.objectValue, &node))
+        return;
+
+    if (node.isNull() || !node.isTextNode())
+        return;
+
+    unsigned offset = arguments[1].toInt32();
+    if (offset >= node.nodeValue().length()) {
+        result->set(WebString().utf8());
+        return;
+    }
+
+    WebSurroundingText surroundingText;
+    unsigned maxLength = arguments[2].toInt32();
+    surroundingText.initialize(node, offset, maxLength);
+    result->set(surroundingText.textContent().utf8());
+}
index e723860..f5ba066 100644 (file)
@@ -456,6 +456,11 @@ public:
     // Used to set the device scale factor.
     void setBackingScaleFactor(const CppArgumentList&, CppVariant*);
 
+    // Retrieves the text surrounding a position in a text node.
+    // The first child of the element given y its id will be used as reference,
+    // or its next sibling if not present. It must be a text node.
+    void textSurroundingElement(const CppArgumentList&, CppVariant*);
+
 public:
     // The following methods are not exposed to JavaScript.
     void setWorkQueueFrozen(bool frozen) { m_workQueue.setFrozen(frozen); }