Chromium Code Reviews| Index: chrome/renderer/net/mockable_one_shot_timer_unittest.cc |
| diff --git a/chrome/renderer/net/mockable_one_shot_timer_unittest.cc b/chrome/renderer/net/mockable_one_shot_timer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bd383931d350910e1d8b23a702d9014c4d860a2f |
| --- /dev/null |
| +++ b/chrome/renderer/net/mockable_one_shot_timer_unittest.cc |
| @@ -0,0 +1,66 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/net/mockable_one_shot_timer.h" |
| + |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#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.
|
| + |
| +namespace { |
| + |
| +class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> { |
| + public: |
| + HasWeakPtr() {} |
| + virtual ~HasWeakPtr() {} |
| + private: |
| + 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.
|
| +}; |
| + |
| +void IncrementCounter(int *counter) { |
| + (*counter)++; |
| +} |
| + |
| +// Constructs a WeakPtr to the supplied HasWeakPtr to ensure that the supplied |
| +// HasWeakPtr (which is owned by the Closure RestartTimer is being called from) |
| +// is not destroyed by timer->Start(). |
| +void RestartTimer(int* counter, HasWeakPtr* has_weak_ptr, |
| + MockableOneShotTimer* timer) { |
| + base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr()); |
| + (*counter)++; |
| + timer->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), |
| + base::Bind(&IncrementCounter, |
| + base::Unretained(counter))); |
| + ASSERT_TRUE(weak_ptr.get()); |
| +} |
| + |
| +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.
|
| + base::MessageLoop loop; |
| + int calls = 0; |
| + MockableOneShotTimer timer; |
| + timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), |
| + base::Bind(&IncrementCounter, |
| + base::Unretained(&calls))); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_EQ(1, calls); |
| +} |
| + |
| +// Test that the Closure contained in the MockableOneShotTimer is not destroyed |
| +// by another Start() from inside the closure callback (i.e., that the lifetime |
| +// of the Closure is longer than the lifetime of the actual callback). |
| +TEST(MockableOneShotTimerTest, StartFromClosure) { |
| + base::MessageLoop loop; |
| + int calls = 0; |
| + MockableOneShotTimer timer; |
| + timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), |
| + base::Bind(&RestartTimer, |
| + base::Unretained(&calls), |
| + base::Owned(new HasWeakPtr()), |
| + base::Unretained(&timer))); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + // 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.
|
| + EXPECT_EQ(2, calls); |
| +} |
| + |
| +} // namespace |