From a9107688a3748e624a0d0afcf4dd5a64bb6f9f04 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Wed, 1 Jun 2022 19:41:40 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BF=AE=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- .../src/bundle_active_group_observer.cpp | 10 +- frameworks/src/bundle_state_common.cpp | 27 +- frameworks/src/bundle_state_query.cpp | 4 +- .../napi/include/bundle_state_inner_errors.h | 1 + .../device_usage_statistics_jsunit.test.js | 315 +++++++++++++----- services/common/src/bundle_active_core.cpp | 2 +- .../unittest/device_usage_statistics_test.cpp | 90 ++++- 7 files changed, 333 insertions(+), 116 deletions(-) diff --git a/frameworks/src/bundle_active_group_observer.cpp b/frameworks/src/bundle_active_group_observer.cpp index 1f5e967..4cacabd 100644 --- a/frameworks/src/bundle_active_group_observer.cpp +++ b/frameworks/src/bundle_active_group_observer.cpp @@ -207,17 +207,17 @@ napi_value ParseRegisterGroupCallBackParameters(const napi_env &env, const napi_ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_ASSERT(env, argc == REGISTER_GROUP_CALLBACK_MIN_PARAMS || argc == REGISTER_GROUP_CALLBACK_PARAMS, "Invalid number of parameters"); + // arg[0] : callback napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "RegisterGroupCallBack Wrong argument type. Object expected."); - BundleActiveGroupObserverInfo bundleActiveGroupObserverInfo; - if (!GetBundleGroupChangeCallback(env, argv[0], bundleActiveGroupObserverInfo)) { + if (valuetype != napi_function || !GetBundleGroupChangeCallback(env, argv[0], bundleActiveGroupObserverInfo)) { BUNDLE_ACTIVE_LOGE("RegisterGroupCallBack bundleActiveGroupObserverInfo parse failed"); - return nullptr; + params.errorCode = ERR_OBSERVER_CALLBACK_IS_INVALID; + } else { + observer = bundleActiveGroupObserverInfo.callback; } - observer = bundleActiveGroupObserverInfo.callback; // argv[1]: asyncCallback if (argc == REGISTER_GROUP_CALLBACK_PARAMS) { diff --git a/frameworks/src/bundle_state_common.cpp b/frameworks/src/bundle_state_common.cpp index dcefc97..5a5d5a3 100644 --- a/frameworks/src/bundle_state_common.cpp +++ b/frameworks/src/bundle_state_common.cpp @@ -306,19 +306,20 @@ void BundleStateCommon::GetModuleRecordForResult(napi_env env, void BundleStateCommon::SetPromiseInfo(const napi_env &env, const napi_deferred &deferred, const napi_value &result, const int32_t &errorCode) { - switch (errorCode) { - case ERR_OK: - napi_resolve_deferred(env, deferred, result); - break; - case -1: - napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_SERVICE_FAILED)); - break; - case 1: - napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_REPEAT_OPERATION)); - break; - default: - napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); - break; + switch (errorCode) + { + case ERR_OK: + napi_resolve_deferred(env, deferred, result); + break; + case -1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_SERVICE_FAILED)); + break; + case 1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_REPEAT_OPERATION)); + break; + default: + napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); + break; } } diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index 7fba2dd..8efe64d 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -794,7 +794,7 @@ napi_value ParseAppUsageBundleGroupInfoParameters(const napi_env &env, const nap // argv[1] : newGroup if ((params.errorCode == ERR_OK) && (BundleStateCommon::GetInt32NumberValue(env, argv[1], params.newGroup) == nullptr)) { - BUNDLE_ACTIVE_LOGE("ParseAppUsageBundleGroupInfoParameters failed, beginTime type is invalid."); + BUNDLE_ACTIVE_LOGE("ParseAppUsageBundleGroupInfoParameters failed, newGroup type is invalid."); params.errorCode = ERR_USAGE_STATS_GROUP_INVALID; } bool flag = false; @@ -812,7 +812,7 @@ napi_value ParseAppUsageBundleGroupInfoParameters(const napi_env &env, const nap } // argv[SECOND_ARG]: callback - if (params.errorCode == ERR_OK && argc == APP_USAGE_PARAMS_BUNDLE_GROUP) { + if (argc == APP_USAGE_PARAMS_BUNDLE_GROUP) { napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[SECOND_ARG], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "ParseAppUsageBundleGroupInfoParameters invalid parameter type. " diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h index fc4c9f6..7d54aa3 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h @@ -56,6 +56,7 @@ enum : int32_t { ERR_SERVICE_FAILED, ERR_REPEAT_OPERATION, ERR_REGISTER_OBSERVER_IS_NULL, + ERR_OBSERVER_CALLBACK_IS_INVALID, }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js b/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js index 0c1a7ba..0e9d9af 100644 --- a/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js +++ b/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js @@ -94,60 +94,12 @@ describe("DeviceUsageStatisticsJsTest", function () { /* * @tc.name: DeviceUsageStatisticsJsTest003 - * @tc.desc: test queryAppUsagePriorityGroup promise. + * @tc.desc: test queryBundleActiveStates promise. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 */ it("DeviceUsageStatisticsJsTest003", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest003---------------------------'); - let bundleName = 'com.example.deviceUsageStatistics'; - let userId = 10; - bundleState.queryAppUsagePriorityGroup(bundleName, userId).then( res => { - console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise success.'); - expect(true).assertEqual(true); - }).catch( err => { - console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failure.'); - expect(false).assertEqual(true); - }); - - setTimeout(()=>{ - done(); - }, 500); - }) - - /* - * @tc.name: DeviceUsageStatisticsJsTest004 - * @tc.desc: test queryAppUsagePriorityGroup callback. - * @tc.type: FUNC - * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G - */ - it("DeviceUsageStatisticsJsTest004", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest004---------------------------'); - let bundleName = 'com.example.deviceUsageStatistics'; - let userId = 10; - bundleState.queryAppUsagePriorityGroup(bundleName, userId, (err, res) => { - if (err) { - console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failure.'); - expect(false).assertEqual(true); - } else { - console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback success.'); - expect(true).assertEqual(true); - } - }); - - setTimeout(()=>{ - done(); - }, 500); - }) - - /* - * @tc.name: DeviceUsageStatisticsJsTest005 - * @tc.desc: test queryBundleActiveStates promise. - * @tc.type: FUNC - * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 - */ - it("DeviceUsageStatisticsJsTest005", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest005---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryBundleActiveStates(beginTime, endTime).then((res) => { @@ -164,13 +116,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest006 + * @tc.name: DeviceUsageStatisticsJsTest004 * @tc.desc: test queryBundleActiveStates callback. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G */ - it("DeviceUsageStatisticsJsTest006", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest006---------------------------'); + it("DeviceUsageStatisticsJsTest004", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest004---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryBundleActiveStates(beginTime, endTime, (err, res) => { @@ -189,13 +141,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest007 + * @tc.name: DeviceUsageStatisticsJsTest005 * @tc.desc: test queryBundleStateInfos promise. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 */ - it("DeviceUsageStatisticsJsTest007", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest007---------------------------'); + it("DeviceUsageStatisticsJsTest005", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest005---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryBundleStateInfos(beginTime, endTime).then((res) => { @@ -212,13 +164,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest008 + * @tc.name: DeviceUsageStatisticsJsTest006 * @tc.desc: test queryBundleStateInfos callback. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G */ - it("DeviceUsageStatisticsJsTest008", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest008---------------------------'); + it("DeviceUsageStatisticsJsTest006", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest006---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryBundleStateInfos(beginTime, endTime, (err, res) => { @@ -237,13 +189,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest009 + * @tc.name: DeviceUsageStatisticsJsTest007 * @tc.desc: test queryCurrentBundleActiveStates promise. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 */ - it("DeviceUsageStatisticsJsTest009", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest009---------------------------'); + it("DeviceUsageStatisticsJsTest007", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest007---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryCurrentBundleActiveStates(beginTime, endTime).then((res) => { @@ -260,13 +212,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest010 + * @tc.name: DeviceUsageStatisticsJsTest008 * @tc.desc: test queryCurrentBundleActiveStates callback. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G */ - it("DeviceUsageStatisticsJsTest010", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest010---------------------------'); + it("DeviceUsageStatisticsJsTest008", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest008---------------------------'); let beginTime = 0; let endTime = 20000000000000; bundleState.queryCurrentBundleActiveStates(beginTime, endTime, (err, res) => { @@ -285,13 +237,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest011 + * @tc.name: DeviceUsageStatisticsJsTest009 * @tc.desc: test queryBundleStateInfoByInterval promise. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 */ - it("DeviceUsageStatisticsJsTest011", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest011---------------------------'); + it("DeviceUsageStatisticsJsTest009", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest009---------------------------'); let intervalType = 0; let beginTime = 0; let endTime = 20000000000000; @@ -309,13 +261,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest012 + * @tc.name: DeviceUsageStatisticsJsTest010 * @tc.desc: test queryBundleStateInfoByInterval callback. * @tc.type: FUNC * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G */ - it("DeviceUsageStatisticsJsTest012", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest012---------------------------'); + it("DeviceUsageStatisticsJsTest010", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest010---------------------------'); let intervalType = 0; let beginTime = 0; let endTime = 20000000000000; @@ -335,13 +287,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest013 + * @tc.name: DeviceUsageStatisticsJsTest011 * @tc.desc: test getRecentlyUsedModules callback. * @tc.type: FUNC * @tc.require: SR000GU2UE AR0003GU3EQ */ - it("DeviceUsageStatisticsJsTest013", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest013---------------------------'); + it("DeviceUsageStatisticsJsTest011", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest011---------------------------'); let maxNum = 1; bundleState.getRecentlyUsedModules(maxNum, (err, res) => { if (err) { @@ -359,13 +311,13 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest014 + * @tc.name: DeviceUsageStatisticsJsTest012 * @tc.desc: test getRecentlyUsedModules promise. * @tc.type: FUNC * @tc.require: SR000GU2UE AR0003GU3EQ */ - it("DeviceUsageStatisticsJsTest014", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest014---------------------------'); + it("DeviceUsageStatisticsJsTest012", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest012---------------------------'); let maxNum = 1; bundleState.getRecentlyUsedModules(maxNum).then((res) => { console.info('BUNDLE_ACTIVE getRecentlyUsedModules promise success.'); @@ -380,8 +332,127 @@ describe("DeviceUsageStatisticsJsTest", function () { }, 500); }) + /* + * @tc.name: DeviceUsageStatisticsJsTest013 + * @tc.desc: test queryAppUsagePriorityGroup promise. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 + */ + it("DeviceUsageStatisticsJsTest013", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest013---------------------------'); + let bundleName = 'com.example.deviceUsageStatistics'; + bundleState.queryAppUsagePriorityGroup(bundleName).then( res => { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise success.'); + expect(true).assertEqual(true); + }).catch( err => { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest014 + * @tc.desc: test queryAppUsagePriorityGroup callback. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G + */ + it("DeviceUsageStatisticsJsTest014", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest014---------------------------'); + let bundleName = 'com.example.deviceUsageStatistics'; + bundleState.queryAppUsagePriorityGroup(bundleName, (err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failure.'); + expect(false).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback success.'); + expect(true).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest015 + * @tc.desc: test queryAppUsagePriorityGroup promise, with no param. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 + */ it("DeviceUsageStatisticsJsTest015", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest015---------------------------'); + bundleState.queryAppUsagePriorityGroup().then( res => { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise success.'); + expect(true).assertEqual(true); + }).catch( err => { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest016 + * @tc.desc: test queryAppUsagePriorityGroup callback, with no param. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G + */ + it("DeviceUsageStatisticsJsTest016", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest016---------------------------'); + bundleState.queryAppUsagePriorityGroup((err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failure.'); + expect(false).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback success.'); + expect(true).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest017 + * @tc.desc: test setBundleGroup promise. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 + */ + it("DeviceUsageStatisticsJsTest017", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest017---------------------------'); + let bundleName = 'com.example.deviceUsageStatistics'; + let newGroup = 10; + bundleState.setBundleGroup(bundleName, newGroup).then( res => { + console.info('BUNDLE_ACTIVE setBundleGroup promise success.'); + expect(true).assertEqual(true); + }).catch( err => { + console.info('BUNDLE_ACTIVE setBundleGroup promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest018 + * @tc.desc: test setBundleGroup callback. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89E AR000GH89F AR000GH89G + */ + it("DeviceUsageStatisticsJsTest018", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest018---------------------------'); let bundleName = 'com.example.deviceUsageStatistics'; let newGroup = 10; bundleState.setBundleGroup(bundleName, newGroup, (err, res) => { @@ -400,15 +471,51 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest015 - * @tc.desc: test getRecentlyUsedModules promise. + * @tc.name: DeviceUsageStatisticsJsTest019 + * @tc.desc: test registerGroupCallBack promise. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 + */ + it("DeviceUsageStatisticsJsTest019", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest019---------------------------'); + let onBundleGroupChanged = (err,res) =>{ + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.'); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack oldGroup is : ' + res.oldGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack newGroup is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack changeReason is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack userId is : ' + res.userId); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack bundleName is : ' + res.bundleName); + }; + bundleState.registerGroupCallBack(onBundleGroupChanged).then( res => { + console.info('BUNDLE_ACTIVE RegisterGroupCallBack promise success.'); + expect(true).assertEqual(true); + }).catch( err => { + console.info('BUNDLE_ACTIVE RegisterGroupCallBack promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest020 + * @tc.desc: test registerGroupCallBack callback. * @tc.type: FUNC * @tc.require: SR000GU2UE AR0003GU3EQ */ - - it("DeviceUsageStatisticsJsTest016", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest017---------------------------'); - bundleState.registerGroupCallBack((err, res) => { + it("DeviceUsageStatisticsJsTest020", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest020---------------------------'); + let onBundleGroupChanged = (err,res) =>{ + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.'); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack oldGroup is : ' + res.oldGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack newGroup is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack changeReason is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack userId is : ' + res.userId); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack bundleName is : ' + res.bundleName); + }; + bundleState.registerGroupCallBack(onBundleGroupChanged, (err, res) => { if (err) { console.info('BUNDLE_ACTIVE registerGroupCallBack callback failure.'); expect(false).assertEqual(true); @@ -422,5 +529,49 @@ describe("DeviceUsageStatisticsJsTest", function () { done(); }, 500); }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest021 + * @tc.desc: test unRegisterGroupCallBack promise. + * @tc.type: FUNC + * @tc.require: SR000GGTN7 AR000GH89H AR000GH89I AR000GH899 + */ + it("DeviceUsageStatisticsJsTest021", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest021---------------------------'); + bundleState.unRegisterGroupCallBack().then( res => { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack promise success.'); + expect(true).assertEqual(true); + }).catch( err => { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest022 + * @tc.desc: test unRegisterGroupCallBack callback. + * @tc.type: FUNC + * @tc.require: SR000GU2UE AR0003GU3EQ + */ + it("DeviceUsageStatisticsJsTest022", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest022---------------------------'); + bundleState.unRegisterGroupCallBack((err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack callback failure.'); + expect(false).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack callback success.'); + expect(true).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) }) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index df29fad..2bf517b 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -731,7 +731,7 @@ int32_t BundleActiveCore::RegisterGroupCallBack(const AccessToken::AccessTokenID } groupChangeObservers_.emplace(tokenId, observer); AddObserverDeathRecipient(observer); - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack number is %{public}d", static_cast(groupChangeObservers_.size())); + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack number is %{public}d", groupChangeObservers_.size()); return 0; } diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 2baf288..1eddce7 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -41,6 +41,9 @@ static std::string DEFAULT_ABILITYNAME = "testability"; static int32_t DEFAULT_USERID = 0; static int64_t LARGE_NUM = 20000000000000; static int32_t DEFAULT_GROUP = 10; +static int32_t DEFAULT_ERRCODE = 0; +static std::shared_ptr observer = nullptr; +const std::vector GROUP_TYPE {10, 20, 30, 40, 50, 60}; class DeviceUsageStatisticsTest : public testing::Test { public: @@ -150,13 +153,6 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_IsBundleIdle_001, EXPECT_EQ(result, true); } -/* - * @tc.name: DeviceUsageStatisticsTest_QueryPackageGroup_001 - * @tc.desc: querypackagegroup - * @tc.type: FUNC - * @tc.require: - */ - /* * @tc.name: DeviceUsageStatisticsTest_QueryFormStatistics_001 * @tc.desc: QueryFormStatistics @@ -175,21 +171,89 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryFormStatistic EXPECT_EQ(results.size(), 0); } +/* + * @tc.name: DeviceUsageStatisticsTest_QueryPackageGroup_001 + * @tc.desc: querypackagegroup, no bundleName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackageGroup_001, Function | MediumTest | Level0) +{ + std::string bundleName = ""; + int32_t result = BundleActiveClient::GetInstance().QueryPackageGroup(bundleName); + bool flag = false; + for (auto item = GROUP_TYPE.begin(); item != GROUP_TYPE.end(); item++) { + if (*item == result) { + flag = true; + break; + } + } + EXPECT_EQ(flag, true); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_QueryPackageGroup_002 + * @tc.desc: querypackagegroup, with bundleName + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackageGroup_002, Function | MediumTest | Level0) +{ + int32_t result = BundleActiveClient::GetInstance().QueryPackageGroup(DEFAULT_BUNDLENAME); + bool flag = false; + for (auto item = GROUP_TYPE.begin(); item != GROUP_TYPE.end(); item++) { + if (*item == result) { + flag = true; + break; + } + } + EXPECT_EQ(flag, true); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_SetBundleGroup_001 + * @tc.desc: setbundlename + * @tc.type: FUNC + * @tc.require: + */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_SetBundleGroup_001, Function | MediumTest | Level0) { - bool result = BundleActiveClient::GetInstance().SetBundleGroup(DEFAULT_BUNDLENAME, DEFAULT_GROUP, DEFAULT_USERID); - EXPECT_EQ(result, true); + int32_t result = BundleActiveClient::GetInstance().SetBundleGroup(DEFAULT_BUNDLENAME, DEFAULT_GROUP, DEFAULT_ERRCODE); + EXPECT_EQ(result, DEFAULT_ERRCODE); } +/* + * @tc.name: DeviceUsageStatisticsTest_RegisterGroupCallBack_001 + * @tc.desc: registercallback + * @tc.type: FUNC + * @tc.require: + */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_RegisterGroupCallBack_001, Function | MediumTest | Level0) { - auto observer=std::make_shared(); + observer=std::make_shared(); if (!observer) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack construct observer!------------------------------"); + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack construct observer"); } - bool result = BundleActiveClient::GetInstance().RegisterGroupCallBack(observer.get()); - EXPECT_EQ(result, true); + int32_t result = BundleActiveClient::GetInstance().RegisterGroupCallBack(observer.get()); + EXPECT_EQ(result, DEFAULT_ERRCODE); } + +/* + * @tc.name: DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001 + * @tc.desc: unregistercallback + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001, Function | MediumTest | Level0) +{ + if (!observer) { + BUNDLE_ACTIVE_LOGI("observer has been delete"); + } + int32_t result = BundleActiveClient::GetInstance().UnregisterGroupCallBack(observer.get()); + observer = nullptr; + EXPECT_EQ(result, DEFAULT_ERRCODE); +} + } // namespace DeviceUsageStats } // namespace OHOS -- Gitee From 177d084df3073dc216b8a7496f2bf8d8d9004fe7 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Wed, 1 Jun 2022 19:57:37 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BF=AE=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- frameworks/src/bundle_state_common.cpp | 27 +++++++++---------- .../include/bundle_active_usage_database.h | 3 +-- .../src/bundle_active_usage_database.cpp | 21 +++++---------- .../src/bundle_active_group_controller.cpp | 2 +- .../include/bundle_active_event.h | 6 ----- .../packageusage/src/bundle_active_event.cpp | 13 --------- .../src/bundle_active_period_stats.cpp | 2 +- .../src/bundle_active_user_service.cpp | 8 +++--- .../unittest/device_usage_statistics_test.cpp | 6 +++-- 9 files changed, 30 insertions(+), 58 deletions(-) diff --git a/frameworks/src/bundle_state_common.cpp b/frameworks/src/bundle_state_common.cpp index 5a5d5a3..dcefc97 100644 --- a/frameworks/src/bundle_state_common.cpp +++ b/frameworks/src/bundle_state_common.cpp @@ -306,20 +306,19 @@ void BundleStateCommon::GetModuleRecordForResult(napi_env env, void BundleStateCommon::SetPromiseInfo(const napi_env &env, const napi_deferred &deferred, const napi_value &result, const int32_t &errorCode) { - switch (errorCode) - { - case ERR_OK: - napi_resolve_deferred(env, deferred, result); - break; - case -1: - napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_SERVICE_FAILED)); - break; - case 1: - napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_REPEAT_OPERATION)); - break; - default: - napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); - break; + switch (errorCode) { + case ERR_OK: + napi_resolve_deferred(env, deferred, result); + break; + case -1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_SERVICE_FAILED)); + break; + case 1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_REPEAT_OPERATION)); + break; + default: + napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); + break; } } diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index e544d6c..23cfe58 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -41,8 +41,7 @@ public: ~BundleActiveUsageDatabase(); void InitDatabaseTableInfo(int64_t currentTime); void InitUsageGroupDatabase(const int32_t databaseType, const bool forModuleRecords); - void UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats); - void UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats); + void UpdateUsageData(int32_t databaseType, BundleActivePeriodStats &stats); std::shared_ptr GetCurrentUsageData(int32_t databaseType, int32_t userId); void RenewTableTime(int64_t timeDiffMillis); int32_t GetOptimalIntervalType(int64_t beginTime, int64_t endTime); diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index a809cb9..aa8118f 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -1167,20 +1167,7 @@ void BundleActiveUsageDatabase::RenewTableTime(int64_t changedTime) } } -void BundleActiveUsageDatabase::UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats) -{ - lock_guard lock(databaseMutex_); - CheckDatabaseFile(databaseType); - if (databaseType != DAILY_DATABASE_INDEX) { - return; - } - if (stats.events_.Size() != 0) { - CheckDatabaseFile(EVENT_DATABASE_INDEX); - FlushEventInfo(EVENT_DATABASE_INDEX, stats); - } -} - -void BundleActiveUsageDatabase::UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats) +void BundleActiveUsageDatabase::UpdateUsageData(int32_t databaseType, BundleActivePeriodStats &stats) { lock_guard lock(databaseMutex_); if (databaseType < 0 || databaseType >= EVENT_DATABASE_INDEX) { @@ -1188,6 +1175,12 @@ void BundleActiveUsageDatabase::UpdateBundleUsageData(int32_t databaseType, Bund return; } CheckDatabaseFile(databaseType); + if (databaseType == DAILY_DATABASE_INDEX) { + if (stats.events_.Size() != 0) { + CheckDatabaseFile(EVENT_DATABASE_INDEX); + FlushEventInfo(EVENT_DATABASE_INDEX, stats); + } + } int32_t packageTableIndex = BundleActiveBinarySearch::GetInstance()->BinarySearch( sortedTableArray_.at(databaseType), stats.beginTime_); if (packageTableIndex < 0) { diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 94f2272..62ec880 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -218,12 +218,12 @@ uint32_t BundleActiveGroupController::EventToGroupReason(const int32_t eventId) void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp, const int32_t userId) { + BUNDLE_ACTIVE_LOGI("ReportEvent called"); if (bundleGroupEnable_ == false) { return; } std::lock_guard lock(mutex_); if (IsBundleInstalled(event.bundleName_, userId) == false) { - BUNDLE_ACTIVE_LOGE("Report an uninstalled package event, return!"); return; } int32_t eventId = event.eventId_; diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index e92dda6..963ca49 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -120,12 +120,6 @@ public: * return: string of bundlename, timestamp, eventid. */ std::string ToString(); - /** - * @brief get if the event is reported by bundle usage. - * - * @return return true if event reported by bundle usage. - */ - static bool IsBundleEvent(const int32_t eventId); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/src/bundle_active_event.cpp b/services/packageusage/src/bundle_active_event.cpp index e005984..c8451b7 100644 --- a/services/packageusage/src/bundle_active_event.cpp +++ b/services/packageusage/src/bundle_active_event.cpp @@ -183,19 +183,6 @@ std::string BundleActiveEvent::ToString() return "bundle name is " + this->bundleName_ + ", event is " + std::to_string(this->eventId_) + ", timestamp is " + std::to_string(this->timeStamp_) + "\n"; } - -bool BundleActiveEvent::IsBundleEvent(const int32_t eventId) -{ - if (eventId == ABILITY_BACKGROUND || - eventId == ABILITY_FOREGROUND || - eventId == ABILITY_STOP || - eventId == LONG_TIME_TASK_STARTTED || - eventId == LONG_TIME_TASK_ENDED || - eventId == END_OF_THE_DAY) { - return true; - } - return false; -} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/src/bundle_active_period_stats.cpp b/services/packageusage/src/bundle_active_period_stats.cpp index c8b6994..d223d62 100644 --- a/services/packageusage/src/bundle_active_period_stats.cpp +++ b/services/packageusage/src/bundle_active_period_stats.cpp @@ -53,7 +53,7 @@ void BundleActivePeriodStats::Update(const std::string bundleName, const std::st tmpUsageStats->Update("", timeStamp, eventId, abilityId); } } - } else if (BundleActiveEvent::IsBundleEvent(eventId)) { + } else { auto usageStats = GetOrCreateUsageStats(bundleName); usageStats->Update(longTimeTaskName, timeStamp, eventId, abilityId); } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 7f091db..9084d78 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -173,12 +173,10 @@ void BundleActiveUserService::RestoreStats(bool forced) BUNDLE_ACTIVE_LOGI("RestoreStats() stat changed is true"); for (uint32_t i = 0; i < currentStats_.size(); i++) { if (currentStats_[i]) { - if (!currentStats_[i]->bundleStats_.empty()) { - database_.UpdateBundleUsageData(i, *(currentStats_[i])); - } - if (!currentStats_[i]->events_.events_.empty() && i == BundleActivePeriodStats::PERIOD_DAILY) { - database_.UpdateEventData(i, *(currentStats_[i])); + if (currentStats_[i]->bundleStats_.empty() && currentStats_[i]->events_.events_.empty()) { + continue; } + database_.UpdateUsageData(i, *(currentStats_[i])); } } if (!moduleRecords_.empty()) { diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 1eddce7..b8d18e0 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -218,7 +218,8 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackageGroup_ */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_SetBundleGroup_001, Function | MediumTest | Level0) { - int32_t result = BundleActiveClient::GetInstance().SetBundleGroup(DEFAULT_BUNDLENAME, DEFAULT_GROUP, DEFAULT_ERRCODE); + int32_t result = BundleActiveClient::GetInstance().SetBundleGroup(DEFAULT_BUNDLENAME, DEFAULT_GROUP, + DEFAULT_ERRCODE); EXPECT_EQ(result, DEFAULT_ERRCODE); } @@ -244,7 +245,8 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_RegisterGroupCallB * @tc.type: FUNC * @tc.require: */ -HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001, Function | MediumTest | Level0) +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001, + Function | MediumTest | Level0) { if (!observer) { BUNDLE_ACTIVE_LOGI("observer has been delete"); -- Gitee From a17a9291b60e3fa41664fab804208d2365ef3e80 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Wed, 1 Jun 2022 20:00:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BF=AE=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- services/common/src/bundle_active_core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 2bf517b..df29fad 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -731,7 +731,7 @@ int32_t BundleActiveCore::RegisterGroupCallBack(const AccessToken::AccessTokenID } groupChangeObservers_.emplace(tokenId, observer); AddObserverDeathRecipient(observer); - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack number is %{public}d", groupChangeObservers_.size()); + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack number is %{public}d", static_cast(groupChangeObservers_.size())); return 0; } -- Gitee From f534766d231967cd84040535a932ceac0bf500f7 Mon Sep 17 00:00:00 2001 From: "yupeng74@huawei.com" Date: Wed, 1 Jun 2022 20:08:27 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BF=AE=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng74@huawei.com --- test/unittest/device_usage_statistics_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index b8d18e0..f9609f9 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -245,7 +245,7 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_RegisterGroupCallB * @tc.type: FUNC * @tc.require: */ -HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001, +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCallBack_001, Function | MediumTest | Level0) { if (!observer) { @@ -255,7 +255,6 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_UnRegisterGroupCal observer = nullptr; EXPECT_EQ(result, DEFAULT_ERRCODE); } - } // namespace DeviceUsageStats } // namespace OHOS -- Gitee