TestMultiMgrs.cpp (6776B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* 8 * Test a chain of managers, ensuring ownership is maintained correctly. 9 */ 10 11 #include "gtest/gtest.h" 12 13 #include "mozilla/_ipdltest/IPDLUnitTest.h" 14 #include "mozilla/_ipdltest/PTestMultiMgrsChild.h" 15 #include "mozilla/_ipdltest/PTestMultiMgrsParent.h" 16 #include "mozilla/_ipdltest/PTestMultiMgrsLeftChild.h" 17 #include "mozilla/_ipdltest/PTestMultiMgrsLeftParent.h" 18 #include "mozilla/_ipdltest/PTestMultiMgrsRightChild.h" 19 #include "mozilla/_ipdltest/PTestMultiMgrsRightParent.h" 20 #include "mozilla/_ipdltest/PTestMultiMgrsBottomChild.h" 21 #include "mozilla/_ipdltest/PTestMultiMgrsBottomParent.h" 22 23 #include <algorithm> 24 25 using namespace mozilla::ipc; 26 27 namespace mozilla::_ipdltest { 28 29 class TestMultiMgrsBottomParent : public PTestMultiMgrsBottomParent { 30 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsBottomParent, override) 31 private: 32 ~TestMultiMgrsBottomParent() = default; 33 }; 34 35 class TestMultiMgrsBottomChild : public PTestMultiMgrsBottomChild { 36 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsBottomChild, override) 37 private: 38 ~TestMultiMgrsBottomChild() = default; 39 }; 40 41 class TestMultiMgrsChild : public PTestMultiMgrsChild { 42 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsChild, override) 43 public: 44 PTestMultiMgrsBottomChild* mBottomL = nullptr; 45 PTestMultiMgrsBottomChild* mBottomR = nullptr; 46 47 private: 48 already_AddRefed<PTestMultiMgrsLeftChild> AllocPTestMultiMgrsLeftChild() 49 final override; 50 51 already_AddRefed<PTestMultiMgrsRightChild> AllocPTestMultiMgrsRightChild() 52 final override; 53 54 IPCResult RecvCheck() final override; 55 56 ~TestMultiMgrsChild() = default; 57 }; 58 59 class TestMultiMgrsLeftParent : public PTestMultiMgrsLeftParent { 60 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsLeftParent, override) 61 public: 62 bool HasChild(PTestMultiMgrsBottomParent* c) { 63 const auto& managed = ManagedPTestMultiMgrsBottomParent(); 64 return std::find(managed.begin(), managed.end(), c) != managed.end(); 65 } 66 67 private: 68 ~TestMultiMgrsLeftParent() = default; 69 }; 70 71 class TestMultiMgrsLeftChild : public PTestMultiMgrsLeftChild { 72 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsLeftChild, override) 73 public: 74 bool HasChild(PTestMultiMgrsBottomChild* c) { 75 const auto& managed = ManagedPTestMultiMgrsBottomChild(); 76 return std::find(managed.begin(), managed.end(), c) != managed.end(); 77 } 78 79 private: 80 already_AddRefed<PTestMultiMgrsBottomChild> AllocPTestMultiMgrsBottomChild() 81 final override { 82 return MakeAndAddRef<TestMultiMgrsBottomChild>(); 83 } 84 85 IPCResult RecvPTestMultiMgrsBottomConstructor( 86 PTestMultiMgrsBottomChild* actor) final override { 87 static_cast<TestMultiMgrsChild*>(Manager())->mBottomL = actor; 88 return IPC_OK(); 89 } 90 91 ~TestMultiMgrsLeftChild() = default; 92 }; 93 94 class TestMultiMgrsRightParent : public PTestMultiMgrsRightParent { 95 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsRightParent, override) 96 public: 97 bool HasChild(PTestMultiMgrsBottomParent* c) { 98 const auto& managed = ManagedPTestMultiMgrsBottomParent(); 99 return std::find(managed.begin(), managed.end(), c) != managed.end(); 100 } 101 102 private: 103 ~TestMultiMgrsRightParent() = default; 104 }; 105 106 class TestMultiMgrsRightChild : public PTestMultiMgrsRightChild { 107 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsRightChild, override) 108 public: 109 bool HasChild(PTestMultiMgrsBottomChild* c) { 110 const auto& managed = ManagedPTestMultiMgrsBottomChild(); 111 return std::find(managed.begin(), managed.end(), c) != managed.end(); 112 } 113 114 private: 115 already_AddRefed<PTestMultiMgrsBottomChild> AllocPTestMultiMgrsBottomChild() 116 final override { 117 return MakeAndAddRef<TestMultiMgrsBottomChild>(); 118 } 119 120 IPCResult RecvPTestMultiMgrsBottomConstructor( 121 PTestMultiMgrsBottomChild* actor) final override { 122 static_cast<TestMultiMgrsChild*>(Manager())->mBottomR = actor; 123 return IPC_OK(); 124 } 125 126 ~TestMultiMgrsRightChild() = default; 127 }; 128 129 class TestMultiMgrsParent : public PTestMultiMgrsParent { 130 NS_INLINE_DECL_REFCOUNTING(TestMultiMgrsParent, override) 131 private: 132 IPCResult RecvOK() final override { 133 Close(); 134 return IPC_OK(); 135 } 136 137 ~TestMultiMgrsParent() = default; 138 }; 139 140 already_AddRefed<PTestMultiMgrsLeftChild> 141 TestMultiMgrsChild::AllocPTestMultiMgrsLeftChild() { 142 return MakeAndAddRef<TestMultiMgrsLeftChild>(); 143 } 144 145 already_AddRefed<PTestMultiMgrsRightChild> 146 TestMultiMgrsChild::AllocPTestMultiMgrsRightChild() { 147 return MakeAndAddRef<TestMultiMgrsRightChild>(); 148 } 149 150 IPCResult TestMultiMgrsChild::RecvCheck() { 151 EXPECT_EQ(ManagedPTestMultiMgrsLeftChild().Count(), (uint32_t)1) 152 << "where's leftie?"; 153 EXPECT_EQ(ManagedPTestMultiMgrsRightChild().Count(), (uint32_t)1) 154 << "where's rightie?"; 155 156 TestMultiMgrsLeftChild* leftie = static_cast<TestMultiMgrsLeftChild*>( 157 LoneManagedOrNullAsserts(ManagedPTestMultiMgrsLeftChild())); 158 TestMultiMgrsRightChild* rightie = static_cast<TestMultiMgrsRightChild*>( 159 LoneManagedOrNullAsserts(ManagedPTestMultiMgrsRightChild())); 160 161 EXPECT_TRUE(leftie->HasChild(mBottomL)) 162 << "leftie didn't have a child it was supposed to!"; 163 EXPECT_FALSE(leftie->HasChild(mBottomR)) << "leftie had rightie's child!"; 164 165 EXPECT_TRUE(rightie->HasChild(mBottomR)) 166 << "rightie didn't have a child it was supposed to!"; 167 EXPECT_FALSE(rightie->HasChild(mBottomL)) << "rightie had leftie's child!"; 168 169 EXPECT_TRUE(SendOK()) << "couldn't send OK()"; 170 171 return IPC_OK(); 172 } 173 174 IPDL_TEST(TestMultiMgrs) { 175 auto leftie = MakeRefPtr<TestMultiMgrsLeftParent>(); 176 EXPECT_TRUE(mActor->SendPTestMultiMgrsLeftConstructor(leftie)) 177 << "error sending ctor"; 178 179 auto rightie = MakeRefPtr<TestMultiMgrsRightParent>(); 180 EXPECT_TRUE(mActor->SendPTestMultiMgrsRightConstructor(rightie)) 181 << "error sending ctor"; 182 183 auto bottomL = MakeRefPtr<TestMultiMgrsBottomParent>(); 184 EXPECT_TRUE(leftie->SendPTestMultiMgrsBottomConstructor(bottomL)) 185 << "error sending ctor"; 186 187 auto bottomR = MakeRefPtr<TestMultiMgrsBottomParent>(); 188 EXPECT_TRUE(rightie->SendPTestMultiMgrsBottomConstructor(bottomR)) 189 << "error sending ctor"; 190 191 EXPECT_TRUE(leftie->HasChild(bottomL)) 192 << "leftie didn't have a child it was supposed to!"; 193 EXPECT_FALSE(leftie->HasChild(bottomR)) << "leftie had rightie's child!"; 194 195 EXPECT_TRUE(rightie->HasChild(bottomR)) 196 << "rightie didn't have a child it was supposed to!"; 197 EXPECT_FALSE(rightie->HasChild(bottomL)) << "rightie had rightie's child!"; 198 199 EXPECT_TRUE(mActor->SendCheck()) << "couldn't kick off the child-side check"; 200 } 201 202 } // namespace mozilla::_ipdltest