OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/public/cpp/utility/run_loop.h" | 5 #include "mojo/public/cpp/utility/run_loop.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 28 matching lines...) Expand all Loading... |
39 void OnHandleError(const Handle& handle, MojoResult result) override { | 39 void OnHandleError(const Handle& handle, MojoResult result) override { |
40 error_count_++; | 40 error_count_++; |
41 last_error_result_ = result; | 41 last_error_result_ = result; |
42 } | 42 } |
43 | 43 |
44 private: | 44 private: |
45 int ready_count_; | 45 int ready_count_; |
46 int error_count_; | 46 int error_count_; |
47 MojoResult last_error_result_; | 47 MojoResult last_error_result_; |
48 | 48 |
49 MOJO_DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); | 49 DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); |
50 }; | 50 }; |
51 | 51 |
52 class RunLoopTest : public testing::Test { | 52 class RunLoopTest : public testing::Test { |
53 public: | 53 public: |
54 RunLoopTest() {} | 54 RunLoopTest() {} |
55 | 55 |
56 void SetUp() override { | 56 void SetUp() override { |
57 Test::SetUp(); | 57 Test::SetUp(); |
58 RunLoop::SetUp(); | 58 RunLoop::SetUp(); |
59 } | 59 } |
60 void TearDown() override { | 60 void TearDown() override { |
61 RunLoop::TearDown(); | 61 RunLoop::TearDown(); |
62 Test::TearDown(); | 62 Test::TearDown(); |
63 } | 63 } |
64 | 64 |
65 private: | 65 private: |
66 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | 66 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); |
67 }; | 67 }; |
68 | 68 |
69 // Trivial test to verify Run() with no added handles returns. | 69 // Trivial test to verify Run() with no added handles returns. |
70 TEST_F(RunLoopTest, ExitsWithNoHandles) { | 70 TEST_F(RunLoopTest, ExitsWithNoHandles) { |
71 RunLoop run_loop; | 71 RunLoop run_loop; |
72 run_loop.Run(); | 72 run_loop.Run(); |
73 } | 73 } |
74 | 74 |
75 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { | 75 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { |
76 public: | 76 public: |
77 RemoveOnReadyRunLoopHandler() : run_loop_(nullptr) {} | 77 RemoveOnReadyRunLoopHandler() : run_loop_(nullptr) {} |
78 ~RemoveOnReadyRunLoopHandler() override {} | 78 ~RemoveOnReadyRunLoopHandler() override {} |
79 | 79 |
80 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 80 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
81 | 81 |
82 // RunLoopHandler: | 82 // RunLoopHandler: |
83 void OnHandleReady(const Handle& handle) override { | 83 void OnHandleReady(const Handle& handle) override { |
84 run_loop_->RemoveHandler(handle); | 84 run_loop_->RemoveHandler(handle); |
85 TestRunLoopHandler::OnHandleReady(handle); | 85 TestRunLoopHandler::OnHandleReady(handle); |
86 } | 86 } |
87 | 87 |
88 private: | 88 private: |
89 RunLoop* run_loop_; | 89 RunLoop* run_loop_; |
90 | 90 |
91 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); | 91 DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); |
92 }; | 92 }; |
93 | 93 |
94 // Verifies RunLoop quits when no more handles (handle is removed when ready). | 94 // Verifies RunLoop quits when no more handles (handle is removed when ready). |
95 TEST_F(RunLoopTest, HandleReady) { | 95 TEST_F(RunLoopTest, HandleReady) { |
96 RemoveOnReadyRunLoopHandler handler; | 96 RemoveOnReadyRunLoopHandler handler; |
97 MessagePipe test_pipe; | 97 MessagePipe test_pipe; |
98 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | 98 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); |
99 | 99 |
100 RunLoop run_loop; | 100 RunLoop run_loop; |
101 handler.set_run_loop(&run_loop); | 101 handler.set_run_loop(&run_loop); |
(...skipping 14 matching lines...) Expand all Loading... |
116 | 116 |
117 // RunLoopHandler: | 117 // RunLoopHandler: |
118 void OnHandleReady(const Handle& handle) override { | 118 void OnHandleReady(const Handle& handle) override { |
119 run_loop_->Quit(); | 119 run_loop_->Quit(); |
120 TestRunLoopHandler::OnHandleReady(handle); | 120 TestRunLoopHandler::OnHandleReady(handle); |
121 } | 121 } |
122 | 122 |
123 private: | 123 private: |
124 RunLoop* run_loop_; | 124 RunLoop* run_loop_; |
125 | 125 |
126 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); | 126 DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); |
127 }; | 127 }; |
128 | 128 |
129 // Verifies Quit() from OnHandleReady() quits the loop. | 129 // Verifies Quit() from OnHandleReady() quits the loop. |
130 TEST_F(RunLoopTest, QuitFromReady) { | 130 TEST_F(RunLoopTest, QuitFromReady) { |
131 QuitOnReadyRunLoopHandler handler; | 131 QuitOnReadyRunLoopHandler handler; |
132 MessagePipe test_pipe; | 132 MessagePipe test_pipe; |
133 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | 133 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); |
134 | 134 |
135 RunLoop run_loop; | 135 RunLoop run_loop; |
136 handler.set_run_loop(&run_loop); | 136 handler.set_run_loop(&run_loop); |
(...skipping 14 matching lines...) Expand all Loading... |
151 | 151 |
152 // RunLoopHandler: | 152 // RunLoopHandler: |
153 void OnHandleError(const Handle& handle, MojoResult result) override { | 153 void OnHandleError(const Handle& handle, MojoResult result) override { |
154 run_loop_->Quit(); | 154 run_loop_->Quit(); |
155 TestRunLoopHandler::OnHandleError(handle, result); | 155 TestRunLoopHandler::OnHandleError(handle, result); |
156 } | 156 } |
157 | 157 |
158 private: | 158 private: |
159 RunLoop* run_loop_; | 159 RunLoop* run_loop_; |
160 | 160 |
161 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); | 161 DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); |
162 }; | 162 }; |
163 | 163 |
164 // Verifies Quit() when the deadline is reached works. | 164 // Verifies Quit() when the deadline is reached works. |
165 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) { | 165 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) { |
166 QuitOnErrorRunLoopHandler handler; | 166 QuitOnErrorRunLoopHandler handler; |
167 MessagePipe test_pipe; | 167 MessagePipe test_pipe; |
168 RunLoop run_loop; | 168 RunLoop run_loop; |
169 handler.set_run_loop(&run_loop); | 169 handler.set_run_loop(&run_loop); |
170 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | 170 run_loop.AddHandler(&handler, test_pipe.handle0.get(), |
171 MOJO_HANDLE_SIGNAL_READABLE, | 171 MOJO_HANDLE_SIGNAL_READABLE, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 void OnHandleError(const Handle& handle, MojoResult result) override { | 204 void OnHandleError(const Handle& handle, MojoResult result) override { |
205 for (size_t i = 0; i < handles_.size(); i++) | 205 for (size_t i = 0; i < handles_.size(); i++) |
206 run_loop_->RemoveHandler(handles_[i]); | 206 run_loop_->RemoveHandler(handles_[i]); |
207 TestRunLoopHandler::OnHandleError(handle, result); | 207 TestRunLoopHandler::OnHandleError(handle, result); |
208 } | 208 } |
209 | 209 |
210 private: | 210 private: |
211 std::vector<Handle> handles_; | 211 std::vector<Handle> handles_; |
212 RunLoop* run_loop_; | 212 RunLoop* run_loop_; |
213 | 213 |
214 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveManyRunLoopHandler); | 214 DISALLOW_COPY_AND_ASSIGN(RemoveManyRunLoopHandler); |
215 }; | 215 }; |
216 | 216 |
217 // Test that handlers are notified of loop destruction. | 217 // Test that handlers are notified of loop destruction. |
218 TEST_F(RunLoopTest, MultipleHandleDestruction) { | 218 TEST_F(RunLoopTest, MultipleHandleDestruction) { |
219 RemoveManyRunLoopHandler odd_handler; | 219 RemoveManyRunLoopHandler odd_handler; |
220 TestRunLoopHandler even_handler; | 220 TestRunLoopHandler even_handler; |
221 MessagePipe test_pipe1, test_pipe2, test_pipe3; | 221 MessagePipe test_pipe1, test_pipe2, test_pipe3; |
222 { | 222 { |
223 RunLoop run_loop; | 223 RunLoop run_loop; |
224 odd_handler.set_run_loop(&run_loop); | 224 odd_handler.set_run_loop(&run_loop); |
(...skipping 29 matching lines...) Expand all Loading... |
254 void OnHandleError(const Handle& handle, MojoResult result) override { | 254 void OnHandleError(const Handle& handle, MojoResult result) override { |
255 run_loop_->AddHandler(this, handle, | 255 run_loop_->AddHandler(this, handle, |
256 MOJO_HANDLE_SIGNAL_READABLE, | 256 MOJO_HANDLE_SIGNAL_READABLE, |
257 MOJO_DEADLINE_INDEFINITE); | 257 MOJO_DEADLINE_INDEFINITE); |
258 TestRunLoopHandler::OnHandleError(handle, result); | 258 TestRunLoopHandler::OnHandleError(handle, result); |
259 } | 259 } |
260 | 260 |
261 private: | 261 private: |
262 RunLoop* run_loop_; | 262 RunLoop* run_loop_; |
263 | 263 |
264 MOJO_DISALLOW_COPY_AND_ASSIGN(AddHandlerOnErrorHandler); | 264 DISALLOW_COPY_AND_ASSIGN(AddHandlerOnErrorHandler); |
265 }; | 265 }; |
266 | 266 |
267 TEST_F(RunLoopTest, AddHandlerOnError) { | 267 TEST_F(RunLoopTest, AddHandlerOnError) { |
268 AddHandlerOnErrorHandler handler; | 268 AddHandlerOnErrorHandler handler; |
269 MessagePipe test_pipe; | 269 MessagePipe test_pipe; |
270 { | 270 { |
271 RunLoop run_loop; | 271 RunLoop run_loop; |
272 handler.set_run_loop(&run_loop); | 272 handler.set_run_loop(&run_loop); |
273 run_loop.AddHandler(&handler, | 273 run_loop.AddHandler(&handler, |
274 test_pipe.handle0.get(), | 274 test_pipe.handle0.get(), |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 EXPECT_EQ(read_result, MOJO_RESULT_OK); | 349 EXPECT_EQ(read_result, MOJO_RESULT_OK); |
350 EXPECT_EQ(read_byte, kSignalMagic); | 350 EXPECT_EQ(read_byte, kSignalMagic); |
351 } | 351 } |
352 | 352 |
353 private: | 353 private: |
354 RunLoop* run_loop_; | 354 RunLoop* run_loop_; |
355 MessagePipe* pipe_; | 355 MessagePipe* pipe_; |
356 size_t depth_; | 356 size_t depth_; |
357 bool reached_depth_limit_; | 357 bool reached_depth_limit_; |
358 | 358 |
359 MOJO_DISALLOW_COPY_AND_ASSIGN(NestingRunLoopHandler); | 359 DISALLOW_COPY_AND_ASSIGN(NestingRunLoopHandler); |
360 }; | 360 }; |
361 | 361 |
362 const size_t NestingRunLoopHandler::kDepthLimit = 10; | 362 const size_t NestingRunLoopHandler::kDepthLimit = 10; |
363 const char NestingRunLoopHandler::kSignalMagic = 'X'; | 363 const char NestingRunLoopHandler::kSignalMagic = 'X'; |
364 | 364 |
365 TEST_F(RunLoopTest, NestedRun) { | 365 TEST_F(RunLoopTest, NestedRun) { |
366 NestingRunLoopHandler handler; | 366 NestingRunLoopHandler handler; |
367 MessagePipe test_pipe; | 367 MessagePipe test_pipe; |
368 RunLoop run_loop; | 368 RunLoop run_loop; |
369 handler.set_run_loop(&run_loop); | 369 handler.set_run_loop(&run_loop); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 run_loop.AddHandler(&handler, | 419 run_loop.AddHandler(&handler, |
420 test_pipe.handle0.get(), | 420 test_pipe.handle0.get(), |
421 MOJO_HANDLE_SIGNAL_READABLE, | 421 MOJO_HANDLE_SIGNAL_READABLE, |
422 MOJO_DEADLINE_INDEFINITE); | 422 MOJO_DEADLINE_INDEFINITE); |
423 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); | 423 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); |
424 run_loop.Run(); | 424 run_loop.Run(); |
425 } | 425 } |
426 | 426 |
427 } // namespace | 427 } // namespace |
428 } // namespace mojo | 428 } // namespace mojo |
OLD | NEW |