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