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

base / timer / mock_timer_unittest.cc [blame]

// Copyright 2014 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/timer/mock_timer.h"

#include "base/functional/bind.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

void CallMeMaybe(int *number) {
  (*number)++;
}

TEST(MockTimerTest, FiresOnce) {
  int calls = 0;
  base::MockOneShotTimer timer;
  base::TimeDelta delay = base::Seconds(2);
  timer.Start(FROM_HERE, delay,
              base::BindOnce(&CallMeMaybe, base::Unretained(&calls)));
  EXPECT_EQ(delay, timer.GetCurrentDelay());
  EXPECT_TRUE(timer.IsRunning());
  timer.Fire();
  EXPECT_FALSE(timer.IsRunning());
  EXPECT_EQ(1, calls);
}

TEST(MockTimerTest, FiresRepeatedly) {
  int calls = 0;
  base::MockRepeatingTimer timer;
  base::TimeDelta delay = base::Seconds(2);
  timer.Start(FROM_HERE, delay,
              base::BindRepeating(&CallMeMaybe, base::Unretained(&calls)));
  timer.Fire();
  EXPECT_TRUE(timer.IsRunning());
  timer.Fire();
  timer.Fire();
  EXPECT_TRUE(timer.IsRunning());
  EXPECT_EQ(3, calls);
}

TEST(MockTimerTest, Stops) {
  int calls = 0;
  base::MockRepeatingTimer timer;
  base::TimeDelta delay = base::Seconds(2);
  timer.Start(FROM_HERE, delay,
              base::BindRepeating(&CallMeMaybe, base::Unretained(&calls)));
  EXPECT_TRUE(timer.IsRunning());
  timer.Stop();
  EXPECT_FALSE(timer.IsRunning());
}

TEST(MockOneShotTimerTest, FireNow) {
  int calls = 0;
  base::MockOneShotTimer timer;
  base::TimeDelta delay = base::Seconds(2);
  timer.Start(FROM_HERE, delay,
              base::BindOnce(&CallMeMaybe, base::Unretained(&calls)));
  EXPECT_EQ(delay, timer.GetCurrentDelay());
  EXPECT_TRUE(timer.IsRunning());
  timer.FireNow();
  EXPECT_FALSE(timer.IsRunning());
  EXPECT_EQ(1, calls);
}

class HasWeakPtr {
 public:
  HasWeakPtr() = default;

  HasWeakPtr(const HasWeakPtr&) = delete;
  HasWeakPtr& operator=(const HasWeakPtr&) = delete;

  virtual ~HasWeakPtr() = default;

  base::WeakPtr<HasWeakPtr> AsWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  base::WeakPtrFactory<HasWeakPtr> weak_ptr_factory_{this};
};

TEST(MockTimerTest, DoesNotRetainClosure) {
  HasWeakPtr *has_weak_ptr = new HasWeakPtr();
  base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr());
  base::MockOneShotTimer timer;
  base::TimeDelta delay = base::Seconds(2);
  ASSERT_TRUE(weak_ptr.get());
  timer.Start(FROM_HERE, delay,
              base::BindOnce([](HasWeakPtr*) {}, base::Owned(has_weak_ptr)));
  ASSERT_TRUE(weak_ptr.get());
  timer.Fire();
  ASSERT_FALSE(weak_ptr.get());
}

}  // namespace