1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEST_UNITTEST_IMAGE_IMAGE_TEST_UTILS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEST_UNITTEST_IMAGE_IMAGE_TEST_UTILS_H 18 19 #include "base/geometry/offset.h" 20 #include "base/geometry/rect.h" 21 #include "core/common/window.h" 22 #include "core/components/common/properties/alignment.h" 23 #include "core/components/display/render_display.h" 24 #include "core/components/root/render_root.h" 25 #include "core/components/stack/render_stack.h" 26 #include "core/pipeline/base/constants.h" 27 28 #define protected public 29 #include "core/components/image/render_image.h" 30 31 namespace OHOS::Ace { 32 33 const double EXTRA_SMALL_LENGTH = 100.0; 34 const double SMALL_LENGTH = 200.0; 35 const double MINUS_EXTRA_SMALL_LENGTH = -100.0; 36 const double MIDDLE_LENGTH = 300.0; 37 const double LARGE_LENGTH = 400.0; 38 const double EXTRA_LARGE_LENGTH = 500.0; 39 const double DEFAULT_LENGTH = -1.0; 40 const int32_t REPEAT_LIST_LENGTH = 4; 41 const int32_t REPEATX_LIST_LENGTH = 2; 42 const int32_t REPEATY_LIST_LENGTH = 2; 43 const int32_t NOREPEAT_LIST_LENGTH = 1; 44 const Size PARENT_SIZE = Size(300.0, 300.0); 45 const bool FIT_MAX_SIZE = true; 46 47 class MockRenderImage final : public RenderImage { 48 DECLARE_ACE_TYPE(MockRenderImage, RenderImage); 49 50 public: 51 MockRenderImage() = default; 52 ~MockRenderImage() override = default; 53 Measure()54 Size Measure() override 55 { 56 return Size(LARGE_LENGTH, LARGE_LENGTH); 57 } 58 }; 59 60 class MockRenderRoot final : public RenderRoot { 61 DECLARE_ACE_TYPE(MockRenderRoot, RenderRoot); 62 63 public: 64 MockRenderRoot() = default; 65 ~MockRenderRoot() override = default; 66 }; 67 68 class MockWindow final : public PlatformWindow { 69 public: MockWindow(AceView * aceView)70 explicit MockWindow(AceView* aceView) {} 71 ~MockWindow() override = default; 72 73 // Platform window interface RequestFrame()74 void RequestFrame() override {} RegisterVsyncCallback(AceVsyncCallback && callback)75 void RegisterVsyncCallback(AceVsyncCallback&& callback) override {} SetRootRenderNode(const RefPtr<RenderNode> & root)76 void SetRootRenderNode(const RefPtr<RenderNode>& root) override {} 77 78 private: 79 ACE_DISALLOW_COPY_AND_MOVE(MockWindow); 80 }; 81 82 class MockRenderContext : public RenderContext { 83 public: Repaint(const RefPtr<RenderNode> & node)84 void Repaint(const RefPtr<RenderNode>& node) override {}; PaintChild(const RefPtr<RenderNode> & child,const Offset & offset)85 void PaintChild(const RefPtr<RenderNode>& child, const Offset& offset) override {}; Restore()86 void Restore() override {}; 87 }; 88 89 class MockRenderStack : public RenderStack { 90 DECLARE_ACE_TYPE(MockRenderStack, RenderStack); 91 }; 92 93 class MockRenderDisplay : public RenderDisplay { 94 DECLARE_ACE_TYPE(MockRenderDisplay, RenderDisplay); 95 }; 96 97 struct ImageFitConfig final { ImageFitConfigfinal98 ImageFitConfig( 99 const Alignment& alignment, const ImageFit& imageFit, const Size& srcRectSize, const Size& dstRectSize) 100 : alignment(alignment), imageFit(imageFit), srcRectSize(srcRectSize), dstRectSize(dstRectSize), 101 srcRect(Offset::Zero(), srcRectSize), dstRect(Offset::Zero(), dstRectSize) 102 {} 103 ~ImageFitConfig() = default; 104 105 Alignment alignment; 106 ImageFit imageFit = ImageFit::CONTAIN; 107 Size srcRectSize; 108 Size dstRectSize; 109 Rect srcRect; 110 Rect dstRect; 111 }; 112 113 void VerifyImageFit(ImageFitConfig&& imageFitConfig, const Rect& expectSrcRect, const Rect& expectDstRect); 114 void VerifyRectSize(const std::list<Rect>& rectList, const Size& expectRectSize); 115 RefPtr<RenderRoot> CreateRenderRoot(const Size& maxSize); 116 RefPtr<RenderImage> CreateRenderImage(double width, double height, bool fitMaxSize = false); 117 RefPtr<PipelineContext> GetMockContext(); 118 } // namespace OHOS::Ace 119 120 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEST_UNITTEST_IMAGE_IMAGE_TEST_UTILS_H 121