| 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 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 void clear_ready_count() { ready_count_ = 0; } | 26 void clear_ready_count() { ready_count_ = 0; } |
| 27 int ready_count() const { return ready_count_; } | 27 int ready_count() const { return ready_count_; } |
| 28 | 28 |
| 29 void clear_error_count() { error_count_ = 0; } | 29 void clear_error_count() { error_count_ = 0; } |
| 30 int error_count() const { return error_count_; } | 30 int error_count() const { return error_count_; } |
| 31 | 31 |
| 32 MojoResult last_error_result() const { return last_error_result_; } | 32 MojoResult last_error_result() const { return last_error_result_; } |
| 33 | 33 |
| 34 // RunLoopHandler: | 34 // RunLoopHandler: |
| 35 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | 35 virtual void OnHandleReady(const Handle& handle) override { ready_count_++; } |
| 36 ready_count_++; | 36 virtual void OnHandleError(const Handle& handle, MojoResult result) override { |
| 37 } | |
| 38 virtual void OnHandleError(const Handle& handle, MojoResult result) | |
| 39 MOJO_OVERRIDE { | |
| 40 error_count_++; | 37 error_count_++; |
| 41 last_error_result_ = result; | 38 last_error_result_ = result; |
| 42 } | 39 } |
| 43 | 40 |
| 44 private: | 41 private: |
| 45 int ready_count_; | 42 int ready_count_; |
| 46 int error_count_; | 43 int error_count_; |
| 47 MojoResult last_error_result_; | 44 MojoResult last_error_result_; |
| 48 | 45 |
| 49 MOJO_DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); | 46 MOJO_DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); |
| 50 }; | 47 }; |
| 51 | 48 |
| 52 class RunLoopTest : public testing::Test { | 49 class RunLoopTest : public testing::Test { |
| 53 public: | 50 public: |
| 54 RunLoopTest() {} | 51 RunLoopTest() {} |
| 55 | 52 |
| 56 virtual void SetUp() MOJO_OVERRIDE { | 53 virtual void SetUp() override { |
| 57 Test::SetUp(); | 54 Test::SetUp(); |
| 58 RunLoop::SetUp(); | 55 RunLoop::SetUp(); |
| 59 } | 56 } |
| 60 virtual void TearDown() MOJO_OVERRIDE { | 57 virtual void TearDown() override { |
| 61 RunLoop::TearDown(); | 58 RunLoop::TearDown(); |
| 62 Test::TearDown(); | 59 Test::TearDown(); |
| 63 } | 60 } |
| 64 | 61 |
| 65 private: | 62 private: |
| 66 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | 63 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopTest); |
| 67 }; | 64 }; |
| 68 | 65 |
| 69 // Trivial test to verify Run() with no added handles returns. | 66 // Trivial test to verify Run() with no added handles returns. |
| 70 TEST_F(RunLoopTest, ExitsWithNoHandles) { | 67 TEST_F(RunLoopTest, ExitsWithNoHandles) { |
| 71 RunLoop run_loop; | 68 RunLoop run_loop; |
| 72 run_loop.Run(); | 69 run_loop.Run(); |
| 73 } | 70 } |
| 74 | 71 |
| 75 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { | 72 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { |
| 76 public: | 73 public: |
| 77 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { | 74 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { |
| 78 } | 75 } |
| 79 virtual ~RemoveOnReadyRunLoopHandler() {} | 76 virtual ~RemoveOnReadyRunLoopHandler() {} |
| 80 | 77 |
| 81 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 78 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 82 | 79 |
| 83 // RunLoopHandler: | 80 // RunLoopHandler: |
| 84 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | 81 virtual void OnHandleReady(const Handle& handle) override { |
| 85 run_loop_->RemoveHandler(handle); | 82 run_loop_->RemoveHandler(handle); |
| 86 TestRunLoopHandler::OnHandleReady(handle); | 83 TestRunLoopHandler::OnHandleReady(handle); |
| 87 } | 84 } |
| 88 | 85 |
| 89 private: | 86 private: |
| 90 RunLoop* run_loop_; | 87 RunLoop* run_loop_; |
| 91 | 88 |
| 92 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); | 89 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); |
| 93 }; | 90 }; |
| 94 | 91 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 110 | 107 |
| 111 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { | 108 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { |
| 112 public: | 109 public: |
| 113 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { | 110 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { |
| 114 } | 111 } |
| 115 virtual ~QuitOnReadyRunLoopHandler() {} | 112 virtual ~QuitOnReadyRunLoopHandler() {} |
| 116 | 113 |
| 117 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 114 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 118 | 115 |
| 119 // RunLoopHandler: | 116 // RunLoopHandler: |
| 120 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | 117 virtual void OnHandleReady(const Handle& handle) override { |
| 121 run_loop_->Quit(); | 118 run_loop_->Quit(); |
| 122 TestRunLoopHandler::OnHandleReady(handle); | 119 TestRunLoopHandler::OnHandleReady(handle); |
| 123 } | 120 } |
| 124 | 121 |
| 125 private: | 122 private: |
| 126 RunLoop* run_loop_; | 123 RunLoop* run_loop_; |
| 127 | 124 |
| 128 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); | 125 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); |
| 129 }; | 126 }; |
| 130 | 127 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 146 | 143 |
| 147 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { | 144 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { |
| 148 public: | 145 public: |
| 149 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { | 146 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { |
| 150 } | 147 } |
| 151 virtual ~QuitOnErrorRunLoopHandler() {} | 148 virtual ~QuitOnErrorRunLoopHandler() {} |
| 152 | 149 |
| 153 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 150 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 154 | 151 |
| 155 // RunLoopHandler: | 152 // RunLoopHandler: |
| 156 virtual void OnHandleError(const Handle& handle, MojoResult result) | 153 virtual void OnHandleError(const Handle& handle, MojoResult result) override { |
| 157 MOJO_OVERRIDE { | |
| 158 run_loop_->Quit(); | 154 run_loop_->Quit(); |
| 159 TestRunLoopHandler::OnHandleError(handle, result); | 155 TestRunLoopHandler::OnHandleError(handle, result); |
| 160 } | 156 } |
| 161 | 157 |
| 162 private: | 158 private: |
| 163 RunLoop* run_loop_; | 159 RunLoop* run_loop_; |
| 164 | 160 |
| 165 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); | 161 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); |
| 166 }; | 162 }; |
| 167 | 163 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 class RemoveManyRunLoopHandler : public TestRunLoopHandler { | 195 class RemoveManyRunLoopHandler : public TestRunLoopHandler { |
| 200 public: | 196 public: |
| 201 RemoveManyRunLoopHandler() : run_loop_(NULL) { | 197 RemoveManyRunLoopHandler() : run_loop_(NULL) { |
| 202 } | 198 } |
| 203 virtual ~RemoveManyRunLoopHandler() {} | 199 virtual ~RemoveManyRunLoopHandler() {} |
| 204 | 200 |
| 205 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 201 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 206 void add_handle(const Handle& handle) { handles_.push_back(handle); } | 202 void add_handle(const Handle& handle) { handles_.push_back(handle); } |
| 207 | 203 |
| 208 // RunLoopHandler: | 204 // RunLoopHandler: |
| 209 virtual void OnHandleError(const Handle& handle, | 205 virtual void OnHandleError(const Handle& handle, MojoResult result) override { |
| 210 MojoResult result) MOJO_OVERRIDE { | |
| 211 for (size_t i = 0; i < handles_.size(); i++) | 206 for (size_t i = 0; i < handles_.size(); i++) |
| 212 run_loop_->RemoveHandler(handles_[i]); | 207 run_loop_->RemoveHandler(handles_[i]); |
| 213 TestRunLoopHandler::OnHandleError(handle, result); | 208 TestRunLoopHandler::OnHandleError(handle, result); |
| 214 } | 209 } |
| 215 | 210 |
| 216 private: | 211 private: |
| 217 std::vector<Handle> handles_; | 212 std::vector<Handle> handles_; |
| 218 RunLoop* run_loop_; | 213 RunLoop* run_loop_; |
| 219 | 214 |
| 220 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveManyRunLoopHandler); | 215 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveManyRunLoopHandler); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 251 | 246 |
| 252 class AddHandlerOnErrorHandler : public TestRunLoopHandler { | 247 class AddHandlerOnErrorHandler : public TestRunLoopHandler { |
| 253 public: | 248 public: |
| 254 AddHandlerOnErrorHandler() : run_loop_(NULL) { | 249 AddHandlerOnErrorHandler() : run_loop_(NULL) { |
| 255 } | 250 } |
| 256 virtual ~AddHandlerOnErrorHandler() {} | 251 virtual ~AddHandlerOnErrorHandler() {} |
| 257 | 252 |
| 258 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 253 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 259 | 254 |
| 260 // RunLoopHandler: | 255 // RunLoopHandler: |
| 261 virtual void OnHandleError(const Handle& handle, | 256 virtual void OnHandleError(const Handle& handle, MojoResult result) override { |
| 262 MojoResult result) MOJO_OVERRIDE { | |
| 263 run_loop_->AddHandler(this, handle, | 257 run_loop_->AddHandler(this, handle, |
| 264 MOJO_HANDLE_SIGNAL_READABLE, | 258 MOJO_HANDLE_SIGNAL_READABLE, |
| 265 MOJO_DEADLINE_INDEFINITE); | 259 MOJO_DEADLINE_INDEFINITE); |
| 266 TestRunLoopHandler::OnHandleError(handle, result); | 260 TestRunLoopHandler::OnHandleError(handle, result); |
| 267 } | 261 } |
| 268 | 262 |
| 269 private: | 263 private: |
| 270 RunLoop* run_loop_; | 264 RunLoop* run_loop_; |
| 271 | 265 |
| 272 MOJO_DISALLOW_COPY_AND_ASSIGN(AddHandlerOnErrorHandler); | 266 MOJO_DISALLOW_COPY_AND_ASSIGN(AddHandlerOnErrorHandler); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 depth_(0), | 301 depth_(0), |
| 308 reached_depth_limit_(false) {} | 302 reached_depth_limit_(false) {} |
| 309 | 303 |
| 310 virtual ~NestingRunLoopHandler() {} | 304 virtual ~NestingRunLoopHandler() {} |
| 311 | 305 |
| 312 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | 306 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } |
| 313 void set_pipe(MessagePipe* pipe) { pipe_ = pipe; } | 307 void set_pipe(MessagePipe* pipe) { pipe_ = pipe; } |
| 314 bool reached_depth_limit() const { return reached_depth_limit_; } | 308 bool reached_depth_limit() const { return reached_depth_limit_; } |
| 315 | 309 |
| 316 // RunLoopHandler: | 310 // RunLoopHandler: |
| 317 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | 311 virtual void OnHandleReady(const Handle& handle) override { |
| 318 TestRunLoopHandler::OnHandleReady(handle); | 312 TestRunLoopHandler::OnHandleReady(handle); |
| 319 EXPECT_EQ(handle.value(), pipe_->handle0.get().value()); | 313 EXPECT_EQ(handle.value(), pipe_->handle0.get().value()); |
| 320 | 314 |
| 321 ReadSignal(); | 315 ReadSignal(); |
| 322 size_t current_depth = ++depth_; | 316 size_t current_depth = ++depth_; |
| 323 if (current_depth < kDepthLimit) { | 317 if (current_depth < kDepthLimit) { |
| 324 WriteSignal(); | 318 WriteSignal(); |
| 325 run_loop_->Run(); | 319 run_loop_->Run(); |
| 326 if (current_depth == kDepthLimit - 1) { | 320 if (current_depth == kDepthLimit - 1) { |
| 327 // The topmost loop Quit()-ed, so its parent takes back the | 321 // The topmost loop Quit()-ed, so its parent takes back the |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 run_loop.AddHandler(&handler, | 422 run_loop.AddHandler(&handler, |
| 429 test_pipe.handle0.get(), | 423 test_pipe.handle0.get(), |
| 430 MOJO_HANDLE_SIGNAL_READABLE, | 424 MOJO_HANDLE_SIGNAL_READABLE, |
| 431 MOJO_DEADLINE_INDEFINITE); | 425 MOJO_DEADLINE_INDEFINITE); |
| 432 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); | 426 run_loop.PostDelayedTask(Closure(QuittingTask(&run_loop)), 0); |
| 433 run_loop.Run(); | 427 run_loop.Run(); |
| 434 } | 428 } |
| 435 | 429 |
| 436 } // namespace | 430 } // namespace |
| 437 } // namespace mojo | 431 } // namespace mojo |
| OLD | NEW |