Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: mojo/public/utility/tests/run_loop_unittest.cc

Issue 148013006: Mojo: re-org public/utility and public/environment (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: lower similarity factor Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/utility/run_loop.cc ('k') | mojo/public/utility/thread_local.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/utility/run_loop.h" 5 #include "mojo/public/utility/run_loop.h"
6 6
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "mojo/public/system/core_cpp.h" 7 #include "mojo/public/system/core_cpp.h"
10 #include "mojo/public/tests/test_support.h" 8 #include "mojo/public/tests/test_support.h"
11 #include "mojo/public/utility/run_loop_handler.h" 9 #include "mojo/public/utility/run_loop_handler.h"
12 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
13 11
14 namespace mojo { 12 namespace mojo {
15 namespace { 13 namespace {
16 14
17 class TestRunLoopHandler : public RunLoopHandler { 15 class TestRunLoopHandler : public RunLoopHandler {
18 public: 16 public:
19 TestRunLoopHandler() 17 TestRunLoopHandler()
20 : ready_count_(0), 18 : ready_count_(0),
21 error_count_(0), 19 error_count_(0),
22 last_error_result_(MOJO_RESULT_OK) { 20 last_error_result_(MOJO_RESULT_OK) {
23 } 21 }
24 virtual ~TestRunLoopHandler() {} 22 virtual ~TestRunLoopHandler() {}
25 23
26 void clear_ready_count() { ready_count_ = 0; } 24 void clear_ready_count() { ready_count_ = 0; }
27 int ready_count() const { return ready_count_; } 25 int ready_count() const { return ready_count_; }
28 26
29 void clear_error_count() { error_count_ = 0; } 27 void clear_error_count() { error_count_ = 0; }
30 int error_count() const { return error_count_; } 28 int error_count() const { return error_count_; }
31 29
32 MojoResult last_error_result() const { return last_error_result_; } 30 MojoResult last_error_result() const { return last_error_result_; }
33 31
34 // RunLoopHandler: 32 // RunLoopHandler:
35 virtual void OnHandleReady(const Handle& handle) OVERRIDE { 33 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE {
36 ready_count_++; 34 ready_count_++;
37 } 35 }
38 virtual void OnHandleError(const Handle& handle, MojoResult result) OVERRIDE { 36 virtual void OnHandleError(const Handle& handle, MojoResult result)
37 MOJO_OVERRIDE {
39 error_count_++; 38 error_count_++;
40 last_error_result_ = result; 39 last_error_result_ = result;
41 } 40 }
42 41
43 private: 42 private:
44 int ready_count_; 43 int ready_count_;
45 int error_count_; 44 int error_count_;
46 MojoResult last_error_result_; 45 MojoResult last_error_result_;
47 46
48 DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler); 47 MOJO_DISALLOW_COPY_AND_ASSIGN(TestRunLoopHandler);
49 }; 48 };
50 49
51 class RunLoopTest : public testing::Test { 50 class RunLoopTest : public testing::Test {
52 public: 51 public:
53 RunLoopTest() {} 52 RunLoopTest() {}
54 53
55 virtual void SetUp() OVERRIDE { 54 virtual void SetUp() MOJO_OVERRIDE {
56 Test::SetUp(); 55 Test::SetUp();
57 RunLoop::SetUp(); 56 RunLoop::SetUp();
58 } 57 }
59 virtual void TearDown() OVERRIDE { 58 virtual void TearDown() MOJO_OVERRIDE {
60 RunLoop::TearDown(); 59 RunLoop::TearDown();
61 Test::TearDown(); 60 Test::TearDown();
62 } 61 }
63 62
64 private: 63 private:
65 DISALLOW_COPY_AND_ASSIGN(RunLoopTest); 64 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopTest);
66 }; 65 };
67 66
68 // Trivial test to verify Run() with no added handles returns. 67 // Trivial test to verify Run() with no added handles returns.
69 TEST_F(RunLoopTest, ExitsWithNoHandles) { 68 TEST_F(RunLoopTest, ExitsWithNoHandles) {
70 RunLoop run_loop; 69 RunLoop run_loop;
71 run_loop.Run(); 70 run_loop.Run();
72 } 71 }
73 72
74 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler { 73 class RemoveOnReadyRunLoopHandler : public TestRunLoopHandler {
75 public: 74 public:
76 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) { 75 RemoveOnReadyRunLoopHandler() : run_loop_(NULL) {
77 } 76 }
78 virtual ~RemoveOnReadyRunLoopHandler() {} 77 virtual ~RemoveOnReadyRunLoopHandler() {}
79 78
80 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } 79 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; }
81 80
82 // RunLoopHandler: 81 // RunLoopHandler:
83 virtual void OnHandleReady(const Handle& handle) OVERRIDE { 82 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE {
84 run_loop_->RemoveHandler(handle); 83 run_loop_->RemoveHandler(handle);
85 TestRunLoopHandler::OnHandleReady(handle); 84 TestRunLoopHandler::OnHandleReady(handle);
86 } 85 }
87 86
88 private: 87 private:
89 RunLoop* run_loop_; 88 RunLoop* run_loop_;
90 89
91 DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler); 90 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoveOnReadyRunLoopHandler);
92 }; 91 };
93 92
94 // Verifies RunLoop quits when no more handles (handle is removed when ready). 93 // Verifies RunLoop quits when no more handles (handle is removed when ready).
95 TEST_F(RunLoopTest, HandleReady) { 94 TEST_F(RunLoopTest, HandleReady) {
96 RemoveOnReadyRunLoopHandler handler; 95 RemoveOnReadyRunLoopHandler handler;
97 MessagePipe test_pipe; 96 MessagePipe test_pipe;
98 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); 97 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get()));
99 98
100 RunLoop run_loop; 99 RunLoop run_loop;
101 handler.set_run_loop(&run_loop); 100 handler.set_run_loop(&run_loop);
102 run_loop.AddHandler(&handler, test_pipe.handle0.get(), 101 run_loop.AddHandler(&handler, test_pipe.handle0.get(),
103 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); 102 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
104 run_loop.Run(); 103 run_loop.Run();
105 EXPECT_EQ(1, handler.ready_count()); 104 EXPECT_EQ(1, handler.ready_count());
106 EXPECT_EQ(0, handler.error_count()); 105 EXPECT_EQ(0, handler.error_count());
107 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); 106 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get()));
108 } 107 }
109 108
110 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler { 109 class QuitOnReadyRunLoopHandler : public TestRunLoopHandler {
111 public: 110 public:
112 QuitOnReadyRunLoopHandler() : run_loop_(NULL) { 111 QuitOnReadyRunLoopHandler() : run_loop_(NULL) {
113 } 112 }
114 virtual ~QuitOnReadyRunLoopHandler() {} 113 virtual ~QuitOnReadyRunLoopHandler() {}
115 114
116 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } 115 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; }
117 116
118 // RunLoopHandler: 117 // RunLoopHandler:
119 virtual void OnHandleReady(const Handle& handle) OVERRIDE { 118 virtual void OnHandleReady(const Handle& handle) MOJO_OVERRIDE {
120 run_loop_->Quit(); 119 run_loop_->Quit();
121 TestRunLoopHandler::OnHandleReady(handle); 120 TestRunLoopHandler::OnHandleReady(handle);
122 } 121 }
123 122
124 private: 123 private:
125 RunLoop* run_loop_; 124 RunLoop* run_loop_;
126 125
127 DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler); 126 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnReadyRunLoopHandler);
128 }; 127 };
129 128
130 // Verifies Quit() from OnHandleReady() quits the loop. 129 // Verifies Quit() from OnHandleReady() quits the loop.
131 TEST_F(RunLoopTest, QuitFromReady) { 130 TEST_F(RunLoopTest, QuitFromReady) {
132 QuitOnReadyRunLoopHandler handler; 131 QuitOnReadyRunLoopHandler handler;
133 MessagePipe test_pipe; 132 MessagePipe test_pipe;
134 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); 133 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get()));
135 134
136 RunLoop run_loop; 135 RunLoop run_loop;
137 handler.set_run_loop(&run_loop); 136 handler.set_run_loop(&run_loop);
138 run_loop.AddHandler(&handler, test_pipe.handle0.get(), 137 run_loop.AddHandler(&handler, test_pipe.handle0.get(),
139 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE); 138 MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
140 run_loop.Run(); 139 run_loop.Run();
141 EXPECT_EQ(1, handler.ready_count()); 140 EXPECT_EQ(1, handler.ready_count());
142 EXPECT_EQ(0, handler.error_count()); 141 EXPECT_EQ(0, handler.error_count());
143 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get())); 142 EXPECT_TRUE(run_loop.HasHandler(test_pipe.handle0.get()));
144 } 143 }
145 144
146 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler { 145 class QuitOnErrorRunLoopHandler : public TestRunLoopHandler {
147 public: 146 public:
148 QuitOnErrorRunLoopHandler() : run_loop_(NULL) { 147 QuitOnErrorRunLoopHandler() : run_loop_(NULL) {
149 } 148 }
150 virtual ~QuitOnErrorRunLoopHandler() {} 149 virtual ~QuitOnErrorRunLoopHandler() {}
151 150
152 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; } 151 void set_run_loop(RunLoop* run_loop) { run_loop_ = run_loop; }
153 152
154 // RunLoopHandler: 153 // RunLoopHandler:
155 virtual void OnHandleError(const Handle& handle, MojoResult result) OVERRIDE { 154 virtual void OnHandleError(const Handle& handle, MojoResult result)
155 MOJO_OVERRIDE {
156 run_loop_->Quit(); 156 run_loop_->Quit();
157 TestRunLoopHandler::OnHandleError(handle, result); 157 TestRunLoopHandler::OnHandleError(handle, result);
158 } 158 }
159 159
160 private: 160 private:
161 RunLoop* run_loop_; 161 RunLoop* run_loop_;
162 162
163 DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler); 163 MOJO_DISALLOW_COPY_AND_ASSIGN(QuitOnErrorRunLoopHandler);
164 }; 164 };
165 165
166 // Verifies Quit() when the deadline is reached works. 166 // Verifies Quit() when the deadline is reached works.
167 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) { 167 TEST_F(RunLoopTest, QuitWhenDeadlineExpired) {
168 QuitOnErrorRunLoopHandler handler; 168 QuitOnErrorRunLoopHandler handler;
169 MessagePipe test_pipe; 169 MessagePipe test_pipe;
170 RunLoop run_loop; 170 RunLoop run_loop;
171 handler.set_run_loop(&run_loop); 171 handler.set_run_loop(&run_loop);
172 run_loop.AddHandler(&handler, test_pipe.handle0.get(), 172 run_loop.AddHandler(&handler, test_pipe.handle0.get(),
173 MOJO_WAIT_FLAG_READABLE, 173 MOJO_WAIT_FLAG_READABLE,
174 static_cast<MojoDeadline>(10000)); 174 static_cast<MojoDeadline>(10000));
175 run_loop.Run(); 175 run_loop.Run();
176 EXPECT_EQ(0, handler.ready_count()); 176 EXPECT_EQ(0, handler.ready_count());
177 EXPECT_EQ(1, handler.error_count()); 177 EXPECT_EQ(1, handler.error_count());
178 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, handler.last_error_result()); 178 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, handler.last_error_result());
179 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get())); 179 EXPECT_FALSE(run_loop.HasHandler(test_pipe.handle0.get()));
180 } 180 }
181 181
182 TEST_F(RunLoopTest, Current) { 182 TEST_F(RunLoopTest, Current) {
183 EXPECT_TRUE(RunLoop::current() == NULL); 183 EXPECT_TRUE(RunLoop::current() == NULL);
184 { 184 {
185 RunLoop run_loop; 185 RunLoop run_loop;
186 EXPECT_EQ(&run_loop, RunLoop::current()); 186 EXPECT_EQ(&run_loop, RunLoop::current());
187 } 187 }
188 EXPECT_TRUE(RunLoop::current() == NULL); 188 EXPECT_TRUE(RunLoop::current() == NULL);
189 } 189 }
190 190
191 } // namespace 191 } // namespace
192 } // namespace mojo 192 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/utility/run_loop.cc ('k') | mojo/public/utility/thread_local.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698