blob: cddd0f70f78ac5becd23a2514c23b164526827a5 [file] [log] [blame]
[email protected]a6e369672013-12-12 07:56:531// 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"
avi1023d012015-12-25 02:39:146#include "base/macros.h"
[email protected]a6e369672013-12-12 07:56:537#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 Reformata30d4232018-04-07 15:31:0614#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]a6e369672013-12-12 07:56:5317
[email protected]b4acaf82013-12-12 09:40:5018namespace content {
[email protected]a6e369672013-12-12 07:56:5319
[email protected]b4acaf82013-12-12 09:40:5020namespace {
21
22class TestGinObject : public gin::Wrappable<TestGinObject> {
[email protected]a6e369672013-12-12 07:56:5323 public:
[email protected]b4acaf82013-12-12 09:40:5024 static gin::WrapperInfo kWrapperInfo;
25
26 static gin::Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) {
[email protected]388049902013-12-12 09:59:4327 return gin::CreateHandle(isolate, new TestGinObject(alive));
[email protected]a6e369672013-12-12 07:56:5328 }
29
30 private:
31 TestGinObject(bool* alive) : alive_(alive) { *alive_ = true; }
dcheng6d18e402014-10-21 12:32:5232 ~TestGinObject() override { *alive_ = false; }
[email protected]a6e369672013-12-12 07:56:5333
34 bool* alive_;
35
36 DISALLOW_COPY_AND_ASSIGN(TestGinObject);
37};
38
[email protected]b4acaf82013-12-12 09:40:5039gin::WrapperInfo TestGinObject::kWrapperInfo = { gin::kEmbedderNativeGin };
[email protected]a6e369672013-12-12 07:56:5340
[email protected]a6e369672013-12-12 07:56:5341class GinBrowserTest : public RenderViewTest {
42 public:
43 GinBrowserTest() {}
dchengf5762152014-10-29 02:12:0644 ~GinBrowserTest() override {}
[email protected]a6e369672013-12-12 07:56:5345
dchengf5762152014-10-29 02:12:0646 void SetUp() override {
avi83883c82014-12-23 00:08:4947 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]a6e369672013-12-12 07:56:5348 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.
60TEST_F(GinBrowserTest, GinAndGarbageCollection) {
61 LoadHTML("<!doctype html>");
62
63 bool alive = false;
64
65 {
Blink Reformat1c4d759e2017-04-09 16:34:5466 v8::Isolate* isolate = blink::MainThreadIsolate();
[email protected]a6e369672013-12-12 07:56:5367 v8::HandleScope handle_scope(isolate);
lukaszadf18ba762017-06-09 22:24:3068 v8::Context::Scope context_scope(GetMainFrame()->MainWorldScriptContext());
[email protected]a6e369672013-12-12 07:56:5369
[email protected]a6e369672013-12-12 07:56:5370 // We create the object inside a scope so it's not kept alive by a handle
71 // on the stack.
[email protected]b4acaf82013-12-12 09:40:5072 TestGinObject::Create(isolate, &alive);
[email protected]a6e369672013-12-12 07:56:5373 }
74
75 CHECK(alive);
76
77 // Should not crash.
Blink Reformat1c4d759e2017-04-09 16:34:5478 blink::MainThreadIsolate()->LowMemoryNotification();
[email protected]a6e369672013-12-12 07:56:5379
80 CHECK(!alive);
81}
82
83} // namespace
84
85} // namespace content