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

base / cancelable_callback_unittest.cc [blame]

// Copyright 2011 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/cancelable_callback.h"

#include <memory>
#include <optional>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace {

class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
 private:
  friend class RefCountedThreadSafe<TestRefCounted>;
  ~TestRefCounted() = default;
};

void Increment(int* count) { (*count)++; }
void IncrementBy(int* count, int n) { (*count) += n; }
void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}

void OnMoveOnlyReceived(int* value, std::unique_ptr<int> result) {
  *value = *result;
}

// Cancel().
//  - Callback can be run multiple times.
//  - After Cancel(), Run() completes but has no effect.
TEST(CancelableCallbackTest, Cancel) {
  int count = 0;
  CancelableRepeatingClosure cancelable(
      BindRepeating(&Increment, Unretained(&count)));

  RepeatingClosure callback = cancelable.callback();
  callback.Run();
  EXPECT_EQ(1, count);

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

  cancelable.Cancel();
  callback.Run();
  EXPECT_EQ(2, count);
}

// Cancel() called multiple times.
//  - Cancel() cancels all copies of the wrapped callback.
//  - Calling Cancel() more than once has no effect.
//  - After Cancel(), callback() returns a null callback.
TEST(CancelableCallbackTest, MultipleCancel) {
  int count = 0;
  CancelableRepeatingClosure cancelable(
      BindRepeating(&Increment, Unretained(&count)));

  RepeatingClosure callback1 = cancelable.callback();
  RepeatingClosure callback2 = cancelable.callback();
  cancelable.Cancel();

  callback1.Run();
  EXPECT_EQ(0, count);

  callback2.Run();
  EXPECT_EQ(0, count);

  // Calling Cancel() again has no effect.
  cancelable.Cancel();

  // callback() of a cancelled callback is null.
  RepeatingClosure callback3 = cancelable.callback();
  EXPECT_TRUE(callback3.is_null());
}

// CancelableRepeatingCallback destroyed before callback is run.
//  - Destruction of CancelableRepeatingCallback cancels outstanding callbacks.
TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
  int count = 0;
  RepeatingClosure callback;

  {
    CancelableRepeatingClosure cancelable(
        BindRepeating(&Increment, Unretained(&count)));

    callback = cancelable.callback();
    callback.Run();
    EXPECT_EQ(1, count);
  }

  callback.Run();
  EXPECT_EQ(1, count);
}

// Cancel() called on bound closure with a RefCounted parameter.
//  - Cancel drops wrapped callback (and, implicitly, its bound arguments).
TEST(CancelableCallbackTest, CancelDropsCallback) {
  scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
  EXPECT_TRUE(ref_counted->HasOneRef());

  CancelableOnceClosure cancelable(BindOnce(RefCountedParam, ref_counted));
  EXPECT_FALSE(cancelable.IsCancelled());
  EXPECT_TRUE(ref_counted.get());
  EXPECT_FALSE(ref_counted->HasOneRef());

  // There is only one reference to |ref_counted| after the Cancel().
  cancelable.Cancel();
  EXPECT_TRUE(cancelable.IsCancelled());
  EXPECT_TRUE(ref_counted.get());
  EXPECT_TRUE(ref_counted->HasOneRef());
}

// Reset().
//  - Reset() replaces the existing wrapped callback with a new callback.
//  - Reset() deactivates outstanding callbacks.
TEST(CancelableCallbackTest, Reset) {
  int count = 0;
  CancelableRepeatingClosure cancelable(
      BindRepeating(&Increment, Unretained(&count)));

  RepeatingClosure callback = cancelable.callback();
  callback.Run();
  EXPECT_EQ(1, count);

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

  cancelable.Reset(BindRepeating(&IncrementBy, Unretained(&count), 3));
  EXPECT_FALSE(cancelable.IsCancelled());

  // The stale copy of the cancelable callback is non-null.
  ASSERT_FALSE(callback.is_null());

  // The stale copy of the cancelable callback is no longer active.
  callback.Run();
  EXPECT_EQ(2, count);

  RepeatingClosure callback2 = cancelable.callback();
  ASSERT_FALSE(callback2.is_null());

  callback2.Run();
  EXPECT_EQ(5, count);
}

// IsCanceled().
//  - Cancel() transforms the CancelableOnceCallback into a cancelled state.
TEST(CancelableCallbackTest, IsNull) {
  CancelableOnceClosure cancelable;
  EXPECT_TRUE(cancelable.IsCancelled());

  int count = 0;
  cancelable.Reset(BindOnce(&Increment, Unretained(&count)));
  EXPECT_FALSE(cancelable.IsCancelled());

  cancelable.Cancel();
  EXPECT_TRUE(cancelable.IsCancelled());
}

// CancelableRepeatingCallback posted to a task environment with PostTask.
//  - Posted callbacks can be cancelled.
//  - Chained callbacks from `.Then()` still run on cancelled callbacks.
TEST(CancelableCallbackTest, PostTask) {
  test::SingleThreadTaskEnvironment task_environment;

  int count = 0;
  CancelableRepeatingClosure cancelable(
      BindRepeating(&Increment, Unretained(&count)));

  SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
                                                        cancelable.callback());
  RunLoop().RunUntilIdle();

  EXPECT_EQ(1, count);

  SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
                                                        cancelable.callback());

  // Cancel before running the task.
  cancelable.Cancel();
  RunLoop().RunUntilIdle();

  // Callback never ran due to cancellation; count is the same.
  EXPECT_EQ(1, count);

  // Chain a callback to the cancelable callback.
  cancelable.Reset(BindRepeating(&Increment, Unretained(&count)));
  SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, cancelable.callback().Then(
                     BindRepeating(&IncrementBy, Unretained(&count), 2)));

  // Cancel before running the task.
  cancelable.Cancel();
  RunLoop().RunUntilIdle();

  // Callback never ran due to cancellation, but chained callback still should
  // have. Count should increase by exactly two.
  EXPECT_EQ(3, count);
}

// CancelableRepeatingCallback posted to a task environment with
// PostTaskAndReply.
//  - Posted callbacks can be cancelled.
TEST(CancelableCallbackTest, PostTaskAndReply) {
  std::optional<test::SingleThreadTaskEnvironment> task_environment;
  task_environment.emplace();

  int count = 0;
  CancelableRepeatingClosure cancelable_reply(
      BindRepeating(&Increment, Unretained(&count)));

  std::optional<RunLoop> loop;
  loop.emplace();
  SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply(
      FROM_HERE, DoNothing(),
      cancelable_reply.callback().Then(loop->QuitClosure()));
  loop->Run();

  EXPECT_EQ(1, count);

  loop.emplace();
  SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply(
      FROM_HERE, DoNothing(),
      cancelable_reply.callback().Then(loop->QuitClosure()));

  // Cancel before running the tasks.
  cancelable_reply.Cancel();
  loop->Run();

  // Callback never ran due to cancellation; count is the same. Note that
  // QuitClosure() is still invoked because chained callbacks via Then() get
  // invoked even if the first callback is cancelled.
  EXPECT_EQ(1, count);

  // Post it again to exercise a shutdown-like scenario.
  cancelable_reply.Reset(BindRepeating(&Increment, Unretained(&count)));

  SingleThreadTaskRunner::GetCurrentDefault()->PostTaskAndReply(
      FROM_HERE, DoNothing(), cancelable_reply.callback());
  task_environment.reset();

  // Callback never ran due to task runner shutdown; count is the same.
  EXPECT_EQ(1, count);
}

// CancelableRepeatingCallback can be used with move-only types.
TEST(CancelableCallbackTest, MoveOnlyType) {
  const int kExpectedResult = 42;

  int result = 0;
  CancelableRepeatingCallback<void(std::unique_ptr<int>)> cb(
      BindRepeating(&OnMoveOnlyReceived, Unretained(&result)));
  cb.callback().Run(std::make_unique<int>(kExpectedResult));

  EXPECT_EQ(kExpectedResult, result);
}

}  // namespace
}  // namespace base