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

base / task / delayed_task_handle_unittest.cc [blame]

// Copyright 2021 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/task/delayed_task_handle.h"

#include "base/memory/raw_ptr.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

// A non-working delegate that allows the testing of DelayedTaskHandle.
class TestDelegate : public DelayedTaskHandle::Delegate {
 public:
  explicit TestDelegate(bool* was_cancel_task_called = nullptr)
      : was_cancel_task_called_(was_cancel_task_called) {
    DCHECK(!was_cancel_task_called_ || !*was_cancel_task_called_);
  }
  ~TestDelegate() override = default;

  bool IsValid() const override { return is_valid_; }
  void CancelTask() override {
    is_valid_ = false;

    if (was_cancel_task_called_)
      *was_cancel_task_called_ = true;
  }

 private:
  // Indicates if this delegate is currently valid.
  bool is_valid_ = true;

  // Indicates if CancelTask() was invoked, if not null. Must outlives |this|.
  raw_ptr<bool> was_cancel_task_called_;
};

}  // namespace

// Tests that a default-constructed handle is invalid.
TEST(DelayedTaskHandleTest, DefaultConstructor) {
  DelayedTaskHandle delayed_task_handle;
  EXPECT_FALSE(delayed_task_handle.IsValid());
}

// Tests that creating a handle with an invalid delegate will DCHECK.
TEST(DelayedTaskHandleTest, RequiresValidDelegateOnConstruction) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  // Invalidate the delegate before creating the handle.
  delegate->CancelTask();
  EXPECT_FALSE(delegate->IsValid());

  EXPECT_DCHECK_DEATH(
      { DelayedTaskHandle delayed_task_handle(std::move(delegate)); });
}

// Tests that calling CancelTask() on the handle will call CancelTask() on the
// delegate and invalidate it.
TEST(DelayedTaskHandleTest, CancelTask) {
  bool was_cancel_task_called = false;
  auto delegate = std::make_unique<TestDelegate>(&was_cancel_task_called);
  EXPECT_FALSE(was_cancel_task_called);
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_FALSE(was_cancel_task_called);
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  delayed_task_handle.CancelTask();

  EXPECT_TRUE(was_cancel_task_called);
  EXPECT_FALSE(delayed_task_handle.IsValid());
}

// Tests that calling CancelTask() on a handle with no delegate will no-op.
TEST(DelayedTaskHandleTest, CancelTaskNoDelegate) {
  DelayedTaskHandle delayed_task_handle;
  EXPECT_FALSE(delayed_task_handle.IsValid());

  delayed_task_handle.CancelTask();
  EXPECT_FALSE(delayed_task_handle.IsValid());
}

// Tests that calling CancelTask() on a handle with an invalid delegate doesn't
// crash and keeps the handle invalid.
TEST(DelayedTaskHandleTest, CancelTaskInvalidDelegate) {
  bool was_cancel_task_called = false;
  auto delegate = std::make_unique<TestDelegate>(&was_cancel_task_called);
  EXPECT_FALSE(was_cancel_task_called);
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_FALSE(was_cancel_task_called);
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  delegate_ptr->CancelTask();

  EXPECT_TRUE(was_cancel_task_called);
  EXPECT_FALSE(delegate_ptr->IsValid());
  EXPECT_FALSE(delayed_task_handle.IsValid());

  // Reset |was_cancel_task_called| to ensure Delegate::CancelTask() is still
  // invoked on an invalid delegate.
  was_cancel_task_called = false;

  delayed_task_handle.CancelTask();

  EXPECT_TRUE(was_cancel_task_called);
  EXPECT_FALSE(delayed_task_handle.IsValid());
}

// Tests that invalidating the delegate will also invalidate the handle.
TEST(DelayedTaskHandleTest, InvalidateDelegate) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  delegate_ptr->CancelTask();

  EXPECT_FALSE(delegate_ptr->IsValid());
  EXPECT_FALSE(delayed_task_handle.IsValid());
}

// Tests that destroying a valid handle will DCHECK.
TEST(DelayedTaskHandleTest, InvalidOnDestuction) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  EXPECT_DCHECK_DEATH({
    DelayedTaskHandle delayed_task_handle(std::move(delegate));
    EXPECT_TRUE(delegate_ptr->IsValid());
    EXPECT_TRUE(delayed_task_handle.IsValid());
  });
}

// Tests the move-constructor.
TEST(DelayedTaskHandleTest, MoveConstructor) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  DelayedTaskHandle other_delayed_task_handle(std::move(delayed_task_handle));
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(other_delayed_task_handle.IsValid());

  // Clean-up.
  other_delayed_task_handle.CancelTask();
}

// Tests the move-assignment.
TEST(DelayedTaskHandleTest, MoveAssignment) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  DelayedTaskHandle other_delayed_task_handle;
  EXPECT_FALSE(other_delayed_task_handle.IsValid());

  other_delayed_task_handle = std::move(delayed_task_handle);
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(other_delayed_task_handle.IsValid());

  // Clean-up.
  other_delayed_task_handle.CancelTask();
}

// Tests that assigning to a valid handle will DCHECK.
TEST(DelayedTaskHandleTest, AssignToValidHandle) {
  auto delegate = std::make_unique<TestDelegate>();
  EXPECT_TRUE(delegate->IsValid());

  auto* delegate_ptr = delegate.get();
  DelayedTaskHandle delayed_task_handle(std::move(delegate));
  EXPECT_TRUE(delegate_ptr->IsValid());
  EXPECT_TRUE(delayed_task_handle.IsValid());

  EXPECT_DCHECK_DEATH({ delayed_task_handle = DelayedTaskHandle(); });

  // Clean-up.
  delayed_task_handle.CancelTask();
}

}  // namespace base