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