Guarantee CleanUpAfterPage gets run even if CloseConnection fails.
[chromium-blink-merge.git] / extensions / renderer / script_context_set_unittest.cc
blobbf7c38d07089a7d079deb89ded27eb4d5b3e1388
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.
5 #include "base/message_loop/message_loop.h"
6 #include "extensions/common/extension.h"
7 #include "extensions/common/features/feature.h"
8 #include "extensions/renderer/script_context.h"
9 #include "extensions/renderer/script_context_set.h"
10 #include "gin/public/context_holder.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15 #include "v8/include/v8.h"
17 namespace extensions {
19 TEST(ScriptContextSet, Lifecycle) {
20 base::MessageLoop loop;
22 ScriptContextSet context_set;
24 v8::Isolate* isolate = v8::Isolate::GetCurrent();
25 v8::HandleScope handle_scope(isolate);
26 gin::ContextHolder context_holder(isolate);
27 context_holder.SetContext(v8::Context::New(isolate));
29 blink::WebView* webview = blink::WebView::create(nullptr);
30 blink::WebFrame* frame = blink::WebLocalFrame::create(nullptr);
31 webview->setMainFrame(frame);
32 const Extension* extension = NULL;
33 ScriptContext* context =
34 new ScriptContext(context_holder.context(), frame, extension,
35 Feature::BLESSED_EXTENSION_CONTEXT, extension,
36 Feature::BLESSED_EXTENSION_CONTEXT);
38 context_set.Add(context);
39 EXPECT_EQ(1u, context_set.GetAll().count(context));
40 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context()));
42 // Adding the same item multiple times should be OK and deduped.
43 context_set.Add(context);
44 EXPECT_EQ(1u, context_set.GetAll().count(context));
46 // GetAll() returns a copy so removing from one should not remove from others.
47 ScriptContextSet::ContextSet set_copy = context_set.GetAll();
48 EXPECT_EQ(1u, set_copy.count(context));
50 context_set.Remove(context);
51 EXPECT_EQ(0, context_set.size());
52 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context()));
53 EXPECT_EQ(1u, set_copy.size());
55 // After removal, the context should be marked for destruction.
56 EXPECT_FALSE(context->web_frame());
58 // Run loop to do the actual deletion.
59 loop.RunUntilIdle();
62 } // namespace extensions