blob: b624742359a2b98eb88b454a7921354b95ac779b [file] [log] [blame]
[email protected]9df013b2014-03-13 22:04:391// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/timer/mock_timer.h"
6
Sebastien Marchand6d0558fd2019-01-25 16:49:377#include "base/bind.h"
Sebastien Marchand17fa2782019-01-25 19:28:108#include "base/bind_helpers.h"
avi9b6f42932015-12-26 22:15:149#include "base/macros.h"
[email protected]9df013b2014-03-13 22:04:3910#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13
14void CallMeMaybe(int *number) {
15 (*number)++;
16}
17
18TEST(MockTimerTest, FiresOnce) {
19 int calls = 0;
tzikfacff252018-07-04 07:55:5220 base::MockOneShotTimer timer;
[email protected]9df013b2014-03-13 22:04:3921 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
22 timer.Start(FROM_HERE, delay,
23 base::Bind(&CallMeMaybe,
24 base::Unretained(&calls)));
25 EXPECT_EQ(delay, timer.GetCurrentDelay());
26 EXPECT_TRUE(timer.IsRunning());
27 timer.Fire();
28 EXPECT_FALSE(timer.IsRunning());
29 EXPECT_EQ(1, calls);
30}
31
32TEST(MockTimerTest, FiresRepeatedly) {
33 int calls = 0;
tzikfacff252018-07-04 07:55:5234 base::MockRepeatingTimer timer;
[email protected]9df013b2014-03-13 22:04:3935 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
36 timer.Start(FROM_HERE, delay,
37 base::Bind(&CallMeMaybe,
38 base::Unretained(&calls)));
39 timer.Fire();
40 EXPECT_TRUE(timer.IsRunning());
41 timer.Fire();
42 timer.Fire();
43 EXPECT_TRUE(timer.IsRunning());
44 EXPECT_EQ(3, calls);
45}
46
47TEST(MockTimerTest, Stops) {
48 int calls = 0;
tzikfacff252018-07-04 07:55:5249 base::MockRepeatingTimer timer;
[email protected]9df013b2014-03-13 22:04:3950 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
51 timer.Start(FROM_HERE, delay,
52 base::Bind(&CallMeMaybe,
53 base::Unretained(&calls)));
54 EXPECT_TRUE(timer.IsRunning());
55 timer.Stop();
56 EXPECT_FALSE(timer.IsRunning());
57}
58
59class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> {
60 public:
Chris Watkinsbb7211c2017-11-29 07:16:3861 HasWeakPtr() = default;
62 virtual ~HasWeakPtr() = default;
[email protected]9df013b2014-03-13 22:04:3963
64 private:
65 DISALLOW_COPY_AND_ASSIGN(HasWeakPtr);
66};
67
[email protected]9df013b2014-03-13 22:04:3968TEST(MockTimerTest, DoesNotRetainClosure) {
69 HasWeakPtr *has_weak_ptr = new HasWeakPtr();
70 base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr());
tzikfacff252018-07-04 07:55:5271 base::MockOneShotTimer timer;
[email protected]9df013b2014-03-13 22:04:3972 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
73 ASSERT_TRUE(weak_ptr.get());
74 timer.Start(FROM_HERE, delay,
Peter Kasting341e1fb2018-02-24 00:03:0175 base::Bind(base::DoNothing::Repeatedly<HasWeakPtr*>(),
[email protected]9df013b2014-03-13 22:04:3976 base::Owned(has_weak_ptr)));
77 ASSERT_TRUE(weak_ptr.get());
78 timer.Fire();
79 ASSERT_FALSE(weak_ptr.get());
80}
81
82} // namespace