diff --git a/BUILD.gn b/BUILD.gn index e30fadd37c70113321998108ac98b23de1ad2eea..383727b7606d1ec913b08399726b0c6e4e1d1196 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -135,6 +135,7 @@ ohos_shared_library("usagestatservice") { "services/common/src/bundle_active_account_helper.cpp", "services/common/src/bundle_active_app_state_obsever.cpp", "services/common/src/bundle_active_binary_search.cpp", + "services/common/src/bundle_active_bundle_mgr_helper.cpp", "services/common/src/bundle_active_continuous_task_observer.cpp", "services/common/src/bundle_active_core.cpp", "services/common/src/bundle_active_debug_mode.cpp", diff --git a/services/common/include/bundle_active_bundle_mgr_helper.h b/services/common/include/bundle_active_bundle_mgr_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..009e5c3c561f27fe1ecc78bd6bfbc4c45e17b92f --- /dev/null +++ b/services/common/include/bundle_active_bundle_mgr_helper.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 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 BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H +#define BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H + +#include "errors.h" +#include "bundle_mgr_interface.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iremote_object.h" +#include "singleton.h" + +#include "bundle_active_log.h" + +namespace OHOS { +namespace DeviceUsageStats { +class BundleActiveBundleMgrHelper : public DelayedSingleton { +public: + /** + * @brief GetNameForUid by uid. + * + * @param uid . + * @param bundleName . + */ + void GetNameForUid(int32_t uid, std::string bundleName); + + /** + * @brief GetBundleInfo by bundleName、flag、userId. + * + * @param bundleName . + * @param flag which type is AppExecFwk::ApplicationFlag. + * @param bundleInfo. + * @param userId which type is AppExecFwk::BundleInfo. + */ + bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, + AppExecFwk::BundleInfo &bundleInfo, int32_t userId); + + /** + * @brief GetApplicationInfo, get bundleName by uid. + * + * @param appName . + * @param flag which type is AppExecFwk::ApplicationFlag. + * @param userId. + * @param appInfo which type is AppExecFwk::ApplicationInfo. + */ + bool GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, + const int userId, AppExecFwk::ApplicationInfo &appInfo); + +private: + bool Connect(); + +private: + sptr bundleMgr_ = nullptr; + std::mutex connectionMutex_; + + DECLARE_DELAYED_SINGLETON(BundleActiveBundleMgrHelper); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H + diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index dfba3f148b72a89e55fcfac081a4da7c9405b076..c3ec9885966832d85d90556d4d6d7a2f7d58fe62 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -242,7 +242,6 @@ private: int64_t realTimeShot_; std::mutex mutex_; std::mutex callbackMutex_; - std::mutex deathRecipientMutex_; std::map> userStatServices_; void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 5f186a5c991ce910e88e25f0745ae5e2bd81aeac..0fb666e6f5bba049ef6b0ba2c9e20fd6e3cc80b7 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -16,7 +16,6 @@ #ifndef BUNDLE_ACTIVE_SERVICE_H #define BUNDLE_ACTIVE_SERVICE_H -#include "bundle_mgr_interface.h" #include "singleton.h" #include "app_mgr_interface.h" @@ -215,7 +214,6 @@ private: #ifdef BGTASKMGR_ENABLE std::shared_ptr continuousTaskObserver_; #endif - sptr sptrBundleMgr_; sptr shutdownCallback_; sptr powerStateCallback_; std::shared_ptr runner_; @@ -224,7 +222,6 @@ private: int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); void InitService(); - ErrCode GetBundleMgrProxy(); ErrCode CheckBundleIsSystemAppAndHasPermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId); ErrCode CheckSystemAppOrNativePermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId); diff --git a/services/common/src/bundle_active_bundle_mgr_helper.cpp b/services/common/src/bundle_active_bundle_mgr_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93eb0ae4b7a657650ff20a7b8fc6caedd65a0d4a --- /dev/null +++ b/services/common/src/bundle_active_bundle_mgr_helper.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2023 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 "bundle_active_bundle_mgr_helper.h" + +#include "accesstoken_kit.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "tokenid_kit.h" + +#include "continuous_task_log.h" + +namespace OHOS { +namespace DeviceUsageStats { +using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED; + +BundleActiveBundleMgrHelper::BundleActiveBundleMgrHelper() +{ +} + +BundleActiveBundleMgrHelper::~BundleActiveBundleMgrHelper() +{ +} + +void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string bundleName) +{ + std::lock_guard lock(connectionMutex_); + if (!Connect()) { + return; + } + if (!bundleMgr_) { + return; + } + bundleMgr_->GetNameForUid(uid, bundleName); + BUNDLE_ACTIVE_LOGD("get Bundle Name: %{public}s", bundleName.c_str()); +} + +bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, + const int userId, AppExecFwk::ApplicationInfo &appInfo) +{ + BUNDLE_ACTIVE_LOGD("start get application info"); + std::lock_guard lock(connectionMutex_); + + if (!Connect()) { + return false; + } + if (!bundleMgr_) { + return false; + } + BUNDLE_ACTIVE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr); + if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) { + return true; + } + return false; +} + +bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, + AppExecFwk::BundleInfo &bundleInfo, int32_t userId) +{ + std::lock_guard lock(connectionMutex_); + + if (!Connect()) { + return false; + } + if (!bundleMgr_) { + return false; + } + if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) { + return true; + } + return false; +} + +bool BundleActiveBundleMgrHelper::Connect() +{ + if (bundleMgr_ != nullptr) { + return true; + } + + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + BUNDLE_ACTIVE_LOGE("get SystemAbilityManager failed"); + return false; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject == nullptr) { + BUNDLE_ACTIVE_LOGE("get Bundle Manager failed"); + return false; + } + + bundleMgr_ = iface_cast(remoteObject); + return bundleMgr_ ? true : false; +} +} // namespace DeviceUsageStats +} // namespace OHOS \ No newline at end of file diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 5f27292be0ee63f06ffca73aae3805ca1b35e872..b12214ea3b5d88c8aa9aaffe50263fef57b3f5a4 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -749,7 +749,7 @@ ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTo void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(deathRecipientMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { BUNDLE_ACTIVE_LOGI("observer nullptr."); return; @@ -775,7 +775,7 @@ void BundleActiveCore::AddObserverDeathRecipient(const sptr & } void BundleActiveCore::RemoveObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(deathRecipientMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { return; } @@ -802,12 +802,12 @@ void BundleActiveCore::OnObserverDied(const wptr &remote) void BundleActiveCore::OnObserverDiedInner(const wptr &remote) { - std::lock_guard lock(deathRecipientMutex_); sptr objectProxy = remote.promote(); if (remote == nullptr || !objectProxy) { BUNDLE_ACTIVE_LOGE("get remote object failed"); return; } + std::lock_guard lock(callbackMutex_); for (const auto& item : groupChangeObservers_) { if (!(item.second)) { continue; diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 5ad925528376b5c08bd4e3ff257431596ea2730f..42a10cf70e4dedf1d68c77731bacee725e4b408d 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -15,7 +15,6 @@ #include "time_service_client.h" #include "power_mgr_client.h" -#include "bundle_mgr_proxy.h" #include "unistd.h" #include "accesstoken_kit.h" @@ -23,6 +22,7 @@ #include "bundle_active_event.h" #include "bundle_active_package_stats.h" #include "bundle_active_account_helper.h" +#include "bundle_active_bundle_mgr_helper.h" #include "bundle_active_service.h" namespace OHOS { @@ -266,13 +266,9 @@ ErrCode BundleActiveService::IsBundleIdle(bool& isBundleIdle, const std::string& // get uid int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); - ErrCode ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("IsBundleIdle Get bundle manager proxy failed!"); - return ret; - } + ErrCode ret = ERR_OK; std::string callingBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, callingBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, callingBundleName); BUNDLE_ACTIVE_LOGI("UID is %{public}d, bundle name is %{public}s", callingUid, callingBundleName.c_str()); // get user id int32_t result = -1; @@ -364,13 +360,8 @@ ErrCode BundleActiveService::SetAppGroup(const std::string& bundleName, int32_t } BUNDLE_ACTIVE_LOGI("SetAppGroup userId is %{public}d", userId); - ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("SetAppGroup Get bundle manager proxy failed!"); - return ret; - } std::string localBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, localBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, localBundleName); if (localBundleName == bundleName) { BUNDLE_ACTIVE_LOGI("SetAppGroup can not set its bundleName"); return ERR_PERMISSION_DENIED; @@ -395,13 +386,8 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vectorGetBundleNameForUid(callingUid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); ErrCode isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId); if (!bundleName.empty() && isSystemAppAndHasPermission == ERR_OK) { int32_t convertedIntervalType = ConvertIntervalType(intervalType); @@ -422,13 +408,8 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vectorGetBundleNameForUid(callingUid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); if (!sptrBundleMgr_->CheckIsSystemAppByUid(callingUid)) { BUNDLE_ACTIVE_LOGE("%{public}s is not sys app", bundleName.c_str()); return ERR_NOT_SYSTEM_APP; @@ -456,13 +437,8 @@ ErrCode BundleActiveService::QueryAppGroup(int32_t& appGroup, std::string& bundl } } if (bundleName.empty()) { - ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("QueryAppGroup Get bundle manager proxy failed!"); - return ret; - } std::string localBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, localBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, localBundleName); if (!sptrBundleMgr_->CheckIsSystemAppByUid(callingUid)) { BUNDLE_ACTIVE_LOGE("%{public}s is not sys app", localBundleName.c_str()); return ERR_NOT_SYSTEM_APP; @@ -507,34 +483,6 @@ ErrCode BundleActiveService::UnRegisterAppGroupCallBack(const sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - BUNDLE_ACTIVE_LOGE("Failed to get system ability mgr."); - return ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - BUNDLE_ACTIVE_LOGE("Failed to get bundle manager service."); - return ERR_GET_SYSTEM_ABILITY_FAILED; - } - sptrBundleMgr_ = iface_cast(remoteObject); - if (!sptrBundleMgr_) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, sptrBundleMgr_"); - return ERR_REMOTE_OBJECT_IF_CAST_FAILED; - } - auto object = sptrBundleMgr_->AsObject(); - if (!object) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, sptrBundleMgr_->AsObject()"); - return ERR_REMOTE_OBJECT_IF_CAST_FAILED; - } - } - return ERR_OK; -} - int32_t BundleActiveService::ConvertIntervalType(const int32_t intervalType) { if (intervalType == PERIOD_BEST_JS) { @@ -548,13 +496,8 @@ int32_t BundleActiveService::ConvertIntervalType(const int32_t intervalType) ErrCode BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId) { - ErrCode errCode = GetBundleMgrProxy(); - if (errCode != ERR_OK) { - BUNDLE_ACTIVE_LOGE("CheckBundleIsSystemAppAndHasPermission Get bundle manager proxy failed!"); - return errCode; - } std::string bundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(uid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(uid, bundleName); int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); if (bundleHasPermission != 0) { @@ -658,19 +601,15 @@ ErrCode BundleActiveService::QueryNotificationEventStats(int64_t beginTime, int6 void BundleActiveService::QueryModuleRecordInfos(BundleActiveModuleRecord& moduleRecord) { - ErrCode errCode = GetBundleMgrProxy(); - if (errCode != ERR_OK) { - return; - } ApplicationInfo appInfo; - bool getInfoIsSuccess = sptrBundleMgr_->GetApplicationInfo(moduleRecord.bundleName_, + bool getInfoIsSuccess = BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfo(moduleRecord.bundleName_, ApplicationFlag::GET_BASIC_APPLICATION_INFO, moduleRecord.userId_, appInfo); if (!getInfoIsSuccess) { BUNDLE_ACTIVE_LOGE("GetApplicationInfo failed!"); return; } BundleInfo bundleInfo; - getInfoIsSuccess = sptrBundleMgr_->GetBundleInfo(moduleRecord.bundleName_, + getInfoIsSuccess = BundleActiveBundleMgrHelper::GetInstance()->GetBundleInfo(moduleRecord.bundleName_, BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, moduleRecord.userId_); if (!getInfoIsSuccess) { BUNDLE_ACTIVE_LOGE("GetBundleInfo failed!"); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index e142220f8c06bc00e831d9b67fe29f22cc6507d8..985ed9544ffbe83b865a904d1b0dcdb79f562ee8 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -50,6 +50,31 @@ ohos_unittest("DeviceUsageStatsTest") { ] } +ohos_unittest("DeviceUsageStatsMultiTest") { + module_out_path = module_output_path + + sources = [ "device_usage_statistics_multi_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "../../:usagestatservice_static", + "../../:usagestatsinner", + "../../:usagestatsutils", + "//third_party/jsoncpp:jsoncpp", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + ohos_unittest("DeviceUsageStatsServiceTest") { module_out_path = module_output_path @@ -158,6 +183,7 @@ group("unittest") { deps = [ ":DeviceUsagePackageUsageTest", ":DeviceUsageStatsMockTest", + ":DeviceUsageStatsMultiTest", ":DeviceUsageStatsServiceTest", ":DeviceUsageStatsTest", ] diff --git a/test/unittest/device_usage_statistics_multi_test.cpp b/test/unittest/device_usage_statistics_multi_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bcf57132b2a03dfa292a5986bd5da741dd0aac93 --- /dev/null +++ b/test/unittest/device_usage_statistics_multi_test.cpp @@ -0,0 +1,362 @@ +/* + * Copyright (c) 2023 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. + */ + +#define private public +#define protected public + +#include + +#include +#include +#include +#include "system_ability_definition.h" + +#include "bundle_active_client.h" +#include "bundle_active_event.h" +#include "app_group_callback_stub.h" +#include "bundle_active_group_map.h" +#include "app_group_callback_info.h" +#include "bundle_active_form_record.h" +#include "bundle_active_event_stats.h" +#include "bundle_active_module_record.h" +#include "bundle_active_package_stats.h" +#include "app_group_callback_proxy.h" +#include "iapp_group_callback.h" + +using namespace testing::ext; +using namespace testing::mt; + +namespace OHOS { +namespace DeviceUsageStats { +static std::string g_defaultBundleName = "com.ohos.camera"; +static std::string g_defaultMoudleName = "defaultmodulename"; +static std::string g_defaultFormName = "defaultformname"; +static int32_t DEFAULT_DIMENSION = 4; +static int32_t PERIOD_YEARLY_JS = 4; +static int32_t ACTIVE_GROUP_DAILY = 2; +static int32_t DEFAULT_USER_ID = 2; +static int32_t MAX_RECORD = 1001; +static int64_t DEFAULT_FORMID = 1; +static int32_t DEFAULT_USERID = 0; +static int32_t COMMON_USERID = 100; +static int64_t LARGE_NUM = 20000000000000; +static int32_t DEFAULT_GROUP = 10; +static std::vector GROUP_TYPE {10, 20, 30, 40, 50}; +static sptr observer = nullptr; + +class DeviceUsageStatisticsMultiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DeviceUsageStatisticsMultiTest::SetUpTestCase(void) +{ +} + +void DeviceUsageStatisticsMultiTest::TearDownTestCase(void) +{ +} + +void DeviceUsageStatisticsMultiTest::SetUp(void) +{ +} + +void DeviceUsageStatisticsMultiTest::TearDown(void) +{ +} + +class TestAppGroupChangeCallback : public AppGroupCallbackStub { +public: + void OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override; +}; + +void TestAppGroupChangeCallback::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) +{ + BUNDLE_ACTIVE_LOGI("TestAppGroupChangeCallback::OnAppGroupChanged!"); + MessageParcel data; + if (!appGroupCallbackInfo.Marshalling(data)) { + BUNDLE_ACTIVE_LOGE("Marshalling fail"); + } + appGroupCallbackInfo.Unmarshalling(data); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_IsBundleIdle_001 + * @tc.desc: isbundleidle + * @tc.type: FUNC + * @tc.require: SR000GGTO5 AR000GH6PG + */ +void MultiTestIsBundleIdle(void) +{ + bool result = false; + int32_t errCode = BundleActiveClient::GetInstance().IsBundleIdle(result, g_defaultBundleName, DEFAULT_USERID); + EXPECT_EQ(result, false); + EXPECT_EQ(errCode, 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_IsBundleIdle_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestIsBundleIdle); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_ReportEvent_001 + * @tc.desc: report a mock event + * @tc.type: FUNC + * @tc.require: SR000GGTO7 SR000GU31B AR000GH6PJ AR000GU380 + */ +void MultiTestReportEvent(void) +{ + BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName, + DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED); + BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID); + BundleActiveEvent eventB(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName, + DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_REMOVED); + BundleActiveClient::GetInstance().ReportEvent(eventB, DEFAULT_USERID); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_ReportEvent_001, Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestReportEvent); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryBundleEvents_001 + * @tc.desc: QueryBundleEvents + * @tc.type: FUNC + * @tc.require: SR000GGTO6 AR000GH6PH + */ +void MultiTestQueryBundleEvents(void) +{ + std::vector result; + BundleActiveClient::GetInstance().QueryBundleEvents(result, 0, LARGE_NUM, DEFAULT_USER_ID); + EXPECT_EQ(result.size() > 0, true); + EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleEvents(result, LARGE_NUM, LARGE_NUM, DEFAULT_USER_ID), 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryBundleEvents_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryBundleEvents); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryCurrentBundleEvents_001 + * @tc.desc: QueryCurrentBundleEvents + * @tc.type: FUNC + * @tc.require: SR000GGTO4 AR000GH6PF + */ +void MultiTestQueryCurrentBundleEvents(void) +{ + std::vector result; + BundleActiveClient::GetInstance().QueryCurrentBundleEvents(result, 0, LARGE_NUM); + EXPECT_EQ(result.size(), 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryCurrentBundleEvents_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryCurrentBundleEvents); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryPackagesStats_001 + * @tc.desc: querypackagestats + * @tc.type: FUNC + * @tc.require: SR000GGTO3 AR000GH6PD + */ +void MultiTestQueryBundleStatsInfoByInterval(void) +{ + std::vector result; + BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, PERIOD_YEARLY_JS, 0, LARGE_NUM); + EXPECT_EQ(result.size(), 0); + EXPECT_NE(BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(result, PERIOD_YEARLY_JS, LARGE_NUM, LARGE_NUM), 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryPackagesStats_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryBundleStatsInfoByInterval); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryBundleStatsInfos_001 + * @tc.desc: QueryBundleStatsInfos + * @tc.type: FUNC + * @tc.require: issuesI5QJD9 + */ +void MultiTestQueryBundleStatsInfos(void) +{ + std::vector result; + BundleActiveClient::GetInstance().QueryBundleStatsInfos(result, PERIOD_YEARLY_JS, 0, LARGE_NUM); + EXPECT_EQ(result.size(), 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryBundleStatsInfos_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryBundleStatsInfos); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryModuleUsageRecords_001 + * @tc.desc: QueryModuleUsageRecords + * @tc.type: FUNC + * @tc.require: SR000GU2T1 AR000GU37U + */ +void MultiTestQueryModuleUsageRecords(void) +{ + int32_t maxNum = 1; + BundleActiveEvent eventA(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName, + DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED); + BundleActiveClient::GetInstance().ReportEvent(eventA, DEFAULT_USERID); + std::vector results; + int32_t errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); + EXPECT_EQ(errCode, 0); + EXPECT_EQ(results.size(), 0); + + results.clear(); + maxNum = 0; + errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); + EXPECT_NE(errCode, 0); + + results.clear(); + maxNum = MAX_RECORD; + errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); + EXPECT_NE(errCode, 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryModuleUsageRecords_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryModuleUsageRecords); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryAppGroup_001 + * @tc.desc: QueryAppGroup, no bundleName + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ +void MultiTestAppGroup(void) +{ + BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, COMMON_USERID); + int32_t result = 0; + BundleActiveClient::GetInstance().QueryAppGroup(result, g_defaultBundleName, COMMON_USERID); + bool flag = false; + for (auto item = GROUP_TYPE.begin(); item != GROUP_TYPE.end(); item++) { + if (*item == result) { + flag = true; + break; + } + } + EXPECT_EQ(flag, true); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_AppGroup_001, Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryModuleUsageRecords); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryDeviceEventStats_001 + * @tc.desc: QueryDeviceEventStats + * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG + */ +void MultiTestQueryDeviceEventStats(void) +{ + std::vector eventStats; + int32_t errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats); + EXPECT_EQ(errCode, 0); + errCode = BundleActiveClient::GetInstance().QueryDeviceEventStats(0, LARGE_NUM, eventStats, COMMON_USERID); + EXPECT_EQ(errCode, 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryDeviceEventStats_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryDeviceEventStats); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_QueryNotificationEventStats_001 + * @tc.desc: QueryNotificationEventStats + * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 + */ +void MultiTestQueryNotificationEventStats(void) +{ + std::vector eventStats; + int32_t errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats); + EXPECT_EQ(errCode, 0); + errCode = BundleActiveClient::GetInstance().QueryNotificationEventStats(0, LARGE_NUM, eventStats, COMMON_USERID); + EXPECT_EQ(errCode, 0); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_QueryNotificationEventStats_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestQueryNotificationEventStats); +} + +/* + * @tc.name: DeviceUsageStatisticsMultiTest_BundleActiveGroupMap_001 + * @tc.desc: BundleActiveGroupMap + * @tc.type: FUNC + * @tc.require: SR000H0G4F AR000H2US8 + */ +void MultiTestDeviceUsageStatsGroupMap(void) +{ + int64_t minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ + .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET); + EXPECT_EQ(minInterval, 0); + minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ + .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE); + EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWO_HOUR); + minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ + .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY); + EXPECT_EQ(minInterval, ACTIVE_GROUP_DAILY * DeviceUsageStatsGroupConst::TWO_HOUR); + minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ + .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED); + EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::TWENTY_FOUR_HOUR); + minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ + .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE); + EXPECT_EQ(minInterval, DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR); +} + +HWTEST_F(DeviceUsageStatisticsMultiTest, DeviceUsageStatisticsMultiTest_BundleActiveGroupMap_001, + Function | MediumTest | Level0) +{ + SET_THREAD_NUM(100); + GTEST_RUN_TASK(MultiTestDeviceUsageStatsGroupMap); +} +} // namespace DeviceUsageStats +} // namespace OHOS +