1 // Copyright 2014 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.
5 #include "base/macros.h"
6 #include "mojo/edk/js/handle.h"
7 #include "mojo/edk/js/handle_close_observer.h"
8 #include "mojo/public/cpp/system/core.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 class HandleWrapperTest : public testing::Test,
15 public HandleCloseObserver {
17 HandleWrapperTest() : closes_observed_(0) {}
19 void OnWillCloseHandle() override { closes_observed_++; }
25 DISALLOW_COPY_AND_ASSIGN(HandleWrapperTest);
28 class TestHandleWrapper : public HandleWrapper {
30 explicit TestHandleWrapper(MojoHandle handle) : HandleWrapper(handle) {}
33 DISALLOW_COPY_AND_ASSIGN(TestHandleWrapper);
36 // Test that calling Close() on a HandleWrapper for an invalid handle does not
38 TEST_F(HandleWrapperTest, CloseWithInvalidHandle) {
40 TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
41 wrapper.AddCloseObserver(this);
42 ASSERT_EQ(0, closes_observed_);
44 EXPECT_EQ(0, closes_observed_);
46 EXPECT_EQ(0, closes_observed_);
49 // Test that destroying a HandleWrapper for an invalid handle does not notify
51 TEST_F(HandleWrapperTest, DestroyWithInvalidHandle) {
53 TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
54 wrapper.AddCloseObserver(this);
55 ASSERT_EQ(0, closes_observed_);
57 EXPECT_EQ(0, closes_observed_);
60 // Test that calling Close on a HandleWrapper for a valid handle notifies
62 TEST_F(HandleWrapperTest, CloseWithValidHandle) {
64 mojo::MessagePipe pipe;
65 TestHandleWrapper wrapper(pipe.handle0.release().value());
66 wrapper.AddCloseObserver(this);
67 ASSERT_EQ(0, closes_observed_);
69 EXPECT_EQ(1, closes_observed_);
70 // Check that calling close again doesn't notify observers.
72 EXPECT_EQ(1, closes_observed_);
74 // Check that destroying a closed HandleWrapper doesn't notify observers.
75 EXPECT_EQ(1, closes_observed_);
78 // Test that destroying a HandleWrapper for a valid handle notifies observers.
79 TEST_F(HandleWrapperTest, DestroyWithValidHandle) {
81 mojo::MessagePipe pipe;
82 TestHandleWrapper wrapper(pipe.handle0.release().value());
83 wrapper.AddCloseObserver(this);
84 ASSERT_EQ(0, closes_observed_);
86 EXPECT_EQ(1, closes_observed_);