[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" | ||||
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 6 | #include "base/macros.h" |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 7 | #include "content/public/common/content_switches.h" |
8 | #include "content/public/test/render_view_test.h" | ||||
9 | #include "content/renderer/render_view_impl.h" | ||||
10 | #include "gin/handle.h" | ||||
11 | #include "gin/per_isolate_data.h" | ||||
12 | #include "gin/wrappable.h" | ||||
13 | #include "testing/gtest/include/gtest/gtest.h" | ||||
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 14 | #include "third_party/blink/public/web/blink.h" |
15 | #include "third_party/blink/public/web/web_local_frame.h" | ||||
16 | #include "third_party/blink/public/web/web_view.h" | ||||
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 17 | |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 18 | namespace content { |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 19 | |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 20 | namespace { |
21 | |||||
22 | class TestGinObject : public gin::Wrappable<TestGinObject> { | ||||
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 23 | public: |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 24 | static gin::WrapperInfo kWrapperInfo; |
25 | |||||
26 | static gin::Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) { | ||||
[email protected] | 38804990 | 2013-12-12 09:59:43 | [diff] [blame] | 27 | return gin::CreateHandle(isolate, new TestGinObject(alive)); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 28 | } |
29 | |||||
30 | private: | ||||
31 | TestGinObject(bool* alive) : alive_(alive) { *alive_ = true; } | ||||
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 32 | ~TestGinObject() override { *alive_ = false; } |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 33 | |
34 | bool* alive_; | ||||
35 | |||||
36 | DISALLOW_COPY_AND_ASSIGN(TestGinObject); | ||||
37 | }; | ||||
38 | |||||
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 39 | gin::WrapperInfo TestGinObject::kWrapperInfo = { gin::kEmbedderNativeGin }; |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 40 | |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 41 | class GinBrowserTest : public RenderViewTest { |
42 | public: | ||||
43 | GinBrowserTest() {} | ||||
dcheng | f576215 | 2014-10-29 02:12:06 | [diff] [blame] | 44 | ~GinBrowserTest() override {} |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 45 | |
dcheng | f576215 | 2014-10-29 02:12:06 | [diff] [blame] | 46 | void SetUp() override { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 47 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 48 | switches::kJavaScriptFlags, "--expose_gc"); |
49 | |||||
50 | RenderViewTest::SetUp(); | ||||
51 | } | ||||
52 | |||||
53 | private: | ||||
54 | |||||
55 | DISALLOW_COPY_AND_ASSIGN(GinBrowserTest); | ||||
56 | }; | ||||
57 | |||||
58 | // Test that garbage collection doesn't crash if a gin-wrapped object is | ||||
59 | // present. | ||||
60 | TEST_F(GinBrowserTest, GinAndGarbageCollection) { | ||||
61 | LoadHTML("<!doctype html>"); | ||||
62 | |||||
63 | bool alive = false; | ||||
64 | |||||
65 | { | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 66 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 67 | v8::HandleScope handle_scope(isolate); |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 68 | v8::Context::Scope context_scope(GetMainFrame()->MainWorldScriptContext()); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 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. | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 78 | blink::MainThreadIsolate()->LowMemoryNotification(); |
[email protected] | a6e36967 | 2013-12-12 07:56:53 | [diff] [blame] | 79 | |
80 | CHECK(!alive); | ||||
81 | } | ||||
82 | |||||
83 | } // namespace | ||||
84 | |||||
85 | } // namespace content |