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 #include "TestRunner.h" 16 #include <src/testing_objects.h> 17 18 #include <gmock/gmock-matchers.h> 19 #include <gtest/gtest.h> 20 21 #include <meta/api/object.h> 22 23 using namespace testing; 24 using namespace testing::ext; 25 META_BEGIN_NAMESPACE() 26 27 class ObjectTest : public testing::Test { 28 public: SetUpTestSuite()29 static void SetUpTestSuite() 30 { 31 SetTest(); 32 } TearDownTestSuite()33 static void TearDownTestSuite() 34 { 35 TearDownTest(); 36 } SetUp()37 void SetUp() {} TearDown()38 void TearDown() {} 39 }; 40 41 /** 42 * @tc.name: ObjectBaseAPI 43 * @tc.desc: test ObjectBaseAPI 44 * @tc.type:FUNC 45 * @tc.require: 46 */ 47 HWTEST_F(ObjectTest, ObjectBaseAPI, TestSize.Level1) 48 { 49 META_NS::Object object; 50 51 EXPECT_NE(object.GetIObject(), nullptr); 52 EXPECT_NE(object.GetInterfacePtr<IObject>(), nullptr); 53 EXPECT_EQ(object.GetInterfacePtr<IObject>(), object.GetIObject()); 54 EXPECT_EQ(interface_pointer_cast<IObject>(object), object); 55 EXPECT_EQ(interface_cast<IObject>(object), object.GetIObject().get()); 56 57 META_NS::Object object2; 58 ASSERT_TRUE(object2.Initialize(object)); 59 EXPECT_EQ(object, object2); 60 EXPECT_TRUE(object == object2); 61 EXPECT_FALSE(object != object2); 62 63 META_NS::Object object3; 64 EXPECT_TRUE(object != object3); 65 EXPECT_FALSE(object == object3); 66 EXPECT_FALSE(object3); // not initialized yet 67 EXPECT_NE(object3.GetInterfacePtr<IObject>(), nullptr); 68 EXPECT_TRUE(object3); // Initialized after get 69 } 70 71 /** 72 * @tc.name: TypeConversion 73 * @tc.desc: test TypeConversion 74 * @tc.type:FUNC 75 * @tc.require: 76 */ 77 HWTEST_F(ObjectTest, TypeConversion, TestSize.Level1) 78 { 79 META_NS::Object o1(interface_pointer_cast<IObject>(CreateTestType())); 80 META_NS::Object o2(interface_pointer_cast<IObject>(CreateTestContainer())); 81 82 EXPECT_TRUE(o1); 83 EXPECT_TRUE(o2); 84 EXPECT_NE(o1.GetIObject()->GetClassId(), o2.GetIObject()->GetClassId()); 85 86 o1 = o2; // Changes type 87 EXPECT_TRUE(o1); 88 EXPECT_EQ(o1.GetIObject(), o2.GetIObject()); 89 EXPECT_EQ(o1.GetIObject()->GetClassId(), o2.GetIObject()->GetClassId()); 90 } 91 92 /** 93 * @tc.name: LazyInitBase 94 * @tc.desc: test LazyInitBase 95 * @tc.type:FUNC 96 * @tc.require: 97 */ 98 HWTEST_F(ObjectTest, LazyInitBase, TestSize.Level1) 99 { 100 static constexpr auto propName = "value"; 101 auto o1 = Object(); 102 Object o2; 103 104 EXPECT_FALSE(o1); 105 EXPECT_FALSE(o2); 106 o2 = o1; // Does nothing 107 EXPECT_TRUE(o1); 108 EXPECT_TRUE(o2); 109 Object o3; 110 EXPECT_FALSE(o3); 111 o1.MetaProperty(propName, 5); 112 o3 = o1; 113 EXPECT_TRUE(o1); 114 EXPECT_TRUE(o2); 115 EXPECT_TRUE(o3); 116 EXPECT_EQ(o1, o2); 117 EXPECT_EQ(o1, o3); 118 119 auto p1 = o1.Metadata().GetProperty(propName); 120 auto p2 = o2.Metadata().GetProperty(propName); 121 auto p3 = o3.Metadata().GetProperty(propName); 122 EXPECT_EQ(p1, p3); 123 EXPECT_EQ(p2, p1); 124 } 125 126 /** 127 * @tc.name: ResetIObject 128 * @tc.desc: test ResetIObject 129 * @tc.type:FUNC 130 * @tc.require: 131 */ 132 HWTEST_F(ObjectTest, ResetIObject, TestSize.Level1) 133 { 134 META_NS::Object object1; 135 META_NS::Object object2; 136 auto iobject1 = object1.GetIObject(); 137 object2 = object1; 138 auto iobject2 = object2.GetIObject(); 139 EXPECT_EQ(iobject1, iobject2); 140 141 object1.ResetIObject(); 142 EXPECT_FALSE(object1); 143 EXPECT_NE(object1.GetIObject(), iobject2); 144 } 145 146 META_END_NAMESPACE() 147