2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "core/testing/URLTestHelpers.h"
34 #include "public/platform/Platform.h"
35 #include "public/platform/WebUnitTestSupport.h"
36 #include "public/web/WebView.h"
37 #include "web/tests/FrameTestHelpers.h"
38 #include <gtest/gtest.h>
39 #include <v8/include/v8-profiler.h>
40 #include <v8/include/v8.h>
42 using namespace blink;
46 const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node, v8::HeapGraphEdge::Type type, const char* name)
48 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
49 const v8::HeapGraphEdge* prop = node->GetChild(i);
50 if (prop->GetType() == type) {
51 v8::String::Utf8Value propName(prop->GetName());
52 if (!strcmp(name, *propName))
53 return prop->GetToNode();
59 int GetNumObjects(const char* constructor)
61 v8::Isolate* isolate = v8::Isolate::GetCurrent();
62 v8::HandleScope scope(isolate);
63 v8::HeapProfiler* profiler = isolate->GetHeapProfiler();
64 const v8::HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(v8::String::NewFromUtf8(isolate, ""));
68 for (int i = 0; i < snapshot->GetNodesCount(); ++i) {
69 const v8::HeapGraphNode* node = snapshot->GetNode(i);
70 if (node->GetType() != v8::HeapGraphNode::kObject)
72 v8::String::Utf8Value nodeName(node->GetName());
73 if (!strcmp(constructor, *nodeName)) {
74 const v8::HeapGraphNode* constructorProp = GetProperty(node, v8::HeapGraphEdge::kProperty, "constructor");
75 // Skip an Object instance named after the constructor.
76 if (constructorProp) {
77 v8::String::Utf8Value constructorName(constructorProp->GetName());
78 if (!strcmp(constructor, *constructorName))
88 class ListenerLeakTest : public testing::Test {
90 void RunTest(const std::string& filename)
92 std::string baseURL("http://www.example.com/");
93 std::string fileName(filename);
94 bool executeScript = true;
95 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
96 webViewHelper.initializeAndLoad(baseURL + fileName, executeScript);
99 virtual void TearDown() override
101 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
105 FrameTestHelpers::WebViewHelper webViewHelper;
109 // This test tries to create a reference cycle between node and its listener.
110 // See http://crbug/17400.
111 TEST_F(ListenerLeakTest, ReferenceCycle)
113 RunTest("listener/listener_leak1.html");
114 ASSERT_EQ(0, GetNumObjects("EventListenerLeakTestObject1"));
117 // This test sets node onclick many times to expose a possible memory
118 // leak where all listeners get referenced by the node.
119 TEST_F(ListenerLeakTest, HiddenReferences)
121 RunTest("listener/listener_leak2.html");
122 ASSERT_EQ(1, GetNumObjects("EventListenerLeakTestObject2"));