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 #include "symbol_gradient.h" 18 #include "convert.h" 19 20 21 using namespace testing; 22 using namespace testing::ext; 23 24 namespace OHOS { 25 namespace Rosen { 26 class OHHmSymbolGradientTest : public testing::Test {}; 27 28 /* 29 * @tc.name: SymbolGradientTest001 30 * @tc.desc: test SymbolGradient 31 * @tc.type: FUNC 32 */ 33 HWTEST_F(OHHmSymbolGradientTest, SymbolGradientTest001, TestSize.Level0) 34 { 35 // init data 36 SymbolGradient gradient = SymbolGradient(); 37 std::vector<Drawing::ColorQuad> colors = {0xFF00FF00, 0XFFFF0000}; // 0xFF00FF00 and 0XFFFF0000 is ARGB 38 std::vector<float> positions = {0.2f, 0.9f}; // 0.2f and 0.9f is position of colors 39 40 // test setColors 41 gradient.SetColors(colors); 42 EXPECT_FALSE(gradient.GetColors().empty()); 43 EXPECT_TRUE(gradient.GetGradientType() == GradientType::NONE_GRADIENT); 44 45 // test SetPositions 46 gradient.SetPositions(positions); 47 EXPECT_FALSE(gradient.GetPositions().empty()); 48 49 // test SetTileMode 50 gradient.SetTileMode(Drawing::TileMode::REPEAT); 51 EXPECT_TRUE(gradient.GetTileMode() == Drawing::TileMode::REPEAT); 52 53 // 0.0f left, 0.0f top, 100.0f right, 100.0f bottom 54 Drawing::Rect bounds = Drawing::Rect(0.0f, 0.0f, 100.0f, 100.0f); 55 Drawing::Point offset = Drawing::Point(0.5f, 0.6f); // 0.5f: x, 0.6f: y 56 gradient.Make(bounds); 57 auto brush = gradient.CreateGradientBrush(); 58 auto pen = gradient.CreateGradientPen(); 59 auto shader = gradient.CreateGradientShader(offset); 60 EXPECT_EQ(shader, nullptr); 61 } 62 63 /* 64 * @tc.name: IsNearlyEqual001 65 * @tc.desc: test IsNearlyEqual 66 * @tc.type: FUNC 67 */ 68 HWTEST_F(OHHmSymbolGradientTest, IsNearlyEqual001, TestSize.Level0) 69 { 70 // init data 71 auto gradient1 = std::make_shared<SymbolGradient>(); 72 auto gradient2 = std::make_shared<SymbolGradient>(); 73 std::vector<Drawing::ColorQuad> colors = {0xFF00FF00, 0XFFFF0000}; // 0xFF00FF00 and 0XFFFF0000 is ARGB 74 std::vector<float> positions = {0.2f, 0.9f}; // 0.2f and 0.9f is position of colors 75 76 // test colors is empty 77 auto brush = gradient1->CreateGradientBrush(); 78 auto pen = gradient1->CreateGradientPen(); 79 EXPECT_TRUE(gradient1->IsNearlyEqual(gradient2)); 80 81 // test gradientType 82 auto gradient3 = std::make_shared<SymbolLineGradient>(45.0f); // 45.0f is angle of linegradient 83 EXPECT_FALSE(gradient1->IsNearlyEqual(gradient3)); 84 85 // test colors 86 gradient1->SetColors(colors); 87 EXPECT_FALSE(gradient1->IsNearlyEqual(gradient2)); 88 89 std::vector<Drawing::ColorQuad> colors1 = {0xFF00FFFF, 0XFFFF0000}; // 0xFF00FFFF and 0XFFFF0000 is ARGB 90 gradient2->SetColors(colors1); 91 EXPECT_FALSE(gradient1->IsNearlyEqual(gradient2)); 92 93 gradient2->SetColors(colors); 94 EXPECT_TRUE(gradient1->IsNearlyEqual(gradient2)); 95 96 // test positions 97 gradient2->SetPositions(positions); 98 EXPECT_FALSE(gradient1->IsNearlyEqual(gradient2)); 99 100 std::vector<float> positions1 = {0.3f, 0.9f}; // 0.3f and 0.9f is position of colors 101 gradient1->SetPositions(positions1); 102 EXPECT_FALSE(gradient1->IsNearlyEqual(gradient2)); 103 104 gradient1->SetPositions(positions); 105 EXPECT_TRUE(gradient1->IsNearlyEqual(gradient2)); 106 107 // test nullptr 108 EXPECT_FALSE(gradient1->IsNearlyEqual(nullptr)); 109 } 110 111 112 /* 113 * @tc.name: SymbolLineGradient001 114 * @tc.desc: test SymbolLineGradient 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(OHHmSymbolGradientTest, SymbolLineGradient001, TestSize.Level0) 118 { 119 // init data 120 SymbolLineGradient gradient = SymbolLineGradient(45.0f); // 45.0f is angle of linegradient 121 std::vector<Drawing::ColorQuad> colors = {0XFF00FF00, 0XFFFF0000}; // 0XFF00FF00 and 0XFFFF0000 is ARGB 122 std::vector<float> positions = {0.2f, 0.9f}; // 0.2f and 0.9f is position of colors 123 // 0.0f left, 0.0f top, 100.0f right, 100.0f bottom 124 Drawing::Rect bounds = Drawing::Rect(0.0f, 0.0f, 100.0f, 100.0f); 125 Drawing::Point offset = Drawing::Point(0.5f, 0.6f); // 0.5f: x, 0.6f: y 126 127 // test colors is empty 128 auto brush = gradient.CreateGradientBrush(offset); 129 auto pen = gradient.CreateGradientPen(offset); 130 EXPECT_EQ(brush.GetShaderEffect(), nullptr); 131 132 // test colors is not empty 133 gradient.SetColors(colors); 134 gradient.SetPositions(positions); 135 gradient.SetPositions(positions); 136 gradient.Make(bounds); 137 brush = gradient.CreateGradientBrush(offset); 138 pen = gradient.CreateGradientPen(offset); 139 EXPECT_NE(brush.GetShaderEffect(), nullptr); 140 } 141 142 /* 143 * @tc.name: PointsFromAngle001 144 * @tc.desc: test PointsFromAngle 145 * @tc.type: FUNC 146 */ 147 HWTEST_F(OHHmSymbolGradientTest, PointsFromAngle001, TestSize.Level0) 148 { 149 // init data 150 // 0.0f left, 0.0f top, 100.0f right, 100.0f bottom 151 Drawing::Rect bounds = Drawing::Rect(0.0f, 0.0f, 100.0f, 100.0f); 152 Drawing::Point firstPoint; 153 Drawing::Point secondPoint; 154 SymbolLineGradient gradient = SymbolLineGradient(-45.0f); // -45.0f is angle 155 gradient.Make(bounds); 156 157 // test angle is 0.0f 158 gradient.PointsFromAngle(0.0f, bounds, firstPoint, secondPoint); 159 EXPECT_EQ(firstPoint.GetY(), bounds.GetHeight()); 160 161 // test angle is 90.0f 162 gradient.PointsFromAngle(90.0f, bounds, firstPoint, secondPoint); 163 EXPECT_EQ(secondPoint.GetX(), bounds.GetWidth()); 164 165 // test angle is 180.0f 166 gradient.PointsFromAngle(180.0f, bounds, firstPoint, secondPoint); 167 EXPECT_EQ(secondPoint.GetY(), bounds.GetHeight()); 168 169 // test angle is 270.0f 170 gradient.PointsFromAngle(270.0f, bounds, firstPoint, secondPoint); 171 EXPECT_EQ(firstPoint.GetX(), bounds.GetWidth()); 172 173 auto half = 50.0f; // 50.0f is half of widht 174 // test angle is 45.0f < 90.0f 175 gradient.PointsFromAngle(45.0f, bounds, firstPoint, secondPoint); 176 EXPECT_TRUE(secondPoint.GetY() < half); 177 EXPECT_TRUE(secondPoint.GetX() > half); 178 179 // test angle is 135.0f <180.0f 180 gradient.PointsFromAngle(135.0f, bounds, firstPoint, secondPoint); 181 EXPECT_TRUE(secondPoint.GetY() > half); 182 EXPECT_TRUE(secondPoint.GetX() > half); 183 184 // test angle is 200.0f < 270.0f 185 gradient.PointsFromAngle(200.0f, bounds, firstPoint, secondPoint); 186 EXPECT_TRUE(secondPoint.GetY() > half); 187 EXPECT_TRUE(secondPoint.GetX() < half); 188 189 // test angle is 300.0f < 360.0f 190 gradient.PointsFromAngle(300.0f, bounds, firstPoint, secondPoint); 191 EXPECT_TRUE(secondPoint.GetY() < half); 192 EXPECT_TRUE(secondPoint.GetX() < half); 193 } 194 195 /* 196 * @tc.name: SymbolRadialGradient001 197 * @tc.desc: test PointsFromAngle 198 * @tc.type: FUNC 199 */ 200 HWTEST_F(OHHmSymbolGradientTest, SymbolRadialGradient001, TestSize.Level0) 201 { 202 // init data 203 Drawing::Point centerPt = Drawing::Point(0.5f, 0.5f); // 0.5f: x, 0.5f: y 204 float radiusRatio = 0.6f; // 0.6f is radius 205 SymbolRadialGradient gradient = SymbolRadialGradient(centerPt, radiusRatio); 206 std::vector<Drawing::ColorQuad> colors = {0xFF00FF00, 0XFFFF0000}; // 0xFF00FF00 and 0XFFFF0000 is ARGB 207 std::vector<float> positions = {0.2f, 0.9f}; // 0.2f and 0.9f is position of colors 208 // 0.0f left, 0.0f top, 100.0f right, 100.0f bottom 209 Drawing::Rect bounds = Drawing::Rect(0.0f, 0.0f, 100.0f, 100.0f); 210 Drawing::Point offset = Drawing::Point(0.5f, 0.6f); // 0.5f: x, 0.6f: y 211 212 // test colors is empty 213 auto brush = gradient.CreateGradientBrush(offset); 214 auto pen = gradient.CreateGradientPen(offset); 215 EXPECT_EQ(brush.GetShaderEffect(), nullptr); 216 217 // test colors is not empty 218 gradient.SetColors(colors); 219 gradient.SetPositions(positions); 220 gradient.SetPositions(positions); 221 gradient.SetCenterPoint(centerPt); 222 gradient.SetRadiusRatio(radiusRatio); 223 gradient.Make(bounds); 224 brush = gradient.CreateGradientBrush(offset); 225 pen = gradient.CreateGradientPen(offset); 226 EXPECT_NE(brush.GetShaderEffect(), nullptr); 227 228 // test SetRadius 229 gradient.SetRadius(10.0f); // 10.0f is radius of radialGradient 230 EXPECT_EQ(gradient.GetRadius(), 10.0f); // 10.0f is radius 231 gradient.Make(bounds); 232 EXPECT_FALSE(gradient.isRadiusRatio_); 233 } 234 235 /* 236 * @tc.name: SymbolRadialGradient_SetRadius 237 * @tc.desc: test SetRadius of SymbolRadialGradient by radius < 0 238 * @tc.type: FUNC 239 */ 240 HWTEST_F(OHHmSymbolGradientTest, SymbolRadialGradient_SetRadius, TestSize.Level0) 241 { 242 Drawing::Point centerPt = Drawing::Point(0.5f, 0.5f); // 0.5f: x, 0.5f: y 243 float radiusRatio = -0.6f; // -0.6f test radiusRatio < 0 244 SymbolRadialGradient gradient = SymbolRadialGradient(centerPt, radiusRatio); 245 EXPECT_EQ(gradient.GetRadiusRatio(), 0.0f); 246 247 gradient.SetRadius(-10.5f); // -10.5f is radius < 0 248 EXPECT_EQ(gradient.GetRadius(), 0.0f); 249 gradient.SetRadiusRatio(-0.5f); // -0.5f is RadiusRatios < 0 250 EXPECT_EQ(gradient.GetRadiusRatio(), 0.0f); 251 } 252 } // namespace Rosen 253 } // namespace OHOS