From 67dc76142cda7eea8fc39ee1db57635c4d4029b1 Mon Sep 17 00:00:00 2001 From: jiangzhijun8 Date: Fri, 17 May 2024 17:25:31 +0800 Subject: [PATCH] =?UTF-8?q?TDD=E8=A6=86=E7=9B=96=E7=8E=87=E6=96=B0?= =?UTF-8?q?=E5=A2=9E[ability=5Frecord=5Fmgr=5Ftest]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiangzhijun8 Change-Id: I8fe6c48133391cb4ec3b43fef34bf14ec35e68e9 --- test/unittest/BUILD.gn | 1 + .../unittest/ability_record_mgr_test/BUILD.gn | 61 +++++ .../ability_record_mgr_test.cpp | 230 ++++++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 test/unittest/ability_record_mgr_test/BUILD.gn create mode 100644 test/unittest/ability_record_mgr_test/ability_record_mgr_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c5dde5a563..cde38202d6 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -348,6 +348,7 @@ group("unittest") { "ability_manager_stub_test:unittest", "ability_manager_test:unittest", "ability_record_dump_test:unittest", + "ability_record_mgr_test:unittest", "ability_record_test:unittest", "ability_running_info_test:unittest", "ability_runtime_error_util_test:unittest", diff --git a/test/unittest/ability_record_mgr_test/BUILD.gn b/test/unittest/ability_record_mgr_test/BUILD.gn new file mode 100644 index 0000000000..fd91592f70 --- /dev/null +++ b/test/unittest/ability_record_mgr_test/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/appkit" +config("coverage_flags") { + if (ability_runtime_feature_coverage) { + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} + +ohos_unittest("ability_record_mgr_test") { + module_out_path = module_output_path + + include_dirs = [] + + sources = [ + "${ability_runtime_native_path}/appkit/app/ability_record_mgr.cpp", + "ability_record_mgr_test.cpp", + ] + + configs = [ ":coverage_flags" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ "//third_party/googletest:gmock_main" ] + + external_deps = [ + "ability_runtime:abilitykit_native", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + ] + + if (background_task_mgr_continuous_task_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + } +} + +group("unittest") { + testonly = true + + deps = [ ":ability_record_mgr_test" ] +} diff --git a/test/unittest/ability_record_mgr_test/ability_record_mgr_test.cpp b/test/unittest/ability_record_mgr_test/ability_record_mgr_test.cpp new file mode 100644 index 0000000000..7e37d50cb5 --- /dev/null +++ b/test/unittest/ability_record_mgr_test/ability_record_mgr_test.cpp @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#define private public +#define protected public +#include "ability_record_mgr.h" +#undef private +#undef protected +#include "hilog_tag_wrapper.h" +#include "hilog_wrapper.h" +#include "iremote_broker.h" +#include "iremote_object.h" +#include "iremote_stub.h" +using namespace testing::ext; +using namespace testing; +using namespace OHOS::AppExecFwk; +namespace OHOS { +namespace AAFwk { +class IAbilityMgrToken : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.AbilityMgrToken"); +}; + +class AbilityMgrToken : public IRemoteStub { +public: + AbilityMgrToken() = default; + virtual ~AbilityMgrToken() = default; + + virtual int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) + { + return 0; + } + +private: + DISALLOW_COPY_AND_MOVE(AbilityMgrToken); +}; + +class AbilityRecordMgrTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AbilityRecordMgrTest::SetUpTestCase() +{} + +void AbilityRecordMgrTest::TearDownTestCase() +{} + +void AbilityRecordMgrTest::SetUp() +{} + +void AbilityRecordMgrTest::TearDown() +{} + +/** + * @tc.number: GetToken_0100 + * @tc.name: GetToken + * @tc.desc: GetToken Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, GetEventHandler_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetToken_0100 start"; + auto recordMgr = std::make_shared(); + recordMgr->tokens_ = nullptr; + auto token = recordMgr->GetToken(); + EXPECT_EQ(token, nullptr); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetToken_0100 end"; +} + +/** + * @tc.number: SetToken_0100 + * @tc.name: SetToken + * @tc.desc: SetToken Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, SetToken_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest SetToken_0100 start"; + sptr token = new (std::nothrow) AbilityMgrToken(); + auto recordMgr = std::make_shared(); + recordMgr->SetToken(token); + EXPECT_NE(recordMgr->GetToken(), nullptr); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest SetToken_0100 end"; +} + +/** + * @tc.number: SetEventRunner_0100 + * @tc.name: SetEventRunner + * @tc.desc: SetEventRunner Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, SetEventRunner_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest SetEventRunner_0100 start"; + auto recordMgr = std::make_shared(); + EXPECT_NE(recordMgr, nullptr); + recordMgr->SetEventRunner(nullptr); + + std::shared_ptr runner = EventRunner::GetMainEventRunner(); + EXPECT_NE(runner, nullptr); + recordMgr->tokens_ = nullptr; + recordMgr->SetEventRunner(runner); + + sptr token = new (std::nothrow) AbilityMgrToken(); + recordMgr->SetToken(token); + recordMgr->SetEventRunner(runner); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest SetEventRunner_0100 end"; +} + +/** + * @tc.number: AddAbilityRecord_0100 + * @tc.name: AddAbilityRecord + * @tc.desc: AddAbilityRecord Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, AddAbilityRecord_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest AddAbilityRecord_0100 start"; + auto recordMgr = std::make_shared(); + EXPECT_NE(recordMgr, nullptr); + sptr token = nullptr; + recordMgr->AddAbilityRecord(token, nullptr); + + token = new (std::nothrow) AbilityMgrToken(); + recordMgr->AddAbilityRecord(token, nullptr); + + auto abilityRecord = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityRecord, nullptr); + + recordMgr->AddAbilityRecord(token, abilityRecord); + EXPECT_EQ(recordMgr->abilityRecords_.size(), 1); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest AddAbilityRecord_0100 end"; +} + +/** + * @tc.number: RemoveAbilityRecord_0100 + * @tc.name: RemoveAbilityRecord + * @tc.desc: RemoveAbilityRecord Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, RemoveAbilityRecord_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest RemoveAbilityRecord_0100 start"; + auto recordMgr = std::make_shared(); + EXPECT_NE(recordMgr, nullptr); + sptr token = nullptr; + recordMgr->RemoveAbilityRecord(token); + + token = new (std::nothrow) AbilityMgrToken(); + auto abilityRecord = std::make_shared(nullptr, nullptr); + recordMgr->AddAbilityRecord(token, abilityRecord); + EXPECT_EQ(recordMgr->abilityRecords_.size(), 1); + + recordMgr->RemoveAbilityRecord(token); + EXPECT_EQ(recordMgr->abilityRecords_.size(), 0); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest RemoveAbilityRecord_0100 end"; +} + +/** + * @tc.number: GetRecordCount_0100 + * @tc.name: GetRecordCount + * @tc.desc: GetRecordCount Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, GetRecordCount_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetRecordCount_0100 start"; + auto recordMgr = std::make_shared(); + EXPECT_EQ(recordMgr->GetRecordCount(), 0); + + sptr token = new (std::nothrow) AbilityMgrToken(); + auto abilityRecord = std::make_shared(nullptr, nullptr); + recordMgr->AddAbilityRecord(token, abilityRecord); + EXPECT_EQ(recordMgr->GetRecordCount(), 1); + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetRecordCount_0100 end"; +} + +/** + * @tc.number: GetAbilityItem_0100 + * @tc.name: GetAbilityItem + * @tc.desc: GetAbilityItem Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, GetAbilityItem_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetAbilityItem_0100 start"; + auto recordMgr = std::make_shared(); + sptr token = nullptr; + EXPECT_EQ(recordMgr->GetAbilityItem(token), nullptr); + + token = new (std::nothrow) AbilityMgrToken(); + auto abilityRecord = std::make_shared(nullptr, nullptr); + recordMgr->AddAbilityRecord(token, abilityRecord); + EXPECT_NE(recordMgr->GetAbilityItem(token), nullptr); + + sptr token2 = new (std::nothrow) AbilityMgrToken(); + EXPECT_EQ(recordMgr->GetAbilityItem(token2), nullptr); + + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetAbilityItem_0100 end"; +} + +/** + * @tc.number: GetAllTokens_0100 + * @tc.name: GetAllTokens + * @tc.desc: GetAllTokens Test, return is not nullptr. + */ +HWTEST_F(AbilityRecordMgrTest, GetAllTokens_0100, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetAllTokens_0100 start"; + auto recordMgr = std::make_shared(); + + sptr token = new (std::nothrow) AbilityMgrToken(); + auto abilityRecord = std::make_shared(nullptr, nullptr); + recordMgr->AddAbilityRecord(token, abilityRecord); + EXPECT_EQ(recordMgr->GetAllTokens().size(), 1); + + GTEST_LOG_(INFO) << "AbilityRecordMgrTest GetAllTokens_0100 end"; +} +} +}