1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431

base / functional / callback_helpers_unittest.cc [blame]

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

#include "base/functional/callback_helpers.h"

#include <functional>
#include <type_traits>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

struct BadArg {};

template <typename TagType, typename CallbackType>
struct TestConversionAndAssignmentImpl {
  static constexpr bool kSupportsConversion =
      std::is_convertible_v<TagType, CallbackType>;
  static constexpr bool kSupportsAssignment =
      std::is_assignable_v<CallbackType, TagType>;
  static_assert(kSupportsConversion == kSupportsAssignment);

  static constexpr bool kValue = kSupportsConversion;
};

template <typename T, typename U>
constexpr bool TestConversionAndAssignment =
    TestConversionAndAssignmentImpl<T, U>::kValue;

#define VOID_RETURN_CALLBACK_TAG_TEST(CallbackType, Sig, BadSig, BoundArg)   \
  static_assert(TestConversionAndAssignment<decltype(base::NullCallback()),  \
                                            CallbackType<Sig>>);             \
  static_assert(                                                             \
      TestConversionAndAssignment<decltype(base::NullCallbackAs<Sig>()),     \
                                  CallbackType<Sig>>);                       \
  static_assert(TestConversionAndAssignment<decltype(base::DoNothing()),     \
                                            CallbackType<Sig>>);             \
  static_assert(                                                             \
      TestConversionAndAssignment<decltype(base::DoNothingAs<Sig>()),        \
                                  CallbackType<Sig>>);                       \
  static_assert(TestConversionAndAssignment<                                 \
                decltype(base::DoNothingWithBoundArgs(BoundArg)),            \
                CallbackType<Sig>>);                                         \
                                                                             \
  static_assert(                                                             \
      !TestConversionAndAssignment<decltype(base::NullCallbackAs<BadSig>()), \
                                   CallbackType<Sig>>);                      \
  static_assert(                                                             \
      !TestConversionAndAssignment<decltype(base::DoNothingAs<BadSig>()),    \
                                   CallbackType<Sig>>);                      \
  static_assert(TestConversionAndAssignment<                                 \
                decltype(base::DoNothingWithBoundArgs(BadArg())),            \
                CallbackType<Sig>>)

#define NON_VOID_RETURN_CALLBACK_TAG_TEST(CallbackType, Sig, BadSig, BoundArg) \
  static_assert(TestConversionAndAssignment<decltype(base::NullCallback()),    \
                                            CallbackType<Sig>>);               \
  static_assert(                                                               \
      TestConversionAndAssignment<decltype(base::NullCallbackAs<Sig>()),       \
                                  CallbackType<Sig>>);                         \
                                                                               \
  /* Unlike callbacks that return void, callbacks that return non-void      */ \
  /* should not be implicitly convertible from DoNothingCallbackTag since   */ \
  /* this would require guessing what the callback should return.           */ \
  static_assert(!TestConversionAndAssignment<decltype(base::DoNothing()),      \
                                             CallbackType<Sig>>);              \
  static_assert(                                                               \
      !TestConversionAndAssignment<decltype(base::DoNothingAs<Sig>()),         \
                                   CallbackType<Sig>>);                        \
  static_assert(!TestConversionAndAssignment<                                  \
                decltype(base::DoNothingWithBoundArgs(BoundArg)),              \
                CallbackType<Sig>>);                                           \
                                                                               \
  static_assert(                                                               \
      !TestConversionAndAssignment<decltype(base::NullCallbackAs<BadSig>()),   \
                                   CallbackType<Sig>>);                        \
  static_assert(                                                               \
      !TestConversionAndAssignment<decltype(base::DoNothingAs<BadSig>()),      \
                                   CallbackType<Sig>>);                        \
  static_assert(!TestConversionAndAssignment<                                  \
                decltype(base::DoNothingWithBoundArgs(BadArg())),              \
                CallbackType<Sig>>)

VOID_RETURN_CALLBACK_TAG_TEST(base::OnceCallback, void(), void(char), );
VOID_RETURN_CALLBACK_TAG_TEST(base::OnceCallback, void(int), void(char), 8);
NON_VOID_RETURN_CALLBACK_TAG_TEST(base::OnceCallback, int(int), char(int), 8);

VOID_RETURN_CALLBACK_TAG_TEST(base::RepeatingCallback, void(), void(char), );
VOID_RETURN_CALLBACK_TAG_TEST(base::RepeatingCallback,
                              void(int),
                              void(char),
                              8);
NON_VOID_RETURN_CALLBACK_TAG_TEST(base::RepeatingCallback,
                                  int(int),
                                  char(int),
                                  8);

#undef VOID_RETURN_CALLBACK_TAG_TEST
#undef NON_VOID_RETURN_CALLBACK_TAG_TEST

TEST(CallbackHelpersTest, IsBaseCallback) {
  // Check that base::{Once,Repeating}Closures and references to them are
  // considered base::{Once,Repeating}Callbacks.
  static_assert(base::IsBaseCallback<base::OnceClosure>);
  static_assert(base::IsBaseCallback<base::RepeatingClosure>);
  static_assert(base::IsBaseCallback<base::OnceClosure&&>);
  static_assert(base::IsBaseCallback<const base::RepeatingClosure&>);

  // Check that base::{Once, Repeating}Callbacks with a given RunType and
  // references to them are considered base::{Once, Repeating}Callbacks.
  static_assert(base::IsBaseCallback<base::OnceCallback<int(int)>>);
  static_assert(base::IsBaseCallback<base::RepeatingCallback<int(int)>>);
  static_assert(base::IsBaseCallback<base::OnceCallback<int(int)>&&>);
  static_assert(base::IsBaseCallback<const base::RepeatingCallback<int(int)>&>);

  // Check that POD types are not considered base::{Once, Repeating}Callbacks.
  static_assert(!base::IsBaseCallback<bool>);
  static_assert(!base::IsBaseCallback<int>);
  static_assert(!base::IsBaseCallback<double>);

  // Check that the closely related std::function is not considered a
  // base::{Once, Repeating}Callback.
  static_assert(!base::IsBaseCallback<std::function<void()>>);
  static_assert(!base::IsBaseCallback<const std::function<void()>&>);
  static_assert(!base::IsBaseCallback<std::function<void()>&&>);
}

void Increment(int* value) {
  (*value)++;
}

void IncrementWithRef(int& value) {
  value++;
}

int IncrementAndReturn(int* value) {
  return ++(*value);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerHasClosure) {
  base::ScopedClosureRunner runner1;
  EXPECT_FALSE(runner1);

  base::ScopedClosureRunner runner2{base::DoNothing()};
  EXPECT_TRUE(runner2);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerExitScope) {
  int run_count = 0;
  {
    base::ScopedClosureRunner runner(base::BindOnce(&Increment, &run_count));
    EXPECT_EQ(0, run_count);
  }
  EXPECT_EQ(1, run_count);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerRelease) {
  int run_count = 0;
  base::OnceClosure c;
  {
    base::ScopedClosureRunner runner(base::BindOnce(&Increment, &run_count));
    c = runner.Release();
    EXPECT_EQ(0, run_count);
  }
  EXPECT_EQ(0, run_count);
  std::move(c).Run();
  EXPECT_EQ(1, run_count);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerReplaceClosure) {
  int run_count_1 = 0;
  int run_count_2 = 0;
  {
    base::ScopedClosureRunner runner;
    runner.ReplaceClosure(base::BindOnce(&Increment, &run_count_1));
    runner.ReplaceClosure(base::BindOnce(&Increment, &run_count_2));
    EXPECT_EQ(0, run_count_1);
    EXPECT_EQ(0, run_count_2);
  }
  EXPECT_EQ(0, run_count_1);
  EXPECT_EQ(1, run_count_2);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerRunAndResetNonNull) {
  int run_count_3 = 0;
  {
    base::ScopedClosureRunner runner(base::BindOnce(&Increment, &run_count_3));
    EXPECT_EQ(0, run_count_3);
    runner.RunAndReset();
    EXPECT_EQ(1, run_count_3);
  }
  EXPECT_EQ(1, run_count_3);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerRunAndResetNull) {
  base::ScopedClosureRunner runner;
  runner.RunAndReset();  // Should not crash.
}

TEST(CallbackHelpersTest, ScopedClosureRunnerMoveConstructor) {
  int run_count = 0;
  {
    std::unique_ptr<base::ScopedClosureRunner> runner(
        new base::ScopedClosureRunner(base::BindOnce(&Increment, &run_count)));
    base::ScopedClosureRunner runner2(std::move(*runner));
    runner.reset();
    EXPECT_EQ(0, run_count);
  }
  EXPECT_EQ(1, run_count);
}

TEST(CallbackHelpersTest, ScopedClosureRunnerMoveAssignment) {
  int run_count_1 = 0;
  int run_count_2 = 0;
  {
    base::ScopedClosureRunner runner(base::BindOnce(&Increment, &run_count_1));
    {
      base::ScopedClosureRunner runner2(
          base::BindOnce(&Increment, &run_count_2));
      runner = std::move(runner2);
      EXPECT_EQ(1, run_count_1);
      EXPECT_EQ(0, run_count_2);
    }
    EXPECT_EQ(1, run_count_1);
    EXPECT_EQ(0, run_count_2);
  }
  EXPECT_EQ(1, run_count_1);
  EXPECT_EQ(1, run_count_2);
}

TEST(CallbackHelpersTest, SplitOnceCallback_EmptyCallback) {
  base::OnceCallback<void(int*)> cb = base::NullCallback();
  EXPECT_FALSE(cb);

  auto split = base::SplitOnceCallback(std::move(cb));

  static_assert(std::is_same_v<decltype(split),
                               std::pair<base::OnceCallback<void(int*)>,
                                         base::OnceCallback<void(int*)>>>,
                "");
  EXPECT_FALSE(split.first);
  EXPECT_FALSE(split.second);
}

TEST(CallbackHelpersTest, SplitOnceCallback_FirstCallback) {
  int count = 0;
  base::OnceCallback<void(int*)> cb =
      base::BindOnce([](int* count) { ++*count; });

  auto split = base::SplitOnceCallback(std::move(cb));

  static_assert(std::is_same_v<decltype(split),
                               std::pair<base::OnceCallback<void(int*)>,
                                         base::OnceCallback<void(int*)>>>,
                "");

  EXPECT_EQ(0, count);
  std::move(split.first).Run(&count);
  EXPECT_EQ(1, count);

#if GTEST_HAS_DEATH_TEST
  EXPECT_CHECK_DEATH(std::move(split.second).Run(&count));
#endif  // GTEST_HAS_DEATH_TEST
}

TEST(CallbackHelpersTest, SplitOnceCallback_SecondCallback) {
  int count = 0;
  base::OnceCallback<void(int*)> cb =
      base::BindOnce([](int* count) { ++*count; });

  auto split = base::SplitOnceCallback(std::move(cb));

  static_assert(std::is_same_v<decltype(split),
                               std::pair<base::OnceCallback<void(int*)>,
                                         base::OnceCallback<void(int*)>>>,
                "");

  EXPECT_EQ(0, count);
  std::move(split.second).Run(&count);
  EXPECT_EQ(1, count);

  EXPECT_CHECK_DEATH(std::move(split.first).Run(&count));
}

TEST(CallbackHelpersTest, SplitSplitOnceCallback_FirstSplit) {
  int count = 0;
  base::OnceCallback<void(int*)> cb =
      base::BindOnce([](int* count) { ++*count; });

  auto split = base::SplitOnceCallback(std::move(cb));
  base::OnceCallback<void(int*)> cb1 = std::move(split.first);
  split = base::SplitOnceCallback(std::move(split.second));
  base::OnceCallback<void(int*)> cb2 = std::move(split.first);
  base::OnceCallback<void(int*)> cb3 = std::move(split.second);

  EXPECT_EQ(0, count);
  std::move(cb1).Run(&count);
  EXPECT_EQ(1, count);

  EXPECT_CHECK_DEATH(std::move(cb3).Run(&count));
}

TEST(CallbackHelpersTest, SplitSplitOnceCallback_SecondSplit) {
  int count = 0;
  base::OnceCallback<void(int*)> cb =
      base::BindOnce([](int* count) { ++*count; });

  auto split = base::SplitOnceCallback(std::move(cb));
  base::OnceCallback<void(int*)> cb1 = std::move(split.first);
  split = base::SplitOnceCallback(std::move(split.second));
  base::OnceCallback<void(int*)> cb2 = std::move(split.first);
  base::OnceCallback<void(int*)> cb3 = std::move(split.second);

  EXPECT_EQ(0, count);
  std::move(cb2).Run(&count);
  EXPECT_EQ(1, count);

  EXPECT_CHECK_DEATH(std::move(cb1).Run(&count));
}

TEST(CallbackHelpersTest, IgnoreArgs) {
  int count = 0;
  base::RepeatingClosure repeating_closure =
      base::BindRepeating(&Increment, &count);
  base::OnceClosure once_closure = base::BindOnce(&Increment, &count);

  base::RepeatingCallback<void(int)> repeating_int_cb =
      base::IgnoreArgs<int>(repeating_closure);
  EXPECT_EQ(0, count);
  repeating_int_cb.Run(42);
  EXPECT_EQ(1, count);
  repeating_int_cb.Run(42);
  EXPECT_EQ(2, count);

  base::OnceCallback<void(int)> once_int_cb =
      base::IgnoreArgs<int>(std::move(once_closure));
  EXPECT_EQ(2, count);
  std::move(once_int_cb).Run(42);
  EXPECT_EQ(3, count);

  // Ignore only some (one) argument and forward the rest.
  auto repeating_callback = base::BindRepeating(&Increment);
  auto repeating_cb_with_extra_arg = base::IgnoreArgs<bool>(repeating_callback);
  repeating_cb_with_extra_arg.Run(false, &count);
  EXPECT_EQ(4, count);

  // Ignore two arguments and forward the rest.
  auto once_callback = base::BindOnce(&Increment);
  auto once_cb_with_extra_arg =
      base::IgnoreArgs<char, bool>(repeating_callback);
  std::move(once_cb_with_extra_arg).Run('d', false, &count);
  EXPECT_EQ(5, count);
}

TEST(CallbackHelpersTest, IgnoreArgs_EmptyCallback) {
  base::RepeatingCallback<void(int)> repeating_int_cb =
      base::IgnoreArgs<int>(base::RepeatingClosure());
  EXPECT_FALSE(repeating_int_cb);

  base::OnceCallback<void(int)> once_int_cb =
      base::IgnoreArgs<int>(base::OnceClosure());
  EXPECT_FALSE(once_int_cb);
}

TEST(CallbackHelpersTest, IgnoreArgs_NonVoidReturn) {
  int count = 0;
  base::RepeatingCallback<int(void)> repeating_no_param_cb =
      base::BindRepeating(&IncrementAndReturn, &count);
  base::OnceCallback<int(void)> once_no_param_cb =
      base::BindOnce(&IncrementAndReturn, &count);

  base::RepeatingCallback<int(int)> repeating_int_cb =
      base::IgnoreArgs<int>(repeating_no_param_cb);
  EXPECT_EQ(0, count);
  EXPECT_EQ(1, repeating_int_cb.Run(42));
  EXPECT_EQ(1, count);
  EXPECT_EQ(2, repeating_int_cb.Run(42));
  EXPECT_EQ(2, count);

  base::OnceCallback<int(int)> once_int_cb =
      base::IgnoreArgs<int>(std::move(once_no_param_cb));
  EXPECT_EQ(2, count);
  EXPECT_EQ(3, std::move(once_int_cb).Run(42));
  EXPECT_EQ(3, count);

  // Ignore only some (one) argument and forward the rest.
  auto repeating_cb = base::BindRepeating(&IncrementAndReturn);
  auto repeating_cb_with_extra_arg = base::IgnoreArgs<bool>(repeating_cb);
  EXPECT_EQ(4, repeating_cb_with_extra_arg.Run(false, &count));
  EXPECT_EQ(4, count);

  // Ignore two arguments and forward the rest.
  auto once_cb = base::BindOnce(&IncrementAndReturn);
  auto once_cb_with_extra_arg =
      base::IgnoreArgs<char, bool>(std::move(once_cb));
  EXPECT_EQ(5, std::move(once_cb_with_extra_arg).Run('d', false, &count));
  EXPECT_EQ(5, count);
}

TEST(CallbackHelpersTest, ForwardRepeatingCallbacks) {
  int count = 0;
  auto tie_cb =
      base::ForwardRepeatingCallbacks({base::BindRepeating(&IncrementWithRef),
                                       base::BindRepeating(&IncrementWithRef)});

  tie_cb.Run(count);
  EXPECT_EQ(count, 2);

  tie_cb.Run(count);
  EXPECT_EQ(count, 4);
}

TEST(CallbackHelpersTest, ReturnValueOnce) {
  // Check that copyable types are supported.
  auto string_factory = base::ReturnValueOnce(std::string("test"));
  static_assert(std::is_same_v<decltype(string_factory),
                               base::OnceCallback<std::string(void)>>);
  EXPECT_EQ(std::move(string_factory).Run(), "test");

  // Check that move-only types are supported.
  auto unique_ptr_factory = base::ReturnValueOnce(std::make_unique<int>(42));
  static_assert(std::is_same_v<decltype(unique_ptr_factory),
                               base::OnceCallback<std::unique_ptr<int>(void)>>);
  EXPECT_EQ(*std::move(unique_ptr_factory).Run(), 42);
}

}  // namespace