1 // Copyright 2014 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.
7 #include "base/message_loop/message_loop.h"
8 #include "extensions/common/extension.h"
9 #include "extensions/common/extension_set.h"
10 #include "extensions/common/features/feature.h"
11 #include "extensions/renderer/script_context.h"
12 #include "extensions/renderer/script_context_set.h"
13 #include "gin/public/context_holder.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/web/WebLocalFrame.h"
16 #include "third_party/WebKit/public/web/WebView.h"
17 #include "v8/include/v8.h"
19 namespace extensions
{
21 TEST(ScriptContextSetTest
, Lifecycle
) {
22 base::MessageLoop loop
;
24 blink::WebView
* webview
= blink::WebView::create(nullptr);
25 blink::WebLocalFrame
* frame
=
26 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document
, nullptr);
27 webview
->setMainFrame(frame
);
29 // Do this after construction of the webview, since it may construct the
31 v8::Isolate
* isolate
= v8::Isolate::GetCurrent();
33 v8::HandleScope
handle_scope(isolate
);
34 v8::Local
<v8::Context
> v8_context
= v8::Context::New(isolate
);
35 v8::Context::Scope
context_scope(v8_context
);
36 // ScriptContext relies on gin, it just doesn't look like it from here.
37 gin::ContextHolder
context_holder(isolate
);
38 context_holder
.SetContext(v8_context
);
40 ExtensionSet extensions
;
41 ExtensionIdSet active_extensions
;
42 ScriptContextSet
context_set(&extensions
, &active_extensions
);
43 ScriptContext
* context
= context_set
.Register(
44 frame
, v8_context
, 0, 0); // no extension group or world ID
46 // Context is valid and resembles correctness.
47 EXPECT_TRUE(context
->is_valid());
48 EXPECT_EQ(frame
, context
->web_frame());
49 EXPECT_EQ(v8_context
, context
->v8_context());
51 // Context has been correctly added.
52 EXPECT_EQ(1u, context_set
.size());
53 EXPECT_EQ(context
, context_set
.GetByV8Context(v8_context
));
55 // Test context is correctly removed.
56 context_set
.Remove(context
);
57 EXPECT_EQ(0u, context_set
.size());
58 EXPECT_EQ(nullptr, context_set
.GetByV8Context(v8_context
));
60 // After removal, the context should be marked for destruction.
61 EXPECT_FALSE(context
->is_valid());
63 // Run loop to do the actual deletion.
67 } // namespace extensions