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 <gtest/gtest.h> 17 18#include <meta/api/content_object.h> 19#include <meta/api/iteration.h> 20#include <meta/api/loaders/json_content_loader.h> 21#include <meta/interface/loaders/intf_class_content_loader.h> 22 23#include "src/serialisation_utils.h" 24#include "src/test_runner.h" 25#include "src/testing_objects.h" 26#include "src/util.h" 27 28using namespace testing; 29using namespace testing::ext; 30 31META_BEGIN_NAMESPACE() 32 33class IntfContentTest : public testing::Test { 34public: 35 static void SetUpTestSuite() 36 { 37 SetTest(); 38 } 39 static void TearDownTestSuite() 40 { 41 ResetTest(); 42 } 43 void SetUp() override {} 44 void TearDown() override {} 45}; 46 47/** 48 * @tc.name: IntfContentTest 49 * @tc.desc: test Search function 50 * @tc.type: FUNC 51 * @tc.require: I7DMS1 52 */ 53HWTEST_F(IntfContentTest, Search, TestSize.Level1) 54{ 55 auto content = CreateTestType<IObject>("Content"); 56 57 ContentObject co; 58 co.SetContent(content); 59 auto c = CreateTestContainer<IContainer>(); 60 c->Add(co); 61 EXPECT_FALSE(c->FindAny({ "Content", TraversalType::NO_HIERARCHY })); 62 EXPECT_EQ(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER }), content); 63 EXPECT_EQ(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID } }), content); 64 EXPECT_EQ(c->FindAny({ "", TraversalType::DEPTH_FIRST_PRE_ORDER, { ITestType::UID } }), content); 65 EXPECT_FALSE(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER, { IContainer::UID } })); 66 67 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 2); 68 EXPECT_EQ(c->FindAll({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 1); 69 70 EXPECT_TRUE(ContainsObjectWithName(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }), "Content")); 71 72 co.ContentSearchable(false); 73 EXPECT_FALSE(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER })); 74 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 1); 75 76 co.ContentSearchable(true); 77 EXPECT_EQ(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER }), content); 78 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 2); 79} 80 81/** 82 * @tc.name: IntfContentTest 83 * @tc.desc: test SearchContentContainer function 84 * @tc.type: FUNC 85 * @tc.require: I7DMS1 86 */ 87HWTEST_F(IntfContentTest, SearchContentContainer, TestSize.Level1) 88{ 89 auto ccontent = CreateTestType<IObject>("Content"); 90 auto content = CreateTestContainer<IContainer>(); 91 content->Add(ccontent); 92 93 ContentObject co; 94 co.SetContent(interface_pointer_cast<IObject>(content)); 95 auto c = CreateTestContainer<IContainer>(); 96 c->Add(co); 97 EXPECT_FALSE(c->FindAny({ "Content", TraversalType::NO_HIERARCHY })); 98 EXPECT_EQ(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER }), ccontent); 99 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 3); 100 101 co.ContentSearchable(false); 102 EXPECT_FALSE(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER })); 103 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 1); 104 105 co.ContentSearchable(true); 106 EXPECT_EQ(c->FindAny({ "Content", TraversalType::DEPTH_FIRST_PRE_ORDER }), ccontent); 107 EXPECT_EQ(c->FindAll({ "", TraversalType::DEPTH_FIRST_PRE_ORDER }).size(), 3); 108} 109 110/** 111 * @tc.name: IntfContentTest 112 * @tc.desc: test Iteration function 113 * @tc.type: FUNC 114 * @tc.require: I7DMS1 115 */ 116HWTEST_F(IntfContentTest, Iteration, TestSize.Level1) 117{ 118 auto ccontent = CreateTestType<IObject>("Content"); 119 auto content = CreateTestContainer<IContainer>(); 120 content->Add(ccontent); 121 122 ContentObject co; 123 co.SetContent(interface_pointer_cast<IObject>(content)); 124 auto c = CreateTestContainer<IContainer>(); 125 c->Add(co); 126 127 { 128 BASE_NS::vector<IObject::Ptr> vec; 129 EXPECT_TRUE(ForEachShared( 130 c, [&](const IObject::Ptr& obj) { vec.push_back(obj); }, TraversalType::DEPTH_FIRST_PRE_ORDER)); 131 EXPECT_EQ(vec.size(), 3); 132 EXPECT_TRUE(ContainsObjectWithName(vec, "Content")); 133 } 134 135 co.ContentSearchable(false); 136 137 { 138 BASE_NS::vector<IObject::Ptr> vec; 139 EXPECT_TRUE(ForEachShared( 140 c, [&](const IObject::Ptr& obj) { vec.push_back(obj); }, TraversalType::DEPTH_FIRST_PRE_ORDER)); 141 EXPECT_EQ(vec.size(), 1); 142 EXPECT_FALSE(ContainsObjectWithName(vec, "Content")); 143 } 144} 145 146/** 147 * @tc.name: IntfContentTest 148 * @tc.desc: test Serialization function 149 * @tc.type: FUNC 150 * @tc.require: I7DMS1 151 */ 152HWTEST_F(IntfContentTest, Serialization, TestSize.Level1) 153{ 154 { 155 TestSerialiser ser; 156 157 ContentObject co; 158 co.SerializeContent(false); 159 co.SetContent(CreateTestType<IObject>("Content")); 160 ASSERT_TRUE(ser.Export(co)); 161 162 auto imp = ser.Import<IContent>(); 163 ASSERT_TRUE(imp); 164 EXPECT_FALSE(imp->Content()->GetValue()); 165 } 166 167 { 168 TestSerialiser ser; 169 170 ContentObject co; 171 co.SerializeContent(true); 172 co.SetContent(CreateTestType<IObject>("Content")); 173 ASSERT_TRUE(ser.Export(co)); 174 175 auto imp = ser.Import<IContent>(); 176 ASSERT_TRUE(imp); 177 ASSERT_TRUE(imp->Content()->GetValue()); 178 EXPECT_EQ(imp->Content()->GetValue()->GetName(), "Content"); 179 } 180} 181 182/** 183 * @tc.name: IntfContentTest 184 * @tc.desc: test Defaults function 185 * @tc.type: FUNC 186 * @tc.require: I7DMS1 187 */ 188HWTEST_F(IntfContentTest, Defaults, TestSize.Level1) 189{ 190 ContentObject co; 191 const auto objectFlags = interface_cast<IObjectFlags>(co); 192 ASSERT_TRUE(objectFlags); 193 EXPECT_FALSE(objectFlags->GetObjectFlags().IsSet(META_NS::ObjectFlagBits::SERIALIZE_HIERARCHY)); 194} 195 196/** 197 * @tc.name: IntfContentTest 198 * @tc.desc: test ObjectFlagsNoHierarchy function 199 * @tc.type: FUNC 200 * @tc.require: I7DMS1 201 */ 202HWTEST_F(IntfContentTest, ObjectFlagsNoHierarchy, TestSize.Level1) 203{ 204 TestSerialiser ser; 205 ContentObject co; 206 co.SetContent(CreateTestType<IObject>("Content")); 207 208 ASSERT_TRUE(ser.Export(co)); 209 210 auto imported = ser.Import<IContent>(); 211 ASSERT_TRUE(imported); 212 213 EXPECT_FALSE(imported->Content()->GetValue()); 214} 215 216/** 217 * @tc.name: IntfContentTest 218 * @tc.desc: test ObjectFlagsHierarchy function 219 * @tc.type: FUNC 220 * @tc.require: I7DMS1 221 */ 222HWTEST_F(IntfContentTest, ObjectFlagsHierarchy, TestSize.Level1) 223{ 224 TestSerialiser ser; 225 ContentObject co; 226 co.SetContent(CreateTestType<IObject>("Content")); 227 228 auto flagsIntPtr = interface_cast<IObjectFlags>(co); 229 ASSERT_TRUE(flagsIntPtr); 230 SetObjectFlags(co, ObjectFlagBits::SERIALIZE_HIERARCHY, true); 231 232 ASSERT_TRUE(ser.Export(co)); 233 auto imported = ser.Import<IContent>(); 234 ASSERT_TRUE(imported); 235 236 auto content = GetValue(imported->Content()); 237 ASSERT_TRUE(content); 238 EXPECT_EQ(content->GetName(), "Content"); 239} 240 241/** 242 * @tc.name: IntfContentTest 243 * @tc.desc: test Loader function 244 * @tc.type: FUNC 245 * @tc.require: I7DMS1 246 */ 247HWTEST_F(IntfContentTest, Loader, TestSize.Level1) 248{ 249 auto loader = GetObjectRegistry().Create<IClassContentLoader>(ClassId::ClassContentLoader); 250 ASSERT_TRUE(loader); 251 loader->ClassId()->SetValue(ClassId::TestType); 252 ASSERT_TRUE(loader->Create({})); 253 254 ContentObject co; 255 co.ContentLoader(loader); 256 257 auto obj = co.Content()->GetValue(); 258 ASSERT_TRUE(obj); 259 EXPECT_TRUE(interface_cast<ITestType>(obj)); 260 261 loader->ClassId()->SetValue(ClassId::TestContainer); 262 263 auto cont = co.Content()->GetValue(); 264 ASSERT_TRUE(cont); 265 EXPECT_TRUE(interface_cast<ITestContainer>(cont)); 266} 267 268/** 269 * @tc.name: IntfContentTest 270 * @tc.desc: test ContentRequiredInterfaces function 271 * @tc.type: FUNC 272 * @tc.require: I7DMS1 273 */ 274HWTEST_F(IntfContentTest, ContentRequiredInterfaces, TestSize.Level1) 275{ 276 ContentObject co; 277 EXPECT_TRUE(co.SetRequiredInterfaces({ ITestType::UID })); 278 279 auto correctType = GetObjectRegistry().Create(ClassId::TestType); 280 auto wrongType = GetObjectRegistry().Create(ClassId::Object); 281 282 EXPECT_FALSE(co.SetContent(wrongType)); 283 EXPECT_FALSE(co.Content()->GetValue()); 284 285 EXPECT_TRUE(co.SetContent(correctType)); 286 EXPECT_EQ(co.Content()->GetValue(), correctType); 287 288 EXPECT_TRUE(co.SetRequiredInterfaces({})); 289 EXPECT_EQ(co.Content()->GetValue(), correctType); 290 291 EXPECT_TRUE(co.SetContent(wrongType)); 292 EXPECT_EQ(co.Content()->GetValue(), wrongType); 293} 294 295/** 296 * @tc.name: IntfContentTest 297 * @tc.desc: test ContentLoaderRequiredInterfaces function 298 * @tc.type: FUNC 299 * @tc.require: I7DMS1 300 */ 301HWTEST_F(IntfContentTest, ContentLoaderRequiredInterfaces, TestSize.Level1) 302{ 303 ContentObject co; 304 EXPECT_TRUE(co.SetRequiredInterfaces({ ITestType::UID })); 305 306 auto correctType = ClassId::TestType; 307 auto wrongType = ClassId::Object; 308 309 auto loader = GetObjectRegistry().Create<IClassContentLoader>(ClassId::ClassContentLoader); 310 ASSERT_TRUE(loader); 311 co.ContentLoader()->SetValue(loader); 312 EXPECT_FALSE(co.Content()->GetValue()); 313 314 loader->ClassId()->SetValue(correctType); 315 EXPECT_TRUE(co.Content()->GetValue()); 316 317 loader->ClassId()->SetValue(wrongType); 318 EXPECT_FALSE(co.Content()->GetValue()); 319 320 EXPECT_TRUE(co.SetRequiredInterfaces({})); 321 EXPECT_TRUE(co.Content()->GetValue()); 322 323 EXPECT_TRUE(co.SetRequiredInterfaces({ ITestType::UID })); 324 EXPECT_FALSE(co.Content()->GetValue()); 325} 326 327META_END_NAMESPACE()