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