[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 1 | // 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 Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 7 | #include "base/bind.h" |
Sebastien Marchand | 17fa278 | 2019-01-25 19:28:10 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include "base/macros.h" |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | void CallMeMaybe(int *number) { |
| 15 | (*number)++; |
| 16 | } |
| 17 | |
| 18 | TEST(MockTimerTest, FiresOnce) { |
| 19 | int calls = 0; |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 20 | base::MockOneShotTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 21 | base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| 22 | timer.Start(FROM_HERE, delay, |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 23 | base::BindOnce(&CallMeMaybe, base::Unretained(&calls))); |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 24 | 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 | |
| 31 | TEST(MockTimerTest, FiresRepeatedly) { |
| 32 | int calls = 0; |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 33 | base::MockRepeatingTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 34 | base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| 35 | timer.Start(FROM_HERE, delay, |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 36 | base::BindRepeating(&CallMeMaybe, base::Unretained(&calls))); |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 37 | 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 | |
| 45 | TEST(MockTimerTest, Stops) { |
| 46 | int calls = 0; |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 47 | base::MockRepeatingTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 48 | base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| 49 | timer.Start(FROM_HERE, delay, |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 50 | base::BindRepeating(&CallMeMaybe, base::Unretained(&calls))); |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 51 | EXPECT_TRUE(timer.IsRunning()); |
| 52 | timer.Stop(); |
| 53 | EXPECT_FALSE(timer.IsRunning()); |
| 54 | } |
| 55 | |
| 56 | class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> { |
| 57 | public: |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 58 | HasWeakPtr() = default; |
| 59 | virtual ~HasWeakPtr() = default; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | DISALLOW_COPY_AND_ASSIGN(HasWeakPtr); |
| 63 | }; |
| 64 | |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 65 | TEST(MockTimerTest, DoesNotRetainClosure) { |
| 66 | HasWeakPtr *has_weak_ptr = new HasWeakPtr(); |
| 67 | base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr()); |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 68 | base::MockOneShotTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 69 | base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| 70 | ASSERT_TRUE(weak_ptr.get()); |
| 71 | timer.Start(FROM_HERE, delay, |
kylechar | 83fb51e5 | 2019-03-14 15:30:43 | [diff] [blame] | 72 | base::BindOnce(base::DoNothing::Once<HasWeakPtr*>(), |
| 73 | base::Owned(has_weak_ptr))); |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 74 | ASSERT_TRUE(weak_ptr.get()); |
| 75 | timer.Fire(); |
| 76 | ASSERT_FALSE(weak_ptr.get()); |
| 77 | } |
| 78 | |
| 79 | } // namespace |