diff --git a/services/abilitymgr/test/unittest/phone/start_option_display_id_test/start_option_display_id_test.cpp b/services/abilitymgr/test/unittest/phone/start_option_display_id_test/start_option_display_id_test.cpp index fbf78a1c06..183d02f8b9 100644 --- a/services/abilitymgr/test/unittest/phone/start_option_display_id_test/start_option_display_id_test.cpp +++ b/services/abilitymgr/test/unittest/phone/start_option_display_id_test/start_option_display_id_test.cpp @@ -74,6 +74,8 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + void OnStartAms(); + void OnStopAms(); public: std::shared_ptr abilityMs_ {nullptr}; @@ -93,10 +95,8 @@ void StartOptionDisplayIdTest::TearDownTestCase() void StartOptionDisplayIdTest::SetUp() { abilityMs_ = OHOS::DelayedSingleton::GetInstance(); - abilityMs_->OnStart(); - WaitUntilTaskFinished(); + OnStartAms(); - abilityMs_->StartUser(USER_ID_U100); auto missionListMgr = abilityMs_->GetListManagerByUserId(USER_ID_U100); if (!missionListMgr) { return; @@ -110,10 +110,59 @@ void StartOptionDisplayIdTest::SetUp() void StartOptionDisplayIdTest::TearDown() { - abilityMs_->OnStop(); + OnStopAms(); OHOS::DelayedSingleton::DestroyInstance(); } +void StartOptionDisplayIdTest::OnStartAms() +{ + if (abilityMs_) { + if (abilityMs_->state_ == ServiceRunningState::STATE_RUNNING) { + return; + } + + abilityMs_->state_ = ServiceRunningState::STATE_RUNNING; + + abilityMs_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE); + EXPECT_TRUE(abilityMs_->eventLoop_); + + abilityMs_->handler_ = std::make_shared(abilityMs_->eventLoop_, abilityMs_); + EXPECT_TRUE(abilityMs_->handler_); + + // init user controller. + abilityMs_->userController_ = std::make_shared(); + EXPECT_TRUE(abilityMs_->userController_); + abilityMs_->userController_->Init(); + int userId = USER_ID_U100; + abilityMs_->userController_->SetCurrentUserId(userId); + abilityMs_->InitConnectManager(userId, true); + abilityMs_->InitDataAbilityManager(userId, true); + abilityMs_->InitPendWantManager(userId, true); + abilityMs_->systemDataAbilityManager_ = std::make_shared(); + EXPECT_TRUE(abilityMs_->systemDataAbilityManager_); + + abilityMs_->amsConfigResolver_ = std::make_shared(); + EXPECT_TRUE(abilityMs_->amsConfigResolver_); + abilityMs_->amsConfigResolver_->Parse(); + + abilityMs_->InitMissionListManager(userId, true); + abilityMs_->connectManager_->SetEventHandler(abilityMs_->handler_); + abilityMs_->eventLoop_->Run(); + + WaitUntilTaskFinished(); + return; + } + + GTEST_LOG_(INFO) << "OnStart fail"; +} + +void StartOptionDisplayIdTest::OnStopAms() +{ + abilityMs_->eventLoop_.reset(); + abilityMs_->handler_.reset(); + abilityMs_->state_ = ServiceRunningState::STATE_NOT_START; +} + /* * Feature: AbilityManagerService * Function: StartAbility diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 036f07e627..9c813997f4 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -155,7 +155,7 @@ void AppMgrServiceInner::LoadAbility(const sptr &token, const spt HILOG_ERROR("CreateAppRunningRecord failed, appRecord is nullptr"); return; } - uint32_t startFlags = BuildStartFlags(*want, *abilityInfo); + uint32_t startFlags = (want == nullptr) ? 0 : BuildStartFlags(*want, *abilityInfo); StartProcess(abilityInfo->applicationName, processName, startFlags, appRecord, appInfo->uid, appInfo->bundleName); } else { diff --git a/services/appmgr/test/unittest/ams_ability_running_record_test/ams_ability_running_record_test.cpp b/services/appmgr/test/unittest/ams_ability_running_record_test/ams_ability_running_record_test.cpp index d7eff5227f..ae894dd5b7 100644 --- a/services/appmgr/test/unittest/ams_ability_running_record_test/ams_ability_running_record_test.cpp +++ b/services/appmgr/test/unittest/ams_ability_running_record_test/ams_ability_running_record_test.cpp @@ -232,7 +232,7 @@ HWTEST_F(AmsAbilityRunningRecordTest, UpdateAbilityRunningRecord_001, TestSize.L EXPECT_EQ(abilityRunningRecord->GetState(), AbilityState::ABILITY_STATE_FOREGROUND) << "execute fail!"; appRunningRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); - EXPECT_CALL(*mockedAppClient_, ScheduleBackgroundApplication()).Times(2); + EXPECT_CALL(*mockedAppClient_, ScheduleBackgroundApplication()).Times(1); appRunningRecord->UpdateAbilityState(token, AbilityState::ABILITY_STATE_BACKGROUND); EXPECT_EQ(abilityRunningRecord->GetState(), AbilityState::ABILITY_STATE_BACKGROUND) << "execute fail!"; HILOG_DEBUG("UpdateAbilityRunningRecord_001 end."); @@ -433,7 +433,7 @@ HWTEST_F(AmsAbilityRunningRecordTest, UpdateAbilityRunningRecord_006, TestSize.L appRunningRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); abilityRunningRecord->SetState(AbilityState::ABILITY_STATE_FOREGROUND); - EXPECT_CALL(*mockedAppClient_, ScheduleBackgroundApplication()).Times(1); + EXPECT_CALL(*mockedAppClient_, ScheduleBackgroundApplication()).Times(2); appRunningRecord->UpdateAbilityState(anotherToken, AbilityState::ABILITY_STATE_FOREGROUND); auto abilities = appRunningRecord->GetAbilities(); diff --git a/services/formmgr/test/unittest/fms_form_sys_event_receiver_test/fms_form_sys_event_receiver_test.cpp b/services/formmgr/test/unittest/fms_form_sys_event_receiver_test/fms_form_sys_event_receiver_test.cpp index aff27f4d73..85a5760277 100644 --- a/services/formmgr/test/unittest/fms_form_sys_event_receiver_test/fms_form_sys_event_receiver_test.cpp +++ b/services/formmgr/test/unittest/fms_form_sys_event_receiver_test/fms_form_sys_event_receiver_test.cpp @@ -70,6 +70,31 @@ const std::string KEY_BUNDLE_NAME = "bundleName"; const std::string DEVICE_ID = "ohos-phone1"; const std::string DEF_LABEL1 = "PermissionFormRequireGrant"; +template +static void WaitUntilTaskCalled(const F &f, const std::shared_ptr &handler, std::atomic &taskCalled) +{ + const uint32_t maxRetryCount = 1000; + const uint32_t sleepTime = 1000; + uint32_t count = 0; + if (handler->PostTask(f)) { + while (!taskCalled.load()) { + ++count; + // if delay more than 1 second, break + if (count >= maxRetryCount) { + break; + } + usleep(sleepTime); + } + } +} + +static void WaitUntilTaskDone(const std::shared_ptr &handler) +{ + std::atomic taskCalled(false); + auto f = [&taskCalled]() { taskCalled.store(true); }; + WaitUntilTaskCalled(f, handler, taskCalled); +} + class FmsFormSysEventReceiverTest : public testing::Test { public: static void SetUpTestCase(); @@ -198,7 +223,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_001, TestSize.Level0) EventFwk::CommonEventData eventData; eventData.SetWant(want); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); FormDbCache::GetInstance().GetAllFormInfo(allFormInfo); FormDBInfo tempFormDBInfo; EXPECT_EQ(ERR_APPEXECFWK_FORM_NOT_EXIST_ID, FormDbCache::GetInstance().GetDBRecord(formId, tempFormDBInfo)); @@ -231,7 +259,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_002, TestSize.Level0) CreateFormRecordAndFormInfo(bundle, formId, callingUid); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); FormRecord tempFormRecord; ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); @@ -267,7 +298,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_003, TestSize.Level0) ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); @@ -297,7 +331,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_004, TestSize.Level0) want.SetBundle(bundle); want.SetParam(KEY_UID, callingUid); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); GTEST_LOG_(INFO) << "fms_form_sys_event_receiver_test_004 end"; } @@ -324,7 +361,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_005, TestSize.Level0) CreateEventData(bundle, formId, callingUid, actionType, eventData); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); GTEST_LOG_(INFO) << "fms_form_sys_event_receiver_test_005 end"; } @@ -372,7 +412,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_006, TestSize.Level0) ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); @@ -407,9 +450,12 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_007, TestSize.Level0) ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); - ASSERT_FALSE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); + ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); ClearFormRecord(formId); @@ -458,7 +504,10 @@ HWTEST_F(FmsFormSysEventReceiverTest, OnReceiveEvent_008, TestSize.Level0) ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); FormSysEventReceiver testCase; + auto handler = std::make_shared(EventRunner::Create()); + testCase.SetEventHandler(handler); testCase.OnReceiveEvent(eventData); + WaitUntilTaskDone(handler); ASSERT_TRUE(FormDataMgr::GetInstance().GetFormRecord(formId, tempFormRecord)); diff --git a/services/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp b/services/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp index 67567f3b81..ba072afe96 100644 --- a/services/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp +++ b/services/test/moduletest/common/ams/app_service_flow_test/ams_app_service_flow_module_test.cpp @@ -693,7 +693,7 @@ HWTEST_F(AmsAppServiceFlowModuleTest, ServiceFlow_ScreenOnAndOff_001, TestSize.L ApplicationState::APP_STATE_BACKGROUND); EXPECT_CALL(*(testAppA.mockAppScheduler_), ScheduleForegroundApplication()).Times(CYCLE_NUMBER + 1); - EXPECT_CALL(*(testAppA.mockAppScheduler_), ScheduleBackgroundApplication()).Times(CYCLE_NUMBER + 1); + EXPECT_CALL(*(testAppA.mockAppScheduler_), ScheduleBackgroundApplication()).Times(CYCLE_NUMBER + 2); for (uint32_t i = 0; i < CYCLE_NUMBER; i++) { // simulate press ScreenOn key diff --git a/services/test/moduletest/start_option_display_id_test/BUILD.gn b/services/test/moduletest/start_option_display_id_test/BUILD.gn index 0b62e642cd..ec8b519200 100644 --- a/services/test/moduletest/start_option_display_id_test/BUILD.gn +++ b/services/test/moduletest/start_option_display_id_test/BUILD.gn @@ -16,7 +16,7 @@ import("//foundation/aafwk/standard/aafwk.gni") module_output_path = "ability_runtime/mstabilitymgrservice" -ohos_moduletest("start_option_display_id_test") { +ohos_moduletest("start_option_module_test") { module_out_path = module_output_path include_dirs = [ @@ -65,5 +65,5 @@ ohos_moduletest("start_option_display_id_test") { group("moduletest") { testonly = true - deps = [ ":start_option_display_id_test" ] + deps = [ ":start_option_module_test" ] } diff --git a/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp b/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp index 6e1fec052b..5a3508b610 100644 --- a/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp +++ b/services/test/moduletest/start_option_display_id_test/start_option_display_id_test.cpp @@ -74,6 +74,8 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + void OnStartAms(); + void OnStopAms(); public: std::shared_ptr abilityMgrServ_ {nullptr}; @@ -93,10 +95,7 @@ void StartOptionDisplayIdTest::TearDownTestCase() void StartOptionDisplayIdTest::SetUp() { abilityMgrServ_ = OHOS::DelayedSingleton::GetInstance(); - abilityMgrServ_->OnStart(); - WaitUntilTaskFinished(); - - abilityMgrServ_->StartUser(USER_ID_U100); + OnStartAms(); auto missionListMgr = abilityMgrServ_->GetListManagerByUserId(USER_ID_U100); if (!missionListMgr) { return; @@ -110,10 +109,59 @@ void StartOptionDisplayIdTest::SetUp() void StartOptionDisplayIdTest::TearDown() { - abilityMgrServ_->OnStop(); + OnStopAms(); OHOS::DelayedSingleton::DestroyInstance(); } +void StartOptionDisplayIdTest::OnStartAms() +{ + if (abilityMgrServ_) { + if (abilityMgrServ_->state_ == ServiceRunningState::STATE_RUNNING) { + return; + } + + abilityMgrServ_->state_ = ServiceRunningState::STATE_RUNNING; + + abilityMgrServ_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE); + EXPECT_TRUE(abilityMgrServ_->eventLoop_); + + abilityMgrServ_->handler_ = std::make_shared(abilityMgrServ_->eventLoop_, abilityMgrServ_); + EXPECT_TRUE(abilityMgrServ_->handler_); + + // init user controller. + abilityMgrServ_->userController_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->userController_); + abilityMgrServ_->userController_->Init(); + int userId = USER_ID_U100; + abilityMgrServ_->userController_->SetCurrentUserId(userId); + abilityMgrServ_->InitConnectManager(userId, true); + abilityMgrServ_->InitDataAbilityManager(userId, true); + abilityMgrServ_->InitPendWantManager(userId, true); + abilityMgrServ_->systemDataAbilityManager_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->systemDataAbilityManager_); + + abilityMgrServ_->amsConfigResolver_ = std::make_shared(); + EXPECT_TRUE(abilityMgrServ_->amsConfigResolver_); + abilityMgrServ_->amsConfigResolver_->Parse(); + + abilityMgrServ_->InitMissionListManager(userId, true); + abilityMgrServ_->connectManager_->SetEventHandler(abilityMgrServ_->handler_); + abilityMgrServ_->eventLoop_->Run(); + + WaitUntilTaskFinished(); + return; + } + + GTEST_LOG_(INFO) << "OnStart fail"; +} + +void StartOptionDisplayIdTest::OnStopAms() +{ + abilityMgrServ_->eventLoop_.reset(); + abilityMgrServ_->handler_.reset(); + abilityMgrServ_->state_ = ServiceRunningState::STATE_NOT_START; +} + /* * Feature: AbilityManagerService * Function: StartAbility diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 622c8a289e..d105ba55cc 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -21,7 +21,8 @@ group("fuzztest") { "finishusertest_fuzzer:fuzztest", "getwantsender_fuzzer:fuzztest", "setmissionlabel_fuzzer:fuzztest", - "startability_fuzzer:fuzztest", + + #"startability_fuzzer:fuzztest", "startcontinuation_fuzzer:fuzztest", "stopserviceability_fuzzer:fuzztest", "updateconfiguration_fuzzer:fuzztest",