From c567f8f846aef6e2f86e590b005280060d8a887a Mon Sep 17 00:00:00 2001 From: z-hf Date: Mon, 23 Jun 2025 21:21:07 +0800 Subject: [PATCH 1/3] add test Signed-off-by: z-hf --- .../test/input_windows_manager_extest.cpp | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 service/window_manager/test/input_windows_manager_extest.cpp diff --git a/service/window_manager/test/input_windows_manager_extest.cpp b/service/window_manager/test/input_windows_manager_extest.cpp new file mode 100644 index 0000000000..6bf9f6b55c --- /dev/null +++ b/service/window_manager/test/input_windows_manager_extest.cpp @@ -0,0 +1,102 @@ +/* + * 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 +#include +#include + +#include "event_filter_handler.h" +#include "fingersense_wrapper.h" +#include "i_pointer_drawing_manager.h" +#include "input_device_manager.h" +#include "input_event_handler.h" +#include "input_windows_manager.h" +#include "libinput_interface.h" +#include "mmi_log.h" +#include "mock_input_windows_manager.h" +#include "pixel_map.h" +#include "pointer_drawing_manager.h" +#include "proto.h" +#include "scene_board_judgement.h" +#include "struct_multimodal.h" +#include "uds_server.h" +#include "util.h" +#include "window_info.h" + +#undef MMI_LOG_TAG +#define MMI_LOG_TAG "InputWindowsManagerOneTest" + +using namespace OHOS::MMI; +using namespace OHOS::Media; +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +MockInputWindowsManager *g_inputWindowManagerInterface; + +MockInputWindowsManager::MockInputWindowsManager() +{ + g_inputWindowManagerInterface = this; +} + +MockInputWindowsManager::~MockInputWindowsManager() +{ + g_inputWindowManagerInterface = nullptr; +} + +static InputWindowsManagerInterface *GetInputWindowsManagerInterface() +{ + return g_inputWindowManagerInterface; +} + +std::optional InputWindowsManager::GetWindowAndDisplayInfo(int32_t windowId, int32_t displayId) +{ + if (GetInputWindowsManagerInterface() != nullptr) { + return GetInputWindowsManagerInterface()->GetWindowAndDisplayInfo(windowId, displayId); + } + return std::nullopt; +} + +void InputWindowsManager::PrintDisplayInfo(const DisplayInfo displayInfo) {} + +bool Rosen::SceneBoardJudgement::IsSceneBoardEnabled() +{ + if (GetInputWindowsManagerInterface() != nullptr) { + return GetInputWindowsManagerInterface()->IsSceneBoardEnabled(); + } + return false; +} + +namespace MMI { +namespace { +constexpr int32_t CAST_INPUT_DEVICEID{ 0xAAAAAAFF }; +constexpr int32_t CAST_SCREEN_DEVICEID{ 0xAAAAAAFE }; +} // namespace + +std::string ReadJsonFile(const std::string &filePath) +{ + if (g_inputWindowManagerInterface != nullptr) { + return GetInputWindowsManagerInterface()->ReadJsonFile(filePath); + } + return ""; +} + +class InputWindowsManagerOneTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(void){}; + void SetDown(void){}; +}; \ No newline at end of file -- Gitee From b6286dd6680a8bb96c4998367db1a18ed3175b20 Mon Sep 17 00:00:00 2001 From: z-hf Date: Mon, 23 Jun 2025 21:25:31 +0800 Subject: [PATCH 2/3] add test Signed-off-by: z-hf --- BUILD.gn | 5 +- service/BUILD.gn | 49 ++++++ .../infrared_emitter_controller_ex_test.cpp | 151 ++++++++++++++++++ service/infrared_emitter/test/mock.cpp | 36 +++++ service/infrared_emitter/test/mock.h | 42 +++++ .../test/input_windows_manager_extest.cpp | 102 ------------ 6 files changed, 282 insertions(+), 103 deletions(-) create mode 100644 service/infrared_emitter/test/infrared_emitter_controller_ex_test.cpp create mode 100644 service/infrared_emitter/test/mock.cpp create mode 100644 service/infrared_emitter/test/mock.h delete mode 100644 service/window_manager/test/input_windows_manager_extest.cpp diff --git a/BUILD.gn b/BUILD.gn index facec1bc37..ddc4fde456 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -245,7 +245,10 @@ group("mmi_tests") { } if (drivers_interface_rtos_extra_enable) { - deps += [ "service:InfraredEmitterTest" ] + deps += [ + "service:InfraredEmitterEXTest", + "service:InfraredEmitterTest", + ] } } diff --git a/service/BUILD.gn b/service/BUILD.gn index b65980baaf..181aea18ab 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -2179,6 +2179,55 @@ ohos_unittest("InfraredEmitterTest") { } } +ohos_unittest("InfraredEmitterEXTest") { + module_out_path = module_output_path + + include_dirs = [ + "infrared_emitter/include", + "infrared_emitter/test", + ] + + configs = [ "${mmi_path}:coverage_flags" ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + cflags_cc = [ "-Wno-inconsistent-missing-override" ] + + sources = [ + "infrared_emitter/src/infrared_emitter_controller.cpp", + "infrared_emitter/test/infrared_emitter_controller_ex_test.cpp", + "infrared_emitter/test/mock.cpp", + ] + + deps = [ + "${mmi_path}/service:libmmi-server", + "${mmi_path}/util:libmmi-util", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utilsbase", + "googletest:gmock_main", + "googletest:gtest_main", + "graphic_2d:2d_graphics", + "hdf_core:libhdi", + "hilog:libhilog", + "hilog:libhilog_base", + "init:libbegetutil", + "ipc:ipc_core", + ] + + if (drivers_interface_rtos_extra_enable) { + external_deps += [ + "drivers_interface_rtos_extra:libconsumerir_proxy_1.0", + "hdf_core:libhdi", + ] + } +} + ohos_unittest("InterceptorTest") { module_out_path = module_output_path diff --git a/service/infrared_emitter/test/infrared_emitter_controller_ex_test.cpp b/service/infrared_emitter/test/infrared_emitter_controller_ex_test.cpp new file mode 100644 index 0000000000..02e8fd3af5 --- /dev/null +++ b/service/infrared_emitter/test/infrared_emitter_controller_ex_test.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2024 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 + +#include +#include + +#include "infrared_emitter_controller.h" +#include "mmi_log.h" +#include "mock.h" + +#undef MMI_LOG_TAG +#define MMI_LOG_TAG "InfraredEmitterControllerTest" + +namespace OHOS { +namespace MMI { +#ifndef OHOS_BUILD_PC_UNIT_TEST +namespace { +using namespace testing::ext; +using namespace testing; +class ConsumerIrTest : public OHOS::HDI::Consumerir::V1_0::ConsumerIr { +public: + ConsumerIrTest() = default; + ~ConsumerIrTest() {} + + int32_t Transmit(int32_t carrierFreq, const std::vector& pattern, bool& ret) { return 0; } + int32_t GetCarrierFreqs(bool& ret, std::vector& range) + { + return 0; + } +}; +class IDeviceManagerTest : public OHOS::HDI::DeviceManager::V1_0::IDeviceManager { +public: + IDeviceManagerTest() = default; + virtual ~IDeviceManagerTest() = default; + + int32_t LoadDevice(const std::string &serviceName) { return 0; } + int32_t UnloadDevice(const std::string &serviceName) { return 0; } + int32_t ListAllDevice(std::vector &deviceInfos) { return 0; } + int32_t ListAllHost(std::vector &pidList) { return 0; } +}; +} // namespace + +class InfraredEmitterControllerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + static inline std::shared_ptr messageParcelMock_ = nullptr; +}; + +void InfraredEmitterControllerTest::SetUpTestCase(void) +{ + messageParcelMock_ = std::make_shared(); + MessageParcelMock::messageParcel = messageParcelMock_; +} + +void InfraredEmitterControllerTest::TearDownTestCase(void) +{ + MessageParcelMock::messageParcel = nullptr; + messageParcelMock_ = nullptr; +} + +void InfraredEmitterControllerTest::SetUp() {} + +void InfraredEmitterControllerTest::TearDown() {} + +/** + * @tc.name: InfraredEmitterControllerTest_InitInfraredEmitter_001 + * @tc.desc: Test the funcation InitInfraredEmitter + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InfraredEmitterControllerTest, InfraredEmitterControllerTest_InitInfraredEmitter_001, TestSize.Level1) +{ + CALL_TEST_DEBUG; + sptr consumerIr = new ConsumerIrTest(); + EXPECT_CALL(*messageParcelMock_, Get(_, _)).WillOnce(Return(consumerIr)); + InfraredEmitterController controller; + ASSERT_NO_FATAL_FAILURE(controller.InitInfraredEmitter()); +} + +/** + * @tc.name: InfraredEmitterControllerTest_InitInfraredEmitter_002 + * @tc.desc: Test the funcation InitInfraredEmitter + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InfraredEmitterControllerTest, InfraredEmitterControllerTest_InitInfraredEmitter_002, TestSize.Level1) +{ + CALL_TEST_DEBUG; + EXPECT_CALL(*messageParcelMock_, Get(_, _)).WillOnce(Return(nullptr)); + EXPECT_CALL(*messageParcelMock_, Get()).WillOnce(Return(nullptr)); + InfraredEmitterController controller; + ASSERT_NO_FATAL_FAILURE(controller.InitInfraredEmitter()); +} + +/** + * @tc.name: InfraredEmitterControllerTest_InitInfraredEmitter_003 + * @tc.desc: Test the funcation InitInfraredEmitter + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InfraredEmitterControllerTest, InfraredEmitterControllerTest_InitInfraredEmitter_003, TestSize.Level1) +{ + CALL_TEST_DEBUG; + EXPECT_CALL(*messageParcelMock_, Get(_, _)) + .WillOnce(Return(nullptr)) + .WillOnce(Return(nullptr)); + sptr iDeviceManager = new IDeviceManagerTest(); + EXPECT_CALL(*messageParcelMock_, Get()).WillOnce(Return(iDeviceManager)); + InfraredEmitterController controller; + ASSERT_NO_FATAL_FAILURE(controller.InitInfraredEmitter()); +} + +/** + * @tc.name: InfraredEmitterControllerTest_InitInfraredEmitter_004 + * @tc.desc: Test the funcation InitInfraredEmitter + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InfraredEmitterControllerTest, InfraredEmitterControllerTest_InitInfraredEmitter_004, TestSize.Level1) +{ + CALL_TEST_DEBUG; + sptr consumerIr = new ConsumerIrTest(); + EXPECT_CALL(*messageParcelMock_, Get(_, _)) + .WillOnce(Return(nullptr)) + .WillOnce(Return(consumerIr)); + sptr iDeviceManager = new IDeviceManagerTest(); + EXPECT_CALL(*messageParcelMock_, Get()).WillOnce(Return(iDeviceManager)); + InfraredEmitterController controller; + ASSERT_NO_FATAL_FAILURE(controller.InitInfraredEmitter()); +} +#endif // OHOS_BUILD_PC_UNIT_TEST +} // namespace MMI +} // namespace OHOS \ No newline at end of file diff --git a/service/infrared_emitter/test/mock.cpp b/service/infrared_emitter/test/mock.cpp new file mode 100644 index 0000000000..bbd3413303 --- /dev/null +++ b/service/infrared_emitter/test/mock.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2024 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 "mock.h" + +namespace OHOS { +using namespace OHOS::MMI; + +sptr OHOS::HDI::Consumerir::V1_0::ConsumerIr::Get( + const std::string& serviceName, bool isStub) +{ + if (OHOS::MMI::DfsMessageParcel::messageParcel == nullptr) { + return 0; + } + return OHOS::MMI::DfsMessageParcel::messageParcel->Get(serviceName, isStub); +} + +sptr OHOS::HDI::DeviceManager::V1_0::IDeviceManager::Get() +{ + if (OHOS::MMI::DfsMessageParcel::messageParcel == nullptr) { + return 0; + } + return OHOS::MMI::DfsMessageParcel::messageParcel->Get(); +} +} // namespace OHOS \ No newline at end of file diff --git a/service/infrared_emitter/test/mock.h b/service/infrared_emitter/test/mock.h new file mode 100644 index 0000000000..22356b2d88 --- /dev/null +++ b/service/infrared_emitter/test/mock.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2024 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. + */ +#ifndef MESSAGE_PARCEL_MOCK_H +#define MESSAGE_PARCEL_MOCK_H + +#include + +#include "idevmgr_hdi.h" +#include "infrared_emitter_controller.h" + +namespace OHOS { +namespace MMI { +class DfsMessageParcel { +public: + virtual ~DfsMessageParcel() = default; +public: + virtual sptr Get(const std::string& serviceName, bool isStub) = 0; + virtual sptr Get() = 0; +public: + static inline std::shared_ptr messageParcel = nullptr; +}; + +class MessageParcelMock : public DfsMessageParcel { +public: + MOCK_METHOD2(Get, sptr(const std::string& serviceName, bool isStub)); + MOCK_METHOD0(Get, sptr()); +}; +} // namespace MMI +} // namespace OHOS +#endif \ No newline at end of file diff --git a/service/window_manager/test/input_windows_manager_extest.cpp b/service/window_manager/test/input_windows_manager_extest.cpp deleted file mode 100644 index 6bf9f6b55c..0000000000 --- a/service/window_manager/test/input_windows_manager_extest.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 -#include -#include - -#include "event_filter_handler.h" -#include "fingersense_wrapper.h" -#include "i_pointer_drawing_manager.h" -#include "input_device_manager.h" -#include "input_event_handler.h" -#include "input_windows_manager.h" -#include "libinput_interface.h" -#include "mmi_log.h" -#include "mock_input_windows_manager.h" -#include "pixel_map.h" -#include "pointer_drawing_manager.h" -#include "proto.h" -#include "scene_board_judgement.h" -#include "struct_multimodal.h" -#include "uds_server.h" -#include "util.h" -#include "window_info.h" - -#undef MMI_LOG_TAG -#define MMI_LOG_TAG "InputWindowsManagerOneTest" - -using namespace OHOS::MMI; -using namespace OHOS::Media; -using namespace testing; -using namespace testing::ext; - -namespace OHOS { -MockInputWindowsManager *g_inputWindowManagerInterface; - -MockInputWindowsManager::MockInputWindowsManager() -{ - g_inputWindowManagerInterface = this; -} - -MockInputWindowsManager::~MockInputWindowsManager() -{ - g_inputWindowManagerInterface = nullptr; -} - -static InputWindowsManagerInterface *GetInputWindowsManagerInterface() -{ - return g_inputWindowManagerInterface; -} - -std::optional InputWindowsManager::GetWindowAndDisplayInfo(int32_t windowId, int32_t displayId) -{ - if (GetInputWindowsManagerInterface() != nullptr) { - return GetInputWindowsManagerInterface()->GetWindowAndDisplayInfo(windowId, displayId); - } - return std::nullopt; -} - -void InputWindowsManager::PrintDisplayInfo(const DisplayInfo displayInfo) {} - -bool Rosen::SceneBoardJudgement::IsSceneBoardEnabled() -{ - if (GetInputWindowsManagerInterface() != nullptr) { - return GetInputWindowsManagerInterface()->IsSceneBoardEnabled(); - } - return false; -} - -namespace MMI { -namespace { -constexpr int32_t CAST_INPUT_DEVICEID{ 0xAAAAAAFF }; -constexpr int32_t CAST_SCREEN_DEVICEID{ 0xAAAAAAFE }; -} // namespace - -std::string ReadJsonFile(const std::string &filePath) -{ - if (g_inputWindowManagerInterface != nullptr) { - return GetInputWindowsManagerInterface()->ReadJsonFile(filePath); - } - return ""; -} - -class InputWindowsManagerOneTest : public testing::Test { -public: - static void SetUpTestCase(void){}; - static void TearDownTestCase(void){}; - void SetUp(void){}; - void SetDown(void){}; -}; \ No newline at end of file -- Gitee From 9661da7e85e2a6b005aa7f92eb39555043695fc6 Mon Sep 17 00:00:00 2001 From: z-hf Date: Tue, 24 Jun 2025 09:19:42 +0800 Subject: [PATCH 3/3] add test Signed-off-by: z-hf --- bundle.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index 3666eb91fc..2d361437bd 100644 --- a/bundle.json +++ b/bundle.json @@ -102,8 +102,7 @@ "ipc", "sensor", "idl_tool", - "runtime_core", - "drivers_interface_rtos_extra" + "runtime_core" ], "third_party": [ "libuv", -- Gitee