| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/system/watcher.h" | 5 #include "mojo/public/cpp/system/watcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | |
| 12 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "mojo/public/c/system/types.h" | 14 #include "mojo/public/c/system/types.h" |
| 16 #include "mojo/public/cpp/system/message_pipe.h" | 15 #include "mojo/public/cpp/system/message_pipe.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace mojo { | 18 namespace mojo { |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 template <typename Handler> | 21 template <typename Handler> |
| 23 void RunResultHandler(Handler f, MojoResult result) { f(result); } | 22 void RunResultHandler(Handler f, MojoResult result) { f(result); } |
| 24 | 23 |
| 25 template <typename Handler> | 24 template <typename Handler> |
| 26 Watcher::ReadyCallback OnReady(Handler f) { | 25 Watcher::ReadyCallback OnReady(Handler f) { |
| 27 return base::Bind(&RunResultHandler<Handler>, f); | 26 return base::Bind(&RunResultHandler<Handler>, f); |
| 28 } | 27 } |
| 29 | 28 |
| 30 Watcher::ReadyCallback NotReached() { | 29 Watcher::ReadyCallback NotReached() { |
| 31 return OnReady([] (MojoResult) { NOTREACHED(); }); | 30 return OnReady([] (MojoResult) { NOTREACHED(); }); |
| 32 } | 31 } |
| 33 | 32 |
| 34 class WatcherTest : public testing::Test { | 33 class WatcherTest : public testing::Test { |
| 35 public: | 34 public: |
| 36 WatcherTest() {} | 35 WatcherTest() {} |
| 37 ~WatcherTest() override {} | 36 ~WatcherTest() override {} |
| 38 | 37 |
| 39 private: | 38 void SetUp() override { |
| 40 base::MessageLoop message_loop_; | 39 message_loop_.reset(new base::MessageLoop); |
| 40 } |
| 41 | 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(WatcherTest); | 42 void TearDown() override { |
| 43 message_loop_.reset(); |
| 44 } |
| 45 |
| 46 protected: |
| 47 std::unique_ptr<base::MessageLoop> message_loop_; |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 TEST_F(WatcherTest, WatchBasic) { | 50 TEST_F(WatcherTest, WatchBasic) { |
| 46 ScopedMessagePipeHandle a, b; | 51 ScopedMessagePipeHandle a, b; |
| 47 CreateMessagePipe(nullptr, &a, &b); | 52 CreateMessagePipe(nullptr, &a, &b); |
| 48 | 53 |
| 49 bool notified = false; | 54 bool notified = false; |
| 50 base::RunLoop run_loop; | 55 base::RunLoop run_loop; |
| 51 Watcher b_watcher; | 56 Watcher b_watcher; |
| 52 EXPECT_EQ(MOJO_RESULT_OK, | 57 EXPECT_EQ(MOJO_RESULT_OK, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 } | 155 } |
| 151 | 156 |
| 152 // This should never trigger the watcher above. | 157 // This should never trigger the watcher above. |
| 153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, | 158 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, |
| 154 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 159 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 155 base::ThreadTaskRunnerHandle::Get()->PostTask( | 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 156 FROM_HERE, run_loop.QuitClosure()); | 161 FROM_HERE, run_loop.QuitClosure()); |
| 157 run_loop.Run(); | 162 run_loop.Run(); |
| 158 } | 163 } |
| 159 | 164 |
| 165 TEST_F(WatcherTest, NotifyOnMessageLoopDestruction) { |
| 166 ScopedMessagePipeHandle a, b; |
| 167 CreateMessagePipe(nullptr, &a, &b); |
| 168 |
| 169 bool notified = false; |
| 170 Watcher b_watcher; |
| 171 EXPECT_EQ(MOJO_RESULT_OK, |
| 172 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 173 OnReady([&] (MojoResult result) { |
| 174 EXPECT_EQ(MOJO_RESULT_ABORTED, result); |
| 175 notified = true; |
| 176 }))); |
| 177 EXPECT_TRUE(b_watcher.IsWatching()); |
| 178 |
| 179 message_loop_.reset(); |
| 180 |
| 181 EXPECT_TRUE(notified); |
| 182 |
| 183 EXPECT_TRUE(b_watcher.IsWatching()); |
| 184 b_watcher.Cancel(); |
| 185 } |
| 186 |
| 160 TEST_F(WatcherTest, CloseAndCancel) { | 187 TEST_F(WatcherTest, CloseAndCancel) { |
| 161 ScopedMessagePipeHandle a, b; | 188 ScopedMessagePipeHandle a, b; |
| 162 CreateMessagePipe(nullptr, &a, &b); | 189 CreateMessagePipe(nullptr, &a, &b); |
| 163 | 190 |
| 164 Watcher b_watcher; | 191 Watcher b_watcher; |
| 165 EXPECT_EQ(MOJO_RESULT_OK, | 192 EXPECT_EQ(MOJO_RESULT_OK, |
| 166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, | 193 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 167 OnReady([](MojoResult result) { FAIL(); }))); | 194 OnReady([](MojoResult result) { FAIL(); }))); |
| 168 EXPECT_TRUE(b_watcher.IsWatching()); | 195 EXPECT_TRUE(b_watcher.IsWatching()); |
| 169 | 196 |
| 170 // This should trigger the watcher above... | 197 // This should trigger the watcher above... |
| 171 b.reset(); | 198 b.reset(); |
| 172 // ...but the watcher is cancelled first. | 199 // ...but the watcher is cancelled first. |
| 173 b_watcher.Cancel(); | 200 b_watcher.Cancel(); |
| 174 | 201 |
| 175 EXPECT_FALSE(b_watcher.IsWatching()); | 202 EXPECT_FALSE(b_watcher.IsWatching()); |
| 176 | 203 |
| 177 base::RunLoop().RunUntilIdle(); | 204 base::RunLoop().RunUntilIdle(); |
| 178 } | 205 } |
| 179 | 206 |
| 180 } // namespace | 207 } // namespace |
| 181 } // namespace mojo | 208 } // namespace mojo |
| OLD | NEW |