1 /*
2 * Copyright (c) 2022 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 <cstdlib>
17 #include <cstring>
18 #include <memory>
19 #include <string.h>
20 #include <securec.h>
21 #include "gtest/gtest.h"
22 #include "gtest/hwext/gtest-tag.h"
23
24 #include "modifier/rs_render_modifier.h"
25 #include "pipeline/rs_recording_canvas.h"
26
27 #include "message_parcel.h"
28 #include "property/rs_properties.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS::Rosen {
34 class RSRenderModifierTest : public testing::Test {
35 public:
36 constexpr static float floatData[] = {
37 0.0f, 485.44f, 2.0f,
38 std::numeric_limits<float>::max(), std::numeric_limits<float>::min(),
39 };
40 PropertyId id = 100;
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45 };
46
SetUpTestCase()47 void RSRenderModifierTest::SetUpTestCase() {}
TearDownTestCase()48 void RSRenderModifierTest::TearDownTestCase() {}
SetUp()49 void RSRenderModifierTest::SetUp() {}
TearDown()50 void RSRenderModifierTest::TearDown() {}
51
52 /**
53 * @tc.name: RSGeometryTransRenderModifier
54 * @tc.desc:
55 * @tc.type:FUNC
56 */
57 HWTEST_F(RSRenderModifierTest, RSGeometryTransRenderModifier, TestSize.Level1)
58 {
59 auto prop = std::make_shared<RSRenderProperty<Drawing::Matrix>>();
60 auto modifier = std::make_shared<RSGeometryTransRenderModifier>(prop);
61 RSProperties properties;
62 RSModifierContext context(properties);
63 auto rsRenderPropertyBase = std::make_shared<RSRenderProperty<Drawing::Matrix>>();
64 ASSERT_TRUE(modifier != nullptr);
65 modifier->Apply(context);
66 modifier->Update(rsRenderPropertyBase, false);
67 ASSERT_TRUE(modifier->GetProperty() == prop);
68 ASSERT_TRUE(modifier->GetPropertyId() == 0);
69 modifier->SetType(RSModifierType::BOUNDS);
70 ASSERT_TRUE(modifier->GetType() == RSModifierType::BOUNDS);
71
72 MessageParcel parcel;
73 ASSERT_TRUE(modifier->Marshalling(parcel));
74 ASSERT_TRUE(RSGeometryTransRenderModifier::Unmarshalling(parcel) != nullptr);
75 }
76
77 /**
78 * @tc.name: LifeCycle001
79 * @tc.desc:
80 * @tc.type:FUNC
81 */
82 HWTEST_F(RSRenderModifierTest, LifeCycle001, TestSize.Level1)
83 {
84 auto prop = std::make_shared<RSRenderProperty<float>>();
85 auto modifier = std::make_shared<RSAlphaRenderModifier>(prop);
86 ASSERT_TRUE(modifier != nullptr);
87 ASSERT_TRUE(modifier->GetProperty() == prop);
88 ASSERT_TRUE(modifier->GetPropertyId() == 0);
89
90 auto prop2 = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
91 auto modifier2 = std::make_shared<RSAlphaRenderModifier>(prop2);
92 ASSERT_TRUE(modifier2 != nullptr);
93 ASSERT_EQ(modifier2->GetPropertyId(), id);
94 }
95
96 /**
97 * @tc.name: Modifier001
98 * @tc.desc:
99 * @tc.type:FUNC
100 */
101 HWTEST_F(RSRenderModifierTest, Modifier001, TestSize.Level1)
102 {
103 auto prop = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
104 auto modifier = std::make_shared<RSAlphaRenderModifier>(prop);
105 ASSERT_TRUE(prop != nullptr);
106 ASSERT_EQ(modifier->GetProperty(), prop);
107
108 auto prop1 = std::make_shared<RSRenderProperty<float>>(floatData[1], id);
109 modifier->Update(prop1, false);
110 ASSERT_EQ(std::static_pointer_cast<RSRenderProperty<float>>(modifier->GetProperty())->Get(), floatData[1]);
111
112 auto prop2 = std::make_shared<RSRenderProperty<float>>(floatData[2], id);
113 modifier->Update(prop2, true);
114 ASSERT_EQ(std::static_pointer_cast<RSRenderProperty<float>>(modifier->GetProperty())->Get(),
115 floatData[1] + floatData[2]);
116 }
117
118 /**
119 * @tc.name: DrawCmdListModifier001
120 * @tc.desc:
121 * @tc.type:FUNC
122 */
123 HWTEST_F(RSRenderModifierTest, DrawCmdListModifier001, TestSize.Level1)
124 {
125 ExtendRecordingCanvas canvas(100, 100);
126 canvas.Translate(15.f, 15.f);
127
128 auto prop = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
129 auto modifier = std::make_shared<RSDrawCmdListRenderModifier>(prop);
130
131 MessageParcel parcel;
132 ASSERT_TRUE(modifier->Marshalling(parcel));
133 ASSERT_TRUE(RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr);
134
135 canvas.Scale(2.f, 2.f);
136 modifier->Update(nullptr, false);
137 auto prop1 = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
138 modifier->Update(prop1, true);
139
140 ASSERT_TRUE(modifier->Marshalling(parcel));
141 ASSERT_TRUE(RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr);
142
143 MessageParcel parcel1;
144 char* buffer = static_cast<char *>(malloc(parcel1.GetMaxCapacity()));
145 memset_s(buffer, parcel1.GetMaxCapacity(), 0, parcel1.GetMaxCapacity());
146 ASSERT_TRUE(parcel1.WriteUnpadBuffer(buffer, parcel1.GetMaxCapacity()));
147 bool ret = false;
148 while (!ret) {
149 ret = (modifier->Marshalling(parcel) && (RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr));
150 parcel1.SetMaxCapacity(parcel1.GetMaxCapacity() + 1);
151 }
152 free(buffer);
153 ASSERT_TRUE(ret);
154 }
155
156 /**
157 * @tc.name: RSParticlesRenderModifier001
158 * @tc.desc:Update
159 * @tc.type:FUNC
160 */
161 HWTEST_F(RSRenderModifierTest, RSParticlesRenderModifier002, TestSize.Level1)
162 {
163 auto prop = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
164 bool isDelta = false;
165 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
166 auto RSPRM = std::make_shared<RSParticlesRenderModifier>(property);
167 RSPRM->Update(prop, isDelta);
168 ASSERT_NE(nullptr, RSPRM->property_);
169 }
170
171 /**
172 * @tc.name: RSEnvForegroundColorRenderModifier002
173 * @tc.desc:Update
174 * @tc.type:FUNC
175 */
176 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorRenderModifier002, TestSize.Level1)
177 {
178 auto prop = std::make_shared<RSRenderAnimatableProperty<Color>>();
179 bool isDelta = false;
180 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
181 auto RSEFC = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
182 RSEFC->Update(prop, isDelta);
183 ASSERT_NE(nullptr, RSEFC->property_);
184 }
185
186 /**
187 * @tc.name: RSEnvForegroundColorStrategyRenderModifier001
188 * @tc.desc:Apply
189 * @tc.type:FUNC
190 */
191 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorStrategyRenderModifier001, TestSize.Level1)
192 {
193 auto prop = std::make_shared<RSRenderAnimatableProperty<Color>>();
194 bool isDelta = false;
195 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
196 auto RSEFCS = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
197 RSEFCS->Update(prop, isDelta);
198 ASSERT_NE(nullptr, RSEFCS->property_);
199 }
200
201 /**
202 * @tc.name: Apply
203 * @tc.desc: Test Apply and Update and Marshalling
204 * @tc.type: FUNC
205 * @tc.require: issueI9QIQO
206 */
207 HWTEST_F(RSRenderModifierTest, Apply, TestSize.Level1)
208 {
209 ExtendRecordingCanvas canvas(100, 100);
210 canvas.Translate(15.f, 15.f);
211 auto prop = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
212 auto modifier = std::make_shared<RSDrawCmdListRenderModifier>(prop);
213 RSProperties properties;
214 RSModifierContext context(properties);
215 modifier->Apply(context);
216 RSPaintFilterCanvas paintFilterCanvas(&canvas);
217 context.canvas_ = &paintFilterCanvas;
218 modifier->Apply(context);
219 ASSERT_NE(nullptr, context.canvas_);
220
221 modifier->Update(prop, true);
222 ASSERT_NE(modifier->GetProperty(), nullptr);
223
224 Parcel parcel;
225 ASSERT_TRUE(modifier->Marshalling(parcel));
226 }
227
228 /**
229 * @tc.name: RSEnvForegroundColorRenderModifier001
230 * @tc.desc: Test Apply and Marshalling
231 * @tc.type:FUNC
232 * @tc.require: issueI9QIQO
233 */
234 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorRenderModifier001, TestSize.Level1)
235 {
236 auto prop = std::make_shared<RSRenderAnimatableProperty<Color>>();
237 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
238 auto modifier = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
239 RSProperties properties;
240 ExtendRecordingCanvas canvas(100, 100);
241 RSPaintFilterCanvas paintFilterCanvas(&canvas);
242 RSModifierContext context(properties, &paintFilterCanvas);
243 modifier->Apply(context);
244 ASSERT_NE(nullptr, context.canvas_);
245
246 Parcel parcel;
247 ASSERT_TRUE(modifier->Marshalling(parcel));
248 }
249
250 /**
251 * @tc.name: RSEnvForegroundColorStrategyRenderModifier001
252 * @tc.desc: test Apply and Marshalling and CalculateInvertColor and GetInvertBackgroundColor and Update
253 * @tc.type:FUNC
254 * @tc.require: issueI9QIQO
255 */
256 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorStrategyRenderModifier002, TestSize.Level1)
257 {
258 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
259 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
260 RSProperties properties;
261 RSModifierContext context(properties);
262 modifier->Apply(context);
263 ASSERT_EQ(nullptr, context.canvas_);
264
265 ForegroundColorStrategyType type = ForegroundColorStrategyType::INVERT_BACKGROUNDCOLOR;
266 auto modifierTwo = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
267
268 auto renderProperty =
269 std::static_pointer_cast<RSRenderProperty<ForegroundColorStrategyType>>(modifierTwo->property_);
270 renderProperty->stagingValue_ = type;
271 ExtendRecordingCanvas canvas(100, 100);
272 Drawing::Surface surface;
273 RSPaintFilterCanvas paintFilterCanvas(&canvas);
274 paintFilterCanvas.surface_ = &surface;
275 RSModifierContext contextArgs(properties, &paintFilterCanvas);
276 ASSERT_NE(nullptr, contextArgs.canvas_);
277 modifierTwo->Apply(contextArgs);
278 ASSERT_NE(nullptr, contextArgs.canvas_);
279
280 Parcel parcel;
281 ASSERT_TRUE(modifier->Marshalling(parcel));
282
283 ASSERT_EQ(modifier->CalculateInvertColor(Color()).alpha_, 0.f);
284
285 modifier->GetInvertBackgroundColor(contextArgs);
286 properties.SetClipToBounds(true);
287 ASSERT_EQ(modifier->GetInvertBackgroundColor(contextArgs).alpha_, 0.f);
288
289 std::shared_ptr<RSRenderPropertyBase> propTwo;
290 modifier->Update(propTwo, true);
291 ASSERT_EQ(propTwo, nullptr);
292 }
293
294 /**
295 * @tc.name: CanvasNull
296 * @tc.desc: Test Apply
297 * @tc.type:FUNC
298 * @tc.require: issueI9QIQO
299 */
300 HWTEST_F(RSRenderModifierTest, CanvasNull, TestSize.Level1)
301 {
302 ExtendRecordingCanvas canvas(100, 100);
303 canvas.Translate(15.f, 15.f);
304 auto prop = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
305 auto modifier = std::make_shared<RSDrawCmdListRenderModifier>(prop);
306 RSProperties properties;
307 RSModifierContext context(properties);
308 context.canvas_ = nullptr;
309 modifier->Apply(context);
310 bool ret = false;
311 ASSERT_EQ(ret, false);
312 }
313
314 /**
315 * @tc.name: CanvasNotNull
316 * @tc.desc: Test Apply
317 * @tc.type:FUNC
318 * @tc.require: issueI9QIQO
319 */
320 HWTEST_F(RSRenderModifierTest, CanvasNotNull, TestSize.Level1)
321 {
322 ExtendRecordingCanvas canvas(100, 100);
323 canvas.Translate(15.f, 15.f);
324 auto prop = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
325 auto modifier = std::make_shared<RSDrawCmdListRenderModifier>(prop);
326 RSProperties properties;
327 RSModifierContext context(properties);
328 Drawing::Canvas* canvas1 = new Drawing::Canvas();
329 context.canvas_ = new RSPaintFilterCanvas(canvas1);
330 modifier->Apply(context);
331
332 bool ret = true;
333 ASSERT_EQ(ret, true);
334 }
335
336 /**
337 * @tc.name: Marshalling
338 * @tc.desc: Test Marshalling
339 * @tc.type:FUNC
340 * @tc.require: issueI9QIQO
341 */
342 HWTEST_F(RSRenderModifierTest, Marshalling, TestSize.Level1)
343 {
344 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
345 auto RSEFC = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
346 Parcel parcel;
347 bool ret = RSEFC->Marshalling(parcel);
348 ASSERT_TRUE(ret == true);
349 }
350
351 /**
352 * @tc.name: Apply001
353 * @tc.desc: Test Apply
354 * @tc.type:FUNC
355 * @tc.require: issueI9QIQO
356 */
357 HWTEST_F(RSRenderModifierTest, Apply001, TestSize.Level1)
358 {
359 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
360 auto RSEFC = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
361 ExtendRecordingCanvas canvas(100, 100);
362 RSPaintFilterCanvas paintFilterCanvas(&canvas);
363 RSProperties properties;
364 RSModifierContext contextArgs(properties, &paintFilterCanvas);
365 RSEFC->Apply(contextArgs);
366 bool ret = true;
367 ASSERT_TRUE(ret == true);
368 }
369
370 /**
371 * @tc.name: Marshalling001
372 * @tc.desc: Test Marshalling
373 * @tc.type:FUNC
374 * @tc.require: issueI9QIQO
375 */
376 HWTEST_F(RSRenderModifierTest, Marshalling001, TestSize.Level1)
377 {
378 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
379 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
380 Parcel parcel;
381 bool ret = modifier->Marshalling(parcel);
382 ASSERT_TRUE(ret == true);
383 }
384
385 /**
386 * @tc.name: Apply002
387 * @tc.desc: Test Marshalling
388 * @tc.type:FUNC
389 * @tc.require: issueI9QIQO
390 */
391 HWTEST_F(RSRenderModifierTest, Apply002, TestSize.Level1)
392 {
393 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
394 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
395 RSProperties properties;
396 RSModifierContext context(properties);
397 modifier->Apply(context);
398 bool ret = true;
399 ASSERT_TRUE(ret == true);
400 }
401
402 /**
403 * @tc.name: GetInvertBackgroundColor
404 * @tc.desc: Test GetInvertBackgroundColor
405 * @tc.type:FUNC
406 * @tc.require: issueI9QIQO
407 */
408 HWTEST_F(RSRenderModifierTest, GetInvertBackgroundColor, TestSize.Level1)
409 {
410 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
411 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
412 RSProperties properties;
413 RSModifierContext context(properties);
414 ExtendRecordingCanvas canvas(100, 100);
415 Drawing::Surface surface;
416 RSPaintFilterCanvas paintFilterCanvas(&canvas);
417 paintFilterCanvas.surface_ = &surface;
418 RSModifierContext contextArgs(properties, &paintFilterCanvas);
419 RSColor color;
420 color.SetAlpha(0xff);
421 contextArgs.properties_.SetBackgroundColor(color);
422 modifier->GetInvertBackgroundColor(contextArgs);
423 bool ret = true;
424 ASSERT_TRUE(ret == true);
425 }
426
427 /**
428 * @tc.name: Update001
429 * @tc.desc: Test Update
430 * @tc.type:FUNC
431 * @tc.require: issueI9QIQO
432 */
433 HWTEST_F(RSRenderModifierTest, Update001, TestSize.Level1)
434 {
435 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
436 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
437 RSProperties properties;
438 RSModifierContext context(properties);
439 const std::shared_ptr<RSRenderPropertyBase> prop;
440 bool isDelta = false;
441 modifier->Update(prop, isDelta);
442 bool ret = true;
443 ASSERT_TRUE(ret == true);
444 }
445
446 /**
447 * @tc.name: Apply003
448 * @tc.desc: Test Apply
449 * @tc.type:FUNC
450 * @tc.require: issueI9QIQO
451 */
452 HWTEST_F(RSRenderModifierTest, Apply003, TestSize.Level1)
453 {
454 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
455 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
456 RSProperties properties;
457 RSModifierContext context(properties);
458 auto test = std::make_shared<RSRenderProperty<ForegroundColorStrategyType>>();
459 modifier->property_ = test;
460 modifier->Apply(context);
461 bool ret = true;
462 ASSERT_TRUE(ret == true);
463 }
464
465 /**
466 * @tc.name: CalculateInvertColor
467 * @tc.desc: Test CalculateInvertColor
468 * @tc.type:FUNC
469 * @tc.require: issueI9QIQO
470 */
471 HWTEST_F(RSRenderModifierTest, CalculateInvertColor, TestSize.Level1)
472 {
473 auto property = std::make_shared<RSRenderAnimatableProperty<Color>>();
474 auto modifier = std::make_shared<RSEnvForegroundColorStrategyRenderModifier>(property);
475 RSProperties properties;
476 RSModifierContext context(properties);
477 Color backgroundColor;
478 modifier->CalculateInvertColor(backgroundColor);
479 bool ret = true;
480 ASSERT_TRUE(ret == true);
481 }
482 }
483