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 <string> | 7 #include <string> |
8 | 8 |
9 #include "mojo/public/cpp/system/core.h" | 9 #include "mojo/public/cpp/system/core.h" |
10 #include "mojo/public/cpp/test_support/test_utils.h" | 10 #include "mojo/public/cpp/test_support/test_utils.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 }; | 64 }; |
65 | 65 |
66 // Trivial test to verify Run() with no added handles returns. | 66 // Trivial test to verify Run() with no added handles returns. |
67 TEST_F(RunLoopTest, ExitsWithNoHandles) { | 67 TEST_F(RunLoopTest, ExitsWithNoHandles) { |
68 RunLoop run_loop; | 68 RunLoop run_loop; |
69 run_loop.Run(); | 69 run_loop.Run(); |
70 } | 70 } |
71 | 71 |
72 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { | 72 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { |
73 public: | 73 public: |
74 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { | 74 RemoveOnReadyRunLoopHandler() : run_loop_(nullptr) {} |
75 } | |
76 ~RemoveOnReadyRunLoopHandler() override {} | 75 ~RemoveOnReadyRunLoopHandler() override {} |
77 | 76 |
78 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 77 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
79 | 78 |
80 // RunLoopHandler: | 79 // RunLoopHandler: |
81 void OnHandleReady(const Handle& handle) override { | 80 void OnHandleReady(const Handle& handle) override { |
82 run_loop_->RemoveHandler(handle); | 81 run_loop_->RemoveHandler(handle); |
83 TestRunLoopHandler::OnHandleReady(handle); | 82 TestRunLoopHandler::OnHandleReady(handle); |
84 } | 83 } |
85 | 84 |
(...skipping 14 matching lines...) Expand all Loading... |
100 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | 99 run_loop.AddHandler(&handler, test_pipe.handle0.get(), |
101 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE); | 100 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE); |
102 run_loop.Run(); | 101 run_loop.Run(); |
103 EXPECT_EQ(1, handler.ready_count()); | 102 EXPECT_EQ(1, handler.ready_count()); |
104 EXPECT_EQ(0, handler.error_count()); | 103 EXPECT_EQ(0, handler.error_count()); |
105 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); | 104 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); |
106 } | 105 } |
107 | 106 |
108 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { | 107 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { |
109 public: | 108 public: |
110 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { | 109 QuitOnReadyRunLoopHandler() : run_loop_(nullptr) {} |
111 } | |
112 ~QuitOnReadyRunLoopHandler() override {} | 110 ~QuitOnReadyRunLoopHandler() override {} |
113 | 111 |
114 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 112 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
115 | 113 |
116 // RunLoopHandler: | 114 // RunLoopHandler: |
117 void OnHandleReady(const Handle& handle) override { | 115 void OnHandleReady(const Handle& handle) override { |
118 run_loop_->Quit(); | 116 run_loop_->Quit(); |
119 TestRunLoopHandler::OnHandleReady(handle); | 117 TestRunLoopHandler::OnHandleReady(handle); |
120 } | 118 } |
121 | 119 |
(...skipping 14 matching lines...) Expand all Loading... |
136 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | 134 run_loop.AddHandler(&handler, test_pipe.handle0.get(), |
137 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE); | 135 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE); |
138 run_loop.Run(); | 136 run_loop.Run(); |
139 EXPECT_EQ(1, handler.ready_count()); | 137 EXPECT_EQ(1, handler.ready_count()); |
140 EXPECT_EQ(0, handler.error_count()); | 138 EXPECT_EQ(0, handler.error_count()); |
141 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get())); | 139 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get())); |
142 } | 140 } |
143 | 141 |
144 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { | 142 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { |
145 public: | 143 public: |
146 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { | 144 QuitOnErrorRunLoopHandler() : run_loop_(nullptr) {} |
147 } | |
148 ~QuitOnErrorRunLoopHandler() override {} | 145 ~QuitOnErrorRunLoopHandler() override {} |
149 | 146 |
150 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 147 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
151 | 148 |
152 // RunLoopHandler: | 149 // RunLoopHandler: |
153 void OnHandleError(const Handle& handle, MojoResult result) override { | 150 void OnHandleError(const Handle& handle, MojoResult result) override { |
154 run_loop_->Quit(); | 151 run_loop_->Quit(); |
155 TestRunLoopHandler::OnHandleError(handle, result); | 152 TestRunLoopHandler::OnHandleError(handle, result); |
156 } | 153 } |
157 | 154 |
(...skipping 29 matching lines...) Expand all Loading... |
187 test_pipe.handle0.get(), | 184 test_pipe.handle0.get(), |
188 MOJO_HANDLE_SIGNAL_READABLE, | 185 MOJO_HANDLE_SIGNAL_READABLE, |
189 MOJO_DEADLINE_INDEFINITE); | 186 MOJO_DEADLINE_INDEFINITE); |
190 } | 187 } |
191 EXPECT_EQ(1, handler.error_count()); | 188 EXPECT_EQ(1, handler.error_count()); |
192 EXPECT_EQ(MOJO_RESULT_ABORTED, handler.last_error_result()); | 189 EXPECT_EQ(MOJO_RESULT_ABORTED, handler.last_error_result()); |
193 } | 190 } |
194 | 191 |
195 class RemoveManyRunLoopHandler : public TestRunLoopHandler { | 192 class RemoveManyRunLoopHandler : public TestRunLoopHandler { |
196 public: | 193 public: |
197 RemoveManyRunLoopHandler() : run_loop_(NULL) { | 194 RemoveManyRunLoopHandler() : run_loop_(nullptr) {} |
198 } | |
199 ~RemoveManyRunLoopHandler() override {} | 195 ~RemoveManyRunLoopHandler() override {} |
200 | 196 |
201 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 197 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
202 void add_handle(const Handle& handle) { handles_.push_back(handle); } | 198 void add_handle(const Handle& handle) { handles_.push_back(handle); } |
203 | 199 |
204 // RunLoopHandler: | 200 // RunLoopHandler: |
205 void OnHandleError(const Handle& handle, MojoResult result) override { | 201 void OnHandleError(const Handle& handle, MojoResult result) override { |
206 for (size_t i = 0; i < handles_.size(); i++) | 202 for (size_t i = 0; i < handles_.size(); i++) |
207 run_loop_->RemoveHandler(handles_[i]); | 203 run_loop_->RemoveHandler(handles_[i]); |
208 TestRunLoopHandler::OnHandleError(handle, result); | 204 TestRunLoopHandler::OnHandleError(handle, result); |
(...skipping 30 matching lines...) Expand all Loading... |
239 MOJO_DEADLINE_INDEFINITE); | 235 MOJO_DEADLINE_INDEFINITE); |
240 } | 236 } |
241 EXPECT_EQ(1, odd_handler.error_count()); | 237 EXPECT_EQ(1, odd_handler.error_count()); |
242 EXPECT_EQ(1, even_handler.error_count()); | 238 EXPECT_EQ(1, even_handler.error_count()); |
243 EXPECT_EQ(MOJO_RESULT_ABORTED, odd_handler.last_error_result()); | 239 EXPECT_EQ(MOJO_RESULT_ABORTED, odd_handler.last_error_result()); |
244 EXPECT_EQ(MOJO_RESULT_ABORTED, even_handler.last_error_result()); | 240 EXPECT_EQ(MOJO_RESULT_ABORTED, even_handler.last_error_result()); |
245 } | 241 } |
246 | 242 |
247 class AddHandlerOnErrorHandler : public TestRunLoopHandler { | 243 class AddHandlerOnErrorHandler : public TestRunLoopHandler { |
248 public: | 244 public: |
249 AddHandlerOnErrorHandler() : run_loop_(NULL) { | 245 AddHandlerOnErrorHandler() : run_loop_(nullptr) {} |
250 } | |
251 ~AddHandlerOnErrorHandler() override {} | 246 ~AddHandlerOnErrorHandler() override {} |
252 | 247 |
253 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 248 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
254 | 249 |
255 // RunLoopHandler: | 250 // RunLoopHandler: |
256 void OnHandleError(const Handle& handle, MojoResult result) override { | 251 void OnHandleError(const Handle& handle, MojoResult result) override { |
257 run_loop_->AddHandler(this, handle, | 252 run_loop_->AddHandler(this, handle, |
258 MOJO_HANDLE_SIGNAL_READABLE, | 253 MOJO_HANDLE_SIGNAL_READABLE, |
259 MOJO_DEADLINE_INDEFINITE); | 254 MOJO_DEADLINE_INDEFINITE); |
260 TestRunLoopHandler::OnHandleError(handle, result); | 255 TestRunLoopHandler::OnHandleError(handle, result); |
(...skipping 14 matching lines...) Expand all Loading... |
275 run_loop.AddHandler(&handler, | 270 run_loop.AddHandler(&handler, |
276 test_pipe.handle0.get(), | 271 test_pipe.handle0.get(), |
277 MOJO_HANDLE_SIGNAL_READABLE, | 272 MOJO_HANDLE_SIGNAL_READABLE, |
278 MOJO_DEADLINE_INDEFINITE); | 273 MOJO_DEADLINE_INDEFINITE); |
279 } | 274 } |
280 EXPECT_EQ(1, handler.error_count()); | 275 EXPECT_EQ(1, handler.error_count()); |
281 EXPECT_EQ(MOJO_RESULT_ABORTED, handler.last_error_result()); | 276 EXPECT_EQ(MOJO_RESULT_ABORTED, handler.last_error_result()); |
282 } | 277 } |
283 | 278 |
284 TEST_F(RunLoopTest, Current) { | 279 TEST_F(RunLoopTest, Current) { |
285 EXPECT_TRUE(RunLoop::current() == NULL); | 280 EXPECT_TRUE(RunLoop::current() == nullptr); |
286 { | 281 { |
287 RunLoop run_loop; | 282 RunLoop run_loop; |
288 EXPECT_EQ(&run_loop, RunLoop::current()); | 283 EXPECT_EQ(&run_loop, RunLoop::current()); |
289 } | 284 } |
290 EXPECT_TRUE(RunLoop::current() == NULL); | 285 EXPECT_TRUE(RunLoop::current() == nullptr); |
291 } | 286 } |
292 | 287 |
293 class NestingRunLoopHandler : public TestRunLoopHandler { | 288 class NestingRunLoopHandler : public TestRunLoopHandler { |
294 public: | 289 public: |
295 static const size_t kDepthLimit; | 290 static const size_t kDepthLimit; |
296 static const char kSignalMagic; | 291 static const char kSignalMagic; |
297 | 292 |
298 NestingRunLoopHandler() | 293 NestingRunLoopHandler() |
299 : run_loop_(NULL), | 294 : run_loop_(nullptr), |
300 pipe_(NULL), | 295 pipe_(nullptr), |
301 depth_(0), | 296 depth_(0), |
302 reached_depth_limit_(false) {} | 297 reached_depth_limit_(false) {} |
303 | 298 |
304 ~NestingRunLoopHandler() override {} | 299 ~NestingRunLoopHandler() override {} |
305 | 300 |
306 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 301 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
307 void set_pipe(MessagePipe* pipe) { pipe_ = pipe; } | 302 void set_pipe(MessagePipe* pipe) { pipe_ = pipe; } |
308 bool reached_depth_limit() const { return reached_depth_limit_; } | 303 bool reached_depth_limit() const { return reached_depth_limit_; } |
309 | 304 |
310 // RunLoopHandler: | 305 // RunLoopHandler: |
(...skipping 17 matching lines...) Expand all Loading... |
328 } else { | 323 } else { |
329 EXPECT_EQ(current_depth, kDepthLimit); | 324 EXPECT_EQ(current_depth, kDepthLimit); |
330 reached_depth_limit_ = true; | 325 reached_depth_limit_ = true; |
331 run_loop_->Quit(); | 326 run_loop_->Quit(); |
332 } | 327 } |
333 --depth_; | 328 --depth_; |
334 } | 329 } |
335 | 330 |
336 void WriteSignal() { | 331 void WriteSignal() { |
337 char write_byte = kSignalMagic; | 332 char write_byte = kSignalMagic; |
338 MojoResult write_result = WriteMessageRaw( | 333 MojoResult write_result = |
339 pipe_->handle1.get(), | 334 WriteMessageRaw(pipe_->handle1.get(), &write_byte, 1, nullptr, 0, |
340 &write_byte, 1, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 335 MOJO_WRITE_MESSAGE_FLAG_NONE); |
341 EXPECT_EQ(write_result, MOJO_RESULT_OK); | 336 EXPECT_EQ(write_result, MOJO_RESULT_OK); |
342 } | 337 } |
343 | 338 |
344 void ReadSignal() { | 339 void ReadSignal() { |
345 char read_byte = 0; | 340 char read_byte = 0; |
346 uint32_t bytes_read = 1; | 341 uint32_t bytes_read = 1; |
347 uint32_t handles_read = 0; | 342 uint32_t handles_read = 0; |
348 MojoResult read_result = ReadMessageRaw( | 343 MojoResult read_result = |
349 pipe_->handle0.get(), | 344 ReadMessageRaw(pipe_->handle0.get(), &read_byte, &bytes_read, nullptr, |
350 &read_byte, &bytes_read, NULL, &handles_read, | 345 &handles_read, MOJO_READ_MESSAGE_FLAG_NONE); |
351 MOJO_READ_MESSAGE_FLAG_NONE); | |
352 EXPECT_EQ(read_result, MOJO_RESULT_OK); | 346 EXPECT_EQ(read_result, MOJO_RESULT_OK); |
353 EXPECT_EQ(read_byte, kSignalMagic); | 347 EXPECT_EQ(read_byte, kSignalMagic); |
354 } | 348 } |
355 | 349 |
356 private: | 350 private: |
357 RunLoop* run_loop_; | 351 RunLoop* run_loop_; |
358 MessagePipe* pipe_; | 352 MessagePipe* pipe_; |
359 size_t depth_; | 353 size_t depth_; |
360 bool reached_depth_limit_; | 354 bool reached_depth_limit_; |
361 | 355 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 run_loop.AddHandler(&handler, | 416 run_loop.AddHandler(&handler, |
423 test_pipe.handle0.get(), | 417 test_pipe.handle0.get(), |
424 MOJO_HANDLE_SIGNAL_READABLE, | 418 MOJO_HANDLE_SIGNAL_READABLE, |
425 MOJO_DEADLINE_INDEFINITE); | 419 MOJO_DEADLINE_INDEFINITE); |
426 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); | 420 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); |
427 run_loop.Run(); | 421 run_loop.Run(); |
428 } | 422 } |
429 | 423 |
430 } // namespace | 424 } // namespace |
431 } // namespace mojo | 425 } // namespace mojo |
OLD | NEW |