| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/public/cpp/utility/run_loop.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "mojo/public/cpp/system/core.h" | |
| 10 #include "mojo/public/cpp/utility/run_loop_handler.h" | |
| 11 #include "mojo/public/tests/test_utils.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) MOJO_OVERRIDE { | |
| 36 ready_count_++; | |
| 37 } | |
| 38 virtual void OnHandleError(const Handle& handle, MojoResult result) | |
| 39 MOJO_OVERRIDE { | |
| 40 error_count_++; | |
| 41 last_error_result_ = result; | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 int ready_count_; | |
| 46 int error_count_; | |
| 47 MojoResult last_error_result_; | |
| 48 | |
| 49 MOJO_DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); | |
| 50 }; | |
| 51 | |
| 52 class RunLoopTest : public testing::Test { | |
| 53 public: | |
| 54 RunLoopTest() {} | |
| 55 | |
| 56 virtual void SetUp() MOJO_OVERRIDE { | |
| 57 Test::SetUp(); | |
| 58 RunLoop::SetUp(); | |
| 59 } | |
| 60 virtual void TearDown() MOJO_OVERRIDE { | |
| 61 RunLoop::TearDown(); | |
| 62 Test::TearDown(); | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopTest); | |
| 67 }; | |
| 68 | |
| 69 // Trivial test to verify Run() with no added handles returns. | |
| 70 TEST_F(RunLoopTest, ExitsWithNoHandles) { | |
| 71 RunLoop run_loop; | |
| 72 run_loop.Run(); | |
| 73 } | |
| 74 | |
| 75 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { | |
| 76 public: | |
| 77 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { | |
| 78 } | |
| 79 virtual ~RemoveOnReadyRunLoopHandler() {} | |
| 80 | |
| 81 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 82 | |
| 83 // RunLoopHandler: | |
| 84 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | |
| 85 run_loop_->RemoveHandler(handle); | |
| 86 TestRunLoopHandler::OnHandleReady(handle); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 RunLoop* run_loop_; | |
| 91 | |
| 92 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); | |
| 93 }; | |
| 94 | |
| 95 // Verifies RunLoop quits when no more handles (handle is removed when ready). | |
| 96 TEST_F(RunLoopTest, HandleReady) { | |
| 97 RemoveOnReadyRunLoopHandler handler; | |
| 98 MessagePipe test_pipe; | |
| 99 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
| 100 | |
| 101 RunLoop run_loop; | |
| 102 handler.set_run_loop(&run_loop); | |
| 103 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 104 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); | |
| 105 run_loop.Run(); | |
| 106 EXPECT_EQ(1, handler.ready_count()); | |
| 107 EXPECT_EQ(0, handler.error_count()); | |
| 108 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 109 } | |
| 110 | |
| 111 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { | |
| 112 public: | |
| 113 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { | |
| 114 } | |
| 115 virtual ~QuitOnReadyRunLoopHandler() {} | |
| 116 | |
| 117 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 118 | |
| 119 // RunLoopHandler: | |
| 120 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE { | |
| 121 run_loop_->Quit(); | |
| 122 TestRunLoopHandler::OnHandleReady(handle); | |
| 123 } | |
| 124 | |
| 125 private: | |
| 126 RunLoop* run_loop_; | |
| 127 | |
| 128 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); | |
| 129 }; | |
| 130 | |
| 131 // Verifies Quit() from OnHandleReady() quits the loop. | |
| 132 TEST_F(RunLoopTest, QuitFromReady) { | |
| 133 QuitOnReadyRunLoopHandler handler; | |
| 134 MessagePipe test_pipe; | |
| 135 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
| 136 | |
| 137 RunLoop run_loop; | |
| 138 handler.set_run_loop(&run_loop); | |
| 139 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 140 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); | |
| 141 run_loop.Run(); | |
| 142 EXPECT_EQ(1, handler.ready_count()); | |
| 143 EXPECT_EQ(0, handler.error_count()); | |
| 144 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 145 } | |
| 146 | |
| 147 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { | |
| 148 public: | |
| 149 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { | |
| 150 } | |
| 151 virtual ~QuitOnErrorRunLoopHandler() {} | |
| 152 | |
| 153 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 154 | |
| 155 // RunLoopHandler: | |
| 156 virtual void OnHandleError(const Handle& handle, MojoResult result) | |
| 157 MOJO_OVERRIDE { | |
| 158 run_loop_->Quit(); | |
| 159 TestRunLoopHandler::OnHandleError(handle, result); | |
| 160 } | |
| 161 | |
| 162 private: | |
| 163 RunLoop* run_loop_; | |
| 164 | |
| 165 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); | |
| 166 }; | |
| 167 | |
| 168 // Verifies Quit() when the deadline is reached works. | |
| 169 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) { | |
| 170 QuitOnErrorRunLoopHandler handler; | |
| 171 MessagePipe test_pipe; | |
| 172 RunLoop run_loop; | |
| 173 handler.set_run_loop(&run_loop); | |
| 174 run_loop.AddHandler(&handler, test_pipe.handle0.get(), | |
| 175 MOJO_WAIT_FLAG_READABLE, | |
| 176 static_cast<MojoDeadline>(10000)); | |
| 177 run_loop.Run(); | |
| 178 EXPECT_EQ(0, handler.ready_count()); | |
| 179 EXPECT_EQ(1, handler.error_count()); | |
| 180 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, handler.last_error_result()); | |
| 181 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); | |
| 182 } | |
| 183 | |
| 184 TEST_F(RunLoopTest, Current) { | |
| 185 EXPECT_TRUE(RunLoop::current() == NULL); | |
| 186 { | |
| 187 RunLoop run_loop; | |
| 188 EXPECT_EQ(&run_loop, RunLoop::current()); | |
| 189 } | |
| 190 EXPECT_TRUE(RunLoop::current() == NULL); | |
| 191 } | |
| 192 | |
| 193 } // namespace | |
| 194 } // namespace mojo | |
| OLD | NEW |