1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include <meta/api/iteration.h>
20 #include <meta/api/make_callback.h>
21 #include <meta/base/ref_uri.h>
22 #include <meta/interface/intf_attachment.h>
23 #include <meta/interface/intf_metadata.h>
24 #include <meta/interface/intf_object_flags.h>
25 #include <meta/interface/intf_required_interfaces.h>
26 
27 #include "src/test_utils.h"
28 #include "src/testing_objects.h"
29 #include "src/util.h"
30 #include "src/test_runner.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 META_BEGIN_NAMESPACE()
36 
operator ==(const ChildChangedInfo& lhs, const ChildChangedInfo& rhs)37 bool operator==(const ChildChangedInfo& lhs, const ChildChangedInfo& rhs)
38 {
39     return lhs.object == rhs.object && lhs.index == rhs.index && lhs.parent.lock() == rhs.parent.lock();
40 }
41 
operator ==(const ChildMovedInfo& lhs, const ChildMovedInfo& rhs)42 bool operator==(const ChildMovedInfo& lhs, const ChildMovedInfo& rhs)
43 {
44     return lhs.object == rhs.object && lhs.from == rhs.from && lhs.to == rhs.to &&
45            lhs.parent.lock() == rhs.parent.lock();
46 }
47 
48 class ContainerTestBase : public testing::Test {
49 public:
50     virtual IContainer::Ptr CreateContainer(BASE_NS::string name) = 0;
51 
52     void SetUp() override
53     {
54         container_ = CreateContainer("Base");
55         ASSERT_NE(container_, nullptr);
56         container_->Add(CreateTestType<IObject>("Object1_1"));
57         container_->Add(CreateTestType<IObject>("SameNameDifferentUid"));
58         container_->Add(CreateTestType<IObject>("ObjectDupe"));
59         container1_1_ = CreateContainer("Container1_1");
60         container_->Add(interface_pointer_cast<IObject>(container1_1_));
61         container1_1_->Add(CreateTestType<IObject>("Object2_1"));
62         container1_1_->Add(CreateTestType<IObject>("ObjectDupe"));
63         container1_1_->Add(CreateTestType<IObject>("SameNameDifferentUid"));
64         container_->Add(CreateTestType<IObject>("Object1_3"));
65         container2_1_ = CreateContainer("SameNameDifferentUid");
66         container1_1_->Add(interface_pointer_cast<IObject>(container2_1_));
67 
68         ASSERT_EQ(container_->GetSize(), NumDirectChildren);
69 
70         container_->OnAdded()->AddHandler(MakeCallback<IOnChildChanged>(this, &ContainerTestBase::OnAdded));
71         container_->OnRemoved()->AddHandler(MakeCallback<IOnChildChanged>(this, &ContainerTestBase::OnRemoved));
72         container_->OnMoved()->AddHandler(MakeCallback<IOnChildMoved>(this, &ContainerTestBase::OnMoved));
73     }
74     void TearDown() override
75     {
76         container_.reset();
77     }
78 
SetUpTestSuite()79     static void SetUpTestSuite()
80     {
81         SetTest();
82     }
TearDownTestSuite()83     static void TearDownTestSuite()
84     {
85         ResetTest();
86     }
87 
88 protected:
OnAdded(const ChildChangedInfo& info)89     void OnAdded(const ChildChangedInfo& info)
90     {
91         addedCalls_.push_back(info);
92     }
OnRemoved(const ChildChangedInfo& info)93     void OnRemoved(const ChildChangedInfo& info)
94     {
95         removedCalls_.push_back(info);
96     }
OnMoved(const ChildMovedInfo& info)97     void OnMoved(const ChildMovedInfo& info)
98     {
99         movedCalls_.push_back(info);
100     }
101 
102     BASE_NS::vector<ChildChangedInfo> addedCalls_;
103     BASE_NS::vector<ChildChangedInfo> removedCalls_;
104     BASE_NS::vector<ChildMovedInfo> movedCalls_;
105 
106     static constexpr size_t NumDirectChildContainers = 1;
107     static constexpr size_t NumDirectChildTestTypes = 4;
108     static constexpr size_t NumDirectChildren = NumDirectChildContainers + NumDirectChildTestTypes;
109     static constexpr size_t NumChildContainers = 2;
110     static constexpr size_t NumChildTestTypes = 7;
111     IContainer::Ptr container_;
112     IContainer::Ptr container1_1_;
113     IContainer::Ptr container2_1_;
114 };
115 
116 class ContainerCommonTest : public ContainerTestBase, public ::testing::WithParamInterface<ClassInfo> {
117 public:
118     IContainer::Ptr CreateContainer(BASE_NS::string name) override
119     {
120         return CreateTestContainer<IContainer>(name, GetParam());
121     }
122 };
123 
124 class ContainerTest : public ContainerTestBase {
125 public:
126     IContainer::Ptr CreateContainer(BASE_NS::string name) override
127     {
128         return CreateTestContainer<IContainer>(name, META_NS::ClassId::TestContainer);
129     }
130 };
131 
132 class FlatContainerTest : public ContainerTestBase {
133 public:
134     IContainer::Ptr CreateContainer(BASE_NS::string name) override
135     {
136         return CreateTestContainer<IContainer>(name, META_NS::ClassId::TestFlatContainer);
137     }
138 };
139 
140 /**
141  * @tc.name: ContainerCommonTest
142  * @tc.desc: test IsAncestorOf function
143  * @tc.type: FUNC
144  * @tc.require: I7DMS1
145  */
HWTEST_P(ContainerCommonTest, IsAncestorOf, TestSize.Level1)146 HWTEST_P(ContainerCommonTest, IsAncestorOf, TestSize.Level1)
147 {
148     auto object = container1_1_->FindAnyFromHierarchy<IObject>("Object2_1");
149     ASSERT_NE(object, nullptr);
150     EXPECT_TRUE(container_->IsAncestorOf(object));
151     EXPECT_FALSE(container_->IsAncestorOf(nullptr));
152 
153     auto object2 = CreateTestType<IObject>("NotThere");
154     ASSERT_NE(object2, nullptr);
155     EXPECT_FALSE(container_->IsAncestorOf(object2));
156 
157     auto container2 = CreateTestContainer<IContainer>("Container2");
158     ASSERT_NE(container2, nullptr);
159     EXPECT_FALSE(container2->IsAncestorOf(object2));
160     EXPECT_TRUE(container2->Add(object2));
161     EXPECT_TRUE(container2->IsAncestorOf(object2));
162 }
163 
164 /**
165  * @tc.name: ContainerCommonTest
166  * @tc.desc: test AddChild function
167  * @tc.type: FUNC
168  * @tc.require: I7DMS1
169  */
HWTEST_P(ContainerCommonTest, AddChild, TestSize.Level1)170 HWTEST_P(ContainerCommonTest, AddChild, TestSize.Level1)
171 {
172     const auto object = interface_pointer_cast<IObject>(CreateTestType());
173     EXPECT_TRUE(container_->Add(object));
174     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
175 
176     ASSERT_THAT(addedCalls_, SizeIs(1));
177     ASSERT_THAT(removedCalls_, SizeIs(0));
178     ASSERT_THAT(movedCalls_, SizeIs(0));
179     auto expected = ChildChangedInfo { object, NumDirectChildren, container_ };
180     EXPECT_EQ(addedCalls_[0], expected);
181 }
182 
183 /**
184  * @tc.name: ContainerCommonTest
185  * @tc.desc: test Insert function
186  * @tc.type: FUNC
187  * @tc.require: I7DMS1
188  */
HWTEST_P(ContainerCommonTest, Insert, TestSize.Level1)189 HWTEST_P(ContainerCommonTest, Insert, TestSize.Level1)
190 {
191     auto all = container_->GetAll();
192     auto expectedSize = NumDirectChildren;
193     const auto item1 = interface_pointer_cast<IObject>(CreateTestType());
194     const auto item2 = interface_pointer_cast<IObject>(CreateTestType());
195     const auto item3 = interface_pointer_cast<IObject>(CreateTestType());
196 
197     EXPECT_TRUE(container_->Insert(all.size(), item1));
198     all = container_->GetAll();
199     EXPECT_EQ(all.size(), ++expectedSize);
200     EXPECT_EQ(all.back(), item1);
201 
202     EXPECT_TRUE(container_->Insert(0, item2));
203     all = container_->GetAll();
204     EXPECT_EQ(all.size(), ++expectedSize);
205     EXPECT_EQ(all.front(), item2);
206 
207     EXPECT_TRUE(container_->Insert(all.size() + 10, item3));
208     all = container_->GetAll();
209     EXPECT_EQ(all.size(), ++expectedSize);
210     EXPECT_EQ(all.back(), item3);
211 
212     ASSERT_THAT(addedCalls_, SizeIs(3));
213     ASSERT_THAT(removedCalls_, SizeIs(0));
214     ASSERT_THAT(movedCalls_, SizeIs(0));
215     auto expected1 = ChildChangedInfo { item1, NumDirectChildren, container_ };
216     auto expected2 = ChildChangedInfo { item2, 0, container_ };
217     auto expected3 = ChildChangedInfo { item3, expectedSize - 1, container_ };
218     EXPECT_THAT(addedCalls_, ElementsAre(expected1, expected2, expected3));
219 }
220 
221 /**
222  * @tc.name: ContainerCommonTest
223  * @tc.desc: test GetAt function
224  * @tc.type: FUNC
225  * @tc.require: I7DMS1
226  */
HWTEST_P(ContainerCommonTest, GetAt, TestSize.Level1)227 HWTEST_P(ContainerCommonTest, GetAt, TestSize.Level1)
228 {
229     const auto all = container_->GetAll();
230     ASSERT_EQ(all.size(), NumDirectChildren);
231 
232     EXPECT_EQ(all.front(), container_->GetAt(0));
233     EXPECT_EQ(all.back(), container_->GetAt(NumDirectChildren - 1));
234     EXPECT_EQ(all[NumDirectChildren / 2], container_->GetAt(NumDirectChildren / 2));
235     EXPECT_EQ(container_->GetAt(NumDirectChildren + 10), nullptr);
236     EXPECT_THAT(addedCalls_, SizeIs(0));
237     EXPECT_THAT(removedCalls_, SizeIs(0));
238     EXPECT_THAT(movedCalls_, SizeIs(0));
239 }
240 
241 /**
242  * @tc.name: ContainerCommonTest
243  * @tc.desc: test RemoveChild function
244  * @tc.type: FUNC
245  * @tc.require: I7DMS1
246  */
HWTEST_P(ContainerCommonTest, RemoveChild, TestSize.Level1)247 HWTEST_P(ContainerCommonTest, RemoveChild, TestSize.Level1)
248 {
249     auto children = container_->GetAll();
250     auto expectedCount = NumDirectChildren;
251     ASSERT_EQ(children.size(), expectedCount);
252 
253     BASE_NS::vector<ChildChangedInfo> removed;
254 
255     auto baseIndex = expectedCount / 3;
256 
257     while (!children.empty()) {
258         auto index = ++baseIndex % expectedCount;
259         const auto child = children[index];
260         removed.push_back({ child, index, container_ });
261         EXPECT_TRUE(container_->Remove(child));
262 
263         expectedCount--;
264         children = container_->GetAll();
265 
266         EXPECT_THAT(children, Not(Contains(child)));
267         ASSERT_EQ(children.size(), expectedCount);
268     }
269 
270     ASSERT_THAT(addedCalls_, SizeIs(0));
271     ASSERT_THAT(removedCalls_, SizeIs(NumDirectChildren));
272     ASSERT_THAT(movedCalls_, SizeIs(0));
273     EXPECT_THAT(removedCalls_, ElementsAreArray(removed));
274 }
275 
276 /**
277  * @tc.name: ContainerCommonTest
278  * @tc.desc: test RemoveIndex function
279  * @tc.type: FUNC
280  * @tc.require: I7DMS1
281  */
HWTEST_P(ContainerCommonTest, RemoveIndex, TestSize.Level1)282 HWTEST_P(ContainerCommonTest, RemoveIndex, TestSize.Level1)
283 {
284     auto children = container_->GetAll();
285     auto expectedCount = NumDirectChildren;
286     ASSERT_EQ(children.size(), expectedCount);
287 
288     BASE_NS::vector<ChildChangedInfo> removed;
289 
290     auto baseIndex = expectedCount / 3;
291 
292     while (!children.empty()) {
293         auto index = ++baseIndex % expectedCount;
294         const auto child = children[index];
295         removed.push_back({ child, index, container_ });
296         EXPECT_TRUE(container_->Remove(index));
297 
298         expectedCount--;
299         children = container_->GetAll();
300 
301         EXPECT_THAT(children, Not(Contains(child)));
302         ASSERT_EQ(children.size(), expectedCount);
303     }
304 
305     ASSERT_THAT(addedCalls_, SizeIs(0));
306     ASSERT_THAT(removedCalls_, SizeIs(NumDirectChildren));
307     ASSERT_THAT(movedCalls_, SizeIs(0));
308     EXPECT_THAT(removedCalls_, ElementsAreArray(removed));
309 }
310 
311 /**
312  * @tc.name: ContainerCommonTest
313  * @tc.desc: test RemoveChildInvalid function
314  * @tc.type: FUNC
315  * @tc.require: I7DMS1
316  */
HWTEST_P(ContainerCommonTest, RemoveChildInvalid, TestSize.Level1)317 HWTEST_P(ContainerCommonTest, RemoveChildInvalid, TestSize.Level1)
318 {
319     const auto children = container_->GetAll();
320     const auto expectedCount = NumDirectChildren;
321     ASSERT_EQ(children.size(), expectedCount);
322 
323     const auto invalid = interface_pointer_cast<IObject>(CreateTestType());
324     EXPECT_FALSE(container_->Remove(invalid));
325     EXPECT_EQ(children.size(), expectedCount);
326     EXPECT_THAT(children, Not(Contains(invalid)));
327     EXPECT_FALSE(container_->Remove(nullptr));
328     EXPECT_EQ(children.size(), expectedCount);
329     EXPECT_THAT(children, Not(Contains(invalid)));
330     EXPECT_THAT(addedCalls_, SizeIs(0));
331     EXPECT_THAT(removedCalls_, SizeIs(0));
332     EXPECT_THAT(movedCalls_, SizeIs(0));
333 }
334 
335 /**
336  * @tc.name: ContainerCommonTest
337  * @tc.desc: test RemoveAll function
338  * @tc.type: FUNC
339  * @tc.require: I7DMS1
340  */
HWTEST_P(ContainerCommonTest, RemoveAll, TestSize.Level1)341 HWTEST_P(ContainerCommonTest, RemoveAll, TestSize.Level1)
342 {
343     container_->RemoveAll();
344     EXPECT_EQ(container_->GetSize(), 0);
345     EXPECT_THAT(container_->GetAll(), IsEmpty());
346     EXPECT_THAT(addedCalls_, SizeIs(0));
347     EXPECT_THAT(removedCalls_, SizeIs(NumDirectChildren));
348     EXPECT_THAT(movedCalls_, SizeIs(0));
349 }
350 
351 /**
352  * @tc.name: ContainerCommonTest
353  * @tc.desc: test Replace function
354  * @tc.type: FUNC
355  * @tc.require: I7DMS1
356  */
HWTEST_P(ContainerCommonTest, Replace, TestSize.Level1)357 HWTEST_P(ContainerCommonTest, Replace, TestSize.Level1)
358 {
359     const auto children = container_->GetAll();
360     const auto expectedCount = NumDirectChildren;
361     ASSERT_EQ(children.size(), expectedCount);
362 
363     auto index = expectedCount / 2;
364     const auto replace = children[index];
365     const auto replaceWith = interface_pointer_cast<IObject>(CreateTestType("Replaced"));
366 
367     EXPECT_TRUE(container_->Replace(replace, replaceWith, false));
368     EXPECT_EQ(container_->GetSize(), expectedCount);
369     EXPECT_THAT(container_->GetAll(), Contains(replaceWith));
370     EXPECT_THAT(container_->GetAll(), Not(Contains(replace)));
371     EXPECT_THAT(movedCalls_, SizeIs(0));
372     const auto removed = ChildChangedInfo { replace, index, container_ };
373     const auto added = ChildChangedInfo { replaceWith, index, container_ };
374     EXPECT_THAT(addedCalls_, ElementsAre(added));
375     EXPECT_THAT(removedCalls_, ElementsAre(removed));
376 }
377 
378 /**
379  * @tc.name: ContainerCommonTest
380  * @tc.desc: test ReplaceSame function
381  * @tc.type: FUNC
382  * @tc.require: I7DMS1
383  */
HWTEST_P(ContainerCommonTest, ReplaceSame, TestSize.Level1)384 HWTEST_P(ContainerCommonTest, ReplaceSame, TestSize.Level1)
385 {
386     const auto children = container_->GetAll();
387     const auto expectedCount = NumDirectChildren;
388     ASSERT_EQ(children.size(), expectedCount);
389 
390     const auto notThere = interface_pointer_cast<IObject>(CreateTestType("NotThere"));
391     const auto replace = children[expectedCount / 2];
392     const auto replaceWith = children[expectedCount / 2];
393 
394     ASSERT_EQ(replace, replaceWith);
395 
396     EXPECT_TRUE(container_->Replace(replace, replaceWith));
397     EXPECT_EQ(container_->GetSize(), expectedCount);
398     EXPECT_THAT(container_->GetAll(), Contains(replaceWith));
399     EXPECT_THAT(container_->GetAll(), Contains(replace));
400 
401     EXPECT_FALSE(container_->Replace(notThere, notThere));
402     EXPECT_EQ(container_->GetSize(), expectedCount);
403     EXPECT_THAT(container_->GetAll(), Not(Contains(notThere)));
404 
405     EXPECT_THAT(addedCalls_, SizeIs(0));
406     EXPECT_THAT(removedCalls_, SizeIs(0));
407     EXPECT_THAT(movedCalls_, SizeIs(0));
408 
409     EXPECT_TRUE(container_->Replace(notThere, notThere, true));
410     EXPECT_EQ(container_->GetSize(), expectedCount + 1);
411     EXPECT_THAT(container_->GetAll(), Contains(notThere));
412 
413     EXPECT_THAT(removedCalls_, SizeIs(0));
414     EXPECT_THAT(movedCalls_, SizeIs(0));
415     const auto added = ChildChangedInfo { notThere, expectedCount, container_ };
416     EXPECT_THAT(addedCalls_, ElementsAre(added));
417 }
418 
419 /**
420  * @tc.name: ContainerCommonTest
421  * @tc.desc: test ReplaceNull function
422  * @tc.type: FUNC
423  * @tc.require: I7DMS1
424  */
HWTEST_P(ContainerCommonTest, ReplaceNull, TestSize.Level1)425 HWTEST_P(ContainerCommonTest, ReplaceNull, TestSize.Level1)
426 {
427     const auto children = container_->GetAll();
428     ASSERT_EQ(children.size(), NumDirectChildren);
429     const auto replaceWith = interface_pointer_cast<IObject>(CreateTestType("Replaced"));
430     const auto replaceWith2 = interface_pointer_cast<IObject>(CreateTestType("Replaced2"));
431 
432     EXPECT_FALSE(container_->Replace({}, {}, false));
433     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
434     EXPECT_FALSE(container_->Replace({}, {}, true));
435     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
436     EXPECT_THAT(addedCalls_, SizeIs(0));
437     EXPECT_THAT(removedCalls_, SizeIs(0));
438     EXPECT_THAT(movedCalls_, SizeIs(0));
439     EXPECT_FALSE(container_->Replace({}, replaceWith, false));
440     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
441     EXPECT_THAT(addedCalls_, SizeIs(0));
442     EXPECT_THAT(removedCalls_, SizeIs(0));
443     EXPECT_THAT(movedCalls_, SizeIs(0));
444     EXPECT_TRUE(container_->Replace({}, replaceWith, true));
445     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
446     EXPECT_FALSE(container_->Replace({}, replaceWith2, false));
447     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
448     EXPECT_THAT(removedCalls_, SizeIs(0));
449     EXPECT_THAT(movedCalls_, SizeIs(0));
450     const auto added = ChildChangedInfo { replaceWith, NumDirectChildren, container_ };
451     EXPECT_THAT(addedCalls_, ElementsAre(added));
452     addedCalls_.clear();
453     EXPECT_TRUE(container_->Replace(replaceWith, {}, false));
454     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
455     EXPECT_TRUE(container_->Replace({}, replaceWith2, true));
456     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
457     EXPECT_TRUE(container_->Replace(replaceWith2, {}, true));
458     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
459     const auto removed1 = ChildChangedInfo { replaceWith, NumDirectChildren, container_ };
460     const auto removed2 = ChildChangedInfo { replaceWith2, NumDirectChildren, container_ };
461     const auto added2 = ChildChangedInfo { replaceWith2, NumDirectChildren, container_ };
462     EXPECT_THAT(removedCalls_, ElementsAre(removed1, removed2));
463     EXPECT_THAT(movedCalls_, SizeIs(0));
464     EXPECT_THAT(addedCalls_, ElementsAre(added2));
465 }
466 
467 /**
468  * @tc.name: ContainerCommonTest
469  * @tc.desc: test ReplaceAdd function
470  * @tc.type: FUNC
471  * @tc.require: I7DMS1
472  */
HWTEST_P(ContainerCommonTest, ReplaceAdd, TestSize.Level1)473 HWTEST_P(ContainerCommonTest, ReplaceAdd, TestSize.Level1)
474 {
475     const auto children = container_->GetAll();
476     const auto expectedCount = NumDirectChildren;
477     ASSERT_EQ(children.size(), expectedCount);
478 
479     const auto replace = interface_pointer_cast<IObject>(CreateTestType("NotThere"));
480     const auto replaceWith = interface_pointer_cast<IObject>(CreateTestType("Replaced"));
481 
482     EXPECT_FALSE(container_->Replace(replace, replaceWith, false));
483     EXPECT_EQ(container_->GetSize(), expectedCount);
484     EXPECT_THAT(container_->GetAll(), Not(Contains(replaceWith)));
485     EXPECT_THAT(container_->GetAll(), Not(Contains(replace)));
486     EXPECT_THAT(addedCalls_, SizeIs(0));
487     EXPECT_THAT(removedCalls_, SizeIs(0));
488     EXPECT_THAT(movedCalls_, SizeIs(0));
489 
490     EXPECT_TRUE(container_->Replace(replace, replaceWith, true));
491     EXPECT_EQ(container_->GetSize(), expectedCount + 1);
492     EXPECT_THAT(container_->GetAll(), Contains(replaceWith));
493     EXPECT_THAT(container_->GetAll(), Not(Contains(replace)));
494     EXPECT_THAT(removedCalls_, SizeIs(0));
495     EXPECT_THAT(movedCalls_, SizeIs(0));
496     const auto added = ChildChangedInfo { replaceWith, expectedCount, container_ };
497     EXPECT_THAT(addedCalls_, ElementsAre(added));
498 }
499 
500 /**
501  * @tc.name: ContainerCommonTest
502  * @tc.desc: test MoveEmpty function
503  * @tc.type: FUNC
504  * @tc.require: I7DMS1
505  */
HWTEST_P(ContainerCommonTest, MoveEmpty, TestSize.Level1)506 HWTEST_P(ContainerCommonTest, MoveEmpty, TestSize.Level1)
507 {
508     auto moveItem = container_->GetAt(0);
509     container_->RemoveAll();
510     removedCalls_.clear();
511     EXPECT_FALSE(container_->Move(1, 1));
512     EXPECT_FALSE(container_->Move(0, 0));
513     EXPECT_FALSE(container_->Move(0, 1));
514     EXPECT_FALSE(container_->Move(moveItem, 1));
515     EXPECT_FALSE(container_->Move(moveItem, 0));
516 
517     EXPECT_THAT(addedCalls_, SizeIs(0));
518     EXPECT_THAT(removedCalls_, SizeIs(0));
519     EXPECT_THAT(movedCalls_, SizeIs(0));
520 }
521 
522 /**
523  * @tc.name: ContainerCommonTest
524  * @tc.desc: test MoveBack function
525  * @tc.type: FUNC
526  * @tc.require: I7DMS1
527  */
HWTEST_P(ContainerCommonTest, MoveBack, TestSize.Level1)528 HWTEST_P(ContainerCommonTest, MoveBack, TestSize.Level1)
529 {
530     size_t from = NumDirectChildren - 1;
531     size_t to = 0;
532     auto moveItem = container_->GetAt(from);
533     auto moved = ChildMovedInfo { moveItem, from, to, container_ };
534     EXPECT_TRUE(container_->Move(from, to));
535     EXPECT_EQ(container_->GetAt(to), moveItem);
536     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
537     EXPECT_THAT(addedCalls_, SizeIs(0));
538     EXPECT_THAT(removedCalls_, SizeIs(0));
539     EXPECT_THAT(movedCalls_, ElementsAre(moved));
540 }
541 
542 /**
543  * @tc.name: ContainerCommonTest
544  * @tc.desc: test MoveForward function
545  * @tc.type: FUNC
546  * @tc.require: I7DMS1
547  */
HWTEST_P(ContainerCommonTest, MoveForward, TestSize.Level1)548 HWTEST_P(ContainerCommonTest, MoveForward, TestSize.Level1)
549 {
550     size_t from = 0;
551     size_t to = NumDirectChildren - 1;
552     auto moveItem = container_->GetAt(from);
553     auto moved = ChildMovedInfo { moveItem, from, to, container_ };
554     EXPECT_TRUE(container_->Move(from, to));
555 
556     EXPECT_EQ(container_->GetAt(to), moveItem);
557     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
558     EXPECT_THAT(movedCalls_, ElementsAre(moved));
559 }
560 
561 /**
562  * @tc.name: ContainerCommonTest
563  * @tc.desc: test MoveNext function
564  * @tc.type: FUNC
565  * @tc.require: I7DMS1
566  */
HWTEST_P(ContainerCommonTest, MoveNext, TestSize.Level1)567 HWTEST_P(ContainerCommonTest, MoveNext, TestSize.Level1)
568 {
569     size_t from = NumDirectChildren / 2;
570     size_t to = from + 1;
571     auto moveItem = container_->GetAt(from);
572     auto moved = ChildMovedInfo { moveItem, from, to, container_ };
573     EXPECT_TRUE(container_->Move(from, to));
574     EXPECT_EQ(container_->GetAt(to), moveItem);
575     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
576     EXPECT_THAT(movedCalls_, ElementsAre(moved));
577 }
578 
579 /**
580  * @tc.name: ContainerCommonTest
581  * @tc.desc: test MoveSame function
582  * @tc.type: FUNC
583  * @tc.require: I7DMS1
584  */
HWTEST_P(ContainerCommonTest, MoveSame, TestSize.Level1)585 HWTEST_P(ContainerCommonTest, MoveSame, TestSize.Level1)
586 {
587     size_t from = NumDirectChildren / 2;
588     size_t to = from;
589     auto moveItem = container_->GetAt(from);
590     EXPECT_TRUE(container_->Move(from, to));
591     EXPECT_EQ(container_->GetAt(to), moveItem);
592     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
593     EXPECT_THAT(addedCalls_, SizeIs(0));
594     EXPECT_THAT(removedCalls_, SizeIs(0));
595     EXPECT_THAT(movedCalls_, SizeIs(0));
596 }
597 
598 /**
599  * @tc.name: ContainerCommonTest
600  * @tc.desc: test MoveFromBiggerThanSize function
601  * @tc.type: FUNC
602  * @tc.require: I7DMS1
603  */
HWTEST_P(ContainerCommonTest, MoveFromBiggerThanSize, TestSize.Level1)604 HWTEST_P(ContainerCommonTest, MoveFromBiggerThanSize, TestSize.Level1)
605 {
606     size_t from = NumDirectChildren + 10;
607     size_t to = 0;
608     auto moveItem = container_->GetAt(NumDirectChildren - 1);
609     EXPECT_TRUE(container_->Move(from, to));
610     EXPECT_EQ(container_->GetAt(to), moveItem);
611     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
612     EXPECT_THAT(addedCalls_, SizeIs(0));
613     EXPECT_THAT(removedCalls_, SizeIs(0));
614     auto moved = ChildMovedInfo { moveItem, NumDirectChildren - 1, to, container_ };
615     EXPECT_THAT(movedCalls_, ElementsAre(moved));
616 }
617 
618 /**
619  * @tc.name: ContainerCommonTest
620  * @tc.desc: test MoveToBiggerThanSize function
621  * @tc.type: FUNC
622  * @tc.require: I7DMS1
623  */
HWTEST_P(ContainerCommonTest, MoveToBiggerThanSize, TestSize.Level1)624 HWTEST_P(ContainerCommonTest, MoveToBiggerThanSize, TestSize.Level1)
625 {
626     size_t from = NumDirectChildren / 2;
627     size_t to = NumDirectChildren + 10;
628     auto moveItem = container_->GetAt(from);
629     EXPECT_TRUE(container_->Move(from, to));
630     EXPECT_EQ(container_->GetAt(NumDirectChildren - 1), moveItem);
631     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
632     EXPECT_THAT(addedCalls_, SizeIs(0));
633     EXPECT_THAT(removedCalls_, SizeIs(0));
634     auto moved = ChildMovedInfo { moveItem, from, NumDirectChildren - 1, container_ };
635     EXPECT_THAT(movedCalls_, ElementsAre(moved));
636 }
637 
638 /**
639  * @tc.name: ContainerCommonTest
640  * @tc.desc: test MoveFromToBiggerThanSize function
641  * @tc.type: FUNC
642  * @tc.require: I7DMS1
643  */
HWTEST_P(ContainerCommonTest, MoveFromToBiggerThanSize, TestSize.Level1)644 HWTEST_P(ContainerCommonTest, MoveFromToBiggerThanSize, TestSize.Level1)
645 {
646     size_t from = NumDirectChildren + 10;
647     size_t to = NumDirectChildren + 4;
648     auto moveItem = container_->GetAt(NumDirectChildren - 1);
649     EXPECT_TRUE(container_->Move(from, to));
650     EXPECT_EQ(container_->GetAt(NumDirectChildren - 1), moveItem);
651     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
652     EXPECT_THAT(addedCalls_, SizeIs(0));
653     EXPECT_THAT(removedCalls_, SizeIs(0));
654     EXPECT_THAT(movedCalls_, SizeIs(0));
655 }
656 
657 /**
658  * @tc.name: ContainerCommonTest
659  * @tc.desc: test MoveObject function
660  * @tc.type: FUNC
661  * @tc.require: I7DMS1
662  */
HWTEST_P(ContainerCommonTest, MoveObject, TestSize.Level1)663 HWTEST_P(ContainerCommonTest, MoveObject, TestSize.Level1)
664 {
665     size_t from = NumDirectChildren - 1;
666     size_t to = 0;
667     const auto child = container_->GetAt(from);
668     auto moved = ChildMovedInfo { child, from, to, container_ };
669     EXPECT_TRUE(container_->Move(child, to));
670     EXPECT_EQ(container_->GetAt(to), child);
671     EXPECT_EQ(container_->GetSize(), NumDirectChildren);
672     EXPECT_THAT(addedCalls_, SizeIs(0));
673     EXPECT_THAT(removedCalls_, SizeIs(0));
674     EXPECT_THAT(movedCalls_, ElementsAre(moved));
675 }
676 
677 /**
678  * @tc.name: ContainerCommonTest
679  * @tc.desc: test FindAllNameDirect function
680  * @tc.type: FUNC
681  * @tc.require: I7DMS1
682  */
HWTEST_P(ContainerCommonTest, FindAllNameDirect, TestSize.Level1)683 HWTEST_P(ContainerCommonTest, FindAllNameDirect, TestSize.Level1)
684 {
685     auto result1 = container_->FindAll({ "Object1_1", TraversalType::NO_HIERARCHY, {}, false });
686     auto result2 = container_->FindAll({ "Object2_1", TraversalType::NO_HIERARCHY, {}, false });
687 
688     EXPECT_THAT(result1, SizeIs(1));
689     EXPECT_THAT(result2, SizeIs(0));
690     EXPECT_THAT(addedCalls_, SizeIs(0));
691     EXPECT_THAT(removedCalls_, SizeIs(0));
692     EXPECT_THAT(movedCalls_, SizeIs(0));
693 }
694 
695 /**
696  * @tc.name: ContainerCommonTest
697  * @tc.desc: test SetRequiredInterfacesReplace function
698  * @tc.type: FUNC
699  * @tc.require: I7DMS1
700  */
HWTEST_P(ContainerCommonTest, SetRequiredInterfacesReplace, TestSize.Level1)701 HWTEST_P(ContainerCommonTest, SetRequiredInterfacesReplace, TestSize.Level1)
702 {
703     auto req = interface_cast<IRequiredInterfaces>(container_);
704     ASSERT_TRUE(req);
705     EXPECT_TRUE(req->SetRequiredInterfaces({ ITestType::UID }));
706     EXPECT_EQ(container_->GetSize(), NumDirectChildTestTypes);
707 
708     const auto children = container_->GetAll();
709     const auto expectedCount = NumDirectChildTestTypes;
710     ASSERT_EQ(children.size(), expectedCount);
711     const auto replace = children[expectedCount / 2];
712 
713     const auto replaceWithItem = interface_pointer_cast<IObject>(CreateTestType("Replaced"));
714     const auto replaceWithContainer = interface_pointer_cast<IObject>(CreateTestContainer("Replaced"));
715 
716     EXPECT_FALSE(container_->Replace(replace, replaceWithContainer, false));
717     EXPECT_EQ(container_->GetSize(), expectedCount);
718     EXPECT_THAT(container_->GetAll(), Not(Contains(replaceWithContainer)));
719     EXPECT_THAT(container_->GetAll(), Contains(replace));
720 
721     EXPECT_FALSE(container_->Replace(replace, replaceWithContainer, true));
722     EXPECT_EQ(container_->GetSize(), expectedCount);
723     EXPECT_THAT(container_->GetAll(), Not(Contains(replaceWithContainer)));
724     EXPECT_THAT(container_->GetAll(), Contains(replace));
725 
726     EXPECT_TRUE(container_->Replace(replace, replaceWithItem, false));
727     EXPECT_EQ(container_->GetSize(), expectedCount);
728     EXPECT_THAT(container_->GetAll(), Contains(replaceWithItem));
729     EXPECT_THAT(container_->GetAll(), Not(Contains(replace)));
730 }
731 
732 /**
733  * @tc.name: ContainerCommonTest
734  * @tc.desc: test FindAnyNameDirect function
735  * @tc.type: FUNC
736  * @tc.require: I7DMS1
737  */
HWTEST_P(ContainerCommonTest, FindAnyNameDirect, TestSize.Level1)738 HWTEST_P(ContainerCommonTest, FindAnyNameDirect, TestSize.Level1)
739 {
740     auto result1 = container_->FindAny({ "Object1_1", TraversalType::NO_HIERARCHY, {}, false });
741     auto result2 = container_->FindAny({ "Object2_1", TraversalType::NO_HIERARCHY, {}, false });
742 
743     EXPECT_NE(result1, nullptr);
744     EXPECT_EQ(result2, nullptr);
745     EXPECT_THAT(addedCalls_, SizeIs(0));
746     EXPECT_THAT(removedCalls_, SizeIs(0));
747     EXPECT_THAT(movedCalls_, SizeIs(0));
748 }
749 
750 /**
751  * @tc.name: ContainerCommonTest
752  * @tc.desc: test SetRequiredInterfaces function
753  * @tc.type: FUNC
754  * @tc.require: I7DMS1
755  */
HWTEST_P(ContainerCommonTest, SetRequiredInterfaces, TestSize.Level1)756 HWTEST_P(ContainerCommonTest, SetRequiredInterfaces, TestSize.Level1)
757 {
758     auto req = interface_cast<IRequiredInterfaces>(container_);
759     ASSERT_TRUE(req);
760     EXPECT_EQ(container_->GetSize(), NumDirectChildContainers + NumDirectChildTestTypes);
761     EXPECT_TRUE(req->SetRequiredInterfaces({ ITestType::UID }));
762     EXPECT_EQ(container_->GetSize(), NumDirectChildTestTypes);
763     EXPECT_TRUE(req->SetRequiredInterfaces({ ITestContainer::UID }));
764     EXPECT_EQ(container_->GetSize(), 0);
765     EXPECT_FALSE(container_->Add(interface_pointer_cast<IObject>(CreateTestType())));
766     EXPECT_EQ(container_->GetSize(), 0);
767     const auto container = interface_pointer_cast<IObject>(CreateTestContainer());
768     EXPECT_TRUE(container_->Add(container));
769     EXPECT_EQ(container_->GetSize(), 1);
770     const auto all = container_->GetAll();
771     ASSERT_THAT(all, SizeIs(1));
772     EXPECT_EQ(all[0], container);
773 }
774 
775 /**
776  * @tc.name: ContainerTest
777  * @tc.desc: test FailAddLoop function
778  * @tc.type: FUNC
779  * @tc.require: I7DMS1
780  */
HWTEST_F(ContainerTest, FailAddLoop, TestSize.Level1)781 HWTEST_F(ContainerTest, FailAddLoop, TestSize.Level1)
782 {
783     EXPECT_FALSE(container2_1_->Add(container_));
784     EXPECT_THAT(addedCalls_, SizeIs(0));
785     EXPECT_THAT(removedCalls_, SizeIs(0));
786     EXPECT_THAT(movedCalls_, SizeIs(0));
787 }
788 
789 /**
790  * @tc.name: ContainerTest
791  * @tc.desc: test FailInsertLoop function
792  * @tc.type: FUNC
793  * @tc.require: I7DMS1
794  */
HWTEST_F(ContainerTest, FailInsertLoop, TestSize.Level1)795 HWTEST_F(ContainerTest, FailInsertLoop, TestSize.Level1)
796 {
797     EXPECT_FALSE(container2_1_->Insert(0, container_));
798     EXPECT_THAT(addedCalls_, SizeIs(0));
799     EXPECT_THAT(removedCalls_, SizeIs(0));
800     EXPECT_THAT(movedCalls_, SizeIs(0));
801 }
802 
803 /**
804  * @tc.name: ContainerTest
805  * @tc.desc: test FailReplaceLoop function
806  * @tc.type: FUNC
807  * @tc.require: I7DMS1
808  */
HWTEST_F(ContainerTest, FailReplaceLoop, TestSize.Level1)809 HWTEST_F(ContainerTest, FailReplaceLoop, TestSize.Level1)
810 {
811     EXPECT_FALSE(container1_1_->Replace(container1_1_->GetAll()[1], container_));
812     EXPECT_THAT(addedCalls_, SizeIs(0));
813     EXPECT_THAT(removedCalls_, SizeIs(0));
814     EXPECT_THAT(movedCalls_, SizeIs(0));
815 }
816 
817 /**
818  * @tc.name: ContainerTest
819  * @tc.desc: test ReplaceNullWithExisting function
820  * @tc.type: FUNC
821  * @tc.require: I7DMS1
822  */
HWTEST_F(ContainerTest, ReplaceNullWithExisting, TestSize.Level1)823 HWTEST_F(ContainerTest, ReplaceNullWithExisting, TestSize.Level1)
824 {
825     const auto children = container_->GetAll();
826     const auto expectedCount = NumDirectChildren;
827     ASSERT_EQ(children.size(), expectedCount);
828 
829     auto indexReplace = expectedCount / 2;
830     const auto replaceWith = children[indexReplace];
831 
832     EXPECT_FALSE(container_->Replace({}, replaceWith, true));
833     EXPECT_EQ(container_->GetSize(), expectedCount);
834 
835     EXPECT_THAT(movedCalls_, SizeIs(0));
836     EXPECT_THAT(addedCalls_, SizeIs(0));
837     EXPECT_THAT(removedCalls_, SizeIs(0));
838 }
839 
840 /**
841  * @tc.name: ContainerTest
842  * @tc.desc: test ReplaceWithExistingAddAlways function
843  * @tc.type: FUNC
844  * @tc.require: I7DMS1
845  */
HWTEST_F(ContainerTest, ReplaceWithExistingAddAlways, TestSize.Level1)846 HWTEST_F(ContainerTest, ReplaceWithExistingAddAlways, TestSize.Level1)
847 {
848     const auto children = container_->GetAll();
849     const auto expectedCount = NumDirectChildren;
850     ASSERT_EQ(children.size(), expectedCount);
851 
852     auto indexReplace = expectedCount / 2;
853     auto indexReplaceWith = indexReplace + 1;
854     const auto replace = children[indexReplace];
855     const auto replaceWith = children[indexReplaceWith];
856 
857     EXPECT_TRUE(container_->Replace(replace, replaceWith, false));
858     EXPECT_EQ(container_->GetSize(), expectedCount - 1);
859 
860     const auto moved = ChildMovedInfo { replaceWith, indexReplaceWith, indexReplace, container_ };
861     EXPECT_THAT(movedCalls_, ElementsAre(moved));
862     EXPECT_THAT(addedCalls_, SizeIs(0));
863     const auto removed = ChildChangedInfo { replace, indexReplace, container_ };
864     EXPECT_THAT(removedCalls_, ElementsAre(removed));
865 }
866 
867 /**
868  * @tc.name: ContainerTest
869  * @tc.desc: test ReplaceWithExistingDontAddAlways function
870  * @tc.type: FUNC
871  * @tc.require: I7DMS1
872  */
HWTEST_F(ContainerTest, ReplaceWithExistingDontAddAlways, TestSize.Level1)873 HWTEST_F(ContainerTest, ReplaceWithExistingDontAddAlways, TestSize.Level1)
874 {
875     const auto children = container_->GetAll();
876     const auto expectedCount = NumDirectChildren;
877     ASSERT_EQ(children.size(), expectedCount);
878 
879     auto indexReplace = expectedCount / 2;
880     auto indexReplaceWith = indexReplace + 1;
881     const auto replace = children[indexReplace];
882     const auto replaceWith = children[indexReplaceWith];
883 
884     EXPECT_TRUE(container_->Replace(replace, replaceWith, true));
885     EXPECT_EQ(container_->GetSize(), expectedCount - 1);
886 
887     const auto moved = ChildMovedInfo { replaceWith, indexReplaceWith, indexReplace, container_ };
888     EXPECT_THAT(movedCalls_, ElementsAre(moved));
889     EXPECT_THAT(addedCalls_, SizeIs(0));
890     const auto removed = ChildChangedInfo { replace, indexReplace, container_ };
891     EXPECT_THAT(removedCalls_, ElementsAre(removed));
892 }
893 
894 /**
895  * @tc.name: ContainerTest
896  * @tc.desc: test AddChildTwice function
897  * @tc.type: FUNC
898  * @tc.require: I7DMS1
899  */
HWTEST_F(ContainerTest, AddChildTwice, TestSize.Level1)900 HWTEST_F(ContainerTest, AddChildTwice, TestSize.Level1)
901 {
902     const auto child = interface_pointer_cast<IObject>(CreateTestType("Twice"));
903     EXPECT_TRUE(container_->Add(child));
904     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
905 
906     EXPECT_TRUE(container_->Add(child));
907     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
908 
909     const auto children = container_->FindAll({ "Twice", TraversalType::NO_HIERARCHY });
910     ASSERT_THAT(children, SizeIs(1));
911     EXPECT_EQ(children[0], child);
912 
913     ASSERT_THAT(addedCalls_, SizeIs(1));
914     ASSERT_THAT(removedCalls_, SizeIs(0));
915     ASSERT_THAT(movedCalls_, SizeIs(0));
916     auto expected = ChildChangedInfo { child, NumDirectChildren, container_ };
917     EXPECT_EQ(addedCalls_[0], expected);
918 }
919 
920 /**
921  * @tc.name: ContainerTest
922  * @tc.desc: test FindAllEmptyName function
923  * @tc.type: FUNC
924  * @tc.require: I7DMS1
925  */
HWTEST_F(ContainerTest, FindAllEmptyName, TestSize.Level1)926 HWTEST_F(ContainerTest, FindAllEmptyName, TestSize.Level1)
927 {
928     auto result1 = container_->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
929     auto result2 = container_->FindAll({ "", TraversalType::NO_HIERARCHY, { ITestContainer::UID }, false });
930     auto result3 = container_->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
931     auto result4 = container_->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID }, false });
932 
933     EXPECT_THAT(result1, SizeIs(NumChildContainers + NumChildTestTypes));
934     EXPECT_THAT(result2, SizeIs(1));
935     EXPECT_THAT(result3, SizeIs(NumChildContainers));
936     EXPECT_THAT(result4, SizeIs(NumChildTestTypes));
937     EXPECT_THAT(addedCalls_, SizeIs(0));
938     EXPECT_THAT(removedCalls_, SizeIs(0));
939     EXPECT_THAT(movedCalls_, SizeIs(0));
940 }
941 
942 /**
943  * @tc.name: ContainerTest
944  * @tc.desc: test FindAllNameRecursive function
945  * @tc.type: FUNC
946  * @tc.require: I7DMS1
947  */
HWTEST_F(ContainerTest, FindAllNameRecursive, TestSize.Level1)948 HWTEST_F(ContainerTest, FindAllNameRecursive, TestSize.Level1)
949 {
950     auto result1 = container_->FindAll({ "Object1_1", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
951     auto result2 = container_->FindAll({ "Object2_1", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
952 
953     EXPECT_THAT(result1, SizeIs(1));
954     EXPECT_THAT(result2, SizeIs(1));
955     EXPECT_THAT(addedCalls_, SizeIs(0));
956     EXPECT_THAT(removedCalls_, SizeIs(0));
957     EXPECT_THAT(movedCalls_, SizeIs(0));
958 }
959 
960 /**
961  * @tc.name: ContainerTest
962  * @tc.desc: test FindAllNameDuplicate function
963  * @tc.type: FUNC
964  * @tc.require: I7DMS1
965  */
HWTEST_F(ContainerTest, FindAllNameDuplicate, TestSize.Level1)966 HWTEST_F(ContainerTest, FindAllNameDuplicate, TestSize.Level1)
967 {
968     auto result1 = container_->FindAll({ "ObjectDupe", TraversalType::NO_HIERARCHY, {}, false });
969     auto result2 = container_->FindAll({ "ObjectDupe", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
970 
971     EXPECT_THAT(result1, SizeIs(1));
972     EXPECT_THAT(result2, SizeIs(2));
973     EXPECT_THAT(addedCalls_, SizeIs(0));
974     EXPECT_THAT(removedCalls_, SizeIs(0));
975     EXPECT_THAT(movedCalls_, SizeIs(0));
976 }
977 
978 /**
979  * @tc.name: ContainerTest
980  * @tc.desc: test FindAllUid function
981  * @tc.type: FUNC
982  * @tc.require: I7DMS1
983  */
HWTEST_F(ContainerTest, FindAllUid, TestSize.Level1)984 HWTEST_F(ContainerTest, FindAllUid, TestSize.Level1)
985 {
986     auto result1 = container_->FindAll({ "", TraversalType::NO_HIERARCHY, { ITestContainer::UID }, false });
987     auto result2 = container_->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
988     auto result3 = container_->FindAll({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
989     auto result4 = container_->FindAll(
990         { "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
991 
992     EXPECT_THAT(result1, SizeIs(1));
993     EXPECT_THAT(result2, SizeIs(2));
994     EXPECT_THAT(result3, SizeIs(3));
995     EXPECT_THAT(result4, SizeIs(1));
996     EXPECT_THAT(addedCalls_, SizeIs(0));
997     EXPECT_THAT(removedCalls_, SizeIs(0));
998     EXPECT_THAT(movedCalls_, SizeIs(0));
999 }
1000 
1001 /**
1002  * @tc.name: ContainerTest
1003  * @tc.desc: test FindAllUidStrict function
1004  * @tc.type: FUNC
1005  * @tc.require: I7DMS1
1006  */
HWTEST_F(ContainerTest, FindAllUidStrict, TestSize.Level1)1007 HWTEST_F(ContainerTest, FindAllUidStrict, TestSize.Level1)
1008 {
1009     auto result1 = container_->FindAll({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER,
1010         { ITestType::UID, ITestContainer::UID }, true });
1011     auto result2 =
1012         container_->FindAll({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID }, true });
1013     auto result3 = container_->FindAll({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER,
1014         { ITestType::UID, ITestContainer::UID }, false });
1015     EXPECT_THAT(result1, SizeIs(0));
1016     EXPECT_THAT(result2, SizeIs(2));
1017     EXPECT_THAT(result3, SizeIs(3));
1018     EXPECT_THAT(addedCalls_, SizeIs(0));
1019     EXPECT_THAT(removedCalls_, SizeIs(0));
1020     EXPECT_THAT(movedCalls_, SizeIs(0));
1021 }
1022 
1023 /**
1024  * @tc.name: ContainerTest
1025  * @tc.desc: test FindAllInvalid function
1026  * @tc.type: FUNC
1027  * @tc.require: I7DMS1
1028  */
HWTEST_F(ContainerTest, FindAllInvalid, TestSize.Level1)1029 HWTEST_F(ContainerTest, FindAllInvalid, TestSize.Level1)
1030 {
1031     auto result1 = container_->FindAll({ "InvalidObject", TraversalType::NO_HIERARCHY, {}, false });
1032     auto result2 = container_->FindAll({ "InvalidObject", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1033     auto result3 =
1034         container_->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { META_NS::IAttachment::UID }, false });
1035     auto result4 = container_->FindAll(
1036         { "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID, META_NS::IAttachment::UID }, true });
1037 
1038     EXPECT_THAT(result1, SizeIs(0));
1039     EXPECT_THAT(result2, SizeIs(0));
1040     EXPECT_THAT(result3, SizeIs(0));
1041     EXPECT_THAT(result4, SizeIs(0));
1042     EXPECT_THAT(addedCalls_, SizeIs(0));
1043     EXPECT_THAT(removedCalls_, SizeIs(0));
1044     EXPECT_THAT(movedCalls_, SizeIs(0));
1045 }
1046 
1047 /**
1048  * @tc.name: ContainerTest
1049  * @tc.desc: test FindAnyEmptyName function
1050  * @tc.type: FUNC
1051  * @tc.require: I7DMS1
1052  */
HWTEST_F(ContainerTest, FindAnyEmptyName, TestSize.Level1)1053 HWTEST_F(ContainerTest, FindAnyEmptyName, TestSize.Level1)
1054 {
1055     auto result1 = container_->FindAny({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1056     auto result2 = container_->FindAny({ "", TraversalType::NO_HIERARCHY, { ITestContainer::UID }, false });
1057     auto result3 = container_->FindAny({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
1058 
1059     EXPECT_NE(result1, nullptr);
1060     EXPECT_NE(result2, nullptr);
1061     EXPECT_NE(result3, nullptr);
1062     EXPECT_THAT(addedCalls_, SizeIs(0));
1063     EXPECT_THAT(removedCalls_, SizeIs(0));
1064     EXPECT_THAT(movedCalls_, SizeIs(0));
1065 }
1066 
1067 /**
1068  * @tc.name: ContainerTest
1069  * @tc.desc: test FindAnyNameRecursive function
1070  * @tc.type: FUNC
1071  * @tc.require: I7DMS1
1072  */
HWTEST_F(ContainerTest, FindAnyNameRecursive, TestSize.Level1)1073 HWTEST_F(ContainerTest, FindAnyNameRecursive, TestSize.Level1)
1074 {
1075     auto result1 = container_->FindAny({ "Object1_1", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1076     auto result2 = container_->FindAny({ "Object2_1", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1077     auto result3 = container_->FindAnyFromHierarchy<IObject>("Object2_1");
1078 
1079     EXPECT_NE(result1, nullptr);
1080     EXPECT_NE(result2, nullptr);
1081     EXPECT_EQ(result2, result3);
1082     EXPECT_THAT(addedCalls_, SizeIs(0));
1083     EXPECT_THAT(removedCalls_, SizeIs(0));
1084     EXPECT_THAT(movedCalls_, SizeIs(0));
1085 }
1086 
1087 /**
1088  * @tc.name: ContainerTest
1089  * @tc.desc: test FindAnyNameDuplicate function
1090  * @tc.type: FUNC
1091  * @tc.require: I7DMS1
1092  */
HWTEST_F(ContainerTest, FindAnyNameDuplicate, TestSize.Level1)1093 HWTEST_F(ContainerTest, FindAnyNameDuplicate, TestSize.Level1)
1094 {
1095     auto result1 = container_->FindAny({ "ObjectDupe", TraversalType::NO_HIERARCHY, {}, false });
1096     auto result2 = container_->FindAny({ "ObjectDupe", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1097 
1098     EXPECT_NE(result1, nullptr);
1099     EXPECT_NE(result2, nullptr);
1100     EXPECT_THAT(addedCalls_, SizeIs(0));
1101     EXPECT_THAT(removedCalls_, SizeIs(0));
1102     EXPECT_THAT(movedCalls_, SizeIs(0));
1103 }
1104 
1105 /**
1106  * @tc.name: ContainerTest
1107  * @tc.desc: test FindAnyUid function
1108  * @tc.type: FUNC
1109  * @tc.require: I7DMS1
1110  */
HWTEST_F(ContainerTest, FindAnyUid, TestSize.Level1)1111 HWTEST_F(ContainerTest, FindAnyUid, TestSize.Level1)
1112 {
1113     auto result1 = container_->FindAny({ "", TraversalType::NO_HIERARCHY, { ITestContainer::UID }, false });
1114     auto result2 = container_->FindAny({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
1115     auto result3 = container_->FindAny({ "SameNameDifferentUid", TraversalType::NO_HIERARCHY, {}, false });
1116     auto result4 = container_->FindAny(
1117         { "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestContainer::UID }, false });
1118 
1119     EXPECT_NE(result1, nullptr);
1120     EXPECT_NE(result2, nullptr);
1121     EXPECT_NE(result3, nullptr);
1122     EXPECT_NE(result4, nullptr);
1123     EXPECT_THAT(addedCalls_, SizeIs(0));
1124     EXPECT_THAT(removedCalls_, SizeIs(0));
1125     EXPECT_THAT(movedCalls_, SizeIs(0));
1126 }
1127 
1128 /**
1129  * @tc.name: ContainerTest
1130  * @tc.desc: test FindAnyUidStrict function
1131  * @tc.type: FUNC
1132  * @tc.require: I7DMS1
1133  */
HWTEST_F(ContainerTest, FindAnyUidStrict, TestSize.Level1)1134 HWTEST_F(ContainerTest, FindAnyUidStrict, TestSize.Level1)
1135 {
1136     auto result1 = container_->FindAny({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER,
1137         { ITestType::UID, ITestContainer::UID }, true });
1138     auto result2 =
1139         container_->FindAny({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID }, true });
1140     auto result3 = container_->FindAny({ "SameNameDifferentUid", TraversalType::DEPTH_FIRST_PRE_ORDER,
1141         { ITestType::UID, ITestContainer::UID }, false });
1142     EXPECT_EQ(result1, nullptr);
1143     EXPECT_NE(result2, nullptr);
1144     EXPECT_NE(result3, nullptr);
1145     EXPECT_THAT(addedCalls_, SizeIs(0));
1146     EXPECT_THAT(removedCalls_, SizeIs(0));
1147     EXPECT_THAT(movedCalls_, SizeIs(0));
1148 }
1149 
1150 /**
1151  * @tc.name: ContainerTest
1152  * @tc.desc: test FindAnyInvalid function
1153  * @tc.type: FUNC
1154  * @tc.require: I7DMS1
1155  */
HWTEST_F(ContainerTest, FindAnyInvalid, TestSize.Level1)1156 HWTEST_F(ContainerTest, FindAnyInvalid, TestSize.Level1)
1157 {
1158     auto result1 = container_->FindAny({ "InvalidObject", TraversalType::NO_HIERARCHY, {}, false });
1159     auto result2 = container_->FindAny({ "InvalidObject", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1160     auto result3 =
1161         container_->FindAny({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { META_NS::IAttachment::UID }, false });
1162     auto result4 = container_->FindAny(
1163         { "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID, META_NS::IAttachment::UID }, true });
1164 
1165     EXPECT_EQ(result1, nullptr);
1166     EXPECT_EQ(result2, nullptr);
1167     EXPECT_EQ(result3, nullptr);
1168     EXPECT_EQ(result4, nullptr);
1169     EXPECT_THAT(addedCalls_, SizeIs(0));
1170     EXPECT_THAT(removedCalls_, SizeIs(0));
1171     EXPECT_THAT(movedCalls_, SizeIs(0));
1172 }
1173 
1174 /**
1175  * @tc.name: ContainerTest
1176  * @tc.desc: test IterationSupport function
1177  * @tc.type: FUNC
1178  * @tc.require: I7DMS1
1179  */
HWTEST_F(ContainerTest, IterationSupport, TestSize.Level1)1180 HWTEST_F(ContainerTest, IterationSupport, TestSize.Level1)
1181 {
1182     {
1183         int count = 0;
1184         ForEachShared(container_, [&](const IObject::Ptr&) { ++count; });
1185         EXPECT_EQ(count, NumDirectChildren);
1186     }
1187     {
1188         int count = 0;
1189         IterateShared(container_, [&](const IObject::Ptr&) {
1190             ++count;
1191             return true;
1192         });
1193         EXPECT_EQ(count, NumDirectChildren);
1194     }
1195 
1196     {
1197         int count = 0;
1198         ForEachShared(
1199             container_, [&](const IObject::Ptr&) { ++count; }, TraversalType::DEPTH_FIRST_PRE_ORDER);
1200         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1201     }
1202     {
1203         int count = 0;
1204         IterateShared(
1205             container_,
1206             [&](const IObject::Ptr&) {
1207                 ++count;
1208                 return true;
1209             },
1210             TraversalType::DEPTH_FIRST_PRE_ORDER);
1211         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1212     }
1213     {
1214         int count = 0;
1215         ForEachUnique(
1216             container_, [&](const IObject::Ptr&) { ++count; }, TraversalType::DEPTH_FIRST_PRE_ORDER);
1217         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1218     }
1219     {
1220         int count = 0;
1221         IterateUnique(
1222             container_,
1223             [&](const IObject::Ptr&) {
1224                 ++count;
1225                 return true;
1226             },
1227             TraversalType::DEPTH_FIRST_PRE_ORDER);
1228         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1229     }
1230     {
1231         int count = 0;
1232         IterateShared(
1233             container_,
1234             [&](const IObject::Ptr& o) {
1235                 ++count;
1236                 return o->GetName() != "SameNameDifferentUid";
1237             },
1238             TraversalType::DEPTH_FIRST_PRE_ORDER);
1239         EXPECT_LT(count, NumChildContainers + NumChildTestTypes);
1240     }
1241     {
1242         int count = 0;
1243         ConstIterate(container_, MakeIterationConstCallable([&](const IObject::Ptr&) {
1244             ++count;
1245             return true;
1246         }),
1247             IterateStrategy { TraversalType::DEPTH_FIRST_PRE_ORDER, LockType::NO_LOCK });
1248         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1249     }
1250     {
1251         int count = 0;
1252         ConstIterate(container_, MakeIterationConstCallable([&](const IObject::Ptr&) {
1253             ++count;
1254             return true;
1255         }),
1256             IterateStrategy { TraversalType::FULL_HIERARCHY, LockType::UNIQUE_LOCK });
1257         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1258     }
1259     {
1260         int count = 0;
1261         ConstIterate(container_, MakeIterationConstCallable([&](const IObject::Ptr&) {
1262             ++count;
1263             return true;
1264         }),
1265             IterateStrategy { TraversalType::BREADTH_FIRST_ORDER, LockType::UNIQUE_LOCK });
1266         EXPECT_EQ(count, NumChildContainers + NumChildTestTypes);
1267     }
1268 }
1269 
1270 /**
1271  * @tc.name: ContainerTest
1272  * @tc.desc: test FindOrder function
1273  * @tc.type: FUNC
1274  * @tc.require: I7DMS1
1275  */
HWTEST_F(ContainerTest, FindOrder, TestSize.Level1)1276 HWTEST_F(ContainerTest, FindOrder, TestSize.Level1)
1277 {
1278     auto c = CreateContainer("X");
1279     auto c1 = CreateContainer("1");
1280     auto c2 = CreateContainer("2");
1281     auto c1_1 = CreateTestType<IObject>("3");
1282     auto c1_2 = CreateTestType<IObject>("1");
1283     auto c2_1 = CreateTestType<IObject>("3");
1284     auto c2_2 = CreateContainer("2_2");
1285     auto c2_2_1 = CreateContainer("4");
1286     auto c2_2_1_1 = CreateTestType<IObject>("4");
1287     auto c2_3 = CreateTestType<IObject>("4");
1288     auto c3 = CreateTestType<IObject>("3");
1289 
1290     c->Add(c1);
1291     c->Add(c2);
1292     c->Add(c3);
1293     c1->Add(c1_1);
1294     c1->Add(c1_2);
1295     c2->Add(c2_1);
1296     c2->Add(c2_2);
1297     c2->Add(c2_3);
1298     c2_2->Add(c2_2_1);
1299     c2_2_1->Add(c2_2_1_1);
1300 
1301     {
1302         auto r = c->FindAny({ "1", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1303         ASSERT_TRUE(r);
1304         EXPECT_EQ(r, interface_pointer_cast<IObject>(c1));
1305     }
1306     {
1307         auto r = c->FindAny({ "1", TraversalType::DEPTH_FIRST_POST_ORDER, {}, false });
1308         ASSERT_TRUE(r);
1309         EXPECT_EQ(r, c1_2);
1310     }
1311     {
1312         auto r = c->FindAny({ "1", TraversalType::BREADTH_FIRST_ORDER, {}, false });
1313         ASSERT_TRUE(r);
1314         EXPECT_EQ(r, interface_pointer_cast<IObject>(c1));
1315     }
1316     {
1317         auto r = c->FindAny({ "1", TraversalType::NO_HIERARCHY, {}, false });
1318         ASSERT_TRUE(r);
1319         EXPECT_EQ(r, interface_pointer_cast<IObject>(c1));
1320     }
1321     {
1322         auto r = c->FindAny({ "3", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1323         ASSERT_TRUE(r);
1324         EXPECT_EQ(r, c1_1);
1325     }
1326     {
1327         auto r = c->FindAny({ "3", TraversalType::DEPTH_FIRST_POST_ORDER, {}, false });
1328         ASSERT_TRUE(r);
1329         EXPECT_EQ(r, c1_1);
1330     }
1331     {
1332         auto r = c->FindAny({ "3", TraversalType::BREADTH_FIRST_ORDER, {}, false });
1333         ASSERT_TRUE(r);
1334         EXPECT_EQ(r, c3);
1335     }
1336     {
1337         auto r = c->FindAny({ "3", TraversalType::NO_HIERARCHY, {}, false });
1338         ASSERT_TRUE(r);
1339         EXPECT_EQ(r, interface_pointer_cast<IObject>(c3));
1340     }
1341     {
1342         auto r = c->FindAny({ "4", TraversalType::DEPTH_FIRST_PRE_ORDER, {}, false });
1343         ASSERT_TRUE(r);
1344         EXPECT_EQ(r, interface_pointer_cast<IObject>(c2_2_1));
1345     }
1346     {
1347         auto r = c->FindAny({ "4", TraversalType::DEPTH_FIRST_POST_ORDER, {}, false });
1348         ASSERT_TRUE(r);
1349         EXPECT_EQ(r, c2_2_1_1);
1350     }
1351     {
1352         auto r = c->FindAny({ "4", TraversalType::BREADTH_FIRST_ORDER, {}, false });
1353         ASSERT_TRUE(r);
1354         EXPECT_EQ(r, c2_3);
1355     }
1356     {
1357         auto r = c->FindAny({ "4", TraversalType::NO_HIERARCHY, {}, false });
1358         ASSERT_FALSE(r);
1359     }
1360 }
1361 
1362 /**
1363  * @tc.name: FlatContainerTest
1364  * @tc.desc: test SameItemMultipleTimes function
1365  * @tc.type: FUNC
1366  * @tc.require: I7DMS1
1367  */
HWTEST_F(FlatContainerTest, SameItemMultipleTimes, TestSize.Level1)1368 HWTEST_F(FlatContainerTest, SameItemMultipleTimes, TestSize.Level1)
1369 {
1370     const auto child = interface_pointer_cast<IObject>(CreateTestType("Twice"));
1371     EXPECT_TRUE(container_->Add(child));
1372     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 1);
1373 
1374     EXPECT_TRUE(container_->Add(child));
1375     EXPECT_EQ(container_->GetSize(), NumDirectChildren + 2);
1376 
1377     const auto children = container_->FindAll({ "Twice", TraversalType::NO_HIERARCHY });
1378     ASSERT_THAT(children, SizeIs(2));
1379     EXPECT_EQ(children[0], child);
1380     EXPECT_EQ(children[1], child);
1381 
1382     ASSERT_THAT(addedCalls_, SizeIs(2));
1383     ASSERT_THAT(removedCalls_, SizeIs(0));
1384     ASSERT_THAT(movedCalls_, SizeIs(0));
1385     auto expected1 = ChildChangedInfo { child, NumDirectChildren, container_ };
1386     auto expected2 = ChildChangedInfo { child, NumDirectChildren + 1, container_ };
1387     EXPECT_EQ(addedCalls_[0], expected1);
1388     EXPECT_EQ(addedCalls_[1], expected2);
1389 }
1390 
1391 /**
1392  * @tc.name: FlatContainerTest
1393  * @tc.desc: test IterationSupport function
1394  * @tc.type: FUNC
1395  * @tc.require: I7DMS1
1396  */
HWTEST_F(FlatContainerTest, IterationSupport, TestSize.Level1)1397 HWTEST_F(FlatContainerTest, IterationSupport, TestSize.Level1)
1398 {
1399     {
1400         int count = 0;
1401         ForEachShared(container_, [&](const IObject::Ptr&) { ++count; });
1402         EXPECT_EQ(count, NumDirectChildren);
1403     }
1404     {
1405         int count = 0;
1406         IterateShared(container_, [&](const IObject::Ptr&) {
1407             ++count;
1408             return true;
1409         });
1410         EXPECT_EQ(count, NumDirectChildren);
1411     }
1412 }
1413 
BuildTestName(const testing::TestParamInfo<ContainerCommonTest::ParamType>& info)1414 static std::string BuildTestName(const testing::TestParamInfo<ContainerCommonTest::ParamType>& info)
1415 {
1416     return info.param == META_NS::ClassId::TestContainer ? "TestContainer" : "TestFlatContainer";
1417 }
1418 
1419 INSTANTIATE_TEST_SUITE_P(ContainerTests, ContainerCommonTest,
1420     testing::Values(META_NS::ClassId::TestContainer, META_NS::ClassId::TestFlatContainer), BuildTestName);
1421 
1422 META_END_NAMESPACE()
1423