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 "ui/rs_canvas_drawing_node.h"
19 
20 #include "image_source.h"
21 #include "image_type.h"
22 #include "image_utils.h"
23 #include "i_input_windows_manager.h"
24 #include "mmi_log.h"
25 #include "knuckle_glow_trace_system.h"
26 
27 #undef MMI_LOG_TAG
28 #define MMI_LOG_TAG "KnuckleGlowTraceSystemTest"
29 
30 namespace OHOS {
31 namespace MMI {
32 namespace {
33 using namespace testing::ext;
34 constexpr int32_t MAX_POINTER_COLOR = 0xffff00;
35 } // namespace
36 
37 class KnuckleGlowTraceSystemTest : public testing::Test {
38 public:
SetUpTestCase(void)39     static void SetUpTestCase(void) {};
TearDownTestCase(void)40     static void TearDownTestCase(void) {};
SetUp(void)41     void SetUp(void)
42     {
43         std::string imagePath = "/system/etc/multimodalinput/mouse_icon/Default.svg";
44         pixelMap = DecodeImageToPixelMap(imagePath);
45     };
TearDown(void)46     void TearDown(void) {};
47 
48 private:
DecodeImageToPixelMap(const std::string & imagePath)49     std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath)
50     {
51         CALL_DEBUG_ENTER;
52         OHOS::Media::SourceOptions opts;
53         uint32_t ret = 0;
54         auto imageSource = OHOS::Media::ImageSource::CreateImageSource(imagePath, opts, ret);
55         CHKPP(imageSource);
56         std::set<std::string> formats;
57         ret = imageSource->GetSupportedFormats(formats);
58         OHOS::Media::DecodeOptions decodeOpts;
59         decodeOpts.desiredSize = {
60             .width = 80,
61             .height = 80
62         };
63 
64         decodeOpts.SVGOpts.fillColor = {.isValidColor = false, .color = MAX_POINTER_COLOR};
65         decodeOpts.SVGOpts.strokeColor = {.isValidColor = false, .color = MAX_POINTER_COLOR};
66 
67         std::shared_ptr<OHOS::Media::PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, ret);
68         if (pixelMap == nullptr) {
69             MMI_HILOGE("The pixelMap is nullptr");
70         }
71         return pixelMap;
72     }
73 
74     std::shared_ptr<OHOS::Media::PixelMap> pixelMap {nullptr};
75 };
76 
77 /**
78  * @tc.name: KnuckleGlowTraceSystem_Update_001
79  * @tc.desc: Test Update
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_Update_001, TestSize.Level1)
84 {
85     CALL_DEBUG_ENTER;
86     int32_t pointSize = 10;
87     int32_t maxDivergenceNum = 0;
88     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
89     knuckleGlowTraceSystem.Update();
90     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 10);
91 }
92 
93 /**
94  * @tc.name: KnuckleGlowTraceSystem_Update_002
95  * @tc.desc: Test Update
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_Update_002, TestSize.Level1)
100 {
101     CALL_DEBUG_ENTER;
102     int32_t pointSize = 0;
103     int32_t maxDivergenceNum = 0;
104     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
105     knuckleGlowTraceSystem.Update();
106     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 0);
107 }
108 
109 /**
110  * @tc.name: KnuckleGlowTraceSystemTest_Draw_001
111  * @tc.desc: Test Draw
112  * @tc.type: FUNC
113  * @tc.require:
114  */
115 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_Draw_001, TestSize.Level1)
116 {
117     CALL_DEBUG_ENTER;
118     int32_t pointSize = 0;
119     int32_t maxDivergenceNum = 0;
120     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
121     std::shared_ptr<Rosen::RSCanvasDrawingNode> canvasNode = Rosen::RSCanvasDrawingNode::Create();
122     auto canvas = static_cast<Rosen::ExtendRecordingCanvas *>(canvasNode->BeginRecording(0, 0));
123     knuckleGlowTraceSystem.Draw(canvas);
124     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 0);
125 }
126 
127 /**
128  * @tc.name: KnuckleGlowTraceSystemTest_Draw_002
129  * @tc.desc: Test Draw
130  * @tc.type: FUNC
131  * @tc.require:
132  */
133 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_Draw_002, TestSize.Level1)
134 {
135     CALL_DEBUG_ENTER;
136     int32_t pointSize = 1;
137     int32_t maxDivergenceNum = 1;
138     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
139     std::shared_ptr<Rosen::RSCanvasDrawingNode> canvasNode = Rosen::RSCanvasDrawingNode::Create();
140     auto canvas = static_cast<Rosen::ExtendRecordingCanvas *>(canvasNode->BeginRecording(0, 0));
141     knuckleGlowTraceSystem.Draw(canvas);
142     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 1);
143 }
144 
145 /**
146  * @tc.name: KnuckleGlowTraceSystemTest_ResetDivergentPoints_001
147  * @tc.desc: Test ResetDivergentPoints
148  * @tc.type: FUNC
149  * @tc.require:
150  */
151 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_ResetDivergentPoints_001, TestSize.Level1)
152 {
153     CALL_DEBUG_ENTER;
154     int32_t pointSize = 0;
155     int32_t maxDivergenceNum = 0;
156     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
157     double pointX = 0.1;
158     double pointY = 0.1;
159     knuckleGlowTraceSystem.ResetDivergentPoints(pointX, pointY);
160     EXPECT_EQ(knuckleGlowTraceSystem.divergentPoints_.size(), 0);
161 }
162 
163 /**
164  * @tc.name: KnuckleGlowTraceSystemTest_ResetDivergentPoints_002
165  * @tc.desc: Test ResetDivergentPoints
166  * @tc.type: FUNC
167  * @tc.require:
168  */
169 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_ResetDivergentPoints_002, TestSize.Level1)
170 {
171     CALL_DEBUG_ENTER;
172     int32_t pointSize = 1;
173     int32_t maxDivergenceNum = 1;
174     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
175     double pointX = 0.1;
176     double pointY = 0.1;
177     knuckleGlowTraceSystem.ResetDivergentPoints(pointX, pointY);
178     EXPECT_EQ(knuckleGlowTraceSystem.divergentPoints_.size(), 1);
179 }
180 
181 /**
182  * @tc.name: KnuckleGlowTraceSystem_AddGlowPoints_001
183  * @tc.desc: Test AddGlowPoints
184  * @tc.type: FUNC
185  * @tc.require:
186  */
187 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_AddGlowPoints_001, TestSize.Level1)
188 {
189     CALL_DEBUG_ENTER;
190     int32_t pointSize = 0;
191     int32_t maxDivergenceNum = 0;
192     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
193     Rosen::Drawing::Path path;
194     int64_t timeInterval = 100;
195     knuckleGlowTraceSystem.AddGlowPoints(path, timeInterval);
196     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 0);
197 }
198 
199 /**
200  * @tc.name: KnuckleGlowTraceSystem_AddGlowPoints_002
201  * @tc.desc: Test AddGlowPoints
202  * @tc.type: FUNC
203  * @tc.require:
204  */
205 HWTEST_F(KnuckleGlowTraceSystemTest, KnuckleGlowTraceSystemTest_AddGlowPoints_002, TestSize.Level1)
206 {
207     CALL_DEBUG_ENTER;
208     int32_t pointSize = 1;
209     int32_t maxDivergenceNum = 1;
210     auto knuckleGlowTraceSystem = KnuckleGlowTraceSystem(pointSize, pixelMap, maxDivergenceNum);
211     Rosen::Drawing::Path path;
212     int64_t timeInterval = 100;
213     knuckleGlowTraceSystem.AddGlowPoints(path, timeInterval);
214     EXPECT_EQ(knuckleGlowTraceSystem.glowPoints_.size(), 1);
215 }
216 } // namespace MMI
217 } // namespace OHOS