[unified-heap] Fix ListenerLeakTest.* unit tests

The tests execute GC from JS which is not sufficient for unifiedh heap.
The fix adds proper GCs before asserting that the leak is not present.

Bug: 843903
Change-Id: Ieecf14acdbec96ebb6f612523ca0fa12aba92ad2
Reviewed-on: https://chromium-review.googlesource.com/c/1307395
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603892}
diff --git a/third_party/blink/renderer/core/dom/events/listener_leak_test.cc b/third_party/blink/renderer/core/dom/events/listener_leak_test.cc
index f858879..bf97de00 100644
--- a/third_party/blink/renderer/core/dom/events/listener_leak_test.cc
+++ b/third_party/blink/renderer/core/dom/events/listener_leak_test.cc
@@ -27,12 +27,13 @@
  * (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 "testing/gtest/include/gtest/gtest.h"
 #include "third_party/blink/public/platform/platform.h"
 #include "third_party/blink/public/platform/web_url_loader_mock_factory.h"
 #include "third_party/blink/public/web/web_view.h"
+#include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h"
 #include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
+#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
 #include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
 #include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
 #include "v8/include/v8-profiler.h"
@@ -40,6 +41,8 @@
 
 namespace blink {
 
+namespace {
+
 const v8::HeapGraphNode* GetProperty(v8::Isolate* isolate,
                                      const v8::HeapGraphNode* node,
                                      v8::HeapGraphEdge::Type type,
@@ -55,8 +58,7 @@
   return nullptr;
 }
 
-int GetNumObjects(const char* constructor) {
-  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+int GetNumObjects(v8::Isolate* isolate, const char* constructor) {
   v8::HandleScope scope(isolate);
   v8::HeapProfiler* profiler = isolate->GetHeapProfiler();
   const v8::HeapSnapshot* snapshot = profiler->TakeHeapSnapshot();
@@ -84,15 +86,23 @@
   return count;
 }
 
+}  // namespace
+
 class ListenerLeakTest : public testing::Test {
  public:
-  void RunTest(const std::string& filename) {
+  void RunTestAndGC(const std::string& filename) {
     std::string base_url("http://www.example.com/");
     std::string file_name(filename);
     url_test_helpers::RegisterMockedURLLoadFromBase(
         WebString::FromUTF8(base_url), blink::test::CoreTestDataPath(),
         WebString::FromUTF8(file_name));
     web_view_helper.InitializeAndLoad(base_url + file_name);
+    V8GCController::CollectAllGarbageForTesting(
+        isolate(), v8::EmbedderHeapTracer::EmbedderStackState::kEmpty);
+  }
+
+  v8::Isolate* isolate() const {
+    return ToIsolate(web_view_helper.LocalMainFrame()->GetFrame());
   }
 
   void TearDown() override {
@@ -108,15 +118,15 @@
 // 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"));
+  RunTestAndGC("listener/listener_leak1.html");
+  ASSERT_EQ(0, GetNumObjects(isolate(), "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"));
+  RunTestAndGC("listener/listener_leak2.html");
+  ASSERT_EQ(1, GetNumObjects(isolate(), "EventListenerLeakTestObject2"));
 }
 
 }  // namespace blink