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

Unified Diff: gin/arguments_unittest.cc

Issue 2765853004: [Gin] Add Arguments::GetHolderCreationContext() (Closed)
Patch Set: Add test Created 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gin/arguments.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gin/arguments_unittest.cc
diff --git a/gin/arguments_unittest.cc b/gin/arguments_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d2b51361995cdc2f38a9cb4ccc5481833422ab96
--- /dev/null
+++ b/gin/arguments_unittest.cc
@@ -0,0 +1,67 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gin/arguments.h"
+
+#include "base/bind.h"
+#include "gin/converter.h"
+#include "gin/object_template_builder.h"
+#include "gin/public/isolate_holder.h"
+#include "gin/test/v8_test.h"
+#include "v8/include/v8.h"
+
+namespace gin {
+
+using ArgumentsTest = V8Test;
+
+// Test that Arguments::GetHolderCreationContext returns the proper context.
+TEST_F(ArgumentsTest, TestArgumentsHolderCreationContext) {
+ v8::Isolate* isolate = instance_->isolate();
+ v8::HandleScope handle_scope(isolate);
+
+ v8::Local<v8::Context> creation_context = context_.Get(instance_->isolate());
+
+ auto check_creation_context = [](v8::Local<v8::Context> expected_context,
+ gin::Arguments* arguments) {
+ EXPECT_EQ(expected_context, arguments->GetHolderCreationContext());
+ };
+
+ // Create an object that will compare GetHolderCreationContext() with
+ // |creation_context|.
+ v8::Local<v8::ObjectTemplate> object_template =
+ ObjectTemplateBuilder(isolate)
+ .SetMethod("checkCreationContext",
+ base::Bind(check_creation_context, creation_context))
+ .Build();
+
+ v8::Local<v8::Object> object =
+ object_template->NewInstance(creation_context).ToLocalChecked();
+
+ // Call checkCreationContext() on the generated object using the passed-in
+ // context as the current context.
+ auto test_context = [object, isolate](v8::Local<v8::Context> context) {
+ v8::Context::Scope context_scope(context);
+ const char kCallFunction[] = "(function(o) { o.checkCreationContext(); })";
+ v8::Local<v8::Script> script =
+ v8::Script::Compile(context, StringToV8(isolate, kCallFunction))
+ .ToLocalChecked();
+ v8::Local<v8::Function> function;
+ ASSERT_TRUE(ConvertFromV8(isolate, script->Run(), &function));
+ v8::Local<v8::Value> args[] = {object};
+ function->Call(v8::Undefined(isolate), arraysize(args), args);
+ };
+
+ // Test calling in the creation context.
+ test_context(creation_context);
+
+ {
+ // Create a second context, and test calling in that. The creation context
+ // should be the same (even though the current context has changed).
+ v8::Local<v8::Context> second_context =
+ v8::Context::New(isolate, nullptr, v8::Local<v8::ObjectTemplate>());
+ test_context(second_context);
+ }
+}
+
+} // namespace gin
« no previous file with comments | « gin/arguments.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698