summaryrefslogtreecommitdiffstats
path: root/mojo/edk/js/handle_unittest.cc
blob: dd2562fa6378d73656d95164d422e47246d87511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/macros.h"
#include "mojo/edk/js/handle.h"
#include "mojo/edk/js/handle_close_observer.h"
#include "mojo/public/cpp/system/core.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace edk {
namespace js {

class HandleWrapperTest : public testing::Test,
                          public HandleCloseObserver {
 public:
  HandleWrapperTest() : closes_observed_(0) {}

  void OnWillCloseHandle() override { closes_observed_++; }

 protected:
  int closes_observed_;

 private:
  DISALLOW_COPY_AND_ASSIGN(HandleWrapperTest);
};

class TestHandleWrapper : public HandleWrapper {
 public:
  explicit TestHandleWrapper(MojoHandle handle) : HandleWrapper(handle) {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestHandleWrapper);
};

// Test that calling Close() on a HandleWrapper for an invalid handle does not
// notify observers.
TEST_F(HandleWrapperTest, CloseWithInvalidHandle) {
  {
    TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
    wrapper.AddCloseObserver(this);
    ASSERT_EQ(0, closes_observed_);
    wrapper.Close();
    EXPECT_EQ(0, closes_observed_);
  }
  EXPECT_EQ(0, closes_observed_);
}

// Test that destroying a HandleWrapper for an invalid handle does not notify
// observers.
TEST_F(HandleWrapperTest, DestroyWithInvalidHandle) {
  {
    TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
    wrapper.AddCloseObserver(this);
    ASSERT_EQ(0, closes_observed_);
  }
  EXPECT_EQ(0, closes_observed_);
}

// Test that calling Close on a HandleWrapper for a valid handle notifies
// observers once.
TEST_F(HandleWrapperTest, CloseWithValidHandle) {
  {
    mojo::MessagePipe pipe;
    TestHandleWrapper wrapper(pipe.handle0.release().value());
    wrapper.AddCloseObserver(this);
    ASSERT_EQ(0, closes_observed_);
    wrapper.Close();
    EXPECT_EQ(1, closes_observed_);
    // Check that calling close again doesn't notify observers.
    wrapper.Close();
    EXPECT_EQ(1, closes_observed_);
  }
  // Check that destroying a closed HandleWrapper doesn't notify observers.
  EXPECT_EQ(1, closes_observed_);
}

// Test that destroying a HandleWrapper for a valid handle notifies observers.
TEST_F(HandleWrapperTest, DestroyWithValidHandle) {
  {
    mojo::MessagePipe pipe;
    TestHandleWrapper wrapper(pipe.handle0.release().value());
    wrapper.AddCloseObserver(this);
    ASSERT_EQ(0, closes_observed_);
  }
  EXPECT_EQ(1, closes_observed_);
}

}  // namespace js
}  // namespace edk
}  // namespace mojo