Home | History | Annotate | Download | only in js
      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.
      4 
      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"
     10 
     11 namespace mojo {
     12 namespace edk {
     13 namespace js {
     14 
     15 class HandleWrapperTest : public testing::Test,
     16                           public HandleCloseObserver {
     17  public:
     18   HandleWrapperTest() : closes_observed_(0) {}
     19 
     20   void OnWillCloseHandle() override { closes_observed_++; }
     21 
     22  protected:
     23   int closes_observed_;
     24 
     25  private:
     26   DISALLOW_COPY_AND_ASSIGN(HandleWrapperTest);
     27 };
     28 
     29 class TestHandleWrapper : public HandleWrapper {
     30  public:
     31   explicit TestHandleWrapper(MojoHandle handle) : HandleWrapper(handle) {}
     32 
     33  private:
     34   DISALLOW_COPY_AND_ASSIGN(TestHandleWrapper);
     35 };
     36 
     37 // Test that calling Close() on a HandleWrapper for an invalid handle does not
     38 // notify observers.
     39 TEST_F(HandleWrapperTest, CloseWithInvalidHandle) {
     40   {
     41     TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
     42     wrapper.AddCloseObserver(this);
     43     ASSERT_EQ(0, closes_observed_);
     44     wrapper.Close();
     45     EXPECT_EQ(0, closes_observed_);
     46   }
     47   EXPECT_EQ(0, closes_observed_);
     48 }
     49 
     50 // Test that destroying a HandleWrapper for an invalid handle does not notify
     51 // observers.
     52 TEST_F(HandleWrapperTest, DestroyWithInvalidHandle) {
     53   {
     54     TestHandleWrapper wrapper(MOJO_HANDLE_INVALID);
     55     wrapper.AddCloseObserver(this);
     56     ASSERT_EQ(0, closes_observed_);
     57   }
     58   EXPECT_EQ(0, closes_observed_);
     59 }
     60 
     61 // Test that calling Close on a HandleWrapper for a valid handle notifies
     62 // observers once.
     63 TEST_F(HandleWrapperTest, CloseWithValidHandle) {
     64   {
     65     mojo::MessagePipe pipe;
     66     TestHandleWrapper wrapper(pipe.handle0.release().value());
     67     wrapper.AddCloseObserver(this);
     68     ASSERT_EQ(0, closes_observed_);
     69     wrapper.Close();
     70     EXPECT_EQ(1, closes_observed_);
     71     // Check that calling close again doesn't notify observers.
     72     wrapper.Close();
     73     EXPECT_EQ(1, closes_observed_);
     74   }
     75   // Check that destroying a closed HandleWrapper doesn't notify observers.
     76   EXPECT_EQ(1, closes_observed_);
     77 }
     78 
     79 // Test that destroying a HandleWrapper for a valid handle notifies observers.
     80 TEST_F(HandleWrapperTest, DestroyWithValidHandle) {
     81   {
     82     mojo::MessagePipe pipe;
     83     TestHandleWrapper wrapper(pipe.handle0.release().value());
     84     wrapper.AddCloseObserver(this);
     85     ASSERT_EQ(0, closes_observed_);
     86   }
     87   EXPECT_EQ(1, closes_observed_);
     88 }
     89 
     90 }  // namespace js
     91 }  // namespace edk
     92 }  // namespace mojo
     93