diff --git a/frameworks/native/appkit/BUILD.gn b/frameworks/native/appkit/BUILD.gn index 25cfd5d626..26b9f0796b 100644 --- a/frameworks/native/appkit/BUILD.gn +++ b/frameworks/native/appkit/BUILD.gn @@ -214,6 +214,7 @@ ohos_shared_library("appkit_native") { "graphic_2d:librender_service_base", "hicollie:libhicollie", "hilog:libhilog", + "hichecker:libhichecker", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "i18n:preferred_language", diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 136f040e10..2b96f068eb 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -2024,6 +2024,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con application_->SetAbilityRecordMgr(abilityRecordMgr_); application_->SetConfiguration(appConfig); contextImpl->SetConfiguration(application_->GetConfiguration()); + application_->InitJSLeakWatcher(bundleInfo.name); applicationImpl_->SetRecordId(appLaunchData.GetRecordId()); applicationImpl_->SetApplication(application_); diff --git a/frameworks/native/appkit/app/ohos_application.cpp b/frameworks/native/appkit/app/ohos_application.cpp index 5455f430c7..2f72246c2c 100644 --- a/frameworks/native/appkit/app/ohos_application.cpp +++ b/frameworks/native/appkit/app/ohos_application.cpp @@ -43,6 +43,7 @@ #include "syspara/parameter.h" #include "ui_ability.h" #include "application_configuration_manager.h" +#include "js_leak_watcher_ts.h" #ifdef SUPPORT_GRAPHICS #include "display_manager.h" #include "window.h" @@ -1298,6 +1299,21 @@ bool OHOSApplication::UpdateETSRuntime(AbilityRuntime::Runtime::Options &option) return true; } +void OHOSApplication::InitJSLeakWatcher(const std::string &bundleName) +{ + TAG_LOGD(AAFwkTag::APPKIT, "InitJSLeakWatcher call"); + if (runtime_ == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null runtime"); + return; + } + if (runtime_->GetLanguage() != AbilityRuntime::Runtime::Language::JS) { + return; + } + auto env = (static_cast(*runtime_)).GetNapiEnv(); + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + JSLeakWatcherEarlyInit(env, bundleName); +} + #ifdef SUPPORT_SCREEN void OHOSApplication::RegisterGetAllUIAbilitiesCallback( const std::shared_ptr &context, diff --git a/interfaces/kits/native/appkit/app/ohos_application.h b/interfaces/kits/native/appkit/app/ohos_application.h index d474db503c..a16d831e5f 100644 --- a/interfaces/kits/native/appkit/app/ohos_application.h +++ b/interfaces/kits/native/appkit/app/ohos_application.h @@ -245,6 +245,8 @@ public: bool UpdateETSRuntime(AbilityRuntime::Runtime::Options &option); + void InitJSLeakWatcher(const std::string &bundleName); + #ifdef SUPPORT_GRAPHICS bool GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr); #endif diff --git a/test/unittest/ability_service_extension_test/BUILD.gn b/test/unittest/ability_service_extension_test/BUILD.gn index 74b39b9039..3c95283a65 100644 --- a/test/unittest/ability_service_extension_test/BUILD.gn +++ b/test/unittest/ability_service_extension_test/BUILD.gn @@ -72,6 +72,7 @@ ohos_unittest("ability_service_extension_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", diff --git a/test/unittest/app_service_extension_test/BUILD.gn b/test/unittest/app_service_extension_test/BUILD.gn index b872e79b95..bc50eb383f 100644 --- a/test/unittest/app_service_extension_test/BUILD.gn +++ b/test/unittest/app_service_extension_test/BUILD.gn @@ -71,6 +71,7 @@ ohos_unittest("app_service_extension_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", diff --git a/test/unittest/appkit/ohos_application_test/BUILD.gn b/test/unittest/appkit/ohos_application_test/BUILD.gn index 6a93fa1ab5..ee78b25419 100644 --- a/test/unittest/appkit/ohos_application_test/BUILD.gn +++ b/test/unittest/appkit/ohos_application_test/BUILD.gn @@ -32,6 +32,7 @@ ohos_unittest("ohos_application_test") { "${ability_runtime_path}/interfaces/kits/native/ability/native", "${ability_runtime_test_path}/mock/frameworks_kits_runtime_test/", "${ability_runtime_test_path}/mock/frameworks_kits_appkit_native_test/include", + "${ability_runtime_test_path}/unittest/runtime_test/", ] sources = [ "ohos_application_test.cpp" ] diff --git a/test/unittest/appkit/ohos_application_test/ohos_application_test.cpp b/test/unittest/appkit/ohos_application_test/ohos_application_test.cpp index 9988ae4c75..c2900baaa2 100644 --- a/test/unittest/appkit/ohos_application_test/ohos_application_test.cpp +++ b/test/unittest/appkit/ohos_application_test/ohos_application_test.cpp @@ -37,6 +37,7 @@ #endif #undef private #include "mock_ability_stage.h" +#include "mock_js_runtime.h" using namespace testing; using namespace testing::ext; @@ -1703,5 +1704,96 @@ HWTEST_F(OHOSApplicationTest, AppExecFwk_OHOSApplicationTest_LaunchElement_0300, GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_LaunchElement_0300 end."; } + +/* +* @tc.number: AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0100 +* @tc.name: InitJSLeakWatcher +* @tc.desc: Verify function InitJSLeakWatcher when runtime_ is nullptr +*/ +HWTEST_F(OHOSApplicationTest, AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0100 start."; + + // Ensure runtime_ is nullptr + ohosApplication_->runtime_ = nullptr; + + // Call InitJSLeakWatcher - should handle gracefully without crashing + std::string bundleName = "com.example.test"; + ohosApplication_->InitJSLeakWatcher(bundleName); + + // Verify runtime_ is still nullptr (no crash occurred) + EXPECT_TRUE(ohosApplication_->runtime_ == nullptr); + + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0100 end."; +} + +/* +* @tc.number: AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0200 +* @tc.name: InitJSLeakWatcher +* @tc.desc: Verify function InitJSLeakWatcher with valid runtime_ +*/ +HWTEST_F(OHOSApplicationTest, AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0200 start."; + + // Set runtime_ to MockJsRuntime + ohosApplication_->runtime_ = std::make_unique(); + ASSERT_NE(ohosApplication_->runtime_, nullptr); + + // Call InitJSLeakWatcher with bundle name + std::string bundleName = "com.example.test"; + ohosApplication_->InitJSLeakWatcher(bundleName); + + // Verify runtime_ is still valid after the call + EXPECT_TRUE(ohosApplication_->runtime_ != nullptr); + + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0200 end."; +} + +/* +* @tc.number: AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0300 +* @tc.name: InitJSLeakWatcher +* @tc.desc: Verify function InitJSLeakWatcher with empty bundle name +*/ +HWTEST_F(OHOSApplicationTest, AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0300, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0300 start."; + + // Set runtime_ to MockJsRuntime + ohosApplication_->runtime_ = std::make_unique(); + ASSERT_NE(ohosApplication_->runtime_, nullptr); + + // Call InitJSLeakWatcher with empty bundle name + std::string bundleName = ""; + ohosApplication_->InitJSLeakWatcher(bundleName); + + // Verify runtime_ is still valid after the call + EXPECT_TRUE(ohosApplication_->runtime_ != nullptr); + + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0300 end."; +} + +/* +* @tc.number: AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0400 +* @tc.name: InitJSLeakWatcher +* @tc.desc: Verify function InitJSLeakWatcher with long bundle name +*/ +HWTEST_F(OHOSApplicationTest, AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0400, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0400 start."; + + // Set runtime_ to MockJsRuntime + ohosApplication_->runtime_ = std::make_unique(); + ASSERT_NE(ohosApplication_->runtime_, nullptr); + + // Call InitJSLeakWatcher with long bundle name + std::string bundleName = "com.very.long.package.name.for.testing.purposes.bundle"; + ohosApplication_->InitJSLeakWatcher(bundleName); + + // Verify runtime_ is still valid after the call + EXPECT_TRUE(ohosApplication_->runtime_ != nullptr); + + GTEST_LOG_(INFO) << "AppExecFwk_OHOSApplicationTest_InitJSLeakWatcher_0400 end."; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/cj_ability_stage_object_test/BUILD.gn b/test/unittest/cj_ability_stage_object_test/BUILD.gn index 157e6e5415..f008ba1ef9 100644 --- a/test/unittest/cj_ability_stage_object_test/BUILD.gn +++ b/test/unittest/cj_ability_stage_object_test/BUILD.gn @@ -81,6 +81,7 @@ ohos_unittest("cj_ability_stage_object_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", diff --git a/test/unittest/cj_ability_stage_test/BUILD.gn b/test/unittest/cj_ability_stage_test/BUILD.gn index 84bc02908b..2f061cc183 100644 --- a/test/unittest/cj_ability_stage_test/BUILD.gn +++ b/test/unittest/cj_ability_stage_test/BUILD.gn @@ -86,6 +86,7 @@ ohos_unittest("cj_ability_stage_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", diff --git a/test/unittest/cj_ui_ability_test/BUILD.gn b/test/unittest/cj_ui_ability_test/BUILD.gn index 6ac252fdbb..0c3c6c7af3 100644 --- a/test/unittest/cj_ui_ability_test/BUILD.gn +++ b/test/unittest/cj_ui_ability_test/BUILD.gn @@ -89,6 +89,7 @@ ohos_unittest("cj_ui_ability_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", diff --git a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn index ba0531af70..610ea79686 100644 --- a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn @@ -118,6 +118,7 @@ ohos_unittest("ability_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "input:libmmi-client", @@ -612,6 +613,7 @@ ohos_unittest("ability_impl_active_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -670,6 +672,7 @@ ohos_unittest("ability_impl_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -736,6 +739,7 @@ ohos_unittest("ui_ability_impl_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -819,6 +823,7 @@ ohos_unittest("ability_thread_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -906,6 +911,7 @@ ohos_unittest("fa_ability_thread_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -1033,6 +1039,7 @@ ohos_unittest("ui_ability_thread_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -1478,6 +1485,7 @@ ohos_unittest("data_ability_impl_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", @@ -1549,6 +1557,7 @@ ohos_unittest("data_ability_impl_file_secondpart_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -1617,6 +1626,7 @@ ohos_unittest("data_ability_impl_file_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -1687,6 +1697,7 @@ ohos_unittest("ability_thread_dataability_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -1935,6 +1946,7 @@ ohos_unittest("ui_ability_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -2031,6 +2043,7 @@ ohos_unittest("form_host_client_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "i18n:intl_util", "init:libbegetutil", @@ -2089,6 +2102,7 @@ ohos_unittest("continuation_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -2321,6 +2335,7 @@ ohos_unittest("ability_window_test") { "graphic_2d:librender_service_base", "graphic_2d:librender_service_client", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "image_framework:image", "init:libbegetutil", @@ -2392,6 +2407,7 @@ ohos_unittest("ability_handler_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -2459,6 +2475,7 @@ ohos_unittest("ability_impl_factory_test") { "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", @@ -3420,6 +3437,7 @@ ohos_unittest("ability_second_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "input:libmmi-client", diff --git a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn index f3d8128de8..b7af07292e 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn @@ -103,6 +103,7 @@ ohos_unittest("application_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -175,6 +176,7 @@ ohos_unittest("context_impl_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -238,6 +240,7 @@ ohos_unittest("context_impl_second_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -300,6 +303,7 @@ ohos_unittest("context_impl_third_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -355,6 +359,7 @@ ohos_unittest("context_container_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -561,6 +566,7 @@ ohos_unittest("context_deal_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -625,6 +631,7 @@ ohos_unittest("application_impl_test") { "form_fwk:fmskit_native", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -777,6 +784,7 @@ ohos_unittest("form_extension_context_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hichecker:libhichecker", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core",