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
16 #include "gtest/gtest.h"
17
18 #include "utils/object.h"
19 #include "utils/object_helper.h"
20 #ifdef ROSEN_OHOS
21 #include <atomic>
22 #include <message_parcel.h>
23 #include <thread>
24 #include <vector>
25 #endif
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33
34 // Mock Object implementation for testing
35 class MockObject : public Object {
36 public:
MockObject(ObjectType type,int32_t subType)37 MockObject(ObjectType type, int32_t subType) : Object(type, subType) {}
38
39 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel)40 bool Marshalling(Parcel& parcel) override
41 {
42 return true; // Mock implementation
43 }
44
Unmarshalling(Parcel & parcel,bool & isValid,int32_t depth=0)45 bool Unmarshalling(Parcel& parcel, bool& isValid, int32_t depth = 0) override
46 {
47 isValid = true;
48 return true; // Mock implementation
49 }
50 #endif
51
GenerateBaseObject()52 std::shared_ptr<void> GenerateBaseObject() override { return nullptr; }
53 };
54
55 class ObjectHelperTest : public testing::Test {
56 public:
57 static void SetUpTestCase();
58 static void TearDownTestCase();
59 void SetUp() override;
60 void TearDown() override;
61 };
62
SetUpTestCase()63 void ObjectHelperTest::SetUpTestCase() {}
TearDownTestCase()64 void ObjectHelperTest::TearDownTestCase() {}
SetUp()65 void ObjectHelperTest::SetUp() {}
TearDown()66 void ObjectHelperTest::TearDown() {}
67
68 /*
69 * @tc.name: RegisterAndGetFunc001
70 * @tc.desc: Test ObjectHelper Register and GetFunc basic functionality
71 * @tc.type: FUNC
72 * @tc.require: AR000GGNV3
73 * @tc.author:
74 */
75 HWTEST_F(ObjectHelperTest, RegisterAndGetFunc001, TestSize.Level1)
76 {
77 int32_t testType = 100;
78 int32_t testSubType = 200;
79
80 // Create a mock unmarshalling function with identifiable behavior
81 ObjectHelper::UnmarshallingFunc mockFunc =
__anonba52164f0102(Parcel& parcel, bool& isValid, int32_t depth) 82 [](Parcel& parcel, bool& isValid, int32_t depth) -> std::shared_ptr<Object> {
83 // Return a mock object with specific type to verify function identity
84 isValid = true;
85 return std::make_shared<MockObject>(Object::ObjectType::SHADER_EFFECT, 12345);
86 };
87
88 // Test Register
89 bool registerResult = ObjectHelper::Instance().Register(testType, testSubType, mockFunc);
90 EXPECT_TRUE(registerResult);
91
92 // Test GetFunc
93 auto retrievedFunc = ObjectHelper::Instance().GetFunc(testType, testSubType);
94 EXPECT_TRUE(retrievedFunc != nullptr);
95
96 // Verify the retrieved function is the same as the registered one by testing behavior
97 #ifdef ROSEN_OHOS
98 MessageParcel testParcel;
99 bool isValid = true;
100 auto result = retrievedFunc(testParcel, isValid, 0);
101 EXPECT_TRUE(result != nullptr);
102 EXPECT_TRUE(isValid);
103 if (result) {
104 EXPECT_EQ(result->GetType(), static_cast<int32_t>(Object::ObjectType::SHADER_EFFECT));
105 EXPECT_EQ(result->GetSubType(), 12345);
106 }
107 #endif
108
109 // Test GetFunc with non-existent type
110 auto nonExistentFunc = ObjectHelper::Instance().GetFunc(999, 888);
111 EXPECT_TRUE(nonExistentFunc == nullptr);
112 }
113
114 /*
115 * @tc.name: RegisterAndGetFunc002
116 * @tc.desc: Test ObjectHelper with multiple registrations
117 * @tc.type: FUNC
118 * @tc.require: AR000GGNV3
119 * @tc.author:
120 */
121 HWTEST_F(ObjectHelperTest, RegisterAndGetFunc002, TestSize.Level1)
122 {
123 // Register multiple functions
124 struct TestCase {
125 int32_t type;
126 int32_t subType;
127 int32_t expectedValue;
128 };
129
130 std::vector<TestCase> testCases = {
131 {101, 201, 1001},
132 {102, 202, 1002},
133 {103, 203, 1003}
134 };
135
136 // Register all test functions
137 for (const auto& testCase : testCases) {
138 ObjectHelper::UnmarshallingFunc func = [expectedValue = testCase.expectedValue]
__anonba52164f0202(Parcel& parcel, bool& isValid, int32_t depth) 139 (Parcel& parcel, bool& isValid, int32_t depth) -> std::shared_ptr<Object> {
140 // Mock function that can be identified by expectedValue as subType
141 isValid = true;
142 return std::make_shared<MockObject>(Object::ObjectType::SHADER_EFFECT, expectedValue);
143 };
144
145 bool result = ObjectHelper::Instance().Register(testCase.type, testCase.subType, func);
146 EXPECT_TRUE(result);
147 }
148
149 // Verify all functions can be retrieved and have correct behavior
150 for (const auto& testCase : testCases) {
151 auto retrievedFunc = ObjectHelper::Instance().GetFunc(testCase.type, testCase.subType);
152 EXPECT_TRUE(retrievedFunc != nullptr);
153
154 #ifdef ROSEN_OHOS
155 // Test the function behavior to verify it's the correct one
156 MessageParcel testParcel;
157 bool isValid = true;
158 auto result = retrievedFunc(testParcel, isValid, 0);
159 EXPECT_TRUE(result != nullptr);
160 EXPECT_TRUE(isValid);
161 if (result) {
162 EXPECT_EQ(result->GetType(), static_cast<int32_t>(Object::ObjectType::SHADER_EFFECT));
163 EXPECT_EQ(result->GetSubType(), testCase.expectedValue);
164 }
165 #endif
166 }
167 }
168
169 /*
170 * @tc.name: RegisterOverwrite001
171 * @tc.desc: Test ObjectHelper Register overwrites existing registration
172 * @tc.type: FUNC
173 * @tc.require: AR000GGNV3
174 * @tc.author:
175 */
176 HWTEST_F(ObjectHelperTest, RegisterOverwrite001, TestSize.Level1)
177 {
178 int32_t testType = 104;
179 int32_t testSubType = 204;
180
181 // Register first function with identifiable behavior
182 ObjectHelper::UnmarshallingFunc func1 =
__anonba52164f0302(Parcel& parcel, bool& isValid, int32_t depth) 183 [](Parcel& parcel, bool& isValid, int32_t depth) -> std::shared_ptr<Object> {
184 isValid = true;
185 return std::make_shared<MockObject>(Object::ObjectType::IMAGE_FILTER, 111);
186 };
187 bool result1 = ObjectHelper::Instance().Register(testType, testSubType, func1);
188 EXPECT_TRUE(result1);
189
190 auto retrievedFunc1 = ObjectHelper::Instance().GetFunc(testType, testSubType);
191 EXPECT_TRUE(retrievedFunc1 != nullptr);
192
193 // Register second function with same type/subType (should overwrite)
194 ObjectHelper::UnmarshallingFunc func2 =
__anonba52164f0402(Parcel& parcel, bool& isValid, int32_t depth) 195 [](Parcel& parcel, bool& isValid, int32_t depth) -> std::shared_ptr<Object> {
196 isValid = true;
197 return std::make_shared<MockObject>(Object::ObjectType::IMAGE_FILTER, 222);
198 };
199 bool result2 = ObjectHelper::Instance().Register(testType, testSubType, func2);
200 EXPECT_TRUE(result2);
201
202 auto retrievedFunc2 = ObjectHelper::Instance().GetFunc(testType, testSubType);
203 EXPECT_TRUE(retrievedFunc2 != nullptr);
204
205 // Verify the retrieved function is the second one (overwritten) by testing behavior
206 #ifdef ROSEN_OHOS
207 MessageParcel testParcel;
208 bool isValid = true;
209 auto result = retrievedFunc2(testParcel, isValid, 0);
210 EXPECT_TRUE(result != nullptr);
211 EXPECT_TRUE(isValid);
212 if (result) {
213 EXPECT_EQ(result->GetType(), static_cast<int32_t>(Object::ObjectType::IMAGE_FILTER));
214 EXPECT_EQ(result->GetSubType(), 222); // Should be the second function's behavior
215 }
216 #endif
217 }
218
219 #ifdef ROSEN_OHOS
220 /*
221 * @tc.name: DataCallbacks001
222 * @tc.desc: Test ObjectHelper Data marshalling/unmarshalling callbacks
223 * @tc.type: FUNC
224 * @tc.require: AR000GGNV3
225 * @tc.author:
226 */
227 HWTEST_F(ObjectHelperTest, DataCallbacks001, TestSize.Level1)
228 {
229 // Create mock callbacks
230 std::function<bool(Parcel&, std::shared_ptr<Data>)> marshallingCallback =
__anonba52164f0502(Parcel& parcel, std::shared_ptr<Data> data) 231 [](Parcel& parcel, std::shared_ptr<Data> data) -> bool {
232 return true; // Mock implementation
233 };
234
235 std::function<std::shared_ptr<Data>(Parcel&)> unmarshallingCallback =
__anonba52164f0602(Parcel& parcel) 236 [](Parcel& parcel) -> std::shared_ptr<Data> {
237 return nullptr; // Mock implementation
238 };
239
240 // Set callbacks
241 ObjectHelper::Instance().SetDataMarshallingCallback(marshallingCallback);
242 ObjectHelper::Instance().SetDataUnmarshallingCallback(unmarshallingCallback);
243
244 // Get callbacks
245 auto retrievedMarshallingCallback = ObjectHelper::Instance().GetDataMarshallingCallback();
246 auto retrievedUnmarshallingCallback = ObjectHelper::Instance().GetDataUnmarshallingCallback();
247
248 EXPECT_TRUE(retrievedMarshallingCallback != nullptr);
249 EXPECT_TRUE(retrievedUnmarshallingCallback != nullptr);
250
251 // Reset callbacks
252 ObjectHelper::Instance().SetDataMarshallingCallback(nullptr);
253 ObjectHelper::Instance().SetDataUnmarshallingCallback(nullptr);
254
255 auto resetMarshallingCallback = ObjectHelper::Instance().GetDataMarshallingCallback();
256 auto resetUnmarshallingCallback = ObjectHelper::Instance().GetDataUnmarshallingCallback();
257
258 EXPECT_TRUE(resetMarshallingCallback == nullptr);
259 EXPECT_TRUE(resetUnmarshallingCallback == nullptr);
260 }
261 #endif
262
263 } // namespace Drawing
264 } // namespace Rosen
265 } // namespace OHOS
266