ScriptContextSetTest should close the main frame before finishing the test

ScriptContextSetTest should close the main frame and the webview, and then
collect garbage in Oilpan's heap before finishing the test.
Otherwise, the resources retained by the main frame are reported as leaking
on LSan builds.

BUG=497658

Review URL: https://codereview.chromium.org/1210513002

Cr-Commit-Position: refs/heads/master@{#336321}
diff --git a/extensions/extensions_tests.gypi b/extensions/extensions_tests.gypi
index afec595..3f52b715 100644
--- a/extensions/extensions_tests.gypi
+++ b/extensions/extensions_tests.gypi
@@ -152,6 +152,8 @@
       'renderer/mojo/keep_alive_client_unittest.cc',
       'renderer/mojo/stash_client_unittest.cc',
       'renderer/safe_builtins_unittest.cc',
+      'renderer/scoped_web_frame.cc',
+      'renderer/scoped_web_frame.h',
       'renderer/script_context_set_unittest.cc',
       'renderer/script_context_unittest.cc',
       'renderer/utils_unittest.cc',
diff --git a/extensions/renderer/scoped_web_frame.cc b/extensions/renderer/scoped_web_frame.cc
new file mode 100644
index 0000000..88c4098
--- /dev/null
+++ b/extensions/renderer/scoped_web_frame.cc
@@ -0,0 +1,24 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "extensions/renderer/scoped_web_frame.h"
+
+#include "third_party/WebKit/public/web/WebHeap.h"
+
+namespace extensions {
+
+ScopedWebFrame::ScopedWebFrame() : view_(nullptr), frame_(nullptr) {
+  view_ = blink::WebView::create(nullptr);
+  frame_ = blink::WebLocalFrame::create(
+      blink::WebTreeScopeType::Document, nullptr);
+  view_->setMainFrame(frame_);
+}
+
+ScopedWebFrame::~ScopedWebFrame() {
+  view_->close();
+  frame_->close();
+  blink::WebHeap::collectAllGarbageForTesting();
+}
+
+}  // namespace extensions
diff --git a/extensions/renderer/scoped_web_frame.h b/extensions/renderer/scoped_web_frame.h
new file mode 100644
index 0000000..7950552a
--- /dev/null
+++ b/extensions/renderer/scoped_web_frame.h
@@ -0,0 +1,33 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SCOPED_WEB_FRAME_H_
+#define SCOPED_WEB_FRAME_H_
+
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
+#include "third_party/WebKit/public/web/WebView.h"
+
+namespace extensions {
+
+// ScopedWebFrame is a class to create a dummy webview and frame for testing.
+// The dymmy webview and frame will be destructed when the scope exits.
+class ScopedWebFrame {
+public:
+  ScopedWebFrame();
+  ~ScopedWebFrame();
+
+  blink::WebLocalFrame* frame() { return frame_; }
+
+private:
+  // The webview and the frame are kept alive by the ScopedWebFrame
+  // because they are not destructed unless ~ScopedWebFrame explicitly
+  // closes the webview and the frame.
+  blink::WebView* view_;
+  blink::WebLocalFrame* frame_;
+  DISALLOW_COPY_AND_ASSIGN(ScopedWebFrame);
+};
+
+}  // namespace extensions
+
+#endif  // SCOPED_WEB_FRAME_H_
diff --git a/extensions/renderer/script_context_set_unittest.cc b/extensions/renderer/script_context_set_unittest.cc
index 44da610..ea9d001 100644
--- a/extensions/renderer/script_context_set_unittest.cc
+++ b/extensions/renderer/script_context_set_unittest.cc
@@ -8,23 +8,19 @@
 #include "extensions/common/extension.h"
 #include "extensions/common/extension_set.h"
 #include "extensions/common/features/feature.h"
+#include "extensions/renderer/scoped_web_frame.h"
 #include "extensions/renderer/script_context.h"
 #include "extensions/renderer/script_context_set.h"
 #include "gin/public/context_holder.h"
 #include "testing/gtest/include/gtest/gtest.h"
-#include "third_party/WebKit/public/web/WebLocalFrame.h"
-#include "third_party/WebKit/public/web/WebView.h"
+#include "third_party/WebKit/public/web/WebFrame.h"
 #include "v8/include/v8.h"
 
 namespace extensions {
 
 TEST(ScriptContextSetTest, Lifecycle) {
   base::MessageLoop loop;
-
-  blink::WebView* webview = blink::WebView::create(nullptr);
-  blink::WebLocalFrame* frame =
-      blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, nullptr);
-  webview->setMainFrame(frame);
+  ScopedWebFrame web_frame;
 
   // Do this after construction of the webview, since it may construct the
   // Isolate.
@@ -41,11 +37,11 @@
   ExtensionIdSet active_extensions;
   ScriptContextSet context_set(&extensions, &active_extensions);
   ScriptContext* context = context_set.Register(
-      frame, v8_context, 0, 0);  // no extension group or world ID
+      web_frame.frame(), v8_context, 0, 0);  // no extension group or world ID
 
   // Context is valid and resembles correctness.
   EXPECT_TRUE(context->is_valid());
-  EXPECT_EQ(frame, context->web_frame());
+  EXPECT_EQ(web_frame.frame(), context->web_frame());
   EXPECT_EQ(v8_context, context->v8_context());
 
   // Context has been correctly added.