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