From b311e4381b0d8b86ec4fa54575c69c023e0b6f56 Mon Sep 17 00:00:00 2001 From: wyuanchao Date: Sat, 4 Jun 2022 19:40:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?tdd=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wyuanchao --- .../device_usage_statistics_jsunit.test.js | 92 +++++++++++++++++++ .../bundle_active_common_event_subscriber.h | 2 +- services/common/src/bundle_active_core.cpp | 16 ++-- .../unittest/device_usage_statistics_test.cpp | 24 +++++ 4 files changed, 123 insertions(+), 11 deletions(-) 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..54d1621 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,97 @@ describe("DeviceUsageStatisticsJsTest", function () { done(); }, 500); }) + + /* + * @tc.name: DeviceUsageStatisticsJsTest020 + * @tc.desc: test queryBundleActiveEventStates promise. + * @tc.type: FUNC + */ + 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 + */ + 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 + */ + 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 + */ + 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 28bfe69..def3de9 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 df29fad..3d7be77 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 2baf288..104f9a4 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -190,6 +190,30 @@ 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 + */ +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 + */ +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 -- Gitee From fe97c31a5c3efba97b87c1506341e5035a05f77c Mon Sep 17 00:00:00 2001 From: wyuanchao Date: Sat, 4 Jun 2022 19:45:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?tdd=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wyuanchao --- test/unittest/device_usage_statistics_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 104f9a4..8c296b2 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -208,7 +208,8 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryEventStats_00 * @tc.desc: QueryAppNotificationNumber * @tc.type: FUNC */ -HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryAppNotificationNumber_001, Function | MediumTest | Level0) +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryAppNotificationNumber_001, Function + | MediumTest | Level0) { std::vector eventStats; int32_t errCode = BundleActiveClient::GetInstance().QueryAppNotificationNumber(0, LARGE_NUM, eventStats); -- Gitee From fa9e780e38da1fb2113a72bd3e188303b8842c42 Mon Sep 17 00:00:00 2001 From: wyuanchao Date: Mon, 6 Jun 2022 09:50:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?tdd=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wyuanchao --- .../device_usage_statistics_jsunit.test.js | 4 ++++ test/unittest/device_usage_statistics_test.cpp | 2 ++ 2 files changed, 6 insertions(+) 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 54d1621..ded30a4 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 @@ -427,6 +427,7 @@ describe("DeviceUsageStatisticsJsTest", function () { * @tc.name: DeviceUsageStatisticsJsTest020 * @tc.desc: test queryBundleActiveEventStates promise. * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG */ it("DeviceUsageStatisticsJsTest020", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest020---------------------------'); @@ -449,6 +450,7 @@ describe("DeviceUsageStatisticsJsTest", function () { * @tc.name: DeviceUsageStatisticsJsTest021 * @tc.desc: test queryBundleActiveEventStates callback. * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG */ it("DeviceUsageStatisticsJsTest021", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest021---------------------------'); @@ -473,6 +475,7 @@ describe("DeviceUsageStatisticsJsTest", function () { * @tc.name: DeviceUsageStatisticsJsTest022 * @tc.desc: test queryAppNotificationNumber promise. * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 */ it("DeviceUsageStatisticsJsTest022", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest022---------------------------'); @@ -495,6 +498,7 @@ describe("DeviceUsageStatisticsJsTest", function () { * @tc.name: DeviceUsageStatisticsJsTest023 * @tc.desc: test queryAppNotificationNumber callback. * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 */ it("DeviceUsageStatisticsJsTest023", 0, async function (done) { console.info('----------------------DeviceUsageStatisticsJsTest023---------------------------'); diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 8c296b2..c54510e 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -195,6 +195,7 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_RegisterGroupCallB * @tc.name: DeviceUsageStatisticsTest_QueryEventStats_001 * @tc.desc: QueryEventStats * @tc.type: FUNC + * @tc.require: SR000H0H9H AR000H0ROG */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryEventStats_001, Function | MediumTest | Level0) { @@ -207,6 +208,7 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryEventStats_00 * @tc.name: DeviceUsageStatisticsTest_QueryAppNotificationNumber_001 * @tc.desc: QueryAppNotificationNumber * @tc.type: FUNC + * @tc.require: SR000H0H7D AR000H0RR6 */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryAppNotificationNumber_001, Function | MediumTest | Level0) -- Gitee