diff --git a/frameworks/native/ability/native/ability.cpp b/frameworks/native/ability/native/ability.cpp index bee1475c9c..0ce7d7db01 100644 --- a/frameworks/native/ability/native/ability.cpp +++ b/frameworks/native/ability/native/ability.cpp @@ -953,7 +953,7 @@ void Ability::DispatchLifecycleOnForeground(const Want &want) HILOG_ERROR("Ability::OnForeground error. abilityLifecycleExecutor_ == nullptr."); return; } - if (abilityInfo_->isStageBasedModel) { + if (abilityInfo_ != nullptr && abilityInfo_->isStageBasedModel) { abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::FOREGROUND_NEW); } else { abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE); diff --git a/test/mock/frameworks_kits_ability_native_test/include/mock_bundle_manager.h b/test/mock/frameworks_kits_ability_native_test/include/mock_bundle_manager.h index 23a6f58a65..58a82376a5 100644 --- a/test/mock/frameworks_kits_ability_native_test/include/mock_bundle_manager.h +++ b/test/mock/frameworks_kits_ability_native_test/include/mock_bundle_manager.h @@ -70,6 +70,13 @@ public: bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override { + if (bundleName == "test_contextImpl") { + bundleInfo.name = "test_contextImpl"; + bundleInfo.applicationInfo.name = "test_contextImpl"; + HapModuleInfo moduleInfo1; + moduleInfo1.moduleName = "test_moduleName"; + bundleInfo.hapModuleInfos.push_back(moduleInfo1); + } return true; } std::string GetAppType(const std::string &bundleName) override; diff --git a/test/moduletest/ability_test/BUILD.gn b/test/moduletest/ability_test/BUILD.gn index 5ad37da8bc..bd3a3afc71 100644 --- a/test/moduletest/ability_test/BUILD.gn +++ b/test/moduletest/ability_test/BUILD.gn @@ -199,6 +199,7 @@ ohos_moduletest("data_ability_operation_moduletest") { "ability_base:configuration", "ability_base:want", "ability_base:zuri", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", diff --git a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn index f93dad9fff..a16f61d2f3 100644 --- a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn @@ -86,6 +86,7 @@ ohos_unittest("ability_test") { "${ability_runtime_native_path}/appkit:app_context", "${distributedschedule_path}/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", "${global_path}/resource_management/frameworks/resmgr:global_resmgr", + "${multimodalinput_path}/frameworks/proxy:libmmi-common", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] diff --git a/test/unittest/frameworks_kits_ability_native_test/ability_test.cpp b/test/unittest/frameworks_kits_ability_native_test/ability_test.cpp index 2d8f004778..bc15bf2b12 100644 --- a/test/unittest/frameworks_kits_ability_native_test/ability_test.cpp +++ b/test/unittest/frameworks_kits_ability_native_test/ability_test.cpp @@ -34,6 +34,7 @@ #include "data_ability_predicates.h" #include "data_ability_result.h" #include "hilog_wrapper.h" +#include "key_event.h" #include "mock_page_ability.h" #include "ohos_application.h" #include "runtime.h" @@ -181,6 +182,50 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_Dump_0100, Function | MediumTest | Level GTEST_LOG_(INFO) << "AaFwk_Ability_Dump_0100 end"; } +/** + * @tc.name: AaFwk_Ability_Dump_0200 + * @tc.desc: Ability Dump basic test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_Dump_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // ability info, lifecycle and lifecycle executor is nullptr + std::string extra = ""; + ability->Dump(extra); + + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->package = "test_Dump"; + abilityInfo->name = "test_Dump"; + abilityInfo->label = "label"; + abilityInfo->description = "test dump"; + abilityInfo->iconPath = "/index/icon"; + abilityInfo->visible = true; + abilityInfo->kind = "kind"; + abilityInfo->type = AbilityType::SERVICE; + abilityInfo->orientation = DisplayOrientation::LANDSCAPE; + abilityInfo->launchMode = LaunchMode::SINGLETON; + abilityInfo->permissions.push_back("ohos.Permission.TestPermission1"); + abilityInfo->permissions.push_back("ohos.Permission.TestPermission2"); + abilityInfo->bundleName = "bundleName"; + abilityInfo->applicationName = "applicationName"; + + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + auto eventRunner = EventRunner::Create(abilityInfo->name); + auto handler = std::make_shared(eventRunner); + sptr token = nullptr; + ability->Init(abilityInfo, application, handler, token); + ability->Dump(extra); + + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnNewWant_0100 * @tc.name: OnNewWant @@ -478,6 +523,68 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnStart_0200, Function | MediumTest | Le GTEST_LOG_(INFO) << "AaFwk_Ability_OnStart_0200 end"; } +/** + * @tc.name: AaFwk_Ability_OnStart_0300 + * @tc.desc: Ability OnStart test when configuration is not nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnStart_0300, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->name = "test_OnStart"; + abilityInfo->type = AbilityType::PAGE; + abilityInfo->isStageBasedModel = true; + + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + Configuration config; + application->SetConfiguration(config); + + auto eventRunner = EventRunner::Create(abilityInfo->name); + auto handler = std::make_shared(eventRunner); + sptr token = nullptr; + ability->Init(abilityInfo, application, handler, token); + + Want want; + ability->OnStart(want); + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: AaFwk_Ability_OnStart_0400 + * @tc.desc: Ability OnStart test when ability lifecycle executor or lifecycle is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnStart_0400, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->name = "test_OnStart"; + abilityInfo->type = AbilityType::PAGE; + ability->abilityInfo_ = abilityInfo; + + Want want; + // branch when lifecycle executor is nullptr + ability->OnStart(want); + + // branch when lifecycle is nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnStart(want); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnStop_0100 * @tc.name: OnStop @@ -527,6 +634,66 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnStop_0200, Function | MediumTest | Lev GTEST_LOG_(INFO) << "AaFwk_Ability_OnStop_0200 end"; } +/** + * @tc.name: AaFwk_Ability_OnStop_0300 + * @tc.desc: Ability OnStop test when ability recovery, window is not nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnStop_0300, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // ability recovery is not nullptr + auto abilityRecovery = std::make_shared(); + EXPECT_NE(abilityRecovery, nullptr); + ability->EnableAbilityRecovery(abilityRecovery); + ability->OnStop(); + + // window is not nullptr + int32_t displayId = 0; + sptr option = new Rosen::WindowOption(); + ability->InitWindow(displayId, option); + ability->OnStop(); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnStop(); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: DestroyInstance_0100 + * @tc.desc: Ability DestroyInstance test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, DestroyInstance_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->name = "test_DestroyInstance"; + abilityInfo->type = AbilityType::PAGE; + abilityInfo->isStageBasedModel = false; + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + auto eventRunner = EventRunner::Create(abilityInfo->name); + auto handler = std::make_shared(eventRunner); + sptr token = nullptr; + ability->Init(abilityInfo, application, handler, token); + + ability->DestroyInstance(); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnActive_0100 * @tc.name: OnActive @@ -576,6 +743,25 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnActive_0200, Function | MediumTest | L GTEST_LOG_(INFO) << "AaFwk_Ability_OnActive_0200 end"; } +/** + * @tc.name: AaFwk_Ability_OnActive_0300 + * @tc.desc: Ability OnActive test when lifecycle is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnActive_0300, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnActive(); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnInactive_0100 * @tc.name: OnInactive @@ -625,6 +811,25 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnInactive_0200, Function | MediumTest | GTEST_LOG_(INFO) << "AaFwk_Ability_OnInactive_0200 end"; } +/** + * @tc.name: AaFwk_Ability_OnInactive_0300 + * @tc.desc: Ability OnActive test when lifecycle is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnInactive_0300, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnInactive(); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnForeground_0100 * @tc.name: OnForeground @@ -790,6 +995,43 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnBackground_0300, Function | MediumTest GTEST_LOG_(INFO) << "AaFwk_Ability_OBackground_0300 end"; } +/** + * @tc.name: AaFwk_Ability_OnBackground_0400 + * @tc.desc: Ability OnBackground basic test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnBackground_0400, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // ability info is nullptr + ability->OnBackground(); + + // stage mode, scene is not nullptr + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->name = "test_OnStart"; + abilityInfo->type = AbilityType::PAGE; + abilityInfo->isStageBasedModel = true; + + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + auto eventRunner = EventRunner::Create(abilityInfo->name); + auto handler = std::make_shared(eventRunner); + sptr token = nullptr; + ability->Init(abilityInfo, application, handler, token); + + int32_t displayId = 0; + sptr option = new Rosen::WindowOption(); + ability->InitWindow(displayId, option); + + ability->OnBackground(); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnConnect_0100 * @tc.name: OnConnect @@ -817,6 +1059,29 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnConnect_0100, Function | MediumTest | GTEST_LOG_(INFO) << "AaFwk_Ability_OnConnect_0100 end"; } +/** + * @tc.name: AaFwk_Ability_OnConnect_0200 + * @tc.desc: Ability OnConnect test when lifecycle is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnConnect_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // lifecycle executor is nullptr + Want want; + ability->OnConnect(want); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnConnect(want); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnCommond_0100 * @tc.name: OnCommand @@ -844,6 +1109,32 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnCommond_0100, Function | MediumTest | GTEST_LOG_(INFO) << "AaFwk_Ability_OnCommond_0100 end"; } +/** + * @tc.name: AaFwk_Ability_OnCommand_0200 + * @tc.desc: Ability OnCommand test when lifecycle is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnCommand_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + Want want; + bool restart = false; + int startId = 0; + + // lifecycle executor is nullptr + ability->OnCommand(want, restart, startId); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->OnCommand(want, restart, startId); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AaFwk_Ability_OnDisconnect_0100 * @tc.name: OnDisconnect @@ -962,6 +1253,50 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_ExecuteBatch_0100, Function | MediumTest GTEST_LOG_(INFO) << "AaFwk_Ability_ExecuteBatch_0100 end"; } +/** + * @tc.name: AaFwk_Ability_ExecuteBatch_0200 + * @tc.desc: Ability ExecuteBatch basic test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_ExecuteBatch_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + std::shared_ptr uri = std::make_shared("dataability:///com.ohos.test"); + std::shared_ptr operation = DataAbilityOperation::NewUpdateBuilder(uri)->Build();; + std::vector> executeBatchOperations; + executeBatchOperations.push_back(operation); + + // ability info is nullptr + auto result = ability->ExecuteBatch(executeBatchOperations); + + auto abilityInfo = std::make_shared(); + EXPECT_NE(abilityInfo, nullptr); + abilityInfo->name = "test_ExecuteOperation"; + abilityInfo->type = AbilityType::PAGE; // not DATA + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + auto eventRunner = EventRunner::Create(abilityInfo->name); + auto handler = std::make_shared(eventRunner); + sptr token = nullptr; + ability->Init(abilityInfo, application, handler, token); + + // type is not DATA + result = ability->ExecuteBatch(executeBatchOperations); + ability->ExecuteOperation(operation, result, -1); + + abilityInfo->type = AbilityType::DATA; + ability->Init(abilityInfo, application, handler, token); + ability->ExecuteOperation(operation, result, 0); + + std::shared_ptr nullOperation = nullptr; + ability->ExecuteOperation(nullOperation, result, 0); + HILOG_INFO("%{public}s end.", __func__); +} + class AbilityTest final : public Ability { public: AbilityTest() {} @@ -995,6 +1330,23 @@ HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnBackPressed_0100, Function | MediumTes GTEST_LOG_(INFO) << "AaFwk_Ability_OnBackPressed_0100 end"; } +/** + * @tc.name: AaFwk_Ability_OnBackPressed_0200 + * @tc.desc: Ability OnBackPressed test when ability info is nullptr. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AaFwk_Ability_OnBackPressed_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + ability->OnBackPressed(); + + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.name: AbilityCreate_0100 * @tc.desc: Ability create test. @@ -1059,6 +1411,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // branch when abilityContext_ is nullptr auto ret = ability->IsRestoredInContinuation(); @@ -1094,6 +1447,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0200, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // branch when abilityRecovery_ is nullptr Want want; @@ -1140,6 +1494,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0300, TestSize.Level1) HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); std::shared_ptr pageAbilityInfo = std::make_shared(); pageAbilityInfo->type = AppExecFwk::AbilityType::PAGE; auto eventRunner = EventRunner::Create(pageAbilityInfo->name); @@ -1164,6 +1519,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0400, TestSize.Level1) HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); auto state = ability->GetContinuationState(); EXPECT_EQ(state, ContinuationState::LOCAL_RUNNING); @@ -1192,6 +1548,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0500, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // branch when abilityInfo_ is nullptr auto regMgr = ability->GetContinuationRegisterManager(); @@ -1242,6 +1599,7 @@ HWTEST_F(AbilityBaseTest, AbilityContinuation_0600, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Want want; want.SetFlags(Want::FLAG_ABILITY_CONTINUATION); @@ -1282,20 +1640,53 @@ HWTEST_F(AbilityBaseTest, AbilityStartAbilityForResult_0100, TestSize.Level1) // branch when abilityInfo_ is nullptr std::shared_ptr ability = std::make_shared(); - ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + ASSERT_NE(ability, nullptr); + auto ret = ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + EXPECT_EQ(ret, ERR_NULL_OBJECT); // branch when type is not PAGE std::shared_ptr handler = nullptr; std::shared_ptr serviceAbilityInfo = std::make_shared(); serviceAbilityInfo->type = AppExecFwk::AbilityType::SERVICE; ability->Init(serviceAbilityInfo, nullptr, handler, nullptr); - ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + ret = ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + EXPECT_EQ(ret, ERR_INVALID_VALUE); // branch when type is PAGE std::shared_ptr pageAbilityInfo = std::make_shared(); pageAbilityInfo->type = AppExecFwk::AbilityType::PAGE; ability->Init(pageAbilityInfo, nullptr, handler, nullptr); - ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + ret = ability->StartAbilityForResult(want, requestCode, abilityStartSetting); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: AbilityStartAbility_0100 + * @tc.desc: Ability StartAbility test when type is not PAGE ans SERVICE. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, AbilityStartAbility_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + Want want; + std::string bundleName = "bundleName"; + std::string abilityName = "abilityName"; + want.SetElementName(bundleName, abilityName); + AbilityStartSetting abilityStartSetting; + + // branch when type is not PAGE + std::shared_ptr handler = nullptr; + std::shared_ptr abilityInfo = std::make_shared(); + abilityInfo->type = AppExecFwk::AbilityType::DATA; + ability->Init(abilityInfo, nullptr, handler, nullptr); + auto ret = ability->StartAbility(want, abilityStartSetting); + EXPECT_EQ(ret, ERR_INVALID_VALUE); HILOG_INFO("%{public}s end.", __func__); } @@ -1310,6 +1701,7 @@ HWTEST_F(AbilityBaseTest, AbilityGetType_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Uri uri("test_get_type"); auto type = ability->GetType(uri); @@ -1333,6 +1725,7 @@ HWTEST_F(AbilityBaseTest, AbilityInsert_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Uri uri("test_insert"); NativeRdb::ValuesBucket value; @@ -1365,6 +1758,7 @@ HWTEST_F(AbilityBaseTest, AbilityCall_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Uri uri("test_call"); std::string method; @@ -1379,6 +1773,58 @@ HWTEST_F(AbilityBaseTest, AbilityCall_0100, TestSize.Level1) HILOG_INFO("%{public}s end.", __func__); } +/** + * @tc.name: InitConfigurationProperties_0100 + * @tc.desc: Ability InitConfigurationProperties test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, InitConfigurationProperties_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + Configuration config; + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "en"); + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, "dark"); + config.AddItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE, "true"); + std::string language; + std::string colormode; + std::string hasPointerDevice; + ability->InitConfigurationProperties(config, language, colormode, hasPointerDevice); + EXPECT_EQ(language, "en"); + EXPECT_EQ(colormode, "dark"); + EXPECT_EQ(hasPointerDevice, "true"); + + // branch when setting is not nullptr + auto setting = std::make_shared(); + ability->SetStartAbilitySetting(setting); + ability->InitConfigurationProperties(config, language, colormode, hasPointerDevice); + EXPECT_EQ(language, "en"); + EXPECT_EQ(colormode, "dark"); + EXPECT_EQ(hasPointerDevice, "true"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: OnKeyUp_0100 + * @tc.desc: Ability OnKeyUp test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, OnKeyUp_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + auto keyEvent = std::make_shared(MMI::KeyEvent::KEYCODE_BACK); + ability->OnKeyUp(keyEvent); + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.name: AbilityOnMemoryLevel_0100 * @tc.desc: Ability OnMemoryLevel test. @@ -1389,6 +1835,7 @@ HWTEST_F(AbilityBaseTest, AbilityOnMemoryLevel_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); int level = 0; ability->OnMemoryLevel(level); @@ -1396,6 +1843,9 @@ HWTEST_F(AbilityBaseTest, AbilityOnMemoryLevel_0100, TestSize.Level1) ability->scene_ = std::make_shared(); ability->OnMemoryLevel(level); + auto contentInfo = ability->GetContentInfo(); + EXPECT_EQ(contentInfo, ""); + HILOG_INFO("%{public}s end.", __func__); } @@ -1409,6 +1859,7 @@ HWTEST_F(AbilityBaseTest, AbilityOpenRawFile_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Uri uri("test_open_file"); std::string mode; @@ -1431,6 +1882,7 @@ HWTEST_F(AbilityBaseTest, AbilityVirtualFunc_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Configuration configuration; ability->OnConfigurationUpdated(configuration); @@ -1466,6 +1918,7 @@ HWTEST_F(AbilityBaseTest, AbilityVirtualFunc_0200, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); bool ret = ability->OnStartContinuation(); EXPECT_EQ(ret, false); @@ -1497,6 +1950,30 @@ HWTEST_F(AbilityBaseTest, AbilityVirtualFunc_0200, TestSize.Level1) HILOG_INFO("%{public}s end.", __func__); } +/** + * @tc.name: DispatchLifecycleOnForeground_0200 + * @tc.desc: Ability DispatchLifecycleOnForeground test. + * @tc.type: FUNC + * @tc.require: issueI60B7N + */ +HWTEST_F(AbilityBaseTest, DispatchLifecycleOnForeground_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // lifecycle executor is nullptr + Want want; + ability->DispatchLifecycleOnForeground(want); + + // lifecycle is nullptr and lifecycle executor is not nullptr + auto lifecycleExecutor = std::make_shared(); + ability->abilityLifecycleExecutor_ = lifecycleExecutor; + ability->DispatchLifecycleOnForeground(want); + + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.name: AbilityBackgroundRunning_0100 * @tc.desc: Ability function test, including StopBackgroundRunning and StartBackgroundRunning @@ -1507,6 +1984,11 @@ HWTEST_F(AbilityBaseTest, AbilityBackgroundRunning_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); + + // branch when ability info is nullptr + AbilityRuntime::WantAgent::WantAgent wantAgent; + ability->StartBackgroundRunning(wantAgent); std::shared_ptr pageAbilityInfo = std::make_shared(); pageAbilityInfo->type = AppExecFwk::AbilityType::PAGE; @@ -1516,8 +1998,6 @@ HWTEST_F(AbilityBaseTest, AbilityBackgroundRunning_0100, TestSize.Level1) auto bundleMgr = ability->GetBundleMgr(); ability->SetBundleManager(bundleMgr); - - AbilityRuntime::WantAgent::WantAgent wantAgent; ability->StartBackgroundRunning(wantAgent); int id = 0; @@ -1541,6 +2021,7 @@ HWTEST_F(AbilityBaseTest, AbilityParseValuesBucketReference_0100, TestSize.Level { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); std::vector> results; std::shared_ptr operation = nullptr; @@ -1565,6 +2046,7 @@ HWTEST_F(AbilityBaseTest, AbilityChangeRef2Value_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // index larger than or equal to numRefs std::vector> results; @@ -1609,6 +2091,7 @@ HWTEST_F(AbilityBaseTest, AbilityCheckAssertQueryResult_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // queryResult is nullptr std::shared_ptr queryResult = nullptr; @@ -1638,6 +2121,7 @@ HWTEST_F(AbilityBaseTest, AbilityStartFeatureAbilityForResult_0100, TestSize.Lev { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); Want want; int requestCode = 0; @@ -1647,6 +2131,9 @@ HWTEST_F(AbilityBaseTest, AbilityStartFeatureAbilityForResult_0100, TestSize.Lev auto ret = ability->StartFeatureAbilityForResult(want, requestCode, std::move(task)); EXPECT_EQ(ret, ERR_NULL_OBJECT); + int resultCode = 0; + ability->OnFeatureAbilityResult(requestCode, resultCode, want); + HILOG_INFO("%{public}s end.", __func__); } @@ -1660,6 +2147,7 @@ HWTEST_F(AbilityBaseTest, AbilityFuncList_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); auto object = ability->CallRequest(); EXPECT_EQ(object, nullptr); @@ -1692,6 +2180,7 @@ HWTEST_F(AbilityBaseTest, AbilityFuncList_0200, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); ability->OnLeaveForeground(); @@ -1716,6 +2205,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetShowOnLockScreen_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); ability->SetShowOnLockScreen(true); ability->SetShowOnLockScreen(false); @@ -1746,6 +2236,7 @@ HWTEST_F(AbilityBaseTest, AbilityScene_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); ability->OnSceneCreated(); ability->OnSceneRestored(); @@ -1767,6 +2258,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetUIContent_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); ComponentContainer componentContainer; ability->SetUIContent(componentContainer); @@ -1786,6 +2278,7 @@ HWTEST_F(AbilityBaseTest, AbilityFormFunction_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); int64_t formId = 0; ability->OnUpdate(formId); @@ -1810,6 +2303,7 @@ HWTEST_F(AbilityBaseTest, AbilityGetCurrentWindowMode_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // scene_ is nullptr int windowMode = ability->GetCurrentWindowMode(); @@ -1819,6 +2313,12 @@ HWTEST_F(AbilityBaseTest, AbilityGetCurrentWindowMode_0100, TestSize.Level1) windowMode = ability->GetCurrentWindowMode(); EXPECT_EQ(windowMode, static_cast(Rosen::WindowMode::WINDOW_MODE_UNDEFINED)); + int32_t displayId = 0; + sptr option = new Rosen::WindowOption(); + ability->InitWindow(displayId, option); + windowMode = ability->GetCurrentWindowMode(); + EXPECT_EQ(windowMode, static_cast(Rosen::WindowMode::WINDOW_MODE_UNDEFINED)); + HILOG_INFO("%{public}s end.", __func__); } @@ -1832,6 +2332,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetMissionLabel_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); std::string label = "test_label"; auto ret = ability->SetMissionLabel(label); @@ -1856,6 +2357,12 @@ HWTEST_F(AbilityBaseTest, AbilitySetMissionLabel_0100, TestSize.Level1) ret = ability->SetMissionLabel(label); EXPECT_EQ(ret, -1); + int32_t displayId = 0; + sptr option = new Rosen::WindowOption(); + ability->InitWindow(displayId, option); + ret = ability->SetMissionLabel(label); + EXPECT_EQ(ret, -1); + // fa mode pageAbilityInfo->isStageBasedModel = false; ability->Init(pageAbilityInfo, nullptr, handler, nullptr); @@ -1875,6 +2382,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetMissionIcon_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); auto icon = std::make_shared(); auto ret = ability->SetMissionIcon(icon); @@ -1899,6 +2407,12 @@ HWTEST_F(AbilityBaseTest, AbilitySetMissionIcon_0100, TestSize.Level1) ret = ability->SetMissionIcon(icon); EXPECT_EQ(ret, -1); + int32_t displayId = 0; + sptr option = new Rosen::WindowOption(); + ability->InitWindow(displayId, option); + ret = ability->SetMissionIcon(icon); + EXPECT_EQ(ret, -1); + // fa mode pageAbilityInfo->isStageBasedModel = false; ability->Init(pageAbilityInfo, nullptr, handler, nullptr); @@ -1918,6 +2432,7 @@ HWTEST_F(AbilityBaseTest, AbilityOnChange_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); std::shared_ptr pageAbilityInfo = std::make_shared(); pageAbilityInfo->type = AppExecFwk::AbilityType::PAGE; @@ -1931,7 +2446,11 @@ HWTEST_F(AbilityBaseTest, AbilityOnChange_0100, TestSize.Level1) ability->OnDestroy(displayId); ability->OnChange(displayId); - auto application = std::shared_ptr(ApplicationLoader::GetInstance().GetApplicationByName()); + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + Configuration config; + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, "dark"); + application->SetConfiguration(config); ability->Init(pageAbilityInfo, application, handler, nullptr); ability->OnChange(displayId); @@ -1948,6 +2467,7 @@ HWTEST_F(AbilityBaseTest, AbilityOnDisplayMove_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); std::shared_ptr pageAbilityInfo = std::make_shared(); pageAbilityInfo->type = AppExecFwk::AbilityType::PAGE; @@ -1960,7 +2480,11 @@ HWTEST_F(AbilityBaseTest, AbilityOnDisplayMove_0100, TestSize.Level1) Rosen::DisplayId toDisplayId = 0; ability->OnDisplayMove(fromDisplayId, toDisplayId); - auto application = std::shared_ptr(ApplicationLoader::GetInstance().GetApplicationByName()); + auto application = std::make_shared(); + EXPECT_NE(application, nullptr); + Configuration config; + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, "dark"); + application->SetConfiguration(config); ability->Init(pageAbilityInfo, application, handler, nullptr); ability->OnDisplayMove(fromDisplayId, toDisplayId); @@ -1977,6 +2501,7 @@ HWTEST_F(AbilityBaseTest, AbilityRequestFocus_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // ability window is nullptr Want want; @@ -2010,6 +2535,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetWakeUpScreen_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // ability window is nullptr bool wakeUp = false; @@ -2043,6 +2569,7 @@ HWTEST_F(AbilityBaseTest, AbilitySetDisplayOrientation_0100, TestSize.Level1) { HILOG_INFO("%{public}s start.", __func__); std::shared_ptr ability = std::make_shared(); + ASSERT_NE(ability, nullptr); // ability window is nullptr int orientation = static_cast(DisplayOrientation::FOLLOWRECENT); diff --git a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn index d04c84396a..71c13199ea 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn @@ -90,6 +90,8 @@ ohos_unittest("application_test") { "${ability_runtime_native_path}/appkit/app/app_context.cpp", "${ability_runtime_native_path}/appkit/app/app_loader.cpp", "${ability_runtime_native_path}/appkit/app/ohos_application.cpp", + "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include/mock_bundle_manager.cpp", + "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include/sys_mgr_client_mock.cpp", "ability_stage_test.cpp", "application_data_manager_test.cpp", "application_test.cpp", diff --git a/test/unittest/frameworks_kits_appkit_native_test/context_impl_test.cpp b/test/unittest/frameworks_kits_appkit_native_test/context_impl_test.cpp index 58c5726f74..1e222777e1 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/context_impl_test.cpp +++ b/test/unittest/frameworks_kits_appkit_native_test/context_impl_test.cpp @@ -20,10 +20,17 @@ #include "context_impl.h" #undef private +#include "ability_constants.h" #include "ability_local_record.h" #include "application_context.h" #include "context.h" +#include "hap_module_info.h" +#include "hilog_wrapper.h" #include "iremote_object.h" +#include "mock_ability_token.h" +#include "mock_bundle_manager.h" +#include "system_ability_definition.h" +#include "sys_mgr_client.h" namespace OHOS { namespace AppExecFwk { @@ -31,6 +38,10 @@ using namespace testing::ext; using namespace OHOS; using namespace OHOS::AppExecFwk; +namespace { +const int64_t CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001); +} // namespace + class ContextImplTest : public testing::Test { public: ContextImplTest() : contextImpl_(nullptr) @@ -53,6 +64,9 @@ void ContextImplTest::TearDownTestCase(void) void ContextImplTest::SetUp(void) { contextImpl_ = std::make_shared(); + sptr bundleObject = new (std::nothrow) BundleMgrService(); + DelayedSingleton::GetInstance()->RegisterSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, + bundleObject); } void ContextImplTest::TearDown(void) @@ -91,6 +105,373 @@ HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_GetBundleName_002, Function | M GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_GetBundleName_002 end"; } +/** + * @tc.name: AppExecFwk_ContextImpl_GetBundleName_003 + * @tc.desc: Get bundle name when parent context is not nullptr. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_GetBundleName_003, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + auto applicationInfo = std::make_shared(); + EXPECT_NE(applicationInfo, nullptr); + applicationInfo->bundleName = "com.test.parentcontext"; + parentContext->SetApplicationInfo(applicationInfo); + + contextImpl->SetParentContext(parentContext); + std::string bundleName = contextImpl->GetBundleName(); + EXPECT_EQ(bundleName, "com.test.parentcontext"); + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetBundleCodeDir_0100 + * @tc.desc: Get bundle code directory. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetBundleCodeDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // branch when application info is nullptr + auto codeDir = contextImpl->GetBundleCodeDir(); + EXPECT_EQ(codeDir, ""); + + // construct application info + auto applicationInfo = std::make_shared(); + EXPECT_NE(applicationInfo, nullptr); + applicationInfo->codePath = "/data/app/el1/bundle/public/testCodeDir"; + contextImpl->SetApplicationInfo(applicationInfo); + + // not create by system app + codeDir = contextImpl->GetBundleCodeDir(); + EXPECT_EQ(codeDir, AbilityRuntime::Constants::LOCAL_CODE_PATH); + + // create by system app(flag is ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP) + contextImpl->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP); + codeDir = contextImpl->GetBundleCodeDir(); + EXPECT_EQ(codeDir, "/data/bundles/testCodeDir"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: IsUpdatingConfigurations_0100 + * @tc.desc: IsUpdatingConfigurations basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, IsUpdatingConfigurations_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto isUpdating = contextImpl->IsUpdatingConfigurations(); + EXPECT_EQ(isUpdating, false); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: PrintDrawnCompleted_0100 + * @tc.desc: PrintDrawnCompleted basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, PrintDrawnCompleted_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto isComplete = contextImpl->PrintDrawnCompleted(); + EXPECT_EQ(isComplete, false); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetDatabaseDir_0100 + * @tc.desc: Get base directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetDatabaseDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // not create by system app and parent context is nullptr + auto databaseDir = contextImpl->GetDatabaseDir(); + EXPECT_EQ(databaseDir, "/data/storage/el2/database"); + + // create by system app and parent context is not nullptr + contextImpl->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP); + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + auto applicationInfo = std::make_shared(); + EXPECT_NE(applicationInfo, nullptr); + applicationInfo->bundleName = "com.test.database"; + parentContext->SetApplicationInfo(applicationInfo); + contextImpl->SetParentContext(parentContext); + databaseDir = contextImpl->GetDatabaseDir(); + EXPECT_EQ(databaseDir, "/data/app/el2/0/database/com.test.database/"); + + // create by system app and hap module info of parent context is not nullptr + AppExecFwk::HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "test_moduleName"; + contextImpl->InitHapModuleInfo(hapModuleInfo); + databaseDir = contextImpl->GetDatabaseDir(); + EXPECT_EQ(databaseDir, "/data/app/el2/0/database/com.test.database/test_moduleName"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetPreferencesDir_0100 + * @tc.desc: Get preference directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetPreferencesDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto preferenceDir = contextImpl->GetPreferencesDir(); + EXPECT_EQ(preferenceDir, "/data/storage/el2/base/preferences"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetTempDir_0100 + * @tc.desc: Get temp directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetTempDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto tempDir = contextImpl->GetTempDir(); + EXPECT_EQ(tempDir, "/data/storage/el2/base/temp"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetFilesDir_0100 + * @tc.desc: Get files directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetFilesDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto filesDir = contextImpl->GetFilesDir(); + EXPECT_EQ(filesDir, "/data/storage/el2/base/files"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetDistributedFilesDir_0100 + * @tc.desc: Get distributed directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetDistributedFilesDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // not create by system app + auto distributedDir = contextImpl->GetDistributedFilesDir(); + EXPECT_EQ(distributedDir, "/data/storage/el2/distributedfiles"); + + // create by system app and bundleName is empty + contextImpl->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP); + distributedDir = contextImpl->GetDistributedFilesDir(); + EXPECT_EQ(distributedDir, "/mnt/hmdfs/0/device_view/local/data/"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetBaseDir_0100 + * @tc.desc: Get base directory basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetBaseDir_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // not create by system app and parent context is nullptr + auto baseDir = contextImpl->GetBaseDir(); + EXPECT_EQ(baseDir, "/data/storage/el2/base"); + + // create by system app and parent context is not nullptr + contextImpl->SetFlags(CONTEXT_CREATE_BY_SYSTEM_APP); + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + auto applicationInfo = std::make_shared(); + EXPECT_NE(applicationInfo, nullptr); + applicationInfo->bundleName = "com.test.base"; + parentContext->SetApplicationInfo(applicationInfo); + contextImpl->SetParentContext(parentContext); + baseDir = contextImpl->GetBaseDir(); + EXPECT_EQ(baseDir, "/data/app/el2/0/base/com.test.base/haps/"); + + // create by system app and hap module info of parent context is not nullptr + AppExecFwk::HapModuleInfo hapModuleInfo; + hapModuleInfo.moduleName = "test_moduleName"; + contextImpl->InitHapModuleInfo(hapModuleInfo); + baseDir = contextImpl->GetBaseDir(); + EXPECT_EQ(baseDir, "/data/app/el2/0/base/com.test.base/haps/test_moduleName"); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: SwitchArea_0100 + * @tc.desc: Switch area basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, SwitchArea_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // invalid mode + contextImpl->SwitchArea(-1); + contextImpl->SwitchArea(2); + + // valid mode + contextImpl->SwitchArea(0); + contextImpl->SwitchArea(1); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetAreaArea_0100 + * @tc.desc: Get area basic test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetAreaArea_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + contextImpl->SwitchArea(0); + auto mode = contextImpl->GetArea(); + EXPECT_EQ(mode, 0); + + contextImpl->SwitchArea(1); + mode = contextImpl->GetArea(); + EXPECT_EQ(mode, 1); + + // invalid area_ + contextImpl->currArea_ = "invalid"; + mode = contextImpl->GetArea(); + EXPECT_EQ(mode, 1); // default is AbilityRuntime::ContextImpl::EL_DEFAULT + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetCurrentAccountId_0100 + * @tc.desc: Get current account id test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetCurrentAccountId_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto accountId = contextImpl->GetCurrentAccountId(); + EXPECT_EQ(accountId, 0); // default account id + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetCurrentActiveAccountId_0100 + * @tc.desc: Get current active account id test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetCurrentActiveAccountId_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto accountId = contextImpl->GetCurrentActiveAccountId(); + EXPECT_EQ(accountId, 100); // default active account id is 100 + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: CreateBundleContext_0100 + * @tc.desc: Create bundle context test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, CreateBundleContext_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // bundle name is empty + auto context = contextImpl->CreateBundleContext(""); + EXPECT_EQ(context, nullptr); + + // bundle name is invalid + context = contextImpl->CreateBundleContext("invalid_bundleName"); + EXPECT_EQ(context, nullptr); + + context = contextImpl->CreateBundleContext("test_contextImpl"); + EXPECT_NE(context, nullptr); + + // parent context is not nullptr + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + contextImpl->SetParentContext(parentContext); + context = contextImpl->CreateBundleContext(""); + EXPECT_EQ(context, nullptr); + + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AppExecFwk_ContextImpl_SetApplicationInfo_001 * @tc.name: SetApplicationInfo @@ -101,6 +482,8 @@ HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_GetBundleName_002, Function | M HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_SetApplicationInfo_001, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_SetApplicationInfo_001 start"; + contextImpl_->SetApplicationInfo(nullptr); + EXPECT_EQ(contextImpl_->GetApplicationInfo(), nullptr); std::shared_ptr applicationInfo = std::make_shared(); contextImpl_->SetApplicationInfo(applicationInfo); EXPECT_NE(contextImpl_->GetApplicationInfo(), nullptr); @@ -132,6 +515,15 @@ HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_GetApplicationContext_001, Func { GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_GetApplicationContext_001 start"; EXPECT_TRUE(contextImpl_->GetApplicationContext() == nullptr); + + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + contextImpl->SetParentContext(parentContext); + EXPECT_EQ(contextImpl->GetApplicationInfo(), nullptr); + GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_GetApplicationContext_001 end"; } @@ -166,19 +558,49 @@ HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_GetHapModuleInfo_001, Function } /** - * @tc.number: AppExecFwk_ContextImpl_CreateModuleContext_001 + * @tc.number: CreateModuleContext_001 * @tc.name: CreateModuleContext * @tc.desc: Test whether CreateModuleContext is called normally. * @tc.type: FUNC * @tc.require: SR000H6I25 */ -HWTEST_F(ContextImplTest, AppExecFwk_ContextImpl_CreateModuleContext_001, Function | MediumTest | Level1) +HWTEST_F(ContextImplTest, CreateModuleContext_001, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_CreateModuleContext_001 start"; EXPECT_EQ(contextImpl_->CreateModuleContext("module_name"), nullptr); GTEST_LOG_(INFO) << "AppExecFwk_ContextImpl_CreateModuleContext_001 end"; } +/** + * @tc.name: CreateModuleContext_002 + * @tc.desc: Create module context test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, CreateModuleContext_002, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // bundleName is valid, but module name is empty + auto moduleContext = contextImpl->CreateModuleContext("test_contextImpl", ""); + EXPECT_EQ(moduleContext, nullptr); + + // bundle name is invalid + moduleContext = contextImpl->CreateModuleContext("invalid_bundleName", "invalid_moduleName"); + EXPECT_EQ(moduleContext, nullptr); + + // module didn't exist + moduleContext = contextImpl->CreateModuleContext("test_contextImpl", "invalid_moduleName"); + EXPECT_EQ(moduleContext, nullptr); + + moduleContext = contextImpl->CreateModuleContext("test_contextImpl", "test_moduleName"); + EXPECT_NE(moduleContext, nullptr); + + HILOG_INFO("%{public}s end.", __func__); +} + /** * @tc.number: AppExecFwk_AppContext_RegisterAbilityLifecycleCallback_001 * @tc.name: RegisterAbilityLifecycleCallback @@ -317,7 +739,7 @@ HWTEST_F(ContextImplTest, AppExecFwk_AppContext_InitResourceManager_003, Functio bundleInfo.hapModuleInfos.push_back(info); contextImpl_->InitResourceManager(bundleInfo, appContext, true, "entry"); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); - + contextImpl_->InitResourceManager(bundleInfo, appContext, true, ""); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); @@ -326,7 +748,7 @@ HWTEST_F(ContextImplTest, AppExecFwk_AppContext_InitResourceManager_003, Functio bundleInfo.hapModuleInfos.push_back(info); contextImpl_->InitResourceManager(bundleInfo, appContext, true, "entry"); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); - + contextImpl_->InitResourceManager(bundleInfo, appContext, true, ""); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); @@ -360,7 +782,7 @@ HWTEST_F(ContextImplTest, AppExecFwk_AppContext_InitResourceManager_004, Functio bundleInfo.hapModuleInfos.push_back(info); contextImpl_->InitResourceManager(bundleInfo, appContext, true, "entry"); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); - + contextImpl_->InitResourceManager(bundleInfo, appContext, false, "entry"); EXPECT_TRUE(appContext->GetResourceManager() != nullptr); @@ -372,5 +794,156 @@ HWTEST_F(ContextImplTest, AppExecFwk_AppContext_InitResourceManager_004, Functio GTEST_LOG_(INFO) << "AppExecFwk_AppContext_InitResourceManager_004 end"; } + +/** + * @tc.name: AppExecFwk_AppContext_InitResourceManager_005 + * @tc.desc: abnornal branch test for InitResourceManager. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, AppExecFwk_AppContext_InitResourceManager_005, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // branch when appContext is nullptr + AppExecFwk::BundleInfo bundleInfo; + contextImpl->InitResourceManager(bundleInfo, nullptr, true, ""); + + // parent context is not nullptr + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + contextImpl->SetParentContext(parentContext); + EXPECT_EQ(contextImpl->GetResourceManager(), nullptr); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetBundleCodePath_0100 + * @tc.desc: Get bundle code path test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetBundleCodePath_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto codePath = contextImpl->GetBundleCodePath(); + EXPECT_EQ(codePath, ""); + + // construt application info + auto applicationInfo = std::make_shared(); + EXPECT_NE(applicationInfo, nullptr); + applicationInfo->codePath = "/data/app/el1"; + contextImpl->SetApplicationInfo(applicationInfo); + EXPECT_EQ(contextImpl->GetBundleCodePath(), "/data/app/el1"); + + // parent context is not nullptr + auto parentContext = std::make_shared(); + EXPECT_NE(parentContext, nullptr); + contextImpl->SetParentContext(parentContext); + EXPECT_EQ(contextImpl->GetBundleCodePath(), ""); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: InitHapModuleInfo_0100 + * @tc.desc: Init hap module info test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, InitHapModuleInfo_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + AppExecFwk::HapModuleInfo hapModuleInfo; + contextImpl->InitHapModuleInfo(hapModuleInfo); + EXPECT_NE(contextImpl->GetHapModuleInfo(), nullptr); + + // branch when hap module info has been assigned + auto abilityInfo = std::make_shared(); + contextImpl->InitHapModuleInfo(abilityInfo); + EXPECT_NE(contextImpl->GetHapModuleInfo(), nullptr); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: InitHapModuleInfo_0200 + * @tc.desc: Init hap module info test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, InitHapModuleInfo_0200, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + auto abilityInfo = std::make_shared(); + contextImpl->InitHapModuleInfo(nullptr); + contextImpl->InitHapModuleInfo(abilityInfo); + EXPECT_NE(contextImpl->GetHapModuleInfo(), nullptr); + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: SetToken_0100 + * @tc.desc: set token and get token test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, SetToken_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + contextImpl->SetToken(nullptr); + sptr token = new (std::nothrow) MockAbilityToken(); + contextImpl->SetToken(token); + auto after = contextImpl->GetToken(); + EXPECT_EQ(token, after); + + HILOG_INFO("%{public}s end.", __func__); +} + +/** + * @tc.name: GetDeviceType_0100 + * @tc.desc: Get device type test. + * @tc.type: FUNC + * @tc.require: issueI61P7Y + */ +HWTEST_F(ContextImplTest, GetDeviceType_0100, TestSize.Level1) +{ + HILOG_INFO("%{public}s start.", __func__); + auto contextImpl = std::make_shared(); + EXPECT_NE(contextImpl, nullptr); + + // branch when config is nullptr + auto deviceType = contextImpl->GetDeviceType(); + EXPECT_EQ(deviceType, Global::Resource::DeviceType::DEVICE_PHONE); + + // get device type again + deviceType = contextImpl->GetDeviceType(); + EXPECT_EQ(deviceType, Global::Resource::DeviceType::DEVICE_PHONE); + + // construct configuration + auto config = std::make_shared(); + EXPECT_NE(config, nullptr); + config->AddItem(AAFwk::GlobalConfigurationKey::DEVICE_TYPE, "phone"); + contextImpl->SetConfiguration(config); + deviceType = contextImpl->GetDeviceType(); + EXPECT_EQ(deviceType, Global::Resource::DeviceType::DEVICE_PHONE); + + HILOG_INFO("%{public}s end.", __func__); +} } // namespace AppExecFwk }