diff --git a/test/unittest/app_mgr_service_inner_ninth_test/BUILD.gn b/test/unittest/app_mgr_service_inner_ninth_test/BUILD.gn index 4e22e11ce0ccdd1a02ecf8aad45e52dcd2669ae7..57721aee83cbe6e6bafb273f1564a9059e911622 100644 --- a/test/unittest/app_mgr_service_inner_ninth_test/BUILD.gn +++ b/test/unittest/app_mgr_service_inner_ninth_test/BUILD.gn @@ -46,7 +46,6 @@ ohos_unittest("app_mgr_service_inner_ninth_test") { #"${ability_runtime_services_path}/abilitymgr/src/ability_connect_callback_stub.cpp", "${ability_runtime_path}/tools/aa/src/shell_command_result.cpp", "${ability_runtime_path}/tools/aa/src/test_observer_proxy.cpp", - "${ability_runtime_services_path}/appmgr/src/ability_running_record.cpp", "${ability_runtime_services_path}/appmgr/src/advanced_security_mode_manager.cpp", "${ability_runtime_services_path}/appmgr/src/ams_mgr_scheduler.cpp", "${ability_runtime_services_path}/appmgr/src/app_config_data_manager.cpp", @@ -62,7 +61,6 @@ ohos_unittest("app_mgr_service_inner_ninth_test") { "${ability_runtime_services_path}/appmgr/src/child_process_record.cpp", "${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp", "${ability_runtime_services_path}/appmgr/src/modal_system_app_freeze_uiextension.cpp", - "${ability_runtime_services_path}/appmgr/src/module_running_record.cpp", "${ability_runtime_services_path}/appmgr/src/multi_user_config_mgr.cpp", "${ability_runtime_services_path}/appmgr/src/quick_fix_callback_with_record.cpp", "${ability_runtime_services_path}/appmgr/src/render_record.cpp", @@ -71,6 +69,7 @@ ohos_unittest("app_mgr_service_inner_ninth_test") { "${ability_runtime_services_path}/appmgr/src/window_pid_visibility_changed_listener.cpp", "${ability_runtime_services_path}/appmgr/src/window_visibility_changed_listener.cpp", "app_mgr_service_inner_ninth_test.cpp", + "mock/src/mock_ability_running_record.cpp", "mock/src/mock_accesstoken_kit.cpp", "mock/src/mock_app_death_recipient.cpp", "mock/src/mock_app_mgr_service_event_handler.cpp", @@ -81,6 +80,7 @@ ohos_unittest("app_mgr_service_inner_ninth_test") { "mock/src/mock_bundle_mgr_helper.cpp", "mock/src/mock_exit_resident_process_manager.cpp", "mock/src/mock_ipc_skeleton.cpp", + "mock/src/mock_module_running_record.cpp", "mock/src/mock_my_status.cpp", "mock/src/mock_os_account_manager_wrapper.cpp", "mock/src/mock_parameters.cpp", diff --git a/test/unittest/app_mgr_service_inner_ninth_test/app_mgr_service_inner_ninth_test.cpp b/test/unittest/app_mgr_service_inner_ninth_test/app_mgr_service_inner_ninth_test.cpp index cce0d341a71c9cbe712be2c3bf6d6315a73a0a7f..27dc2c229c732cfb5c66ce28b061195ee5a1cd2a 100644 --- a/test/unittest/app_mgr_service_inner_ninth_test/app_mgr_service_inner_ninth_test.cpp +++ b/test/unittest/app_mgr_service_inner_ninth_test/app_mgr_service_inner_ninth_test.cpp @@ -286,6 +286,1565 @@ HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_006, TestSize.Level1) EXPECT_EQ(ret, AAFwk::ERR_NOT_SYSTEM_APP); TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_006 end"); } +/** + * @tc.name: PreloadApplication_007 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_007, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_007 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_OK; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + appMgrServiceInner->taskHandler_ = nullptr; + + std::string bundleName = "com.test.preload"; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRE_MAKE; + int32_t appIndex = 0; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_007 end"); +} + +/** + * @tc.name: PreloadApplication_008 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_008, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_008 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_INVALID_VALUE; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + + std::string bundleName = "com.test.preload"; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRE_MAKE; + int32_t appIndex = 0; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_008 end"); +} + +/** + * @tc.name: PreloadApplication_009 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_009, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_009 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_OK; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + appMgrServiceInner->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("test_queue2"); + + std::string bundleName = "com.test.preload"; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRESS_DOWN; + int32_t appIndex = 0; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_OK); + appMgrServiceInner->taskHandler_.reset(); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_009 end"); +} + +/** + * @tc.name: PreloadApplication_010 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_010, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_010 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_OK; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + appMgrServiceInner->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("test_queue3"); + + std::string bundleName = "com.test.preload"; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRELOAD_MODULE; + int32_t appIndex = 0; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_OK); + appMgrServiceInner->taskHandler_.reset(); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_010 end"); +} + +/** + * @tc.name: PreloadApplication_011 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_011, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_011 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_INVALID_VALUE; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + + std::string bundleName = ""; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRE_MAKE; + int32_t appIndex = 0; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_011 end"); +} + +/** + * @tc.name: PreloadApplication_012 + * @tc.desc: test PreloadApplication + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, PreloadApplication_012, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_012 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().judgeCallerIsAllowed_ = true; + AAFwk::MyStatus::GetInstance().verifyCallingPermission_ = true; + AAFwk::MyStatus::GetInstance().isLogoutUser_ = false; + AAFwk::MyStatus::GetInstance().allowPreload_ = true; + AAFwk::MyStatus::GetInstance().generatePreloadRequestRet_ = ERR_OK; + appMgrServiceInner->appPreloader_ = std::make_shared(nullptr); + appMgrServiceInner->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("test_queue4"); + + std::string bundleName = "com.test.preload"; + int32_t userId = 100; + PreloadMode preloadMode = PreloadMode::PRE_MAKE; + int32_t appIndex = 1; + + int32_t ret = appMgrServiceInner->PreloadApplication(bundleName, userId, preloadMode, appIndex); + EXPECT_EQ(ret, ERR_OK); + appMgrServiceInner->taskHandler_.reset(); + TAG_LOGI(AAFwkTag::TEST, "PreloadApplication_012 end"); +} + +/** + * @tc.name: HandlePreloadApplication_001 + * @tc.desc: Test HandlePreloadApplication with null abilityInfo + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_001 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Setup request with null abilityInfo + PreloadRequest request; + request.abilityInfo = nullptr; + request.appInfo = std::make_shared(); + request.bundleInfo.name = "com.test.app"; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRE_MAKE; + request.appIndex = 0; + appMgrServiceInner->HandlePreloadApplication(request); + + EXPECT_EQ(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_001 end"); +} + +/** + * @tc.name: HandlePreloadApplication_002 + * @tc.desc: Test HandlePreloadApplication with null appRunningManager + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_002 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Set appRunningManager to null + appMgrServiceInner->appRunningManager_ = nullptr; + + // Setup valid request + PreloadRequest request; + request.abilityInfo = std::make_shared(); + request.appInfo = std::make_shared(); + request.bundleInfo.name = "com.test.app"; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRE_MAKE; + request.appIndex = 0; + + // Call HandlePreloadApplication + appMgrServiceInner->HandlePreloadApplication(request); + + // Verify that CheckAppRunningRecordIsExist was never called (early return) + EXPECT_EQ(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_002 end"); +} + +/** + * @tc.name: HandlePreloadApplication_003 + * @tc.desc: Test HandlePreloadApplication with existing app record + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_003 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Setup appRunningManager + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + // Configure mock to return existing app record + AAFwk::MyStatus::GetInstance().checkAppRunning_ = std::make_shared( + std::make_shared(), 0, "test"); + + // Setup valid request + PreloadRequest request; + request.abilityInfo = std::make_shared(); + request.appInfo = std::make_shared(); + request.bundleInfo.name = "com.test.app"; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRE_MAKE; + request.appIndex = 0; + + // Call HandlePreloadApplication + appMgrServiceInner->HandlePreloadApplication(request); + + // Verify that CheckAppRunningRecordIsExist was called and existing app record was detected + EXPECT_GT(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().handlePreloadApplication_existingAppRecord_called_); + + // Cleanup + AAFwk::MyStatus::GetInstance().checkAppRunning_ = nullptr; + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_003 end"); +} + +/** + * @tc.name: HandlePreloadApplication_004 + * @tc.desc: Test HandlePreloadApplication with app multi-user not existing + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_004 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Setup appRunningManager + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + // Configure mock to return no existing app record and no multi-user app + AAFwk::MyStatus::GetInstance().checkAppRunning_ = nullptr; + AAFwk::MyStatus::GetInstance().checkAppRunningByUid_ = false; + + // Setup valid request + PreloadRequest request; + request.abilityInfo = std::make_shared(); + request.appInfo = std::make_shared(); + request.bundleInfo.name = "com.test.app"; + request.bundleInfo.uid = 1000; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRE_MAKE; + request.appIndex = 0; + + // Call HandlePreloadApplication + appMgrServiceInner->HandlePreloadApplication(request); + + // Verify that CheckAppRunningRecordIsExist was called and app multi-user not existing was detected + EXPECT_GT(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().handlePreloadApplication_appMultiUserNotExist_called_); + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_004 end"); +} + +/** + * @tc.name: HandlePreloadApplication_005 + * @tc.desc: Test HandlePreloadApplication with successful app record creation for PRE_MAKE mode + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_005 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Setup appRunningManager + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + // Configure mock for successful flow + AAFwk::MyStatus::GetInstance().checkAppRunning_ = nullptr; + AAFwk::MyStatus::GetInstance().checkAppRunningByUid_ = true; + + // Setup valid request with PRE_MAKE mode + PreloadRequest request; + request.abilityInfo = std::make_shared(); + request.abilityInfo->name = "TestAbility"; + request.appInfo = std::make_shared(); + request.appInfo->name = "com.test.app"; + request.bundleInfo.name = "com.test.app"; + request.bundleInfo.uid = 1000; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRE_MAKE; + request.appIndex = 0; + + // Call HandlePreloadApplication + appMgrServiceInner->HandlePreloadApplication(request); + + // Verify that CheckAppRunningRecordIsExist was called (normal flow) + EXPECT_GT(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_005 end"); +} + +/** + * @tc.name: HandlePreloadApplication_006 + * @tc.desc: Test HandlePreloadApplication with PRELOAD_MODULE mode + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, HandlePreloadApplication_006, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_006 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset tracking flags + AAFwk::MyStatus::GetInstance().resetHandlePreloadApplicationFlags(); + AAFwk::MyStatus::GetInstance().checkAppRunningCall_ = 0; + + // Setup appRunningManager + appMgrServiceInner->appRunningManager_ = std::make_shared(); + + // Configure mock for successful flow + AAFwk::MyStatus::GetInstance().checkAppRunning_ = nullptr; + AAFwk::MyStatus::GetInstance().checkAppRunningByUid_ = true; + + // Setup valid request with PRELOAD_MODULE mode + PreloadRequest request; + request.abilityInfo = std::make_shared(); + request.abilityInfo->name = "TestAbility"; + request.appInfo = std::make_shared(); + request.appInfo->name = "com.test.app"; + request.bundleInfo.name = "com.test.app"; + request.bundleInfo.uid = 1000; + request.hapModuleInfo.name = "entry"; + request.hapModuleInfo.bundleName = "com.test.app"; + request.want = std::make_shared(); + request.preloadMode = PreloadMode::PRELOAD_MODULE; + request.appIndex = 0; + + appMgrServiceInner->HandlePreloadApplication(request); + + EXPECT_GT(AAFwk::MyStatus::GetInstance().checkAppRunningCall_, 0); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().handlePreloadApplication_existingAppRecord_called_); + + TAG_LOGI(AAFwkTag::TEST, "HandlePreloadApplication_006 end"); +} + +/** + * @tc.name: SetSceneBoardAttachFlag_001 + * @tc.desc: Test SetSceneBoardAttachFlag with different flag values + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, SetSceneBoardAttachFlag_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "SetSceneBoardAttachFlag_001 start"); + + auto appMgrServiceInner = std::make_shared(); + + // Test setting flag to true + appMgrServiceInner->SetSceneBoardAttachFlag(true); + EXPECT_TRUE(appMgrServiceInner->sceneBoardAttachFlag_); + + // Test setting flag to false + appMgrServiceInner->SetSceneBoardAttachFlag(false); + EXPECT_FALSE(appMgrServiceInner->sceneBoardAttachFlag_); + + TAG_LOGI(AAFwkTag::TEST, "SetSceneBoardAttachFlag_001 end"); +} + +/** + * @tc.name: RestartResidentProcessDependedOnWeb_001 + * @tc.desc: Test RestartResidentProcessDependedOnWeb when bundleNames is empty + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, RestartResidentProcessDependedOnWeb_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_001 start"); + + auto appMgrServiceInner = std::make_shared(); + + // Reset mock flags before test + AAFwk::MyStatus::GetInstance().resetRestartResidentProcessDependedOnWebFlags(); + + // Setup mock to return empty bundleNames (default behavior) + AAFwk::MyStatus::GetInstance().mockBundleNames_.clear(); + + // Call RestartResidentProcessDependedOnWeb - should return early due to empty bundleNames + appMgrServiceInner->RestartResidentProcessDependedOnWeb(); + + // Verify HandleExitResidentBundleDependedOnWeb was called + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().handleExitResidentBundleDependedOnWeb_called_); + + // Verify early return path was taken (emptyBundleNames flag should be set) + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().restartResidentProcessDependedOnWeb_emptyBundleNames_called_); + + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_001 end"); +} + +/** + * @tc.name: RestartResidentProcessDependedOnWeb_002 + * @tc.desc: Test RestartResidentProcessDependedOnWeb when taskHandler_ is null + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, RestartResidentProcessDependedOnWeb_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_002 start"); + + auto appMgrServiceInner = std::make_shared(); + + // Reset mock flags before test + AAFwk::MyStatus::GetInstance().resetRestartResidentProcessDependedOnWebFlags(); + + // Setup mock to return non-empty bundleNames to pass the first check + AAFwk::MyStatus::GetInstance().mockBundleNames_.clear(); + AAFwk::MyStatus::GetInstance().mockBundleNames_.push_back( + AppExecFwk::ExitResidentProcessInfo("com.test.bundle", 1000)); + + // Ensure taskHandler_ is null (it should be null by default) + EXPECT_EQ(appMgrServiceInner->taskHandler_, nullptr); + + // Call RestartResidentProcessDependedOnWeb - should return early due to null taskHandler_ + appMgrServiceInner->RestartResidentProcessDependedOnWeb(); + // Verify HandleExitResidentBundleDependedOnWeb was called + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().handleExitResidentBundleDependedOnWeb_called_); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().restartResidentProcessDependedOnWeb_taskSubmitted_called_); + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_002 end"); +} + +/** + * @tc.name: RestartResidentProcessDependedOnWeb_003 + * @tc.desc: Test RestartResidentProcessDependedOnWeb normal flow with valid taskHandler + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, RestartResidentProcessDependedOnWeb_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_003 start"); + + auto appMgrServiceInner = std::make_shared(); + + // Reset mock flags before test + AAFwk::MyStatus::GetInstance().resetRestartResidentProcessDependedOnWebFlags(); + + // Setup mock to return non-empty bundleNames + AAFwk::MyStatus::GetInstance().mockBundleNames_.clear(); + AAFwk::MyStatus::GetInstance().mockBundleNames_.push_back( + AppExecFwk::ExitResidentProcessInfo("com.test.bundle1", 1000)); + AAFwk::MyStatus::GetInstance().mockBundleNames_.push_back( + AppExecFwk::ExitResidentProcessInfo("com.test.bundle2", 1001)); + + appMgrServiceInner->taskHandler_ = + std::make_shared("RestartResidentProcessTest"); + + appMgrServiceInner->RestartResidentProcessDependedOnWeb(); + + // Verify HandleExitResidentBundleDependedOnWeb was called + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().handleExitResidentBundleDependedOnWeb_called_); + + // Verify task was submitted to taskHandler (normal flow) + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().restartResidentProcessDependedOnWeb_taskSubmitted_called_); + + // Verify that early return paths were not taken + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().restartResidentProcessDependedOnWeb_emptyBundleNames_called_); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().restartResidentProcessDependedOnWeb_nullTaskHandler_called_); + + TAG_LOGI(AAFwkTag::TEST, "RestartResidentProcessDependedOnWeb_003 end"); +} + +/** + * @tc.name: AttachApplication_001 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_001 start"); + AAFwk::MyStatus::GetInstance().resetGetApplicationInfoFlag(); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().getApplicationInfoCalled_); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = sptr::MakeSptr(); + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + // Verify that GetApplicationInfo was called (line 1243 was reached) + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().getApplicationInfoCalled_); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_001 end"); +} + +/** + * @tc.name: AttachApplication_002 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_002 start"); + AAFwk::MyStatus::GetInstance().resetAppDeathRecipientSetTaskHandlerFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = sptr::MakeSptr(); + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().appDeathRecipientSetTaskHandlerCalled_); + AAFwk::MyStatus::GetInstance().resetAppDeathRecipientSetTaskHandlerFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_002 end"); +} + +/** + * @tc.name: AttachApplication_003 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_003 start"); + AAFwk::MyStatus::GetInstance().resetGetApplicationInfoFlag(); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = nullptr; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().appDeathRecipientSetTaskHandlerCalled_); + AAFwk::MyStatus::GetInstance().resetAppDeathRecipientSetTaskHandlerFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_003 end"); +} + +/** + * @tc.name: AttachApplication_004 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_004 start"); + AAFwk::MyStatus::GetInstance().resetGetApplicationInfoFlag(); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = sptr::MakeSptr(); + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().appDeathRecipientSetTaskHandlerCalled_); + AAFwk::MyStatus::GetInstance().resetAppDeathRecipientSetTaskHandlerFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_004 end"); +} + +/** + * @tc.name: AttachApplication_005 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_005 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=false; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().appRunningRecordSetAppDeathRecipientCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_005 end"); +} + +/** + * @tc.name: AttachApplication_006 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_006, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_006 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().appRunningRecordSetAppDeathRecipientCalled_); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=true; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().appRunningRecordSetAppDeathRecipientCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_006 end"); +} + +/** + * @tc.name: AttachApplication_007 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_007, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_007 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetState(ApplicationState::APP_STATE_CREATE); + + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=true; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().setNWebPreloadCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_007 end"); +} + +/** + * @tc.name: AttachApplication_008 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_008, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_008 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr applicationInfo = nullptr; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetState(ApplicationState::APP_STATE_READY); + + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=true; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().setNWebPreloadCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_008 end"); +} + +/** + * @tc.name: AttachApplication_009 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_009, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_009 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + appMgrServiceInner->sceneBoardAttachFlag_ = false; + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetState(ApplicationState::APP_STATE_CREATE); + + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=true; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_FALSE(appMgrServiceInner->sceneBoardAttachFlag_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_009 end"); +} + +/** + * @tc.name: AttachApplication_010 + * @tc.desc: Test AttachApplication calls GetApplicationInfo to verify execution + * @tc.type: FUNC + * @tc.require: Test AttachApplication method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, AttachApplication_010, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_010 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + appMgrServiceInner->sceneBoardAttachFlag_ = false; + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = Constants::SCENE_BOARD_BUNDLE_NAME; + const int32_t testPid = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, 1, processName); + + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetState(ApplicationState::APP_STATE_CREATE); + + sptr mockAppScheduler = sptr::MakeSptr(); + AAFwk::MyStatus::GetInstance().addDeathRecipientReturn_=true; + appMgrServiceInner->AttachApplication(testPid, mockAppScheduler); + EXPECT_TRUE(appMgrServiceInner->sceneBoardAttachFlag_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "AttachApplication_010 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_001 + * @tc.desc: Test ApplicationBackgrounded with null app record + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_001 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + const int32_t testRecordId = 1234; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = nullptr; + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().addAppLifecycleEventCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_001 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_002 + * @tc.desc: Test ApplicationBackgrounded with invalid schedule state + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_002 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_FOREGROUNDING); + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().addAppLifecycleEventCalled_); + EXPECT_NE(appRecord->GetApplicationScheduleState(), ApplicationScheduleState::SCHEDULE_READY); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_002 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_003 + * @tc.desc: Test ApplicationBackgrounded with valid foreground app + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_003 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + + // Set valid conditions for backgrounding + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); + + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().setApplicationScheduleStateCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_003 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_004 + * @tc.desc: Test ApplicationBackgrounded with non-foreground app + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_004 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); + appRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_EQ(appRecord->GetState(), ApplicationState::APP_STATE_BACKGROUND); + + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_004 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_005 + * @tc.desc: Test ApplicationBackgrounded with FOREGROUNDING pending state + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_005 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + + // Set valid conditions with FOREGROUNDING pending state + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); + appRecord->SetState(ApplicationState::APP_STATE_BACKGROUND); + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().getNameCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_005 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_006 + * @tc.desc: Test ApplicationBackgrounded with BACKGROUNDING pending state + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_006, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_006 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + + // Set valid conditions with BACKGROUNDING pending state + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); + appRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + appRecord->SetApplicationPendingState(ApplicationPendingState::FOREGROUNDING); + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().scheduleForegroundRunningCalled_); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_006 end"); +} + +/** + * @tc.name: ApplicationBackgrounded_007 + * @tc.desc: Test ApplicationBackgrounded with UI extension type + * @tc.type: FUNC + * @tc.require: Test ApplicationBackgrounded method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, ApplicationBackgrounded_007, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_007 start"); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = appRecord; + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.clear(); + AAFwk::MyStatus::GetInstance().getAppRunningRecordMap_.insert({testRecordId, appRecord}); + + // Set valid conditions with BACKGROUNDING pending state + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); + appRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + appRecord->SetApplicationPendingState(ApplicationPendingState::BACKGROUNDING); + appMgrServiceInner->ApplicationBackgrounded(testRecordId); + EXPECT_TRUE(appRecord->GetApplicationPendingState() == ApplicationPendingState::READY); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + + TAG_LOGI(AAFwkTag::TEST, "ApplicationBackgrounded_007 end"); +} + +/** + * @tc.name: GetRunningProcesses_001 + * @tc.desc: Test GetRunningProcesses function + * @tc.type: FUNC + * @tc.require: Test GetRunningProcesses + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcesses_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcesses_001 start"); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.bundle.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + EXPECT_NE(appRecord, nullptr); + std::vector info; + EXPECT_EQ(info.size(), 0); + appMgrServiceInner->GetRunningProcesses(appRecord, info); + EXPECT_EQ(info.size(), 1); + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcesses_001 end"); +} + +/** + * @tc.name: GetRunningProcess_002 + * @tc.desc: Test GetRunningProcess function + * @tc.type: FUNC + * @tc.require: Test GetRunningProcess method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcess_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_002 start"); + auto appMgrServiceInner = std::make_shared(); + + std::shared_ptr appRecord = nullptr; + RunningProcessInfo info; + appMgrServiceInner->GetRunningProcess(appRecord, info); + EXPECT_TRUE(info.processName_.empty()); + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_002 end"); +} + +/** + * @tc.name: GetRunningProcess_003 + * @tc.desc: Test GetRunningProcess function + * @tc.type: FUNC + * @tc.require: Test GetRunningProcess method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcess_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_003 start"); + auto appMgrServiceInner = std::make_shared(); + + // Create valid application info + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.app.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + EXPECT_NE(appRecord, nullptr); + + RunningProcessInfo info; + appMgrServiceInner->GetRunningProcess(appRecord, info); + EXPECT_EQ(info.processName_, processName); + + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_003 end"); +} + +/** + * @tc.name: GetRunningProcess_004 + * @tc.desc: Test GetRunningProcess + * @tc.type: FUNC + * @tc.require: Test GetRunningProcess + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcess_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_004 start"); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.app.name"; + applicationInfo->bundleName = "test.bundle.name"; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + auto userTestInfo = std::make_shared(); + appRecord->SetUserTestInfo(userTestInfo); + RunningProcessInfo info; + AAFwk::MyStatus::GetInstance().getBoolParameter_ = true; + info.isTestMode = false; + appMgrServiceInner->GetRunningProcess(appRecord, info); + EXPECT_TRUE(info.isTestMode); + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_004 end"); +} + +/** + * @tc.name: GetRunningProcess_005 + * @tc.desc: Test GetRunningProcess function + * @tc.type: FUNC + * @tc.require: Test GetRunningProcess method + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcess_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_005 start"); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.app.name"; + applicationInfo->bundleName = "test.bundle.name"; + applicationInfo->bundleType = BundleType::APP; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + EXPECT_NE(appRecord, nullptr); + + RunningProcessInfo info; + appMgrServiceInner->GetRunningProcess(appRecord, info); + EXPECT_EQ(info.bundleType, static_cast(BundleType::APP)); + + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_005 end"); +} + +/** + * @tc.name: GetRunningProcess_006 + * @tc.desc: Test GetRunningProcess function + * @tc.type: FUNC + * @tc.require: Test GetRunningProcess + */ +HWTEST_F(AppMgrServiceInnerNinthTest, GetRunningProcess_006, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_006 start"); + auto appMgrServiceInner = std::make_shared(); + + auto applicationInfo = std::make_shared(); + applicationInfo->name = "test.app.name"; + applicationInfo->bundleName = "test.bundle.name"; + applicationInfo->multiAppMode.multiAppModeType = MultiAppModeType::APP_CLONE; + const int32_t testRecordId = 1234; + const std::string processName = "test_process"; + auto appRecord = std::make_shared(applicationInfo, testRecordId, processName); + EXPECT_NE(appRecord, nullptr); + const int32_t testAppIndex = 5; + appRecord->SetAppIndex(testAppIndex); + RunningProcessInfo info; + appMgrServiceInner->GetRunningProcess(appRecord, info); + EXPECT_EQ(info.appCloneIndex, testAppIndex); + + TAG_LOGI(AAFwkTag::TEST, "GetRunningProcess_006 end"); +} + +/** + * @tc.name: WaitForRemoteProcessExit_001 + * @tc.desc: Test WaitForRemoteProcessExit when all processes exit immediately + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, WaitForRemoteProcessExit_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_001 start"); + auto appMgrServiceInner = std::make_shared(); + + // Create a list of PIDs that don't exist (simulating processes that have already exited) + std::list pids = {99999, 88888}; // Non-existent PIDs + int64_t startTime = appMgrServiceInner->SystemTimeMillisecond(); + + // Since ProcessUtil::CheckAllProcessExit will return true for non-existent PIDs, + // the function should return true immediately + bool result = appMgrServiceInner->WaitForRemoteProcessExit(pids, startTime); + EXPECT_TRUE(result); + + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_001 end"); +} + +/** + * @tc.name: WaitForRemoteProcessExit_002 + * @tc.desc: Test WaitForRemoteProcessExit timeout scenario + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, WaitForRemoteProcessExit_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_002 start"); + auto appMgrServiceInner = std::make_shared(); + + // Create a list with PID 1 (init process that will always exist) + std::list pids = {1}; + // Set start time far in the past to simulate timeout condition + int64_t startTime = appMgrServiceInner->SystemTimeMillisecond() - 2000; // 2 seconds ago + + // The function should timeout and return false since PID 1 will always exist + bool result = appMgrServiceInner->WaitForRemoteProcessExit(pids, startTime); + EXPECT_FALSE(result); + + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_002 end"); +} + +/** + * @tc.name: WaitForRemoteProcessExit_003 + * @tc.desc: Test WaitForRemoteProcessExit with empty PID list + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, WaitForRemoteProcessExit_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_003 start"); + auto appMgrServiceInner = std::make_shared(); + + // Create an empty list of PIDs + std::list pids; + int64_t startTime = appMgrServiceInner->SystemTimeMillisecond(); + + // With an empty list, CheckAllProcessExit should return true immediately + bool result = appMgrServiceInner->WaitForRemoteProcessExit(pids, startTime); + EXPECT_TRUE(result); + + TAG_LOGI(AAFwkTag::TEST, "WaitForRemoteProcessExit_003 end"); +} + +/** + * @tc.name: StartAbility_001 + * @tc.desc: Test StartAbility with null abilityInfo parameter + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_001 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = nullptr; + std::shared_ptr abilityInfo = nullptr; // Null abilityInfo + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + // Call StartAbility with null abilityInfo + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function returns early due to null abilityInfo + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().startAbility_nullAbilityInfo_called_); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_001 end"); +} + +/** + * @tc.name: StartAbility_002 + * @tc.desc: Test StartAbility with null appRecord parameter + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_002 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = nullptr; + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + std::shared_ptr appRecord = nullptr; // Null appRecord + HapModuleInfo hapModuleInfo; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + // Call StartAbility with null appRecord + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function returns early due to null appRecord + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().startAbility_nullAppRecord_called_); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_002 end"); +} + +/** + * @tc.name: StartAbility_003 + * @tc.desc: Test StartAbility with singleton mode and existing ability + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_003 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = nullptr; + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::SINGLETON; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + // Set up mock to return existing ability for singleton mode + auto existingAbility = std::make_shared(abilityInfo, token, 1); + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = existingAbility; + appRecord->securityFlag_=true; // Simulate security flag for singleton ability + // Call StartAbility with singleton mode and existing ability + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + EXPECT_TRUE(want->GetBoolParam(DLP_PARAMS_SECURITY_FLAG, false)); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_003 end"); +} + +/** + * @tc.name: StartAbility_004 + * @tc.desc: Test StartAbility with existing ability and preToken + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_004 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = new MockAppScheduler(); // Non-null preToken + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::STANDARD; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + auto want = std::make_shared(); + want->SetParam(DEBUG_APP, true); + appRecord->SetDebugApp(false); + int32_t abilityRecordId = 1; + + // Set up mock to return existing ability + auto existingAbility = std::make_shared(abilityInfo, token, 1); + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = existingAbility; + + // Call StartAbility with existing ability and preToken + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function returns early due to ability already existing with preToken + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().debugAppCalledTimes_ == 2); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_004 end"); +} + +/** + * @tc.name: StartAbility_005 + * @tc.desc: Test StartAbility with module record creation failure + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_005, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_005 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = nullptr; + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::SINGLETON; + abilityInfo->applicationInfo.bundleName = "com.test.bundle"; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "entry"; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = + std::make_shared(abilityInfo, token, abilityRecordId); + + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().addModuleCalled_); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_005 end"); +} + +/** + * @tc.name: StartAbility_006 + * @tc.desc: Test StartAbility with ability creation failure + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_006, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_006 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = new MockAppScheduler(); // Non-null preToken + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::STANDARD; + abilityInfo->applicationInfo.bundleName = "com.test.bundle"; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "entry"; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = + std::make_shared(abilityInfo, token, abilityRecordId); + + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function returns early due to ability creation failure + EXPECT_EQ(AAFwk::MyStatus::GetInstance().addModuleCalledTimes_, 0); + TAG_LOGI(AAFwkTag::TEST, "StartAbility_006 end"); +} + +/** + * @tc.name: StartAbility_007 + * @tc.desc: Test StartAbility with app state CREATE (should not launch) + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_007, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_007 start"); + auto appMgrServiceInner = std::make_shared(); + + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = new MockAppScheduler(); // Non-null preToken + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::STANDARD; + abilityInfo->applicationInfo.bundleName = "com.test.bundle"; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + appRecord->SetState(ApplicationState::APP_STATE_CREATE); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "entry"; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = nullptr; // No existing ability + + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function returns early due to ability creation failure + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().addModuleCalled_); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_007 end"); +} + +/** + * @tc.name: StartAbility_008 + * @tc.desc: Test StartAbility normal flow with successful ability launch + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_008, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_008 start"); + auto appMgrServiceInner = std::make_shared(); + // Reset flags + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + // Create test parameters + sptr token = new MockAppScheduler(); + sptr preToken = new MockAppScheduler(); // Non-null preToken + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::STANDARD; + abilityInfo->applicationInfo.bundleName = "com.test.bundle"; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "entry"; + auto want = std::make_shared(); + int32_t abilityRecordId = 1; + + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByToken_ = nullptr; // No existing ability + // Set up mock to return valid module record + auto appInfo = std::make_shared(abilityInfo->applicationInfo); + //auto moduleRecord = std::make_shared(appInfo, nullptr); + AAFwk::MyStatus::GetInstance().getModuleRecord_ = nullptr; + + AAFwk::MyStatus::GetInstance().simulateAddModuleFails_ = true; + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + // Verify that the function completed the normal flow and launched ability + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().getModuleRecordByModuleNameCalled_); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByTokenCalled_); + TAG_LOGI(AAFwkTag::TEST, "StartAbility_008 end"); +} + +/** + * @tc.name: StartAbility_009 + * @tc.desc: Test StartAbility with debug app processing + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerNinthTest, StartAbility_009, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "StartAbility_009 start"); + auto appMgrServiceInner = std::make_shared(); + + AAFwk::MyStatus::GetInstance().resetStartAbilityFlags(); + AAFwk::MyStatus::GetInstance().resetRunningRecordFunctionFlag(); + sptr token = new MockAppScheduler(); + sptr preToken = nullptr; + auto abilityInfo = std::make_shared(); + abilityInfo->bundleName = "com.test.bundle"; + abilityInfo->name = "TestAbility"; + abilityInfo->launchMode = LaunchMode::STANDARD; + abilityInfo->applicationInfo.bundleName = "com.test.bundle"; + auto appRecord = std::make_shared(nullptr, 1, "test_process"); + appRecord->SetState(ApplicationState::APP_STATE_READY); + HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "entry"; + auto want = std::make_shared(); + // Set debug app parameter + want->SetParam(DEBUG_APP, true); + int32_t abilityRecordId = 1; + + // Set up mock to return null for GetAbilityRunningRecordByToken (no existing ability) + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByTokenModule_ = nullptr; + // Set up mock to return valid module record + auto appInfo = std::make_shared(abilityInfo->applicationInfo); + auto moduleRecord = std::make_shared(appInfo, nullptr); + AAFwk::MyStatus::GetInstance().getModuleRecord_ = moduleRecord; + + // Call StartAbility with debug app + appMgrServiceInner->StartAbility(token, preToken, abilityInfo, appRecord, hapModuleInfo, want, abilityRecordId); + + EXPECT_TRUE(AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByTokenCalled_); + EXPECT_FALSE(AAFwk::MyStatus::GetInstance().getStateCalled_); + + TAG_LOGI(AAFwkTag::TEST, "StartAbility_009 end"); +} } // namespace AppExecFwk } // namespace OHOS - diff --git a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_ability_running_record.cpp b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_ability_running_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64caff2b990752c09cb0bfcf77452065d4b8341f --- /dev/null +++ b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_ability_running_record.cpp @@ -0,0 +1,187 @@ +/* + * 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 "ability_running_record.h" +#include "hilog_tag_wrapper.h" +#include "mock_my_status.h" +namespace OHOS { +namespace AppExecFwk { +namespace { +constexpr const char* BUNDLE_NAME_SCENEBOARD = "com.ohos.sceneboard"; +constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility"; +constexpr const char* IS_HOOK = "ohos.ability_runtime.is_hook"; +static const std::string EMPTY_NAME; +} +AbilityRunningRecord::AbilityRunningRecord(std::shared_ptr info, + sptr token, int32_t abilityRecordId) + : abilityRecordId_(abilityRecordId), info_(info), token_(token) +{} + +AbilityRunningRecord::~AbilityRunningRecord() +{} + +const std::string &AbilityRunningRecord::GetName() const +{ + if (info_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "GetName info_ nullptr"); + return EMPTY_NAME; + } + return info_->name; +} + +const std::string &AbilityRunningRecord::GetBundleName() const +{ + if (info_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "GetBundleName info_ nullptr"); + return EMPTY_NAME; + } + return info_->bundleName; +} + +const std::string &AbilityRunningRecord::GetModuleName() const +{ + if (info_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "GetModuleName info_ nullptr"); + return EMPTY_NAME; + } + return info_->moduleName; +} + +const std::shared_ptr &AbilityRunningRecord::GetAbilityInfo() const +{ + AAFwk::MyStatus::GetInstance().getAbilityInfoCalled_ = true; + return info_; +} + +const std::shared_ptr &AbilityRunningRecord::GetWant() const +{ + return want_; +} + +void AbilityRunningRecord::SetWant(const std::shared_ptr &want) +{ + want_ = want; +} + +const sptr &AbilityRunningRecord::GetToken() const +{ + return token_; +} + +void AbilityRunningRecord::SetState(const AbilityState state) +{ + state_ = state; +} + +AbilityState AbilityRunningRecord::GetState() const +{ + return state_; +} + +void AbilityRunningRecord::SetEventId(const int64_t eventId) +{ + eventId_ = eventId; +} + +int64_t AbilityRunningRecord::GetEventId() const +{ + return eventId_; +} + +void AbilityRunningRecord::SetTerminating() +{ + isTerminating_ = true; +} + +bool AbilityRunningRecord::IsTerminating() const +{ + return isTerminating_; +} + +void AbilityRunningRecord::SetOwnerUserId(int32_t ownerUserId) +{ + ownerUserId_ = ownerUserId; +} + +int32_t AbilityRunningRecord::GetOwnerUserId() const +{ + return ownerUserId_; +} + +void AbilityRunningRecord::SetIsSingleUser(bool flag) +{ + isSingleUser_ = flag; +} + +bool AbilityRunningRecord::IsSingleUser() const +{ + return isSingleUser_; +} + +void AbilityRunningRecord::UpdateFocusState(bool isFocus) +{ + isFocused_ = isFocus; +} + +bool AbilityRunningRecord::GetFocusFlag() const +{ + return isFocused_; +} + +void AbilityRunningRecord::SetUIExtensionAbilityId(const int32_t uiExtensionAbilityId) +{ + uiExtensionAbilityId_ = uiExtensionAbilityId; +} + +int32_t AbilityRunningRecord::GetUIExtensionAbilityId() const +{ + return uiExtensionAbilityId_; +} + +void AbilityRunningRecord::SetUserRequestCleaningStatus() +{ + isUserRequestCleaning_ = true; +} + +bool AbilityRunningRecord::IsUserRequestCleaning() const +{ + return isUserRequestCleaning_; +} + +bool AbilityRunningRecord::IsSceneBoard() const +{ + if (info_ == nullptr) { + return false; + } + return info_->name == SCENEBOARD_ABILITY_NAME && info_->bundleName == BUNDLE_NAME_SCENEBOARD; +} + +bool AbilityRunningRecord::IsHook() const +{ + return want_ && want_->GetBoolParam(IS_HOOK, false); +} + +void AbilityRunningRecord::SetUIExtensionBindAbilityId(const int32_t uiExtensionBindAbilityId) +{ + uiExtensionBindAbilityId_ = uiExtensionBindAbilityId; +} + +int32_t AbilityRunningRecord::GetUIExtensionBindAbilityId() const +{ + AAFwk::MyStatus::GetInstance().getUIExtensionBindAbilityIdCalled_ = true; + return uiExtensionBindAbilityId_; +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_module_running_record.cpp b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_module_running_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0696d4290fc61e04f5bb6f890a05a32c61b51ade --- /dev/null +++ b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_module_running_record.cpp @@ -0,0 +1,446 @@ +/* + * 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 "module_running_record.h" +#include "app_mgr_service_inner.h" +#include "app_running_record.h" +#include "app_utils.h" +#include "global_constant.h" +#include "hilog_tag_wrapper.h" +#include "hitrace_meter.h" +#include "in_process_call_wrapper.h" +#include "ui_extension_utils.h" +#include "cache_process_manager.h" +#include "mock_my_status.h" +namespace OHOS { +namespace AppExecFwk { +namespace { +const std::string ABILITY_OWNER_USERID = "AbilityMS_Owner_UserId"; +} +ModuleRunningRecord::ModuleRunningRecord( + const std::shared_ptr &info, const std::shared_ptr &eventHandler) + : appInfo_(info), eventHandler_(eventHandler) +{} + +ModuleRunningRecord::~ModuleRunningRecord() +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); +} + +void ModuleRunningRecord::Init(const HapModuleInfo &info, int32_t appIndex, + std::weak_ptr appMgrService, std::shared_ptr appLifeCycleDeal) +{ + moduleName_ = info.moduleName; + appIndex_ = appIndex; + appMgrServiceInner_ = appMgrService; + appLifeCycleDeal_ = appLifeCycleDeal; + owenState_ = ModuleRecordState::INITIALIZED_STATE; +} + +const std::string &ModuleRunningRecord::GetModuleName() const +{ + return moduleName_; +} + +const std::shared_ptr ModuleRunningRecord::GetAppInfo() +{ + return appInfo_; +} + +std::shared_ptr ModuleRunningRecord::GetAbilityRunningRecordByToken( + const sptr &token) const +{ + AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByTokenCalled_ = true; + return AAFwk::MyStatus::GetInstance().getAbilityRunningRecordByTokenModule_; +} + +std::shared_ptr ModuleRunningRecord::AddAbility(sptr token, + std::shared_ptr abilityInfo, std::shared_ptr want, int32_t abilityRecordId) +{ + TAG_LOGD(AAFwkTag::APPMGR, "Add ability."); + if (!token || !abilityInfo) { + TAG_LOGE(AAFwkTag::APPMGR, "null abilityInfo or token"); + return nullptr; + } + if (GetAbilityRunningRecordByToken(token)) { + TAG_LOGE(AAFwkTag::APPMGR, "AbilityRecord no need to add"); + return nullptr; + } + auto abilityRecord = std::make_shared(abilityInfo, token, abilityRecordId); + abilityRecord->SetWant(want); + if (appInfo_) { + abilityRecord->SetIsSingleUser(appInfo_->singleton); + } + if (want) { + abilityRecord->SetOwnerUserId(want->GetIntParam(ABILITY_OWNER_USERID, -1)); + } + std::lock_guard lock(abilitiesMutex_); + abilities_.emplace(token, abilityRecord); + return abilityRecord; +} + +bool ModuleRunningRecord::IsLastAbilityRecord(const sptr &token) +{ + if (!token) { + TAG_LOGE(AAFwkTag::APPMGR, "null token"); + return false; + } + std::lock_guard lock(abilitiesMutex_); + return ((abilities_.size() == 1) && (abilities_.find(token) != abilities_.end())); +} + +int32_t ModuleRunningRecord::GetPageAbilitySize() +{ + int pageAbilitySize = 0; + std::lock_guard lock(abilitiesMutex_); + for (auto it : abilities_) { + std::shared_ptr abilityRunningRecord = it.second; + std::shared_ptr abilityInfo = abilityRunningRecord->GetAbilityInfo(); + if (abilityInfo && (abilityInfo->type == AbilityType::PAGE || + abilityInfo->extensionAbilityType == ExtensionAbilityType::EMBEDDED_UI)) { + pageAbilitySize++; + } + } + + return pageAbilitySize; +} + +bool ModuleRunningRecord::ExtensionAbilityRecordExists() +{ + std::lock_guard lock(abilitiesMutex_); + for (auto it : abilities_) { + std::shared_ptr abilityRunningRecord = it.second; + std::shared_ptr abilityInfo = abilityRunningRecord->GetAbilityInfo(); + if (abilityInfo && abilityInfo->type != AbilityType::PAGE) { + return true; + } + } + return false; +} + +const std::map, std::shared_ptr> ModuleRunningRecord::GetAbilities() + const +{ + std::lock_guard lock(abilitiesMutex_); + return abilities_; +} + +std::shared_ptr ModuleRunningRecord::GetAbilityByTerminateLists( + const sptr &token) const +{ + if (!token) { + TAG_LOGE(AAFwkTag::APPMGR, "null token"); + return nullptr; + } + std::lock_guard lock(abilitiesMutex_); + const auto &iter = terminateAbilities_.find(token); + if (iter != terminateAbilities_.end()) { + return iter->second; + } + return nullptr; +} + +std::shared_ptr ModuleRunningRecord::GetAbilityRunningRecord(const int64_t eventId) const +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + std::lock_guard lock(abilitiesMutex_); + const auto &iter = std::find_if(abilities_.begin(), abilities_.end(), [eventId](const auto &pair) { + return pair.second->GetEventId() == eventId; + }); + if (iter != abilities_.end()) { + return iter->second; + } + + const auto &finder = std::find_if(terminateAbilities_.begin(), + terminateAbilities_.end(), + [eventId](const auto &pair) { return pair.second->GetEventId() == eventId; }); + if (finder != terminateAbilities_.end()) { + return finder->second; + } + return nullptr; +} + +void ModuleRunningRecord::OnAbilityStateChanged( + const std::shared_ptr &ability, const AbilityState state) +{ + if (!ability) { + TAG_LOGE(AAFwkTag::APPMGR, "null ability"); + return; + } + AbilityState oldState = ability->GetState(); + ability->SetState(state); + TAG_LOGI(AAFwkTag::APPMGR, + "change from %{public}d to %{public}d. bundle: %{public}s, ability: %{public}s.", oldState, state, + ability->GetAbilityInfo()->bundleName.c_str(), ability->GetName().c_str()); + auto serviceInner = appMgrServiceInner_.lock(); + if (serviceInner) { + serviceInner->OnAbilityStateChanged(ability, state); + } +} + +void ModuleRunningRecord::LaunchAbility(const std::shared_ptr &ability) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::APPMGR, "called"); + if (!ability || !ability->GetToken()) { + TAG_LOGE(AAFwkTag::APPMGR, "null abilityRecord or abilityToken"); + return; + } + std::lock_guard lock(abilitiesMutex_); + const auto &iter = abilities_.find(ability->GetToken()); + if (iter != abilities_.end() && appLifeCycleDeal_->GetApplicationClient()) { + TAG_LOGD(AAFwkTag::APPMGR, "Schedule launch ability, name is %{public}s.", ability->GetName().c_str()); + appLifeCycleDeal_->LaunchAbility(ability); + ability->SetState(AbilityState::ABILITY_STATE_READY); + SetLoaded(); + } else { + TAG_LOGE(AAFwkTag::APPMGR, "null appThread or ability"); + } +} + +void ModuleRunningRecord::LaunchPendingAbilities() +{ + TAG_LOGD(AAFwkTag::APPMGR, "Launch pending abilities."); + std::lock_guard lock(abilitiesMutex_); + if (abilities_.empty()) { + TAG_LOGE(AAFwkTag::APPMGR, "abilities_ is empty"); + return; + } + + for (const auto &item : abilities_) { + const auto &ability = item.second; + TAG_LOGD(AAFwkTag::APPMGR, "state : %{public}d", ability->GetState()); + if (ability->GetState() == AbilityState::ABILITY_STATE_CREATE && ability->GetToken() && + appLifeCycleDeal_->GetApplicationClient()) { + TAG_LOGD(AAFwkTag::APPMGR, "name is %{public}s.", ability->GetName().c_str()); + appLifeCycleDeal_->LaunchAbility(ability); + ability->SetState(AbilityState::ABILITY_STATE_READY); + SetLoaded(); + } + } +} + +void ModuleRunningRecord::TerminateAbility(const std::shared_ptr &appRecord, + const sptr &token, const bool isForce) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + auto abilityRecord = GetAbilityRunningRecordByToken(token); + if (!abilityRecord) { + TAG_LOGE(AAFwkTag::APPMGR, "null abilityRecord"); + return; + } + + { + std::lock_guard lock(abilitiesMutex_); + terminateAbilities_.emplace(token, abilityRecord); + abilities_.erase(token); + } + + if (!isForce) { + auto curAbilityState = abilityRecord->GetState(); + auto curAbilityType = abilityRecord->GetAbilityInfo()->type; + if (curAbilityState != AbilityState::ABILITY_STATE_BACKGROUND && + curAbilityType == AppExecFwk::AbilityType::PAGE) { + TAG_LOGE(AAFwkTag::APPMGR, "current state(%{public}d) error", static_cast(curAbilityState)); + return; + } + } + + if (appLifeCycleDeal_) { + if (!(appRecord->IsDebugApp() || appRecord->IsAttachDebug())) { + SendEvent(AMSEventHandler::TERMINATE_ABILITY_TIMEOUT_MSG, + AMSEventHandler::TERMINATE_ABILITY_TIMEOUT * AAFwk::AppUtils::GetInstance().GetTimeoutUnitTimeRatio(), + abilityRecord); + } + bool isCachedProcess = DelayedSingleton::GetInstance()->IsAppShouldCache(appRecord); + appLifeCycleDeal_->ScheduleCleanAbility(token, isCachedProcess); + } else { + TAG_LOGW(AAFwkTag::APPMGR, "null appLifeCycleDeal_"); + auto serviceInner = appMgrServiceInner_.lock(); + if (serviceInner) { + serviceInner->TerminateApplication(appRecord); + } + } + + TAG_LOGD(AAFwkTag::APPMGR, "end"); +} + +void ModuleRunningRecord::SendEvent( + uint32_t msg, int64_t timeOut, const std::shared_ptr &abilityRecord) +{ + TAG_LOGD(AAFwkTag::APPMGR, "Send event"); + if (!eventHandler_) { + TAG_LOGE(AAFwkTag::APPMGR, "null eventHandler_"); + return; + } + + AppRunningRecord::appEventId_++; + abilityRecord->SetEventId(AppRunningRecord::appEventId_); + eventHandler_->SendEvent(AAFwk::EventWrap(msg, AppRunningRecord::appEventId_), timeOut); +} + +void ModuleRunningRecord::AbilityTerminated(const sptr &token) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + if (!token) { + TAG_LOGE(AAFwkTag::APPMGR, "null token"); + return; + } + + if (RemoveTerminateAbilityTimeoutTask(token)) { + std::lock_guard lock(abilitiesMutex_); + terminateAbilities_.erase(token); + } +} + +bool ModuleRunningRecord::RemoveTerminateAbilityTimeoutTask(const sptr& token) const +{ + auto abilityRecord = GetAbilityByTerminateLists(token); + if (!abilityRecord) { + TAG_LOGE(AAFwkTag::APPMGR, "null abilityRecord"); + return false; + } + if (!eventHandler_) { + TAG_LOGE(AAFwkTag::APPMGR, "null eventHandler_"); + return false; + } + eventHandler_->RemoveEvent(AMSEventHandler::TERMINATE_ABILITY_TIMEOUT_MSG, abilityRecord->GetEventId()); + return true; +} + +bool ModuleRunningRecord::IsAbilitiesBackgrounded() +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + std::lock_guard lock(abilitiesMutex_); + for (const auto &iter : abilities_) { + const auto &ability = iter.second; + if (ability == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null ability"); + continue; + } + const auto &abilityInfo = ability->GetAbilityInfo(); + // uiextensionability also has foreground and background states. + if (abilityInfo != nullptr && abilityInfo->type != AbilityType::PAGE && + !AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo->extensionAbilityType)) { + continue; + } + + const auto &state = ability->GetState(); + if (state != AbilityState::ABILITY_STATE_BACKGROUND && + state != AbilityState::ABILITY_STATE_TERMINATED && + state != AbilityState::ABILITY_STATE_END) { + return false; + } + } + return true; +} + +void ModuleRunningRecord::SetAppMgrServiceInner(const std::weak_ptr &inner) +{ + appMgrServiceInner_ = inner; +} + +ModuleRecordState ModuleRunningRecord::GetModuleRecordState() +{ + return owenState_; +} + +void ModuleRunningRecord::SetModuleRecordState(const ModuleRecordState &state) +{ + owenState_ = state; +} + +void ModuleRunningRecord::GetHapModuleInfo(HapModuleInfo &info) +{ + if (appInfo_ == nullptr) { + TAG_LOGW(AAFwkTag::APPMGR, "appInfo null"); + return; + } + BundleInfo bundleInfo; + auto bundleMgrHelper = DelayedSingleton::GetInstance(); + auto userId = appInfo_->uid / BASE_USER_RANGE; + TAG_LOGD(AAFwkTag::APPMGR, "userId: %{public}d, bundleName: %{public}s, appIndex: %{public}d", userId, + appInfo_->bundleName.c_str(), appIndex_); + int32_t bundleMgrResult; + auto flag = static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) | + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE); + if (appIndex_ <= AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX) { + bundleMgrResult = IN_PROCESS_CALL(bundleMgrHelper->GetCloneBundleInfo(appInfo_->bundleName, + flag, appIndex_, bundleInfo, userId)); + } else { + bundleMgrResult = IN_PROCESS_CALL(bundleMgrHelper->GetSandboxBundleInfo(appInfo_->bundleName, + appIndex_, userId, bundleInfo)); + } + + if (bundleMgrResult != ERR_OK) { + TAG_LOGE(AAFwkTag::APPMGR, "getBundleInfo fail"); + return; + } + + bool found = false; + for (const auto &moduleIno : bundleInfo.hapModuleInfos) { + if (moduleIno.moduleName == moduleName_) { + info = moduleIno; + found = true; + break; + } + } + + if (!found) { + TAG_LOGW(AAFwkTag::APPMGR, "not found userId: %{public}d, bundleName: %{public}s, appIndex: %{public}d, " + "name: %{public}s", userId, appInfo_->bundleName.c_str(), appIndex_, moduleName_.c_str()); + } +} + +void ModuleRunningRecord::SetApplicationClient(std::shared_ptr &appLifeCycleDeal) +{ + appLifeCycleDeal_ = appLifeCycleDeal; +} + +ModuleRecordState ModuleRunningRecord::GetState() const +{ + return owenState_; +} + +bool ModuleRunningRecord::IsAllAbilityReadyToCleanedByUserRequest() +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + std::lock_guard lock(abilitiesMutex_); + for (const auto &iter : abilities_) { + const auto &ability = iter.second; + if (ability == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null ability"); + continue; + } + const auto &abilityInfo = ability->GetAbilityInfo(); + if (abilityInfo != nullptr && abilityInfo->type != AbilityType::PAGE) { + continue; + } + + if (!ability->IsUserRequestCleaning()) { + return false; + } + + const auto &state = ability->GetState(); + if (state != AbilityState::ABILITY_STATE_BACKGROUND && + state != AbilityState::ABILITY_STATE_TERMINATED && + state != AbilityState::ABILITY_STATE_END) { + return false; + } + } + return true; +} +} // namespace AppExecFwk +} // namespace OHOS