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