!14083 【元能力】【测试用例】AppStateObserverManager TDD补充

Merge pull request !14083 from zhangzezhong/znzd_0501_007
This commit is contained in:
openharmony_ci
2025-05-09 06:25:47 +00:00
committed by Gitee
9 changed files with 2150 additions and 40 deletions
+1
View File
@@ -487,6 +487,7 @@ group("unittest") {
"app_spawn_client_second_test:unittest",
"app_spawn_client_test:unittest",
"app_state_observer_manager_test:unittest",
"app_state_observer_manager_test_second:unittest",
"app_utils_test:unittest",
"appkit:unittest",
"application_context_second_test:unittest",
@@ -63,6 +63,7 @@ public:
{
return {};
}
MOCK_METHOD(void, OnProcessReused, (const ProcessData &processData), (override));
};
class AppForegroundStateObserver : public AppForegroundStateObserverStub {
public:
@@ -1481,6 +1482,38 @@ HWTEST_F(AppSpawnSocketTest, OnObserverDied_0300, TestSize.Level1)
EXPECT_EQ(0, appForegroundStateObserverSetSize);
}
/**
* @tc.name: OnObserverDied_0400
* @tc.desc: Test when observer is not nullptr and type is APP_FOREGROUND_STATE_OBSERVER.
* @tc.type: FUNC
*/
HWTEST_F(AppSpawnSocketTest, OnObserverDied_0400, TestSize.Level1)
{
sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
wptr<IRemoteObject> remote(remoteObject);
ObserverType type = ObserverType::APP_FOREGROUND_STATE_OBSERVER;
DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
auto appForegroundStateObserverSetSize =
DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverMap_.size();
EXPECT_EQ(0, appForegroundStateObserverSetSize);
}
/**
* @tc.name: OnObserverDied_0500
* @tc.desc: Test when observer is not nullptr and type is undefined.
* @tc.type: FUNC
*/
HWTEST_F(AppSpawnSocketTest, OnObserverDied_0500, TestSize.Level1)
{
sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
wptr<IRemoteObject> remote(remoteObject);
ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, static_cast<ObserverType>(100));
auto appForegroundStateObserverSetSize =
DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverMap_.size();
EXPECT_EQ(0, appForegroundStateObserverSetSize);
}
/*
* Feature: AppStateObserverManager
* Function: OnAppCacheStateChanged
@@ -1583,37 +1616,7 @@ HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_004, TestSize.Level2)
manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessReused
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessReused
* EnvConditions: NA
* CaseDescription: Verify OnProcessReused
*/
HWTEST_F(AppSpawnSocketTest, OnProcessReused_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
ASSERT_NE(manager, nullptr);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
manager->OnProcessReused(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessReused
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessReused
* EnvConditions: NA
* CaseDescription: Verify OnProcessReused
*/
HWTEST_F(AppSpawnSocketTest, OnProcessReused_002, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
ASSERT_NE(manager, nullptr);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
manager->Init();
manager->OnProcessReused(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: AddObserverCount
@@ -1629,9 +1632,10 @@ HWTEST_F(AppSpawnSocketTest, AddObserverCount_001, TestSize.Level2)
int32_t uid = 1000;
manager->AddObserverCount(uid);
ASSERT_EQ(manager->observerCountMap_[uid], 1);
ASSERT_EQ(manager->observerAmount_, 1);
EXPECT_EQ(manager->observerCountMap_[uid], 1);
EXPECT_EQ(manager->observerAmount_, 1);
}
/*
* Feature: AppStateObserverManager
* Function: DecreaseObserverCount
@@ -1647,13 +1651,14 @@ HWTEST_F(AppSpawnSocketTest, DecreaseObserverCount_001, TestSize.Level2)
int32_t uid = 1000;
manager->AddObserverCount(uid);
ASSERT_EQ(manager->observerCountMap_[uid], 1);
ASSERT_EQ(manager->observerAmount_, 1);
EXPECT_EQ(manager->observerCountMap_[uid], 1);
EXPECT_EQ(manager->observerAmount_, 1);
manager->DecreaseObserverCount(uid);
ASSERT_TRUE(manager->observerCountMap_.empty());
ASSERT_EQ(manager->observerAmount_, 0);
EXPECT_TRUE(manager->observerCountMap_.empty());
EXPECT_EQ(manager->observerAmount_, 0);
}
/*
* Feature: AppStateObserverManager
* Function: WrapAppStateData
@@ -1675,5 +1680,89 @@ HWTEST_F(AppSpawnSocketTest, WrapAppStateData_001, TestSize.Level2)
EXPECT_EQ(data.bundleName, "com.test.app");
EXPECT_EQ(data.state, static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND));
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnProcessResued
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnProcessResued
* EnvConditions: NA
* CaseDescription: Verify HandleOnProcessResued
*/
HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto* mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer = mockObserver;
manager->appStateObserverMap_[observer] = AppStateObserverInfo{0, {"com.example"}};
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(0);
manager->HandleOnProcessResued(nullptr);
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnProcessResued
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnProcessResued
* EnvConditions: NA
* CaseDescription: Verify HandleOnProcessResued
*/
HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_002, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
ASSERT_NE(manager, nullptr);
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
std::vector<std::string> bundleNames{"com.ohos.unittest"};
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(1);
manager->HandleOnProcessResued(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnProcessResued
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnProcessResued
* EnvConditions: NA
* CaseDescription: Verify HandleOnProcessResued
*/
HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_003, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
ASSERT_NE(manager, nullptr);
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
std::vector<std::string> bundleNames;
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(1);
manager->HandleOnProcessResued(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnProcessResued
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnProcessResued
* EnvConditions: NA
* CaseDescription: Verify HandleOnProcessResued
*/
HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_004, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
ASSERT_NE(manager, nullptr);
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
std::vector<std::string> bundleNames{"com.ohos.other"};
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(0);
manager->HandleOnProcessResued(appRecord);
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -0,0 +1,88 @@
# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
import("//foundation/ability/ability_runtime/ability_runtime.gni")
module_output_path = "ability_runtime/ability_runtime/appmgr"
ohos_unittest("app_state_observer_manager_test_second") {
module_out_path = module_output_path
cflags_cc = []
include_dirs = [
"${ability_runtime_services_path}/appmgr/include",
"${ability_runtime_innerkits_path}/app_manager/include/appmgr",
"${ability_runtime_path}/tools/test/unittest/ability_delegator/",
"${ability_runtime_test_path}/unittest/appkit/ohos_application_test/",
"${ability_runtime_test_path}/unittest/ability_foreground_state_observer_stub_test",
"${ability_runtime_test_path}/unittest/app_state_observer_manager_test_second",
"${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include/",
]
sources =
[ "${ability_runtime_services_path}/appmgr/src/ams_mgr_scheduler.cpp" ]
sources += [ "app_state_observer_manager_test_second.cpp" ]
configs = [ "${ability_runtime_test_path}/unittest:appmgr_test_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${ability_runtime_innerkits_path}/app_manager:app_manager",
"${ability_runtime_path}/services/abilitymgr:abilityms_target",
"${ability_runtime_services_path}/appmgr:libappms",
"${ability_runtime_services_path}/common:app_util",
"${ability_runtime_services_path}/common:perm_verification",
"${ability_runtime_services_path}/common:task_handler_wrap",
"${ability_runtime_test_path}/unittest:appmgr_test_source",
]
external_deps = [
"ability_runtime:ability_manager",
"access_token:libaccesstoken_sdk",
"appspawn:appspawn_client",
"bundle_framework:appexecfwk_core",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
defines = [ "AMS_LOG_TAG = \"AppMgrService\"" ]
if (ability_command_for_test) {
defines += [ "ABILITY_COMMAND_FOR_TEST" ]
}
if (ability_runtime_graphics) {
defines += [ "SUPPORT_GRAPHICS" ]
deps += []
external_deps += [
"i18n:intl_util",
"window_manager:libwm",
]
}
if (ability_runtime_upms) {
deps += [
"${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr",
]
}
}
group("unittest") {
testonly = true
deps = [ ":app_state_observer_manager_test_second" ]
}
@@ -0,0 +1,585 @@
/*
* Copyright (c) 2021-2022 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_foreground_state_observer_proxy.h"
#define private public
#include "app_state_observer_manager.h"
#undef private
#include "app_foreground_state_observer_proxy.h"
#include "app_foreground_state_observer_stub.h"
#include "application_state_observer_stub.h"
#include "iapplication_state_observer.h"
#include "iremote_broker.h"
#include "mock_ability_foreground_state_observer_server_stub.h"
#include "mock_i_remote_object.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace AppExecFwk {
namespace {
const int BUNDLE_NAME_LIST_MAX_SIZE = 128;
}
class MockApplicationStateObserver : public IApplicationStateObserver {
public:
MockApplicationStateObserver() = default;
virtual ~MockApplicationStateObserver() = default;
MOCK_METHOD(void, OnForegroundApplicationChanged, (const AppStateData &appStateData), (override));
MOCK_METHOD(void, OnAbilityStateChanged, (const AbilityStateData &abilityStateData), (override));
MOCK_METHOD(void, OnExtensionStateChanged, (const AbilityStateData &abilityStateData), (override));
MOCK_METHOD(void, OnProcessCreated, (const ProcessData &processData), (override));
MOCK_METHOD(void, OnProcessStateChanged, (const ProcessData &processData), (override));
MOCK_METHOD(void, OnProcessDied, (const ProcessData &processData), (override));
MOCK_METHOD(void, OnApplicationStateChanged, (const AppStateData &appStateData), (override));
MOCK_METHOD(void, OnAppStateChanged, (const AppStateData &appStateData), (override));
MOCK_METHOD(void, OnAppStopped, (const AppStateData &appStateData), (override));
MOCK_METHOD(void, OnAppStarted, (const AppStateData &appStateData), (override));
MOCK_METHOD(void, OnProcessReused, (const ProcessData &processData), (override));
MOCK_METHOD(void, OnWindowHidden, (const ProcessData &processData), (override));
MOCK_METHOD(void, OnWindowShow, (const ProcessData &processData), (override));
sptr<IRemoteObject> AsObject() override
{
return {};
}
};
class MockTaskHandlerWrap : public AAFwk::TaskHandlerWrap {
public:
explicit MockTaskHandlerWrap(const std::string& queueName = "") : TaskHandlerWrap(queueName) {};
virtual ~MockTaskHandlerWrap() {};
std::shared_ptr<AAFwk::InnerTaskHandle> SubmitTaskInner(
std::function<void()>&& task, const AAFwk::TaskAttribute& taskAttr) override
{
task();
return nullptr;
}
bool CancelTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle>& taskHandle) override
{
return false;
}
void WaitTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle>& taskHandle) override
{
return;
}
uint64_t GetTaskCount() override
{
return tasks_.size();
}
};
class AppForegroundStateObserver : public AppForegroundStateObserverStub {
public:
AppForegroundStateObserver() = default;
virtual ~AppForegroundStateObserver() = default;
void OnAppStateChanged(const AppStateData &appStateData) override
{}
};
class AppStateObserverManagerTestSecond : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
std::shared_ptr<AppRunningRecord> MockAppRecord();
sptr<IApplicationStateObserver> observer_ {nullptr};
};
void AppStateObserverManagerTestSecond::SetUpTestCase()
{}
void AppStateObserverManagerTestSecond::TearDownTestCase()
{}
void AppStateObserverManagerTestSecond::SetUp()
{
sptr<IApplicationStateObserver> observer_ = new MockApplicationStateObserver();
}
void AppStateObserverManagerTestSecond::TearDown()
{}
std::shared_ptr<AppRunningRecord> AppStateObserverManagerTestSecond::MockAppRecord()
{
ApplicationInfo appInfo;
appInfo.accessTokenId = 1;
std::shared_ptr<ApplicationInfo> info = std::make_shared<ApplicationInfo>(appInfo);
info->accessTokenId = 1;
std::shared_ptr<AppRunningRecord> appRecord = std::make_shared<AppRunningRecord>(info, 0, "process");
std::shared_ptr<PriorityObject> priorityObject = std::make_shared<PriorityObject>();
priorityObject->SetPid(1);
appRecord->priorityObject_ = priorityObject;
appRecord->SetUid(1);
appRecord->SetState(ApplicationState::APP_STATE_CREATE);
appRecord->SetContinuousTaskAppState(false);
appRecord->SetKeepAliveEnableState(false);
appRecord->SetKeepAliveDkv(false);
appRecord->SetEmptyKeepAliveAppState(false);
appRecord->SetRequestProcCode(1);
appRecord->isFocused_ = false;
return appRecord;
}
/*
* Feature: AppStateObserverManager
* Function: OnAppStarted
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnAppStarted
* EnvConditions: NA
* CaseDescription: Verify OnAppStarted
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnAppStarted_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
std::vector<std::string> bundleNames;
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
EXPECT_CALL(*mockObserver, OnAppStarted(_)).Times(1);
manager->OnAppStarted(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnAppStopped
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnAppStopped
* EnvConditions: NA
* CaseDescription: Verify OnAppStopped
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnAppStopped_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
std::vector<std::string> bundleNames;
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
EXPECT_CALL(*mockObserver, OnAppStopped(_)).Times(1);
manager->OnAppStopped(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnAppStateChanged
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnAppStateChanged
* EnvConditions: NA
* CaseDescription: Verify OnAppStateChanged
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnAppStateChanged_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
ApplicationState state = ApplicationState::APP_STATE_FOREGROUND;
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnForegroundApplicationChanged(_)).Times(1);
EXPECT_CALL(*mockObserver, OnAppStateChanged(_)).Times(1);
manager->OnAppStateChanged(appRecord, state, true, false);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessDied
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessDied
* EnvConditions: NA
* CaseDescription: Verify OnProcessDied
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnProcessDied_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnProcessDied(_)).Times(1);
manager->OnProcessDied(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnWindowShow
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnWindowShow
* EnvConditions: NA
* CaseDescription: Verify OnWindowShow
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnWindowShow_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnWindowShow(_)).Times(1);
manager->OnWindowShow(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnWindowHidden
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnWindowHidden
* EnvConditions: NA
* CaseDescription: Verify OnWindowHidden
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnWindowHidden_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnWindowHidden(_)).Times(1);
manager->OnWindowHidden(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessCreated
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessCreated
* EnvConditions: NA
* CaseDescription: Verify OnProcessCreated
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnProcessCreated_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnProcessCreated(_)).Times(1);
manager->OnProcessCreated(appRecord, false);
}
/*
* Feature: AppStateObserverManager
* Function: OnChildProcessDied
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnChildProcessDied
* EnvConditions: NA
* CaseDescription: Verify OnChildProcessDied
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnChildProcessDied_001, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessDied(_)).Times(1);
manager->OnChildProcessDied(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnChildProcessDied
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnChildProcessDied
* EnvConditions: NA
* CaseDescription: Verify OnChildProcessDied
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnChildProcessDied_002, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
manager->handler_ = nullptr;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessDied(_)).Times(0);
manager->OnChildProcessDied(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnChildProcessCreated
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnChildProcessCreated
* EnvConditions: NA
* CaseDescription: Verify OnChildProcessCreated
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnChildProcessCreated_001, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessCreated(_)).Times(1);
manager->OnChildProcessCreated(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnChildProcessCreated
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnChildProcessCreated
* EnvConditions: NA
* CaseDescription: Verify OnChildProcessCreated
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnChildProcessCreated_002, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
manager->handler_ = nullptr;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessCreated(_)).Times(0);
manager->OnChildProcessCreated(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnChildProcessCreated
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnChildProcessCreated
* EnvConditions: NA
* CaseDescription: Verify HandleOnChildProcessCreated
*/
HWTEST_F(AppStateObserverManagerTestSecond, HandleOnChildProcessCreated_001, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessCreated(_)).Times(1);
manager->HandleOnChildProcessCreated(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: HandleOnChildProcessDied
* SubFunction: NA
* FunctionPoints: AppStateObserverManager HandleOnChildProcessDied
* EnvConditions: NA
* CaseDescription: Verify HandleOnChildProcessDied
*/
HWTEST_F(AppStateObserverManagerTestSecond, HandleOnChildProcessDied_001, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
ChildProcessRequest request;
request.srcEntry = "test.js";
request.childProcessType = CHILD_PROCESS_TYPE_JS;
request.childProcessCount = 1;
std::shared_ptr<ChildProcessRecord> childRecord =
std::make_shared<ChildProcessRecord>(1, request, appRecord);
EXPECT_CALL(*mockObserver, OnProcessDied(_)).Times(1);
manager->HandleOnChildProcessDied(childRecord);
}
/*
* Feature: AppStateObserverManager
* Function: WrapChildProcessData
* SubFunction: NA
* FunctionPoints: AppStateObserverManager WrapChildProcessData
* EnvConditions: NA
* CaseDescription: Verify WrapChildProcessData
*/
HWTEST_F(AppStateObserverManagerTestSecond, WrapChildProcessData_001, TestSize.Level1)
{
auto manager = std::make_shared<AppStateObserverManager>();
std::shared_ptr<ChildProcessRecord> nullChildRecord = nullptr;
ProcessData processData;
EXPECT_EQ(manager->WrapChildProcessData(processData, nullChildRecord), ERR_INVALID_VALUE);
}
/*
* Feature: AppStateObserverManager
* Function: DecreaseObserverCount
* SubFunction: NA
* FunctionPoints: AppStateObserverManager DecreaseObserverCount
* EnvConditions: NA
* CaseDescription: Verify DecreaseObserverCount
*/
HWTEST_F(AppStateObserverManagerTestSecond, DecreaseObserverCount_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
sptr<IApplicationStateObserver> obs = new MockApplicationStateObserver();
int32_t uid = 1000;
int32_t del_uid = 1001;
manager->AddObserverCount(uid);
EXPECT_EQ(manager->observerAmount_, 1);
manager->DecreaseObserverCount(del_uid);
EXPECT_EQ(manager->observerAmount_, 1);
}
/*
* Feature: AppStateObserverManager
* Function: AddObserverCount
* SubFunction: NA
* FunctionPoints: AppStateObserverManager AddObserverCount
* EnvConditions: NA
* CaseDescription: Verify AddObserverCount
*/
HWTEST_F(AppStateObserverManagerTestSecond, AddObserverCount_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
sptr<IApplicationStateObserver> obs = new MockApplicationStateObserver();
int32_t uid = 1000;
manager->observerCountMap_.emplace(uid, 1);
manager->AddObserverCount(uid);
EXPECT_EQ(manager->observerCountMap_[uid], 2);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessReused
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessReused
* EnvConditions: NA
* CaseDescription: Verify OnProcessReused with mock handler
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnProcessReused_001, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
manager->handler_ = mockHandler;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(1);
manager->OnProcessReused(appRecord);
}
/*
* Feature: AppStateObserverManager
* Function: OnProcessReused
* SubFunction: NA
* FunctionPoints: AppStateObserverManager OnProcessReused
* EnvConditions: NA
* CaseDescription: Verify OnProcessReused with null handler
*/
HWTEST_F(AppStateObserverManagerTestSecond, OnProcessReused_002, TestSize.Level2)
{
auto manager = std::make_shared<AppStateObserverManager>();
manager->handler_ = nullptr;
auto mockObserver = new MockApplicationStateObserver();
sptr<IApplicationStateObserver> observer(mockObserver);
std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
appRecord->mainBundleName_ = "com.ohos.unittest";
manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, {"com.ohos.unittest"}});
EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(0);
manager->OnProcessReused(appRecord);
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -16,7 +16,8 @@
#include <gtest/gtest.h>
#include "ecological_rule/ability_ecological_rule_mgr_service_param.h"
#include "interceptor/ecological_rule_interceptor.h"
#include "start_ability_utils.h"
#include "parameters.h"
using namespace testing;
using namespace testing::ext;
using namespace OHOS::AppExecFwk;
@@ -24,6 +25,8 @@ using ErmsCallerInfo = OHOS::EcologicalRuleMgrService::AbilityCallerInfo;
namespace OHOS {
namespace AAFwk {
constexpr const char* ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE =
"persist.sys.abilityms.support.ecologicalrulemgrservice";
class EcologicalRuleInterceptorTest : public testing::Test {
public:
static void SetUpTestCase();
@@ -95,6 +98,413 @@ HWTEST_F(EcologicalRuleInterceptorTest, EcologicalRuleInterceptorTest_GetAppType
auto result = EcologicalRuleInterceptor::GetAppTypeByBundleType(bundleType);
EXPECT_EQ(result, ErmsCallerInfo::TYPE_INVALID);
}
/**
* @tc.name: QueryAtomicServiceStartupRule_001
* @tc.desc: Tests the first branch in QueryAtomicServiceStartupRule where caller and target bundle names are the same
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_001, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
ElementName element("", bundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName);
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
AtomicServiceStartupRule rule;
sptr<Want> replaceWant = nullptr;
ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant);
EXPECT_EQ(result, ERR_INVALID_CALLER);
}
/**
* @tc.name: QueryAtomicServiceStartupRule_002
* @tc.desc: Tests the second branch in QueryAtomicServiceStartupRule where ERMS is not supported
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, QueryAtomicServiceStartupRule_002, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string targetBundleName = "com.example.target";
std::string callerBundleName = "com.example.caller";
ElementName element("", targetBundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
AtomicServiceStartupRule rule;
sptr<Want> replaceWant = nullptr;
OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false");
ErrCode result = interceptor->QueryAtomicServiceStartupRule(want, callerToken, userId, rule, replaceWant);
EXPECT_EQ(result, ERR_CAPABILITY_NOT_SUPPORT);
}
/**
* @tc.name: GetEcologicalTargetInfo_001
* @tc.desc: Tests GetEcologicalTargetInfo with non-null StartAbilityInfo matching the want
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalTargetInfo_001, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
std::string abilityName = "MainAbility";
ElementName element("", bundleName, abilityName);
want.SetElement(element);
std::string testFeature= "testFeature";
int testType = 123;
want.SetParam("send_to_erms_targetLinkFeature", testFeature);
want.SetParam("send_to_erms_targetLinkType", testType);
StartAbilityUtils::startAbilityInfo = std::make_shared<StartAbilityInfo>();
StartAbilityUtils::startAbilityInfo->abilityInfo.bundleName = bundleName;
StartAbilityUtils::startAbilityInfo->abilityInfo.name = abilityName;
StartAbilityUtils::startAbilityInfo->abilityInfo.applicationInfo.appDistributionType = "test_dist_type";
StartAbilityUtils::startAbilityInfo->abilityInfo.applicationInfo.appProvisionType = "test_provision_type";
StartAbilityUtils::startAbilityInfo->abilityInfo.applicationInfo.bundleType =
static_cast<AppExecFwk::BundleType>(AppExecFwk::BundleType::APP);
StartAbilityUtils::startAbilityInfo->abilityInfo.type = AppExecFwk::AbilityType::PAGE;
StartAbilityUtils::startAbilityInfo->abilityInfo.extensionAbilityType = ExtensionAbilityType::SERVICE;
StartAbilityUtils::startAbilityInfo->abilityInfo.applicationInfo.applicationReservedFlag =
static_cast<AppExecFwk::ApplicationFlag>(123);
ErmsCallerInfo callerInfo;
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = nullptr;
interceptor->GetEcologicalTargetInfo(want, abilityInfo, callerInfo);
EXPECT_EQ(callerInfo.targetLinkFeature, "testFeature");
EXPECT_EQ(callerInfo.targetLinkType, 123);
EXPECT_EQ(callerInfo.targetAppDistType, "test_dist_type");
EXPECT_EQ(callerInfo.targetAppProvisionType, "test_provision_type");
EXPECT_EQ(callerInfo.targetAppType, ErmsCallerInfo::TYPE_HARMONY_APP);
EXPECT_EQ(callerInfo.targetAbilityType, AppExecFwk::AbilityType::PAGE);
StartAbilityUtils::startAbilityInfo = nullptr;
}
/**
* @tc.name: GetEcologicalTargetInfo_002
* @tc.desc: Tests GetEcologicalTargetInfo with non-null abilityInfo when StartAbilityInfo doesn't match
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalTargetInfo_002, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
std::string abilityName = "MainAbility";
ElementName element("", bundleName, abilityName);
want.SetElement(element);
std::string testFeature = "testFeature";
int testType = 123;
want.SetParam("send_to_erms_targetLinkFeature", testFeature);
want.SetParam("send_to_erms_targetLinkType", testType);
StartAbilityUtils::startAbilityInfo = std::make_shared<StartAbilityInfo>();
StartAbilityUtils::startAbilityInfo->abilityInfo.bundleName = "different.bundle";
StartAbilityUtils::startAbilityInfo->abilityInfo.name = "DifferentAbility";
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
abilityInfo->applicationInfo.appDistributionType = "test_dist_type2";
abilityInfo->applicationInfo.appProvisionType = "test_provision_type2";
abilityInfo->applicationInfo.bundleType =
static_cast<AppExecFwk::BundleType>(AppExecFwk::BundleType::ATOMIC_SERVICE);
abilityInfo->type = AppExecFwk::AbilityType::SERVICE;
abilityInfo->extensionAbilityType = OHOS::AppExecFwk::ExtensionAbilityType::SERVICE;
abilityInfo->applicationInfo.applicationReservedFlag = static_cast<AppExecFwk::ApplicationFlag>(456);
ErmsCallerInfo callerInfo;
interceptor->GetEcologicalTargetInfo(want, abilityInfo, callerInfo);
EXPECT_EQ(callerInfo.targetLinkFeature, "testFeature");
EXPECT_EQ(callerInfo.targetLinkType, 123);
EXPECT_EQ(callerInfo.targetAppDistType, "test_dist_type2");
EXPECT_EQ(callerInfo.targetAppProvisionType, "test_provision_type2");
EXPECT_EQ(callerInfo.targetAppType, ErmsCallerInfo::TYPE_ATOM_SERVICE);
EXPECT_EQ(callerInfo.targetAbilityType, AppExecFwk::AbilityType::SERVICE);
StartAbilityUtils::startAbilityInfo = nullptr;
}
/**
* @tc.name: GetEcologicalTargetInfo_003
* @tc.desc: Tests GetEcologicalTargetInfo with null abilityInfo and no matching StartAbilityInfo
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalTargetInfo_003, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
std::string abilityName = "MainAbility";
ElementName element("", bundleName, abilityName);
want.SetElement(element);
std::string testFeature = "testFeature";
int testType = 123;
want.SetParam("send_to_erms_targetLinkFeature", testFeature);
want.SetParam("send_to_erms_targetLinkType", testType);
StartAbilityUtils::startAbilityInfo = std::make_shared<StartAbilityInfo>();
StartAbilityUtils::startAbilityInfo->abilityInfo.bundleName = "different.bundle";
StartAbilityUtils::startAbilityInfo->abilityInfo.name = "DifferentAbility";
ErmsCallerInfo callerInfo;
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = nullptr;
interceptor->GetEcologicalTargetInfo(want, abilityInfo, callerInfo);
EXPECT_EQ(callerInfo.targetLinkFeature, "testFeature");
EXPECT_EQ(callerInfo.targetLinkType, 123);
EXPECT_EQ(callerInfo.targetAppDistType, "");
EXPECT_EQ(callerInfo.targetAppProvisionType, "");
EXPECT_EQ(callerInfo.targetAppType, 0);
EXPECT_EQ(callerInfo.targetAbilityType, AppExecFwk::AbilityType::UNKNOWN);
StartAbilityUtils::startAbilityInfo = nullptr;
}
/**
* @tc.name: GetEcologicalCallerInfo_001
* @tc.desc: Tests GetEcologicalCallerInfo with callerToken is null
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_001, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string callerBundleName = "com.example.caller";
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000);
want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000);
ErmsCallerInfo callerInfo;
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken);
EXPECT_EQ(callerInfo.callerAppProvisionType, "");
EXPECT_EQ(callerInfo.callerAppType, 0);
}
/**
* @tc.name: GetEcologicalCallerInfo_002
* @tc.desc: Tests GetEcologicalCallerInfo with sceneboard bundle case
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, GetEcologicalCallerInfo_002, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string callerBundleName = "com.ohos.sceneboard";
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000);
want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000);
ErmsCallerInfo callerInfo;
callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP;
callerInfo.packageName = "";
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
interceptor->GetEcologicalCallerInfo(want, callerInfo, userId, callerToken);
EXPECT_NE(callerInfo.callerAppType, ErmsCallerInfo::TYPE_ATOM_SERVICE);
EXPECT_NE(callerInfo.callerAppType, ErmsCallerInfo::TYPE_APP_SERVICE);
}
/**
* @tc.name: InitErmsCallerInfo_001
* @tc.desc: Tests InitErmsCallerInfo with non-null abilityInfo when want element bundleName is empty
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_001, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
ElementName element("", "", "MainAbility");
want.SetElement(element);
std::string callerBundleName = "com.example.caller";
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000);
want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000);
want.SetParam("send_to_erms_embedded", 1);
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
abilityInfo->bundleName = "com.example.target";
ErmsCallerInfo callerInfo;
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken);
EXPECT_EQ(callerInfo.packageName, callerBundleName);
EXPECT_EQ(callerInfo.uid, 1000);
EXPECT_EQ(callerInfo.pid, 2000);
EXPECT_EQ(callerInfo.embedded, 1);
EXPECT_EQ(callerInfo.userId, userId);
EXPECT_EQ(callerInfo.targetBundleName, "com.example.target");
}
/**
* @tc.name: InitErmsCallerInfo_002
* @tc.desc: Tests InitErmsCallerInfo with non-empty want element bundleName
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_002, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.target";
ElementName element("", bundleName, "MainAbility");
want.SetElement(element);
std::string callerBundleName = "com.example.caller";
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000);
want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000);
want.SetParam("send_to_erms_embedded", 1);
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = std::make_shared<AppExecFwk::AbilityInfo>();
abilityInfo->bundleName = "com.example.different";
ErmsCallerInfo callerInfo;
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
interceptor->InitErmsCallerInfo(want, abilityInfo, callerInfo, userId, callerToken);
EXPECT_EQ(callerInfo.packageName, callerBundleName);
EXPECT_EQ(callerInfo.uid, 1000);
EXPECT_EQ(callerInfo.pid, 2000);
EXPECT_EQ(callerInfo.embedded, 1);
EXPECT_EQ(callerInfo.userId, userId);
EXPECT_EQ(callerInfo.targetBundleName, "");
}
/**
* @tc.name: InitErmsCallerInfo_003
* @tc.desc: Tests InitErmsCallerInfo with null abilityInfo
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, InitErmsCallerInfo_003, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
ElementName element("", "", "MainAbility");
want.SetElement(element);
std::string callerBundleName = "com.example.caller";
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
want.SetParam(Want::PARAM_RESV_CALLER_UID, 1000);
want.SetParam(Want::PARAM_RESV_CALLER_PID, 2000);
want.SetParam("send_to_erms_embedded", 1);
ErmsCallerInfo callerInfo;
sptr<IRemoteObject> callerToken = nullptr;
int userId = 100;
interceptor->InitErmsCallerInfo(want, nullptr, callerInfo, userId, callerToken);
EXPECT_EQ(callerInfo.packageName, callerBundleName);
EXPECT_EQ(callerInfo.uid, 1000);
EXPECT_EQ(callerInfo.pid, 2000);
EXPECT_EQ(callerInfo.embedded, 1);
EXPECT_EQ(callerInfo.userId, userId);
EXPECT_EQ(callerInfo.targetBundleName, "");
}
/**
* @tc.name: DoProcess_001
* @tc.desc: Tests the first branch in DoProcess where skipErms is true
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_001, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
int requestCode = 0;
int userId = 100;
StartAbilityUtils::skipErms = true;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr,
shouldBlockFunc);
ErrCode result = interceptor->DoProcess(param);
EXPECT_EQ(result, ERR_OK);
EXPECT_EQ(StartAbilityUtils::skipErms, false);
}
/**
* @tc.name: DoProcess_002
* @tc.desc: Tests the branch where caller bundle name equals target bundle name
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_002, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
ElementName element("", bundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName);
int requestCode = 0;
int userId = 100;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr,
shouldBlockFunc);
ErrCode result = interceptor->DoProcess(param);
EXPECT_EQ(result, ERR_OK);
}
/**
* @tc.name: DoProcess_003
* @tc.desc: Tests DoProcess with Want and userId parameters (overloaded version)
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_003, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string bundleName = "com.example.test";
ElementName element("", bundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName);
int userId = 100;
bool result = interceptor->DoProcess(want, userId);
EXPECT_TRUE(result);
}
/**
* @tc.name: DoProcess_004
* @tc.desc: Tests DoProcess with different caller and target bundle names
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_004, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string targetBundleName = "com.example.target";
std::string callerBundleName = "com.example.caller";
ElementName element("", targetBundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
int requestCode = 0;
int userId = 100;
StartAbilityUtils::skipErms = true;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, false, nullptr,
shouldBlockFunc);
ErrCode result = interceptor->DoProcess(param);
EXPECT_EQ(result, ERR_OK);
StartAbilityUtils::skipErms = false;
}
/**
* @tc.name: DoProcess_005
* @tc.desc: Tests DoProcess (Want overload) with different caller and target bundle names
* @tc.type: FUNC
* @tc.require: No
*/
HWTEST_F(EcologicalRuleInterceptorTest, DoProcess_005, TestSize.Level1)
{
std::shared_ptr<EcologicalRuleInterceptor> interceptor = std::make_shared<EcologicalRuleInterceptor>();
Want want;
std::string targetBundleName = "com.example.target";
std::string callerBundleName = "com.example.caller";
ElementName element("", targetBundleName, "MainAbility");
want.SetElement(element);
want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
OHOS::system::SetParameter(ABILITY_SUPPORT_ECOLOGICAL_RULEMGRSERVICE, "false");
int userId = 100;
StartAbilityUtils::skipErms = true;
bool result = interceptor->DoProcess(want, userId);
EXPECT_TRUE(result);
StartAbilityUtils::skipErms = false;
}
} // namespace AAFwk
} // namespace OHOS
@@ -22,9 +22,11 @@ ohos_unittest("extension_control_interceptor_test") {
module_out_path = module_output_path
sources = [
"${ability_runtime_services_path}/abilitymgr/src/app_scheduler.cpp",
"${ability_runtime_services_path}/abilitymgr/src/interceptor/extension_control_interceptor.cpp",
"${ability_runtime_services_path}/abilitymgr/src/utils/start_ability_utils.cpp",
"${ability_runtime_services_path}/abilitymgr/src/utils/state_utils.cpp",
"extension_control_interceptor_test.cpp",
"mock/mock_extension_config.cpp",
]
cflags = []
@@ -34,6 +36,10 @@ ohos_unittest("extension_control_interceptor_test") {
"${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper",
"${ability_runtime_innerkits_path}/app_manager/include/appmgr",
"${ability_runtime_services_path}/abilitymgr/include/interceptor",
"${ability_runtime_services_path}/abilitymgr/include",
"${ability_runtime_services_path}/common/include",
"${ability_runtime_path}/test/mock/services_abilitymgr_test/include",
"mock",
]
deps = [
@@ -56,6 +62,7 @@ ohos_unittest("extension_control_interceptor_test") {
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"config_policy:configpolicy_util",
"dsoftbus:softbus_client",
"eventhandler:libeventhandler",
"ffrt:libffrt",
@@ -65,6 +72,7 @@ ohos_unittest("extension_control_interceptor_test") {
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"ipc:ipc_core",
"json:nlohmann_json_static",
"safwk:api_cache_manager",
"samgr:samgr_proxy",
]
@@ -17,9 +17,9 @@
#define private public
#include "extension_control_interceptor.h"
#undef private
#include "mock/status_singleton.h"
#include "ability_record.h"
#include "start_ability_utils.h"
using namespace testing::ext;
using namespace OHOS::AppExecFwk;
@@ -79,5 +79,558 @@ HWTEST_F(ExtensionControlInterceptorTest, DoProcess_001, TestSize.Level1)
EXPECT_EQ(extensionControlInterceptor->DoProcess(param2), ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: DoProcess
* TestPoint: Test early return when GetCallerAbilityInfo returns true
*/
HWTEST_F(ExtensionControlInterceptorTest, DoProcess_002, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.test", "TestAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
sptr<IRemoteObject> token = GetAbilityToken();
auto shouldBlockFunc = []() { return false; };
auto callerInfo = std::make_shared<StartAbilityInfo>();
callerInfo->abilityInfo.type = AppExecFwk::AbilityType::PAGE;
StartAbilityUtils::callerAbilityInfo = callerInfo;
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, token,
shouldBlockFunc);
EXPECT_EQ(extensionControlInterceptor->DoProcess(param), ERR_OK);
StartAbilityUtils::callerAbilityInfo.reset();
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: DoProcess
* TestPoint: Test execution path to ProcessInterceptNew
*/
HWTEST_F(ExtensionControlInterceptorTest, DoProcess_003, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
sptr<IRemoteObject> token = GetAbilityToken();
auto shouldBlockFunc = []() { return false; };
auto callerInfo = std::make_shared<StartAbilityInfo>();
callerInfo->abilityInfo.type = AppExecFwk::AbilityType::EXTENSION;
callerInfo->abilityInfo.bundleName = "com.example.different";
callerInfo->abilityInfo.extensionTypeName = "testExtension";
StartAbilityUtils::callerAbilityInfo = callerInfo;
StatusSingleton::GetInstance().SetHasAbilityAccess(true);
auto targetInfo = std::make_shared<StartAbilityInfo>();
targetInfo->abilityInfo.bundleName = "com.example.target";
targetInfo->abilityInfo.name = "TargetAbility";
targetInfo->abilityInfo.applicationInfo.isSystemApp = true;
StartAbilityUtils::startAbilityInfo = targetInfo;
StatusSingleton::GetInstance().SetHasDefaultAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartDefaultEnable(true);
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, token,
shouldBlockFunc);
EXPECT_EQ(extensionControlInterceptor->DoProcess(param), ERR_OK);
StartAbilityUtils::callerAbilityInfo.reset();
StartAbilityUtils::startAbilityInfo.reset();
StatusSingleton::GetInstance().Reset();
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptOld
* TestPoint: Test behavior when third-party app access is blocked
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptOld_001, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = false;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().SetExtensionStartThirdPartyAppEnable(false);
int32_t result = extensionControlInterceptor->ProcessInterceptOld(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_THIRD_PARTY_APP_FLAG);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptOld
* TestPoint: Test behavior when service access is blocked
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptOld_002, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.targetservice", "ServiceAbility");
want.SetElement(element);
want.SetUri("service://com.example.test/path");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().SetExtensionStartServiceEnable(false);
int32_t result = extensionControlInterceptor->ProcessInterceptOld(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_SERVICE_LIST);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptOld
* TestPoint: Test behavior with DATASHARE extension type
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptOld_003, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.datashare", "DataShareAbility");
want.SetElement(element);
want.SetUri("datashare://com.example.test/data");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::DATASHARE;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().SetExtensionStartThirdPartyAppEnable(true);
StatusSingleton::GetInstance().SetExtensionStartServiceEnable(false);
int32_t result = extensionControlInterceptor->ProcessInterceptOld(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_SERVICE_LIST);
StatusSingleton::GetInstance().SetExtensionStartServiceEnable(true);
result = extensionControlInterceptor->ProcessInterceptOld(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when target is not a system app and extension has thirdPartyAppAccessFlag,
* but starting third party app is not enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_001, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
want.SetUri("test://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = false;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartThirdPartyAppEnableNew(false);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_THIRD_PARTY_APP_FLAG);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when target is not a system app and extension has thirdPartyAppAccessFlag,
* and starting third party app is enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_002, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
want.SetUri("test://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = false;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartThirdPartyAppEnableNew(true);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when target is a SERVICE extension and caller has serviceAccessFlag,
* but starting service is not enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_003, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.service", "ServiceAbility");
want.SetElement(element);
want.SetUri("service://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartServiceEnableNew(false);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_SERVICE_LIST);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when target is a SERVICE extension and caller has serviceAccessFlag,
* and starting service is enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_004, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.service", "ServiceAbility");
want.SetElement(element);
want.SetUri("service://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartServiceEnableNew(true);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when target is a DATASHARE extension and caller has serviceAccessFlag,
* but starting datashare is not enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_005, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.datashare", "DataShareAbility");
want.SetElement(element);
want.SetUri("datashare://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::DATASHARE;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartThirdPartyAppEnableNew(false);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, EXTENSION_BLOCKED_BY_SERVICE_LIST);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when extension has defaultAccessFlag but starting ability is not enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_006, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "DefaultAbility");
want.SetElement(element);
want.SetUri("default://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(false);
StatusSingleton::GetInstance().SetHasDefaultAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartDefaultEnable(false);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_EXTENSION_START_ABILITY_CONTROLEED);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test behavior when extension has defaultAccessFlag and starting ability is enabled
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_007, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "DefaultAbility");
want.SetElement(element);
want.SetUri("default://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(false);
StatusSingleton::GetInstance().SetHasDefaultAccessFlag(true);
StatusSingleton::GetInstance().SetExtensionStartDefaultEnable(true);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: ProcessInterceptNew
* TestPoint: Test default behavior when no access flags are set
*/
HWTEST_F(ExtensionControlInterceptorTest, ProcessInterceptNew_008, TestSize.Level1)
{
std::shared_ptr<ExtensionControlInterceptor> extensionControlInterceptor =
std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "DefaultAbility");
want.SetElement(element);
want.SetUri("default://uri");
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
AppExecFwk::AbilityInfo targetAbilityInfo;
targetAbilityInfo.applicationInfo.isSystemApp = true;
targetAbilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM;
AppExecFwk::AbilityInfo callerAbilityInfo;
callerAbilityInfo.extensionTypeName = "testExtension";
callerAbilityInfo.bundleName = "com.example.testExtension";
StatusSingleton::GetInstance().Reset();
StatusSingleton::GetInstance().SetHasThridPartyAppAccessFlag(false);
StatusSingleton::GetInstance().SetHasServiceAccessFlag(false);
StatusSingleton::GetInstance().SetHasDefaultAccessFlag(false);
int32_t result = extensionControlInterceptor->ProcessInterceptNew(param, targetAbilityInfo, callerAbilityInfo);
EXPECT_EQ(result, ERR_OK);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: GetCallerAbilityInfo
* TestPoint: Test when GetCallerAbilityInfo returns true with non-EXTENSION type
*/
HWTEST_F(ExtensionControlInterceptorTest, GetCallerAbilityInfo_001, TestSize.Level1)
{
auto interceptor = std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
sptr<IRemoteObject> token = GetAbilityToken();
auto callerInfo = std::make_shared<StartAbilityInfo>();
callerInfo->abilityInfo.type = AppExecFwk::AbilityType::PAGE;
StartAbilityUtils::callerAbilityInfo = callerInfo;
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, token,
shouldBlockFunc);
AppExecFwk::AbilityInfo callerAbilityInfo;
bool result = interceptor->GetCallerAbilityInfo(param, callerAbilityInfo);
StartAbilityUtils::callerAbilityInfo.reset();
EXPECT_TRUE(result);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: GetCallerAbilityInfo
* TestPoint: Test when GetCallerAbilityInfo returns true with same bundleName as want
*/
HWTEST_F(ExtensionControlInterceptorTest, GetCallerAbilityInfo_002, TestSize.Level1)
{
auto interceptor = std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.fuzzTest", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
sptr<IRemoteObject> token = GetAbilityToken();
auto callerInfo = std::make_shared<StartAbilityInfo>();
callerInfo->abilityInfo.type = AppExecFwk::AbilityType::EXTENSION;
callerInfo->abilityInfo.bundleName = "com.example.fuzzTest";
StartAbilityUtils::callerAbilityInfo = callerInfo;
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, token,
shouldBlockFunc);
AppExecFwk::AbilityInfo callerAbilityInfo;
bool result = interceptor->GetCallerAbilityInfo(param, callerAbilityInfo);
StartAbilityUtils::callerAbilityInfo.reset();
EXPECT_TRUE(result);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: GetCallerAbilityInfo
* TestPoint: Test when GetCallerAbilityInfo returns false with null token
*/
HWTEST_F(ExtensionControlInterceptorTest, GetCallerAbilityInfo_003, TestSize.Level1)
{
auto interceptor = std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
sptr<IRemoteObject> token = nullptr;
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, token,
shouldBlockFunc);
AppExecFwk::AbilityInfo callerAbilityInfo;
bool result = interceptor->GetCallerAbilityInfo(param, callerAbilityInfo);
EXPECT_FALSE(result);
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: GetTargetAbilityInfo
* TestPoint: Test when startAbilityInfo exists and matches the Want parameters
*/
HWTEST_F(ExtensionControlInterceptorTest, GetTargetAbilityInfo_001, TestSize.Level1)
{
auto interceptor = std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
auto startAbilityInfo = std::make_shared<StartAbilityInfo>();
startAbilityInfo->abilityInfo.bundleName = "com.example.target";
startAbilityInfo->abilityInfo.name = "TargetAbility";
StartAbilityUtils::startAbilityInfo = startAbilityInfo;
AppExecFwk::AbilityInfo targetAbilityInfo;
bool result = interceptor->GetTargetAbilityInfo(param, targetAbilityInfo);
StartAbilityUtils::startAbilityInfo.reset();
EXPECT_FALSE(result);
EXPECT_EQ(targetAbilityInfo.bundleName, "com.example.target");
EXPECT_EQ(targetAbilityInfo.name, "TargetAbility");
}
/*
* Feature: ExtensionControlInterceptorTest
* Function: GetTargetAbilityInfo
* TestPoint: Test when startAbilityInfo doesn't match and we go through normal QueryAbilityInfo path
*/
HWTEST_F(ExtensionControlInterceptorTest, GetTargetAbilityInfo_002, TestSize.Level1)
{
auto interceptor = std::make_shared<ExtensionControlInterceptor>();
Want want;
ElementName element("", "com.example.target", "TargetAbility");
want.SetElement(element);
int requestCode = 1;
int32_t userId = 100;
bool isWithUI = false;
auto shouldBlockFunc = []() { return false; };
AbilityInterceptorParam param = AbilityInterceptorParam(want, requestCode, userId, isWithUI, nullptr,
shouldBlockFunc);
StartAbilityUtils::startAbilityInfo.reset();
AppExecFwk::AbilityInfo targetAbilityInfo;
bool result = interceptor->GetTargetAbilityInfo(param, targetAbilityInfo);
EXPECT_FALSE(result);
}
}
}
@@ -0,0 +1,207 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "extension_config.h"
#include <fstream>
#include "config_policy_utils.h"
#include "hilog_tag_wrapper.h"
#include "json_utils.h"
#include "status_singleton.h"
namespace OHOS {
namespace AAFwk {
namespace {
constexpr const char* EXTENSION_CONFIG_DEFAULT_PATH = "/system/etc/extension_config.json";
constexpr const char* EXTENSION_CONFIG_FILE_PATH = "/etc/extension_config.json";
constexpr const char* EXTENSION_CONFIG_NAME = "extension_config";
constexpr const char* EXTENSION_TYPE_NAME = "extension_type_name";
constexpr const char* EXTENSION_AUTO_DISCONNECT_TIME = "auto_disconnect_time";
// old access flag, deprecated
constexpr const char* EXTENSION_THIRD_PARTY_APP_BLOCKED_FLAG_NAME = "third_party_app_blocked_flag";
constexpr const char* EXTENSION_SERVICE_BLOCKED_LIST_NAME = "service_blocked_list";
constexpr const char* EXTENSION_SERVICE_STARTUP_ENABLE_FLAG = "service_startup_enable_flag";
// new access flag
constexpr const char* ABILITY_ACCESS = "ability_access";
constexpr const char* THIRD_PARTY_APP_ACCESS_FLAG = "third_party_app_access_flag";
constexpr const char* SERVICE_ACCESS_FLAG = "service_access_flag";
constexpr const char* DEFAULT_ACCESS_FLAG = "default_access_flag";
constexpr const char* BLOCK_LIST = "blocklist";
constexpr const char* ALLOW_LIST = "allowlist";
constexpr const char* NETWORK_ACCESS_ENABLE_FLAG = "network_access_enable_flag";
constexpr const char* SA_ACCESS_ENABLE_FLAG = "sa_access_enable_flag";
}
std::string ExtensionConfig::GetExtensionConfigPath() const
{
std::string configPath = "/etc/extension_config.json";
return configPath;
}
void ExtensionConfig::LoadExtensionConfiguration()
{
return;
}
int32_t ExtensionConfig::GetExtensionAutoDisconnectTime(const std::string &extensionTypeName)
{
return DEFAULT_EXTENSION_AUTO_DISCONNECT_TIME;
}
bool ExtensionConfig::IsExtensionStartThirdPartyAppEnable(const std::string &extensionTypeName)
{
TAG_LOGE(AAFwkTag::ABILITYMGR, "testcase IsExtensionStartThirdPartyAppEnable");
return StatusSingleton::GetInstance().isExtensionStartThirdPartyAppEnable_;
}
bool ExtensionConfig::IsExtensionStartServiceEnable(const std::string &extensionTypeName, const std::string &targetUri)
{
return StatusSingleton::GetInstance().isExtensionStartServiceEnable_;
}
void ExtensionConfig::LoadExtensionConfig(const nlohmann::json &object)
{
return;
}
void ExtensionConfig::LoadExtensionAutoDisconnectTime(const nlohmann::json &object,
const std::string &extensionTypeName)
{
return;
}
void ExtensionConfig::LoadExtensionThirdPartyAppBlockedList(const nlohmann::json &object,
std::string extensionTypeName)
{
return;
}
void ExtensionConfig::LoadExtensionServiceBlockedList(const nlohmann::json &object, std::string extensionTypeName)
{
return;
}
bool ExtensionConfig::LoadExtensionAbilityAccess(const nlohmann::json &object, const std::string &extensionTypeName)
{
return true;
}
std::string ExtensionConfig::FormatAccessFlag(const std::optional<bool> &flag)
{
if (!flag.has_value()) {
return "null";
}
return flag.value() ? "true" : "false";
}
void ExtensionConfig::LoadExtensionAllowOrBlockedList(const nlohmann::json &object, const std::string &key,
std::unordered_set<std::string> &list)
{
return;
}
void ExtensionConfig::LoadExtensionNetworkEnable(const nlohmann::json &object,
const std::string &extensionTypeName)
{
return;
}
void ExtensionConfig::LoadExtensionSAEnable(const nlohmann::json &object,
const std::string &extensionTypeName)
{
return;
}
bool ExtensionConfig::HasAbilityAccess(const std::string &extensionTypeName)
{
return StatusSingleton::GetInstance().hasAbilityAccess_;
}
bool ExtensionConfig::HasThridPartyAppAccessFlag(const std::string &extensionTypeName)
{
return StatusSingleton::GetInstance().hasThridPartyAppAccessFlag_;
}
bool ExtensionConfig::HasServiceAccessFlag(const std::string &extensionTypeName)
{
return StatusSingleton::GetInstance().hasServiceAccessFlag_;
}
bool ExtensionConfig::HasDefaultAccessFlag(const std::string &extensionTypeName)
{
return StatusSingleton::GetInstance().hasDefaultAccessFlag_;
}
std::optional<bool> ExtensionConfig::GetSingleAccessFlag(const std::string &extensionTypeName,
std::function<std::optional<bool>(const AbilityAccessItem&)> getAccessFlag)
{
return std::nullopt;
}
bool ExtensionConfig::IsExtensionStartThirdPartyAppEnableNew(const std::string &extensionTypeName,
const std::string &targetUri)
{
return StatusSingleton::GetInstance().isExtensionStartThirdPartyAppEnableNew_;
}
bool ExtensionConfig::IsExtensionStartServiceEnableNew(const std::string &extensionTypeName,
const std::string &targetUri)
{
return StatusSingleton::GetInstance().isExtensionStartServiceEnableNew_;
}
bool ExtensionConfig::IsExtensionStartDefaultEnable(const std::string &extensionTypeName,
const std::string &targetUri)
{
return StatusSingleton::GetInstance().isExtensionStartDefaultEnable_;
}
bool ExtensionConfig::IsExtensionAbilityAccessEnable(const std::string &extensionTypeName,
const std::string &targetUri,
std::function<std::optional<bool>(const AbilityAccessItem&)> getAccessFlag)
{
return true;
}
bool ExtensionConfig::FindTargetUriInList(const AppExecFwk::ElementName &targetElementName,
std::unordered_set<std::string> &list)
{
return true;
}
bool ExtensionConfig::IsExtensionNetworkEnable(const std::string &extensionTypeName)
{
return EXTENSION_NETWORK_ENABLE_FLAG_DEFAULT;
}
bool ExtensionConfig::IsExtensionSAEnable(const std::string &extensionTypeName)
{
return EXTENSION_SA_ENABLE_FLAG_DEFAULT;
}
bool ExtensionConfig::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
{
return true;
}
bool ExtensionConfig::CheckExtensionUriValid(const std::string &uri)
{
return true;
}
}
}
@@ -0,0 +1,169 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_ABILITY_ABILITY_RUNTIME_TEST_UNITTEST_EXTENSION_CONTROL_INTERCEPTOR_TEST_MOCK_STATUS_SINGLETON_H
#define FOUNDATION_ABILITY_ABILITY_RUNTIME_TEST_UNITTEST_EXTENSION_CONTROL_INTERCEPTOR_TEST_MOCK_STATUS_SINGLETON_H
#include <string>
#include <mutex>
#include <memory>
namespace OHOS {
namespace AAFwk {
/**
* @class StatusSingleton
* Singleton class that holds static member variables representing the status of extension configuration
* for testing purposes.
*/
class StatusSingleton {
public:
/**
* Get the singleton instance.
*
* @return The singleton instance of StatusSingleton.
*/
static StatusSingleton& GetInstance()
{
static StatusSingleton instance;
return instance;
}
// Delete copy constructor and assignment operator
StatusSingleton(const StatusSingleton&) = delete;
StatusSingleton& operator=(const StatusSingleton&) = delete;
// Static member variables corresponding to the boolean functions in mock_extension_config.cpp
bool isExtensionStartThirdPartyAppEnable_;
bool isExtensionStartServiceEnable_;
bool isExtensionStartThirdPartyAppEnableNew_;
bool isExtensionStartServiceEnableNew_;
bool isExtensionStartDefaultEnable_;
bool isExtensionNetworkEnable_;
bool isExtensionSAEnable_;
bool isExtensionAbilityAccessEnable_;
bool checkExtensionUriValid_;
bool findTargetUriInList_;
bool readFileInfoJson_;
bool hasAbilityAccess_;
bool hasThridPartyAppAccessFlag_;
bool hasServiceAccessFlag_;
bool hasDefaultAccessFlag_;
void SetExtensionStartThirdPartyAppEnable(bool value)
{
isExtensionStartThirdPartyAppEnable_ = value;
}
void SetExtensionStartServiceEnable(bool value)
{
isExtensionStartServiceEnable_ = value;
}
void SetExtensionStartThirdPartyAppEnableNew(bool value)
{
isExtensionStartThirdPartyAppEnableNew_ = value;
}
void SetExtensionStartServiceEnableNew(bool value)
{
isExtensionStartServiceEnableNew_ = value;
}
void SetExtensionStartDefaultEnable(bool value)
{
isExtensionStartDefaultEnable_ = value;
}
void SetExtensionNetworkEnable(bool value)
{
isExtensionNetworkEnable_ = value;
}
void SetExtensionSAEnable(bool value)
{
isExtensionSAEnable_ = value;
}
void SetExtensionAbilityAccessEnable(bool value)
{
isExtensionAbilityAccessEnable_ = value;
}
void SetCheckExtensionUriValid(bool value)
{
checkExtensionUriValid_ = value;
}
void SetFindTargetUriInList(bool value)
{
findTargetUriInList_ = value;
}
void SetReadFileInfoJson(bool value)
{
readFileInfoJson_ = value;
}
void SetHasAbilityAccess(bool value)
{
hasAbilityAccess_ = value;
}
void SetHasThridPartyAppAccessFlag(bool value)
{
hasThridPartyAppAccessFlag_ = value;
}
void SetHasServiceAccessFlag(bool value)
{
hasServiceAccessFlag_ = value;
}
void SetHasDefaultAccessFlag(bool value)
{
hasDefaultAccessFlag_ = value;
}
// Reset all static members to default values
void Reset()
{
isExtensionStartThirdPartyAppEnable_ = false;
isExtensionStartServiceEnable_ = false;
isExtensionStartThirdPartyAppEnableNew_ = false;
isExtensionStartServiceEnableNew_ = false;
isExtensionStartDefaultEnable_ = false;
isExtensionNetworkEnable_ = false;
isExtensionSAEnable_ = false;
isExtensionAbilityAccessEnable_ = false;
checkExtensionUriValid_ = false;
findTargetUriInList_ = false;
readFileInfoJson_ = false;
hasAbilityAccess_ = false;
hasThridPartyAppAccessFlag_ = false;
hasServiceAccessFlag_ = false;
hasDefaultAccessFlag_ = false;
}
private:
// Private constructor to ensure singleton pattern
StatusSingleton()
{
Reset();
}
};
} // namespace AAFwk
} // namespace OHOS
#endif // FOUNDATION_ABILITY_ABILITY_RUNTIME_TEST_UNITTEST_EXTENSION_CONTROL_INTERCEPTOR_TEST_MOCK_STATUS_SINGLETON_H