[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 | |||||
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 17 | namespace content { |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 18 | |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 19 | namespace { |
20 | |||||
21 | class TestGinObject : public gin::Wrappable<TestGinObject> { | ||||
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 22 | public: |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 23 | static gin::WrapperInfo kWrapperInfo; |
24 | |||||
25 | static gin::Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) { | ||||
[email protected] | 38804990 | 2013-12-12 09:59:43 | [diff] [blame] | 26 | return gin::CreateHandle(isolate, new TestGinObject(alive)); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 27 | } |
28 | |||||
29 | private: | ||||
30 | TestGinObject(bool* alive) : alive_(alive) { *alive_ = true; } | ||||
31 | virtual ~TestGinObject() { *alive_ = false; } | ||||
32 | |||||
33 | bool* alive_; | ||||
34 | |||||
35 | DISALLOW_COPY_AND_ASSIGN(TestGinObject); | ||||
36 | }; | ||||
37 | |||||
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 38 | gin::WrapperInfo TestGinObject::kWrapperInfo = { gin::kEmbedderNativeGin }; |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 39 | |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 40 | class GinBrowserTest : public RenderViewTest { |
41 | public: | ||||
42 | GinBrowserTest() {} | ||||
43 | virtual ~GinBrowserTest() {} | ||||
44 | |||||
45 | virtual void SetUp() OVERRIDE { | ||||
46 | CommandLine::ForCurrentProcess()->AppendSwitchASCII( | ||||
47 | switches::kJavaScriptFlags, "--expose_gc"); | ||||
48 | |||||
49 | RenderViewTest::SetUp(); | ||||
50 | } | ||||
51 | |||||
52 | private: | ||||
53 | |||||
54 | DISALLOW_COPY_AND_ASSIGN(GinBrowserTest); | ||||
55 | }; | ||||
56 | |||||
57 | // Test that garbage collection doesn't crash if a gin-wrapped object is | ||||
58 | // present. | ||||
59 | TEST_F(GinBrowserTest, GinAndGarbageCollection) { | ||||
60 | LoadHTML("<!doctype html>"); | ||||
61 | |||||
62 | bool alive = false; | ||||
63 | |||||
64 | { | ||||
65 | v8::Isolate* isolate = blink::mainThreadIsolate(); | ||||
66 | v8::HandleScope handle_scope(isolate); | ||||
67 | v8::Context::Scope context_scope( | ||||
68 | view_->GetWebView()->mainFrame()->mainWorldScriptContext()); | ||||
69 | |||||
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 70 | // We create the object inside a scope so it's not kept alive by a handle |
71 | // on the stack. | ||||
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 72 | TestGinObject::Create(isolate, &alive); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 73 | } |
74 | |||||
75 | CHECK(alive); | ||||
76 | |||||
77 | // Should not crash. | ||||
78 | v8::V8::LowMemoryNotification(); | ||||
79 | |||||
80 | CHECK(!alive); | ||||
81 | } | ||||
82 | |||||
83 | } // namespace | ||||
84 | |||||
85 | } // namespace content |