[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, |
| 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 | |
| 32 | TEST(MockTimerTest, FiresRepeatedly) { |
| 33 | int calls = 0; |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 34 | base::MockRepeatingTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 35 | 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 | |
| 47 | TEST(MockTimerTest, Stops) { |
| 48 | int calls = 0; |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 49 | base::MockRepeatingTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 50 | 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 | |
| 59 | class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> { |
| 60 | public: |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 61 | HasWeakPtr() = default; |
| 62 | virtual ~HasWeakPtr() = default; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | DISALLOW_COPY_AND_ASSIGN(HasWeakPtr); |
| 66 | }; |
| 67 | |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 68 | TEST(MockTimerTest, DoesNotRetainClosure) { |
| 69 | HasWeakPtr *has_weak_ptr = new HasWeakPtr(); |
| 70 | base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr()); |
tzik | facff25 | 2018-07-04 07:55:52 | [diff] [blame] | 71 | base::MockOneShotTimer timer; |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 72 | base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| 73 | ASSERT_TRUE(weak_ptr.get()); |
| 74 | timer.Start(FROM_HERE, delay, |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 75 | base::Bind(base::DoNothing::Repeatedly<HasWeakPtr*>(), |
[email protected] | 9df013b | 2014-03-13 22:04:39 | [diff] [blame] | 76 | base::Owned(has_weak_ptr))); |
| 77 | ASSERT_TRUE(weak_ptr.get()); |
| 78 | timer.Fire(); |
| 79 | ASSERT_FALSE(weak_ptr.get()); |
| 80 | } |
| 81 | |
| 82 | } // namespace |