• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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/system/scope_to_message_pipe.h"
6 #include "base/bind.h"
7 #include "base/callback.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "base/test/bind_test_util.h"
11 #include "base/test/scoped_task_environment.h"
12 #include "mojo/public/cpp/system/message_pipe.h"
13 #include "mojo/public/cpp/system/simple_watcher.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace mojo {
17 namespace {
18 
19 class RunCallbackOnDestruction {
20  public:
RunCallbackOnDestruction(base::OnceClosure destruction_callback)21   explicit RunCallbackOnDestruction(base::OnceClosure destruction_callback)
22       : destruction_callback_(std::move(destruction_callback)) {}
~RunCallbackOnDestruction()23   ~RunCallbackOnDestruction() { std::move(destruction_callback_).Run(); }
24 
25  private:
26   base::OnceClosure destruction_callback_;
27 
28   DISALLOW_COPY_AND_ASSIGN(RunCallbackOnDestruction);
29 };
30 
31 class ScopeToMessagePipeTest : public testing::Test {
32  public:
33   ScopeToMessagePipeTest() = default;
34   ~ScopeToMessagePipeTest() override = default;
35 
36  private:
37   base::test::ScopedTaskEnvironment task_environment_;
38   DISALLOW_COPY_AND_ASSIGN(ScopeToMessagePipeTest);
39 };
40 
TEST_F(ScopeToMessagePipeTest,ObjectDestroyedOnPeerClosure)41 TEST_F(ScopeToMessagePipeTest, ObjectDestroyedOnPeerClosure) {
42   base::RunLoop wait_for_destruction;
43   MessagePipe pipe;
44   ScopeToMessagePipe(std::make_unique<RunCallbackOnDestruction>(
45                          wait_for_destruction.QuitClosure()),
46                      std::move(pipe.handle0));
47   pipe.handle1.reset();
48   wait_for_destruction.Run();
49 }
50 
TEST_F(ScopeToMessagePipeTest,PipeClosedOnPeerClosure)51 TEST_F(ScopeToMessagePipeTest, PipeClosedOnPeerClosure) {
52   base::RunLoop wait_for_pipe_closure;
53   MessagePipe pipe;
54   SimpleWatcher watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC);
55   watcher.Watch(pipe.handle1.get(), MOJO_HANDLE_SIGNAL_READABLE,
56                 MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
57                 base::BindLambdaForTesting(
58                     [&](MojoResult result, const HandleSignalsState& state) {
59                       EXPECT_EQ(result, MOJO_RESULT_CANCELLED);
60                       wait_for_pipe_closure.Quit();
61                     }));
62 
63   ScopeToMessagePipe(42, std::move(pipe.handle0));
64   pipe.handle1.reset();
65   wait_for_pipe_closure.Run();
66 }
67 
68 }  // namespace
69 }  // namespace mojo
70