Title: [116460] trunk/Source/WebKit/chromium
Revision
116460
Author
[email protected]
Date
2012-05-08 15:45:10 -0700 (Tue, 08 May 2012)

Log Message

move listener_leak_test to webkit_unit_tests
https://bugs.webkit.org/show_bug.cgi?id=85922

Reviewed by Ojan Vafai.

Porting the test from src/webkit/tools/test_shell/listener_leak_test.cc.
All I did was change the style and use the FrameTestHelper to load the file.

* WebKit.gyp: Exclude file in components build.
* WebKit.gypi: Add new file.
* tests/ListenerLeakTest.cpp: Added.
(WebKit):
(WebKit::GetProperty):
(WebKit::GetNumObjects):
(ListenerLeakTest):
(WebKit::ListenerLeakTest::ListenerLeakTest):
(WebKit::ListenerLeakTest::RunTest):
(WebKit::TEST_F):
* tests/data/listener/listener_leak1.html: Added.
* tests/data/listener/listener_leak2.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/chromium/ChangeLog (116459 => 116460)


--- trunk/Source/WebKit/chromium/ChangeLog	2012-05-08 22:31:23 UTC (rev 116459)
+++ trunk/Source/WebKit/chromium/ChangeLog	2012-05-08 22:45:10 UTC (rev 116460)
@@ -1,3 +1,26 @@
+2012-05-08  Tony Chang  <[email protected]>
+
+        move listener_leak_test to webkit_unit_tests
+        https://bugs.webkit.org/show_bug.cgi?id=85922
+
+        Reviewed by Ojan Vafai.
+
+        Porting the test from src/webkit/tools/test_shell/listener_leak_test.cc.
+        All I did was change the style and use the FrameTestHelper to load the file.
+
+        * WebKit.gyp: Exclude file in components build.
+        * WebKit.gypi: Add new file.
+        * tests/ListenerLeakTest.cpp: Added.
+        (WebKit):
+        (WebKit::GetProperty):
+        (WebKit::GetNumObjects):
+        (ListenerLeakTest):
+        (WebKit::ListenerLeakTest::ListenerLeakTest):
+        (WebKit::ListenerLeakTest::RunTest):
+        (WebKit::TEST_F):
+        * tests/data/listener/listener_leak1.html: Added.
+        * tests/data/listener/listener_leak2.html: Added.
+
 2012-05-08  W. James MacLean  <[email protected]>
 
         [chromium] Create LinkHighlightLayerChromium class to provide link-highlight preview animations for GraphicsLayerChromium.

Modified: trunk/Source/WebKit/chromium/WebKit.gyp (116459 => 116460)


--- trunk/Source/WebKit/chromium/WebKit.gyp	2012-05-08 22:31:23 UTC (rev 116459)
+++ trunk/Source/WebKit/chromium/WebKit.gyp	2012-05-08 22:45:10 UTC (rev 116460)
@@ -738,6 +738,7 @@
                                 'tests/EventListenerTest.cpp',
                                 'tests/FrameTestHelpers.cpp',
                                 'tests/LevelDBTest.cpp',
+                                'tests/ListenerLeakTest.cpp',
                                 'tests/PopupMenuTest.cpp',
                                 'tests/RenderTableCellTest.cpp',
                                 'tests/RenderTableRowTest.cpp',

Modified: trunk/Source/WebKit/chromium/WebKit.gypi (116459 => 116460)


--- trunk/Source/WebKit/chromium/WebKit.gypi	2012-05-08 22:31:23 UTC (rev 116459)
+++ trunk/Source/WebKit/chromium/WebKit.gypi	2012-05-08 22:45:10 UTC (rev 116460)
@@ -115,6 +115,7 @@
             'tests/LayerTextureUpdaterTest.cpp',
             'tests/LevelDBTest.cpp',
             'tests/LinkHighlightTest.cpp',
+            'tests/ListenerLeakTest.cpp',
             'tests/LocalizedNumberICUTest.cpp',
             'tests/MockCCQuadCuller.h',
             'tests/PaintAggregatorTest.cpp',

Added: trunk/Source/WebKit/chromium/tests/ListenerLeakTest.cpp (0 => 116460)


--- trunk/Source/WebKit/chromium/tests/ListenerLeakTest.cpp	                        (rev 0)
+++ trunk/Source/WebKit/chromium/tests/ListenerLeakTest.cpp	2012-05-08 22:45:10 UTC (rev 116460)
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2012 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "FrameTestHelpers.h"
+#include "WebView.h"
+#include <gtest/gtest.h>
+#include <v8/include/v8-profiler.h>
+#include <v8/include/v8.h>
+#include <webkit/support/webkit_support.h>
+
+using namespace WebKit;
+
+namespace {
+
+const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node, v8::HeapGraphEdge::Type type, const char* name)
+{
+    for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
+        const v8::HeapGraphEdge* prop = node->GetChild(i);
+        if (prop->GetType() == type) {
+            v8::String::AsciiValue propName(prop->GetName());
+            if (!strcmp(name, *propName))
+                return prop->GetToNode();
+        }
+    }
+    return 0;
+}
+
+int GetNumObjects(const char* constructor)
+{
+    v8::HandleScope scope;
+    const v8::HeapSnapshot* snapshot = v8::HeapProfiler::TakeSnapshot(v8::String::New(""), v8::HeapSnapshot::kFull);
+    if (!snapshot)
+        return -1;
+    int count = 0;
+    for (int i = 0; i < snapshot->GetNodesCount(); ++i) {
+        const v8::HeapGraphNode* node = snapshot->GetNode(i);
+        if (node->GetType() != v8::HeapGraphNode::kObject)
+            continue;
+        v8::String::AsciiValue nodeName(node->GetName());
+        if (!strcmp(constructor, *nodeName)) {
+            const v8::HeapGraphNode* constructorProp = GetProperty(node, v8::HeapGraphEdge::kProperty, "constructor");
+            // Skip an Object instance named after the constructor.
+            if (constructorProp) {
+                v8::String::AsciiValue constructorName(constructorProp->GetName());
+                if (!strcmp(constructor, *constructorName))
+                    continue;
+            }
+            ++count;
+        }
+    }
+    return count;
+}
+
+
+class ListenerLeakTest : public testing::Test {
+public:
+    ListenerLeakTest() : m_webView(0) { }
+
+    void RunTest(const std::string& filename)
+    {
+        std::string baseURL("http://www.example.com/");
+        std::string fileName(filename);
+        bool executeScript = true;
+        FrameTestHelpers::registerMockedURLLoad(baseURL, fileName);
+        m_webView = FrameTestHelpers::createWebViewAndLoad(baseURL + fileName, executeScript);
+    }
+
+    virtual void TearDown() OVERRIDE
+    {
+        if (m_webView)
+            m_webView->close();
+        webkit_support::UnregisterAllMockedURLs();
+    }
+
+protected:
+    WebView* m_webView;
+};
+
+
+// This test tries to create a reference cycle between node and its listener.
+// See http://crbug/17400.
+TEST_F(ListenerLeakTest, ReferenceCycle)
+{
+    RunTest("listener/listener_leak1.html");
+    ASSERT_EQ(0, GetNumObjects("EventListenerLeakTestObject1"));
+}
+
+// This test sets node onclick many times to expose a possible memory
+// leak where all listeners get referenced by the node.
+TEST_F(ListenerLeakTest, HiddenReferences)
+{
+    RunTest("listener/listener_leak2.html");
+    ASSERT_EQ(1, GetNumObjects("EventListenerLeakTestObject2"));
+}
+
+} // namespace

Added: trunk/Source/WebKit/chromium/tests/data/listener/listener_leak1.html (0 => 116460)


--- trunk/Source/WebKit/chromium/tests/data/listener/listener_leak1.html	                        (rev 0)
+++ trunk/Source/WebKit/chromium/tests/data/listener/listener_leak1.html	2012-05-08 22:45:10 UTC (rev 116460)
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html>
+<head><title>Event Listener Leak Test 1</title></head>
+<body _onload_="leak()">
+<script>
+if (typeof(gc) == "undefined") gc = function() {};
+
+function EventListenerLeakTestObject1() {}
+
+function createListener(node) {
+  var foo = new EventListenerLeakTestObject1();
+  return function(evt) {
+    // This closure references |node| and an instance of leak object.
+    node.foo = foo;
+  };
+}
+
+function doLeak() {
+  for (var i = 0; i < 10000; i++) {
+    var node = document.createElement('span');
+    node._onclick_ = createListener(node);
+  }
+}
+
+function leak() {
+  doLeak();
+  gc();
+  gc();
+}
+</script>
+
+<p>This page leaks memory.</p>
+
+<!-- Allow leaking manually. -->
+<input type="button" value="Leak More" _onclick_="leak()">
+
+</body>
+</html>

Added: trunk/Source/WebKit/chromium/tests/data/listener/listener_leak2.html (0 => 116460)


--- trunk/Source/WebKit/chromium/tests/data/listener/listener_leak2.html	                        (rev 0)
+++ trunk/Source/WebKit/chromium/tests/data/listener/listener_leak2.html	2012-05-08 22:45:10 UTC (rev 116460)
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html>
+<head><title>Event Listener Leak Test 2</title></head>
+<body _onload_="leak()">
+<script>
+if (typeof(gc) == "undefined") gc = function() {};
+
+var node = null;
+
+function EventListenerLeakTestObject2() {}
+
+function createListener(node) {
+  var foo = new EventListenerLeakTestObject2();
+  return function(evt) {
+    // This closure references |node| and an instance of leak object.
+    node.foo = foo;
+  };
+}
+
+function doLeak() {
+  if (!node) node = document.createElement('span');
+  for (var i = 0; i < 10000; i++) {
+    node._onclick_ = createListener(node);
+  }
+}
+
+function leak() {
+  doLeak();
+  gc();
+  gc();
+}
+</script>
+
+<p>This page leaks memory.</p>
+
+<!-- Allow leaking manually. -->
+<input type="button" value="Leak More" _onclick_="leak()">
+
+</body>
+</html>
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to