OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/system/handle_table.h" | 5 #include "mojo/edk/system/handle_table.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "mojo/edk/system/dispatcher.h" | 9 #include "mojo/edk/system/dispatcher.h" |
10 #include "mojo/edk/system/handle.h" | 10 #include "mojo/edk/system/handle.h" |
11 #include "mojo/edk/system/mock_simple_dispatcher.h" | 11 #include "mojo/edk/system/mock_simple_dispatcher.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using mojo::util::MakeRefCounted; | 14 using mojo::util::MakeRefCounted; |
15 using mojo::util::RefPtr; | 15 using mojo::util::RefPtr; |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace system { | 18 namespace system { |
19 namespace { | 19 namespace { |
20 | 20 |
21 TEST(HandleTableTest, Basic) { | 21 TEST(HandleTableTest, Basic) { |
22 HandleTable ht(1000u); | 22 HandleTable ht(1000u); |
23 | 23 |
24 RefPtr<Dispatcher> d = MakeRefCounted<test::MockSimpleDispatcher>(); | 24 RefPtr<Dispatcher> d = MakeRefCounted<test::MockSimpleDispatcher>(); |
25 | 25 |
26 MojoHandle h = ht.AddHandle(Handle(d.Clone(), MOJO_HANDLE_RIGHT_NONE)); | 26 MojoHandle hv = ht.AddHandle(Handle(d.Clone(), MOJO_HANDLE_RIGHT_NONE)); |
27 ASSERT_NE(h, MOJO_HANDLE_INVALID); | 27 ASSERT_NE(hv, MOJO_HANDLE_INVALID); |
28 | 28 |
29 // Save the pointer value (without taking a ref), so we can check that we get | 29 // Save the pointer value (without taking a ref), so we can check that we get |
30 // the same object back. | 30 // the same object back. |
31 Dispatcher* dv = d.get(); | 31 Dispatcher* dv = d.get(); |
32 // Reset this, to make sure that the handle table takes a ref. | 32 // Reset this, to make sure that the handle table takes a ref. |
33 d = nullptr; | 33 d = nullptr; |
34 | 34 |
35 EXPECT_EQ(MOJO_RESULT_OK, ht.GetDispatcher(h, &d)); | 35 Handle h; |
36 EXPECT_EQ(d.get(), dv); | 36 EXPECT_EQ(MOJO_RESULT_OK, ht.GetHandle(hv, &h)); |
| 37 EXPECT_EQ(dv, h.dispatcher.get()); |
37 | 38 |
38 d = nullptr; | 39 d = nullptr; |
39 ASSERT_EQ(MOJO_RESULT_OK, ht.GetAndRemoveDispatcher(h, &d)); | 40 ASSERT_EQ(MOJO_RESULT_OK, ht.GetAndRemoveDispatcher(hv, &d)); |
40 ASSERT_EQ(d.get(), dv); | 41 ASSERT_EQ(dv, d.get()); |
41 | 42 |
42 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 43 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
43 | 44 |
44 // We removed |h|, so it should no longer be valid. | 45 // We removed |hv|, so it should no longer be valid. |
45 d = nullptr; | 46 h.reset(); |
46 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, ht.GetDispatcher(h, &d)); | 47 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, ht.GetHandle(hv, &h)); |
47 } | 48 } |
48 | 49 |
49 TEST(HandleTableTest, AddHandlePair) { | 50 TEST(HandleTableTest, AddHandlePair) { |
50 HandleTable ht(1000u); | 51 HandleTable ht(1000u); |
51 | 52 |
52 auto d1 = MakeRefCounted<test::MockSimpleDispatcher>(); | 53 auto d1 = MakeRefCounted<test::MockSimpleDispatcher>(); |
53 auto d2 = MakeRefCounted<test::MockSimpleDispatcher>(); | 54 auto d2 = MakeRefCounted<test::MockSimpleDispatcher>(); |
54 | 55 |
55 auto hp = ht.AddHandlePair(Handle(d1.Clone(), MOJO_HANDLE_RIGHT_NONE), | 56 auto hp = ht.AddHandlePair(Handle(d1.Clone(), MOJO_HANDLE_RIGHT_NONE), |
56 Handle(d2.Clone(), MOJO_HANDLE_RIGHT_NONE)); | 57 Handle(d2.Clone(), MOJO_HANDLE_RIGHT_NONE)); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 ASSERT_EQ(dispatchers[i], d) << i; | 241 ASSERT_EQ(dispatchers[i], d) << i; |
241 } | 242 } |
242 | 243 |
243 for (size_t i = 0u; i < kNumHandles; i++) | 244 for (size_t i = 0u; i < kNumHandles; i++) |
244 EXPECT_EQ(MOJO_RESULT_OK, dispatchers[i]->Close()) << i; | 245 EXPECT_EQ(MOJO_RESULT_OK, dispatchers[i]->Close()) << i; |
245 } | 246 } |
246 | 247 |
247 } // namespace | 248 } // namespace |
248 } // namespace system | 249 } // namespace system |
249 } // namespace mojo | 250 } // namespace mojo |
OLD | NEW |