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 #include <gmock/gmock.h> 16 #include <gtest/gtest.h> 17 18 #include <meta/api/make_callback.h> 19 #include <meta/interface/builtin_objects.h> 20 #include <meta/interface/intf_container.h> 21 #include <meta/interface/model/intf_data_model.h> 22 23 #include "TestRunner.h" 24 #include "helpers/testing_objects.h" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 META_BEGIN_NAMESPACE() 30 31 class DataModelTest : public testing::Test { 32 public: SetUpTestSuite()33 static void SetUpTestSuite() 34 { 35 SetTest(); 36 } TearDownTestSuite()37 static void TearDownTestSuite() 38 { 39 TearDownTest(); 40 } SetUp()41 void SetUp() {} TearDown()42 void TearDown() {} 43 }; 44 45 HWTEST_F(DataModelTest, ContainerDataModel, TestSize.Level1) 46 { 47 auto model = GetObjectRegistry().Create<IDataModel>(META_NS::ClassId::ContainerDataModel); 48 ASSERT_TRUE(model); 49 auto cont = interface_cast<IContainer>(model); 50 ASSERT_TRUE(cont); 51 52 size_t addIndex = -1; 53 size_t RemoveIndex = -1; 54 size_t moveFromIndex = -1; 55 size_t moveToIndex = -1; 56 57 model->OnDataAdded()->AddHandler( __anon2c81a5ce0102(auto index, auto count) 58 MakeCallback<IOnDataAdded>([&](auto index, auto count) { addIndex = index.Index(); })); 59 model->OnDataRemoved()->AddHandler( __anon2c81a5ce0202(auto index, auto count) 60 MakeCallback<IOnDataRemoved>([&](auto index, auto count) { RemoveIndex = index.Index(); })); __anon2c81a5ce0302(auto from, auto count, auto to) 61 model->OnDataMoved()->AddHandler(MakeCallback<IOnDataMoved>([&](auto from, auto count, auto to) { 62 moveFromIndex = from.Index(); 63 moveToIndex = to.Index(); 64 })); 65 66 EXPECT_EQ(model->GetSize(), 0); 67 68 auto o1 = CreateTestType<IObject>(); 69 cont->Add(o1); 70 71 EXPECT_EQ(model->GetSize(), 1); 72 EXPECT_EQ(addIndex, 0); 73 74 EXPECT_EQ(model->GetModelData(DataModelIndex { 0 }), interface_pointer_cast<IMetadata>(o1)); 75 76 cont->Remove(cont->GetAt(0)); 77 78 EXPECT_EQ(model->GetSize(), 0); 79 EXPECT_EQ(RemoveIndex, 0); 80 81 cont->Add(o1); 82 auto o2 = CreateTestType<IObject>(); 83 cont->Add(o2); 84 auto o3 = CreateTestType<IObject>(); 85 cont->Add(o3); 86 87 cont->Move(0, 2); 88 EXPECT_EQ(model->GetSize(), 3); 89 EXPECT_EQ(moveFromIndex, 0); 90 EXPECT_EQ(moveToIndex, 2); 91 92 EXPECT_EQ(model->GetModelData(DataModelIndex { 0 }), interface_pointer_cast<IMetadata>(o2)); 93 EXPECT_EQ(model->GetModelData(DataModelIndex { 1 }), interface_pointer_cast<IMetadata>(o3)); 94 EXPECT_EQ(model->GetModelData(DataModelIndex { 2 }), interface_pointer_cast<IMetadata>(o1)); 95 } 96 97 META_END_NAMESPACE() 98