Chromium Code Reviews| Index: base/timer/mock_timer_unittest.cc |
| diff --git a/base/timer/mock_timer_unittest.cc b/base/timer/mock_timer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca8d97dd8ec0b3d243fb36e709b21bcb7b931809 |
| --- /dev/null |
| +++ b/base/timer/mock_timer_unittest.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/timer/mock_timer.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +void CallMeMaybe(int *number) { |
|
Mark Mentovai
2014/03/13 15:17:34
:)
|
| + (*number)++; |
| +} |
| + |
| +TEST(MockTimerTest, FiresOnce) { |
| + int calls; |
| + base::MockTimer timer(false, false); |
| + base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&CallMeMaybe, |
| + base::Unretained(&calls))); |
| + EXPECT_EQ(delay, timer.GetCurrentDelay()); |
| + EXPECT_TRUE(timer.IsRunning()); |
| + timer.Fire(); |
| + EXPECT_FALSE(timer.IsRunning()); |
| + EXPECT_EQ(1, calls); |
| +} |
| + |
| +TEST(MockTimerTest, FiresRepeatedly) { |
| + int calls; |
| + base::MockTimer timer(true, true); |
| + base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&CallMeMaybe, |
| + base::Unretained(&calls))); |
| + timer.Fire(); |
| + EXPECT_TRUE(timer.IsRunning()); |
| + timer.Fire(); |
| + timer.Fire(); |
| + EXPECT_TRUE(timer.IsRunning()); |
| + EXPECT_EQ(3, calls); |
| +} |
| + |
| +TEST(MockTimerTest, Stops) { |
| + int calls; |
| + base::MockTimer timer(true, true); |
| + base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&CallMeMaybe, |
| + base::Unretained(&calls))); |
| + EXPECT_TRUE(timer.IsRunning()); |
| + timer.Stop(); |
| + EXPECT_FALSE(timer.IsRunning()); |
| +} |
| + |
| +class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> { |
| + public: |
| + HasWeakPtr() {} |
| + virtual ~HasWeakPtr() {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(HasWeakPtr); |
| +}; |
| + |
| +void DoNothingWithWeakPtr(HasWeakPtr* has_weak_ptr) { |
| +} |
| + |
| +TEST(MockTimerTest, DoesNotRetainClosure) { |
| + HasWeakPtr *has_weak_ptr = new HasWeakPtr(); |
| + base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr()); |
| + base::MockTimer timer(false, false); |
| + base::TimeDelta delay = base::TimeDelta::FromSeconds(2); |
| + ASSERT_TRUE(weak_ptr.get()); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&DoNothingWithWeakPtr, |
| + base::Owned(has_weak_ptr))); |
| + ASSERT_TRUE(weak_ptr.get()); |
| + timer.Fire(); |
| + ASSERT_FALSE(weak_ptr.get()); |
| +} |
| + |
| +} // namespace |