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 "pipeline/rs_recording_canvas.h" 23 #include "render/rs_pixel_map_util.h" 24 25 #include "mmi_log.h" 26 #include "pointer_event.h" 27 #include "ui/rs_canvas_drawing_node.h" 28 #include "knuckle_divergent_point.h" 29 30 #undef MMI_LOG_TAG 31 #define MMI_LOG_TAG "KnuckleDivergentPointTest" 32 33 namespace OHOS { 34 namespace MMI { 35 namespace { 36 using namespace testing::ext; 37 constexpr int32_t MAX_POINTER_COLOR = 0xffffff; 38 } // namespace 39 class KnuckleDivergentPointTest : 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 if (knuckleDivergentPoint == nullptr) { 46 std::string imagePath = "/system/etc/multimodalinput/mouse_icon/Default.svg"; 47 auto pixelMap = DecodeImageToPixelMap(imagePath); 48 CHKPV(pixelMap); 49 knuckleDivergentPoint = std::make_shared<KnuckleDivergentPoint>(pixelMap); 50 } 51 } TearDown(void)52 void TearDown(void) {} 53 private: DecodeImageToPixelMap(const std::string & imagePath)54 std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath) 55 { 56 CALL_DEBUG_ENTER; 57 OHOS::Media::SourceOptions opts; 58 uint32_t ret = 0; 59 auto imageSource = OHOS::Media::ImageSource::CreateImageSource(imagePath, opts, ret); 60 CHKPP(imageSource); 61 std::set<std::string> formats; 62 ret = imageSource->GetSupportedFormats(formats); 63 OHOS::Media::DecodeOptions decodeOpts; 64 decodeOpts.desiredSize = { 65 .width = 80, 66 .height = 80 67 }; 68 69 decodeOpts.SVGOpts.fillColor = {.isValidColor = false, .color = MAX_POINTER_COLOR}; 70 decodeOpts.SVGOpts.strokeColor = {.isValidColor = false, .color = MAX_POINTER_COLOR}; 71 72 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, ret); 73 if (pixelMap == nullptr) { 74 MMI_HILOGE("The pixelMap is nullptr"); 75 } 76 return pixelMap; 77 } 78 79 std::shared_ptr<KnuckleDivergentPoint> knuckleDivergentPoint { nullptr }; 80 }; 81 82 /** 83 * @tc.name: KnuckleDivergentPointTest_IsEnded_001 84 * @tc.desc: Test IsEnded 85 * @tc.type: Function 86 * @tc.require: 87 */ 88 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_IsEnded_001, TestSize.Level1) 89 { 90 CALL_TEST_DEBUG; 91 EXPECT_TRUE(knuckleDivergentPoint->IsEnded()); 92 } 93 94 /** 95 * @tc.name: KnuckleDivergentPointTest_IsEnded_002 96 * @tc.desc: Test IsEnded 97 * @tc.type: Function 98 * @tc.require: 99 */ 100 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_IsEnded_002, TestSize.Level1) 101 { 102 CALL_TEST_DEBUG; 103 knuckleDivergentPoint->lifespan_ = 1; 104 EXPECT_FALSE(knuckleDivergentPoint->IsEnded()); 105 } 106 107 /** 108 * @tc.name: KnuckleDivergentPointTest_Update_001 109 * @tc.desc: Test Update 110 * @tc.type: Function 111 * @tc.require: 112 */ 113 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Update_001, TestSize.Level1) 114 { 115 CALL_TEST_DEBUG; 116 knuckleDivergentPoint->Update(); 117 EXPECT_LT(knuckleDivergentPoint->lifespan_, 0); 118 } 119 120 /** 121 * @tc.name: KnuckleDivergentPointTest_Update_002 122 * @tc.desc: Test Update 123 * @tc.type: Function 124 * @tc.require: 125 */ 126 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Update_002, TestSize.Level1) 127 { 128 CALL_TEST_DEBUG; 129 knuckleDivergentPoint->lifespan_ = 1; 130 knuckleDivergentPoint->Update(); 131 EXPECT_EQ(knuckleDivergentPoint->lifespan_, 0); 132 } 133 134 /** 135 * @tc.name: KnuckleDivergentPointTest_Clear_001 136 * @tc.desc: Test Clear 137 * @tc.type: Function 138 * @tc.require: 139 */ 140 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Clear_001, TestSize.Level1) 141 { 142 CALL_TEST_DEBUG; 143 knuckleDivergentPoint->Clear(); 144 EXPECT_EQ(knuckleDivergentPoint->lifespan_, -1); 145 } 146 147 /** 148 * @tc.name: KnuckleDivergentPointTest_Draw_001 149 * @tc.desc: Test Draw 150 * @tc.type: Function 151 * @tc.require: 152 */ 153 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Draw_001, TestSize.Level1) 154 { 155 CALL_TEST_DEBUG; 156 knuckleDivergentPoint->Draw(nullptr); 157 EXPECT_EQ(knuckleDivergentPoint->lifespan_, -1); 158 } 159 160 /** 161 * @tc.name: KnuckleDivergentPointTest_Draw_002 162 * @tc.desc: Test Draw 163 * @tc.type: Function 164 * @tc.require: 165 */ 166 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Draw_002, TestSize.Level1) 167 { 168 CALL_TEST_DEBUG; 169 std::shared_ptr<Rosen::RSCanvasDrawingNode> canvasNode = Rosen::RSCanvasDrawingNode::Create(); 170 auto canvas = static_cast<Rosen::ExtendRecordingCanvas *>(canvasNode->BeginRecording(10, 10)); 171 knuckleDivergentPoint->Draw(canvas); 172 EXPECT_EQ(knuckleDivergentPoint->lifespan_, -1); 173 } 174 175 /** 176 * @tc.name: KnuckleDivergentPointTest_Reset_001 177 * @tc.desc: Test Reset 178 * @tc.type: Function 179 * @tc.require: 180 */ 181 HWTEST_F(KnuckleDivergentPointTest, KnuckleDivergentPointTest_Reset_001, TestSize.Level1) 182 { 183 CALL_TEST_DEBUG; 184 double pointX = 10.0; 185 double pointY = 5.0; 186 knuckleDivergentPoint->Reset(pointX, pointY); 187 EXPECT_DOUBLE_EQ(knuckleDivergentPoint->pointX_, 10.0); 188 EXPECT_DOUBLE_EQ(knuckleDivergentPoint->pointY_, 5.0); 189 } 190 } // namespace MMI 191 } // namespace OHOS