Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(590)

Side by Side Diff: extensions/renderer/script_context_set_unittest.cc

Issue 995283004: Move Extension ScriptContext creation into ScriptContextSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ready Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
6
5 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
6 #include "extensions/common/extension.h" 8 #include "extensions/common/extension.h"
9 #include "extensions/common/extension_set.h"
7 #include "extensions/common/features/feature.h" 10 #include "extensions/common/features/feature.h"
8 #include "extensions/renderer/script_context.h" 11 #include "extensions/renderer/script_context.h"
9 #include "extensions/renderer/script_context_set.h" 12 #include "extensions/renderer/script_context_set.h"
10 #include "gin/public/context_holder.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #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/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebView.h" 15 #include "third_party/WebKit/public/web/WebView.h"
15 #include "v8/include/v8.h" 16 #include "v8/include/v8.h"
16 17
17 namespace extensions { 18 namespace extensions {
18 19
19 TEST(ScriptContextSet, Lifecycle) { 20 TEST(ScriptContextSetTest, Lifecycle) {
20 base::MessageLoop loop; 21 base::MessageLoop loop;
21 22
22 ScriptContextSet context_set; 23 ExtensionSet extensions;
24 ExtensionIdSet active_extensions;
25 ScriptContextSet context_set(&extensions, &active_extensions);
23 26
27 blink::WebView* webview = blink::WebView::create(nullptr);
28 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(nullptr);
29 webview->setMainFrame(frame);
24 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 30 v8::Isolate* isolate = v8::Isolate::GetCurrent();
25 v8::HandleScope handle_scope(isolate); 31 v8::HandleScope handle_scope(isolate);
26 gin::ContextHolder context_holder(isolate); 32 v8::Handle<v8::Context> v8_context = v8::Context::New(isolate);
27 context_holder.SetContext(v8::Context::New(isolate)); 33 v8::Context::Scope context_scope(v8_context);
34 ScriptContext* context = context_set.Register(
35 frame, v8_context, 0, 0); // no extension group or world ID
28 36
29 blink::WebView* webview = blink::WebView::create(nullptr); 37 // Context is valid and resembles correctness.
30 blink::WebFrame* frame = blink::WebLocalFrame::create(nullptr); 38 EXPECT_TRUE(context->is_valid());
31 webview->setMainFrame(frame); 39 EXPECT_EQ(frame, context->web_frame());
32 const Extension* extension = NULL; 40 EXPECT_EQ(v8_context, context->v8_context());
33 ScriptContext* context =
34 new ScriptContext(context_holder.context(),
35 frame,
36 extension,
37 Feature::BLESSED_EXTENSION_CONTEXT,
38 extension,
39 Feature::BLESSED_EXTENSION_CONTEXT);
40 41
41 context_set.Add(context); 42 // Context has been correctly added.
42 EXPECT_EQ(1u, context_set.GetAll().count(context)); 43 EXPECT_EQ(1u, context_set.size());
43 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context())); 44 EXPECT_EQ(context, context_set.GetByV8Context(v8_context));
44 45
45 // Adding the same item multiple times should be OK and deduped. 46 // Test context is correctly removed.
46 context_set.Add(context); 47 context_set.Remove(context);
47 EXPECT_EQ(1u, context_set.GetAll().count(context)); 48 EXPECT_EQ(0u, context_set.size());
49 EXPECT_EQ(nullptr, context_set.GetByV8Context(v8_context));
48 50
49 // GetAll() returns a copy so removing from one should not remove from others. 51 // After removal, the context should be invalid.
50 ScriptContextSet::ContextSet set_copy = context_set.GetAll(); 52 EXPECT_FALSE(context->is_valid());
51 EXPECT_EQ(1u, set_copy.count(context)); 53 EXPECT_EQ(nullptr, context->web_frame());
52
53 context_set.Remove(context);
54 EXPECT_EQ(0, context_set.size());
55 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context()));
56 EXPECT_EQ(1u, set_copy.size());
57
58 // After removal, the context should be marked for destruction.
59 EXPECT_FALSE(context->web_frame());
60 54
61 // Run loop to do the actual deletion. 55 // Run loop to do the actual deletion.
62 loop.RunUntilIdle(); 56 loop.RunUntilIdle();
63 } 57 }
64 58
65 } // namespace extensions 59 } // namespace extensions
OLDNEW
« extensions/renderer/script_context_set.cc ('K') | « extensions/renderer/script_context_set.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698