[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame^] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/command_line.h" |
| 6 | #include "content/public/common/content_switches.h" |
| 7 | #include "content/public/test/render_view_test.h" |
| 8 | #include "content/renderer/render_view_impl.h" |
| 9 | #include "gin/handle.h" |
| 10 | #include "gin/per_isolate_data.h" |
| 11 | #include "gin/wrappable.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 14 | #include "third_party/WebKit/public/web/WebKit.h" |
| 15 | #include "third_party/WebKit/public/web/WebView.h" |
| 16 | |
| 17 | namespace gin { |
| 18 | |
| 19 | class TestGinObject : public Wrappable<TestGinObject> { |
| 20 | public: |
| 21 | static Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) { |
| 22 | return CreateHandle(isolate, new TestGinObject(alive)); |
| 23 | } |
| 24 | |
| 25 | private: |
| 26 | TestGinObject(bool* alive) : alive_(alive) { *alive_ = true; } |
| 27 | virtual ~TestGinObject() { *alive_ = false; } |
| 28 | |
| 29 | bool* alive_; |
| 30 | |
| 31 | DISALLOW_COPY_AND_ASSIGN(TestGinObject); |
| 32 | }; |
| 33 | |
| 34 | INIT_WRAPPABLE(TestGinObject); |
| 35 | |
| 36 | } // namespace gin |
| 37 | |
| 38 | namespace content { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | void RegisterTemplates(v8::Isolate* isolate) { |
| 43 | gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
| 44 | |
| 45 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate); |
| 46 | templ->SetInternalFieldCount(gin::kNumberOfInternalFields); |
| 47 | data->SetObjectTemplate(&gin::TestGinObject::kWrapperInfo, templ); |
| 48 | } |
| 49 | |
| 50 | class GinBrowserTest : public RenderViewTest { |
| 51 | public: |
| 52 | GinBrowserTest() {} |
| 53 | virtual ~GinBrowserTest() {} |
| 54 | |
| 55 | virtual void SetUp() OVERRIDE { |
| 56 | CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 57 | switches::kJavaScriptFlags, "--expose_gc"); |
| 58 | |
| 59 | RenderViewTest::SetUp(); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(GinBrowserTest); |
| 65 | }; |
| 66 | |
| 67 | // Test that garbage collection doesn't crash if a gin-wrapped object is |
| 68 | // present. |
| 69 | TEST_F(GinBrowserTest, GinAndGarbageCollection) { |
| 70 | LoadHTML("<!doctype html>"); |
| 71 | |
| 72 | bool alive = false; |
| 73 | |
| 74 | { |
| 75 | v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 76 | v8::HandleScope handle_scope(isolate); |
| 77 | v8::Context::Scope context_scope( |
| 78 | view_->GetWebView()->mainFrame()->mainWorldScriptContext()); |
| 79 | |
| 80 | RegisterTemplates(isolate); |
| 81 | |
| 82 | // We create the object inside a scope so it's not kept alive by a handle |
| 83 | // on the stack. |
| 84 | gin::TestGinObject::Create(isolate, &alive); |
| 85 | } |
| 86 | |
| 87 | CHECK(alive); |
| 88 | |
| 89 | // Should not crash. |
| 90 | v8::V8::LowMemoryNotification(); |
| 91 | |
| 92 | CHECK(!alive); |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | |
| 97 | } // namespace content |