| 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..5801c7f1e95d2405dbea420350b50a630b9de18e
|
| --- /dev/null
|
| +++ b/chrome/renderer/net/mockable_one_shot_timer_unittest.cc
|
| @@ -0,0 +1,81 @@
|
| +// Copyright 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 "chrome/renderer/net/mockable_one_shot_timer.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/weak_ptr.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> {
|
| + public:
|
| + HasWeakPtr() {}
|
| + virtual ~HasWeakPtr() {}
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(HasWeakPtr);
|
| +};
|
| +
|
| +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) {
|
| + 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(MockableOneShotTimerTest, TimerStops) {
|
| + base::MessageLoop loop;
|
| + int calls = 0;
|
| + MockableOneShotTimer timer;
|
| + timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0),
|
| + base::Bind(&IncrementCounter,
|
| + base::Unretained(&calls)));
|
| + timer.Stop();
|
| + base::MessageLoop::current()->RunUntilIdle();
|
| + EXPECT_EQ(0, 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.
|
| + EXPECT_EQ(2, calls);
|
| +}
|
| +
|
| +} // namespace
|
|
|