diff --git a/frameworks/src/bundle_active_group_observer.cpp b/frameworks/src/bundle_active_group_observer.cpp index 1f5e9677cf5c6df50c83cc22f9472854ff80bd13..8de846dc2b0bf0daacbe74b9c4eec4e5968f840e 100644 --- a/frameworks/src/bundle_active_group_observer.cpp +++ b/frameworks/src/bundle_active_group_observer.cpp @@ -201,23 +201,22 @@ napi_value GetBundleGroupChangeCallback( napi_value ParseRegisterGroupCallBackParameters(const napi_env &env, const napi_callback_info &info, RegisterCallbackInfo ¶ms, sptr &observer) { - BUNDLE_ACTIVE_LOGI("enter ParseRegisterGroupCallBackParameters"); size_t argc = REGISTER_GROUP_CALLBACK_PARAMS; napi_value argv[REGISTER_GROUP_CALLBACK_PARAMS] = {nullptr}; 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) { @@ -231,7 +230,6 @@ napi_value ParseRegisterGroupCallBackParameters(const napi_env &env, const napi_ napi_value RegisterGroupCallBack(napi_env env, napi_callback_info info) { - BUNDLE_ACTIVE_LOGI("enter RegisterGroupCallBack"); RegisterCallbackInfo params; ParseRegisterGroupCallBackParameters(env, info, params, registerObserver); @@ -316,15 +314,13 @@ napi_value ParseUnRegisterGroupCallBackParameters(const napi_env &env, const nap napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) { - BUNDLE_ACTIVE_LOGI("enter UnRegisterGroupCallBack"); - UnRegisterCallbackInfo params; ParseUnRegisterGroupCallBackParameters(env, info, params); if (params.errorCode != ERR_OK) { return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } if (!registerObserver) { - BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack observer is not"); + BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack observer is not exist"); params.errorCode = ERR_REGISTER_OBSERVER_IS_NULL; return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index 7fba2dd8f3d33a7b3c2c6283b7c51aaa3ca8ce3a..d0f35edbcf8e0fb6ac488c8990ed68af35c99334 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -43,7 +43,7 @@ const uint32_t MODULE_RECORDS_PARAMS = 2; const uint32_t SECOND_ARG = 2; const uint32_t THIRD_ARG = 3; const int32_t MAXNUM_UP_LIMIT = 1000; -const std::vector GROUP_TYPE {10, 20, 30, 40, 50, 60}; +const std::vector GROUP_TYPE {10, 20, 30, 40, 50, 60}; const uint32_t EVENT_STATES_MIN_PARAMS = 2; const uint32_t EVENT_STATES_PARAMS = 3; @@ -334,11 +334,10 @@ napi_value QueryAppUsagePriorityGroup(napi_env env, napi_callback_info info) AsyncCallbackInfoPriorityGroup *asyncCallbackInfo = (AsyncCallbackInfoPriorityGroup *)data; if (asyncCallbackInfo) { if (asyncCallbackInfo->priorityGroup == -1) { - asyncCallbackInfo->priorityGroup = ERR_SERVICE_FAILED; + asyncCallbackInfo->errorCode = ERR_SERVICE_FAILED; } napi_value result = nullptr; napi_create_int32(env, asyncCallbackInfo->priorityGroup, &result); - BUNDLE_ACTIVE_LOGD("QueryPackageGroup, group is %{public}d", asyncCallbackInfo->priorityGroup); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } }, @@ -790,11 +789,10 @@ napi_value ParseAppUsageBundleGroupInfoParameters(const napi_env &env, const nap BUNDLE_ACTIVE_LOGE("Wrong argument type, string expected."); params.errorCode = ERR_USAGE_STATS_BUNDLENAME_TYPE; } - // 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; @@ -810,9 +808,8 @@ napi_value ParseAppUsageBundleGroupInfoParameters(const napi_env &env, const nap BUNDLE_ACTIVE_LOGE("ParseAppUsageBundleGroupInfoParameters failed, newGroup value is invalid."); params.errorCode = ERR_USAGE_STATS_GROUP_INVALID; } - // 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/innerkits/include/bundle_active_group_callback_proxy.h b/interfaces/innerkits/include/bundle_active_group_callback_proxy.h index b287ff5f76865bec0d6690e4cdd3cc2a0fd09abd..a3b29e726bab2dff25e310c2ac988861b7dc26a2 100644 --- a/interfaces/innerkits/include/bundle_active_group_callback_proxy.h +++ b/interfaces/innerkits/include/bundle_active_group_callback_proxy.h @@ -23,10 +23,6 @@ namespace OHOS { namespace DeviceUsageStats { class BundleActiveGroupCallbackProxy : public IRemoteProxy { public: - /* - * function: default constructor. - */ - BundleActiveGroupCallbackProxy() = delete; explicit BundleActiveGroupCallbackProxy(const sptr& impl); virtual ~BundleActiveGroupCallbackProxy() override; DISALLOW_COPY_AND_MOVE(BundleActiveGroupCallbackProxy); diff --git a/interfaces/innerkits/src/bundle_active_group_callback_stub.cpp b/interfaces/innerkits/src/bundle_active_group_callback_stub.cpp index 87b7d9f09f34d8f96a57bf69aa86f22f82529022..7b8ebce88690908661e287ffb5b0530e0cda6897 100644 --- a/interfaces/innerkits/src/bundle_active_group_callback_stub.cpp +++ b/interfaces/innerkits/src/bundle_active_group_callback_stub.cpp @@ -17,7 +17,6 @@ namespace OHOS { namespace DeviceUsageStats { -static std::mutex callbackMutex_; int32_t BundleActiveGroupCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) { @@ -30,16 +29,13 @@ int32_t BundleActiveGroupCallbackStub::OnRemoteRequest(uint32_t code, MessagePar BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack BundleActiveGroupCallbackStub will switch"); switch (code) { case static_cast(IBundleActiveGroupCallback::message::ON_BUNDLE_GROUP_CHANGED): { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack OnRemoteRequest is nowing ON_BUNDLE_GROUP_CHANGED"); - BundleActiveGroupCallbackInfo* groupInfo = nullptr; - std::unique_lock lock(callbackMutex_); - groupInfo = data.ReadParcelable(); + std::shared_ptr groupInfo( + data.ReadParcelable()); if (!groupInfo) { BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack ReadParcelable failed"); return -1; } - OnBundleGroupChanged(*groupInfo); - delete groupInfo; + OnBundleGroupChanged(*(groupInfo.get())); groupInfo = nullptr; break; } diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index a48b4fa41f867546447b700561f8c96aa8fcd351..1392c7d3e063d0e6c3660f1bb633bb77968e8690 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -252,7 +252,6 @@ int32_t BundleActiveProxy::QueryFormStatistics(int32_t maxNum, std::vector &observer) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack enter proxy"); if (!observer) { BUNDLE_ACTIVE_LOGE("RegisterGroupCallBack observer null"); return false; @@ -281,7 +280,6 @@ int32_t BundleActiveProxy::UnregisterGroupCallBack(const sptr { - 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,17 +332,136 @@ describe("DeviceUsageStatisticsJsTest", function () { }, 500); }) + /* + * @tc.name: DeviceUsageStatisticsJsTest013 + * @tc.desc: test queryAppUsagePriorityGroup promise. + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ + 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: SR000H0HAQ AR000H0ROE + */ + 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: SR000H0HAQ AR000H0ROE + */ 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: SR000H0HAQ AR000H0ROE + */ + 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: SR000H0HAQ AR000H0ROE + */ + it("DeviceUsageStatisticsJsTest017", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest017---------------------------'); + let bundleName = 'com.example.deviceUsageStatistics'; + let newGroup = 30; + 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: SR000H0HAQ AR000H0ROE + */ + it("DeviceUsageStatisticsJsTest018", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest018---------------------------'); let bundleName = 'com.example.deviceUsageStatistics'; - let newGroup = 10; + let newGroup = 30; bundleState.setBundleGroup(bundleName, newGroup, (err, res) => { if (err) { console.info('BUNDLE_ACTIVE setBundleGroup callback failure.'); - expect(false).assertEqual(true); + expect(true).assertEqual(true); } else { console.info('BUNDLE_ACTIVE setBundleGroup callback success.'); - expect(true).assertEqual(true); + expect(false).assertEqual(true); } }); @@ -400,21 +471,101 @@ describe("DeviceUsageStatisticsJsTest", function () { }) /* - * @tc.name: DeviceUsageStatisticsJsTest015 - * @tc.desc: test getRecentlyUsedModules promise. + * @tc.name: DeviceUsageStatisticsJsTest019 + * @tc.desc: test registerGroupCallBack promise. * @tc.type: FUNC - * @tc.require: SR000GU2UE AR0003GU3EQ + * @tc.require: SR000H0HAQ AR000H0ROE */ + 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); + }) - it("DeviceUsageStatisticsJsTest016", 0, async function (done) { - console.info('----------------------DeviceUsageStatisticsJsTest017---------------------------'); - bundleState.registerGroupCallBack((err, res) => { + /* + * @tc.name: DeviceUsageStatisticsJsTest020 + * @tc.desc: test registerGroupCallBack callback. + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ + 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); + expect(true).assertEqual(true); } else { console.info('BUNDLE_ACTIVE registerGroupCallBack callback success.'); + expect(false).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest021 + * @tc.desc: test unRegisterGroupCallBack promise. + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ + 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: SR000H0HAQ AR000H0ROE + */ + it("DeviceUsageStatisticsJsTest022", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest022---------------------------'); + bundleState.unRegisterGroupCallBack((err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack callback failure.'); expect(true).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE unRegisterGroupCallBack callback success.'); + expect(false).assertEqual(true); } }); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index df29fad79c24ef6410c6197e502011af3035d4ef..bce0cd6ce3a1cf4e8c6d1eebbe90acf7c2b65f37 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -720,7 +720,6 @@ void BundleActiveCore::OnBundleGroupChanged(const BundleActiveGroupCallbackInfo& int32_t BundleActiveCore::RegisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack enter BundleActiveCore, return"); std::lock_guard lock(callbackMutex_); if (!observer) { return -1; diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 8fec918d58dbf55019cef1d4e7cba83c32b9ec1b..d38c0de9a85478809c2f2ac24b7ec2229fb0809d 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -450,7 +450,6 @@ int32_t BundleActiveService::QueryPackageGroup(std::string& bundleName, int32_t int32_t BundleActiveService::RegisterGroupCallBack(const sptr &observer) { - BUNDLE_ACTIVE_LOGD("RegisterGroupCallBack enter bundleService"); int result = -1; if (!bundleActiveCore_) { return result; @@ -475,7 +474,6 @@ int32_t BundleActiveService::UnregisterGroupCallBack(const sptrUnregisterGroupCallBack(tokenId, observer); diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 2baf2883def00e54332fa27555faaba58a5dea7d..d2428b2f78f4f07c870098bf17fe42cbd9cfb184 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -39,8 +39,12 @@ static int64_t DEFAULT_FORMID = 1; static std::string DEFAULT_ABILITYID = "1234"; static std::string DEFAULT_ABILITYNAME = "testability"; static int32_t DEFAULT_USERID = 0; +static int32_t COMMON_USERID = 100; +static int32_t DEFAULT_ERRCODE = 0; static int64_t LARGE_NUM = 20000000000000; static int32_t DEFAULT_GROUP = 10; +static std::vector GROUP_TYPE {10, 20, 30, 40, 50}; +static std::shared_ptr observer = nullptr; class DeviceUsageStatisticsTest : public testing::Test { public: @@ -150,13 +154,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 +172,69 @@ 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: SR000H0HAQ AR000H0ROE + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackageGroup_001, Function | MediumTest | Level0) +{ + int32_t result = BundleActiveClient::GetInstance().QueryPackageGroup(DEFAULT_BUNDLENAME, 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); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_SetBundleGroup_001 + * @tc.desc: setbundlename + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ 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, COMMON_USERID); + EXPECT_EQ(result, DEFAULT_ERRCODE); } +/* + * @tc.name: DeviceUsageStatisticsTest_RegisterGroupCallBack_001 + * @tc.desc: registercallback + * @tc.type: FUNC + * @tc.require: SR000H0HAQ AR000H0ROE + */ 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!------------------------------"); } - 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: SR000H0HAQ AR000H0ROE + */ +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