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, Hardware
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 "effect/blur_draw_looper.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 class BlurDrawLooperTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase()34 void BlurDrawLooperTest::SetUpTestCase() {}
TearDownTestCase()35 void BlurDrawLooperTest::TearDownTestCase() {}
SetUp()36 void BlurDrawLooperTest::SetUp() {}
TearDown()37 void BlurDrawLooperTest::TearDown() {}
38
39 /**
40 * @tc.name: CreateDashPathEffect001
41 * @tc.desc:
42 * @tc.type: FUNC
43 * @tc.require: AR20240104201189
44 * @tc.author:
45 */
46 HWTEST_F(BlurDrawLooperTest, CreateBlurDrawLooperTest001, TestSize.Level1)
47 {
48 // 1.f 2.f 3.f and 0x12345678 is setted to compare.
49 float radius = 1.f;
50 Point point{2.f, 3.f};
51 Color color = Color(0x12345678);
52 std::shared_ptr<BlurDrawLooper> blurDrawLooper = BlurDrawLooper::CreateBlurDrawLooper(radius,
53 point.GetX(), point.GetY(), color);
54 EXPECT_NE(blurDrawLooper, nullptr);
55 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper->GetRadius(), radius));
56 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper->GetXOffset(), point.GetX()));
57 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper->GetYOffset(), point.GetY()));
58 EXPECT_TRUE(color == blurDrawLooper->GetColor());
59
60 // except branch
61 std::shared_ptr<BlurDrawLooper> blurDrawLooper2 = BlurDrawLooper::CreateBlurDrawLooper(-1.f,
62 2.f, 3.f, Color(0x12345678));
63 EXPECT_EQ(blurDrawLooper2, nullptr);
64 }
65
66 /**
67 * @tc.name: BlurDrawLooperEqualTest002
68 * @tc.desc:
69 * @tc.type: FUNC
70 * @tc.require: AR20240104201189
71 * @tc.author:
72 */
73 HWTEST_F(BlurDrawLooperTest, BlurDrawLooperEqualTest002, TestSize.Level1)
74 {
75 // 1.f 2.f 3.f and 0x12345678 is setted to compare.
76 float radius = 1.f;
77 Point point{2.f, 3.f};
78 Color color = Color(0x12345678);
79 std::shared_ptr<BlurDrawLooper> blurDrawLooper1 = BlurDrawLooper::CreateBlurDrawLooper(radius,
80 point.GetX(), point.GetY(), color);
81 EXPECT_NE(blurDrawLooper1, nullptr);
82
83 std::shared_ptr<BlurDrawLooper> blurDrawLooper2 = BlurDrawLooper::CreateBlurDrawLooper(radius,
84 point.GetX(), point.GetY(), color);
85 EXPECT_NE(blurDrawLooper2, nullptr);
86 // Color(0x87654321) and color, not same
87 std::shared_ptr<BlurDrawLooper> blurDrawLooper3 = BlurDrawLooper::CreateBlurDrawLooper(radius,
88 point.GetX(), point.GetY(), Color(0x87654321));
89 EXPECT_NE(blurDrawLooper3, nullptr);
90 // Point{1.f, -1.f} and point, not same
91 std::shared_ptr<BlurDrawLooper> blurDrawLooper4 = BlurDrawLooper::CreateBlurDrawLooper(radius,
92 1.f, -1.f, color);
93 EXPECT_NE(blurDrawLooper4, nullptr);
94 // 3.f and radius, not same
95 std::shared_ptr<BlurDrawLooper> blurDrawLooper5 = BlurDrawLooper::CreateBlurDrawLooper(3.f,
96 point.GetX(), point.GetY(), color);
97 EXPECT_NE(blurDrawLooper5, nullptr);
98
99 EXPECT_TRUE(*blurDrawLooper1 == *blurDrawLooper2);
100 EXPECT_TRUE(*blurDrawLooper1 != *blurDrawLooper3);
101 EXPECT_TRUE(*blurDrawLooper1 != *blurDrawLooper4);
102 EXPECT_TRUE(*blurDrawLooper1 != *blurDrawLooper5);
103 }
104
105 /**
106 * @tc.name: BlurDrawLooperSerializeTest003
107 * @tc.desc:
108 * @tc.type: FUNC
109 * @tc.require: AR20240104201189
110 * @tc.author:
111 */
112 HWTEST_F(BlurDrawLooperTest, BlurDrawLooperSerializeTest003, TestSize.Level1)
113 {
114 // 1.f 2.f 3.f and 0x12345678 is setted to compare.
115 float radius = 1.f;
116 Point point{2.f, 3.f};
117 Color color = Color(0x12345678);
118 std::shared_ptr<BlurDrawLooper> blurDrawLooper1 = BlurDrawLooper::CreateBlurDrawLooper(radius,
119 point.GetX(), point.GetY(), color);
120 EXPECT_NE(blurDrawLooper1, nullptr);
121
122 // serialize
123 std::shared_ptr<Data> data = blurDrawLooper1->Serialize();
124 EXPECT_NE(data, nullptr);
125 EXPECT_NE(data->GetData(), nullptr);
126 EXPECT_NE(data->GetSize(), 0);
127
128 // deserialize
129 std::shared_ptr<BlurDrawLooper> blurDrawLooper2 = BlurDrawLooper::Deserialize(data);
130 EXPECT_NE(blurDrawLooper2, nullptr);
131 EXPECT_NE(blurDrawLooper2->GetMaskFilter(), nullptr);
132 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper2->GetRadius(), radius));
133 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper2->GetXOffset(), point.GetX()));
134 EXPECT_TRUE(IsScalarAlmostEqual(blurDrawLooper2->GetYOffset(), point.GetY()));
135 EXPECT_TRUE(blurDrawLooper2->GetColor() == color);
136
137 // compare
138 EXPECT_TRUE(*blurDrawLooper1 == *blurDrawLooper2);
139
140 //except branch
141 EXPECT_EQ(BlurDrawLooper::Deserialize(nullptr), nullptr);
142 auto emptyData = std::make_shared<Data>();
143 EXPECT_EQ(BlurDrawLooper::Deserialize(emptyData), nullptr);
144 }
145 } // namespace Drawing
146 } // namespace Rosen
147 } // namespace OHOS
148