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.
5 #include "base/timer/mock_timer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 void CallMeMaybe(int *number
) {
15 TEST(MockTimerTest
, FiresOnce
) {
17 base::MockTimer
timer(false, false);
18 base::TimeDelta delay
= base::TimeDelta::FromSeconds(2);
19 timer
.Start(FROM_HERE
, delay
,
20 base::Bind(&CallMeMaybe
,
21 base::Unretained(&calls
)));
22 EXPECT_EQ(delay
, timer
.GetCurrentDelay());
23 EXPECT_TRUE(timer
.IsRunning());
25 EXPECT_FALSE(timer
.IsRunning());
29 TEST(MockTimerTest
, FiresRepeatedly
) {
31 base::MockTimer
timer(true, true);
32 base::TimeDelta delay
= base::TimeDelta::FromSeconds(2);
33 timer
.Start(FROM_HERE
, delay
,
34 base::Bind(&CallMeMaybe
,
35 base::Unretained(&calls
)));
37 EXPECT_TRUE(timer
.IsRunning());
40 EXPECT_TRUE(timer
.IsRunning());
44 TEST(MockTimerTest
, Stops
) {
46 base::MockTimer
timer(true, true);
47 base::TimeDelta delay
= base::TimeDelta::FromSeconds(2);
48 timer
.Start(FROM_HERE
, delay
,
49 base::Bind(&CallMeMaybe
,
50 base::Unretained(&calls
)));
51 EXPECT_TRUE(timer
.IsRunning());
53 EXPECT_FALSE(timer
.IsRunning());
56 class HasWeakPtr
: public base::SupportsWeakPtr
<HasWeakPtr
> {
59 virtual ~HasWeakPtr() {}
62 DISALLOW_COPY_AND_ASSIGN(HasWeakPtr
);
65 void DoNothingWithWeakPtr(HasWeakPtr
* has_weak_ptr
) {
68 TEST(MockTimerTest
, DoesNotRetainClosure
) {
69 HasWeakPtr
*has_weak_ptr
= new HasWeakPtr();
70 base::WeakPtr
<HasWeakPtr
> weak_ptr(has_weak_ptr
->AsWeakPtr());
71 base::MockTimer
timer(false, false);
72 base::TimeDelta delay
= base::TimeDelta::FromSeconds(2);
73 ASSERT_TRUE(weak_ptr
.get());
74 timer
.Start(FROM_HERE
, delay
,
75 base::Bind(&DoNothingWithWeakPtr
,
76 base::Owned(has_weak_ptr
)));
77 ASSERT_TRUE(weak_ptr
.get());
79 ASSERT_FALSE(weak_ptr
.get());