Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/message_loop/message_loop.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 void BumpCounter(int *counter) { | |
| 13 (*counter)++; | |
| 14 } | |
| 15 | |
| 16 } // namespace | |
|
mmenke
2014/03/11 20:07:54
Can't this entire file be put in the anonymous nam
Elly Fong-Jones
2014/03/12 15:08:58
Done.
| |
| 17 | |
| 18 TEST(MockableOneShotTimerTest, TimerRuns) { | |
| 19 base::MessageLoop loop; | |
| 20 int calls = 0; | |
| 21 MockableOneShotTimer timer; | |
| 22 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 23 base::Bind(&BumpCounter, | |
| 24 base::Unretained(&calls))); | |
| 25 base::MessageLoop::current()->RunUntilIdle(); | |
| 26 EXPECT_EQ(1, calls); | |
| 27 } | |
| 28 | |
| 29 class Incrementer { | |
|
mmenke
2014/03/11 20:07:54
I think this is very hard to follow. How about so
mmenke
2014/03/12 13:19:42
Could actually get away with just using a WeakFact
Elly Fong-Jones
2014/03/12 15:08:58
Done.
Elly Fong-Jones
2014/03/12 15:08:58
Done.
| |
| 30 public: | |
| 31 Incrementer(int* counter) : counter_(counter) {} | |
|
mmenke
2014/03/11 20:07:54
explicit
Elly Fong-Jones
2014/03/12 15:08:58
Deleted.
| |
| 32 ~Incrementer() { (*counter_)++; } | |
| 33 void set_counter(int* counter) { counter_ = counter; } | |
| 34 private: | |
|
mmenke
2014/03/11 20:07:54
Blank line before private.
Elly Fong-Jones
2014/03/12 15:08:58
Deleted.
| |
| 35 int* counter_; | |
|
mmenke
2014/03/11 20:07:54
DISALLOW_COPY_AND_ASSIGN
Elly Fong-Jones
2014/03/12 15:08:58
Deleted.
| |
| 36 }; | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 void AllDone(int* counter) { | |
|
mmenke
2014/03/11 20:07:54
Do we really need both this and BumpCounter?
Elly Fong-Jones
2014/03/12 15:08:58
Deleted.
| |
| 41 (*counter)++; | |
| 42 } | |
| 43 | |
| 44 void RestartTimer(int* counter, int* new_counter, Incrementer* incrementer, | |
| 45 MockableOneShotTimer* timer) { | |
| 46 (*counter)++; | |
| 47 timer->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 48 base::Bind(&AllDone, | |
| 49 base::Unretained(counter))); | |
| 50 incrementer->set_counter(new_counter); | |
| 51 }; | |
| 52 | |
| 53 } | |
| 54 | |
| 55 // Test that MockableOneShotTimer does not destroy the callback while the | |
| 56 // callback is still being run. Create a closure that owns an Incrementer (see | |
| 57 // above) and keep track of whether the Incrementer is destroyed "early" while | |
| 58 // the callback is running or not. Also, ensure that the timer executes both | |
| 59 // RestartTimer() and AllDone(). | |
| 60 TEST(MockableOneShotTimerTest, NoDestructionAtStart) { | |
| 61 base::MessageLoop loop; | |
| 62 int early_destructions = 0; | |
| 63 int destructions = 0; | |
| 64 int calls = 0; | |
| 65 MockableOneShotTimer timer; | |
| 66 // If |incrementer| is destroyed, it will increment |early_destructions|; | |
| 67 // |RestartTimer| is responsible for swapping its destruction counter from | |
| 68 // |early_destructions| to |destructions|. | |
| 69 Incrementer* incrementer = new Incrementer(&early_destructions); | |
| 70 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(0), | |
| 71 base::Bind(&RestartTimer, | |
| 72 base::Unretained(&calls), | |
| 73 base::Unretained(&destructions), | |
| 74 base::Owned(incrementer), | |
| 75 base::Unretained(&timer))); | |
| 76 base::MessageLoop::current()->RunUntilIdle(); | |
| 77 // Both |AllDone| and |RestartTimer| must have run. | |
| 78 EXPECT_EQ(2, calls); | |
| 79 EXPECT_EQ(0, early_destructions); | |
| 80 EXPECT_EQ(1, destructions); | |
| 81 } | |
| OLD | NEW |