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