diff --git a/frameworks/src/bundle_active_group_observer.cpp b/frameworks/src/bundle_active_group_observer.cpp index 1f5e9677cf5c6df50c83cc22f9472854ff80bd13..4cacabd4f1fb1df0ed4db681bb156e8fea78a161 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_query.cpp b/frameworks/src/bundle_state_query.cpp index 7fba2dd8f3d33a7b3c2c6283b7c51aaa3ca8ce3a..8efe64dbc2ddec88d996bc229213d7a2b70085d7 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 fc4c9f6122c40cd39b20b5e937229ad6b87fe263..7d54aa3fb3b90dc1c3c78e56d3c863543f54a3f5 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 0c1a7ba9b9ceee731f37608c99bc873a3bfbc55c..0e9d9af0a9e55e6ea23af7cf59f7599542a4a028 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/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index e544d6c8b01d2d9839b9729a6a5c41aa483a6f04..23cfe58259279a52b7c8054178954c5fa506deb3 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 a809cb9f2b572edae1cb139985c77c45a01acb06..aa8118fc619e8da49c68870bd14abed96cfd4a35 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 94f22724fbb60244fc7c884d838cbe7d041993e2..62ec880abb279edf63b12dd9bf26f2e451915543 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 e92dda6ed8befb8495ff63fcda45dd46eaa88f78..963ca49c30023d245c7e0b94b8cb27a1412722a4 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 e005984cc546979bd874623730de25e8d263c1e0..c8451b72448e8e3fb4e528d295710300bc0a0d19 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 c8b6994ace7efeb4b80072d719a531289554ccf5..d223d62b91bda2bb7608ce32d190ca5e2780d7d3 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 7f091db16bfbbaf5d451a4e04d43ef7b8839d6b9..9084d788f0466bfd01b78f3d8796b361a116ee0e 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 2baf2883def00e54332fa27555faaba58a5dea7d..f9609f9385c6ef53fae54bb84e27385cfa7f02e1 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,20 +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