OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "extensions/common/extension.h" | 6 #include "extensions/common/extension.h" |
7 #include "extensions/common/features/feature.h" | 7 #include "extensions/common/features/feature.h" |
8 #include "extensions/renderer/script_context.h" | 8 #include "extensions/renderer/script_context.h" |
9 #include "extensions/renderer/script_context_set.h" | 9 #include "extensions/renderer/script_context_set.h" |
| 10 #include "gin/public/context_holder.h" |
| 11 #include "gin/public/isolate_holder.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/WebKit/public/web/WebFrame.h" | 13 #include "third_party/WebKit/public/web/WebFrame.h" |
12 #include "v8/include/v8.h" | 14 #include "v8/include/v8.h" |
13 | 15 |
14 namespace extensions { | 16 namespace extensions { |
15 | 17 |
16 TEST(ScriptContextSet, Lifecycle) { | 18 TEST(ScriptContextSet, Lifecycle) { |
17 base::MessageLoop loop; | 19 base::MessageLoop loop; |
18 | 20 |
19 ScriptContextSet context_set; | 21 ScriptContextSet context_set; |
20 | 22 |
21 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 23 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 24 gin::IsolateHolder isolate_holder(isolate, NULL); |
22 v8::HandleScope handle_scope(isolate); | 25 v8::HandleScope handle_scope(isolate); |
23 v8::Handle<v8::Context> v8_context(v8::Context::New(isolate)); | 26 gin::ContextHolder context_holder(isolate); |
| 27 context_holder.SetContext(v8::Context::New(isolate)); |
24 | 28 |
25 // Dirty hack, but we don't actually need the frame, and this is easier than | 29 // Dirty hack, but we don't actually need the frame, and this is easier than |
26 // creating a whole webview. | 30 // creating a whole webview. |
27 blink::WebFrame* frame = reinterpret_cast<blink::WebFrame*>(1); | 31 blink::WebFrame* frame = reinterpret_cast<blink::WebFrame*>(1); |
28 const Extension* extension = NULL; | 32 const Extension* extension = NULL; |
29 ScriptContext* context = new ScriptContext( | 33 ScriptContext* context = |
30 v8_context, frame, extension, Feature::BLESSED_EXTENSION_CONTEXT); | 34 new ScriptContext(context_holder.context(), |
| 35 frame, |
| 36 extension, |
| 37 Feature::BLESSED_EXTENSION_CONTEXT); |
31 | 38 |
32 context_set.Add(context); | 39 context_set.Add(context); |
33 EXPECT_EQ(1u, context_set.GetAll().count(context)); | 40 EXPECT_EQ(1u, context_set.GetAll().count(context)); |
34 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context())); | 41 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context())); |
35 | 42 |
36 // Adding the same item multiple times should be OK and deduped. | 43 // Adding the same item multiple times should be OK and deduped. |
37 context_set.Add(context); | 44 context_set.Add(context); |
38 EXPECT_EQ(1u, context_set.GetAll().count(context)); | 45 EXPECT_EQ(1u, context_set.GetAll().count(context)); |
39 | 46 |
40 // GetAll() returns a copy so removing from one should not remove from others. | 47 // GetAll() returns a copy so removing from one should not remove from others. |
41 ScriptContextSet::ContextSet set_copy = context_set.GetAll(); | 48 ScriptContextSet::ContextSet set_copy = context_set.GetAll(); |
42 EXPECT_EQ(1u, set_copy.count(context)); | 49 EXPECT_EQ(1u, set_copy.count(context)); |
43 | 50 |
44 context_set.Remove(context); | 51 context_set.Remove(context); |
45 EXPECT_EQ(0, context_set.size()); | 52 EXPECT_EQ(0, context_set.size()); |
46 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context())); | 53 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context())); |
47 EXPECT_EQ(1u, set_copy.size()); | 54 EXPECT_EQ(1u, set_copy.size()); |
48 | 55 |
49 // After removal, the context should be marked for destruction. | 56 // After removal, the context should be marked for destruction. |
50 EXPECT_FALSE(context->web_frame()); | 57 EXPECT_FALSE(context->web_frame()); |
51 | 58 |
52 // Run loop to do the actual deletion. | 59 // Run loop to do the actual deletion. |
53 loop.RunUntilIdle(); | 60 loop.RunUntilIdle(); |
54 } | 61 } |
55 | 62 |
56 } // namespace extensions | 63 } // namespace extensions |
OLD | NEW |