1 /*
2 * Copyright (c) 2025 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 <TestRunner.h>
17 #include <src/testing_objects.h>
18
19 #include <gmock/gmock-matchers.h>
20 #include <gtest/gtest.h>
21
22 #include <meta/api/property/array_property_event_handler.h>
23 #include <meta/interface/property/construct_array_property.h>
24
25 #include "src/util.h"
26
27 using namespace CORE_NS;
28 using namespace testing::ext;
29
30 META_BEGIN_NAMESPACE()
31
32 class ArrayPropertyEventHandlerTest : public testing::Test {
33 public:
SetUpTestSuite()34 static void SetUpTestSuite()
35 {
36 SetTest();
37 }
TearDownTestSuite()38 static void TearDownTestSuite()
39 {
40 TearDownTest();
41 }
SetUp()42 void SetUp() {}
TearDown()43 void TearDown() {}
44 };
45
46 template<typename Type>
ExpectChanges(ArrayChanges<Type> a1,ArrayChanges<Type> a2)47 static void ExpectChanges(ArrayChanges<Type> a1, ArrayChanges<Type> a2)
48 {
49 EXPECT_EQ(a1.indexesRemoved, a2.indexesRemoved);
50 EXPECT_EQ(a1.valuesAdded, a2.valuesAdded);
51 EXPECT_EQ(a1.positionChanged, a2.positionChanged);
52 }
53
54 /**
55 * @tc.name: Diff
56 * @tc.desc: test diff
57 * @tc.type:FUNC
58 * @tc.require:
59 */
60 HWTEST_F(ArrayPropertyEventHandlerTest, Diff, TestSize.Level1)
61 {
62 auto p = ConstructArrayProperty<int>("test", BASE_NS::vector<int> { 1, 2, 3 });
63 ArrayPropertyChangedEventHandler<int> h;
64 ArrayChanges<int> change;
__anon19cb04c40102(ArrayChanges<int> c) 65 h.Subscribe(p, [&](ArrayChanges<int> c) { change = c; });
66
67 p->SetValue({ 1, 2 });
68 ExpectChanges(change, { { 2 } });
69
70 p->SetValue({ 1, 2, 3, 4 });
71 ExpectChanges(change, { {}, { { 3, 2 }, { 4, 3 } } });
72
73 p->SetValue({ 1, 5, 6, 4 });
74 ExpectChanges(change, { { 1, 2 }, { { 5, 1 }, { 6, 2 } } });
75
76 p->SetValue({ 1, 4, 5, 6 });
77 ExpectChanges(change, { {}, {}, { { 3, 1 }, { 1, 2 }, { 2, 3 } } });
78
79 p->SetValue({ 0, 1, 6, 5 });
80 ExpectChanges(change, { { 1 }, { { 0, 0 } }, { { 0, 1 }, { 3, 2 }, { 2, 3 } } });
81
82 p->SetValue({ 0, 1, 0 });
83 p->SetValue({ 1, 0 });
84 ExpectChanges(change, { { 2 }, {}, { { 1, 0 }, { 0, 1 } } });
85
86 p->SetValue({ 0, 1, 0 });
87 p->SetValue({ 2, 1, 0 });
88 ExpectChanges(change, { { 2 }, { { 2, 0 } }, { { 0, 2 } } });
89
90 p->SetValue({ 0 });
91 p->SetValue(BASE_NS::vector<int> { 0, 0 });
92 ExpectChanges(change, { {}, { { 0, 1 } } });
93
94 p->SetValue({ 0, 1, 2, 2, 1, 0, 1, 0, 1, 0 });
95 p->SetValue({ 0, 0, 2, 1, 0, 0, 1, 0, 1, 0 });
96 ExpectChanges(
97 change, { { 3, 8 }, { { 0, 7 }, { 0, 9 } }, { { 5, 1 }, { 1, 3 }, { 7, 4 }, { 9, 5 }, { 4, 6 }, { 6, 8 } } });
98
99 change = {};
100 h.Unsubscribe();
101 p->SetValue({ -1, 0 });
102 ExpectChanges(change, {});
103 }
104 META_END_NAMESPACE()
105