diff --git a/test/unittest/interfaces/ace_ui_input_event/BUILD.gn b/test/unittest/interfaces/ace_ui_input_event/BUILD.gn index fd3ea193c03bd2bf65f291857ddd1410f7d68d7c..f2ec419d3533d3e7697537288ab61428942af1b6 100644 --- a/test/unittest/interfaces/ace_ui_input_event/BUILD.gn +++ b/test/unittest/interfaces/ace_ui_input_event/BUILD.gn @@ -53,6 +53,7 @@ ohos_unittest("ui_input_event_test") { "oh_arkui_uiinputevent_geteventtime_test.cpp", "oh_arkui_uiinputevent_getpointercount_test.cpp", "oh_arkui_uiinputevent_getpointerid_test.cpp", + "oh_arkui_uiinputevent_getpressedkeys.cpp", "oh_arkui_uiinputevent_getsourcetype_test.cpp", "oh_arkui_uiinputevent_gettooltype_test.cpp", "oh_arkui_uiinputevent_gettype_test.cpp", diff --git a/test/unittest/interfaces/ace_ui_input_event/oh_arkui_uiinputevent_getpressedkeys.cpp b/test/unittest/interfaces/ace_ui_input_event/oh_arkui_uiinputevent_getpressedkeys.cpp new file mode 100644 index 0000000000000000000000000000000000000000..014962bf565c137cfb20505618540f28dc625efe --- /dev/null +++ b/test/unittest/interfaces/ace_ui_input_event/oh_arkui_uiinputevent_getpressedkeys.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../ui_input_event_test.h" + +#include "core/common/ace_application_info.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS::Ace { + +namespace { +struct GetPressedKeysKeyInfo { + int32_t keyCodesLength; + int32_t* pressedKeyCodes; + GetPressedKeysKeyInfo(int32_t keyCodesLength, int32_t* pressedKeyCodes) + : keyCodesLength(keyCodesLength), pressedKeyCodes(pressedKeyCodes) + {} +}; + +struct GetPressedKeysInputInfo { + ArkUI_UIInputEvent event; + int32_t* expectPressedKeyCodes; + int32_t* expectLength; + int32_t expectResult; + GetPressedKeysInputInfo(const ArkUI_UIInputEvent* event, int32_t* expectPressedKeyCodes, + int32_t* expectLength, int32_t expectResult) + : event(event), expectPressedKeyCodes(expectPressedKeyCodes), + expectLength(expectLength), expectResult(expectResult) + {}; + GetPressedKeysInputInfo(GetPressedKeysKeyInfo keyInfo, int32_t* expectPressedKeyCodes, + int32_t* expectLength, int32_t expectResult) + : expectPressedKeyCodes(expectPressedKeyCodes), expectLength(expectLength), expectResult(expectResult) + { + event = ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_MOUSE, C_MOUSE_EVENT_ID, nullptr }; + ArkUIKeyEvent keyEvent; + keyEvent.keyCodesLength = keyInfo.keyCodesLength; + keyEvent.pressedKeyCodes = keyInfo.pressedKeyCodes; + uiInputEvent.keyEvent = keyEvent; + }; +}; +} // namespace + +HWTEST_F(UIInputEventTest, OH_ArkUI_UIInputEvent_GetPressedKeysTest001, TestSize.Level0) +{ + auto uiInputEvent = ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, nullptr }; + int32_t pressedKeyCodes = 0; + int32_t length = 0; + std::vector testCases = { + GetPressedKeysInputInfo(nullptr, nullptr, nullptr, ARKUI_ERROR_CODE_PARAM_INVALID), + GetPressedKeysInputInfo(&uiInputEvent, nullptr, nullptr, ARKUI_ERROR_CODE_PARAM_INVALID), + GetPressedKeysInputInfo(&uiInputEvent, &pressedKeyCodes, nullptr, ARKUI_ERROR_CODE_PARAM_INVALID), + GetPressedKeysInputInfo(&uiInputEvent, &pressedKeyCodes, &length, ARKUI_ERROR_CODE_PARAM_INVALID), + }; + for (auto testCase : testCases) { + auto result = OH_ArkUI_UIInputEvent_GetPressedKeys( + testCase.event, testCase.expectPressedKeyCodes, testCase.expectLength); + EXPECT_EQ(result, testCase.expectResult); + } +} + +HWTEST_F(UIInputEventTest, OH_ArkUI_UIInputEvent_GetPressedKeysTest002, TestSize.Level0) +{ + int32_t length = 2; + int32_t* pressedKeyCodes = new int[2]; + std::vector testCases = { + GetPressedKeysInputInfo(GetPressedKeysKeyInfo(0, []), &pressedKeyCodes, &length, ARKUI_ERROR_CODE_BUFFER_SIZE_NOT_ENOUGH), + GetPressedKeysInputInfo(GetPressedKeysKeyInfo(1, [1]), &pressedKeyCodes, &length, ARKUI_ERROR_CODE_BUFFER_SIZE_NOT_ENOUGH), + GetPressedKeysInputInfo(GetPressedKeysKeyInfo(2, [1, 2]), &pressedKeyCodes, &length, ARKUI_ERROR_CODE_BUFFER_SIZE_NOT_ENOUGH), + GetPressedKeysInputInfo(GetPressedKeysKeyInfo(3, [1, 2, 3]), &pressedKeyCodes, &length, ARKUI_ERROR_CODE_BUFFER_SIZE_NOT_ENOUGH), + }; + + for (auto testCase : testCases) { + auto result = OH_ArkUI_UIInputEvent_GetPressedKeys( + testCase.event, testCase.expectPressedKeyCodes, testCase.expectLength); + EXPECT_EQ(result, expect); + } +} +} // namespace OHOS::Ace \ No newline at end of file