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 #ifndef USE_ROSEN_DRAWING
60 auto prop = std::make_shared<RSRenderProperty<SkMatrix>>();
61 #else
62 auto prop = std::make_shared<RSRenderProperty<Drawing::Matrix>>();
63 #endif
64 auto modifier = std::make_shared<RSGeometryTransRenderModifier>(prop);
65 RSProperties properties;
66 RSModifierContext context(properties);
67 auto rsRenderPropertyBase = std::make_shared<RSRenderProperty<Drawing::Matrix>>();
68 ASSERT_TRUE(modifier != nullptr);
69 modifier->Apply(context);
70 modifier->Update(rsRenderPropertyBase, false);
71 ASSERT_TRUE(modifier->GetProperty() == prop);
72 ASSERT_TRUE(modifier->GetPropertyId() == 0);
73 modifier->SetType(RSModifierType::BOUNDS);
74 ASSERT_TRUE(modifier->GetType() == RSModifierType::BOUNDS);
75
76 MessageParcel parcel;
77 ASSERT_TRUE(modifier->Marshalling(parcel));
78 ASSERT_TRUE(RSGeometryTransRenderModifier::Unmarshalling(parcel) != nullptr);
79 }
80
81 /**
82 * @tc.name: LifeCycle001
83 * @tc.desc:
84 * @tc.type:FUNC
85 */
86 HWTEST_F(RSRenderModifierTest, LifeCycle001, TestSize.Level1)
87 {
88 auto prop = std::make_shared<RSRenderProperty<float>>();
89 auto modifier = std::make_shared<RSAlphaRenderModifier>(prop);
90 ASSERT_TRUE(modifier != nullptr);
91 ASSERT_TRUE(modifier->GetProperty() == prop);
92 ASSERT_TRUE(modifier->GetPropertyId() == 0);
93
94 auto prop2 = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
95 auto modifier2 = std::make_shared<RSAlphaRenderModifier>(prop2);
96 ASSERT_TRUE(modifier2 != nullptr);
97 ASSERT_EQ(modifier2->GetPropertyId(), id);
98 }
99
100 /**
101 * @tc.name: Modifier001
102 * @tc.desc:
103 * @tc.type:FUNC
104 */
105 HWTEST_F(RSRenderModifierTest, Modifier001, TestSize.Level1)
106 {
107 auto prop = std::make_shared<RSRenderProperty<float>>(floatData[0], id);
108 auto modifier = std::make_shared<RSAlphaRenderModifier>(prop);
109 ASSERT_TRUE(prop != nullptr);
110 ASSERT_EQ(modifier->GetProperty(), prop);
111
112 auto prop1 = std::make_shared<RSRenderProperty<float>>(floatData[1], id);
113 modifier->Update(prop1, false);
114 ASSERT_EQ(std::static_pointer_cast<RSRenderProperty<float>>(modifier->GetProperty())->Get(), floatData[1]);
115
116 auto prop2 = std::make_shared<RSRenderProperty<float>>(floatData[2], id);
117 modifier->Update(prop2, true);
118 ASSERT_EQ(std::static_pointer_cast<RSRenderProperty<float>>(modifier->GetProperty())->Get(),
119 floatData[1] + floatData[2]);
120 }
121
122 /**
123 * @tc.name: DrawCmdListModifier001
124 * @tc.desc:
125 * @tc.type:FUNC
126 */
127 HWTEST_F(RSRenderModifierTest, DrawCmdListModifier001, TestSize.Level1)
128 {
129 #ifndef USE_ROSEN_DRAWING
130 RSRecordingCanvas canvas(100, 100);
131 canvas.translate(15.f, 15.f);
132 #else
133 ExtendRecordingCanvas canvas(100, 100);
134 canvas.Translate(15.f, 15.f);
135 #endif
136
137 #ifndef USE_ROSEN_DRAWING
138 auto prop = std::make_shared<RSRenderProperty<DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
139 #else
140 auto prop = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
141 #endif
142 auto modifier = std::make_shared<RSDrawCmdListRenderModifier>(prop);
143
144 MessageParcel parcel;
145 ASSERT_TRUE(modifier->Marshalling(parcel));
146 ASSERT_TRUE(RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr);
147
148 #ifndef USE_ROSEN_DRAWING
149 canvas.scale(2.f, 2.f);
150 #else
151 canvas.Scale(2.f, 2.f);
152 #endif
153 modifier->Update(nullptr, false);
154 #ifndef USE_ROSEN_DRAWING
155 auto prop1 = std::make_shared<RSRenderProperty<DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
156 #else
157 auto prop1 = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(canvas.GetDrawCmdList(), id);
158 #endif
159 modifier->Update(prop1, true);
160
161 ASSERT_TRUE(modifier->Marshalling(parcel));
162 ASSERT_TRUE(RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr);
163
164 MessageParcel parcel1;
165 char* buffer = static_cast<char *>(malloc(parcel1.GetMaxCapacity()));
166 memset_s(buffer, parcel1.GetMaxCapacity(), 0, parcel1.GetMaxCapacity());
167 ASSERT_TRUE(parcel1.WriteUnpadBuffer(buffer, parcel1.GetMaxCapacity()));
168 bool ret = false;
169 while (!ret) {
170 ret = (modifier->Marshalling(parcel) && (RSDrawCmdListRenderModifier::Unmarshalling(parcel) != nullptr));
171 parcel1.SetMaxCapacity(parcel1.GetMaxCapacity() + 1);
172 }
173 free(buffer);
174 ASSERT_TRUE(ret);
175 }
176
177 /**
178 * @tc.name: RSParticlesRenderModifier001
179 * @tc.desc:Update
180 * @tc.type:FUNC
181 */
182 HWTEST_F(RSRenderModifierTest, RSParticlesRenderModifier002, TestSize.Level1)
183 {
184 auto prop = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
185 bool isDelta = false;
186 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
187 auto RSPRM = std::make_shared<RSParticlesRenderModifier>(property);
188 RSPRM->Update(prop, isDelta);
189 ASSERT_NE(nullptr, RSPRM->property_);
190 }
191
192 /**
193 * @tc.name: RSEnvForegroundColorRenderModifier001
194 * @tc.desc:Update
195 * @tc.type:FUNC
196 */
197 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorRenderModifier001, TestSize.Level1)
198 {
199 auto prop = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
200 bool isDelta = true;
201 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
202 auto RSEFC = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
203 RSEFC->Update(prop, isDelta);
204 ASSERT_NE(nullptr, RSEFC->property_);
205 }
206
207 /**
208 * @tc.name: RSEnvForegroundColorRenderModifier002
209 * @tc.desc:Update
210 * @tc.type:FUNC
211 */
212 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorRenderModifier002, TestSize.Level1)
213 {
214 auto prop = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
215 bool isDelta = false;
216 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
217 auto RSEFC = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
218 RSEFC->Update(prop, isDelta);
219 ASSERT_NE(nullptr, RSEFC->property_);
220 }
221
222 /**
223 * @tc.name: RSEnvForegroundColorStrategyRenderModifier001
224 * @tc.desc:Apply
225 * @tc.type:FUNC
226 */
227 HWTEST_F(RSRenderModifierTest, RSEnvForegroundColorStrategyRenderModifier001, TestSize.Level1)
228 {
229 auto prop = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
230 bool isDelta = false;
231 auto property = std::make_shared<RSRenderProperty<RSRenderParticleVector>>();
232 auto RSEFCS = std::make_shared<RSEnvForegroundColorRenderModifier>(property);
233 RSEFCS->Update(prop, isDelta);
234 ASSERT_NE(nullptr, RSEFCS->property_);
235 }
236 }
237