1 // Copyright (c) 2012 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 "chrome/renderer/extensions/chrome_v8_context.h"
7 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
8 #include "extensions/common/extension.h"
9 #include "extensions/common/features/feature.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/web/WebFrame.h"
12 #include "v8/include/v8.h"
14 namespace extensions {
16 TEST(ChromeV8ContextSet, Lifecycle) {
17 base::MessageLoop loop;
19 ChromeV8ContextSet context_set;
21 v8::Isolate* isolate = v8::Isolate::GetCurrent();
22 v8::HandleScope handle_scope(isolate);
23 v8::Handle<v8::Context> v8_context(v8::Context::New(isolate));
25 // Dirty hack, but we don't actually need the frame, and this is easier than
26 // creating a whole webview.
27 blink::WebFrame* frame = reinterpret_cast<blink::WebFrame*>(1);
28 const Extension* extension = NULL;
29 ChromeV8Context* context = new ChromeV8Context(
33 Feature::BLESSED_EXTENSION_CONTEXT);
35 context_set.Add(context);
36 EXPECT_EQ(1u, context_set.GetAll().count(context));
37 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context()));
39 // Adding the same item multiple times should be OK and deduped.
40 context_set.Add(context);
41 EXPECT_EQ(1u, context_set.GetAll().count(context));
43 // GetAll() returns a copy so removing from one should not remove from others.
44 ChromeV8ContextSet::ContextSet set_copy = context_set.GetAll();
45 EXPECT_EQ(1u, set_copy.count(context));
47 context_set.Remove(context);
48 EXPECT_EQ(0, context_set.size());
49 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context()));
50 EXPECT_EQ(1u, set_copy.size());
52 // After removal, the context should be marked for destruction.
53 EXPECT_FALSE(context->web_frame());
55 // Run loop to do the actual deletion.
59 } // namespace extensions