• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
17 
18 #include "gtest/gtest.h"
19 #include "test/mock/core/rosen/mock_canvas.h"
20 #include "test/mock/core/rosen/testing_rect.h"
21 
22 #define private public
23 #define protected public
24 
25 #include "core/components_ng/svg/svg_dom.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS::Ace::NG {
31 namespace {
32 const Size LAYOUT = { 400, 500 };
33 const Size SVG_SIZE = { 50, 50 };
34 const Rect VIEW_BOX = { -4.0, -10.0, 300.0, 300.0 };
35 constexpr float EPSILON = 0.001;
36 } // namespace
37 class SvgDomTestNg : public testing::Test {};
38 
39 /**
40  * @tc.name: SvgDom001
41  * @tc.desc: test fit image
42  * @tc.type: FUNC
43  */
44 HWTEST_F(SvgDomTestNg, SvgDom001, TestSize.Level1)
45 {
46     auto svgDom = AceType::MakeRefPtr<SvgDom>();
47 
48     svgDom->svgSize_ = SVG_SIZE;
49     svgDom->viewBox_ = VIEW_BOX;
50     Testing::MockCanvas canvas;
51     float layoutScale = LAYOUT.Width() / SVG_SIZE.Width();
52     float viewBoxScale = SVG_SIZE.Width() / VIEW_BOX.Width();
53     float tx = 4.0f * viewBoxScale * layoutScale;
54     float ty = 10.0f * viewBoxScale * layoutScale;
55 
56     ty += (LAYOUT.Height() - SVG_SIZE.Height() * layoutScale) / 2;
57     // check translate and scale parameters
58     EXPECT_CALL(canvas, Translate(FloatNear(tx, EPSILON), FloatNear(ty, EPSILON)));
59     EXPECT_CALL(canvas, Scale(layoutScale * viewBoxScale, layoutScale * viewBoxScale));
60     auto clipRect = Testing::TestingRect(0, 0, LAYOUT.Width(), LAYOUT.Height());
61     EXPECT_CALL(canvas, ClipRect(_, _, _));
62     svgDom->FitImage(canvas, ImageFit::CONTAIN, LAYOUT);
63 }
64 } // namespace OHOS::Ace::NG
65