1 /* 2 * Copyright (c) 2025 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 #include <gmock/gmock.h> 18 19 #include "i_knuckle_drawing.h" 20 #include "knuckle_drawing_component.h" 21 #include "mmi_log.h" 22 23 #undef MMI_LOG_TAG 24 #define MMI_LOG_TAG "KnuckleDrawingComponentTest" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 namespace OHOS { 30 namespace MMI { 31 32 class KnuckleDrawingComponentTest : public testing::Test { 33 public: SetUpTestCase(void)34 static void SetUpTestCase(void) {}; TearDownTestCase(void)35 static void TearDownTestCase(void) {}; SetUp(void)36 void SetUp(void) {} SetCase()37 void SetCase() {} 38 }; 39 40 class MockIKnuckleDrawing : public IKnuckleDrawing { 41 public: 42 MockIKnuckleDrawing() = default; 43 ~MockIKnuckleDrawing() override = default; 44 45 MOCK_METHOD2(Draw, void(const OLD::DisplayInfo&, const std::shared_ptr<PointerEvent>&)); 46 MOCK_METHOD2(SetMultiWindowScreenId, void(uint64_t, uint64_t)); 47 MOCK_METHOD1(RegisterAddTimer, void(AddTimerFunc)); 48 }; 49 50 /** 51 * @tc.name: LoadKnuckleDrawing 52 * @tc.desc: Test Overrides Draw function branches 53 * @tc.type: Function 54 * @tc.require: 55 */ 56 HWTEST_F(KnuckleDrawingComponentTest, LoadKnuckleDrawing, TestSize.Level1) 57 { 58 CALL_TEST_DEBUG; 59 KnuckleDrawingComponent &knuckleDrawingComp = KnuckleDrawingComponent::GetInstance(); 60 ASSERT_EQ(knuckleDrawingComp.handle_, nullptr); 61 ASSERT_EQ(knuckleDrawingComp.create_, nullptr); 62 ASSERT_EQ(knuckleDrawingComp.destroy_, nullptr); 63 ASSERT_EQ(knuckleDrawingComp.impl_, nullptr); 64 ASSERT_EQ(knuckleDrawingComp.timerId_, -1); 65 } 66 67 /** 68 * @tc.name: KnuckleDrawingComponentTest_Draw 69 * @tc.desc: Test Overrides Draw function branches 70 * @tc.type: Function 71 * @tc.require: 72 */ 73 HWTEST_F(KnuckleDrawingComponentTest, KnuckleDrawingComponentTest_Draw, TestSize.Level1) 74 { 75 CALL_TEST_DEBUG; 76 KnuckleDrawingComponent::GetInstance().Unload(); 77 78 MockIKnuckleDrawing *knuckleDrawing = new (std::nothrow) MockIKnuckleDrawing(); 79 ASSERT_NE(knuckleDrawing, nullptr); 80 81 bool isCalled = false; __anon795579d10102null82 EXPECT_CALL(*knuckleDrawing, Draw(_, _)).WillOnce([&isCalled] { 83 isCalled = true; 84 }); 85 KnuckleDrawingComponent::GetInstance().impl_ = knuckleDrawing; 86 87 OLD::DisplayInfo displayInfo; 88 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create(); 89 if (pointerEvent == nullptr) { 90 KnuckleDrawingComponent::GetInstance().impl_ = nullptr; 91 delete knuckleDrawing; 92 knuckleDrawing = nullptr; 93 FAIL() << "Create PointerEvent fail"; 94 } 95 96 KnuckleDrawingComponent::GetInstance().Draw(displayInfo, pointerEvent); 97 KnuckleDrawingComponent::GetInstance().impl_ = nullptr; 98 delete knuckleDrawing; 99 knuckleDrawing = nullptr; 100 101 EXPECT_TRUE(isCalled); 102 } 103 104 /** 105 * @tc.name: KnuckleDrawingComponentTest_SetMultiWindowScreenId 106 * @tc.desc: Test Overrides SetMultiWindowScreenId function branches 107 * @tc.type: Function 108 * @tc.require: 109 */ 110 HWTEST_F(KnuckleDrawingComponentTest, KnuckleDrawingComponentTest_SetMultiWindowScreenId, TestSize.Level1) 111 { 112 CALL_TEST_DEBUG; 113 KnuckleDrawingComponent::GetInstance().Unload(); 114 115 MockIKnuckleDrawing *knuckleDrawing = new (std::nothrow) MockIKnuckleDrawing(); 116 ASSERT_NE(knuckleDrawing, nullptr); 117 118 bool isCalled = false; __anon795579d10202null119 EXPECT_CALL(*knuckleDrawing, SetMultiWindowScreenId(_, _)).WillOnce([&isCalled] { 120 isCalled = true; 121 }); 122 KnuckleDrawingComponent::GetInstance().impl_ = knuckleDrawing; 123 124 uint64_t screenId = 0; 125 uint64_t displayNodeScreenId = 0; 126 KnuckleDrawingComponent::GetInstance().SetMultiWindowScreenId(screenId, displayNodeScreenId); 127 KnuckleDrawingComponent::GetInstance().impl_ = nullptr; 128 delete knuckleDrawing; 129 knuckleDrawing = nullptr; 130 131 EXPECT_TRUE(isCalled); 132 } 133 134 /** 135 * @tc.name: KnuckleDrawingComponentTest_Load 136 * @tc.desc: Test Overrides Load function branches 137 * @tc.type: Function 138 * @tc.require: 139 */ 140 HWTEST_F(KnuckleDrawingComponentTest, KnuckleDrawingComponentTest_Load, TestSize.Level1) 141 { 142 CALL_TEST_DEBUG; 143 KnuckleDrawingComponent::GetInstance().Unload(); 144 145 MockIKnuckleDrawing *knuckleDrawing = new (std::nothrow) MockIKnuckleDrawing(); 146 ASSERT_NE(knuckleDrawing, nullptr); 147 148 KnuckleDrawingComponent::GetInstance().impl_ = knuckleDrawing; 149 KnuckleDrawingComponent::GetInstance().Load(); 150 KnuckleDrawingComponent::GetInstance().impl_ = nullptr; 151 delete knuckleDrawing; 152 knuckleDrawing = nullptr; 153 154 KnuckleDrawingComponent::GetInstance().Load(); 155 ASSERT_NE(KnuckleDrawingComponent::GetInstance().impl_, nullptr); 156 ASSERT_NE(KnuckleDrawingComponent::GetInstance().handle_, nullptr); 157 ASSERT_NE(KnuckleDrawingComponent::GetInstance().create_, nullptr); 158 ASSERT_NE(KnuckleDrawingComponent::GetInstance().destroy_, nullptr); 159 ASSERT_GE(KnuckleDrawingComponent::GetInstance().timerId_, 0); 160 161 KnuckleDrawingComponent::GetInstance().Unload(); 162 } 163 164 /** 165 * @tc.name: KnuckleDrawingComponentTest_Unload 166 * @tc.desc: Test Overrides Unload function branches 167 * @tc.type: Function 168 * @tc.require: 169 */ 170 HWTEST_F(KnuckleDrawingComponentTest, KnuckleDrawingComponentTest_Unload, TestSize.Level1) 171 { 172 CALL_TEST_DEBUG; 173 KnuckleDrawingComponent::GetInstance().Unload(); 174 ASSERT_EQ(KnuckleDrawingComponent::GetInstance().impl_, nullptr); 175 ASSERT_EQ(KnuckleDrawingComponent::GetInstance().handle_, nullptr); 176 ASSERT_EQ(KnuckleDrawingComponent::GetInstance().create_, nullptr); 177 ASSERT_EQ(KnuckleDrawingComponent::GetInstance().destroy_, nullptr); 178 ASSERT_EQ(KnuckleDrawingComponent::GetInstance().timerId_, -1); 179 } 180 181 /** 182 * @tc.name: KnuckleDrawingComponentTest_LoadKnuckleSharedLibrary 183 * @tc.desc: Test Overrides LoadKnuckleSharedLibrary function branches 184 * @tc.type: Function 185 * @tc.require: 186 */ 187 HWTEST_F(KnuckleDrawingComponentTest, KnuckleDrawingComponentTest_LoadKnuckleSharedLibrary, TestSize.Level1) 188 { 189 CALL_TEST_DEBUG; 190 KnuckleDrawingComponent::GetInstance().Unload(); 191 bool ret = KnuckleDrawingComponent::GetInstance().LoadKnuckleSharedLibrary(); 192 ASSERT_TRUE(ret); 193 ASSERT_NE(KnuckleDrawingComponent::GetInstance().handle_, nullptr); 194 ASSERT_NE(KnuckleDrawingComponent::GetInstance().create_, nullptr); 195 ASSERT_NE(KnuckleDrawingComponent::GetInstance().destroy_, nullptr); 196 KnuckleDrawingComponent::GetInstance().Unload(); 197 } 198 } // namespace OHOS 199 } // namespace MMI