chromium/extensions/renderer/bindings/api_binding_unittest.cc

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/bindings/api_binding.h"

#include <string_view>
#include <tuple>

#include "base/auto_reset.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "extensions/renderer/bindings/api_binding_hooks.h"
#include "extensions/renderer/bindings/api_binding_hooks_test_delegate.h"
#include "extensions/renderer/bindings/api_binding_test.h"
#include "extensions/renderer/bindings/api_binding_test_util.h"
#include "extensions/renderer/bindings/api_binding_types.h"
#include "extensions/renderer/bindings/api_binding_util.h"
#include "extensions/renderer/bindings/api_event_handler.h"
#include "extensions/renderer/bindings/api_invocation_errors.h"
#include "extensions/renderer/bindings/api_request_handler.h"
#include "extensions/renderer/bindings/api_signature.h"
#include "extensions/renderer/bindings/api_type_reference_map.h"
#include "extensions/renderer/bindings/binding_access_checker.h"
#include "extensions/renderer/bindings/exception_handler.h"
#include "extensions/renderer/bindings/test_interaction_provider.h"
#include "extensions/renderer/bindings/test_js_runner.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/public/context_holder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"

namespace extensions {

namespace {

const char kBindingName[] =;

// Function spec; we use single quotes for readability and then replace them.
const char kFunctions[] =;

constexpr char kFunctionsWithCallbackSignatures[] =;

constexpr char kFunctionsWithPromiseSignatures[] =;

bool AllowAllFeatures(v8::Local<v8::Context> context, const std::string& name) {}

bool DisallowPromises(v8::Local<v8::Context> context) {}

void OnEventListenersChanged(const std::string& event_name,
                             binding::EventListenersChanged change,
                             const base::Value::Dict* filter,
                             bool was_manual,
                             v8::Local<v8::Context> context) {}

}  // namespace

class APIBindingUnittest : public APIBindingTest {};

APIBindingDeathTest;

v8::Local<v8::Value> APIBindingUnittest::RunTest(
    v8::Local<v8::Context> context,
    v8::Local<v8::Object> object,
    const std::string& script_source,
    bool should_pass,
    const std::string& expected_json_arguments,
    bool expect_async_handler,
    const std::string& expected_error) {}

TEST_F(APIBindingUnittest, TestEmptyAPI) {}

// Tests the basic call -> request flow of the API binding (ensuring that
// functions are set up correctly and correctly enforced).
TEST_F(APIBindingUnittest, TestBasicAPICalls) {}

// Test that enum values are properly exposed on the binding object.
TEST_F(APIBindingUnittest, EnumValues) {}

// Test that empty enum entries are (unfortunately) allowed.
TEST_F(APIBindingUnittest, EnumWithEmptyEntry) {}

// Test that type references are correctly set up in the API.
TEST_F(APIBindingUnittest, TypeRefsTest) {}

TEST_F(APIBindingUnittest, RestrictedAPIs) {}

// Tests that events specified in the API are created as properties of the API
// object.
TEST_F(APIBindingUnittest, TestEventCreation) {}

TEST_F(APIBindingUnittest, TestProperties) {}

TEST_F(APIBindingUnittest, TestRefProperties) {}

TEST_F(APIBindingUnittest, TestDisposedContext) {}

TEST_F(APIBindingUnittest, TestInvalidatedContext) {}

TEST_F(APIBindingUnittest, MultipleContexts) {}

// Tests adding custom hooks for an API method.
TEST_F(APIBindingUnittest, TestCustomHooks) {}

TEST_F(APIBindingUnittest, TestJSCustomHook) {}

// Tests the updateArgumentsPreValidate hook.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPreValidate) {}

// Tests the updateArgumentsPreValidate hook.
TEST_F(APIBindingUnittest, TestThrowInUpdateArgumentsPreValidate) {}

// Tests that custom JS hooks can return results synchronously.
TEST_F(APIBindingUnittest, TestReturningResultFromCustomJSHook) {}

// Tests that the setHandleRequest hook can use callbacks and promises.
TEST_F(APIBindingUnittest, TestReturningPromiseFromHandleRequestHook) {}

// Tests that JS custom hooks can throw exceptions for bad invocations.
TEST_F(APIBindingUnittest, TestThrowingFromCustomJSHook) {}

// Tests that JS setHandleRequestHooks can use the failure callback to return a
// failure result for an API.
TEST_F(APIBindingUnittest, TestHandleRequestFailureCallback) {}

// Tests that a JS handle request hook that calls the resolver callback more
// than once will fail gracefully on a release build. Regression test for
// https://crbug.com/1298409.
TEST_F(APIBindingUnittest, TestHandleRequestHookCalledTwiceGracefulRegression) {}

// Tests that JS custom hooks correctly handle the context being invalidated.
// Regression test for https://crbug.com/944014.
TEST_F(APIBindingUnittest, TestInvalidatingInCustomHook) {}

// Tests that native custom hooks can return results synchronously, or throw
// exceptions for bad invocations.
TEST_F(APIBindingUnittest,
       TestReturningResultAndThrowingExceptionFromCustomNativeHook) {}

// Tests the updateArgumentsPostValidate hook.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidate) {}

// Tests using setUpdateArgumentsPostValidate to return a list of arguments
// that violates the function schema. Sadly, this should succeed. :(
// See comment in api_binding.cc.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidateViolatingSchema) {}

// Test that user gestures are properly recorded when calling APIs.
TEST_F(APIBindingUnittest, TestUserGestures) {}

TEST_F(APIBindingUnittest, FilteredEvents) {}

TEST_F(APIBindingUnittest, HooksTemplateInitializer) {}

TEST_F(APIBindingUnittest, HooksInstanceInitializer) {}

// Test that running hooks returning different results correctly sends requests
// or notifies of silent requests.
TEST_F(APIBindingUnittest, TestSendingRequestsAndSilentRequestsWithHooks) {}

// Test native hooks that don't handle the result, but set a custom callback
// instead.
TEST_F(APIBindingUnittest, TestHooksWithCustomCallback) {}

// Test native hooks that don't handle the result, but add a result modifier.
TEST_F(APIBindingUnittest, TestHooksWithResultModifier) {}

// Test native hooks that add a result modifier are compatible with JS hooks
// which handle the request.
TEST_F(APIBindingUnittest, TestHooksWithResultModifierAndJSHook) {}

TEST_F(APIBindingUnittest, AccessAPIMethodsAndEventsAfterInvalidation) {}

TEST_F(APIBindingUnittest, CallbackSignaturesAreAdded) {}

TEST_F(APIBindingUnittest,
       CallbackSignaturesAreNotAddedWhenValidationDisabled) {}

// Tests promise-based APIs exposed on bindings.
TEST_F(APIBindingUnittest, PromiseBasedAPIs) {}

TEST_F(APIBindingUnittest, TestPromisesWithJSCustomCallback) {}

TEST_F(APIBindingUnittest, TestPromiseWithJSUpdateArgumentsPreValidate) {}

TEST_F(APIBindingUnittest, TestPromiseWithJSUpdateArgumentsPostValidate) {}

}  // namespace extensions