blob: 0b51ed46822a448a69c9462e2d1c2dce0ae04cf5 [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,
kylechar83fb51e52019-03-14 15:30:4323 base::BindOnce(&CallMeMaybe, base::Unretained(&calls)));
[email protected]9df013b2014-03-13 22:04:3924 EXPECT_EQ(delay, timer.GetCurrentDelay());
25 EXPECT_TRUE(timer.IsRunning());
26 timer.Fire();
27 EXPECT_FALSE(timer.IsRunning());
28 EXPECT_EQ(1, calls);
29}
30
31TEST(MockTimerTest, FiresRepeatedly) {
32 int calls = 0;
tzikfacff252018-07-04 07:55:5233 base::MockRepeatingTimer timer;
[email protected]9df013b2014-03-13 22:04:3934 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
35 timer.Start(FROM_HERE, delay,
kylechar83fb51e52019-03-14 15:30:4336 base::BindRepeating(&CallMeMaybe, base::Unretained(&calls)));
[email protected]9df013b2014-03-13 22:04:3937 timer.Fire();
38 EXPECT_TRUE(timer.IsRunning());
39 timer.Fire();
40 timer.Fire();
41 EXPECT_TRUE(timer.IsRunning());
42 EXPECT_EQ(3, calls);
43}
44
45TEST(MockTimerTest, Stops) {
46 int calls = 0;
tzikfacff252018-07-04 07:55:5247 base::MockRepeatingTimer timer;
[email protected]9df013b2014-03-13 22:04:3948 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
49 timer.Start(FROM_HERE, delay,
kylechar83fb51e52019-03-14 15:30:4350 base::BindRepeating(&CallMeMaybe, base::Unretained(&calls)));
[email protected]9df013b2014-03-13 22:04:3951 EXPECT_TRUE(timer.IsRunning());
52 timer.Stop();
53 EXPECT_FALSE(timer.IsRunning());
54}
55
56class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> {
57 public:
Chris Watkinsbb7211c2017-11-29 07:16:3858 HasWeakPtr() = default;
59 virtual ~HasWeakPtr() = default;
[email protected]9df013b2014-03-13 22:04:3960
61 private:
62 DISALLOW_COPY_AND_ASSIGN(HasWeakPtr);
63};
64
[email protected]9df013b2014-03-13 22:04:3965TEST(MockTimerTest, DoesNotRetainClosure) {
66 HasWeakPtr *has_weak_ptr = new HasWeakPtr();
67 base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr());
tzikfacff252018-07-04 07:55:5268 base::MockOneShotTimer timer;
[email protected]9df013b2014-03-13 22:04:3969 base::TimeDelta delay = base::TimeDelta::FromSeconds(2);
70 ASSERT_TRUE(weak_ptr.get());
71 timer.Start(FROM_HERE, delay,
kylechar83fb51e52019-03-14 15:30:4372 base::BindOnce(base::DoNothing::Once<HasWeakPtr*>(),
73 base::Owned(has_weak_ptr)));
[email protected]9df013b2014-03-13 22:04:3974 ASSERT_TRUE(weak_ptr.get());
75 timer.Fire();
76 ASSERT_FALSE(weak_ptr.get());
77}
78
79} // namespace