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 18 #include "gtest/gtest.h" 19 20 #define private public 21 #define protected public 22 23 #include "base/memory/ace_type.h" 24 #include "test/mock/core/rosen/mock_canvas.h" 25 #include "test/mock/core/rosen/testing_rect.h" 26 #include "core/components_ng/svg/parse/svg_animation.h" 27 #include "core/components_ng/svg/parse/svg_circle.h" 28 #include "core/components_ng/svg/parse/svg_node.h" 29 #include "core/components_ng/svg/svg_dom.h" 30 #include "core/components_ng/svg/svg_ulils.h" 31 #include "include/core/SkStream.h" 32 33 using namespace testing; 34 using namespace testing::ext; 35 36 namespace OHOS::Ace::NG { 37 namespace { 38 const std::string CIRCLE_SVG_LABEL = 39 "<svg width=\"400px\" height=\"400px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\"> " 40 "<circle id=\"myCircle\" cx=\"60px\" " 41 "cy=\"200px\" r = \"50px\" fill=\"red\" opacity=\"0.5\" stroke=\"blue\" stroke-width=\"16px\" " 42 "stroke-opacity=\"0.3\" id=\"circleId\"/></svg>"; 43 const std::string CIRCLE_SVG_CLASS_LABEL = 44 "<svg width=\"400px\" height=\"400px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\"> " 45 "<circle id=\"myCircle\" cx=\"60px\" " 46 "cy=\"200px\" r = \"50px\" fill=\"red\" opacity=\"0.5\" stroke=\"blue\" stroke-width=\"16px\" " 47 "stroke-opacity=\"0.3\" id=\"circleId\" class=\"\"/></svg>"; 48 constexpr int32_t ANIMATION_BEGIN = 100; 49 constexpr int32_t ANIMATION_DUR = 1000; 50 constexpr int32_t ANIMATION_END = 2000; 51 constexpr int32_t ANIMATION_REPEATCOUNT = 1; 52 constexpr int32_t ANIMATION_FROM = 1000; 53 constexpr int32_t ANIMATION_TO = 2000; 54 const std::string ANIMATION_KETIMES = "20;800;2000"; 55 const std::string ANIMATION_VALUES = "10;20;10"; 56 } 57 class SvgContextTestNg : public testing::Test {}; 58 59 /** 60 * @tc.name: SvgContext001 61 * @tc.desc: Get svg node By ID 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(SvgContextTestNg, SvgContext001, TestSize.Level1) 65 { 66 auto svgContext = AceType::MakeRefPtr<SvgContext>(); 67 EXPECT_NE(svgContext, nullptr); 68 ImageSourceInfo src; 69 src.SetFillColor(Color::BLACK); 70 auto svgStream = SkMemoryStream::MakeCopy(CIRCLE_SVG_LABEL.c_str(), CIRCLE_SVG_LABEL.length()); 71 EXPECT_NE(svgStream, nullptr); 72 auto svgCircle = AceType::DynamicCast<SvgCircle>(SvgCircle::Create()); 73 EXPECT_NE(svgCircle, nullptr); 74 std::string label("label"); 75 svgContext->Push(label, svgCircle); 76 EXPECT_NE(svgContext->GetSvgNodeById(label), nullptr); 77 } 78 79 /** 80 * @tc.name: SvgContext002 81 * @tc.desc: parse class 82 * @tc.type: FUNC 83 */ 84 HWTEST_F(SvgContextTestNg, SvgContext002, TestSize.Level1) 85 { 86 auto svgContext = AceType::MakeRefPtr<SvgContext>(); 87 EXPECT_NE(svgContext, nullptr); 88 ImageSourceInfo src; 89 src.SetFillColor(Color::BLACK); 90 auto svgStream = SkMemoryStream::MakeCopy(CIRCLE_SVG_CLASS_LABEL.c_str(), CIRCLE_SVG_CLASS_LABEL.length()); 91 EXPECT_NE(svgStream, nullptr); 92 auto svgCircle = AceType::DynamicCast<SvgCircle>(SvgCircle::Create()); 93 EXPECT_NE(svgCircle, nullptr); 94 std::string label("parseClass"); 95 svgContext->Push(label, svgCircle); 96 EXPECT_NE(svgContext->GetSvgNodeById(label), nullptr); 97 } 98 99 /** 100 * @tc.name: SvgContext004 101 * @tc.desc: test Animation 102 * @tc.type: FUNC 103 */ 104 HWTEST_F(SvgContextTestNg, SvgContext004, TestSize.Level1) 105 { 106 auto animateMotionNode = AceType::DynamicCast<SvgAnimation>(SvgAnimation::CreateAnimateMotion()); 107 EXPECT_NE(animateMotionNode, nullptr); 108 animateMotionNode->SetAttr("begin", std::to_string(ANIMATION_BEGIN)); 109 animateMotionNode->SetAttr("dur", std::to_string(ANIMATION_DUR)); 110 animateMotionNode->SetAttr("end", std::to_string(ANIMATION_END)); 111 animateMotionNode->SetAttr("repeatCount", std::to_string(ANIMATION_REPEATCOUNT)); 112 animateMotionNode->SetAttr("fill", ""); 113 animateMotionNode->SetAttr("calcumode", ""); 114 animateMotionNode->SetAttr("values", ""); 115 animateMotionNode->SetAttr("keyTimes", ""); 116 animateMotionNode->SetAttr("from", std::to_string(ANIMATION_FROM)); 117 animateMotionNode->SetAttr("to", std::to_string(ANIMATION_TO)); 118 animateMotionNode->UpdateAttr(); 119 EXPECT_FLOAT_EQ(animateMotionNode->animateAttr_.begin, ANIMATION_BEGIN); 120 EXPECT_FLOAT_EQ(animateMotionNode->animateAttr_.dur, ANIMATION_DUR); 121 EXPECT_FLOAT_EQ(animateMotionNode->animateAttr_.end, ANIMATION_END); 122 EXPECT_FLOAT_EQ(animateMotionNode->animateAttr_.repeatCount, ANIMATION_REPEATCOUNT); 123 } 124 125 /** 126 * @tc.name: SvgContext005 127 * @tc.desc: test Animation 128 * @tc.type: FUNC 129 */ 130 HWTEST_F(SvgContextTestNg, SvgContext005, TestSize.Level1) 131 { 132 auto svgContext = AceType::MakeRefPtr<SvgContext>(); 133 EXPECT_NE(svgContext, nullptr); 134 Animator animator; 135 RefPtr<Animator> animator1 = AceType::MakeRefPtr<Animator>(); 136 svgContext->AddAnimator(1, animator1); 137 RefPtr<Animator> animator2 = AceType::MakeRefPtr<Animator>(); 138 svgContext->AddAnimator(2, animator2); 139 svgContext->RemoveAnimator(1); 140 svgContext->RemoveAnimator(2); 141 EXPECT_EQ(svgContext->animators_.size(), 0); 142 } 143 144 /** 145 * @tc.name: SvgContext006 146 * @tc.desc: test Animation 147 * @tc.type: FUNC 148 */ 149 HWTEST_F(SvgContextTestNg, SvgContext006, TestSize.Level1) 150 { 151 auto svgContext = AceType::MakeRefPtr<SvgContext>(); 152 EXPECT_NE(svgContext, nullptr); 153 Animator animator; 154 RefPtr<Animator> animator1 = AceType::MakeRefPtr<Animator>(); 155 svgContext->AddAnimator(1, animator1); 156 RefPtr<Animator> animator2 = AceType::MakeRefPtr<Animator>(); 157 svgContext->AddAnimator(2, animator2); 158 svgContext->ControlAnimators(true); 159 svgContext->AnimateFlush(); 160 svgContext->RemoveAnimator(1); 161 svgContext->RemoveAnimator(2); 162 EXPECT_EQ(svgContext->animators_.size(), 0); 163 } 164 } // namespace OHOS::Ace::NG 165