Properly attach InfoBarContainer when it is swapped to a new WebContents
[chromium-blink-merge.git] / base / timer / mock_timer_unittest.cc
blobf6b6953648a0abed87fa888e631487b03336434c
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"
9 namespace {
11 void CallMeMaybe(int *number) {
12 (*number)++;
15 TEST(MockTimerTest, FiresOnce) {
16 int calls = 0;
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());
24 timer.Fire();
25 EXPECT_FALSE(timer.IsRunning());
26 EXPECT_EQ(1, calls);
29 TEST(MockTimerTest, FiresRepeatedly) {
30 int calls = 0;
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)));
36 timer.Fire();
37 EXPECT_TRUE(timer.IsRunning());
38 timer.Fire();
39 timer.Fire();
40 EXPECT_TRUE(timer.IsRunning());
41 EXPECT_EQ(3, calls);
44 TEST(MockTimerTest, Stops) {
45 int calls = 0;
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());
52 timer.Stop();
53 EXPECT_FALSE(timer.IsRunning());
56 class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> {
57 public:
58 HasWeakPtr() {}
59 virtual ~HasWeakPtr() {}
61 private:
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());
78 timer.Fire();
79 ASSERT_FALSE(weak_ptr.get());
82 } // namespace