1 /*
2  * Copyright (c) 2023 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 <vector>
17 
18 #include "gtest/gtest.h"
19 #include "pixel_map.h"
20 
21 #include "animation/rs_render_particle_animation.h"
22 #include "common/rs_vector2.h"
23 #include "modifier/rs_render_property.h"
24 #include "pipeline/rs_canvas_render_node.h"
25 #include "render/rs_image.h"
26 #include "render/rs_image_cache.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 namespace Rosen {
33 class RSRenderParticleTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp() override;
38     void TearDown() override;
39     static constexpr uint64_t ANIMATION_ID = 12345;
40     static constexpr uint64_t PROPERTY_ID = 54321;
41     std::shared_ptr<ParticleRenderParams> params;
42     std::shared_ptr<RSRenderParticle> particle;
43 };
44 
SetUpTestCase()45 void RSRenderParticleTest::SetUpTestCase() {}
TearDownTestCase()46 void RSRenderParticleTest::TearDownTestCase() {}
SetUp()47 void RSRenderParticleTest::SetUp()
48 {
49     int emitRate = 20;
50     ShapeType emitShape = ShapeType::RECT;
51     Vector2f position = Vector2f(0.f, 0.f);
52     Vector2f emitSize = Vector2f(10.f, 10.f);
53     int particleCount = 20;
54     Range<int64_t> lifeTime = Range<int64_t>(3000, 3000); // 3000 is lifeTime.
55     ParticleType type = ParticleType::POINTS;
56     float radius = 1;
57     std::shared_ptr<RSImage> image;
58     Vector2f imageSize = Vector2f(1.f, 1.f);
59     EmitterConfig emitterConfig =
60         EmitterConfig(emitRate, emitShape, position, emitSize, particleCount, lifeTime, type, radius, image, imageSize);
61     ParticleVelocity velocity;
62     RenderParticleAcceleration acceleration;
63     RenderParticleColorParaType color;
64     RenderParticleParaType<float> opacity;
65     RenderParticleParaType<float> scale;
66     RenderParticleParaType<float> spin;
67     params = std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
68     particle = std::make_shared<RSRenderParticle>(params);
69 }
TearDown()70 void RSRenderParticleTest::TearDown() {}
71 
CreatePixelMap(int width,int height)72 static std::shared_ptr<Media::PixelMap> CreatePixelMap(int width, int height)
73 {
74     Media::InitializationOptions opts;
75     opts.size.width = width;
76     opts.size.height = height;
77     auto pixelmap = Media::PixelMap::Create(opts);
78     auto address = const_cast<uint32_t*>(pixelmap->GetPixel32(0, 0));
79     if (address == nullptr) {
80         return nullptr;
81     }
82     Drawing::ImageInfo info { pixelmap->GetWidth(), pixelmap->GetHeight(), Drawing::ColorType::COLORTYPE_RGBA_8888,
83         Drawing::AlphaType::ALPHATYPE_PREMUL };
84     auto srfce = Drawing::Surface::MakeRasterDirect(info, address, pixelmap->GetRowBytes());
85     if (srfce == nullptr) {
86         return nullptr;
87     }
88     auto cnvs = srfce->GetCanvas();
89     if (cnvs == nullptr) {
90         return nullptr;
91     }
92     cnvs->Clear(Drawing::Color::COLOR_YELLOW);
93     Drawing::Brush brush;
94     brush.SetColor(Drawing::Color::COLOR_RED);
95     int a = 4;
96     int b = 2;
97     cnvs->AttachBrush(brush);
98     cnvs->DrawRect(Drawing::Rect(width / a, height / a, width / a + width / b, height / a + height / b));
99     cnvs->DetachBrush();
100     return pixelmap;
101 }
102 
103 /**
104  * @tc.name: InitProperty001
105  * @tc.desc: Verify the InitProperty
106  * @tc.type:FUNC
107  * @tc.require: issueIA6IWR
108  */
109 HWTEST_F(RSRenderParticleTest, InitProperty001, TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty001 start";
112     ASSERT_TRUE(particle != nullptr);
113     particle->InitProperty();
114     EXPECT_TRUE(particle != nullptr);
115     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty001 end";
116 }
117 
118 /**
119  * @tc.name: InitProperty002
120  * @tc.desc: Verify the InitProperty image
121  * @tc.type:FUNC
122  * @tc.require: issueIA6IWR
123  */
124 HWTEST_F(RSRenderParticleTest, InitProperty002, TestSize.Level1)
125 {
126     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty002 start";
127     int emitRate = 20;
128     ShapeType emitShape = ShapeType::RECT;
129     Vector2f position = Vector2f(0.f, 0.f);
130     Vector2f emitSize = Vector2f(10.f, 10.f);
131     int particleCount = 20;
132     Range<int64_t> lifeTime = Range<int64_t>(
133         std::numeric_limits<uint64_t>::max() / NS_PER_MS, std::numeric_limits<uint64_t>::max() / NS_PER_MS + 1);
134     ParticleType type = ParticleType::IMAGES;
135     float radius = 1;
136     std::shared_ptr<RSImage> rsImage = nullptr;
137     Vector2f imageSize = Vector2f(200.f, 300.f);
138     EmitterConfig emitterConfig = EmitterConfig(
139         emitRate, emitShape, position, emitSize, particleCount, lifeTime, type, radius, rsImage, imageSize);
140     ParticleVelocity velocity;
141     RenderParticleAcceleration acceleration;
142     RenderParticleColorParaType color;
143     RenderParticleParaType<float> opacity;
144     RenderParticleParaType<float> scale;
145     RenderParticleParaType<float> spin;
146     params = std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
147     particle = std::make_shared<RSRenderParticle>(params);
148     ASSERT_TRUE(particle != nullptr);
149     particle->InitProperty();
150     EXPECT_TRUE(particle != nullptr);
151     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty002 end";
152 }
153 
154 /**
155  * @tc.name: InitProperty003
156  * @tc.desc: Verify the InitProperty image
157  * @tc.type:FUNC
158  * @tc.require: issueIA6IWR
159  */
160 HWTEST_F(RSRenderParticleTest, InitProperty003, TestSize.Level1)
161 {
162     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty003 start";
163     int emitRate = 20;
164     ShapeType emitShape = ShapeType::RECT;
165     Vector2f position = Vector2f(0.f, 0.f);
166     Vector2f emitSize = Vector2f(10.f, 10.f);
167     int particleCount = 20;
168     Range<int64_t> lifeTime = Range<int64_t>(0, 3000); // 3000 is lifeTime range end.
169     ParticleType type = ParticleType::IMAGES;
170     float radius = 1;
171     std::shared_ptr<RSImage> rsImage = std::make_shared<RSImage>();
172     std::shared_ptr<Media::PixelMap> pixelMap = nullptr;
173     ASSERT_TRUE(rsImage != nullptr);
174     rsImage->SetPixelMap(pixelMap);
175     Vector2f imageSize = Vector2f(200.f, 300.f);
176     EmitterConfig emitterConfig = EmitterConfig(
177         emitRate, emitShape, position, emitSize, particleCount, lifeTime, type, radius, rsImage, imageSize);
178     ParticleVelocity velocity;
179     RenderParticleAcceleration acceleration;
180     RenderParticleColorParaType color;
181     RenderParticleParaType<float> opacity;
182     RenderParticleParaType<float> scale;
183     RenderParticleParaType<float> spin;
184     params = std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
185     particle = std::make_shared<RSRenderParticle>(params);
186     ASSERT_TRUE(particle != nullptr);
187     particle->InitProperty();
188     EXPECT_TRUE(particle != nullptr);
189     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty003 end";
190 }
191 
192 /**
193  * @tc.name: InitProperty004
194  * @tc.desc: Verify the InitProperty image
195  * @tc.type:FUNC
196  * @tc.require: issueIA6IWR
197  */
198 HWTEST_F(RSRenderParticleTest, InitProperty004, TestSize.Level1)
199 {
200     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty003 start";
201     int emitRate = 20;
202     ShapeType emitShape = ShapeType::ELLIPSE;
203     Vector2f position = Vector2f(0.f, 0.f);
204     Vector2f emitSize = Vector2f(10.f, 10.f);
205     int particleCount = 20;
206     Range<int64_t> lifeTime = Range<int64_t>(-1, -1);
207     ParticleType type = ParticleType::IMAGES;
208     float radius = 1;
209     std::shared_ptr<RSImage> rsImage = std::make_shared<RSImage>();
210     ASSERT_TRUE(rsImage != nullptr);
211     std::shared_ptr<Media::PixelMap> pixelMap;
212     int width = 200;
213     int height = 300;
214     pixelMap = CreatePixelMap(width, height);
215     rsImage->SetPixelMap(pixelMap);
216     Vector2f imageSize = Vector2f(200.f, 300.f);
217     EmitterConfig emitterConfig = EmitterConfig(
218         emitRate, emitShape, position, emitSize, particleCount, lifeTime, type, radius, rsImage, imageSize);
219     ParticleVelocity velocity;
220     RenderParticleAcceleration acceleration;
221     RenderParticleColorParaType color;
222     RenderParticleParaType<float> opacity;
223     RenderParticleParaType<float> scale;
224     RenderParticleParaType<float> spin;
225     params = std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
226     particle = std::make_shared<RSRenderParticle>(params);
227     ASSERT_TRUE(particle != nullptr);
228     particle->InitProperty();
229     EXPECT_TRUE(particle != nullptr);
230     GTEST_LOG_(INFO) << "RSRenderParticleTest InitProperty004 end";
231 }
232 
233 /**
234  * @tc.name: IsAlive001
235  * @tc.desc: Verify the IsAlive
236  * @tc.type:FUNC
237  * @tc.require: issueIA6IWR
238  */
239 HWTEST_F(RSRenderParticleTest, IsAlive001, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "RSRenderParticleTest IsAlive001 start";
242     ASSERT_TRUE(particle != nullptr);
243     EXPECT_TRUE(particle->IsAlive() == true);
244     particle->SetIsDead();
245     EXPECT_TRUE(particle->IsAlive() == false);
246     GTEST_LOG_(INFO) << "RSRenderParticleTest IsAlive001 end";
247 }
248 
249 /**
250  * @tc.name: ColorLerp001
251  * @tc.desc: Verify the Color Lerp
252  * @tc.type:FUNC
253  * @tc.require: issueIA6IWR
254  */
255 HWTEST_F(RSRenderParticleTest, ColorLerp001, TestSize.Level1)
256 {
257     GTEST_LOG_(INFO) << "RSRenderParticleTest ColorLerp001 start";
258     ASSERT_TRUE(particle != nullptr);
259     Color start = Color(0, 0, 0, 128);
260     Color end = Color(255, 255, 255, 255);
261     float t = 0.5f;
262     Color color = particle->Lerp(start, end, t);
263     EXPECT_TRUE((color != start) && (color != end));
264     GTEST_LOG_(INFO) << "RSRenderParticleTest ColorLerp001 end";
265 }
266 
267 } // namespace Rosen
268 } // namespace OHOS