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, 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 <string> 17 #include "gtest/gtest.h" 18 19 #define private public 20 #define protected public 21 22 #include "test/mock/core/rosen/mock_canvas.h" 23 #include "test/mock/core/rosen/testing_rect.h" 24 #include "frameworks/core/components_ng/svg/svg_ulils.h" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 namespace OHOS::Ace::NG { 30 class SvgUtilsTestNg : public testing::Test {}; 31 32 /** 33 * @tc.name: SvgCalculate001 34 * @tc.desc: test view box invalid 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(SvgUtilsTestNg, SvgCalculate001, TestSize.Level1) 38 { 39 Size svgContentSize; 40 Size svgContainerSize = { 100, 100 }; 41 Size svgSize = { -50, -50 }; 42 Rect viewBox = { -4.0, -10.0, -300.0, -300.0 }; 43 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 44 svgSize, viewBox); 45 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgContainerSize.Width()); 46 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgContainerSize.Height()); 47 } 48 49 /** 50 * @tc.name: SvgCalculate002 51 * @tc.desc: test view box invalid 52 * @tc.type: FUNC 53 */ 54 HWTEST_F(SvgUtilsTestNg, SvgCalculate002, TestSize.Level1) 55 { 56 Size svgContentSize; 57 Size svgContainerSize = { 100, 100 }; 58 Size svgSize = { 50, -50 }; 59 Rect viewBox = { -4.0, -10.0, -300.0, -300.0 }; 60 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 61 svgSize, viewBox); 62 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgSize.Width()); 63 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgContainerSize.Height()); 64 } 65 66 /** 67 * @tc.name: SvgCalculate003 68 * @tc.desc: test view box invalid 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(SvgUtilsTestNg, SvgCalculate003, TestSize.Level1) 72 { 73 Size svgContentSize; 74 Size svgContainerSize = { 100, 100 }; 75 Size svgSize = { -50, 50 }; 76 Rect viewBox = { -4.0, -10.0, -300.0, -300.0 }; 77 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 78 svgSize, viewBox); 79 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgContainerSize.Width()); 80 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgSize.Height()); 81 } 82 83 /** 84 * @tc.name: SvgCalculate004 85 * @tc.desc: test view box invalid 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(SvgUtilsTestNg, SvgCalculate004, TestSize.Level1) 89 { 90 Size svgContentSize; 91 Size svgContainerSize = { 0, 0 }; 92 Size svgSize = { -50, -50 }; 93 Rect viewBox = { -4.0, -10.0, -300.0, -300.0 }; 94 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 95 svgSize, viewBox); 96 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgContainerSize.Width()); 97 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgContainerSize.Height()); 98 } 99 100 /** 101 * @tc.name: SvgCalculate005 102 * @tc.desc: test view box valid and svg invalid 103 * @tc.type: FUNC 104 */ 105 HWTEST_F(SvgUtilsTestNg, SvgCalculate005, TestSize.Level1) 106 { 107 Size svgContentSize; 108 Size svgContainerSize = { 0, 0}; 109 Size svgSize = { -50, -50 }; 110 Rect viewBox = { -4.0, -10.0, 300.0, 300.0 }; 111 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 112 svgSize, viewBox); 113 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgContainerSize.Width()); 114 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgContainerSize.Height()); 115 } 116 117 /** 118 * @tc.name: SvgCalculate006 119 * @tc.desc: test view box valid and svg width valid, height invalid 120 * @tc.type: FUNC 121 */ 122 HWTEST_F(SvgUtilsTestNg, SvgCalculate006, TestSize.Level1) 123 { 124 Size svgContentSize; 125 Size svgContainerSize = { 0, 0 }; 126 Size svgSize = { 50, -50 }; 127 Rect viewBox = { -4.0, -10.0, 300.0, 300.0 }; 128 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 129 svgSize, viewBox); 130 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgSize.Width()); 131 EXPECT_FLOAT_EQ(svgContentSize.Height(), viewBox.Height() / viewBox.Width() * svgSize.Width()); 132 } 133 134 /** 135 * @tc.name: SvgCalculate007 136 * @tc.desc: test view box valid and svg width valid, height invalid 137 * @tc.type: FUNC 138 */ 139 HWTEST_F(SvgUtilsTestNg, SvgCalculate007, TestSize.Level1) 140 { 141 Size svgContentSize; 142 Size svgContainerSize = { 0, 0 }; 143 Size svgSize = { -50, 50 }; 144 Rect viewBox = { -4.0, -10.0, 300.0, 300.0 }; 145 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 146 svgSize, viewBox); 147 EXPECT_FLOAT_EQ(svgContentSize.Width(), viewBox.Width() / viewBox.Height() * svgSize.Height()); 148 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgSize.Height()); 149 } 150 151 /** 152 * @tc.name: SvgCalculate008 153 * @tc.desc: test view box valid and svg valid 154 * @tc.type: FUNC 155 */ 156 HWTEST_F(SvgUtilsTestNg, SvgCalculate008, TestSize.Level1) 157 { 158 Size svgContentSize; 159 Size svgContainerSize = { 0, 0 }; 160 Size svgSize = { 50, 50 }; 161 Rect viewBox = { -4.0, -10.0, 300.0, 300.0 }; 162 SvgUtils::CalculateSvgConentSize(svgContentSize, svgContainerSize, 163 svgSize, viewBox); 164 EXPECT_FLOAT_EQ(svgContentSize.Width(), svgSize.Width()); 165 EXPECT_FLOAT_EQ(svgContentSize.Height(), svgSize.Height()); 166 } 167 } // namespace OHOS::Ace::NG 168