Index: extensions/renderer/api_binding_test_util.cc |
diff --git a/extensions/renderer/api_binding_test_util.cc b/extensions/renderer/api_binding_test_util.cc |
index b07fc4a68cb0fa53ece9253b28f2c07f8f6c0e07..d1b0cc80b59a04dc5619f9c21f2f205362aba3f8 100644 |
--- a/extensions/renderer/api_binding_test_util.cc |
+++ b/extensions/renderer/api_binding_test_util.cc |
@@ -14,6 +14,38 @@ |
namespace extensions { |
+namespace { |
+ |
+// Common call function implementation. Calls the given |function| with the |
+// specified |receiver| and arguments. If the call succeeds (doesn't throw an |
+// error), populates |out_value| with the returned result. If the call does |
+// throw, populates |out_error| with the thrown error. |
+// Returns true if the function runs without throwing an error. |
+bool RunFunctionImpl(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ v8::Local<v8::Value> receiver, |
+ int argc, |
+ v8::Local<v8::Value> argv[], |
+ v8::Local<v8::Value>* out_value, |
+ std::string* out_error) { |
+ v8::TryCatch try_catch(context->GetIsolate()); |
+ v8::MaybeLocal<v8::Value> maybe_result = |
+ function->Call(context, receiver, argc, argv); |
+ if (try_catch.HasCaught()) { |
+ *out_error = gin::V8ToString(try_catch.Message()->Get()); |
+ return false; |
+ } |
+ v8::Local<v8::Value> result; |
+ if (!maybe_result.ToLocal(&result)) { |
+ *out_error = "Could not convert result to v8::Local."; |
+ return false; |
+ } |
+ *out_value = result; |
+ return true; |
+} |
+ |
+} // namespace |
+ |
std::string ReplaceSingleQuotes(base::StringPiece str) { |
std::string result; |
base::ReplaceChars(str.as_string(), "'", "\"", &result); |
@@ -67,4 +99,80 @@ std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value, |
return converter->FromV8Value(value, context); |
} |
+v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ v8::Local<v8::Value> receiver, |
+ int argc, |
+ v8::Local<v8::Value> argv[]) { |
+ std::string error; |
+ v8::Local<v8::Value> result; |
+ EXPECT_TRUE( |
+ RunFunctionImpl(function, context, receiver, argc, argv, &result, &error)) |
+ << error; |
+ EXPECT_FALSE(result.IsEmpty()); |
+ return result; |
+} |
+ |
+v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ int argc, |
+ v8::Local<v8::Value> argv[]) { |
+ return RunFunction(function, context, v8::Undefined(context->GetIsolate()), |
+ argc, argv); |
+} |
+ |
+v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ int argc, |
+ v8::Local<v8::Value> argv[]) { |
+ return RunFunction(function, context, context->Global(), argc, argv); |
+} |
+ |
+void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ int argc, |
+ v8::Local<v8::Value> argv[]) { |
+ RunFunction(function, context, context->Global(), argc, argv); |
+} |
+ |
+void RunFunctionAndExpectError(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ v8::Local<v8::Value> receiver, |
+ int argc, |
+ v8::Local<v8::Value> argv[], |
+ const std::string& expected_error) { |
+ std::string error; |
+ v8::Local<v8::Value> result; |
+ EXPECT_FALSE(RunFunctionImpl(function, context, receiver, argc, argv, &result, |
+ &error)); |
+ EXPECT_TRUE(result.IsEmpty()); |
+ EXPECT_EQ(expected_error, error); |
+} |
+ |
+void RunFunctionAndExpectError(v8::Local<v8::Function> function, |
+ v8::Local<v8::Context> context, |
+ int argc, |
+ v8::Local<v8::Value> argv[], |
+ const std::string& expected_error) { |
+ RunFunctionAndExpectError(function, context, |
+ v8::Undefined(context->GetIsolate()), argc, argv, |
+ expected_error); |
+} |
+ |
+v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object, |
+ v8::Local<v8::Context> context, |
+ base::StringPiece key) { |
+ v8::Local<v8::Value> result; |
+ EXPECT_TRUE(object->Get(context, gin::StringToV8(context->GetIsolate(), key)) |
+ .ToLocal(&result)); |
+ return result; |
+} |
+ |
+std::unique_ptr<base::Value> GetBaseValuePropertyFromObject( |
+ v8::Local<v8::Object> object, |
+ v8::Local<v8::Context> context, |
+ base::StringPiece key) { |
+ return V8ToBaseValue(GetPropertyFromObject(object, context, key), context); |
+} |
+ |
} // namespace extensions |