| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "base/message_loop.h" |
| 6 #include "chrome/common/extensions/feature.h" |
| 6 #include "chrome/renderer/extensions/chrome_v8_context.h" | 7 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 7 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | 8 #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 10 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
| 11 | 12 |
| 12 TEST(ChromeV8ContextSet, Lifecycle) { | 13 TEST(ChromeV8ContextSet, Lifecycle) { |
| 13 MessageLoop loop; | 14 MessageLoop loop; |
| 14 | 15 |
| 15 ChromeV8ContextSet context_set; | 16 ChromeV8ContextSet context_set; |
| 16 | 17 |
| 17 v8::HandleScope handle_scope; | 18 v8::HandleScope handle_scope; |
| 18 v8::Handle<v8::Context> v8_context(v8::Context::New()); | 19 v8::Handle<v8::Context> v8_context(v8::Context::New()); |
| 19 | 20 |
| 20 // Dirty hack, but we don't actually need the frame, and this is easier than | 21 // Dirty hack, but we don't actually need the frame, and this is easier than |
| 21 // creating a whole webview. | 22 // creating a whole webview. |
| 22 WebKit::WebFrame* frame = reinterpret_cast<WebKit::WebFrame*>(1); | 23 WebKit::WebFrame* frame = reinterpret_cast<WebKit::WebFrame*>(1); |
| 23 std::string extension_id = "00000000000000000000000000000000"; | 24 std::string extension_id = "00000000000000000000000000000000"; |
| 24 ChromeV8Context* context = | 25 ChromeV8Context* context = new ChromeV8Context( |
| 25 new ChromeV8Context( | 26 v8_context, |
| 26 v8_context, frame, extension_id, ChromeV8Context::OTHER); | 27 frame, |
| 28 extension_id, |
| 29 extensions::Feature::PRIVILEGED_CONTEXT); |
| 27 | 30 |
| 28 context_set.Add(context); | 31 context_set.Add(context); |
| 29 EXPECT_EQ(1u, context_set.GetAll().count(context)); | 32 EXPECT_EQ(1u, context_set.GetAll().count(context)); |
| 30 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context())); | 33 EXPECT_EQ(context, context_set.GetByV8Context(context->v8_context())); |
| 31 | 34 |
| 32 // Adding the same item multiple times should be OK and deduped. | 35 // Adding the same item multiple times should be OK and deduped. |
| 33 context_set.Add(context); | 36 context_set.Add(context); |
| 34 EXPECT_EQ(1u, context_set.GetAll().count(context)); | 37 EXPECT_EQ(1u, context_set.GetAll().count(context)); |
| 35 | 38 |
| 36 // GetAll() returns a copy so removing from one should not remove from others. | 39 // GetAll() returns a copy so removing from one should not remove from others. |
| 37 ChromeV8ContextSet::ContextSet set_copy = context_set.GetAll(); | 40 ChromeV8ContextSet::ContextSet set_copy = context_set.GetAll(); |
| 38 EXPECT_EQ(1u, set_copy.count(context)); | 41 EXPECT_EQ(1u, set_copy.count(context)); |
| 39 | 42 |
| 40 context_set.Remove(context); | 43 context_set.Remove(context); |
| 41 EXPECT_EQ(0, context_set.size()); | 44 EXPECT_EQ(0, context_set.size()); |
| 42 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context())); | 45 EXPECT_FALSE(context_set.GetByV8Context(context->v8_context())); |
| 43 EXPECT_EQ(1u, set_copy.size()); | 46 EXPECT_EQ(1u, set_copy.size()); |
| 44 | 47 |
| 45 // After removal, the context should be marked for destruction. | 48 // After removal, the context should be marked for destruction. |
| 46 EXPECT_FALSE(context->web_frame()); | 49 EXPECT_FALSE(context->web_frame()); |
| 47 | 50 |
| 48 // Run loop to do the actual deletion. | 51 // Run loop to do the actual deletion. |
| 49 loop.RunAllPending(); | 52 loop.RunAllPending(); |
| 50 } | 53 } |
| OLD | NEW |