| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/public/utility/run_loop.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "mojo/public/system/core_cpp.h" | |
| 10 #include "mojo/public/tests/test_support.h" | |
| 11 #include "mojo/public/utility/run_loop_handler.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace { | |
| 16 | |
| 17 class TestRunLoopHandler : public RunLoopHandler { | |
| 18 public: | |
| 19 TestRunLoopHandler() | |
| 20 : ready_count_(0), | |
| 21 error_count_(0), | |
| 22 last_error_result_(MOJO_RESULT_OK) { | |
| 23 } | |
| 24 virtual ~TestRunLoopHandler() {} | |
| 25 | |
| 26 void clear_ready_count() { ready_count_ = 0; } | |
| 27 int ready_count() const { return ready_count_; } | |
| 28 | |
| 29 void clear_error_count() { error_count_ = 0; } | |
| 30 int error_count() const { return error_count_; } | |
| 31 | |
| 32 MojoResult last_error_result() const { return last_error_result_; } | |
| 33 | |
| 34 // RunLoopHandler: | |
| 35 virtual void OnHandleReady(const Handle& handle) OVERRIDE { | |
| 36 ready_count_++; | |
| 37 } | |
| 38 virtual void OnHandleError(const Handle& handle, MojoResult result) OVERRIDE { | |
| 39 error_count_++; | |
| 40 last_error_result_ = result; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 int ready_count_; | |
| 45 int error_count_; | |
| 46 MojoResult last_error_result_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); | |
| 49 }; | |
| 50 | |
| 51 class RunLoopTest : public testing::Test { | |
| 52 public: | |
| 53 RunLoopTest() {} | |
| 54 | |
| 55 virtual void SetUp() OVERRIDE { | |
| 56 Test::SetUp(); | |
| 57 RunLoop::SetUp(); | |
| 58 } | |
| 59 virtual void TearDown() OVERRIDE { | |
| 60 RunLoop::TearDown(); | |
| 61 Test::TearDown(); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | |
| 66 }; | |
| 67 | |
| 68 // Trivial test to verify Run() with no added handles returns. | |
| 69 TEST_F(RunLoopTest, ExitsWithNoHandles) { | |
| 70 RunLoop run_loop; | |
| 71 run_loop.Run(); | |
| 72 } | |
| 73 | |
| 74 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { | |
| 75 public: | |
| 76 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { | |
| 77 } | |
| 78 virtual ~RemoveOnReadyRunLoopHandler() {} | |
| 79 | |
| 80 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 81 | |
| 82 // RunLoopHandler: | |
| 83 virtual void OnHandleReady(const Handle& handle) OVERRIDE { | |
| 84 run_loop_->RemoveHandler(handle); | |
| 85 TestRunLoopHandler::OnHandleReady(handle); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 RunLoop* run_loop_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); | |
| 92 }; | |
| 93 | |
| 94 // Verifies RunLoop quits when no more handles (handle is removed when ready). | |
| 95 TEST_F(RunLoopTest, HandleReady) { | |
| 96 RemoveOnReadyRunLoopHandler handler; | |
| 97 MessagePipe test_pipe; | |
| 98 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | |
| 99 | |
| 100 RunLoop run_loop; | |
| 101 handler.set_run_loop(&run_loop); | |
| 102 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 103 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); | |
| 104 run_loop.Run(); | |
| 105 EXPECT_EQ(1, handler.ready_count()); | |
| 106 EXPECT_EQ(0, handler.error_count()); | |
| 107 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 108 } | |
| 109 | |
| 110 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { | |
| 111 public: | |
| 112 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { | |
| 113 } | |
| 114 virtual ~QuitOnReadyRunLoopHandler() {} | |
| 115 | |
| 116 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 117 | |
| 118 // RunLoopHandler: | |
| 119 virtual void OnHandleReady(const Handle& handle) OVERRIDE { | |
| 120 run_loop_->Quit(); | |
| 121 TestRunLoopHandler::OnHandleReady(handle); | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 RunLoop* run_loop_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); | |
| 128 }; | |
| 129 | |
| 130 // Verifies Quit() from OnHandleReady() quits the loop. | |
| 131 TEST_F(RunLoopTest, QuitFromReady) { | |
| 132 QuitOnReadyRunLoopHandler handler; | |
| 133 MessagePipe test_pipe; | |
| 134 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | |
| 135 | |
| 136 RunLoop run_loop; | |
| 137 handler.set_run_loop(&run_loop); | |
| 138 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 139 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); | |
| 140 run_loop.Run(); | |
| 141 EXPECT_EQ(1, handler.ready_count()); | |
| 142 EXPECT_EQ(0, handler.error_count()); | |
| 143 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 144 } | |
| 145 | |
| 146 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { | |
| 147 public: | |
| 148 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { | |
| 149 } | |
| 150 virtual ~QuitOnErrorRunLoopHandler() {} | |
| 151 | |
| 152 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 153 | |
| 154 // RunLoopHandler: | |
| 155 virtual void OnHandleError(const Handle& handle, MojoResult result) OVERRIDE { | |
| 156 run_loop_->Quit(); | |
| 157 TestRunLoopHandler::OnHandleError(handle, result); | |
| 158 } | |
| 159 | |
| 160 private: | |
| 161 RunLoop* run_loop_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); | |
| 164 }; | |
| 165 | |
| 166 // Verifies Quit() when the deadline is reached works. | |
| 167 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) { | |
| 168 QuitOnErrorRunLoopHandler handler; | |
| 169 MessagePipe test_pipe; | |
| 170 RunLoop run_loop; | |
| 171 handler.set_run_loop(&run_loop); | |
| 172 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 173 MOJO_WAIT_FLAG_READABLE, | |
| 174 static_cast<MojoDeadline>(10000)); | |
| 175 run_loop.Run(); | |
| 176 EXPECT_EQ(0, handler.ready_count()); | |
| 177 EXPECT_EQ(1, handler.error_count()); | |
| 178 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, handler.last_error_result()); | |
| 179 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 180 } | |
| 181 | |
| 182 TEST_F(RunLoopTest, Current) { | |
| 183 EXPECT_TRUE(RunLoop::current() == NULL); | |
| 184 { | |
| 185 RunLoop run_loop; | |
| 186 EXPECT_EQ(&run_loop, RunLoop::current()); | |
| 187 } | |
| 188 EXPECT_TRUE(RunLoop::current() == NULL); | |
| 189 } | |
| 190 | |
| 191 } // namespace | |
| 192 } // namespace mojo | |
| OLD | NEW |