OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 void ShouldNotRunTask() { | 29 void ShouldNotRunTask() { |
30 ADD_FAILURE() << "Ran a task that shouldn't run."; | 30 ADD_FAILURE() << "Ran a task that shouldn't run."; |
31 } | 31 } |
32 | 32 |
33 void RunNestedLoopTask(int* counter) { | 33 void RunNestedLoopTask(int* counter) { |
34 RunLoop nested_run_loop; | 34 RunLoop nested_run_loop; |
35 | 35 |
36 // This task should quit |nested_run_loop| but not the main RunLoop. | 36 // This task should quit |nested_run_loop| but not the main RunLoop. |
37 ThreadTaskRunnerHandle::Get()->PostTask( | 37 ThreadTaskRunnerHandle::Get()->PostTask( |
38 FROM_HERE, Bind(&QuitWhenIdleTask, Unretained(&nested_run_loop), | 38 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&nested_run_loop), |
39 Unretained(counter))); | 39 Unretained(counter))); |
40 | 40 |
41 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 41 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
42 FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1)); | 42 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1)); |
43 | 43 |
44 MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current()); | 44 MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current()); |
45 nested_run_loop.Run(); | 45 nested_run_loop.Run(); |
46 | 46 |
47 ++(*counter); | 47 ++(*counter); |
48 } | 48 } |
49 | 49 |
50 class RunLoopTest : public testing::Test { | 50 class RunLoopTest : public testing::Test { |
51 protected: | 51 protected: |
52 RunLoopTest() = default; | 52 RunLoopTest() = default; |
53 | 53 |
54 MessageLoop message_loop_; | 54 MessageLoop message_loop_; |
55 RunLoop run_loop_; | 55 RunLoop run_loop_; |
56 int counter_ = 0; | 56 int counter_ = 0; |
57 | 57 |
58 private: | 58 private: |
59 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | 59 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); |
60 }; | 60 }; |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 TEST_F(RunLoopTest, QuitWhenIdle) { | 64 TEST_F(RunLoopTest, QuitWhenIdle) { |
65 message_loop_.task_runner()->PostTask( | 65 message_loop_.task_runner()->PostTask( |
66 FROM_HERE, | 66 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_), |
67 Bind(&QuitWhenIdleTask, Unretained(&run_loop_), Unretained(&counter_))); | 67 Unretained(&counter_))); |
68 message_loop_.task_runner()->PostTask( | 68 message_loop_.task_runner()->PostTask( |
69 FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_))); | 69 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_))); |
70 message_loop_.task_runner()->PostDelayedTask( | 70 message_loop_.task_runner()->PostDelayedTask( |
71 FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1)); | 71 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1)); |
72 | 72 |
73 run_loop_.Run(); | 73 run_loop_.Run(); |
74 EXPECT_EQ(2, counter_); | 74 EXPECT_EQ(2, counter_); |
75 } | 75 } |
76 | 76 |
77 TEST_F(RunLoopTest, QuitWhenIdleNestedLoop) { | 77 TEST_F(RunLoopTest, QuitWhenIdleNestedLoop) { |
78 message_loop_.task_runner()->PostTask( | 78 message_loop_.task_runner()->PostTask( |
79 FROM_HERE, Bind(&RunNestedLoopTask, Unretained(&counter_))); | 79 FROM_HERE, BindOnce(&RunNestedLoopTask, Unretained(&counter_))); |
80 message_loop_.task_runner()->PostTask( | 80 message_loop_.task_runner()->PostTask( |
81 FROM_HERE, | 81 FROM_HERE, BindOnce(&QuitWhenIdleTask, Unretained(&run_loop_), |
82 Bind(&QuitWhenIdleTask, Unretained(&run_loop_), Unretained(&counter_))); | 82 Unretained(&counter_))); |
83 message_loop_.task_runner()->PostTask( | 83 message_loop_.task_runner()->PostTask( |
84 FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_))); | 84 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_))); |
85 message_loop_.task_runner()->PostDelayedTask( | 85 message_loop_.task_runner()->PostDelayedTask( |
86 FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1)); | 86 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1)); |
87 | 87 |
88 run_loop_.Run(); | 88 run_loop_.Run(); |
89 EXPECT_EQ(4, counter_); | 89 EXPECT_EQ(4, counter_); |
90 } | 90 } |
91 | 91 |
92 TEST_F(RunLoopTest, QuitWhenIdleClosure) { | 92 TEST_F(RunLoopTest, QuitWhenIdleClosure) { |
93 message_loop_.task_runner()->PostTask(FROM_HERE, | 93 message_loop_.task_runner()->PostTask(FROM_HERE, |
94 run_loop_.QuitWhenIdleClosure()); | 94 run_loop_.QuitWhenIdleClosure()); |
95 message_loop_.task_runner()->PostTask( | 95 message_loop_.task_runner()->PostTask( |
96 FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_))); | 96 FROM_HERE, BindOnce(&ShouldRunTask, Unretained(&counter_))); |
97 message_loop_.task_runner()->PostDelayedTask( | 97 message_loop_.task_runner()->PostDelayedTask( |
98 FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1)); | 98 FROM_HERE, BindOnce(&ShouldNotRunTask), TimeDelta::FromDays(1)); |
99 | 99 |
100 run_loop_.Run(); | 100 run_loop_.Run(); |
101 EXPECT_EQ(1, counter_); | 101 EXPECT_EQ(1, counter_); |
102 } | 102 } |
103 | 103 |
104 // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been | 104 // Verify that the QuitWhenIdleClosure() can run after the RunLoop has been |
105 // deleted. It should have no effect. | 105 // deleted. It should have no effect. |
106 TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) { | 106 TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) { |
107 Closure quit_when_idle_closure; | 107 Closure quit_when_idle_closure; |
108 { | 108 { |
109 RunLoop run_loop; | 109 RunLoop run_loop; |
110 quit_when_idle_closure = run_loop.QuitWhenIdleClosure(); | 110 quit_when_idle_closure = run_loop.QuitWhenIdleClosure(); |
111 run_loop.RunUntilIdle(); | 111 run_loop.RunUntilIdle(); |
112 } | 112 } |
113 quit_when_idle_closure.Run(); | 113 quit_when_idle_closure.Run(); |
114 } | 114 } |
115 | 115 |
116 } // namespace base | 116 } // namespace base |
OLD | NEW |