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..ded30a4c204694acc37889007ffab483555ace07 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 @@ -422,5 +422,101 @@ describe("DeviceUsageStatisticsJsTest", function () { done(); }, 500); }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest020 + * @tc.desc: test queryBundleActiveEventStates promise. + * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG + */ + it("DeviceUsageStatisticsJsTest020", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest020---------------------------'); + let beginTime = 0; + let endTime = 20000000000000; + bundleState.queryBundleActiveEventStates(beginTime, endTime).then((res) => { + console.info('BUNDLE_ACTIVE queryBundleActiveEventStates promise success.'); + expect(true).assertEqual(true); + }).catch((err) => { + console.info('BUNDLE_ACTIVE queryBundleActiveEventStates promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest021 + * @tc.desc: test queryBundleActiveEventStates callback. + * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG + */ + it("DeviceUsageStatisticsJsTest021", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest021---------------------------'); + let beginTime = 0; + let endTime = 20000000000000; + bundleState.queryBundleActiveEventStates(beginTime, endTime, (err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE queryBundleActiveEventStates callback failure.'); + expect(false).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE queryBundleActiveEventStates callback success.'); + expect(true).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest022 + * @tc.desc: test queryAppNotificationNumber promise. + * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 + */ + it("DeviceUsageStatisticsJsTest022", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest022---------------------------'); + let beginTime = 0; + let endTime = 20000000000000; + bundleState.queryAppNotificationNumber(beginTime, endTime).then((res) => { + console.info('BUNDLE_ACTIVE queryAppNotificationNumber promise success.'); + expect(true).assertEqual(true); + }).catch((err) => { + console.info('BUNDLE_ACTIVE queryAppNotificationNumber promise failure.'); + expect(false).assertEqual(true); + }); + + setTimeout(()=>{ + done(); + }, 500); + }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest023 + * @tc.desc: test queryAppNotificationNumber callback. + * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 + */ + it("DeviceUsageStatisticsJsTest023", 0, async function (done) { + console.info('----------------------DeviceUsageStatisticsJsTest023---------------------------'); + let beginTime = 0; + let endTime = 20000000000000; + bundleState.queryAppNotificationNumber(beginTime, endTime, (err, res) => { + if (err) { + console.info('BUNDLE_ACTIVE queryAppNotificationNumber callback failure.'); + expect(false).assertEqual(true); + } else { + console.info('BUNDLE_ACTIVE queryAppNotificationNumber callback success.'); + expect(true).assertEqual(true); + } + }); + + setTimeout(()=>{ + done(); + }, 500); + }) }) diff --git a/services/common/include/bundle_active_common_event_subscriber.h b/services/common/include/bundle_active_common_event_subscriber.h index 28bfe69947046c906c2a4fd2b53ba9d5d7592c72..def3de908c2c2755bc2e67cf38bda7ed79557d45 100644 --- a/services/common/include/bundle_active_common_event_subscriber.h +++ b/services/common/include/bundle_active_common_event_subscriber.h @@ -46,7 +46,7 @@ public: bundleActiveReportHandler_(bundleActiveReportHandler) {} ~BundleActiveCommonEventSubscriber() = default; void OnReceiveEvent(const CommonEventData &data) override; - void HandleLockEvent(const std::string& action); + void HandleLockEvent(const std::string& action, const int32_t userId); private: std::mutex mutex_; diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index df29fad79c24ef6410c6197e502011af3035d4ef..3d7be7766fc9c8fb6e65d72a6e39583571be42ec 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -113,11 +113,13 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da bundleActiveReportHandler_.lock()->SendEvent(event); } } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { - HandleLockEvent(action); + int32_t userId = data.GetWant().GetIntParam("userId", 0); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); + HandleLockEvent(action, userId); } } -void BundleActiveCommonEventSubscriber::HandleLockEvent(const std::string& action) +void BundleActiveCommonEventSubscriber::HandleLockEvent(const std::string& action, const int32_t userId) { if (bundleActiveReportHandler_.expired()) { return; @@ -130,6 +132,7 @@ void BundleActiveCommonEventSubscriber::HandleLockEvent(const std::string& actio } else { tmpHandlerObject.event_.eventId_ = BundleActiveEvent::SYSTEM_LOCK; } + tmpHandlerObject.userId_ = userId; sptr timer = MiscServices::TimeServiceClient::GetInstance(); tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); auto handlerobjToPtr = std::make_shared(tmpHandlerObject); @@ -461,16 +464,9 @@ void BundleActiveCore::OnUserSwitched(const int32_t userId) int32_t BundleActiveCore::ReportEvent(BundleActiveEvent& event, int32_t userId) { BUNDLE_ACTIVE_LOGI("FLUSH interval is %{public}lld, debug is %{public}d", (long long)flushInterval_, debugCore_); + ObtainSystemEventName(event); event.PrintEvent(debugCore_); std::lock_guard lock(mutex_); - if (event.eventId_ == BundleActiveEvent::SYSTEM_LOCK || event.eventId_ == BundleActiveEvent::SYSTEM_UNLOCK) { - std::vector currentActiveUser; - BundleActiveCore::GetAllActiveUser(currentActiveUser); - if (currentActiveUser.size() == 1) { - userId = currentActiveUser.front(); - } - } - ObtainSystemEventName(event); if (userId == 0 || userId == -1) { return -1; } diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 2baf2883def00e54332fa27555faaba58a5dea7d..c54510e0c1081c86e6d8914610ccf84fa60aa105 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -190,6 +190,33 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_RegisterGroupCallB bool result = BundleActiveClient::GetInstance().RegisterGroupCallBack(observer.get()); EXPECT_EQ(result, true); } + +/* + * @tc.name: DeviceUsageStatisticsTest_QueryEventStats_001 + * @tc.desc: QueryEventStats + * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryEventStats_001, Function | MediumTest | Level0) +{ + std::vector eventStats; + int32_t errCode = BundleActiveClient::GetInstance().QueryEventStats(0, LARGE_NUM, eventStats); + EXPECT_EQ(errCode, 0); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_QueryAppNotificationNumber_001 + * @tc.desc: QueryAppNotificationNumber + * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryAppNotificationNumber_001, Function + | MediumTest | Level0) +{ + std::vector eventStats; + int32_t errCode = BundleActiveClient::GetInstance().QueryAppNotificationNumber(0, LARGE_NUM, eventStats); + EXPECT_EQ(errCode, 0); +} } // namespace DeviceUsageStats } // namespace OHOS