OLD | NEW |
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/common/handle_watcher.h" | 5 #include "mojo/common/handle_watcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/test/simple_test_tick_clock.h" | 12 #include "base/test/simple_test_tick_clock.h" |
13 #include "mojo/common/time_helper.h" | 13 #include "mojo/common/time_helper.h" |
14 #include "mojo/public/cpp/environment/environment.h" | 14 #include "mojo/public/cpp/environment/environment.h" |
15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
16 #include "mojo/public/cpp/test_support/test_utils.h" | 16 #include "mojo/public/cpp/test_support/test_utils.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace mojo { | 19 namespace mojo { |
20 namespace common { | 20 namespace common { |
21 namespace test { | 21 namespace test { |
22 | 22 |
| 23 void ObserveCallback(bool* was_signaled, |
| 24 MojoResult* result_observed, |
| 25 MojoResult result) { |
| 26 *was_signaled = true; |
| 27 *result_observed = result; |
| 28 } |
| 29 |
23 void RunUntilIdle() { | 30 void RunUntilIdle() { |
24 base::RunLoop run_loop; | 31 base::RunLoop run_loop; |
25 run_loop.RunUntilIdle(); | 32 run_loop.RunUntilIdle(); |
26 } | 33 } |
27 | 34 |
28 void DeleteWatcherAndForwardResult( | 35 void DeleteWatcherAndForwardResult( |
29 HandleWatcher* watcher, | 36 HandleWatcher* watcher, |
30 base::Callback<void(MojoResult)> next_callback, | 37 base::Callback<void(MojoResult)> next_callback, |
31 MojoResult result) { | 38 MojoResult result) { |
32 delete watcher; | 39 delete watcher; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); | 101 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); |
95 }; | 102 }; |
96 | 103 |
97 class HandleWatcherTest : public testing::Test { | 104 class HandleWatcherTest : public testing::Test { |
98 public: | 105 public: |
99 HandleWatcherTest() {} | 106 HandleWatcherTest() {} |
100 virtual ~HandleWatcherTest() { | 107 virtual ~HandleWatcherTest() { |
101 test::SetTickClockForTest(NULL); | 108 test::SetTickClockForTest(NULL); |
102 } | 109 } |
103 | 110 |
104 virtual void SetUp() OVERRIDE { | |
105 environment_.reset(new Environment); | |
106 } | |
107 | |
108 virtual void TearDown() OVERRIDE { | |
109 environment_.reset(); | |
110 } | |
111 | |
112 protected: | 111 protected: |
113 void InstallTickClock() { | 112 void InstallTickClock() { |
114 test::SetTickClockForTest(&tick_clock_); | 113 test::SetTickClockForTest(&tick_clock_); |
115 } | 114 } |
116 | 115 |
117 base::SimpleTestTickClock tick_clock_; | 116 base::SimpleTestTickClock tick_clock_; |
118 | 117 |
119 private: | 118 private: |
| 119 Environment environment_; |
120 base::MessageLoop message_loop_; | 120 base::MessageLoop message_loop_; |
121 scoped_ptr<Environment> environment_; | |
122 | 121 |
123 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); | 122 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); |
124 }; | 123 }; |
125 | 124 |
126 // Trivial test case with a single handle to watch. | 125 // Trivial test case with a single handle to watch. |
127 TEST_F(HandleWatcherTest, SingleHandler) { | 126 TEST_F(HandleWatcherTest, SingleHandler) { |
128 MessagePipe test_pipe; | 127 MessagePipe test_pipe; |
129 ASSERT_TRUE(test_pipe.handle0.is_valid()); | 128 ASSERT_TRUE(test_pipe.handle0.is_valid()); |
130 CallbackHelper callback_helper; | 129 CallbackHelper callback_helper; |
131 HandleWatcher watcher; | 130 HandleWatcher watcher; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), | 307 callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), |
309 base::Bind(&DeleteWatcherAndForwardResult, | 308 base::Bind(&DeleteWatcherAndForwardResult, |
310 watcher, | 309 watcher, |
311 callback_helper.GetCallback())); | 310 callback_helper.GetCallback())); |
312 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), | 311 EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), |
313 std::string())); | 312 std::string())); |
314 callback_helper.RunUntilGotCallback(); | 313 callback_helper.RunUntilGotCallback(); |
315 EXPECT_TRUE(callback_helper.got_callback()); | 314 EXPECT_TRUE(callback_helper.got_callback()); |
316 } | 315 } |
317 | 316 |
| 317 TEST(HandleWatcherCleanEnvironmentTest, AbortedOnMessageLoopDestruction) { |
| 318 bool was_signaled = false; |
| 319 MojoResult result = MOJO_RESULT_OK; |
| 320 |
| 321 Environment env; |
| 322 |
| 323 MessagePipe pipe; |
| 324 HandleWatcher watcher; |
| 325 { |
| 326 base::MessageLoop loop; |
| 327 |
| 328 watcher.Start(pipe.handle0.get(), |
| 329 MOJO_WAIT_FLAG_READABLE, |
| 330 MOJO_DEADLINE_INDEFINITE, |
| 331 base::Bind(&ObserveCallback, &was_signaled, &result)); |
| 332 |
| 333 // Now, let the MessageLoop get torn down. We expect our callback to run. |
| 334 } |
| 335 |
| 336 EXPECT_TRUE(was_signaled); |
| 337 EXPECT_EQ(MOJO_RESULT_ABORTED, result); |
| 338 } |
| 339 |
318 } // namespace test | 340 } // namespace test |
319 } // namespace common | 341 } // namespace common |
320 } // namespace mojo | 342 } // namespace mojo |
OLD | NEW |