Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
|
mmenke
2014/03/12 16:03:46
2014?
Elly Fong-Jones
2014/03/12 16:58:47
Done.
| |
| 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 "chrome/renderer/net/mockable_one_shot_timer.h" | |
| 6 | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
|
mmenke
2014/03/12 16:03:46
include bind.h?
Elly Fong-Jones
2014/03/12 16:58:47
Done.
| |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> { | |
| 14 public: | |
| 15 HasWeakPtr() {} | |
| 16 virtual ~HasWeakPtr() {} | |
| 17 private: | |
| 18 DISALLOW_COPY_AND_ASSIGN(HasWeakPtr); | |
|
mmenke
2014/03/12 16:03:46
Include macros.h for this.
Elly Fong-Jones
2014/03/12 16:58:47
Done.
| |
| 19 }; | |
| 20 | |
| 21 void IncrementCounter(int *counter) { | |
| 22 (*counter)++; | |
| 23 } | |
| 24 | |
| 25 // Constructs a WeakPtr to the supplied HasWeakPtr to ensure that the supplied | |
| 26 // HasWeakPtr (which is owned by the Closure RestartTimer is being called from) | |
| 27 // is not destroyed by timer->Start(). | |
| 28 void RestartTimer(int* counter, HasWeakPtr* has_weak_ptr, | |
| 29 MockableOneShotTimer* timer) { | |
| 30 base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr()); | |
| 31 (*counter)++; | |
| 32 timer->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 33 base::Bind(&IncrementCounter, | |
| 34 base::Unretained(counter))); | |
| 35 ASSERT_TRUE(weak_ptr.get()); | |
| 36 } | |
| 37 | |
| 38 TEST(MockableOneShotTimerTest, TimerRuns) { | |
|
mmenke
2014/03/12 16:03:46
May want a timer stops, just like this, but have a
Elly Fong-Jones
2014/03/12 16:58:47
Done.
| |
| 39 base::MessageLoop loop; | |
| 40 int calls = 0; | |
| 41 MockableOneShotTimer timer; | |
| 42 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 43 base::Bind(&IncrementCounter, | |
| 44 base::Unretained(&calls))); | |
| 45 base::MessageLoop::current()->RunUntilIdle(); | |
| 46 EXPECT_EQ(1, calls); | |
| 47 } | |
| 48 | |
| 49 // Test that the Closure contained in the MockableOneShotTimer is not destroyed | |
| 50 // by another Start() from inside the closure callback (i.e., that the lifetime | |
| 51 // of the Closure is longer than the lifetime of the actual callback). | |
| 52 TEST(MockableOneShotTimerTest, StartFromClosure) { | |
| 53 base::MessageLoop loop; | |
| 54 int calls = 0; | |
| 55 MockableOneShotTimer timer; | |
| 56 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 57 base::Bind(&RestartTimer, | |
| 58 base::Unretained(&calls), | |
| 59 base::Owned(new HasWeakPtr()), | |
| 60 base::Unretained(&timer))); | |
| 61 base::MessageLoop::current()->RunUntilIdle(); | |
| 62 // both RestartTimer() and IncrementCounter() must have run. | |
|
mmenke
2014/03/12 16:03:46
nit: both -> Both
Elly Fong-Jones
2014/03/12 16:58:47
Done.
| |
| 63 EXPECT_EQ(2, calls); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| OLD | NEW |