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 <gtest/gtest.h>
17 
18 #include "image_source.h"
19 #include "image_type.h"
20 #include "image_utils.h"
21 
22 #include "ui/rs_canvas_drawing_node.h"
23 #include "ui/rs_surface_node.h"
24 
25 #include "i_input_windows_manager.h"
26 #include "mmi_log.h"
27 #include "knuckle_glow_point.h"
28 
29 #undef MMI_LOG_TAG
30 #define MMI_LOG_TAG "KnuckleGlowPointTest"
31 
32 namespace OHOS {
33 namespace MMI {
34 namespace {
35 using namespace testing::ext;
36 constexpr int32_t MAX_POINTER_COLOR = 0xff0000;
37 } // namespace
38 
39 class KnuckleGlowPointTest : public testing::Test {
40 public:
SetUpTestCase(void)41     static void SetUpTestCase(void) {};
TearDownTestCase(void)42     static void TearDownTestCase(void) {};
SetUp(void)43     void SetUp(void)
44     {
45         std::string imagePath = "/system/etc/multimodalinput/mouse_icon/Default.svg";
46         pixelMap = DecodeImageToPixelMap(imagePath);
47     };
TearDown(void)48     void TearDown(void) {};
49 
50 private:
DecodeImageToPixelMap(const std::string & imagePath)51     std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath)
52     {
53         CALL_DEBUG_ENTER;
54         OHOS::Media::SourceOptions opts;
55         uint32_t ret = 0;
56         auto imageSource = OHOS::Media::ImageSource::CreateImageSource(imagePath, opts, ret);
57         CHKPP(imageSource);
58         std::set<std::string> formats;
59         ret = imageSource->GetSupportedFormats(formats);
60         OHOS::Media::DecodeOptions decodeOpts;
61         decodeOpts.desiredSize = {
62             .width = 80,
63             .height = 80
64         };
65 
66         decodeOpts.SVGOpts.fillColor = {.isValidColor = false, .color = MAX_POINTER_COLOR};
67         decodeOpts.SVGOpts.strokeColor = {.isValidColor = false, .color = MAX_POINTER_COLOR};
68 
69         std::shared_ptr<OHOS::Media::PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, ret);
70         if (pixelMap == nullptr) {
71             MMI_HILOGE("The pixelMap is nullptr");
72         }
73         return pixelMap;
74     }
75 
76     std::shared_ptr<OHOS::Media::PixelMap> pixelMap {nullptr};
77 };
78 
79 /**
80  * @tc.name: KnuckleGlowPoint_Update_001
81  * @tc.desc: Test Update
82  * @tc.type: FUNC
83  * @tc.require:
84  */
85 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_Update_001, TestSize.Level1)
86 {
87     CALL_DEBUG_ENTER;
88     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
89     knuckleGlowPoint.Update();
90     EXPECT_EQ(knuckleGlowPoint.lifespan_, -1);
91 }
92 
93 /**
94  * @tc.name: KnuckleGlowPoint_Update_002
95  * @tc.desc: Test Update
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_Update_002, TestSize.Level1)
100 {
101     CALL_DEBUG_ENTER;
102     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
103     knuckleGlowPoint.lifespan_ = 1;
104     knuckleGlowPoint.Update();
105     EXPECT_LT(knuckleGlowPoint.lifespan_, 1);
106 }
107 
108 /**
109  * @tc.name: KnuckleGlowPointTest_Draw_001
110  * @tc.desc: Test Draw
111  * @tc.type: FUNC
112  * @tc.require:
113  */
114 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_Draw_001, TestSize.Level1)
115 {
116     CALL_DEBUG_ENTER;
117     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
118     std::shared_ptr<Rosen::RSCanvasDrawingNode> canvasNode = Rosen::RSCanvasDrawingNode::Create();
119     auto canvas = static_cast<Rosen::ExtendRecordingCanvas *>(canvasNode->BeginRecording(0, 0));
120     knuckleGlowPoint.Draw(canvas);
121     EXPECT_EQ(knuckleGlowPoint.lifespan_, -1);
122 }
123 
124 /**
125  * @tc.name: KnuckleGlowPointTest_Draw_002
126  * @tc.desc: Test Draw
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_Draw_002, TestSize.Level1)
131 {
132     CALL_DEBUG_ENTER;
133     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
134     knuckleGlowPoint.lifespan_ = 1;
135     knuckleGlowPoint.pointX_ = 1;
136     knuckleGlowPoint.pointY_ = 1;
137     std::shared_ptr<Rosen::RSCanvasDrawingNode> canvasNode = Rosen::RSCanvasDrawingNode::Create();
138     auto canvas = static_cast<Rosen::ExtendRecordingCanvas *>(canvasNode->BeginRecording(0, 0));
139     knuckleGlowPoint.Draw(canvas);
140     EXPECT_EQ(knuckleGlowPoint.lifespan_, 1);
141 }
142 
143 /**
144  * @tc.name: KnuckleGlowPointTest_Reset_001
145  * @tc.desc: Test Reset
146  * @tc.type: FUNC
147  * @tc.require:
148  */
149 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_Reset_001, TestSize.Level1)
150 {
151     CALL_DEBUG_ENTER;
152     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
153     double pointX = 0.1;
154     double pointY = 0.1;
155     float lifespanoffset = 0.1f;
156     knuckleGlowPoint.Reset(pointX, pointY, lifespanoffset);
157     EXPECT_DOUBLE_EQ(knuckleGlowPoint.pointX_, 0.1);
158 }
159 
160 /**
161  * @tc.name: KnuckleGlowPointTest_IsEnded_001
162  * @tc.desc: Test Reset
163  * @tc.type: FUNC
164  * @tc.require:
165  */
166 HWTEST_F(KnuckleGlowPointTest, KnuckleGlowPointTest_IsEnded_001, TestSize.Level1)
167 {
168     CALL_DEBUG_ENTER;
169     auto knuckleGlowPoint = KnuckleGlowPoint(pixelMap);
170     double pointX = 0.1;
171     double pointY = 0.1;
172     float lifespanoffset = -0.1f;
173     knuckleGlowPoint.Reset(pointX, pointY, lifespanoffset);
174     ASSERT_FALSE(knuckleGlowPoint.IsEnded());
175 }
176 } // namespace MMI
177 } // namespace OHOS