Chromium Code Reviews| 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 | 183 |
| 184 TEST_F(RunLoopTest, Current) { | 184 TEST_F(RunLoopTest, Current) { |
| 185 EXPECT_TRUE(RunLoop::current() == NULL); | 185 EXPECT_TRUE(RunLoop::current() == NULL); |
| 186 { | 186 { |
| 187 RunLoop run_loop; | 187 RunLoop run_loop; |
| 188 EXPECT_EQ(&run_loop, RunLoop::current()); | 188 EXPECT_EQ(&run_loop, RunLoop::current()); |
| 189 } | 189 } |
| 190 EXPECT_TRUE(RunLoop::current() == NULL); | 190 EXPECT_TRUE(RunLoop::current() == NULL); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // TODO(darin): Add tests for nested calls to RunLoop::Run(). See crbug/384633. | 193 |
|
viettrungluu
2014/07/16 02:46:08
nit: Remove extra blank line.
| |
| 194 class NestingRunLoopHandler : public TestRunLoopHandler { | |
| 195 public: | |
| 196 static const size_t kDepthLimit; | |
| 197 static const char kSignalMagic; | |
| 198 | |
| 199 NestingRunLoopHandler() | |
| 200 : run_loop_(NULL), | |
| 201 pipe_(NULL), | |
| 202 depth_(0), | |
| 203 reached_depth_limit_(false) {} | |
| 204 | |
| 205 virtual ~NestingRunLoopHandler() {} | |
| 206 | |
| 207 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 208 void set_pipe(MessagePipe* pipe) { pipe_ = pipe; } | |
| 209 bool reached_depth_limit() const { return reached_depth_limit_; } | |
| 210 | |
| 211 // RunLoopHandler: | |
| 212 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | |
| 213 TestRunLoopHandler::OnHandleReady(handle); | |
| 214 EXPECT_EQ(handle.value(), pipe_->handle0.get().value()); | |
| 215 | |
| 216 ReadSignal(); | |
| 217 size_t current_depth = ++depth_; | |
| 218 if (current_depth < kDepthLimit) { | |
| 219 WriteSignal(); | |
| 220 run_loop_->Run(); | |
| 221 if (current_depth == kDepthLimit - 1) { | |
| 222 // The topmost loop Quit()-ed, so its parent takes back the | |
| 223 // control without exeeding deadline. | |
| 224 EXPECT_EQ(error_count(), 0); | |
| 225 } else { | |
| 226 EXPECT_EQ(error_count(), 1); | |
| 227 } | |
| 228 | |
| 229 } else { | |
| 230 EXPECT_EQ(current_depth, kDepthLimit); | |
| 231 reached_depth_limit_ = true; | |
| 232 run_loop_->Quit(); | |
| 233 } | |
| 234 --depth_; | |
| 235 } | |
| 236 | |
| 237 void WriteSignal() { | |
| 238 char write_byte = kSignalMagic; | |
| 239 MojoResult write_result = WriteMessageRaw( | |
| 240 pipe_->handle1.get(), | |
| 241 &write_byte, 1, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 242 EXPECT_EQ(write_result, MOJO_RESULT_OK); | |
| 243 } | |
| 244 | |
| 245 void ReadSignal() { | |
| 246 char read_byte = 0; | |
| 247 uint32_t bytes_read = 1; | |
| 248 uint32_t handles_read = 0; | |
| 249 MojoResult read_result = ReadMessageRaw( | |
| 250 pipe_->handle0.get(), | |
| 251 &read_byte, &bytes_read, NULL, &handles_read, | |
| 252 MOJO_READ_MESSAGE_FLAG_NONE); | |
| 253 EXPECT_EQ(read_result, MOJO_RESULT_OK); | |
| 254 EXPECT_EQ(read_byte, kSignalMagic); | |
| 255 } | |
| 256 | |
| 257 private: | |
| 258 RunLoop* run_loop_; | |
| 259 MessagePipe* pipe_; | |
| 260 size_t depth_; | |
| 261 bool reached_depth_limit_; | |
| 262 | |
| 263 MOJO_DISALLOW_COPY_AND_ASSIGN(NestingRunLoopHandler); | |
| 264 }; | |
| 265 | |
| 266 const size_t NestingRunLoopHandler::kDepthLimit = 10; | |
| 267 const char NestingRunLoopHandler::kSignalMagic = 'X'; | |
| 268 | |
| 269 TEST_F(RunLoopTest, NestedRun) { | |
| 270 NestingRunLoopHandler handler; | |
| 271 MessagePipe test_pipe; | |
| 272 RunLoop run_loop; | |
| 273 handler.set_run_loop(&run_loop); | |
| 274 handler.set_pipe(&test_pipe); | |
| 275 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 276 MOJO_HANDLE_SIGNAL_READABLE, | |
| 277 static_cast<MojoDeadline>(10000)); | |
| 278 handler.WriteSignal(); | |
| 279 run_loop.Run(); | |
| 280 | |
| 281 EXPECT_TRUE(handler.reached_depth_limit()); | |
| 282 // Got MOJO_RESULT_DEADLINE_EXCEEDED once then removed from the | |
| 283 // RunLoop's handler list. | |
| 284 EXPECT_EQ(handler.error_count(), 1); | |
| 285 EXPECT_EQ(handler.last_error_result(), MOJO_RESULT_DEADLINE_EXCEEDED); | |
| 286 } | |
| 194 | 287 |
| 195 } // namespace | 288 } // namespace |
| 196 } // namespace mojo | 289 } // namespace mojo |
| OLD | NEW |