Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: chrome/renderer/net/mockable_one_shot_timer_unittest.cc

Issue 136203009: Support auto-reload on errors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Rebase? Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/renderer/net/mockable_one_shot_timer.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 class HasWeakPtr : public base::SupportsWeakPtr<HasWeakPtr> {
16 public:
17 HasWeakPtr() {}
18 virtual ~HasWeakPtr() {}
19 private:
20 DISALLOW_COPY_AND_ASSIGN(HasWeakPtr);
21 };
22
23 void IncrementCounter(int *counter) {
24 (*counter)++;
25 }
26
27 // Constructs a WeakPtr to the supplied HasWeakPtr to ensure that the supplied
28 // HasWeakPtr (which is owned by the Closure RestartTimer is being called from)
29 // is not destroyed by timer->Start().
30 void RestartTimer(int* counter, HasWeakPtr* has_weak_ptr,
31 MockableOneShotTimer* timer) {
32 base::WeakPtr<HasWeakPtr> weak_ptr(has_weak_ptr->AsWeakPtr());
33 (*counter)++;
34 timer->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0),
35 base::Bind(&IncrementCounter,
36 base::Unretained(counter)));
37 ASSERT_TRUE(weak_ptr.get());
38 }
39
40 TEST(MockableOneShotTimerTest, TimerRuns) {
41 base::MessageLoop loop;
42 int calls = 0;
43 MockableOneShotTimer timer;
44 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0),
45 base::Bind(&IncrementCounter,
46 base::Unretained(&calls)));
47 base::MessageLoop::current()->RunUntilIdle();
48 EXPECT_EQ(1, calls);
49 }
50
51
52 TEST(MockableOneShotTimerTest, TimerStops) {
53 base::MessageLoop loop;
54 int calls = 0;
55 MockableOneShotTimer timer;
56 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0),
57 base::Bind(&IncrementCounter,
58 base::Unretained(&calls)));
59 timer.Stop();
60 base::MessageLoop::current()->RunUntilIdle();
61 EXPECT_EQ(0, calls);
62 }
63
64 // Test that the Closure contained in the MockableOneShotTimer is not destroyed
65 // by another Start() from inside the closure callback (i.e., that the lifetime
66 // of the Closure is longer than the lifetime of the actual callback).
67 TEST(MockableOneShotTimerTest, StartFromClosure) {
68 base::MessageLoop loop;
69 int calls = 0;
70 MockableOneShotTimer timer;
71 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0),
72 base::Bind(&RestartTimer,
73 base::Unretained(&calls),
74 base::Owned(new HasWeakPtr()),
75 base::Unretained(&timer)));
76 base::MessageLoop::current()->RunUntilIdle();
77 // Both RestartTimer() and IncrementCounter() must have run.
78 EXPECT_EQ(2, calls);
79 }
80
81 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698