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

Unified Diff: extensions/renderer/api_bindings_system_unittest.cc

Issue 2552343006: [Extensions Binding] Allow for registering custom hooks (Closed)
Patch Set: . Created 4 years 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 | « extensions/renderer/api_bindings_system.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/api_bindings_system_unittest.cc
diff --git a/extensions/renderer/api_bindings_system_unittest.cc b/extensions/renderer/api_bindings_system_unittest.cc
index b1fcfce546d19661ea463c6d627d0695bbffdc66..f59f4261f48319fade8fb210a422569e7229f3ba 100644
--- a/extensions/renderer/api_bindings_system_unittest.cc
+++ b/extensions/renderer/api_bindings_system_unittest.cc
@@ -12,8 +12,11 @@
#include "base/values.h"
#include "extensions/common/extension_api.h"
#include "extensions/renderer/api_binding.h"
+#include "extensions/renderer/api_binding_hooks.h"
#include "extensions/renderer/api_binding_test.h"
#include "extensions/renderer/api_binding_test_util.h"
+#include "extensions/renderer/api_binding_types.h"
+#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/try_catch.h"
@@ -289,6 +292,54 @@ TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) {
}
}
+// Tests adding a custom hook to an API.
+TEST_F(APIBindingsSystemTest, TestCustomHooks) {
+ v8::HandleScope handle_scope(isolate());
+ v8::Local<v8::Context> context = ContextLocal();
+
+ bool did_call = false;
+ auto hook = [](bool* did_call, const binding::APISignature* signature,
+ gin::Arguments* arguments) {
+ *did_call = true;
+ std::string argument;
+ EXPECT_EQ(2, arguments->Length());
+ EXPECT_TRUE(arguments->GetNext(&argument));
+ EXPECT_EQ("foo", argument);
+ v8::Local<v8::Function> function;
+ ASSERT_TRUE(arguments->GetNext(&function));
+ v8::Local<v8::String> response =
+ gin::StringToV8(arguments->isolate(), "bar");
+ v8::Local<v8::Value> response_args[] = {response};
+ RunFunctionOnGlobal(function, arguments->isolate()->GetCurrentContext(),
+ 1, response_args);
+ };
+
+ APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName);
+ ASSERT_TRUE(hooks);
+ hooks->RegisterHandleRequest(
+ "alpha.functionWithCallback",
+ base::Bind(hook, &did_call));
+
+ v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance(
+ kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs));
+ ASSERT_FALSE(alpha_api.IsEmpty());
+
+ {
+ // Test a simple call -> response.
+ const char kTestCall[] =
+ "obj.functionWithCallback('foo', function() {\n"
+ " this.callbackArguments = Array.from(arguments);\n"
+ "});";
+ CallFunctionOnObject(context, alpha_api, kTestCall);
+ EXPECT_TRUE(did_call);
+
+ std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject(
+ context->Global(), context, "callbackArguments");
+ ASSERT_TRUE(result);
+ EXPECT_EQ("[\"bar\"]", ValueToString(*result));
+ }
+}
+
// An implementation using real API schemas.
class APIBindingsSystemTestWithRealAPI : public APIBindingsSystemTestBase {
protected:
« no previous file with comments | « extensions/renderer/api_bindings_system.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698