add getLogFileDir

Signed-off-by: 任国军 <renguojun1@h-partners.com>
This commit is contained in:
任国军
2025-10-13 14:11:44 +08:00
parent e626812ecb
commit 46ea7bcdf8
46 changed files with 653 additions and 19 deletions
@@ -529,4 +529,25 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(Abili
return ABILITY_RUNTIME_ERROR_CODE_START_TIMEOUT;
}
return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
}
AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLogFileDir(
char* buffer, const int32_t bufferSize, int32_t* writeLength)
{
TAG_LOGD(AAFwkTag::APPKIT, "getLogFileDir called");
auto ret = CheckParameters(buffer, writeLength);
if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
return ret;
}
const auto appContext = Context::GetApplicationContext();
if (appContext == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
}
const std::string logFileDir = appContext->GetLogFileDir();
if (logFileDir.empty()) {
TAG_LOGE(AAFwkTag::APPKIT, "logFileDir is empty");
return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
}
return WriteStringToBuffer(logFileDir, buffer, bufferSize, writeLength);
}
+2 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cj_context.h"
#include "context.h"
@@ -32,6 +32,7 @@ ani_object GetApplicationContextSync(ani_env *env, ani_object aniObj);
std::shared_ptr<Context> GetBaseContext(ani_env *env, ani_object aniObj);
void SwitchArea(ani_env *env, ani_object obj, ani_enum_item areaModeItem);
ani_enum_item GetArea(ani_env *env, ani_object obj);
ani_string GetLogFileDir(ani_env *env, ani_object obj);
ani_object CreateModuleResourceManagerSync(ani_env *env, ani_object aniObj,
ani_string bundleName, ani_string moduleName);
void Clean(ani_env *env, ani_object object);
@@ -124,6 +124,7 @@ void BindContextDir(ani_env *aniEnv, ani_object contextObj, std::shared_ptr<Cont
TAG_LOGE(AAFwkTag::APPKIT, "tempDir SetField status: %{public}d", status);
return;
}
BindContextDirInner(aniEnv, contextObj, context);
}
} // namespace
@@ -270,6 +271,8 @@ void BindNativeFunction(ani_env *aniEnv)
reinterpret_cast<void *>(ContextUtil::SwitchArea)},
ani_native_function {"getArea", nullptr,
reinterpret_cast<void *>(ContextUtil::GetArea)},
ani_native_function {"getLogFileDir", nullptr,
reinterpret_cast<void *>(ContextUtil::GetLogFileDir)},
ani_native_function {"createModuleResourceManagerSync", "Lstd/core/String;Lstd/core/String;"
":L@ohos/resourceManager/resourceManager/ResourceManager;",
reinterpret_cast<void *>(ContextUtil::CreateModuleResourceManagerSync)},
@@ -730,6 +733,17 @@ ani_enum_item GetArea(ani_env *env, ani_object obj)
OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(env, AREA_MODE_ENUM_NAME, areaMode, areaModeItem);
return areaModeItem;
}
ani_string GetLogFileDir(ani_env *env, ani_object obj)
{
auto context = GetBaseContext(env, obj);
if (env == nullptr || context == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "env or context is null");
return AppExecFwk::GetAniString(env, "");
}
auto logFileDir = context->GetLogFileDir();
return AppExecFwk::GetAniString(env, logFileDir);
}
}
} // namespace AbilityRuntime
} // namespace OHOS
+6 -1
View File
@@ -61,7 +61,7 @@ export class Context extends BaseContext {
this.eventHub = new EventHub();
}
}
private setEtsContextPtr(ptr: long) {
if (this.etsContextPtr == 0) {
this.etsContextPtr = ptr;
@@ -80,6 +80,7 @@ export class Context extends BaseContext {
public native createModuleResourceManagerSync(bundleName: string, moduleName: string): resmgr.ResourceManager;
private native switchArea(mode: contextConstant.AreaMode): void;
private native getArea(): contextConstant.AreaMode;
private native getLogFileDir(): string;
public native nativeGetGroupDir(dataGroupID: string, callback: AsyncCallbackWrapper<string>): void;
public native nativeCreateDisplayContext(displayId: long): Context;
public native nativeCreateAreaModeContext(areaMode: contextConstant.AreaMode): Context;
@@ -104,6 +105,10 @@ export class Context extends BaseContext {
return this.getArea();
}
get logFileDir(): string {
return this.getLogFileDir();
}
getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void {
let myCall = new AsyncCallbackWrapper<string>(callback);
taskpool.execute((): void => {
@@ -399,6 +399,10 @@ class ApplicationContext {
return this.__context_impl__.cloudFileDir;
}
get logFileDir() {
return this.__context_impl__.logFileDir;
}
get eventHub() {
return this.__context_impl__.eventHub;
}
+4 -1
View File
@@ -89,7 +89,6 @@ class EventHub {
if (this.eventMap[event].indexOf(callback) === -1) {
this.eventMap[event].push(callback);
}
if (this.emitMultiThreadingEnabled === true && this.emitter !== undefined) {
if (this.emitter.getListenerCount(this.getEmitterEventName(event)) === 0) {
this.emitter.on(this.getEmitterEventName(event), (eventData) => this.onEmitterFunction(eventData));
@@ -324,6 +323,10 @@ class Context {
return this.__context_impl__.cloudFileDir;
}
get logFileDir() {
return this.__context_impl__.logFileDir;
}
get eventHub() {
return this.__context_impl__.eventHub;
}
@@ -134,6 +134,11 @@ std::string AbilityContextImpl::GetCloudFileDir()
return stageContext_ ? stageContext_->GetCloudFileDir() : "";
}
std::string AbilityContextImpl::GetLogFileDir()
{
return stageContext_ ? stageContext_->GetLogFileDir() : "";
}
bool AbilityContextImpl::IsUpdatingConfigurations()
{
return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false;
@@ -344,6 +344,16 @@ std::string AbilityStageContext::GetCloudFileDir()
return contextImpl_->GetCloudFileDir();
}
std::string AbilityStageContext::GetLogFileDir()
{
if (contextImpl_ == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
return {};
}
return contextImpl_->GetLogFileDir();
}
std::string AbilityStageContext::GetBaseDir() const
{
if (contextImpl_ == nullptr) {
@@ -766,6 +766,11 @@ std::string ApplicationContext::GetCloudFileDir()
return (contextImpl_ != nullptr) ? contextImpl_->GetCloudFileDir() : "";
}
std::string ApplicationContext::GetLogFileDir()
{
return (contextImpl_ != nullptr) ? contextImpl_->GetLogFileDir() : "";
}
std::string ApplicationContext::GetLaunchParameter() const
{
return (contextImpl_ != nullptr) ? contextImpl_->GetLaunchParameter() : "";
@@ -81,6 +81,7 @@ const std::string ContextImpl::CONTEXT_FILES("/files");
const std::string ContextImpl::CONTEXT_HAPS("/haps");
const std::string ContextImpl::CONTEXT_ELS[] = {"el1", "el2", "el3", "el4", "el5"};
const std::string ContextImpl::CONTEXT_RESOURCE_END = "/resources/resfile";
const std::string ContextImpl::CONTEXT_LOGFILE("log");
Global::Resource::DeviceType ContextImpl::deviceType_ = Global::Resource::DeviceType::DEVICE_NOT_SET;
const std::string OVERLAY_STATE_CHANGED = "usual.event.OVERLAY_STATE_CHANGED";
const int32_t TYPE_RESERVE = 1;
@@ -409,6 +410,12 @@ std::string ContextImpl::GetCloudFileDir()
return dir;
}
std::string ContextImpl::GetLogFileDir()
{
std::string dir = CONTEXT_DATA_STORAGE + CONTEXT_ELS[1] + CONTEXT_FILE_SEPARATOR + CONTEXT_LOGFILE;
return dir;
}
void ContextImpl::SwitchArea(int mode)
{
TAG_LOGD(AAFwkTag::APPKIT, "mode:%{public}d", mode);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024 Huawei Device Co., Ltd.
* Copyright (c) 2022-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -160,6 +160,7 @@ napi_value JsApplicationContextUtils::OnSwitchArea(napi_env env, NapiCallbackInf
BindNativeProperty(env, object, "preferencesDir", GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", GetLogFileDir);
return CreateJsUndefined(env);
}
@@ -448,6 +449,23 @@ napi_value JsApplicationContextUtils::OnGetCloudFileDir(napi_env env, NapiCallba
return CreateJsValue(env, path);
}
napi_value JsApplicationContextUtils::GetLogFileDir(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsApplicationContextUtils,
OnGetLogFileDir, APPLICATION_CONTEXT_NAME);
}
napi_value JsApplicationContextUtils::OnGetLogFileDir(napi_env env, NapiCallbackInfo& info)
{
auto applicationContext = applicationContext_.lock();
if (!applicationContext) {
TAG_LOGW(AAFwkTag::APPKIT, "null applicationContext");
return CreateJsUndefined(env);
}
std::string path = applicationContext->GetLogFileDir();
return CreateJsValue(env, path);
}
napi_value JsApplicationContextUtils::GetProcessName(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsApplicationContextUtils, OnGetProcessName, APPLICATION_CONTEXT_NAME);
@@ -1824,6 +1842,7 @@ void JsApplicationContextUtils::BindNativeApplicationContextOne(napi_env env, na
BindNativeProperty(env, object, "preferencesDir", JsApplicationContextUtils::GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", JsApplicationContextUtils::GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", JsApplicationContextUtils::GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", JsApplicationContextUtils::GetLogFileDir);
BindNativeProperty(env, object, "processName", JsApplicationContextUtils::GetProcessName);
BindNativeFunction(env, object, "registerAbilityLifecycleCallback", MD_NAME,
JsApplicationContextUtils::RegisterAbilityLifecycleCallback);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024 Huawei Device Co., Ltd.
* Copyright (c) 2021-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -101,6 +101,7 @@ public:
napi_value OnGetGroupDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetBundleCodeDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetCloudFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetLogFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetProcessName(napi_env env, NapiCallbackInfo &info);
static napi_value GetCacheDir(napi_env env, napi_callback_info info);
@@ -113,6 +114,7 @@ public:
static napi_value GetGroupDir(napi_env env, napi_callback_info info);
static napi_value GetBundleCodeDir(napi_env env, napi_callback_info info);
static napi_value GetCloudFileDir(napi_env env, napi_callback_info info);
static napi_value GetLogFileDir(napi_env env, napi_callback_info info);
static napi_value GetProcessName(napi_env env, napi_callback_info info);
protected:
@@ -191,6 +193,7 @@ napi_value JsBaseContext::OnSwitchArea(napi_env env, NapiCallbackInfo& info)
BindNativeProperty(env, object, "preferencesDir", GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", GetLogFileDir);
return CreateJsUndefined(env);
}
@@ -565,6 +568,22 @@ napi_value JsBaseContext::OnGetCloudFileDir(napi_env env, NapiCallbackInfo& info
return CreateJsValue(env, path);
}
napi_value JsBaseContext::GetLogFileDir(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsBaseContext, OnGetLogFileDir, BASE_CONTEXT_NAME);
}
napi_value JsBaseContext::OnGetLogFileDir(napi_env env, NapiCallbackInfo& info)
{
auto context = context_.lock();
if (!context) {
TAG_LOGW(AAFwkTag::APPKIT, "null context");
return CreateJsUndefined(env);
}
std::string path = context->GetLogFileDir();
return CreateJsValue(env, path);
}
napi_value JsBaseContext::GetProcessName(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsBaseContext, OnGetProcessName, BASE_CONTEXT_NAME);
@@ -942,6 +961,7 @@ void BindPropertyAndFunction(napi_env env, napi_value object, const char* module
BindNativeProperty(env, object, "preferencesDir", JsBaseContext::GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", JsBaseContext::GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", JsBaseContext::GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", JsBaseContext::GetLogFileDir);
BindNativeProperty(env, object, "area", JsBaseContext::GetArea);
BindNativeProperty(env, object, "processName", JsBaseContext::GetProcessName);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -52,6 +52,7 @@ public:
std::string GetPreferencesDir() override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
void SwitchArea(int mode) override;
int GetArea() override;
std::string GetBaseDir() override;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -54,6 +54,7 @@ public:
std::string GetPreferencesDir() override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
void SwitchArea(int mode) override;
int GetArea() override;
std::string GetBaseDir() override;
@@ -63,6 +63,7 @@ public:
std::string GetPreferencesDir() override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
void SwitchArea(int mode) override;
int GetArea() override;
std::string GetBaseDir() override;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -49,6 +49,7 @@ public:
static napi_value GetPreferencesDir(napi_env env, napi_callback_info info);
static napi_value GetBundleCodeDir(napi_env env, napi_callback_info info);
static napi_value GetCloudFileDir(napi_env env, napi_callback_info info);
static napi_value GetLogFileDir(napi_env env, napi_callback_info info);
static napi_value GetApplicationContext(napi_env env, napi_callback_info info);
static napi_value KillProcessBySelf(napi_env env, napi_callback_info info);
static napi_value GetRunningProcessInformation(napi_env env, napi_callback_info info);
@@ -65,6 +66,7 @@ public:
napi_value OnGetPreferencesDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetBundleCodeDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetCloudFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetLogFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetArea(napi_env env, NapiCallbackInfo& info);
napi_value OnSetSupportedProcessCacheSelf(napi_env env, NapiCallbackInfo& info);
@@ -140,6 +140,11 @@ std::string AbilityContext::GetCloudFileDir()
return stageContext_ ? stageContext_->GetCloudFileDir() : "";
}
std::string AbilityContext::GetLogFileDir()
{
return stageContext_ ? stageContext_->GetLogFileDir() : "";
}
void AbilityContext::SwitchArea(int mode)
{
if (stageContext_) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -37,6 +37,7 @@ constexpr const char *CONTEXT_ASSET("asset");
constexpr const char *CONTEXT_ELS[] = {"el1", "el2", "el3", "el4", "el5"};
constexpr const char *CONTEXT_RESOURCE_BASE("/data/storage/el1/bundle");
constexpr const char *CONTEXT_RESOURCE_END("/resources/resfile");
constexpr const char *CONTEXT_LOG("log");
constexpr int DIR_DEFAULT_PERM = 0770;
}
const size_t AbilityStageContext::CONTEXT_TYPE_ID(std::hash<const char*> {} ("AbilityStageContext"));
@@ -239,6 +240,18 @@ std::string AbilityStageContext::GetCloudFileDir()
return dir;
}
std::string AbilityStageContext::GetLogFileDir()
{
auto preivewDir = GetPreviewPath();
if (preivewDir.empty()) {
return "";
}
auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_LOG;
CreateMultiDir(dir);
return dir;
}
void AbilityStageContext::SwitchArea(int mode)
{
TAG_LOGD(AAFwkTag::ABILITY_SIM, "called, mode:%{public}d", mode);
@@ -45,6 +45,7 @@ constexpr const char *CONTEXT_ASSET("asset");
constexpr const char *CONTEXT_ELS[] = {"el1", "el2", "el3", "el4", "el5"};
constexpr const char *CONTEXT_RESOURCE_BASE("/data/storage/el1/bundle");
constexpr const char *CONTEXT_RESOURCE_END("/resources/resfile");
constexpr const char *CONTEXT_LOG("log");
const int32_t TYPE_RESERVE = 1;
const int32_t TYPE_OTHERS = 2;
const int32_t API11 = 11;
@@ -239,6 +240,18 @@ std::string ContextImpl::GetCloudFileDir()
return dir;
}
std::string ContextImpl::GetLogFileDir()
{
auto previewPath = GetPreviewPath();
if (previewPath.empty()) {
return "";
}
auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_LOG;
CreateMultiDir(dir);
return dir;
}
void ContextImpl::SwitchArea(int mode)
{
TAG_LOGD(AAFwkTag::ABILITY_SIM, "mode:%{public}d", mode);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -60,6 +60,7 @@ napi_value JsApplicationContextUtils::OnSwitchArea(napi_env env, NapiCallbackInf
BindNativeProperty(env, object, "preferencesDir", GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", GetLogFileDir);
return CreateJsUndefined(env);
}
@@ -238,6 +239,22 @@ napi_value JsApplicationContextUtils::OnGetCloudFileDir(napi_env env, NapiCallba
return CreateJsValue(env, path);
}
napi_value JsApplicationContextUtils::GetLogFileDir(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsApplicationContextUtils,
OnGetLogFileDir, APPLICATION_CONTEXT_NAME);
}
napi_value JsApplicationContextUtils::OnGetLogFileDir(napi_env env, NapiCallbackInfo &info)
{
auto context = context_.lock();
if (!context) {
TAG_LOGW(AAFwkTag::ABILITY_SIM, "context released");
return CreateJsUndefined(env);
}
std::string path = context->GetLogFileDir();
return CreateJsValue(env, path);
}
napi_value JsApplicationContextUtils::KillProcessBySelf(napi_env env, napi_callback_info info)
{
return nullptr;
@@ -337,6 +354,7 @@ void JsApplicationContextUtils::BindNativeApplicationContext(napi_env env, napi_
BindNativeProperty(env, object, "preferencesDir", JsApplicationContextUtils::GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", JsApplicationContextUtils::GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", JsApplicationContextUtils::GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", JsApplicationContextUtils::GetLogFileDir);
BindNativeFunction(env, object, "registerAbilityLifecycleCallback", MD_NAME,
JsApplicationContextUtils::RegisterAbilityLifecycleCallback);
BindNativeFunction(env, object, "unregisterAbilityLifecycleCallback", MD_NAME,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -71,6 +71,7 @@ public:
static napi_value GetPreferencesDir(napi_env env, napi_callback_info info);
static napi_value GetBundleCodeDir(napi_env env, napi_callback_info info);
static napi_value GetCloudFileDir(napi_env env, napi_callback_info info);
static napi_value GetLogFileDir(napi_env env, napi_callback_info info);
napi_value OnGetCacheDir(napi_env env, NapiCallbackInfo &info);
napi_value OnGetTempDir(napi_env env, NapiCallbackInfo &info);
@@ -81,6 +82,7 @@ public:
napi_value OnGetPreferencesDir(napi_env env, NapiCallbackInfo &info);
napi_value OnGetBundleCodeDir(napi_env env, NapiCallbackInfo &info);
napi_value OnGetCloudFileDir(napi_env env, NapiCallbackInfo &info);
napi_value OnGetLogFileDir(napi_env env, NapiCallbackInfo &info);
napi_value OnSwitchArea(napi_env env, NapiCallbackInfo &info);
napi_value OnGetArea(napi_env env, NapiCallbackInfo &info);
napi_value OnGetApplicationContext(napi_env env, NapiCallbackInfo &info);
@@ -150,6 +152,7 @@ napi_value JsBaseContext::OnSwitchArea(napi_env env, NapiCallbackInfo &info)
BindNativeProperty(env, object, "preferencesDir", GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", GetLogFileDir);
return CreateJsUndefined(env);
}
@@ -395,6 +398,22 @@ napi_value JsBaseContext::OnGetCloudFileDir(napi_env env, NapiCallbackInfo &info
return CreateJsValue(env, path);
}
napi_value JsBaseContext::GetLogFileDir(napi_env env, napi_callback_info info)
{
GET_NAPI_INFO_WITH_NAME_AND_CALL(env, info, JsBaseContext, OnGetLogFileDir, BASE_CONTEXT_NAME);
}
napi_value JsBaseContext::OnGetLogFileDir(napi_env env, NapiCallbackInfo &info)
{
auto context = context_.lock();
if (!context) {
TAG_LOGW(AAFwkTag::ABILITY_SIM, "context released");
return CreateJsUndefined(env);
}
std::string path = context->GetLogFileDir();
return CreateJsValue(env, path);
}
napi_value JsBaseContext::OnGetApplicationContext(napi_env env, NapiCallbackInfo &info)
{
TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
@@ -453,6 +472,7 @@ napi_value CreateJsBaseContext(napi_env env, std::shared_ptr<Context> context, b
BindNativeProperty(env, object, "preferencesDir", JsBaseContext::GetPreferencesDir);
BindNativeProperty(env, object, "bundleCodeDir", JsBaseContext::GetBundleCodeDir);
BindNativeProperty(env, object, "cloudFileDir", JsBaseContext::GetCloudFileDir);
BindNativeProperty(env, object, "logFileDir", JsBaseContext::GetLogFileDir);
BindNativeProperty(env, object, "area", JsBaseContext::GetArea);
const char *moduleName = "JsBaseContext";
BindNativeFunction(env, object, "createBundleContext", moduleName, JsBaseContext::CreateBundleContext);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -60,6 +60,8 @@ public:
virtual std::string GetCloudFileDir() = 0;
virtual std::string GetLogFileDir() = 0;
virtual void SwitchArea(int mode) = 0;
virtual int GetArea() = 0;
@@ -383,6 +383,23 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter(
AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(AbilityBase_Want *want,
AbilityRuntime_StartOptions *options, int32_t *targetPid);
/**
* @brief Obtain the log file directory of the application.
*
* @param buffer A pointer to a buffer that receives the log file directory of the application.
* @param bufferSize The length of the buffer.
* @param writeLength The string length actually written to the buffer,
* when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
* @return The error code.
* {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
* {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
* or the buffer size is less than the minimum buffer size.
* {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the application context does not exist.
* @since 21
*/
AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLogFileDir(
char* buffer, const int32_t bufferSize, int32_t* writeLength);
#ifdef __cplusplus
} // extern "C"
#endif
@@ -47,6 +47,7 @@ public:
std::string GetPreferencesDir() override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
void SwitchArea(int mode) override;
@@ -379,7 +380,7 @@ public:
ErrCode AddCompletionHandlerForAtomicService(const std::string &requestId, OnAtomicRequestSuccess onRequestSucc,
OnAtomicRequestFailure onRequestFail, const std::string &appId) override;
ErrCode AddCompletionHandlerForOpenLink(const std::string &requestId,
AAFwk::OnOpenLinkRequestFunc onRequestSucc, AAFwk::OnOpenLinkRequestFunc onRequestFail) override;
@@ -69,6 +69,7 @@ public:
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetBaseDir() const override;
std::string GetLogFileDir() override;
int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
@@ -111,6 +111,7 @@ public:
std::string GetGroupDir(std::string groupId) override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
sptr<IRemoteObject> GetToken() override;
void SetToken(const sptr<IRemoteObject> &token) override;
void SwitchArea(int mode) override;
@@ -193,6 +193,13 @@ public:
virtual std::string GetCloudFileDir() = 0;
/**
* @brief Obtains the log file path of the application
*
* @return Returns the log file path.
*/
virtual std::string GetLogFileDir() = 0;
/**
* @brief Obtains token.
*
@@ -148,6 +148,13 @@ public:
std::string GetCloudFileDir() override;
/**
* @brief Obtains the log file path of the application
*
* @return Returns the log file path.
*/
std::string GetLogFileDir() override;
/**
* @brief Switch file area
*
@@ -328,7 +335,7 @@ public:
* @param hapModuleInfo HapModuleInfo instance.
*/
void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
/**
* @brief Set HapModuleInfo
*
@@ -480,6 +487,7 @@ private:
static const std::string CONTEXT_HAPS;
static const std::string CONTEXT_ELS[];
static const std::string CONTEXT_RESOURCE_END;
static const std::string CONTEXT_LOGFILE;
int flags_ = 0x00000000;
void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext,
@@ -81,6 +81,7 @@ public:
napi_value OnGetGroupDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetBundleCodeDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetCloudFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetLogFileDir(napi_env env, NapiCallbackInfo& info);
napi_value OnGetProcessName(napi_env env, NapiCallbackInfo &info);
napi_value OnKillProcessBySelf(napi_env env, NapiCallbackInfo& info);
napi_value OnGetRunningProcessInformation(napi_env env, NapiCallbackInfo& info);
@@ -106,6 +107,7 @@ public:
static napi_value GetGroupDir(napi_env env, napi_callback_info info);
static napi_value GetBundleCodeDir(napi_env env, napi_callback_info info);
static napi_value GetCloudFileDir(napi_env env, napi_callback_info info);
static napi_value GetLogFileDir(napi_env env, napi_callback_info info);
static napi_value GetProcessName(napi_env env, napi_callback_info info);
static napi_value GetApplicationContext(napi_env env, napi_callback_info info);
static napi_value KillProcessBySelf(napi_env env, napi_callback_info info);
@@ -157,6 +157,7 @@ HWTEST_F(AbilityStageContextTest, AbilityStageContextTest_0300, TestSize.Level1)
EXPECT_EQ(abilityStageContext->GetDistributedFilesDir(), "/data/storage/el2/distributedfiles");
EXPECT_EQ(abilityStageContext->GetCloudFileDir(), "/data/storage/el2/cloud");
EXPECT_EQ(abilityStageContext->GetBaseDir(), "/data/storage/el2/base/haps/");
EXPECT_EQ(abilityStageContext->GetLogFileDir(), "/data/storage/el2/log");
TAG_LOGI(AAFwkTag::TEST, "end.");
}
@@ -80,6 +80,11 @@ std::string MockContextImpl::GetCloudFileDir()
return "/cloud";
}
std::string MockContextImpl::GetLogFileDir()
{
return "/log";
}
std::string MockContextImpl::GetGroupDir(std::string groupId)
{
return "/group";
@@ -54,6 +54,8 @@ public:
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
@@ -686,6 +686,35 @@ HWTEST_F(ApplicationContextTest, GetCloudFileDir_0100, TestSize.Level1)
GTEST_LOG_(INFO) << "GetCloudFileDir_0100 end";
}
/**
* @tc.number: GetLogFileDir_0100
* @tc.name: GetLogFileDir
* @tc.desc: Get Log File Dir failed
*/
HWTEST_F(ApplicationContextTest, GetLogFileDir_0100, TestSize.Level1)
{
GTEST_LOG_(INFO) << "GetLogFileDir_0100 start";
std::shared_ptr<ContextImpl> contextImpl = nullptr;
context_->AttachContextImpl(contextImpl);
auto ret = context_->GetLogFileDir();
EXPECT_EQ(ret, "");
GTEST_LOG_(INFO) << "GetLogFileDir_0100 end";
}
/**
* @tc.number: GetLogFileDir_0200
* @tc.name: GetLogFileDir
* @tc.desc: Get Log File Dir failed
*/
HWTEST_F(ApplicationContextTest, GetLogFileDir_0200, TestSize.Level1)
{
GTEST_LOG_(INFO) << "GetLogFileDir_0200 start";
context_->AttachContextImpl(mock_);
auto ret = context_->GetLogFileDir();
EXPECT_EQ(ret, "/log");
GTEST_LOG_(INFO) << "GetLogFileDir_0200 end";
}
/**
* @tc.number: GetToken_0100
* @tc.name: GetToken
@@ -80,6 +80,11 @@ std::string MockContextImpl::GetCloudFileDir()
return "/cloud";
}
std::string MockContextImpl::GetLogFileDir()
{
return "/log";
}
std::string MockContextImpl::GetGroupDir(std::string groupId)
{
return "/group";
@@ -54,6 +54,8 @@ public:
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
@@ -25,7 +25,10 @@ ohos_unittest("capi_ability_runtime_application_context_test") {
cflags = [ "-DBINDER_IPC_32BIT" ]
}
sources = [ "capi_ability_runtime_application_context_test.cpp" ]
sources = [
"capi_ability_runtime_application_context_test.cpp",
"capi_ability_runtime_application_context_second_test.cpp",
]
include_dirs = [
"${ability_runtime_ndk_path}/ability_runtime",
@@ -0,0 +1,303 @@
/*
* Copyright (c) 2024-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <utility>
#include "ability_business_error_utils.h"
#include "ability_manager_errors.h"
#include "application_context.h"
#include "context/application_context.h"
#include "securec.h"
#include "start_options_impl.h"
#include "string_wrapper.h"
#include "want_manager.h"
#include "want_utils.h"
namespace OHOS::AbilityRuntime {
namespace {
constexpr const char* DATA_STORAGE = "/data/storage/";
constexpr const char* BASE_LOG_FILE = "/log";
constexpr const char* EL_LIST[] = { "el1", "el2", "el3", "el4", "el5" };
constexpr int32_t BUFFER_SIZE = 1024;
const std::string TEST_BUNDLE_NAME = "com.example.myapplication";
}
using namespace testing;
using namespace testing::ext;
class TestContextImpl final : public ContextImpl {
public:
explicit TestContextImpl(std::string bundleName) : bundleName_(std::move(bundleName))
{
SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL2);
}
std::string GetLogFileDir() override
{
std::string dir;
dir.append(DATA_STORAGE);
dir.append(EL_LIST[1]);
dir.append(BASE_LOG_FILE);
return dir;
}
void SwitchArea(const int32_t mode) override
{
if (mode < 0 || mode >= std::size(EL_LIST)) {
return;
}
areaMode_ = EL_LIST[mode];
}
static void ResetApplicationContext()
{
applicationContext_ = nullptr;
}
private:
std::string bundleName_;
std::string areaMode_;
};
class CapiAbilityRuntimeApplicationContextSecondTest : public Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
protected:
static void InitApplicationContext();
static std::shared_ptr<TestContextImpl> InitApplicationContextImpl(const std::string &bundleName);
};
void CapiAbilityRuntimeApplicationContextSecondTest::SetUpTestCase()
{
}
void CapiAbilityRuntimeApplicationContextSecondTest::TearDownTestCase()
{
}
void CapiAbilityRuntimeApplicationContextSecondTest::SetUp()
{
}
void CapiAbilityRuntimeApplicationContextSecondTest::TearDown()
{
const auto applicationContext = ApplicationContext::GetApplicationContext();
if (applicationContext != nullptr) {
applicationContext->AttachContextImpl(nullptr);
TestContextImpl::ResetApplicationContext();
}
}
void CapiAbilityRuntimeApplicationContextSecondTest::InitApplicationContext()
{
ApplicationContext::GetInstance();
}
std::shared_ptr<TestContextImpl> CapiAbilityRuntimeApplicationContextSecondTest::InitApplicationContextImpl(
const std::string &bundleName)
{
auto applicationContext = ApplicationContext::GetInstance();
if (applicationContext != nullptr) {
auto contextImpl = std::make_shared<TestContextImpl>(bundleName);
applicationContext->AttachContextImpl(contextImpl);
return contextImpl;
}
return nullptr;
}
/**
* @tc.number: GetLogFileDirTest_001
* @tc.desc: Function test with applicationContext is nullptr
* @tc.type: FUNC
*/
HWTEST_F(CapiAbilityRuntimeApplicationContextSecondTest, GetLogFileDirTest_001, TestSize.Level2)
{
char buffer[BUFFER_SIZE] = { 0 };
int32_t writeLength = 0;
AbilityRuntime_ErrorCode code =
OH_AbilityRuntime_ApplicationContextGetLogFileDir(NULL, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(nullptr, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, NULL);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, nullptr);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, -1, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, 0, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
}
/**
* @tc.number: GetLogFileDirTest_002
* @tc.desc: Function test with applicationContextImpl is nullptr
* @tc.type: FUNC
*/
HWTEST_F(CapiAbilityRuntimeApplicationContextSecondTest, GetLogFileDirTest_002, TestSize.Level2)
{
InitApplicationContext();
char buffer[BUFFER_SIZE] = { 0 };
int32_t writeLength = 0;
AbilityRuntime_ErrorCode code =
OH_AbilityRuntime_ApplicationContextGetLogFileDir(NULL, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(nullptr, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, NULL);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, nullptr);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, -1, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, 0, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST);
ASSERT_EQ(writeLength, 0);
}
/**
* @tc.number: GetLogFileDirTest_003
* @tc.desc: Function test
* @tc.type: FUNC
*/
HWTEST_F(CapiAbilityRuntimeApplicationContextSecondTest, GetLogFileDirTest_003, TestSize.Level2)
{
char buffer[BUFFER_SIZE] = { 0 };
int32_t writeLength = 0;
const auto contextImpl = InitApplicationContextImpl(TEST_BUNDLE_NAME);
ASSERT_NE(contextImpl, nullptr);
AbilityRuntime_ErrorCode code =
OH_AbilityRuntime_ApplicationContextGetLogFileDir(NULL, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(nullptr, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, NULL);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, nullptr);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, -1, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, 0, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
std::string logFileDir = contextImpl->GetLogFileDir();
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, logFileDir.length(), &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID);
ASSERT_EQ(writeLength, 0);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
logFileDir = contextImpl->GetLogFileDir();
ASSERT_EQ(writeLength, logFileDir.length());
ASSERT_STREQ(buffer, logFileDir.c_str());
}
/**
* @tc.number: GetLogFileDirTest_004
* @tc.desc: Function test
* @tc.type: FUNC
*/
HWTEST_F(CapiAbilityRuntimeApplicationContextSecondTest, GetLogFileDirTest_004, TestSize.Level2)
{
char buffer[BUFFER_SIZE] = { 0 };
int32_t writeLength = 0;
const auto contextImpl = InitApplicationContextImpl(TEST_BUNDLE_NAME);
ASSERT_NE(contextImpl, nullptr);
contextImpl->SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL1);
AbilityRuntime_ErrorCode code =
OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
std::string logFileDir = contextImpl->GetLogFileDir();
ASSERT_STREQ(buffer, logFileDir.c_str());
ASSERT_EQ(writeLength, logFileDir.length());
contextImpl->SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL2);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
logFileDir = contextImpl->GetLogFileDir();
ASSERT_EQ(writeLength, logFileDir.length());
ASSERT_STREQ(buffer, logFileDir.c_str());
contextImpl->SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL3);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
logFileDir = contextImpl->GetLogFileDir();
ASSERT_EQ(writeLength, logFileDir.length());
ASSERT_STREQ(buffer, logFileDir.c_str());
contextImpl->SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL4);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
logFileDir = contextImpl->GetLogFileDir();
ASSERT_EQ(writeLength, logFileDir.length());
ASSERT_STREQ(buffer, logFileDir.c_str());
contextImpl->SwitchArea(ABILITY_RUNTIME_AREA_MODE_EL5);
code = OH_AbilityRuntime_ApplicationContextGetLogFileDir(buffer, BUFFER_SIZE, &writeLength);
ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR);
logFileDir = contextImpl->GetLogFileDir();
ASSERT_EQ(writeLength, logFileDir.length());
ASSERT_STREQ(buffer, logFileDir.c_str());
}
}
@@ -1838,7 +1838,6 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, GetCloudFileDirTest_004, Test
ASSERT_STREQ(buffer, cloudFileDir.c_str());
}
/**
* @tc.number: GetResourceDirTest_001
* @tc.desc: Function test with applicationContext is nullptr
@@ -303,7 +303,7 @@ HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_SetMissionContinueState_02
if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
EXPECT_EQ(ret, ERR_OK);
}
wptr<IRemoteObject> token(new IPCObjectStub());
context_->SetWeakSessionToken(token);
context_->SetMissionContinueState(state);
@@ -339,7 +339,7 @@ HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_SetMissionContinueState_03
if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
EXPECT_EQ(ret, ERR_OK);
}
wptr<IRemoteObject> token(new IPCObjectStub());
context_->SetWeakSessionToken(token);
context_->SetMissionContinueState(state);
@@ -1148,6 +1148,30 @@ HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetCloudFileDir_0200, Func
EXPECT_EQ(ret, "");
}
/**
* @tc.number: Ability_Context_Impl_GetLogFileDir_0100
* @tc.name: GetLogFileDir
* @tc.desc: Get Log File Dir sucess
*/
HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetLogFileDir_0100, Function | MediumTest | Level1)
{
context_->SetStageContext(mock_);
auto ret = context_->GetLogFileDir();
EXPECT_EQ(ret, "/log");
}
/**
* @tc.number: Ability_Context_Impl_GetLogFileDir_0200
* @tc.name: GetLogFileDir
* @tc.desc: Get Log File Dir failed
*/
HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetLogFileDir_0200, Function | MediumTest | Level1)
{
context_->SetStageContext(nullptr);
auto ret = context_->GetLogFileDir();
EXPECT_EQ(ret, "");
}
/**
* @tc.number: Ability_Context_Impl_IsUpdatingConfigurations_0100
* @tc.name: IsUpdatingConfigurations
@@ -215,6 +215,10 @@ public:
{
return "MockAbilityContext for tdd";
}
virtual std::string GetLogFileDir()
{
return "MockAbilityContext for tdd";
}
virtual sptr<IRemoteObject> GetToken()
{
return nullptr;
@@ -81,6 +81,11 @@ std::string MockContext::GetCloudFileDir()
return "/cloud";
}
std::string MockContext::GetLogFileDir()
{
return "/log";
}
std::shared_ptr<Context> MockContext::CreateModuleContext(const std::string &moduleName)
{
return nullptr;
@@ -53,6 +53,8 @@ public:
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
@@ -112,6 +112,11 @@ std::string MockAbilityRuntimeContext::GetCloudFileDir()
return {};
};
std::string MockAbilityRuntimeContext::GetLogFileDir()
{
return {};
}
sptr<IRemoteObject> MockAbilityRuntimeContext::GetToken()
{
return {};
@@ -50,6 +50,7 @@ public:
std::string GetPreferencesDir() override;
std::string GetDistributedFilesDir() override;
std::string GetCloudFileDir() override;
std::string GetLogFileDir() override;
sptr<IRemoteObject> GetToken() override;
void SetToken(const sptr<IRemoteObject> &token) override;
void SwitchArea(int mode) override;
@@ -498,6 +498,23 @@ HWTEST_F(ContextImplTest, GetCloudFileDir_0100, TestSize.Level1)
TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
}
/**
* @tc.name: GetLogFileDir_0100
* @tc.desc: Get log directory basic test.
* @tc.type: FUNC
*/
HWTEST_F(ContextImplTest, GetLogFileDir_0100, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
auto contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
EXPECT_NE(contextImpl, nullptr);
auto logDir = contextImpl->GetLogFileDir();
EXPECT_EQ(logDir, "/data/storage/el2/log");
TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
}
/**
* @tc.name: GetBaseDir_0100
* @tc.desc: Get base directory basic test.