/* * Copyright (C) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include "src/test_runner.h" #include "src/util.h" #include "src/testing_objects.h" using namespace testing::ext; META_BEGIN_NAMESPACE() class ArrayPropertyEventHandlerTest : public testing::Test { public: static void SetUpTestSuite() { SetTest(); } static void TearDownTestSuite() { ResetTest(); } void SetUp() override {} void TearDown() override {} }; template static void ExpectChanges(ArrayChanges a1, ArrayChanges a2) { EXPECT_EQ(a1.indexesRemoved, a2.indexesRemoved); EXPECT_EQ(a1.valuesAdded, a2.valuesAdded); EXPECT_EQ(a1.positionChanged, a2.positionChanged); } /** * @tc.name: ArrayPropertyEventHandlerTest * @tc.desc: test Diff function * @tc.type: FUNC * @tc.require: I7DMS1 */ HWTEST_F(ArrayPropertyEventHandlerTest, Diff, TestSize.Level1) { auto p = ConstructArrayProperty("test", BASE_NS::vector { 1, 2, 3 }); ArrayPropertyChangedEventHandler h; ArrayChanges change; h.Subscribe(p, [&] (ArrayChanges c) { change = c; }); p->SetValue(BASE_NS::vector{ 1, 2 }); ExpectChanges(change, { { 2 } }); p->SetValue(BASE_NS::vector{ 1, 2, 3, 4 }); ExpectChanges(change, { {}, { { 3, 2 }, { 4, 3 } } }); p->SetValue(BASE_NS::vector{ 1, 5, 6, 4 }); ExpectChanges(change, { { 1, 2 }, { { 5, 1 }, { 6, 2 } } }); p->SetValue(BASE_NS::vector{ 1, 4, 5, 6 }); ExpectChanges(change, { {}, {}, { { 3, 1 }, { 1, 2 }, { 2, 3 } } }); p->SetValue(BASE_NS::vector{ 0, 1, 6, 5 }); ExpectChanges(change, { { 1 }, { { 0, 0 } }, { { 0, 1 }, { 3, 2 }, { 2, 3 } } }); p->SetValue(BASE_NS::vector{ 0, 1, 0 }); p->SetValue(BASE_NS::vector{ 1, 0 }); ExpectChanges(change, { { 2 }, {}, { { 1, 0 }, { 0, 1 } } }); p->SetValue(BASE_NS::vector{ 0, 1, 0 }); p->SetValue(BASE_NS::vector{ 2, 1, 0 }); ExpectChanges(change, { { 2 }, { { 2, 0 } }, { { 0, 2 } } }); p->SetValue(BASE_NS::vector{ 0 }); p->SetValue(BASE_NS::vector { 0, 0 }); ExpectChanges(change, { {}, { { 0, 1 } } }); p->SetValue(BASE_NS::vector{ 0, 1, 2, 2, 1, 0, 1, 0, 1, 0 }); p->SetValue(BASE_NS::vector{ 0, 0, 2, 1, 0, 0, 1, 0, 1, 0 }); ExpectChanges( change, { { 3, 8 }, { { 0, 7 }, { 0, 9 } }, { { 5, 1 }, { 1, 3 }, { 7, 4 }, { 9, 5 }, { 4, 6 }, { 6, 8 } } }); } META_END_NAMESPACE()