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 "mojo/bindings/js/handle.h"
6 #include "mojo/bindings/js/handle_close_observer.h"
7 #include "mojo/public/cpp/system/core.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 class HandleWrapperTest : public testing::Test,
14 public gin::HandleCloseObserver {
16 HandleWrapperTest() : closes_observed_(0) {}
18 virtual void OnWillCloseHandle() OVERRIDE { closes_observed_++; }
24 DISALLOW_COPY_AND_ASSIGN(HandleWrapperTest);
27 class TestHandleWrapper : public gin::HandleWrapper {
29 explicit TestHandleWrapper(MojoHandle handle) : HandleWrapper(handle) {}
32 DISALLOW_COPY_AND_ASSIGN(TestHandleWrapper);
35 // Test that calling Close() on a HandleWrapper for an invalid handle does not
37 TEST_F(HandleWrapperTest, CloseWithInvalidHandle) {
39 TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
40 wrapper.AddCloseObserver(this);
41 ASSERT_EQ(0, closes_observed_);
43 EXPECT_EQ(0, closes_observed_);
45 EXPECT_EQ(0, closes_observed_);
48 // Test that destroying a HandleWrapper for an invalid handle does not notify
50 TEST_F(HandleWrapperTest, DestroyWithInvalidHandle) {
52 TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
53 wrapper.AddCloseObserver(this);
54 ASSERT_EQ(0, closes_observed_);
56 EXPECT_EQ(0, closes_observed_);
59 // Test that calling Close on a HandleWrapper for a valid handle notifies
61 TEST_F(HandleWrapperTest, CloseWithValidHandle) {
63 mojo::MessagePipe pipe;
64 TestHandleWrapper wrapper(pipe.handle0.release().value());
65 wrapper.AddCloseObserver(this);
66 ASSERT_EQ(0, closes_observed_);
68 EXPECT_EQ(1, closes_observed_);
69 // Check that calling close again doesn't notify observers.
71 EXPECT_EQ(1, closes_observed_);
73 // Check that destroying a closed HandleWrapper doesn't notify observers.
74 EXPECT_EQ(1, closes_observed_);
77 // Test that destroying a HandleWrapper for a valid handle notifies observers.
78 TEST_F(HandleWrapperTest, DestroyWithValidHandle) {
80 mojo::MessagePipe pipe;
81 TestHandleWrapper wrapper(pipe.handle0.release().value());
82 wrapper.AddCloseObserver(this);
83 ASSERT_EQ(0, closes_observed_);
85 EXPECT_EQ(1, closes_observed_);