Description:migrate wantagent module to aafwk repo
Sig: SIG_ApplicationFramework
Feature or Bugfix:Feature
Binary Source:No

Signed-off-by: liqiang <liqiang121@huawei.com>
Change-Id: Ic88147aeb2ee3b5a07f9e2d2ec6ba322e9468add
This commit is contained in:
liqiang 2022-02-10 11:41:05 +08:00
parent 45ae84c0f4
commit 67d6d3c1ca
61 changed files with 7167 additions and 28 deletions

View File

@ -61,9 +61,28 @@
"//foundation/aafwk/standard/common:common_target",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:appexec"
"//foundation/aafwk/standard/frameworks/kits/appkit:appexec",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits"
],
"inner_kits": [
{
"header": {
"header_base": "//foundation/aafwk/standard/interfaces/innerkits/wantagent/include/",
"header_files": [
"cancel_listener.h",
"completed_callback.h",
"completed_dispatcher.h",
"pending_want.h",
"trigger_info.h",
"want_agent_constant.h",
"want_agent_helper.h",
"want_agent_info.h",
"want_agent_log_wrapper.h",
"want_agent.h"
]
},
"name": "//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits"
},
{
"header": {
"header_base": "//foundation/aafwk/standard/interfaces/innerkits/intent/include/",
@ -168,6 +187,7 @@
"//foundation/aafwk/standard/common/test:moduletest",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/test:unittest",
"//foundation/aafwk/standard/frameworks/kits/appkit/test:moduletest",
"//foundation/aafwk/standard/frameworks/kits/wantagent/test/:unittest",
"//foundation/aafwk/standard/services/appmgr/test:unittest"
]
}

View File

@ -234,19 +234,19 @@ ohos_shared_library("abilitykit_native") {
configs = [ ":ability_config" ]
public_configs = [
":ability_public_config",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits_public_config",
"//foundation/aafwk/standard/frameworks/kits/ability/ability_runtime:ability_context_public_config",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits_public_config",
]
deps = [
":static_subscriber_ipc",
"${INNERKITS_PATH}/base:base",
"${INNERKITS_PATH}/want:want",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard",
"//foundation/aafwk/standard/common/task_dispatcher:task_dispatcher",
"//foundation/aafwk/standard/frameworks/kits/ability/ability_runtime:ability_context_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
"//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager",

View File

@ -1021,7 +1021,7 @@ public:
* @param wantAgent Indicates which ability to start when user click the notification bar.
* @return the method result code, 0 means succeed
*/
virtual int StartBackgroundRunning(const Notification::WantAgent::WantAgent &wantAgent) final;
virtual int StartBackgroundRunning(const AbilityRuntime::WantAgent::WantAgent &wantAgent) final;
/**
* @brief Cancel background running of this ability to free up system memory.

View File

@ -2000,11 +2000,11 @@ bool Ability::DeleteForm(const int64_t formId)
* @param wantAgent Indicates which ability to start when user click the notification bar.
* @return the method result code, 0 means succeed
*/
int Ability::StartBackgroundRunning(const Notification::WantAgent::WantAgent &wantAgent)
int Ability::StartBackgroundRunning(const AbilityRuntime::WantAgent::WantAgent &wantAgent)
{
uint32_t defaultBgMode = 0;
BackgroundTaskMgr::ContinuousTaskParam taskParam = BackgroundTaskMgr::ContinuousTaskParam(false, defaultBgMode,
std::make_shared<Notification::WantAgent::WantAgent>(wantAgent), abilityInfo_->name, GetToken());
std::make_shared<AbilityRuntime::WantAgent::WantAgent>(wantAgent), abilityInfo_->name, GetToken());
return BackgroundTaskMgr::BackgroundTaskMgrHelper::RequestStartBackgroundRunning(taskParam);
}

View File

@ -0,0 +1,82 @@
# Copyright (c) 2021 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")
group("native_wantagent") {
deps = [ ":wantagent_innerkits" ]
}
config("wantagent_innerkits_public_config") {
visibility = [ ":*" ]
include_dirs = [
"//base/global/resmgr_standard/interfaces/innerkits/include",
"//foundation/aafwk/standard/frameworks/kits/ability/native/include",
"//foundation/aafwk/standard/interfaces/innerkits/ability_manager/include",
"//foundation/aafwk/standard/interfaces/innerkits/wantagent/include",
"//foundation/aafwk/standard/services/abilitymgr/include",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager/include/appmgr",
"//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr",
"//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/app/include",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime",
"//third_party/icu/icu4c/source/common",
]
}
ohos_shared_library("wantagent_innerkits") {
include_dirs = [
"//utils/native/base/include",
"//foundation/aafwk/standard/services/common/include",
"//foundation/distributedschedule/safwk/services/safwk/include",
]
sources = [
"src/completed_dispatcher.cpp",
"src/pending_want.cpp",
"src/trigger_info.cpp",
"src/want_agent.cpp",
"src/want_agent_helper.cpp",
"src/want_agent_info.cpp",
"src/want_agent_log_wrapper.cpp",
]
configs = [ "//utils/native/base:utils_config" ]
public_configs = [ ":wantagent_innerkits_public_config" ]
deps = [
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:want",
"ability_runtime:ability_manager",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
if (is_double_framework) {
cflags = [ "-DCONFIG_DUAL_FRAMEWORK" ]
}
subsystem_name = "aafwk"
part_name = "ability_runtime"
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 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 "completed_dispatcher.h"
namespace OHOS::AbilityRuntime::WantAgent {
CompletedDispatcher::CompletedDispatcher(const std::shared_ptr<PendingWant> &pendingWant,
const std::shared_ptr<CompletedCallback> &callback, const std::shared_ptr<AppExecFwk::EventHandler> &handler)
: pendingWant_(pendingWant), callback_(callback), handler_(handler)
{}
void CompletedDispatcher::Send(const int32_t resultCode)
{}
void CompletedDispatcher::PerformReceive(const AAFwk::Want &want, int resultCode, const std::string &data,
const AAFwk::WantParams &extras, bool serialized, bool sticky, int sendingUser)
{
want_ = want;
resultCode_ = resultCode;
resultData_ = data;
resultExtras_ = extras;
if (handler_ == nullptr) {
Run();
}
}
void CompletedDispatcher::Run()
{
if (callback_ != nullptr) {
callback_->OnSendFinished(want_, resultCode_, resultData_, resultExtras_);
}
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,422 @@
/*
* Copyright (c) 2021 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 "pending_want.h"
#include "ability_manager_client.h"
#include "hilog_wrapper.h"
#include "pending_want_record.h"
#include "want_agent_log_wrapper.h"
#include "want_sender_info.h"
using namespace OHOS::AppExecFwk;
using namespace OHOS::AAFwk;
namespace OHOS::AbilityRuntime::WantAgent {
PendingWant::PendingWant(const sptr<AAFwk::IWantSender> &target)
: target_(target), cancelReceiver_(nullptr), whitelistToken_(nullptr)
{}
PendingWant::PendingWant(const sptr<AAFwk::IWantSender> &target, const sptr<IRemoteObject> whitelistToken)
: target_(target), cancelReceiver_(nullptr), whitelistToken_(whitelistToken)
{}
WantAgentConstant::OperationType PendingWant::GetType(const sptr<AAFwk::IWantSender> &target)
{
int32_t operationType = 0;
AbilityManagerClient::GetInstance()->GetPendingWantType(target, operationType);
return (WantAgentConstant::OperationType)operationType;
}
std::shared_ptr<PendingWant> PendingWant::GetAbility(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<Want> &want, unsigned int flags)
{
return GetAbility(context, requestCode, want, flags, nullptr);
}
std::shared_ptr<PendingWant> PendingWant::GetAbility(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, int requestCode,
const std::shared_ptr<Want> &want, unsigned int flags, const std::shared_ptr<WantParams> &options)
{
WANT_AGENT_LOGI("PendingWant::GetAbility begin.");
if (context == nullptr) {
WANT_AGENT_LOGE("PendingWant::GetAbility invalid input param.");
return nullptr;
}
WantsInfo wantsInfo;
wantsInfo.want = *want;
wantsInfo.resolvedTypes = want != nullptr ? want->GetType() : "";
if (options != nullptr) {
wantsInfo.want.SetParams(*options);
}
WantSenderInfo wantSenderInfo;
wantSenderInfo.type = (int32_t)WantAgentConstant::OperationType::START_ABILITY;
wantSenderInfo.allWants.push_back(wantsInfo);
wantSenderInfo.bundleName = context->GetBundleName();
wantSenderInfo.flags = flags;
wantSenderInfo.userId = -1; // -1 : invalid user id
wantSenderInfo.requestCode = requestCode;
sptr<IWantSender> target = AbilityManagerClient::GetInstance()->GetWantSender(wantSenderInfo, nullptr);
WANT_AGENT_LOGI("PendingWant::GetAbility end.");
return std::make_shared<PendingWant>(target);
}
std::shared_ptr<PendingWant> PendingWant::GetAbilities(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, std::vector<std::shared_ptr<Want>> &wants, unsigned int flags)
{
return GetAbilities(context, requestCode, wants, flags, nullptr);
}
std::shared_ptr<PendingWant> PendingWant::GetAbilities(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, std::vector<std::shared_ptr<Want>> &wants,
unsigned int flags, const std::shared_ptr<WantParams> &options)
{
if (context == nullptr) {
WANT_AGENT_LOGE("PendingWant::GetAbilities invalid input param.");
return nullptr;
}
WantSenderInfo wantSenderInfo;
wantSenderInfo.type = (int32_t)WantAgentConstant::OperationType::START_ABILITIES;
wantSenderInfo.bundleName = context->GetBundleName();
wantSenderInfo.flags = flags;
wantSenderInfo.userId = -1; // -1 : invalid user id
wantSenderInfo.requestCode = requestCode;
for (auto want : wants) {
WantsInfo wantsInfo;
if (want != nullptr) {
wantsInfo.want = *want;
}
wantsInfo.resolvedTypes = want != nullptr ? want->GetType() : "";
if (options != nullptr) {
wantsInfo.want.SetParams(*options);
}
wantSenderInfo.allWants.push_back(wantsInfo);
}
sptr<IWantSender> target = AbilityManagerClient::GetInstance()->GetWantSender(wantSenderInfo, nullptr);
return std::make_shared<PendingWant>(target);
}
std::shared_ptr<PendingWant> PendingWant::GetCommonEvent(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<Want> &want, unsigned int flags)
{
return GetCommonEventAsUser(context, requestCode, want, flags, 0);
}
std::shared_ptr<PendingWant> PendingWant::GetCommonEventAsUser(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<Want> &want, unsigned int flags, int uid)
{
if (context == nullptr) {
WANT_AGENT_LOGE("PendingWant::GetCommonEventAsUser invalid input param.");
return nullptr;
}
WantsInfo wantsInfo;
if (want != nullptr) {
wantsInfo.want = *want;
}
wantsInfo.resolvedTypes = want != nullptr ? want->GetType() : "";
WantSenderInfo wantSenderInfo;
wantSenderInfo.type = (int32_t)WantAgentConstant::OperationType::SEND_COMMON_EVENT;
wantSenderInfo.allWants.push_back(wantsInfo);
wantSenderInfo.bundleName = context->GetBundleName();
wantSenderInfo.flags = flags;
wantSenderInfo.userId = -1; // -1 : invalid user id
wantSenderInfo.requestCode = requestCode;
sptr<IWantSender> target = AbilityManagerClient::GetInstance()->GetWantSender(wantSenderInfo, nullptr);
return std::make_shared<PendingWant>(target);
}
std::shared_ptr<PendingWant> PendingWant::GetService(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<Want> &want, unsigned int flags)
{
return BuildServicePendingWant(context, requestCode, want, flags,
WantAgentConstant::OperationType::START_SERVICE);
}
std::shared_ptr<PendingWant> PendingWant::GetForegroundService(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, int requestCode,
const std::shared_ptr<Want> &want, unsigned int flags)
{
return BuildServicePendingWant(
context, requestCode, want, flags, WantAgentConstant::OperationType::START_FOREGROUND_SERVICE);
}
std::shared_ptr<PendingWant> PendingWant::BuildServicePendingWant(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<Want> &want,
unsigned int flags, WantAgentConstant::OperationType serviceKind)
{
if (context == nullptr) {
WANT_AGENT_LOGE("PendingWant::BuildServicePendingWant invalid input param.");
return nullptr;
}
WantsInfo wantsInfo;
if (want != nullptr) {
wantsInfo.want = *want;
}
wantsInfo.resolvedTypes = want != nullptr ? want->GetType() : "";
WantSenderInfo wantSenderInfo;
wantSenderInfo.type = (int32_t)serviceKind;
wantSenderInfo.allWants.push_back(wantsInfo);
wantSenderInfo.bundleName = context->GetBundleName();
wantSenderInfo.flags = flags;
wantSenderInfo.userId = -1; // -1 : invalid user id
wantSenderInfo.requestCode = requestCode;
sptr<IWantSender> target = AbilityManagerClient::GetInstance()->GetWantSender(wantSenderInfo, nullptr);
return std::make_shared<PendingWant>(target);
}
void PendingWant::Cancel(const sptr<AAFwk::IWantSender> &target)
{
AbilityManagerClient::GetInstance()->CancelWantSender(target);
}
void PendingWant::Send(const sptr<AAFwk::IWantSender> &target)
{
Send(0, nullptr, nullptr, "", nullptr, target);
}
void PendingWant::Send(int resultCode, const sptr<AAFwk::IWantSender> &target)
{
Send(resultCode, nullptr, nullptr, "", nullptr, target);
}
void PendingWant::Send(int resultCode, const std::shared_ptr<Want> &want,
const sptr<AAFwk::IWantSender> &target)
{
Send(resultCode, want, nullptr, "", nullptr, target);
}
void PendingWant::Send(
int resultCode, const sptr<CompletedDispatcher> &onCompleted, const sptr<AAFwk::IWantSender> &target)
{
Send(resultCode, nullptr, onCompleted, "", nullptr, target);
}
void PendingWant::Send(int resultCode, const std::shared_ptr<Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const sptr<AAFwk::IWantSender> &target)
{
Send(resultCode, want, onCompleted, "", nullptr, target);
}
void PendingWant::Send(int resultCode, const std::shared_ptr<Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission,
const sptr<AAFwk::IWantSender> &target)
{
Send(resultCode, want, onCompleted, requiredPermission, nullptr, target);
}
void PendingWant::Send(int resultCode, const std::shared_ptr<Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission,
const std::shared_ptr<WantParams> &options, const sptr<AAFwk::IWantSender> &target)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (SendAndReturnResult(resultCode, want, onCompleted, requiredPermission, options, target) != 0) {
WANT_AGENT_LOGE("PendingWant::SendAndReturnResult failed.");
}
}
int PendingWant::SendAndReturnResult(int resultCode, const std::shared_ptr<Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission,
const std::shared_ptr<WantParams> &options, const sptr<AAFwk::IWantSender> &target)
{
HILOG_INFO("%{public}s:begin.", __func__);
SenderInfo senderInfo;
senderInfo.resolvedType = want != nullptr ? want->GetType() : "";
if (want != nullptr) {
senderInfo.want = *want;
}
if (options != nullptr) {
senderInfo.want.SetParams(*options);
}
senderInfo.requiredPermission = requiredPermission;
senderInfo.code = resultCode;
senderInfo.finishedReceiver = onCompleted;
return AbilityManagerClient::GetInstance()->SendWantSender(target, senderInfo);
}
bool PendingWant::Equals(
const std::shared_ptr<PendingWant> &targetPendingWant, const std::shared_ptr<PendingWant> &otherPendingWant)
{
if (targetPendingWant == nullptr && otherPendingWant == nullptr) {
return true;
}
if (targetPendingWant == nullptr || otherPendingWant == nullptr) {
return false;
}
return targetPendingWant->GetHashCode(targetPendingWant->GetTarget()) ==
targetPendingWant->GetHashCode(otherPendingWant->GetTarget());
}
sptr<IWantSender> PendingWant::GetTarget()
{
return target_;
}
void PendingWant::SetTarget(const sptr<AAFwk::IWantSender> &target)
{
target_ = target;
}
PendingWant::CancelReceiver::CancelReceiver(const std::weak_ptr<PendingWant> &outerInstance)
: outerInstance_(outerInstance)
{}
void PendingWant::CancelReceiver::PerformReceive(const AAFwk::Want &want, int resultCode, const std::string &data,
const AAFwk::WantParams &extras, bool serialized, bool sticky, int sendingUser)
{}
void PendingWant::CancelReceiver::Send(const int32_t resultCode)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (outerInstance_.lock() != nullptr) {
outerInstance_.lock()->NotifyCancelListeners(resultCode);
}
}
void PendingWant::RegisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (cancelListener == nullptr) {
WANT_AGENT_LOGE("PendingWant::RegisterCancelListener invalid input param.");
return;
}
std::scoped_lock<std::mutex> lock(lock_object);
if (cancelReceiver_ == nullptr) {
cancelReceiver_ = new (std::nothrow) CancelReceiver(weak_from_this());
}
bool isEmpty = cancelListeners_.empty();
cancelListeners_.push_back(cancelListener);
if (isEmpty) {
AbilityManagerClient::GetInstance()->RegisterCancelListener(target, cancelReceiver_);
}
}
void PendingWant::NotifyCancelListeners(int32_t resultCode)
{
HILOG_INFO("%{public}s:begin.", __func__);
std::vector<std::shared_ptr<CancelListener>> cancelListeners;
{
std::scoped_lock<std::mutex> lock(lock_object);
cancelListeners = std::vector<std::shared_ptr<CancelListener>>(cancelListeners_);
}
for (auto cancelListener : cancelListeners) {
if (cancelListener != nullptr) {
cancelListener->OnCancelled(resultCode);
}
}
}
void PendingWant::UnregisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (cancelListener == nullptr) {
WANT_AGENT_LOGE("PendingWant::UnregisterCancelListener invalid input param.");
return;
}
std::scoped_lock<std::mutex> lock(lock_object);
bool isEmpty = cancelListeners_.empty();
cancelListeners_.erase(remove_if(cancelListeners_.begin(),
cancelListeners_.end(),
[cancelListener](std::shared_ptr<CancelListener> x) { return x == cancelListener; }),
cancelListeners_.end());
if (cancelListeners_.empty() && !isEmpty) {
AbilityManagerClient::GetInstance()->UnregisterCancelListener(target, cancelReceiver_);
}
}
int PendingWant::GetHashCode(const sptr<AAFwk::IWantSender> &target)
{
int32_t code = -1;
AbilityManagerClient::GetInstance()->GetPendingWantCode(target, code);
return code;
}
int PendingWant::GetUid(const sptr<AAFwk::IWantSender> &target)
{
int32_t uid = -1;
AbilityManagerClient::GetInstance()->GetPendingWantUid(target, uid);
return uid;
}
std::string PendingWant::GetBundleName(const sptr<AAFwk::IWantSender> &target)
{
std::string bundleName = "";
AbilityManagerClient::GetInstance()->GetPendingWantBundleName(target, bundleName);
return bundleName;
}
std::shared_ptr<Want> PendingWant::GetWant(const sptr<AAFwk::IWantSender> &target)
{
std::shared_ptr<Want> want = std::make_shared<Want>();
int ret = AbilityManagerClient::GetInstance()->GetPendingRequestWant(target, want);
return ret ? nullptr : want;
}
bool PendingWant::Marshalling(Parcel &parcel) const
{
if (!parcel.WriteParcelable(target_->AsObject())) {
WANT_AGENT_LOGE("parcel WriteString failed");
return false;
}
return true;
}
PendingWant *PendingWant::Unmarshalling(Parcel &parcel)
{
PendingWant *pendingWant = new (std::nothrow) PendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("read from parcel failed");
return nullptr;
}
sptr<AAFwk::IWantSender> target = iface_cast<AAFwk::IWantSender>(parcel.ReadParcelable<IRemoteObject>());
if (target == nullptr) {
return nullptr;
}
pendingWant->SetTarget(target);
return pendingWant;
}
std::shared_ptr<WantSenderInfo> PendingWant::GetWantSenderInfo(const sptr<AAFwk::IWantSender> &target)
{
std::shared_ptr<WantSenderInfo> info = std::make_shared<WantSenderInfo>();
int ret = AbilityManagerClient::GetInstance()->GetWantSenderInfo(target, info);
return ret ? nullptr : info;
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2021 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 "trigger_info.h"
using namespace OHOS::AAFwk;
namespace OHOS::AbilityRuntime::WantAgent {
TriggerInfo::TriggerInfo() : permission_(""), extraInfo_(nullptr), want_(nullptr), resultCode_(0)
{}
TriggerInfo::TriggerInfo(const std::string &permission, const std::shared_ptr<WantParams> &extraInfo,
const std::shared_ptr<Want> &want, int resultCode)
{
permission_ = permission;
if (extraInfo != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*extraInfo);
}
if (want != nullptr) {
want_ = std::make_shared<Want>(*want);
}
resultCode_ = resultCode;
}
TriggerInfo::TriggerInfo(const TriggerInfo &paramInfo)
{
permission_ = paramInfo.GetPermission();
if (paramInfo.GetExtraInfo() != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*paramInfo.GetExtraInfo());
}
if (paramInfo.GetWant() != nullptr) {
want_ = std::make_shared<Want>(*paramInfo.GetWant());
}
resultCode_ = paramInfo.GetResultCode();
}
const TriggerInfo &TriggerInfo::operator=(const TriggerInfo &paramInfo)
{
permission_ = paramInfo.GetPermission();
if (paramInfo.GetExtraInfo() != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*paramInfo.GetExtraInfo());
}
if (paramInfo.GetWant() != nullptr) {
want_ = std::make_shared<Want>(*paramInfo.GetWant());
}
resultCode_ = paramInfo.GetResultCode();
return *this;
}
std::string TriggerInfo::GetPermission() const
{
return permission_;
}
std::shared_ptr<WantParams> TriggerInfo::GetExtraInfo() const
{
return extraInfo_;
}
std::shared_ptr<Want> TriggerInfo::GetWant() const
{
return want_;
}
int TriggerInfo::GetResultCode() const
{
return resultCode_;
}
TriggerInfo::Builder::Builder()
{}
std::shared_ptr<TriggerInfo::Builder> TriggerInfo::Builder::SetPermission(const std::string &permission)
{
permission_ = permission;
return shared_from_this();
}
std::shared_ptr<TriggerInfo::Builder> TriggerInfo::Builder::SetWantParams(const std::shared_ptr<WantParams> &params)
{
params_ = params;
return shared_from_this();
}
std::shared_ptr<TriggerInfo::Builder> TriggerInfo::Builder::SetWant(const std::shared_ptr<Want> &want)
{
want_ = want;
return shared_from_this();
}
std::shared_ptr<TriggerInfo::Builder> TriggerInfo::Builder::SetResultCode(int resultCode)
{
resultCode_ = resultCode;
return shared_from_this();
}
std::shared_ptr<TriggerInfo> TriggerInfo::Builder::Build()
{
return std::make_shared<TriggerInfo>(permission_, params_, want_, resultCode_);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2021 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 "want_agent.h"
#include "want_agent_log_wrapper.h"
namespace OHOS::AbilityRuntime::WantAgent {
WantAgent::WantAgent(const std::shared_ptr<PendingWant> &pendingWant)
{
pendingWant_ = pendingWant;
}
std::shared_ptr<PendingWant> WantAgent::GetPendingWant()
{
return pendingWant_;
}
void WantAgent::SetPendingWant(const std::shared_ptr<PendingWant> &pendingWant)
{
pendingWant_ = pendingWant;
}
bool WantAgent::Marshalling(Parcel &parcel) const
{
if (!parcel.WriteParcelable(pendingWant_.get())) {
WANT_AGENT_LOGE("parcel WriteString failed");
return false;
}
return true;
}
WantAgent *WantAgent::Unmarshalling(Parcel &parcel)
{
WantAgent *agent = new (std::nothrow) WantAgent();
if (agent == nullptr) {
WANT_AGENT_LOGE("read from parcel failed");
return nullptr;
}
std::shared_ptr<PendingWant> pendingWant(parcel.ReadParcelable<PendingWant>());
agent->SetPendingWant(pendingWant);
return agent;
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,439 @@
/*
* Copyright (c) 2021 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 "want_agent_helper.h"
#include "ability_manager_client.h"
#include "hilog_wrapper.h"
#include "ohos/aafwk/content/want_params_wrapper.h"
#include "pending_want.h"
#include "want_agent_log_wrapper.h"
#include "want_sender_info.h"
#include "want_sender_interface.h"
using namespace OHOS::AAFwk;
using namespace OHOS::AppExecFwk;
namespace OHOS::AbilityRuntime::WantAgent {
WantAgentHelper::WantAgentHelper()
{}
unsigned int WantAgentHelper::FlagsTransformer(const std::vector<WantAgentConstant::Flags> &flags)
{
unsigned int wantFlags = 0;
if (flags.empty()) {
wantFlags |= FLAG_UPDATE_CURRENT;
return wantFlags;
}
for (auto flag : flags) {
switch (flag) {
case WantAgentConstant::Flags::ONE_TIME_FLAG:
wantFlags |= FLAG_ONE_SHOT;
break;
case WantAgentConstant::Flags::NO_BUILD_FLAG:
wantFlags |= FLAG_NO_CREATE;
break;
case WantAgentConstant::Flags::CANCEL_PRESENT_FLAG:
wantFlags |= FLAG_CANCEL_CURRENT;
break;
case WantAgentConstant::Flags::UPDATE_PRESENT_FLAG:
wantFlags |= FLAG_UPDATE_CURRENT;
break;
case WantAgentConstant::Flags::CONSTANT_FLAG:
wantFlags |= FLAG_IMMUTABLE;
break;
default:
WANT_AGENT_LOGE("WantAgentHelper::flags is error.");
break;
}
}
return wantFlags;
}
std::shared_ptr<WantAgent> WantAgentHelper::GetWantAgent(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const WantAgentInfo &paramsInfo)
{
WANT_AGENT_LOGI("WantAgentHelper::GetWantAgent begin.");
if (context == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent invalid input param.");
return nullptr;
}
std::vector<std::shared_ptr<Want>> wants = paramsInfo.GetWants();
if (wants.empty()) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent invalid input param.");
return nullptr;
}
HILOG_INFO("%{public}s:bundle name = %{public}s; ability name = %{public}s",
__func__,
wants[0]->GetElement().GetBundleName().c_str(),
wants[0]->GetElement().GetAbilityName().c_str());
unsigned int flags = FlagsTransformer(paramsInfo.GetFlags());
if (flags == 0) {
WANT_AGENT_LOGE("WantAgentHelper::flags invalid.");
return nullptr;
}
std::shared_ptr<WantParams> extraInfo = paramsInfo.GetExtraInfo();
std::shared_ptr<PendingWant> pendingWant = nullptr;
int requestCode = paramsInfo.GetRequestCode();
WantAgentConstant::OperationType operationType = paramsInfo.GetOperationType();
switch (operationType) {
case WantAgentConstant::OperationType::START_ABILITY:
pendingWant = PendingWant::GetAbility(context, requestCode, wants[0], flags, extraInfo);
break;
case WantAgentConstant::OperationType::START_ABILITIES:
pendingWant = PendingWant::GetAbilities(context, requestCode, wants, flags, extraInfo);
break;
case WantAgentConstant::OperationType::START_SERVICE:
pendingWant = PendingWant::GetService(context, requestCode, wants[0], flags);
break;
case WantAgentConstant::OperationType::START_FOREGROUND_SERVICE:
pendingWant = PendingWant::GetForegroundService(context, requestCode, wants[0], flags);
break;
case WantAgentConstant::OperationType::SEND_COMMON_EVENT:
pendingWant = PendingWant::GetCommonEvent(context, requestCode, wants[0], flags);
break;
default:
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent operation type is error.");
break;
}
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent the wants does not meet the requirements.");
return nullptr;
}
std::shared_ptr<WantAgent> agent = std::make_shared<WantAgent>(pendingWant);
WANT_AGENT_LOGI("WantAgentHelper::GetWantAgent end.");
return agent;
}
std::shared_ptr<WantAgent> WantAgentHelper::GetWantAgent(const WantAgentInfo &paramsInfo, int32_t userId)
{
std::vector<std::shared_ptr<Want>> wants = paramsInfo.GetWants();
if (wants.empty()) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent invalid input param.");
return nullptr;
}
std::shared_ptr<Want> want = wants[0];
if (want == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent invalid input param.");
return nullptr;
}
WantsInfo wantsInfo;
wantsInfo.want = *want;
wantsInfo.resolvedTypes = want->GetType();
if (paramsInfo.GetExtraInfo() != nullptr) {
wantsInfo.want.SetParams(*paramsInfo.GetExtraInfo());
}
HILOG_INFO("%{public}s:bundle name = %{public}s; ability name = %{public}s",
__func__,
wantsInfo.want.GetElement().GetBundleName().c_str(),
wantsInfo.want.GetElement().GetAbilityName().c_str());
WantSenderInfo wantSenderInfo;
wantSenderInfo.allWants.push_back(wantsInfo);
wantSenderInfo.bundleName = want->GetOperation().GetBundleName();
wantSenderInfo.flags = FlagsTransformer(paramsInfo.GetFlags());
wantSenderInfo.type = (int32_t)paramsInfo.GetOperationType();
wantSenderInfo.userId = userId;
sptr<IWantSender> target = AbilityManagerClient::GetInstance()->GetWantSender(wantSenderInfo, nullptr);
if (target == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWantAgent target is nullptr.");
return nullptr;
}
std::shared_ptr<WantAgent> agent = std::make_shared<WantAgent>(std::make_shared<PendingWant>(target));
return agent;
}
WantAgentConstant::OperationType WantAgentHelper::GetType(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr || agent->GetPendingWant() == nullptr) {
return WantAgentConstant::OperationType::UNKNOWN_TYPE;
}
return agent->GetPendingWant()->GetType(agent->GetPendingWant()->GetTarget());
}
void WantAgentHelper::TriggerWantAgent(const std::shared_ptr<WantAgent> &agent,
const std::shared_ptr<CompletedCallback> &callback, const TriggerInfo &paramsInfo)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::TriggerWantAgent invalid input param.");
return;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
WantAgentConstant::OperationType type = GetType(agent);
sptr<CompletedDispatcher> dispatcher = nullptr;
if (callback != nullptr) {
dispatcher = new (std::nothrow) CompletedDispatcher(pendingWant, callback, nullptr);
}
Send(pendingWant, type, dispatcher, paramsInfo);
}
void WantAgentHelper::Send(const std::shared_ptr<PendingWant> &pendingWant,
WantAgentConstant::OperationType type, const sptr<CompletedDispatcher> &callBack, const TriggerInfo &paramsInfo)
{
HILOG_INFO("%{public}s:begin.", __func__);
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::Send invalid input param.");
return;
}
pendingWant->Send(paramsInfo.GetResultCode(),
paramsInfo.GetWant(),
callBack,
paramsInfo.GetPermission(),
paramsInfo.GetExtraInfo(),
pendingWant->GetTarget());
}
void WantAgentHelper::Cancel(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::Cancel WantAgent invalid input param.");
return;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::Cancel PendingWant invalid input param.");
return;
}
pendingWant->Cancel(pendingWant->GetTarget());
}
bool WantAgentHelper::JudgeEquality(
const std::shared_ptr<WantAgent> &agent, const std::shared_ptr<WantAgent> &otherAgent)
{
if ((agent == nullptr) && (otherAgent == nullptr)) {
return true;
}
if ((agent == nullptr) || (otherAgent == nullptr)) {
return false;
}
return PendingWant::Equals(agent->GetPendingWant(), otherAgent->GetPendingWant());
}
int WantAgentHelper::GetHashCode(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetHashCode WantAgent invalid input param.");
return 0;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetHashCode PendingWant invalid input param.");
return 0;
}
return pendingWant->GetHashCode(pendingWant->GetTarget());
}
std::string WantAgentHelper::GetBundleName(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetBundleName WantAgent invalid input param.");
return "";
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetBundleName PendingWant invalid input param.");
return "";
}
return pendingWant->GetBundleName(pendingWant->GetTarget());
}
int WantAgentHelper::GetUid(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetUid WantAgent invalid input param.");
return -1;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetUid PendingWant invalid input param.");
return -1;
}
return pendingWant->GetUid(pendingWant->GetTarget());
}
std::shared_ptr<Want> WantAgentHelper::GetWant(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWant WantAgent invalid input param.");
return nullptr;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::GetWant PendingWant invalid input param.");
return nullptr;
}
return pendingWant->GetWant(pendingWant->GetTarget());
}
void WantAgentHelper::RegisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::RegisterCancelListener WantAgent invalid input param.");
return;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::RegisterCancelListener PendingWant invalid input param.");
return;
}
pendingWant->RegisterCancelListener(cancelListener, pendingWant->GetTarget());
}
void WantAgentHelper::UnregisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::UnregisterCancelListener WantAgent invalid input param.");
return;
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::UnregisterCancelListener PendingWant invalid input param.");
return;
}
pendingWant->UnregisterCancelListener(cancelListener, pendingWant->GetTarget());
}
std::string WantAgentHelper::ToString(const std::shared_ptr<WantAgent> &agent)
{
if (agent == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::ToString WantAgent invalid input param.");
return "";
}
std::shared_ptr<PendingWant> pendingWant = agent->GetPendingWant();
if (pendingWant == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::ToString PendingWant invalid input param.");
return "";
}
std::shared_ptr<WantSenderInfo> info = pendingWant->GetWantSenderInfo(pendingWant->GetTarget());
if (info == nullptr) {
WANT_AGENT_LOGE("WantAgentHelper::ToString WantSenderInfo invalid input param.");
return "";
}
nlohmann::json jsonObject;
jsonObject["requestCode"] = (*info.get()).requestCode;
jsonObject["operationType"] = (*info.get()).type;
jsonObject["flags"] = (*info.get()).flags;
nlohmann::json wants = nlohmann::json::array();
for (auto &wantInfo : (*info.get()).allWants) {
wants.emplace_back(wantInfo.want.ToString());
}
jsonObject["wants"] = wants;
if ((*info.get()).allWants.size() > 0) {
nlohmann::json paramsObj;
AAFwk::WantParamWrapper wWrapper((*info.get()).allWants[0].want.GetParams());
paramsObj["extraInfoValue"] = wWrapper.ToString();
jsonObject["extraInfo"] = paramsObj;
}
return jsonObject.dump();
}
std::shared_ptr<WantAgent> WantAgentHelper::FromString(const std::string &jsonString)
{
if (jsonString.empty()) {
return nullptr;
}
nlohmann::json jsonObject = nlohmann::json::parse(jsonString);
int requestCode = -1;
if (jsonObject.contains("requestCode")) {
requestCode = jsonObject.at("requestCode").get<int>();
}
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::UNKNOWN_TYPE;
if (jsonObject.contains("operationType")) {
operationType = static_cast<WantAgentConstant::OperationType>(jsonObject.at("operationType").get<int>());
}
int flags = -1;
std::vector<WantAgentConstant::Flags> flagsVec = {};
if (jsonObject.contains("flags")) {
flags = jsonObject.at("flags").get<int>();
}
if (flags | FLAG_ONE_SHOT) {
flagsVec.emplace_back(WantAgentConstant::Flags::ONE_TIME_FLAG);
} else if (flags | FLAG_NO_CREATE) {
flagsVec.emplace_back(WantAgentConstant::Flags::NO_BUILD_FLAG);
} else if (flags | FLAG_CANCEL_CURRENT) {
flagsVec.emplace_back(WantAgentConstant::Flags::CANCEL_PRESENT_FLAG);
} else if (flags | FLAG_UPDATE_CURRENT) {
flagsVec.emplace_back(WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
} else if (flags | FLAG_IMMUTABLE) {
flagsVec.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
}
std::vector<std::shared_ptr<AAFwk::Want>> wants = {};
if (jsonObject.contains("wants")) {
for (auto &wantObj : jsonObject.at("wants")) {
auto wantString = wantObj.get<std::string>();
wants.emplace_back(std::make_shared<AAFwk::Want>(*Want::FromString(wantString)));
}
}
std::shared_ptr<AAFwk::WantParams> extraInfo = nullptr;
if (jsonObject.contains("extraInfo")) {
auto extraInfoObj = jsonObject.at("extraInfo");
if (extraInfoObj.contains("extraInfoValue")) {
auto pwWrapper = AAFwk::WantParamWrapper::Parse(extraInfoObj.at("extraInfoValue").get<std::string>());
AAFwk::WantParams params;
if (pwWrapper->GetValue(params) == ERR_OK) {
extraInfo = std::make_shared<AAFwk::WantParams>(params);
}
}
}
WantAgentInfo info(requestCode, operationType, flagsVec, wants, extraInfo);
return GetWantAgent(info);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,107 @@
/*
* Copyright (c) 2021 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 "want_agent_info.h"
#include "want.h"
#include "want_params.h"
using namespace OHOS::AAFwk;
namespace OHOS::AbilityRuntime::WantAgent {
WantAgentInfo::WantAgentInfo() : WantAgentInfo(nullptr)
{}
WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType,
WantAgentConstant::Flags flag, std::vector<std::shared_ptr<Want>> &wants,
const std::shared_ptr<WantParams> &extraInfo)
{
requestCode_ = requestCode;
operationType_ = operationType;
flags_.push_back(flag);
if (!wants.empty()) {
for (auto want : wants) {
if (want != nullptr) {
wants_.push_back(std::make_shared<Want>(*want));
}
}
}
if (extraInfo != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*extraInfo);
}
}
WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType,
const std::vector<WantAgentConstant::Flags> &flags, std::vector<std::shared_ptr<Want>> &wants,
const std::shared_ptr<WantParams> &extraInfo)
{
requestCode_ = requestCode;
operationType_ = operationType;
if (!flags.empty()) {
flags_.insert(flags_.end(), flags.begin(), flags.end());
}
if (!wants.empty()) {
for (auto want : wants) {
if (want != nullptr) {
wants_.push_back(std::make_shared<Want>(*want));
}
}
}
if (extraInfo != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*extraInfo);
}
}
WantAgentInfo::WantAgentInfo(const std::shared_ptr<WantAgentInfo> &paramInfo)
{
if (paramInfo != nullptr) {
requestCode_ = paramInfo->GetRequestCode();
operationType_ = paramInfo->GetOperationType();
flags_.insert(flags_.end(), paramInfo->GetFlags().begin(), paramInfo->GetFlags().end());
if (!paramInfo->GetWants().empty()) {
for (auto want : paramInfo->GetWants()) {
wants_.push_back(std::make_shared<Want>(*want));
}
}
if (paramInfo->GetExtraInfo() != nullptr) {
extraInfo_ = std::make_shared<WantParams>(*paramInfo->GetExtraInfo());
}
}
}
int WantAgentInfo::GetRequestCode() const
{
return requestCode_;
}
WantAgentConstant::OperationType WantAgentInfo::GetOperationType() const
{
return operationType_;
}
std::vector<WantAgentConstant::Flags> WantAgentInfo::GetFlags() const
{
return flags_;
}
std::vector<std::shared_ptr<Want>> WantAgentInfo::GetWants() const
{
return wants_;
}
std::shared_ptr<WantParams> WantAgentInfo::GetExtraInfo() const
{
return extraInfo_;
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2021 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 "want_agent_log_wrapper.h"
namespace OHOS::AbilityRuntime::WantAgent {
// initial static member object
WantAgentLogLevel WantAgentLogWrapper::level_ = WantAgentLogLevel::DEBUG;
bool WantAgentLogWrapper::JudgeLevel(const WantAgentLogLevel &level)
{
const WantAgentLogLevel &curLevel = WantAgentLogWrapper::GetLogLevel();
if (level < curLevel) {
return false;
}
return true;
}
std::string WantAgentLogWrapper::GetBriefFileName(const char *str)
{
if (!str) {
return std::string();
}
std::string fullPath(str);
size_t pos = fullPath.find_last_of("/");
if (pos == std::string::npos) {
return std::string();
}
return fullPath.substr(pos + 1);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,25 @@
# Copyright (c) 2021 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.
group("unittest") {
testonly = true
deps = [
"unittest/completed_dispatcher_test:unittest",
"unittest/pending_want_test:unittest",
"unittest/trigger_Info_test:unittest",
"unittest/want_agent_helper_test:unittest",
"unittest/want_agent_info_test:unittest",
"unittest/want_agent_test:unittest",
]
}

View File

@ -0,0 +1,60 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("completed_dispatcher_test") {
module_out_path = module_output_path
include_dirs = [ "//foundation/aafwk/standard/services/abilitymgr/include" ]
sources = [ "completed_dispatcher_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":completed_dispatcher_test" ]
}

View File

@ -0,0 +1,209 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "completed_callback.h"
#define private public
#define protected public
#include "completed_dispatcher.h"
#undef private
#undef protected
#include "element_name.h"
#include "event_handler.h"
#include "pending_want.h"
#include "ohos/aafwk/base/base_types.h"
#include "want.h"
#include "want_params.h"
#include "want_receiver_stub.h"
#include "ohos/aafwk/base/bool_wrapper.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class CompletedDispatcherTest : public testing::Test {
public:
CompletedDispatcherTest()
{}
~CompletedDispatcherTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
Want MakeWant(std::string deviceId, std::string abilityName, std::string bundleName);
class CompletedCallbackSon : public CompletedCallback {
/**
* Called when a Send operation as completed.
*
* @param want The original Want that was sent.
* @param resultCode The final result code determined by the Send.
* @param resultData The final data collected by a broadcast.
* @param resultExtras The final extras collected by a broadcast.
*/
public:
void OnSendFinished(const AAFwk::Want &want, int resultCode, const std::string &resultData,
const AAFwk::WantParams &resultExtras) override;
static int code;
};
};
int CompletedDispatcherTest::CompletedCallbackSon::code = 0;
void CompletedDispatcherTest::CompletedCallbackSon::OnSendFinished(
const AAFwk::Want &want, int resultCode, const std::string &resultData, const AAFwk::WantParams &resultExtras)
{
code = 100;
}
Want CompletedDispatcherTest::MakeWant(std::string deviceId, std::string abilityName, std::string bundleName)
{
ElementName element(deviceId, bundleName, abilityName);
Want want;
want.SetElement(element);
return want;
}
void CompletedDispatcherTest::SetUpTestCase(void)
{}
void CompletedDispatcherTest::TearDownTestCase(void)
{}
void CompletedDispatcherTest::SetUp(void)
{}
void CompletedDispatcherTest::TearDown(void)
{}
/*
* @tc.number : CompletedDispatcher_0100
* @tc.name : CompletedDispatcher Constructors
* @tc.desc : 1.The parameter is nullptr
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0100, Function | MediumTest | Level1)
{
CompletedDispatcher completedDispatcher(nullptr, nullptr, nullptr);
EXPECT_EQ(completedDispatcher.pendingWant_, nullptr);
EXPECT_EQ(completedDispatcher.callback_, nullptr);
EXPECT_EQ(completedDispatcher.handler_, nullptr);
}
/*
* @tc.number : CompletedDispatcher_0200
* @tc.name : CompletedDispatcher Constructors
* @tc.desc : 1.The parameter is not nullptr
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0200, Function | MediumTest | Level1)
{
std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
EXPECT_EQ(completedDispatcher.pendingWant_, nullptr);
EXPECT_EQ(completedDispatcher.callback_, callBack);
EXPECT_EQ(completedDispatcher.handler_, nullptr);
}
/*
* @tc.number : CompletedDispatcher_0300
* @tc.name : CompletedDispatcher PerformReceive
* @tc.desc : 1.The parameter is not nullptr
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0300, Function | MediumTest | Level1)
{
std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
Want want = MakeWant("device", "ability", "bundleName");
std::string key = "key";
bool value = true;
WantParams wParams;
wParams.SetParam(key, Boolean::Box(value));
completedDispatcher.PerformReceive(want, 10, "test", wParams, 0, 0, 1);
EXPECT_EQ(completedDispatcher.want_.GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(completedDispatcher.want_.GetElement().GetAbilityName(), "ability");
EXPECT_EQ(completedDispatcher.resultCode_, 10);
EXPECT_EQ(completedDispatcher.resultData_, "test");
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(completedDispatcher.resultExtras_.GetParam(key))), value);
EXPECT_EQ(CompletedCallbackSon::code, 100);
CompletedCallbackSon::code = 0;
}
/*
* @tc.number : CompletedDispatcher_0400
* @tc.name : CompletedDispatcher PerformReceive
* @tc.desc : 1.The parameter is not nullptr
* 2.called callBack
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0400, Function | MediumTest | Level1)
{
std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>();
CompletedDispatcher completedDispatcher(nullptr, callBack, handler);
Want want = MakeWant("device", "ability", "bundleName");
std::string key = "key";
bool value = false;
WantParams wParams;
wParams.SetParam(key, Boolean::Box(value));
completedDispatcher.PerformReceive(want, 10, "test", wParams, 0, 0, 1);
EXPECT_EQ(completedDispatcher.want_.GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(completedDispatcher.want_.GetElement().GetAbilityName(), "ability");
EXPECT_EQ(completedDispatcher.resultCode_, 10);
EXPECT_EQ(completedDispatcher.resultData_, "test");
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(completedDispatcher.resultExtras_.GetParam(key))), value);
EXPECT_EQ(CompletedCallbackSon::code, 0);
CompletedCallbackSon::code = 0;
}
/*
* @tc.number : CompletedDispatcher_0500
* @tc.name : CompletedDispatcher Run
* @tc.desc : 1.The parameter is not nullptr
* 2.called callBack
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0500, Function | MediumTest | Level1)
{
std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
EXPECT_EQ(completedDispatcher.callback_, callBack);
completedDispatcher.Run();
EXPECT_EQ(CompletedCallbackSon::code, 100);
CompletedCallbackSon::code = 0;
}
/*
* @tc.number : CompletedDispatcher_0600
* @tc.name : CompletedDispatcher Run
* @tc.desc : 1.The parameter is nullptr
* 2.no called callBack
*/
HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0600, Function | MediumTest | Level1)
{
CompletedDispatcher completedDispatcher(nullptr, nullptr, nullptr);
EXPECT_EQ(completedDispatcher.callback_, nullptr);
completedDispatcher.Run();
EXPECT_EQ(CompletedCallbackSon::code, 0);
CompletedCallbackSon::code = 0;
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,68 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("pending_want_test") {
module_out_path = module_output_path
include_dirs = [
"//aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context/",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime",
"//foundation/aafwk/standard/services/abilitymgr/include",
"//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include/",
"//utils/system/safwk/native/include",
"//utils/native/base/include",
]
sources = [ "pending_want_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":pending_want_test" ]
}

View File

@ -0,0 +1,700 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "ability_manager_client.h"
#include "cancel_listener.h"
#include "completed_callback.h"
#include "completed_dispatcher.h"
#include "context/context.h"
#include "context_container.h"
#include "context_impl.h"
#include "element_name.h"
#include "event_handler.h"
#include "iservice_registry.h"
#include "ohos/aafwk/base/base_types.h"
#define private public
#define protected public
#include "pending_want.h"
#undef private
#undef protected
#include "sa_mgr_client.h"
#include "system_ability_definition.h"
#include "want.h"
#include "wants_info.h"
#include "want_params.h"
#include "want_receiver_stub.h"
#include "want_agent_helper.h"
#include "want_sender_info.h"
#include "want_sender_stub.h"
#include "ohos/aafwk/base/array_wrapper.h"
#include "ohos/aafwk/base/bool_wrapper.h"
#include "ohos/aafwk/base/zchar_wrapper.h"
#include "ohos/aafwk/base/byte_wrapper.h"
#include "ohos/aafwk/base/double_wrapper.h"
#include "ohos/aafwk/base/float_wrapper.h"
#include "ohos/aafwk/base/int_wrapper.h"
#include "ohos/aafwk/base/long_wrapper.h"
#include "ohos/aafwk/base/short_wrapper.h"
#include "ohos/aafwk/base/string_wrapper.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class PendingWantTest : public testing::Test {
public:
PendingWantTest()
{}
~PendingWantTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
static Want MakeWant(std::string deviceId, std::string abilityName, std::string bundleName);
static int callBackCancelListenerConnt;
class WantSender : public AAFwk::WantSenderStub {
public:
void Send(SenderInfo &senderInfo) override;
};
class CancelListenerSon : public CancelListener {
public:
void OnCancelled(int resultCode) override;
};
};
int PendingWantTest::callBackCancelListenerConnt = 0;
void PendingWantTest::WantSender::Send(SenderInfo &senderInfo)
{}
void PendingWantTest::CancelListenerSon::OnCancelled(int resultCode)
{
callBackCancelListenerConnt++;
}
Want PendingWantTest::MakeWant(std::string deviceId, std::string abilityName, std::string bundleName)
{
ElementName element(deviceId, bundleName, abilityName);
Want want;
want.SetElement(element);
return want;
}
std::shared_ptr<OHOS::AbilityRuntime::Context> GetAppContext()
{
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
return context;
}
void PendingWantTest::SetUpTestCase(void)
{}
void PendingWantTest::TearDownTestCase(void)
{}
void PendingWantTest::SetUp(void)
{}
void PendingWantTest::TearDown(void)
{}
/*
* @tc.number : PendingWant_0100
* @tc.name : PendingWant Constructors
* @tc.desc : 1.The parameter is nullptr
*/
HWTEST_F(PendingWantTest, PendingWant_0100, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
EXPECT_EQ(pendingWant.target_, nullptr);
EXPECT_EQ(pendingWant.cancelReceiver_, nullptr);
EXPECT_EQ(pendingWant.whitelistToken_, nullptr);
}
/*
* @tc.number : PendingWant_0200
* @tc.name : PendingWant Constructors
* @tc.desc : 1.The parameter target is not nullptr
*/
HWTEST_F(PendingWantTest, PendingWant_0200, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target(new (std::nothrow) WantSender());
PendingWant pendingWant(target);
EXPECT_EQ(pendingWant.target_, target);
EXPECT_EQ(pendingWant.cancelReceiver_, nullptr);
EXPECT_EQ(pendingWant.whitelistToken_, nullptr);
}
/*
* @tc.number : PendingWant_0300
* @tc.name : PendingWant GetType
* @tc.desc : 1.Get PendingWant Type (UNKNOWN_TYPE)
*/
HWTEST_F(PendingWantTest, PendingWant_0300, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target = nullptr;
PendingWant pendingWant(target);
EXPECT_EQ(pendingWant.target_, target);
EXPECT_EQ(pendingWant.GetType(target), WantAgentConstant::OperationType::UNKNOWN_TYPE);
}
/*
* @tc.number : PendingWant_0400
* @tc.name : PendingWant GetAbility
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_0400, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetAbility(nullptr, requestCode, want, flags);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_0600
* @tc.name : PendingWant GetAbility
* @tc.desc : 1.Get pendingWant (options is not nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_0600, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetAbility(
GetAppContext(), requestCode, want, flags, wParams);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_0700
* @tc.name : PendingWant GetAbilities
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_0700, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
std::shared_ptr<Want> want2 = std::make_shared<Want>();
ElementName element2("device", "bundleName", "abilityName");
want2->SetElement(element2);
std::vector<std::shared_ptr<Want>> wants;
wants.emplace_back(want);
wants.emplace_back(want2);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetAbilities(
nullptr, requestCode, wants, flags);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_0800
* @tc.name : PendingWant GetAbilities
* @tc.desc : 1.Get pendingWant (context is not nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_0800, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
std::shared_ptr<Want> want2 = std::make_shared<Want>();
ElementName element2("device", "bundleName", "abilityName");
want2->SetElement(element2);
std::vector<std::shared_ptr<Want>> wants;
wants.emplace_back(want);
wants.emplace_back(want2);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetAbilities(
GetAppContext(), requestCode, wants, flags);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_0900
* @tc.name : PendingWant GetAbilities
* @tc.desc : 1.Get pendingWant (options is not nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_0900, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
std::shared_ptr<Want> want2 = std::make_shared<Want>();
ElementName element2("device", "bundleName", "abilityName");
want2->SetElement(element2);
std::vector<std::shared_ptr<Want>> wants;
wants.emplace_back(want);
wants.emplace_back(want2);
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetAbilities(
GetAppContext(), requestCode, wants, flags, wParams);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1000
* @tc.name : PendingWant GetCommonEventAsUser
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1000, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetCommonEventAsUser(
nullptr, requestCode, want, flags, 0);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1100
* @tc.name : PendingWant GetCommonEventAsUser
* @tc.desc : 1.Get pendingWant (context is not nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1100, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetCommonEventAsUser(
GetAppContext(), requestCode, want, flags, 0);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1200
* @tc.name : PendingWant GetCommonEventAsUser
* @tc.desc : 1.Get pendingWant (want is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1200, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want;
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetCommonEventAsUser(
GetAppContext(), requestCode, want, flags, 0);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1300
* @tc.name : PendingWant GetService
* @tc.desc : 1.Get pendingWant (want is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1300, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want;
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetService(
GetAppContext(), requestCode, want, flags);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1400
* @tc.name : PendingWant GetService
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1400, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetService(nullptr, requestCode, want, flags);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1500
* @tc.name : PendingWant GetForegroundService
* @tc.desc : 1.Get pendingWant (want is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1500, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want;
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetForegroundService(
GetAppContext(), requestCode, want, flags);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1600
* @tc.name : PendingWant GetForegroundService
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1600, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
std::shared_ptr<PendingWant> pendingWant = PendingWant::GetForegroundService(nullptr, requestCode, want, flags);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1700
* @tc.name : PendingWant GetForegroundService
* @tc.desc : 1.Get pendingWant (want is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1700, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want;
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE;
std::shared_ptr<PendingWant> pendingWant =
PendingWant::BuildServicePendingWant(GetAppContext(), requestCode, want, flags, type);
EXPECT_NE(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1800
* @tc.name : PendingWant GetForegroundService
* @tc.desc : 1.Get pendingWant (context is nullptr)
*/
HWTEST_F(PendingWantTest, PendingWant_1800, Function | MediumTest | Level1)
{
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE;
std::shared_ptr<PendingWant> pendingWant =
PendingWant::BuildServicePendingWant(nullptr, requestCode, want, flags, type);
EXPECT_EQ(pendingWant, nullptr);
}
/*
* @tc.number : PendingWant_1900
* @tc.name : PendingWant Equals
* @tc.desc : 1.Equals
*/
HWTEST_F(PendingWantTest, PendingWant_1900, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
sptr<AAFwk::IWantSender> target2(nullptr);
std::shared_ptr<PendingWant> pendingWant2 = std::make_shared<PendingWant>(target2);
EXPECT_EQ(pendingWant->Equals(pendingWant, pendingWant2), false);
}
/*
* @tc.number : PendingWant_2000
* @tc.name : PendingWant Equals
* @tc.desc : 1.Equals
*/
HWTEST_F(PendingWantTest, PendingWant_2000, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<PendingWant> pendingWant2(nullptr);
EXPECT_EQ(pendingWant->Equals(pendingWant, pendingWant2), false);
}
/*
* @tc.number : PendingWant_2100
* @tc.name : PendingWant SendAndReturnResult
* @tc.desc : SendAndReturnResult
*/
HWTEST_F(PendingWantTest, PendingWant_2100, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
bool value = true;
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
std::string key = "key";
wParams->SetParam(key, Boolean::Box(value));
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
AbilityManagerClient::GetInstance()->Connect();
EXPECT_EQ(INNER_ERR,
pendingWant.SendAndReturnResult(requestCode, want, nullptr, "Permission", nullptr, nullptr));
}
/*
* @tc.number : PendingWant_2200
* @tc.name : PendingWant SendAndReturnResult
* @tc.desc : SendAndReturnResult
*/
HWTEST_F(PendingWantTest, PendingWant_2200, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
int requestCode = 10;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
bool value = true;
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
std::string key = "key";
wParams->SetParam(key, Boolean::Box(value));
unsigned int flags = 1;
flags |= FLAG_NO_CREATE;
AbilityManagerClient::GetInstance()->Connect();
EXPECT_EQ(1, pendingWant.SendAndReturnResult(requestCode, want, nullptr, "Permission", nullptr, nullptr));
}
/*
* @tc.number : PendingWant_2300
* @tc.name : PendingWant Equals
* @tc.desc : Equals
*/
HWTEST_F(PendingWantTest, PendingWant_2300, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<PendingWant> pendingWant2 = std::make_shared<PendingWant>(target);
EXPECT_EQ(pendingWant->Equals(pendingWant, pendingWant2), true);
}
/*
* @tc.number : PendingWant_2400
* @tc.name : PendingWant GetTarget
* @tc.desc : 1.GetTarget
*/
HWTEST_F(PendingWantTest, PendingWant_2400, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
auto target = pendingWant.GetTarget();
EXPECT_EQ(target, nullptr);
}
/*
* @tc.number : PendingWant_2500
* @tc.name : PendingWant GetTarget
* @tc.desc : 1.GetTarget
*/
HWTEST_F(PendingWantTest, PendingWant_2500, Function | MediumTest | Level1)
{
sptr<AAFwk::IWantSender> target(new (std::nothrow) WantSender());
PendingWant pendingWant(target);
auto target1 = pendingWant.GetTarget();
EXPECT_EQ(target1, target);
}
/*
* @tc.number : PendingWant_2600
* @tc.name : PendingWant RegisterCancelListener
* @tc.desc : 1.RegisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_2600, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener1 = std::make_shared<CancelListenerSon>();
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(cancelListener1, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 2);
}
/*
* @tc.number : PendingWant_2700
* @tc.name : PendingWant RegisterCancelListener
* @tc.desc : 1.RegisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_2700, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 1);
}
/*
* @tc.number : PendingWant_2800
* @tc.name : PendingWant RegisterCancelListener
* @tc.desc : 1.RegisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_2800, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.RegisterCancelListener(nullptr, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 0);
}
/*
* @tc.number : PendingWant_2900
* @tc.name : PendingWant NotifyCancelListeners
* @tc.desc : 1.NotifyCancelListeners
*/
HWTEST_F(PendingWantTest, PendingWant_2900, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener1 = std::make_shared<CancelListenerSon>();
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(cancelListener1, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
pendingWant.NotifyCancelListeners(0);
EXPECT_EQ(callBackCancelListenerConnt, 2);
callBackCancelListenerConnt = 0;
}
/*
* @tc.number : PendingWant_3000
* @tc.name : PendingWant NotifyCancelListeners
* @tc.desc : 1.NotifyCancelListeners
*/
HWTEST_F(PendingWantTest, PendingWant_3000, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
pendingWant.NotifyCancelListeners(0);
EXPECT_EQ(callBackCancelListenerConnt, 1);
callBackCancelListenerConnt = 0;
}
/*
* @tc.number : PendingWant_3100
* @tc.name : PendingWant NotifyCancelListeners
* @tc.desc : 1.NotifyCancelListeners
*/
HWTEST_F(PendingWantTest, PendingWant_3100, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.NotifyCancelListeners(0);
EXPECT_EQ(callBackCancelListenerConnt, 0);
callBackCancelListenerConnt = 0;
}
/*
* @tc.number : PendingWant_3200
* @tc.name : PendingWant UnregisterCancelListener
* @tc.desc : 1.UnregisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_3200, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener1 = std::make_shared<CancelListenerSon>();
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(cancelListener1, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 2);
pendingWant.UnregisterCancelListener(cancelListener1, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 1);
}
/*
* @tc.number : PendingWant_3300
* @tc.name : PendingWant UnregisterCancelListener
* @tc.desc : 1.UnregisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_3300, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
std::shared_ptr<CancelListener> cancelListener2 = std::make_shared<CancelListenerSon>();
pendingWant.RegisterCancelListener(nullptr, nullptr);
pendingWant.RegisterCancelListener(cancelListener2, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 1);
pendingWant.UnregisterCancelListener(cancelListener2, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 0);
}
/*
* @tc.number : PendingWant_3400
* @tc.name : PendingWant UnregisterCancelListener
* @tc.desc : 1.UnregisterCancelListener
*/
HWTEST_F(PendingWantTest, PendingWant_3400, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
pendingWant.UnregisterCancelListener(nullptr, nullptr);
EXPECT_EQ(static_cast<int>(pendingWant.cancelListeners_.size()), 0);
}
/*
* @tc.number : PendingWant_3500
* @tc.name : PendingWant GetWant
* @tc.desc : 1.GetWant
*/
HWTEST_F(PendingWantTest, PendingWant_3500, Function | MediumTest | Level1)
{
PendingWant pendingWant(nullptr);
auto want = pendingWant.GetWant(nullptr);
EXPECT_EQ(want, nullptr);
}
/*
* @tc.number : PendingWant_3600
* @tc.name : PendingWant Equals
* @tc.desc : Equals
*/
HWTEST_F(PendingWantTest, PendingWant_3700, Function | MediumTest | Level1)
{
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<PendingWant> pendingWant2(nullptr);
EXPECT_EQ(pendingWant->Equals(pendingWant, pendingWant2), true);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,60 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("trigger_Info_test") {
module_out_path = module_output_path
include_dirs = [ "//foundation/aafwk/standard/services/abilitymgr/include" ]
sources = [ "trigger_Info_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":trigger_Info_test" ]
}

View File

@ -0,0 +1,225 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "completed_callback.h"
#include "event_handler.h"
#include "element_name.h"
#include "ohos/aafwk/base/base_types.h"
#include "pending_want.h"
#define private public
#define protected public
#include "trigger_info.h"
#undef private
#undef protected
#include "want.h"
#include "want_params.h"
#include "want_receiver_stub.h"
#include "ohos/aafwk/base/bool_wrapper.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class TriggerInfoTest : public testing::Test {
public:
TriggerInfoTest()
{}
~TriggerInfoTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void TriggerInfoTest::SetUpTestCase(void)
{}
void TriggerInfoTest::TearDownTestCase(void)
{}
void TriggerInfoTest::SetUp(void)
{}
void TriggerInfoTest::TearDown(void)
{}
/*
* @tc.number : TriggerInfo_0100
* @tc.name : TriggerInfo Constructors
* @tc.desc : 1.def Constructors
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0100, Function | MediumTest | Level1)
{
TriggerInfo triggerInfo;
EXPECT_EQ(triggerInfo.permission_, "");
EXPECT_EQ(triggerInfo.extraInfo_, nullptr);
EXPECT_EQ(triggerInfo.want_, nullptr);
EXPECT_EQ(triggerInfo.resultCode_, 0);
}
/*
* @tc.number : TriggerInfo_0200
* @tc.name : TriggerInfo Constructors
* @tc.desc : 1.def Constructors
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0200, Function | MediumTest | Level1)
{
std::string permission = "nihao";
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
int resultCode = 10;
TriggerInfo triggerInfo(permission, wParams, want, resultCode);
EXPECT_EQ(triggerInfo.permission_, permission);
EXPECT_NE(triggerInfo.extraInfo_, wParams);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(triggerInfo.extraInfo_->GetParam(key))), value);
EXPECT_NE(triggerInfo.want_, want);
EXPECT_EQ(triggerInfo.want_->GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(triggerInfo.want_->GetElement().GetAbilityName(), "abilityName");
EXPECT_EQ(triggerInfo.resultCode_, resultCode);
}
/*
* @tc.number : TriggerInfo_0300
* @tc.name : TriggerInfo Constructors
* @tc.desc : 1.copy Constructors
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0300, Function | MediumTest | Level1)
{
std::string permission = "nihao";
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
int resultCode = 10;
TriggerInfo triggerInfo(permission, wParams, want, resultCode);
EXPECT_EQ(triggerInfo.permission_, permission);
EXPECT_NE(triggerInfo.extraInfo_, wParams);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(triggerInfo.extraInfo_->GetParam(key))), value);
EXPECT_NE(triggerInfo.want_, want);
EXPECT_EQ(triggerInfo.want_->GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(triggerInfo.want_->GetElement().GetAbilityName(), "abilityName");
EXPECT_EQ(triggerInfo.resultCode_, resultCode);
TriggerInfo triggerInfo1(triggerInfo);
EXPECT_EQ(triggerInfo1.permission_, triggerInfo.permission_);
EXPECT_NE(triggerInfo1.extraInfo_, triggerInfo.extraInfo_);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(triggerInfo1.extraInfo_->GetParam(key))), value);
EXPECT_NE(triggerInfo1.want_, triggerInfo.want_);
EXPECT_EQ(triggerInfo1.want_->GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(triggerInfo1.want_->GetElement().GetAbilityName(), "abilityName");
EXPECT_EQ(triggerInfo1.resultCode_, triggerInfo.resultCode_);
}
/*
* @tc.number : TriggerInfo_0400
* @tc.name : TriggerInfo::Builder SetPermission
* @tc.desc : 1.SetPermission
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0400, Function | MediumTest | Level1)
{
std::shared_ptr<TriggerInfo::Builder> builder = std::make_shared<TriggerInfo::Builder>();
builder->SetPermission("nihao");
EXPECT_EQ(builder->permission_, "nihao");
}
/*
* @tc.number : TriggerInfo_0500
* @tc.name : TriggerInfo::Builder SetWantParams
* @tc.desc : 1.SetWantParams
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0500, Function | MediumTest | Level1)
{
std::shared_ptr<TriggerInfo::Builder> builder = std::make_shared<TriggerInfo::Builder>();
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
builder->SetWantParams(wParams);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(builder->params_->GetParam(key))), value);
}
/*
* @tc.number : TriggerInfo_0600
* @tc.name : TriggerInfo::Builder SetWant
* @tc.desc : 1.SetWant
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0600, Function | MediumTest | Level1)
{
std::shared_ptr<TriggerInfo::Builder> builder = std::make_shared<TriggerInfo::Builder>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
builder->SetWant(want);
EXPECT_EQ(builder->want_->GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(builder->want_->GetElement().GetAbilityName(), "abilityName");
}
/*
* @tc.number : TriggerInfo_0700
* @tc.name : TriggerInfo::Builder SetResultCode
* @tc.desc : 1.SetResultCode
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0700, Function | MediumTest | Level1)
{
std::shared_ptr<TriggerInfo::Builder> builder = std::make_shared<TriggerInfo::Builder>();
int resultCode = 100;
builder->SetResultCode(resultCode);
EXPECT_EQ(builder->resultCode_, resultCode);
}
/*
* @tc.number : TriggerInfo_0800
* @tc.name : TriggerInfo::Builder Build
* @tc.desc : 1.Build,Great TriggerInfo object
*/
HWTEST_F(TriggerInfoTest, TriggerInfo_0800, Function | MediumTest | Level1)
{
std::shared_ptr<TriggerInfo::Builder> builder = std::make_shared<TriggerInfo::Builder>();
builder->SetPermission("nihao");
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
builder->SetWantParams(wParams);
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
builder->SetWant(want);
int resultCode = 100;
builder->SetResultCode(resultCode);
auto info = builder->Build();
EXPECT_NE(info, nullptr);
EXPECT_EQ(info->permission_, "nihao");
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(info->extraInfo_->GetParam(key))), value);
EXPECT_EQ(info->want_->GetElement().GetBundleName(), "bundleName");
EXPECT_EQ(info->want_->GetElement().GetAbilityName(), "abilityName");
EXPECT_EQ(info->resultCode_, resultCode);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,65 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("want_agent_helper_test") {
module_out_path = module_output_path
include_dirs = [
"//aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context/",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime",
"//foundation/aafwk/standard/services/abilitymgr/include",
]
sources = [ "want_agent_helper_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":want_agent_helper_test" ]
}

View File

@ -0,0 +1,711 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "ability_context.h"
#include "completed_callback.h"
#include "context_container.h"
#include "context_impl.h"
#include "context/context.h"
#include "element_name.h"
#include "event_handler.h"
#include "ohos/aafwk/base/base_types.h"
#include "pending_want.h"
#include "want.h"
#include "want_agent.h"
#include "want_agent_constant.h"
#define private public
#define protected public
#include "want_agent_helper.h"
#include "want_agent_info.h"
#undef private
#undef protected
#include "want_params.h"
#include "want_receiver_stub.h"
#include "want_sender_stub.h"
#include "ohos/aafwk/base/bool_wrapper.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class WantAgentHelperTest : public testing::Test {
public:
WantAgentHelperTest()
{}
~WantAgentHelperTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
class WantSender : public AAFwk::WantSenderStub {
public:
void Send(SenderInfo &senderInfo) override;
};
};
void WantAgentHelperTest::WantSender::Send(SenderInfo &senderInfo)
{}
void WantAgentHelperTest::SetUpTestCase(void)
{}
void WantAgentHelperTest::TearDownTestCase(void)
{}
void WantAgentHelperTest::SetUp(void)
{}
void WantAgentHelperTest::TearDown(void)
{}
/*
* @tc.number : WantAgentHelper_0100
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::ONE_TIME_FLAG
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0100, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::ONE_TIME_FLAG);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0x40000000));
}
/*
* @tc.number : WantAgentHelper_0200
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::NO_BUILD_FLAG
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0200, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::NO_BUILD_FLAG);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0x20000000));
}
/*
* @tc.number : WantAgentHelper_0300
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::CANCEL_PRESENT_FLAG
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0300, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::CANCEL_PRESENT_FLAG);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0x10000000));
}
/*
* @tc.number : WantAgentHelper_0400
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::UPDATE_PRESENT_FLAG
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0400, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0x8000000));
}
/*
* @tc.number : WantAgentHelper_0500
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::CONSTANT_FLAG
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0500, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0x4000000));
}
/*
* @tc.number : WantAgentHelper_0600
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::REPLACE_BUNDLE
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0600, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::REPLACE_BUNDLE);
EXPECT_EQ(
wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(WantAgentConstant::Flags::ONE_TIME_FLAG));
}
/*
* @tc.number : WantAgentHelper_0700
* @tc.name : WantAgentHelper Constructors
* @tc.desc : 1.WantAgentConstant::Flags::CONSTANT_FLAG | UPDATE_PRESENT_FLAG | REPLACE_BUNDLE
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0700, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::vector<WantAgentConstant::Flags> flags;
flags.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
flags.emplace_back(WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
flags.emplace_back(WantAgentConstant::Flags::REPLACE_BUNDLE);
EXPECT_EQ(wantAgentHelper->FlagsTransformer(flags), static_cast<unsigned int>(0xc000000));
}
/*
* @tc.number : WantAgentHelper_0800
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0800, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
WantAgentInfo wantAgentInfo;
auto wantAgent = wantAgentHelper->GetWantAgent(nullptr, wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_0900
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* ` 2.wantAgentInfo.wants_ empty
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_0900, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.clear();
auto wantAgent = wantAgentHelper->GetWantAgent(nullptr, wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1000
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() != wantAgentInfo.flags_.siez()
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1000, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = OHOS::AbilityRuntime::Context::GetApplicationContext();
WantAgentInfo wantAgentInfo;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.clear();
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1100
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return 0
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1100, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = OHOS::AbilityRuntime::Context::GetApplicationContext();
WantAgentInfo wantAgentInfo;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::REPLACE_BUNDLE);
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1200
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is WantAgentConstant::OperationType::START_ABILITY
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1200, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = WantAgentConstant::OperationType::START_ABILITY;
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_NE(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1300
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is WantAgentConstant::OperationType::START_ABILITIES
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1300, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = WantAgentConstant::OperationType::START_ABILITIES;
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_NE(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1400
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is WantAgentConstant::OperationType::START_SERVICE
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1400, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = WantAgentConstant::OperationType::START_SERVICE;
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_NE(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1500
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is WantAgentConstant::OperationType::START_FOREGROUND_SERVICE
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1500, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE;
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_NE(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1600
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is WantAgentConstant::OperationType::START_FOREGROUND_SERVICE
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1600, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = std::make_shared<AbilityRuntime::ContextImpl>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = WantAgentConstant::OperationType::SEND_COMMON_EVENT;
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_NE(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1700
* @tc.name : WantAgentHelper GetWantAgent
* @tc.desc : 1.GetWantAgent context is not nullptr
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.FlagsTransformer return ok
* 4.Type is 100
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1700, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<AbilityRuntime::Context> context = OHOS::AbilityRuntime::Context::GetApplicationContext();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(context, wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1800
* @tc.name : WantAgentHelper GetWantAgent(const WantAgentInfo &paramsInfo)
* @tc.desc : 1.wantAgentInfo.wants_.siez() == 0
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1800, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.clear();
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_1900
* @tc.name : WantAgentHelper GetWantAgent(const WantAgentInfo &paramsInfo)
* @tc.desc : 1.wantAgentInfo.wants_ not empty
* 2.wantAgentInfo.wants_.siez() != wantAgentInfo.flags_.siez()
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_1900, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.clear();
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_2000
* @tc.name : WantAgentHelper GetWantAgent(const WantAgentInfo &paramsInfo)
* @tc.desc : 1.wantAgentInfo.wants_ not empty
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.wantAgentInfo.wants_[0] is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2000, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(nullptr);
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_2100
* @tc.name : WantAgentHelper GetWantAgent(const WantAgentInfo &paramsInfo)
* @tc.desc : 1.wantAgentInfo.wants_ not empty
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.wantAgentInfo.wants_[0] is not nullptr
* 4.wantAgentInfo.extraInfo_ is not nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2100, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
bool value = true;
std::string key = "key";
std::shared_ptr<WantParams> wParams = std::make_shared<WantParams>();
wParams->SetParam(key, Boolean::Box(value));
wantAgentInfo.extraInfo_ = wParams;
auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_2200
* @tc.name : WantAgentHelper GetWantAgent(const WantAgentInfo &paramsInfo)
* @tc.desc : 1.wantAgentInfo.wants_ not empty
* 2.wantAgentInfo.wants_.siez() == wantAgentInfo.flags_.siez()
* 3.wantAgentInfo.wants_[0] is not nullptr
* 4.wantAgentInfo.extraInfo_ is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2200, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
WantAgentInfo wantAgentInfo;
wantAgentInfo.wants_.emplace_back(want);
wantAgentInfo.flags_.emplace_back(WantAgentConstant::Flags::CONSTANT_FLAG);
wantAgentInfo.operationType_ = static_cast<WantAgentConstant::OperationType>(100);
wantAgentInfo.requestCode_ = 10;
wantAgentInfo.extraInfo_ = nullptr;
auto wantAgent = wantAgentHelper->GetWantAgent(wantAgentInfo);
EXPECT_EQ(wantAgent, nullptr);
}
/*
* @tc.number : WantAgentHelper_2300
* @tc.name : WantAgentHelper GetType
* @tc.desc : 1.agent is not nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2300, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
WantAgentInfo wantAgentInfo;
auto wantAgent = wantAgentHelper->GetWantAgent(nullptr, wantAgentInfo);
auto type = wantAgentHelper->GetType(wantAgent);
EXPECT_EQ(type, WantAgentConstant::OperationType::UNKNOWN_TYPE);
}
/*
* @tc.number : WantAgentHelper_2400
* @tc.name : WantAgentHelper GetType
* @tc.desc : 1.agent is not nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2400, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
auto type = wantAgentHelper->GetType(wantAgent);
EXPECT_EQ(type, WantAgentConstant::OperationType::UNKNOWN_TYPE);
}
/*
* @tc.number : WantAgentHelper_2500
* @tc.name : WantAgentHelper GetType
* @tc.desc : 1.agent is not nullptr,PendingWant is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2500, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
auto type = wantAgentHelper->GetType(wantAgent);
EXPECT_EQ(type, WantAgentConstant::OperationType::UNKNOWN_TYPE);
}
/*
* @tc.number : WantAgentHelper_2600
* @tc.name : WantAgentHelper JudgeEquality
* @tc.desc : 1.JudgeEquality
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2600, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
std::shared_ptr<WantAgent> wantAgent2(nullptr);
auto isEqual = wantAgentHelper->JudgeEquality(wantAgent, wantAgent2);
EXPECT_EQ(isEqual, true);
}
/*
* @tc.number : WantAgentHelper_2700
* @tc.name : WantAgentHelper JudgeEquality
* @tc.desc : 1.JudgeEquality,Judge whether it is equal or not
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2700, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
sptr<IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
std::shared_ptr<WantAgent> wantAgent2(nullptr);
auto isEqual = wantAgentHelper->JudgeEquality(wantAgent, wantAgent2);
EXPECT_EQ(isEqual, false);
}
/*
* @tc.number : WantAgentHelper_2800
* @tc.name : WantAgentHelper JudgeEquality
* @tc.desc : 1.JudgeEquality,Judge whether it is equal or not
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2800, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
sptr<IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
std::shared_ptr<WantAgent> wantAgent2 = std::make_shared<WantAgent>(pendingWant);
auto isEqual = wantAgentHelper->JudgeEquality(wantAgent, wantAgent2);
EXPECT_EQ(isEqual, true);
}
/*
* @tc.number : WantAgentHelper_2900
* @tc.name : WantAgentHelper JudgeEquality
* @tc.desc : 1.JudgeEquality,Judge whether it is equal or not
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_2900, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
sptr<IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<PendingWant> pendingWant2(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
std::shared_ptr<WantAgent> wantAgent2 = std::make_shared<WantAgent>(pendingWant2);
auto isEqual = wantAgentHelper->JudgeEquality(wantAgent, wantAgent2);
EXPECT_EQ(isEqual, false);
}
/*
* @tc.number : WantAgentHelper_3000
* @tc.name : WantAgentHelper GetHashCode
* @tc.desc : 1.GetHashCode WantAgent is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3000, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
auto hashCode = wantAgentHelper->GetHashCode(wantAgent);
EXPECT_EQ(hashCode, 0);
}
/*
* @tc.number : WantAgentHelper_3100
* @tc.name : WantAgentHelper GetHashCode
* @tc.desc : 1.GetHashCode WantAgent.PendingWant.target is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3100, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
auto hashCode = wantAgentHelper->GetHashCode(wantAgent);
EXPECT_EQ(hashCode, 0);
}
/*
* @tc.number : WantAgentHelper_3200
* @tc.name : WantAgentHelper GetBundleName
* @tc.desc : 1.GetBundleName WantAgent is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3200, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
auto bundleName = wantAgentHelper->GetBundleName(wantAgent);
EXPECT_EQ(bundleName, "");
}
/*
* @tc.number : WantAgentHelper_3300
* @tc.name : WantAgentHelper GetBundleName
* @tc.desc : 1.GetBundleName WantAgent.PendingWant.target is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3300, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
auto bundleName = wantAgentHelper->GetBundleName(wantAgent);
EXPECT_EQ(bundleName, "");
}
/*
* @tc.number : WantAgentHelper_3400
* @tc.name : WantAgentHelper GetUid
* @tc.desc : 1.GetUid WantAgent is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3400, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
auto uid = wantAgentHelper->GetUid(wantAgent);
EXPECT_EQ(uid, -1);
}
/*
* @tc.number : WantAgentHelper_3500
* @tc.name : WantAgentHelper GetUid
* @tc.desc : 1.GetUid WantAgent.PendingWant.target is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3500, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
auto uid = wantAgentHelper->GetUid(wantAgent);
EXPECT_EQ(uid, -1);
}
/*
* @tc.number : WantAgentHelper_3600
* @tc.name : WantAgentHelper GetWant
* @tc.desc : 1.GetWant WantAgent is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3600, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<WantAgent> wantAgent(nullptr);
auto want = wantAgentHelper->GetWant(wantAgent);
EXPECT_EQ(want, nullptr);
}
/*
* @tc.number : WantAgentHelper_3700
* @tc.name : WantAgentHelper GetWant
* @tc.desc : 1.GetWant WantAgent.PendingWant.target is nullptr
*/
HWTEST_F(WantAgentHelperTest, WantAgentHelper_3700, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgentHelper> wantAgentHelper = std::make_shared<WantAgentHelper>();
std::shared_ptr<PendingWant> pendingWant(nullptr);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
auto want = wantAgentHelper->GetWant(wantAgent);
EXPECT_EQ(want, nullptr);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,60 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("want_agent_info_test") {
module_out_path = module_output_path
include_dirs = [ "//foundation/aafwk/standard/services/abilitymgr/include" ]
sources = [ "want_agent_info_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":want_agent_info_test" ]
}

View File

@ -0,0 +1,322 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "ability_context.h"
#include "completed_callback.h"
#include "context_container.h"
#include "element_name.h"
#include "event_handler.h"
#include "ohos/aafwk/base/base_types.h"
#include "pending_want.h"
#include "want.h"
#include "want_agent.h"
#include "want_agent_constant.h"
#include "want_agent_helper.h"
#define private public
#define protected public
#include "want_agent_info.h"
#undef private
#undef protected
#include "want_params.h"
#include "want_receiver_stub.h"
#include "want_sender_stub.h"
#include "ohos/aafwk/base/bool_wrapper.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class WantAgentInfoTest : public testing::Test {
public:
WantAgentInfoTest()
{}
~WantAgentInfoTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
class WantSender : public AAFwk::WantSenderStub {
public:
void Send(SenderInfo &senderInfo) override;
};
};
void WantAgentInfoTest::WantSender::Send(SenderInfo &senderInfo)
{}
void WantAgentInfoTest::SetUpTestCase(void)
{}
void WantAgentInfoTest::TearDownTestCase(void)
{}
void WantAgentInfoTest::SetUp(void)
{}
void WantAgentInfoTest::TearDown(void)
{}
/*
* @tc.number : WantAgentInfo_0100
* @tc.name : WantAgentInfo Constructors
* @tc.desc : 1.Constructors
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0100, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flag, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
}
/*
* @tc.number : WantAgentInfo_0200
* @tc.name : WantAgentInfo Constructors
* @tc.desc : 1.Constructors
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0200, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
}
/*
* @tc.number : WantAgentInfo_0300
* @tc.name : WantAgentInfo Constructors
* @tc.desc : 1.Copy Constructors
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0300, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
WantAgentInfo wantAgentInfo1(wantAgentInfo);
EXPECT_EQ(wantAgentInfo->GetRequestCode(), wantAgentInfo1.GetRequestCode());
EXPECT_EQ(wantAgentInfo->GetOperationType(), wantAgentInfo1.GetOperationType());
EXPECT_EQ(wantAgentInfo->GetFlags().at(0), wantAgentInfo1.GetFlags().at(0));
EXPECT_NE(wantAgentInfo->GetWants().at(0), wantAgentInfo1.GetWants().at(0));
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(),
wantAgentInfo1.GetWants().at(0)->GetElement().GetBundleName());
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(),
wantAgentInfo1.GetWants().at(0)->GetElement().GetAbilityName());
EXPECT_NE(wantAgentInfo->GetExtraInfo(), wantAgentInfo1.GetExtraInfo());
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
Boolean::Unbox(IBoolean::Query(wantAgentInfo1.GetExtraInfo()->GetParam(key))));
}
/*
* @tc.number : WantAgentInfo_0400
* @tc.name : WantAgentInfo GetRequestCode
* @tc.desc : 1.GetRequestCode
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0400, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
}
/*
* @tc.number : WantAgentInfo_0500
* @tc.name : WantAgentInfo GetOperationType
* @tc.desc : 1.GetOperationType
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0500, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
}
/*
* @tc.number : WantAgentInfo_0600
* @tc.name : WantAgentInfo GetFlags
* @tc.desc : 1.GetFlags
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0600, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
}
/*
* @tc.number : WantAgentInfo_0700
* @tc.name : WantAgentInfo GetWants
* @tc.desc : 1.GetWants
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0700, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
}
/*
* @tc.number : WantAgentInfo_0800
* @tc.name : WantAgentInfo GetWants
* @tc.desc : 1.GetWants
*/
HWTEST_F(WantAgentInfoTest, WantAgentInfo_0800, Function | MediumTest | Level1)
{
int requestCode = 10;
WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
std::vector<WantAgentConstant::Flags> flags;
WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
flags.emplace_back(flag);
std::vector<std::shared_ptr<Want>> wants;
std::shared_ptr<Want> want = std::make_shared<Want>();
ElementName element("device", "bundleName", "abilityName");
want->SetElement(element);
wants.emplace_back(want);
std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
bool value = true;
std::string key = "key";
extraInfo->SetParam(key, Boolean::Box(value));
std::shared_ptr<WantAgentInfo> wantAgentInfo =
std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,60 @@
# Copyright (c) 2021 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("//base/notification/ans_standard/notification.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "ans_standard/wantagent"
ohos_unittest("want_agent_test") {
module_out_path = module_output_path
include_dirs = [ "//foundation/aafwk/standard/services/abilitymgr/include" ]
sources = [ "want_agent_test.cpp" ]
configs = [ "//utils/native/base:utils_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${core_path}:ans_core",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"ces_standard:cesfwk_core",
"dmsfwk_standard:zuri",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"samgr_standard:samgr_proxy",
]
}
group("unittest") {
testonly = true
deps = [ ":want_agent_test" ]
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2021 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 <gtest/gtest.h>
#include "ability_context.h"
#include "completed_callback.h"
#include "context_container.h"
#include "element_name.h"
#include "event_handler.h"
#include "ohos/aafwk/base/base_types.h"
#include "pending_want.h"
#include "want.h"
#define private public
#define protected public
#include "want_agent.h"
#undef private
#undef protected
#include "want_agent_constant.h"
#include "want_agent_helper.h"
#include "want_agent_info.h"
#include "want_params.h"
#include "want_receiver_stub.h"
#include "want_sender_stub.h"
using namespace testing::ext;
using namespace OHOS::AAFwk;
using namespace OHOS;
using OHOS::AppExecFwk::ElementName;
using namespace OHOS::AppExecFwk;
using namespace OHOS::AbilityRuntime::WantAgent;
using vector_str = std::vector<std::string>;
namespace OHOS::AbilityRuntime::WantAgent {
class WantAgentTest : public testing::Test {
public:
WantAgentTest()
{}
~WantAgentTest()
{}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
class WantSender : public AAFwk::WantSenderStub {
public:
void Send(SenderInfo &senderInfo) override;
};
};
void WantAgentTest::WantSender::Send(SenderInfo &senderInfo)
{}
void WantAgentTest::SetUpTestCase(void)
{}
void WantAgentTest::TearDownTestCase(void)
{}
void WantAgentTest::SetUp(void)
{}
void WantAgentTest::TearDown(void)
{}
/*
* @tc.number : WantAgent_0100
* @tc.name : WantAgentInfo Constructors
* @tc.desc : 1.Constructors and GetPendingWant
*/
HWTEST_F(WantAgentTest, WantAgent_0100, Function | MediumTest | Level1)
{
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(nullptr);
EXPECT_EQ(wantAgent->GetPendingWant(), nullptr);
}
/*
* @tc.number : WantAgent_0200
* @tc.name : WantAgentInfo Constructors
* @tc.desc : 1.Constructors and GetPendingWant
*/
HWTEST_F(WantAgentTest, WantAgent_0200, Function | MediumTest | Level1)
{
sptr<IWantSender> target(new (std::nothrow) WantSender());
std::shared_ptr<PendingWant> pendingWant = std::make_shared<PendingWant>(target);
std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(pendingWant);
EXPECT_EQ(wantAgent->GetPendingWant(), pendingWant);
}
} // namespace OHOS::AbilityRuntime::WantAgent

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_CANCEL_LISTENER_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_CANCEL_LISTENER_H
namespace OHOS::AbilityRuntime::WantAgent {
class CancelListener {
public:
/**
* Called when a Cancel operation as completed.
*
* @param resultCode The final result code determined by the Send.
*/
virtual void OnCancelled(int resultCode) = 0;
virtual ~CancelListener() = default;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_CANCEL_LISTENER_H

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_CALLBACK_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_CALLBACK_H
#include <string>
#include <memory>
#include "want.h"
#include "want_params.h"
namespace OHOS::AbilityRuntime::WantAgent {
class CompletedCallback {
public:
virtual ~CompletedCallback() = default;
/**
* Called when a Send operation as completed.
*
* @param want The original Want that was sent.
* @param resultCode The final result code determined by the Send.
* @param resultData The final data collected by a broadcast.
* @param resultExtras The final extras collected by a broadcast.
*/
virtual void OnSendFinished(const AAFwk::Want &want, int resultCode, const std::string &resultData,
const AAFwk::WantParams &resultExtras) = 0;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_CALLBACK_H

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_DISPATCHER_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_DISPATCHER_H
#include <string>
#include <memory>
#include "completed_callback.h"
#include "event_handler.h"
#include "want.h"
#include "want_params.h"
#include "want_receiver_stub.h"
namespace OHOS::AbilityRuntime::WantAgent {
class PendingWant;
class CompletedDispatcher : public AAFwk::WantReceiverStub {
public:
CompletedDispatcher(const std::shared_ptr<PendingWant> &pendingWant,
const std::shared_ptr<CompletedCallback> &callback, const std::shared_ptr<AppExecFwk::EventHandler> &handler);
virtual ~CompletedDispatcher() = default;
void Send(const int32_t resultCode) override;
void PerformReceive(const AAFwk::Want &want, int resultCode, const std::string &data,
const AAFwk::WantParams &extras, bool serialized, bool sticky, int sendingUser) override;
void Run();
private:
const std::shared_ptr<PendingWant> pendingWant_;
const std::shared_ptr<CompletedCallback> callback_;
const std::shared_ptr<AppExecFwk::EventHandler> handler_;
AAFwk::Want want_;
int resultCode_ = 0;
std::string resultData_;
AAFwk::WantParams resultExtras_;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_COMPLETED_DISPATCHER_H

View File

@ -0,0 +1,281 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_PENDING_WANT_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_PENDING_WANT_H
#include <string>
#include <vector>
#include <mutex>
#include <memory>
#include "cancel_listener.h"
#include "context/context.h"
#include "completed_dispatcher.h"
#include "event_handler.h"
#include "want.h"
#include "want_agent_constant.h"
#include "want_params.h"
#include "want_receiver_stub.h"
#include "want_sender_info.h"
#include "want_sender_stub.h"
namespace OHOS::AbilityRuntime::WantAgent {
class PendingWant final : public std::enable_shared_from_this<PendingWant>, public Parcelable {
public:
PendingWant() {};
PendingWant(const sptr<AAFwk::IWantSender> &target);
PendingWant(const sptr<AAFwk::IWantSender> &target, const sptr<IRemoteObject> whitelistToken);
virtual ~PendingWant() = default;
WantAgentConstant::OperationType GetType(const sptr<AAFwk::IWantSender> &target);
/**
* Retrieve a PendingWant that will start a new ability.
*
* @param context The Context in which this PendingWant should start
* the ability.
* @param requestCode Private request code for the sender
* @param want Want of the ability to be launched.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE},
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetAbility(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags);
/**
* Retrieve a PendingWant that will start a new ability
*
* @param context The Context in which this PendingWant should start
* the ability.
* @param requestCode Private request code for the sender
* @param want Want of the ability to be launched.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT.
* @param options Additional options for how the ability should be started.
* May be null if there are no options.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetAbility(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want,
unsigned int flags, const std::shared_ptr<AAFwk::WantParams> &options);
/**
* Like GetAbility(Context, int, Want, int)}, but allows an
* array of Wants to be supplied. The last Want in the array is
* taken as the primary key for the PendingWant, like the single Want
* given to GetAbility(Context, int, Want, int).
*
* @param context The Context in which this PendingWant should start
* the ability.
* @param requestCode Private request code for the sender
* @param wants Array of Wants of the abilities to be launched.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetAbilities(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, std::vector<std::shared_ptr<AAFwk::Want>> &wants, unsigned int flags);
/**
* Like GetAbility(Context, int, Want, int)}, but allows an
* array of Wants to be supplied. The last Want in the array is
* taken as the primary key for the PendingWant, like the single Want
* given to GetAbility(Context, int, Want, int).
*
* @param context The Context in which this PendingWant should start
* the ability.
* @param requestCode Private request code for the sender
* @param wants Array of Wants of the abilities to be launched.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT} link #FLAG_UPDATE_CURRENT,
* FLAG_IMMUTABLE.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetAbilities(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, std::vector<std::shared_ptr<AAFwk::Want>> &wants, unsigned int flags,
const std::shared_ptr<AAFwk::WantParams> &options);
/**
* Retrieve a PendingWant that will perform a common event.
*
* @param context The Context in which this PendingWant should perform
* the common event.
* @param requestCode Private request code for the sender
* @param want The Want to be common event.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,
* FLAG_IMMUTABLE.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetCommonEvent(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags);
/**
* Note that current user will be interpreted at the time the
* common event is sent, not when the pending want is created.
*/
static std::shared_ptr<PendingWant> GetCommonEventAsUser(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, int uid);
/**
* Retrieve a PendingWant that will start a service.
*
* @param context The Context in which this PendingWant should start
* the service.
* @param requestCode Private request code for the sender
* @param want An Want describing the service to be started.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,
* FLAG_IMMUTABLE.
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetService(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags);
/**
* Retrieve a PendingWant that will start a foreground service.
*
* @param context The Context in which this PendingWant should start
* the service.
* @param requestCode Private request code for the sender
* @param want An Want describing the service to be started.
* @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE,
* FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,
* FLAG_IMMUTABLE .
*
* @return Returns an existing or new PendingWant matching the given
* parameters. May return null only if FLAG_NO_CREATE has been
* supplied.
*/
static std::shared_ptr<PendingWant> GetForegroundService(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags);
/**
* @description: Marshals a Want into a Parcel.
* Fields in the Want are marshalled separately. If any field fails to be marshalled, false is returned.
* @param parcel Indicates the Parcel object for marshalling.
* @return Returns true if the marshalling is successful; returns false otherwise.
*/
virtual bool Marshalling(Parcel &parcel) const;
/**
* @description: Unmarshals a Want from a Parcel.
* Fields in the Want are unmarshalled separately. If any field fails to be unmarshalled, false is returned.
* @param parcel Indicates the Parcel object for unmarshalling.
* @return Returns true if the unmarshalling is successful; returns false otherwise.
*/
static PendingWant *Unmarshalling(Parcel &parcel);
static bool Equals(
const std::shared_ptr<PendingWant> &targetPendingWant, const std::shared_ptr<PendingWant> &otherPendingWant);
void Cancel(const sptr<AAFwk::IWantSender> &target);
void Send(const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode, const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode,
const std::shared_ptr<AAFwk::Want> &want, const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode, const sptr<CompletedDispatcher> &onCompleted, const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode,
const std::shared_ptr<AAFwk::Want> &want, const sptr<CompletedDispatcher> &onCompleted,
const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode,
const std::shared_ptr<AAFwk::Want> &want, const sptr<CompletedDispatcher> &onCompleted,
const std::string &requiredPermission, const sptr<AAFwk::IWantSender> &target);
void Send(int resultCode, const std::shared_ptr<AAFwk::Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission,
const std::shared_ptr<AAFwk::WantParams> &options, const sptr<AAFwk::IWantSender> &target);
int SendAndReturnResult(int resultCode, const std::shared_ptr<AAFwk::Want> &want,
const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission,
const std::shared_ptr<AAFwk::WantParams> &options, const sptr<AAFwk::IWantSender> &target);
std::string GetBundleName(const sptr<AAFwk::IWantSender> &target);
int GetUid(const sptr<AAFwk::IWantSender> &target);
sptr<AAFwk::IWantSender> GetTarget();
void SetTarget(const sptr<AAFwk::IWantSender> &target);
void RegisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target);
void NotifyCancelListeners(int32_t resultCode);
void UnregisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target);
int GetHashCode(const sptr<AAFwk::IWantSender> &target);
std::shared_ptr<AAFwk::Want> GetWant(const sptr<AAFwk::IWantSender> &target);
std::shared_ptr<AAFwk::WantSenderInfo> GetWantSenderInfo(const sptr<AAFwk::IWantSender> &target);
private:
std::mutex lock_object;
sptr<AAFwk::IWantSender> target_;
sptr<AAFwk::IWantReceiver> cancelReceiver_;
sptr<IRemoteObject> whitelistToken_;
std::vector<std::shared_ptr<CancelListener>> cancelListeners_;
class CancelReceiver : public AAFwk::WantReceiverStub {
public:
explicit CancelReceiver(const std::weak_ptr<PendingWant> &outerInstance);
virtual ~CancelReceiver() = default;
void Send(const int32_t resultCode) override;
void PerformReceive(const AAFwk::Want &want, int resultCode, const std::string &data,
const AAFwk::WantParams &extras, bool serialized, bool sticky, int sendingUser) override;
private:
std::weak_ptr<PendingWant> outerInstance_;
};
static std::shared_ptr<PendingWant> BuildServicePendingWant(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags,
WantAgentConstant::OperationType serviceKind);
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_PENDING_WANT_H

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_TRIGGER_INFO_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_TRIGGER_INFO_H
#include <string>
#include <memory>
#include "want.h"
#include "want_params.h"
namespace OHOS::AbilityRuntime::WantAgent {
class TriggerInfo final : public std::enable_shared_from_this<TriggerInfo> {
public:
/**
* Default constructor used to create a {@code TriggerInfo} instance.
*
*/
TriggerInfo();
virtual ~TriggerInfo() = default;
/**
* A constructor used to create a {@code TriggerInfo} instance based on the input parameters.
*
* @param permission Indicates the permission required for an {@link WantAgent} recipient.
* This parameter is valid only when the {@link WantAgent} is triggered to send common events.
* @param extraInfo Indicates the custom extra data you want to add for triggering an {@link WantAgent}.
* @param want Indicates the extra {@link ohos.aafwk.content.Want}.
* If {@code flags} in {@link WantAgentInfo} contains {@link WantAgentConstant.Flags#CONSTANT_FLAG},
* this parameter is invalid. If flags contains {@link WantAgentConstant.Flags#REPLACE_ELEMENT},
* {@link WantAgentConstant.Flags#REPLACE_ACTION}, {@link WantAgentConstant.Flags#REPLACE_URI},
* {@link WantAgentConstant.Flags#REPLACE_ENTITIES}, and {@link WantAgentConstant.Flags#REPLACE_BUNDLE},
* the {@code element}, {@code action}, {@code uri}, {@code entities}, and {@code bundleName} attributes of the
* {@link ohos.aafwk.content.Want} specified in this parameter will be used to replace the
* corresponding attributes in the original {@link ohos.aafwk.content.Want}, respectively.
* If this parameter is null, the original {@link ohos.aafwk.content.Want} remains unchanged.
* @param code Indicates the result code provided for the target of the {@link WantAgent}.
*/
TriggerInfo(const std::string &permission, const std::shared_ptr<AAFwk::WantParams> &extraInfo,
const std::shared_ptr<AAFwk::Want> &want, int resultCode);
/**
* A constructor used to create a {@code TriggerInfo} instance by copying parameters from an existing one.
*
* @param paramInfo Indicates the existing {@code TriggerInfo} object.
*/
explicit TriggerInfo(const TriggerInfo &paramInfo);
/**
* A copy assignment operator used to create a {@code TriggerInfo} instance by copying parameters from an existing
* one.
*
* @param paramInfo Indicates the existing {@code TriggerInfo} object.
*/
const TriggerInfo &operator=(const TriggerInfo &paramInfo);
/**
* Obtains the permission from the current {@code TriggerInfo} object.
*
* @return Returns the permission name.
*/
std::string GetPermission() const;
/**
* Obtains the extra data from the {@code TriggerInfo} object.
*
* @return Returns the extra data.
*/
std::shared_ptr<AAFwk::WantParams> GetExtraInfo() const;
/**
* Obtains the {@link ohos.aafwk.content.Want} used for triggering an {@link WantAgent}.
*
* @return Returns an {@link ohos.aafwk.content.Want} object.
*/
std::shared_ptr<AAFwk::Want> GetWant() const;
/**
* Obtains the result code provided for the target of the {@link WantAgent}.
*
* @return Returns the result code provided for the target of the {@link WantAgent}.
*/
int GetResultCode() const;
/**
* A builder class for {@link TriggerInfo} objects.
*
*/
public:
class Builder final : public std::enable_shared_from_this<Builder> {
public:
/**
* Default constructor used to create a {@code Builder} instance.
*
*/
Builder();
virtual ~Builder() = default;
/**
* Sets the permission that the {@link WantAgent} recipient must have.
*
* @param permission Indicates the permission to set. This parameter is valid only when the {@link WantAgent}
* to trigger is intended to send a common event.
* @return Returns this {@code Builder} object with the specified permission.
*/
std::shared_ptr<Builder> SetPermission(const std::string &permission);
/**
* Sets custom data.
*
* @param params Indicates the custom data to set.
* @return Returns this {@code Builder} object with the custom data.
*/
std::shared_ptr<Builder> SetWantParams(const std::shared_ptr<AAFwk::WantParams> &params);
/**
* Sets a custom {@link ohos.aafwk.content.Want}.
*
* @param want Indicates the custom {@code Want} to set. If the member variable {@code flags} of the
* {@link WantAgentInfo} contains {@link WantAgentConstant.Flags#CONSTANT_FLAG}, this parameter does not
* take effect. If {@code flags} contains {@link WantAgentConstant.Flags#REPLACE_ELEMENT},
* {@link WantAgentConstant.Flags#REPLACE_ACTION}, {@link WantAgentConstant.Flags#REPLACE_URI},
* {@link WantAgentConstant.Flags#REPLACE_ENTITIES}, and {@link WantAgentConstant.Flags#REPLACE_BUNDLE},
* the {@code element}, {@code action}, {@code uri}, {@code entities}, and {@code bundleName} attributes of the
* {@link ohos.aafwk.content.Want} specified in this parameter will be used to replace the corresponding
* attributes in the original {@link ohos.aafwk.content.Want}, respectively. If this parameter is null, the
* original {@link ohos.aafwk.content.Want} remains unchanged.
* @return Returns this {@code Builder} object with the custom {@code Want}.
*/
std::shared_ptr<Builder> SetWant(const std::shared_ptr<AAFwk::Want> &want);
/**
* Sets the result code provided for the target of the {@link WantAgent}.
*
* @param code Indicates the result code provided for the target of the {@link WantAgent}.
* @return Returns this {@code Builder} object with the specified result code.
*/
std::shared_ptr<Builder> SetResultCode(int resultCode);
/**
* Creates a {@link TriggerInfo} object using all of the settings.
*
* @return Returns the created {@code TriggerInfo} object.
*/
std::shared_ptr<TriggerInfo> Build();
private:
std::string permission_;
std::shared_ptr<AAFwk::WantParams> params_;
std::shared_ptr<AAFwk::Want> want_;
int resultCode_ = 0;
};
private:
std::string permission_;
std::shared_ptr<AAFwk::WantParams> extraInfo_;
std::shared_ptr<AAFwk::Want> want_;
int resultCode_ = 0;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_TRIGGER_INFO_H

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_H
#include <string>
#include <memory>
#include "parcel.h"
#include "pending_want.h"
#include "want.h"
#include "want_params.h"
namespace OHOS::AbilityRuntime::WantAgent {
class WantAgent final : public std::enable_shared_from_this<WantAgent>, public Parcelable {
public:
WantAgent() {};
virtual ~WantAgent() = default;
/**
* Constructor.
*
* @param obj The proxy object.
*/
explicit WantAgent(const std::shared_ptr<PendingWant> &pendingWant);
/**
* Gets proxy obj.
*
* @return Return obj.
*/
std::shared_ptr<PendingWant> GetPendingWant();
/**
* Sets proxy obj.
*
* @param obj The proxy object.
*/
void SetPendingWant(const std::shared_ptr<PendingWant> &pendingWant);
/**
* @description: Marshals a Want into a Parcel.
* Fields in the Want are marshalled separately. If any field fails to be marshalled, false is returned.
* @param parcel Indicates the Parcel object for marshalling.
* @return Returns true if the marshalling is successful; returns false otherwise.
*/
virtual bool Marshalling(Parcel &parcel) const;
/**
* @description: Unmarshals a Want from a Parcel.
* Fields in the Want are unmarshalled separately. If any field fails to be unmarshalled, false is returned.
* @param parcel Indicates the Parcel object for unmarshalling.
* @return Returns true if the unmarshalling is successful; returns false otherwise.
*/
static WantAgent *Unmarshalling(Parcel &parcel);
private:
std::shared_ptr<PendingWant> pendingWant_;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_H

View File

@ -0,0 +1,133 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_CONSTANT_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_CONSTANT_H
namespace OHOS::AbilityRuntime::WantAgent {
/// <summary>
/// Provides enumerated constants that are used for setting object attributes in the methods provided by
/// <seealso cref="WantAgentInfo"/>.
///
/// </summary>
class WantAgentConstant final {
/// <summary>
/// Enumerates flags for using an <seealso cref="WantAgent"/>.
///
/// </summary>
public:
enum class Flags {
/// <summary>
/// Indicates that the <seealso cref="WantAgent"/> can be used only once.
/// </summary>
ONE_TIME_FLAG,
/// <summary>
/// Indicates that {@code null} is returned if the <seealso cref="WantAgent"/> does not exist.
/// </summary>
NO_BUILD_FLAG,
/// <summary>
/// Indicates that the existing <seealso cref="WantAgent"/> should be canceled before the new object is
/// generated.
/// </summary>
CANCEL_PRESENT_FLAG,
/// <summary>
/// Indicates that the system only replaces the extra data of the existing <seealso cref="WantAgent"/>
/// with that of the new object.
/// </summary>
UPDATE_PRESENT_FLAG,
/// <summary>
/// Indicates that the created <seealso cref="WantAgent"/> should be immutable.
/// </summary>
CONSTANT_FLAG,
/// <summary>
/// Indicates that the current value of {@code element} can be replaced
/// when the <seealso cref="WantAgent"/> is triggered.
/// </summary>
REPLACE_ELEMENT,
/// <summary>
/// Indicates that the current value of {@code action} can be replaced
/// when the <seealso cref="WantAgent"/> is triggered.
/// </summary>
REPLACE_ACTION,
/// <summary>
/// Indicates that the current value of {@code uri} can be replaced when the <seealso cref="WantAgent"/> is
/// triggered.
/// </summary>
REPLACE_URI,
/// <summary>
/// Indicates that the current value of {@code entities} can be replaced
/// when the <seealso cref="WantAgent"/> is triggered.
/// </summary>
REPLACE_ENTITIES,
/// <summary>
/// Indicates that the current value of {@code bundleName} can be replaced
/// when the <seealso cref="WantAgent"/> is triggered.
/// </summary>
REPLACE_BUNDLE
};
/// <summary>
/// Identifies the operation for using an <seealso cref="WantAgent"/>, such as starting an ability or sending a
/// common event.
///
/// </summary>
public:
enum class OperationType {
/// <summary>
/// Unknown operation.
/// </summary>
UNKNOWN_TYPE,
/// <summary>
/// Starts an ability with a UI.
/// </summary>
START_ABILITY,
/// <summary>
/// Starts multiple abilities.
/// </summary>
START_ABILITIES,
/// <summary>
/// Starts an ability without a UI.
/// </summary>
START_SERVICE,
/// <summary>
/// Sends a common event.
/// </summary>
SEND_COMMON_EVENT,
/// <summary>
/// Starts a foreground ability without a UI.
/// </summary>
START_FOREGROUND_SERVICE
};
private:
WantAgentConstant() {};
virtual ~WantAgentConstant() = default;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_CONSTANT_H

View File

@ -0,0 +1,199 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_HELPER_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_HELPER_H
#include <string>
#include <memory>
#include "context/context.h"
#include "completed_callback.h"
#include "completed_dispatcher.h"
#include "event_handler.h"
#include "nlohmann/json.hpp"
#include "trigger_info.h"
#include "want.h"
#include "want_agent.h"
#include "want_agent_info.h"
#include "want_params.h"
namespace OHOS::AbilityRuntime::WantAgent {
/**
* A helper class used to obtain, trigger, cancel, and compare WantAgent objects and to obtain
* the bundle name, UID, and hash code value of an WantAgent object.
*
*/
static const int FLAG_ONE_SHOT = 1 << 30;
static const int FLAG_NO_CREATE = 1 << 29;
static const int FLAG_CANCEL_CURRENT = 1 << 28;
static const int FLAG_UPDATE_CURRENT = 1 << 27;
static const int FLAG_IMMUTABLE = 1 << 26;
static const int INVLID_WANT_AGENT_USER_ID = -1;
class WantAgentHelper final : public std::enable_shared_from_this<WantAgentHelper> {
public:
/**
* Obtains an WantAgent object.
* The WantAgent class does not have any constructor, and you can only use this method to create an
* WantAgent object.
*
* @param context Indicates the context of the caller. This parameter cannot be null.
* @param paramsInfo Indicates the WantAgentInfo object that contains parameters of the
* WantAgent object to create.
* @return Returns the created WantAgent object.
*/
static std::shared_ptr<WantAgent> GetWantAgent(
const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const WantAgentInfo &paramsInfo);
/**
* Obtains an WantAgent object.
*
* The WantAgent class does not have any constructor, and you can only use this method to create an
* WantAgent object.
*
* @param paramsInfo Indicates the WantAgentInfo object that contains parameters of the
* WantAgent object to create.
* @param userId Indicates the user id for this wantagent info, default is INVLID_WANT_AGENT_USER_ID(-1).
* @return Returns the created WantAgent object.
*/
static std::shared_ptr<WantAgent> GetWantAgent(const WantAgentInfo &paramsInfo,
int32_t userId = INVLID_WANT_AGENT_USER_ID);
/**
* Obtains an WantAgent object operation type.
*
* @param agent Indicates the WantAgent to trigger.
* @return Returns the created WantAgent object.
*/
static WantAgentConstant::OperationType GetType(const std::shared_ptr<WantAgent> &agent);
/**
* Triggers an WantAgent.
*
* After this method is called, events associated with the specified WantAgent will be executed,
* such as starting an ability or sending a common event.
*
* @param context Indicates the context of the caller. This parameter cannot be null.
* @param agent Indicates the WantAgent to trigger.
* @param onCompleted Indicates the callback method to be called after the WantAgent is triggered.
* This parameter can be null.
* @param handler Indicates the thread for executing the callback indicated by OnCompleted.
* If this parameter is null, the callback method will be executed in a thread in the thread pool of
* the current process.
* @param paramsInfo Indicates the TriggerInfo object that contains triggering parameters.
*/
static void TriggerWantAgent(const std::shared_ptr<WantAgent> &agent,
const std::shared_ptr<CompletedCallback> &callback,
const TriggerInfo &paramsInfo);
/**
* Cancels an WantAgent.
*
* Only the application that creates the WantAgent can cancel it.
*
* @param agent Indicates the WantAgent to cancel.
*/
static void Cancel(const std::shared_ptr<WantAgent> &agent);
/**
* Checks whether two WantAgent objects are the same.
*
* @param agent Indicates one of the WantAgent object to compare.
* @param otherAgent Indicates the other WantAgent object to compare.
* @return Returns true If the two objects are the same; returns false otherwise.
*/
static bool JudgeEquality(const std::shared_ptr<WantAgent> &agent, const std::shared_ptr<WantAgent> &otherAgent);
/**
* Obtains the hash code value of an WantAgent.
*
* @param agent Indicates the antAgent whose hash code value is to be obtained.
* @return Returns the hash code value of the WantAgent.
*/
static int GetHashCode(const std::shared_ptr<WantAgent> &agent);
/**
* Obtains the bundle name of an WantAgent.
*
* @param agent Indicates the WantAgent whose bundle name is to be obtained.
* @return Returns the bundle name of the WantAgent if any; returns {@code null} otherwise.
*/
static std::string GetBundleName(const std::shared_ptr<WantAgent> &agent);
/**
* Obtains the user ID (UID) of an WantAgent.
*
* @param agent Indicates the WantAgent whose UID is to be obtained.
* @return Returns the UID of the WantAgent if any; returns -1} otherwise.
*/
static int GetUid(const std::shared_ptr<WantAgent> &agent);
/**
* Obtains the Want WantAgent.
*
* @param agent Indicates the WantAgent whose Want is to be obtained.
* @return Returns the Want of the WantAgent.
*/
static std::shared_ptr<AAFwk::Want> GetWant(const std::shared_ptr<WantAgent> &agent);
/**
* Register Cancel function Listener.
*
* @param cancelListener Register listener object.
* @param agent Indicates the WantAgent whose bundle name is to be obtained.
*/
static void RegisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const std::shared_ptr<WantAgent> &agent);
/**
* Unregister Cancel function Listener.
*
* @param cancelListener Register listener object.
* @param agent Indicates the WantAgent whose bundle name is to be obtained.
*/
static void UnregisterCancelListener(
const std::shared_ptr<CancelListener> &cancelListener, const std::shared_ptr<WantAgent> &agent);
/**
* Convert WantAgentInfo object to json string.
*
* @param jsonObject Json object.
* @return WantAgentInfo object's json string.
*/
static std::string ToString(const std::shared_ptr<WantAgent> &agent);
/**
* Convert json string to WantAgentInfo object.
*
* @param jsonString Json string.
* @return WantAgentInfo object.
*/
static std::shared_ptr<WantAgent> FromString(const std::string &jsonString);
private:
WantAgentHelper();
virtual ~WantAgentHelper() = default;
private:
static void Send(const std::shared_ptr<PendingWant> &pendingWant,
WantAgentConstant::OperationType type,
const sptr<CompletedDispatcher> &callBack,
const TriggerInfo &paramsInfo);
static unsigned int FlagsTransformer(const std::vector<WantAgentConstant::Flags> &flags);
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_HELPER_H

View File

@ -0,0 +1,124 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_INFO_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_INFO_H
#include <vector>
#include <memory>
#include "want.h"
#include "want_agent_constant.h"
#include "want_params.h"
namespace OHOS::AbilityRuntime::WantAgent {
/**
* A parametric class that contains the parameters required by WantAgentHelper GetWantAgent.
*
* This class is used to encapsulate parameters requestCode, operationType,
* flag, Wants, and extraInfo. It is used as the input parameter for
* the WantAgentHelper GetWantAgent method.
*
*/
class WantAgentInfo final : public std::enable_shared_from_this<WantAgentInfo> {
public:
/**
* Default constructor used to create an empty WantAgentInfo instance.
*
*/
WantAgentInfo();
virtual ~WantAgentInfo() = default;
/**
* A constructor used to create an WantAgentInfo instance based on the input parameters.
*
* @param requestCode Indicates the request code to set. It is a private value defined by the user.
* @param operationType Indicates the type of the operation to be performed by the WantAgent object.
* For details about the value range, see WantAgentConstant.OperationType.
* @param flag Indicates the flag for handling the WantAgent.
* For details about the value range, see WantAgentConstant.Flags.
* @param Wants Indicates the collection of Want objects to be used for creating the WantAgent
* object. The number of Wants in the collection is determined by WantAgentConstant.OperationType.
* @param extraInfo Indicates the extra information to be used for creating the WantAgent object.
*/
WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType, WantAgentConstant::Flags flag,
std::vector<std::shared_ptr<AAFwk::Want>> &Wants, const std::shared_ptr<AAFwk::WantParams> &extraInfo);
/**
* A constructor used to create an WantAgentInfo instance based on the input parameters.
*
* @param requestCode Indicates the request code to set. It is a private value defined by the user.
* @param operationType Indicates the type of the operation to be performed by the WantAgent object.
* For details about the value range, see WantAgentConstant.OperationType.
* @param flags Indicates the flags for handling the WantAgent.
* For details about the value range, see WantAgentConstant.Flags.
* @param Wants Indicates the collection of Want objects to be used for creating the WantAgent
* object. The number of Wants in the collection is determined by WantAgentConstant.OperationType.
* @param extraInfo Indicates the extra information to be used for creating the WantAgent object.
*/
WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType,
const std::vector<WantAgentConstant::Flags> &flags, std::vector<std::shared_ptr<AAFwk::Want>> &Wants,
const std::shared_ptr<AAFwk::WantParams> &extraInfo);
/**
* A constructor used to create an WantAgentInfo instance by copying parameters from an existing one.
*
* @param paramInfo Indicates the existing WantAgentInfo object.
*/
explicit WantAgentInfo(const std::shared_ptr<WantAgentInfo> &paramInfo);
/**
* Obtains the requestCode of the WantAgent object.
*
* @return Returns the requestCode of the WantAgent object.
*/
int GetRequestCode() const;
/**
* Obtains the operationType of the WantAgent object.
*
* @return Returns the operationType of the WantAgent object.
*/
WantAgentConstant::OperationType GetOperationType() const;
/**
* Obtains the flag of the WantAgent object.
*
* @return Returns the flag of the WantAgent object.
*/
std::vector<WantAgentConstant::Flags> GetFlags() const;
/**
* Obtains the collection of all Wants of the WantAgent object.
*
* @return Returns the collection of all Wants of the WantAgent object.
*/
std::vector<std::shared_ptr<AAFwk::Want>> GetWants() const;
/**
* Obtains the extra information of the WantAgent object.
*
* @return Returns the extra information of the WantAgent object.
*/
std::shared_ptr<AAFwk::WantParams> GetExtraInfo() const;
private:
int requestCode_ = 0;
WantAgentConstant::OperationType operationType_ = WantAgentConstant::OperationType::UNKNOWN_TYPE;
std::vector<WantAgentConstant::Flags> flags_ = std::vector<WantAgentConstant::Flags>();
std::vector<std::shared_ptr<AAFwk::Want>> wants_ = std::vector<std::shared_ptr<AAFwk::Want>>();
std::shared_ptr<AAFwk::WantParams> extraInfo_;
};
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_INFO_H

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_LOG_WRAPPER_H
#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_LOG_WRAPPER_H
#include <string>
#include "hilog/log.h"
namespace OHOS::AbilityRuntime::WantAgent {
#ifndef WANT_AGENT_LOG_DOMAIN
#define WANT_AGENT_LOG_DOMAIN 0xD001000
#endif
#ifndef WANT_AGENT_LOG_TAG
#define WANT_AGENT_LOG_TAG "WantAgent"
#endif
enum class WantAgentLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL };
static constexpr OHOS::HiviewDFX::HiLogLabel Want_Agent_LABEL = {LOG_CORE, WANT_AGENT_LOG_DOMAIN, WANT_AGENT_LOG_TAG};
class WantAgentLogWrapper {
public:
static bool JudgeLevel(const WantAgentLogLevel &level);
static void SetLogLevel(const WantAgentLogLevel &level)
{
level_ = level;
}
static const WantAgentLogLevel &GetLogLevel()
{
return level_;
}
static std::string GetBriefFileName(const char *str);
private:
static WantAgentLogLevel level_;
};
#define PRINT_LOG(LEVEL, Level, fmt, ...) \
if (WantAgentLogWrapper::JudgeLevel(WantAgentLogLevel::LEVEL)) \
OHOS::HiviewDFX::HiLog::Level(Want_Agent_LABEL, \
"[%{public}s(%{public}s)] " fmt, \
WantAgentLogWrapper::GetBriefFileName(__FILE__).c_str(), \
__FUNCTION__, \
##__VA_ARGS__)
#define WANT_AGENT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
#define WANT_AGENT_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
#define WANT_AGENT_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
#define WANT_AGENT_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
#define WANT_AGENT_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
} // namespace OHOS::AbilityRuntime::WantAgent
#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_WANTAGENT_INCLUDE_WANT_AGENT_LOG_WRAPPER_H

204
interfaces/kits/js/@ohos.wantAgent.d.ts vendored Normal file
View File

@ -0,0 +1,204 @@
/*
* Copyright (c) 2021 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 { AsyncCallback , Callback} from './basic';
import { Want } from './ability/want';
import { WantAgentInfo } from './wantAgent/wantAgentInfo';
import { TriggerInfo } from './wantAgent/triggerInfo';
/**
* Provide the method obtain trigger, cancel, and compare and to obtain
* the bundle name, UID of an {@link WantAgent} object.
*
* @name wantAgent
* @since 7
* @syscap SystemCapability.Ability.AbilityRuntime.Core
* @import import wantAgent from '@ohos.wantAgent';
* @permission N/A
*/
declare namespace wantAgent {
/**
* Obtains the bundle name of a WantAgent.
*
* @param WantAgent whose bundle name to obtain.
* @return Returns the bundle name of the {@link WantAgent} if any.
*/
function getBundleName(agent: WantAgent, callback: AsyncCallback<string>): void;
function getBundleName(agent: WantAgent): Promise<string>;
/**
* Obtains the UID of a WantAgent.
*
* @param WantAgent whose UID to obtain.
* @return Returns the UID of the {@link WantAgent} if any; returns {@code -1} otherwise.
*/
function getUid(agent: WantAgent, callback: AsyncCallback<number>): void;
function getUid(agent: WantAgent): Promise<number>;
/**
* Obtains the {@link Want} of an {@link WantAgent}.
*
* @param agent Indicates the {@link WantAgent} whose UID is to be obtained.
* @return Returns the {@link Want} of the {@link WantAgent}.
* @systemapi Hide this for inner system use.
*/
function getWant(agent: WantAgent, callback: AsyncCallback<Want>): void;
/**
* Obtains the {@link Want} of an {@link WantAgent}.
*
* @param agent Indicates the {@link WantAgent} whose UID is to be obtained.
* @return Returns the {@link Want} of the {@link WantAgent}.
* @systemapi Hide this for inner system use.
*/
function getWant(agent: WantAgent): Promise<Want>;
/**
* Cancels a WantAgent. Only the application that creates the WantAgent can cancel it.
*
* @param WantAgent to cancel.
*/
function cancel(agent: WantAgent, callback: AsyncCallback<void>): void;
function cancel(agent: WantAgent): Promise<void>;
/**
* Triggers a WantAgent.
*
* @param WantAgent to trigger.
* @param Trigger parameters.
* @param callback Indicates the callback method to be called after the {@link WantAgent} is triggered.
*/
function trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback<CompleteData>): void;
/**
* Checks whether two WantAgent objects are equal.
*
* @param WantAgent to compare.
* @param WantAgent to compare.
* @return Returns {@code true} If the two objects are the same; returns {@code false} otherwise.
*/
function equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback<boolean>): void;
function equal(agent: WantAgent, otherAgent: WantAgent): Promise<boolean>;
/**
* Obtains a WantAgent object.
*
* @param Information about the WantAgent object to obtain.
* @return Returns the created {@link WantAgent} object.
*/
function getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void;
function getWantAgent(info: WantAgentInfo): Promise<WantAgent>;
/**
* Enumerates flags for using a WantAgent.
*/
export enum WantAgentFlags {
/**
* Indicates that the WantAgent can be used only once.
* This flag is valid only when OperationType is set to START_ABILITY, START_SERVICE, or SEND_COMMON_EVENT.
*/
ONE_TIME_FLAG = 0,
/**
* Indicates that null is returned if the WantAgent does not exist.
* This flag is valid only when OperationType is set to START_ABILITY, START_SERVICE, or SEND_COMMON_EVENT.
*/
NO_BUILD_FLAG,
/**
* Indicates that the existing WantAgent should be canceled before a new object is generated.
* This flag is valid only when OperationType is set to START_ABILITY, START_SERVICE, or SEND_COMMON_EVENT.
*/
CANCEL_PRESENT_FLAG,
/**
* Indicates that the system only replaces the extra data of the existing WantAgent with that of the new object.
* This flag is valid only when OperationType is set to START_ABILITY, START_SERVICE, or SEND_COMMON_EVENT.
*/
UPDATE_PRESENT_FLAG,
/**
* Indicates that the created WantAgent should be immutable.
*/
CONSTANT_FLAG
}
/**
* Identifies the operation for using a WantAgent, such as starting an ability or sending a common event.
*/
export enum OperationType {
/**
* Unknown operation.
*/
UNKNOWN_TYPE = 0,
/**
* Starts an ability with a UI.
*/
START_ABILITY,
/**
* Starts multiple abilities with a UI.
*/
START_ABILITIES,
/**
* Starts an ability without a UI.
*/
START_SERVICE,
/**
* Sends a common event.
*/
SEND_COMMON_EVENT
}
/**
* Describes the data returned by after wantAgent.trigger is called.
*/
export interface CompleteData {
/**
* Triggered WantAgent.
*/
info: WantAgent;
/**
* Existing Want that is triggered.
*/
want: Want;
/**
* Request code used to trigger the WantAgent.
*/
finalCode: number;
/**
* Final data collected by the common event.
*/
finalData: string;
/**
* Extra data collected by the common event.
*/
extraInfo?: {[key: string]: any};
}
}
/**
* WantAgent object.
*/
export type WantAgent = object;
export default wantAgent;

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 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 { Want } from '../ability/want';
/**
* Provides the information required for triggering a WantAgent.
*
* @name TriggerInfo
* @since 7
* @syscap SystemCapability.Ability.AbilityRuntime.Core
* @permission N/A
*/
export interface TriggerInfo {
/**
* Result code.
*/
code: number;
/**
* Extra Want.
* If flags in WantAgentInfo contain CONSTANT_FLAG, this parameter is invalid.
* If flags contain REPLACE_ELEMENT, REPLACE_ACTION, REPLACE_URI, REPLACE_ENTITIES, and REPLACE_BUNDLE,
* the element, action, uri, entities, and bundleName attributes of the Want specified in this parameter
* will be used to replace the corresponding attributes in the original Want, respectively.
* If this parameter is null, the original Want remains unchanged.
*/
want?: Want;
/**
* Permission required for a WantAgent recipient.
* This parameter is valid only when the WantAgent is triggered to send common events.
* If permission is null, no permission is required on the recipient.
*/
permission?: string;
/**
* Custom extra data you want to add for triggering a WantAgent.
*/
extraInfo?: {[key: string]: any};
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 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 { Want } from '../ability/want';
import wantAgent from '../@ohos.wantAgent'
/**
* Provides the information required for triggering a WantAgent.
*
* @name WantAgentInfo
* @since 7
* @syscap SystemCapability.Ability.AbilityRuntime.Core
* @permission N/A
*/
export interface WantAgentInfo {
/**
* An array of all Wants for starting abilities or sending common events. Only Wants can be displayed.
*/
wants: Array<Want>;
/**
* Type of the action specified in a Want.
*/
operationType: wantAgent.OperationType;
/**
* Request code defined by the user.
*/
requestCode: number;
/**
* An array of flags for using the WantAgent.
*/
wantAgentFlags?: Array<wantAgent.WantAgentFlags>;
/**
* Extra information about how the Want starts an ability.
* If there is no extra information to set, this constant can be left empty.
*/
extraInfo?: {[key: string]: any};
}

View File

@ -47,6 +47,7 @@ group("napi_packages") {
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/static_subscriber_extension:staticsubscriberextension_napi",
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/static_subscriber_extension_context:staticsubscriberextensioncontext_napi",
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/wantConstant:wantconstant",
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/wantagent:wantagent",
]
}

View File

@ -30,7 +30,6 @@ ohos_shared_library("napi_common") {
]
deps = [
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/interfaces/innerkits/base:base",
@ -44,6 +43,7 @@ ohos_shared_library("napi_common") {
"ability_runtime:ability_manager",
"ability_runtime:app_manager",
"ability_runtime:napi_base_context",
"ability_runtime:wantagent_innerkits",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"eventhandler:libeventhandler",

View File

@ -58,7 +58,7 @@ struct AsyncCallbackInfo {
napi_async_work asyncWork = nullptr;
napi_deferred deferred = nullptr;
Ability *ability = nullptr;
Notification::WantAgent::WantAgent *wantAgent = nullptr;
AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr;
CallAbilityParam param;
CallbackInfo aceCallback;
bool native_result;

View File

@ -3502,7 +3502,7 @@ napi_value AcquireDataAbilityHelperWrap(napi_env env, napi_callback_info info, D
return result;
}
napi_value UnwrapParamForWantAgent(napi_env &env, napi_value &args, Notification::WantAgent::WantAgent *&wantAgent)
napi_value UnwrapParamForWantAgent(napi_env &env, napi_value &args, AbilityRuntime::WantAgent::WantAgent *&wantAgent)
{
napi_valuetype valuetype = napi_undefined;
NAPI_CALL(env, napi_typeof(env, args, &valuetype));
@ -3547,10 +3547,10 @@ void StartBackgroundRunningExecuteCB(napi_env env, void *data)
return;
}
Notification::WantAgent::WantAgent wantAgentObj;
AbilityRuntime::WantAgent::WantAgent wantAgentObj;
if (!asyncCallbackInfo->wantAgent) {
HILOG_WARN("input param without wantAgent");
wantAgentObj = Notification::WantAgent::WantAgent();
wantAgentObj = AbilityRuntime::WantAgent::WantAgent();
} else {
wantAgentObj = *asyncCallbackInfo->wantAgent;
}

View File

@ -0,0 +1,59 @@
# Copyright (c) 2021 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")
ohos_shared_library("wantagent") {
include_dirs = [
"//third_party/node/src",
"//foundation/aafwk/standard/services/common/include",
"//utils/system/safwk/native/include",
"//third_party/libuv/include",
"//foundation/aafwk/standard/frameworks/kits/ability/native/include",
"//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime",
"//foundation/aafwk/standard/services/abilitymgr/include",
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common",
"//foundation/aafwk/standard/services/common/include",
"./",
]
sources = [
"napi_want_agent.cpp",
"native_module.cpp",
]
deps = [
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common:napi_common",
"//utils/native/base:utils",
]
external_deps = [
"ability_base:want",
"ability_runtime:abilitykit_native",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"eventhandler:libeventhandler",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",
]
relative_install_dir = "module"
subsystem_name = "aafwk"
part_name = "ability_runtime"
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,197 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef NAPI_WANT_AGENT_H
#define NAPI_WANT_AGENT_H
#include <map>
#include <memory>
#include <mutex>
#include <uv.h>
#include "ability.h"
#include "completed_callback.h"
#include "context/context.h"
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "trigger_info.h"
#include "want.h"
#include "want_agent.h"
#include "want_agent_constant.h"
#include "want_params.h"
namespace OHOS {
using namespace OHOS::AppExecFwk;
using namespace OHOS::AbilityRuntime::WantAgent;
const uint8_t NUMBER_OF_PARAMETERS_ZERO = 0;
const uint8_t NUMBER_OF_PARAMETERS_ONE = 1;
const uint8_t NUMBER_OF_PARAMETERS_TWO = 2;
const uint8_t NUMBER_OF_PARAMETERS_THREE = 3;
const uint8_t NUMBER_OF_PARAMETERS_FOUR = 4;
const uint8_t NUMBER_OF_PARAMETERS_FIVE = 5;
const uint8_t NUMBER_OF_PARAMETERS_SIX = 6;
const uint8_t NUMBER_OF_PARAMETERS_SEVEN = 7;
const uint8_t NUMBER_OF_PARAMETERS_EIGHT = 8;
const uint8_t NUMBER_OF_PARAMETERS_NINE = 9;
class TriggerCompleteCallBack;
struct AsyncGetWantAgentCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::vector<std::shared_ptr<AAFwk::Want>> wants;
WantAgentConstant::OperationType operationType;
int32_t requestCode = -1;
std::vector<WantAgentConstant::Flags> wantAgentFlags;
std::shared_ptr<AAFwk::WantParams> extraInfo;
std::shared_ptr<AbilityRuntime::Context> context;
std::shared_ptr<WantAgent> wantAgent;
};
struct AsyncGetBundleNameCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::shared_ptr<WantAgent> wantAgent;
std::string bundleName;
};
struct AsyncGetUidCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::shared_ptr<WantAgent> wantAgent;
int32_t uid;
};
struct AsyncGetWantCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::shared_ptr<WantAgent> wantAgent;
std::shared_ptr<AAFwk::Want> want;
};
struct AsyncEqualCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::shared_ptr<WantAgent> wantAgentFirst;
std::shared_ptr<WantAgent> wantAgentSecond;
bool result;
};
struct AsyncCancelCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
std::shared_ptr<WantAgent> wantAgent;
};
struct AsyncTriggerCallbackInfo {
napi_env env;
napi_async_work asyncWork;
napi_deferred deferred;
napi_ref callback[2] = {0};
bool callBackMode = false;
std::shared_ptr<TriggerCompleteCallBack> triggerObj;
std::shared_ptr<WantAgent> wantAgent;
TriggerInfo triggerInfo;
};
struct CallbackInfo {
std::shared_ptr<WantAgent> wantAgent;
napi_env env;
napi_ref ref;
};
struct TriggerReceiveDataWorker {
napi_env env;
napi_ref ref = 0;
std::shared_ptr<WantAgent> wantAgent;
AAFwk::Want want;
int resultCode;
std::string resultData;
AAFwk::WantParams resultExtras;
};
struct WantAgentWantsParas {
std::vector<std::shared_ptr<AAFwk::Want>> &wants;
int32_t &operationType;
int32_t &requestCode;
std::vector<WantAgentConstant::Flags> &wantAgentFlags;
AAFwk::WantParams &extraInfo;
};
class TriggerCompleteCallBack : public CompletedCallback {
public:
TriggerCompleteCallBack();
virtual ~TriggerCompleteCallBack();
public:
virtual void OnSendFinished(const AAFwk::Want &want, int resultCode, const std::string &resultData,
const AAFwk::WantParams &resultExtras) override;
void SetCallbackInfo(const napi_env &env, const napi_ref &ref);
void SetWantAgentInstance(const std::shared_ptr<WantAgent> &wantAgent);
private:
CallbackInfo triggerCompleteInfo_;
};
napi_value WantAgentInit(napi_env env, napi_value exports);
void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const std::string &propName);
napi_value WantAgentFlagsInit(napi_env env, napi_value exports);
napi_value WantAgentOperationTypeInit(napi_env env, napi_value exports);
napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info);
napi_value NAPI_GetUid(napi_env env, napi_callback_info info);
napi_value NAPI_GetWant(napi_env env, napi_callback_info info);
napi_value NAPI_Cancel(napi_env env, napi_callback_info info);
napi_value NAPI_Trigger(napi_env env, napi_callback_info info);
napi_value NAPI_Equal(napi_env env, napi_callback_info info);
napi_value NAPI_GetWantAgent(napi_env env, napi_callback_info info);
napi_value GetCallbackErrorResult(napi_env env, int errCode);
napi_value NapiGetNull(napi_env env);
void DeleteRecordByCode(const int32_t code);
static std::unique_ptr<std::map<AsyncGetWantAgentCallbackInfo *, const int32_t>,
std::function<void(std::map<AsyncGetWantAgentCallbackInfo *, const int32_t> *)>>
g_WantAgentMap(new std::map<AsyncGetWantAgentCallbackInfo *, const int32_t>,
[](std::map<AsyncGetWantAgentCallbackInfo *, const int32_t> *map) {
if (map == nullptr) {
return;
}
for (auto &item : *map) {
if (item.first != nullptr) {
delete item.first;
}
}
map->clear();
delete map;
});
static std::recursive_mutex g_mutex;
} // namespace OHOS
#endif // NAPI_WANT_AGENT_H

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2021 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 <pthread.h>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include "napi_want_agent.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "hilog_wrapper.h"
using namespace OHOS::AbilityRuntime::WantAgent;
namespace OHOS {
EXTERN_C_START
/*
* function for module exports
*/
static napi_value Init(napi_env env, napi_value exports)
{
WantAgentInit(env, exports);
WantAgentFlagsInit(env, exports);
WantAgentOperationTypeInit(env, exports);
return exports;
}
EXTERN_C_END
/*
* Module define
*/
static napi_module _module = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "wantagent",
.nm_priv = ((void *)0),
.reserved = {0},
};
/*
* Module register function
*/
extern "C" __attribute__((constructor)) void RegisterModule(void)
{
napi_module_register(&_module);
}
} // namespace OHOS

View File

@ -13,8 +13,10 @@
"//foundation/aafwk/standard/common:common_target",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:native_wantagent",
"//foundation/aafwk/standard/frameworks/kits/appkit:appexec",
"//foundation/aafwk/standard/test/resource/amssystemtestability/abilitySrc:ams_system_test_app"
],
"inner_kits": [
{

View File

@ -91,8 +91,8 @@ ohos_shared_library("libfms") {
"${appexecfwk_path}/common:libappexecfwk_common",
"${appexecfwk_path}/libs/libeventhandler:libeventhandler_target",
"//base/miscservices/time/services:time_service",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner",
"//foundation/distributedschedule/dmsfwk/services/dtbschedmgr:distributedschedsvr",
"//utils/native/base:utils",

View File

@ -39,7 +39,7 @@
namespace OHOS {
namespace AppExecFwk {
using namespace OHOS::Notification::WantAgent;
using namespace OHOS::AbilityRuntime::WantAgent;
/**
* @class FormTimerMgr
* form timer task manager.

View File

@ -32,7 +32,7 @@ public:
virtual void SetType(const int &type) override;
virtual void SetRepeat(bool repeat) override;
virtual void SetInterval(const uint64_t &interval) override;
virtual void SetWantAgent(std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> wantAgent) override;
virtual void SetWantAgent(std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent) override;
void SetCallbackInfo(std::function<void()> callBack);
private:
@ -74,7 +74,7 @@ void FormTimerOption::SetInterval(const uint64_t &interval)
this->interval = interval;
}
void FormTimerOption::SetWantAgent(std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> wantAgent)
void FormTimerOption::SetWantAgent(std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent)
{
this->wantAgent = wantAgent;
}

View File

@ -61,8 +61,8 @@ ohos_unittest("FmsFormSetNextRefreshTest") {
"${appexecfwk_path}/interfaces/innerkits/appexecfwk_core:appexecfwk_core",
"${services_path}/formmgr:fms_target",
"//base/miscservices/time/services:time_service",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
#"${libs_path}/libeventhandler:libeventhandler_target",

View File

@ -62,8 +62,8 @@ ohos_unittest("FmsFormSysEventReceiverTest") {
"${appexecfwk_path}/libs/libeventhandler:libeventhandler_target",
"${services_path}/formmgr:fms_target",
"//base/miscservices/time/services:time_service",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk:libpermissionsdk_standard",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
"//foundation/aafwk/standard/interfaces/innerkits/base:base",
"//foundation/aafwk/standard/interfaces/innerkits/want:want",

View File

@ -39,8 +39,8 @@ ohos_unittest("FmsFormTimerMgrTest") {
"${appexecfwk_path}/common:libappexecfwk_common",
"${services_path}/formmgr:fms_target",
"//base/miscservices/time/services:time_service",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//base/notification/ces_standard/frameworks/common:libevent_common",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/distributedschedule/dmsfwk/services/dtbschedmgr:distributedschedsvr",
"//third_party/googletest:gmock_main",
"//utils/native/base:utils",

View File

@ -19,11 +19,11 @@
#include "gmock/gmock.h"
#include "completed_callback.h"
namespace OHOS::Notification::WantAgent {
namespace OHOS::AbilityRuntime::WantAgent {
class MockCompletedCallback : public CompletedCallback {
public:
MOCK_METHOD4(OnSendFinished, void(const AAFwk::Want &want, int resultCode, const std::string &resultData,
const AAFwk::WantParams &resultExtras));
};
} // namespace OHOS::Notification::WantAgent
} // namespace OHOS::WantAgent
#endif

View File

@ -27,7 +27,7 @@ public:
const WantParams &extras, bool serialized, bool sticky, int sendingUser));
};
class MockCancelListener : public Notification::WantAgent::CancelListener {
class MockCancelListener : public AbilityRuntime::WantAgent::CancelListener {
public:
MOCK_METHOD1(OnCancelled, void(int resultCode));
};

View File

@ -53,10 +53,10 @@ ohos_moduletest("PandingWantMgrTest") {
"//base/account/os_account/frameworks/osaccount/native:os_account_innerkits",
"//base/global/i18n_standard/frameworks/intl:intl_util",
"//base/hiviewdfx/hiview/adapter/utility:hiview_adapter_utility",
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
"//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr",
"//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base",

View File

@ -38,7 +38,7 @@
#include "mock_ability_mgr_service.h"
#include "os_account_manager.h"
using namespace OHOS::Notification::WantAgent;
using namespace OHOS::AbilityRuntime::WantAgent;
using namespace OHOS::AppExecFwk;
using namespace testing;

View File

@ -45,10 +45,10 @@ ohos_shared_library("amsAbilityVisibleTestPageA") {
]
configs = [ ":amsAbilityVisibleTestPageAConfig" ]
deps = [
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
"//foundation/aafwk/standard/interfaces/innerkits/want:want",

View File

@ -31,7 +31,7 @@ namespace OHOS {
namespace AppExecFwk {
using AbilityConnectionStub = OHOS::AAFwk::AbilityConnectionStub;
using Uri = OHOS::Uri;
using namespace OHOS::Notification::WantAgent;
using namespace OHOS::AbilityRuntime::WantAgent;
class AbilityConnectCallback : public AbilityConnectionStub {
public:

View File

@ -45,10 +45,10 @@ ohos_shared_library("amsAbilityVisibleTestServiceB") {
]
configs = [ ":amsAbilityVisibleTestServiceBConfig" ]
deps = [
"//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/frameworks/kits/wantagent:wantagent_innerkits",
"//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager",
"//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",
"//foundation/aafwk/standard/interfaces/innerkits/want:want",

View File

@ -32,8 +32,8 @@ namespace OHOS {
namespace AppExecFwk {
using AbilityConnectionStub = OHOS::AAFwk::AbilityConnectionStub;
using Uri = OHOS::Uri;
using CompletedCallback = OHOS::Notification::WantAgent::CompletedCallback;
using namespace OHOS::Notification::WantAgent;
using CompletedCallback = OHOS::AbilityRuntime::WantAgent::CompletedCallback;
using namespace OHOS::WantAgent;
class AbilityConnectCallback : public AbilityConnectionStub {
public: