From a1156564b1c197c0d62cb620690c7109859e337a Mon Sep 17 00:00:00 2001 From: li_junsong Date: Tue, 19 Nov 2024 17:02:52 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90test=E3=80=91=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E8=AE=BE=E5=AE=9A=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=8F=AF=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li_junsong Signed-off-by: 徐未来 --- .../system_timer/include/napi_system_timer.h | 1 + .../system_timer/src/napi_system_timer.cpp | 16 +++++++++ interfaces/inner_api/include/itimer_info.h | 14 +++++++- services/ipc/proxy/time_service_proxy.cpp | 4 +++ services/ipc/stub/time_service_stub.cpp | 2 ++ services/time_system_ability.cpp | 20 ++++++----- services/time_system_ability.h | 2 +- services/timer/include/timer_info.h | 2 ++ services/timer/include/timer_manager.h | 4 ++- .../timer/include/timer_manager_interface.h | 1 + services/timer/src/timer_info.cpp | 2 ++ services/timer/src/timer_manager.cpp | 27 ++++++++------- .../timetesttimer_fuzzer/timer_info.h | 5 +++ .../timetesttimer_fuzzer.cpp | 1 + .../service_test/include/timer_info_test.h | 6 ++++ .../service_test/src/time_client_test.cpp | 34 +++++++++++++++++++ .../service_test/src/time_proxy_test.cpp | 10 +++--- .../service_test/src/time_service_test.cpp | 29 ++++++++-------- utils/native/include/time_common.h | 1 + 19 files changed, 138 insertions(+), 43 deletions(-) diff --git a/framework/js/napi/system_timer/include/napi_system_timer.h b/framework/js/napi/system_timer/include/napi_system_timer.h index 5f06c31e..38f8b819 100644 --- a/framework/js/napi/system_timer/include/napi_system_timer.h +++ b/framework/js/napi/system_timer/include/napi_system_timer.h @@ -37,6 +37,7 @@ public: virtual void SetRepeat(bool repeat) override; virtual void SetInterval(const uint64_t &interval) override; virtual void SetWantAgent(std::shared_ptr wantAgent) override; + void SetAutoRestore(bool _autoRestore); void SetCallbackInfo(const napi_env &env, const napi_ref &ref); private: diff --git a/framework/js/napi/system_timer/src/napi_system_timer.cpp b/framework/js/napi/system_timer/src/napi_system_timer.cpp index e8a0017f..d94fc314 100644 --- a/framework/js/napi/system_timer/src/napi_system_timer.cpp +++ b/framework/js/napi/system_timer/src/napi_system_timer.cpp @@ -94,6 +94,10 @@ void ITimerInfoInstance::SetInterval(const uint64_t &_interval) { interval = _interval; } +void ITimerInfoInstance::SetAutoRestore(bool _autoRestore) +{ + autoRestore = _autoRestore; +} void ITimerInfoInstance::SetWantAgent(std::shared_ptr _wantAgent) { wantAgent = _wantAgent; @@ -124,6 +128,7 @@ napi_value NapiSystemTimer::SystemTimerInit(napi_env env, napi_value exports) std::map PARA_NAPI_TYPE_MAP = { { "type", napi_number }, { "repeat", napi_boolean }, + { "autoRestore", napi_boolean }, { "interval", napi_number }, { "wantAgent", napi_object }, { "callback", napi_function }, @@ -132,6 +137,7 @@ std::map PARA_NAPI_TYPE_MAP = { std::map NAPI_TYPE_STRING_MAP = { { "type", "number" }, { "repeat", "boolean" }, + { "autoRestore", "boolean" }, { "interval", "number" }, { "wantAgent", "object" }, { "callback", "function" }, @@ -156,6 +162,10 @@ void ParseTimerOptions(napi_env env, ContextBase *context, std::string paraType, bool repeat = false; napi_get_value_bool(env, result, &repeat); iTimerInfoInstance->SetRepeat(repeat); + } else if (paraType == "autoRestore") { + bool autoRestore = false; + napi_get_value_bool(env, result, &autoRestore); + iTimerInfoInstance->SetAutoRestore(autoRestore); } else if (paraType == "interval") { int64_t interval = 0; napi_get_value_int64(env, result, &interval); @@ -195,6 +205,12 @@ void NapiSystemTimer::GetTimerOptions(const napi_env &env, ContextBase *context, ParseTimerOptions(env, context, "repeat", value, iTimerInfoInstance); CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, context, context->errMessage, JsErrorCode::PARAMETER_ERROR); + // autoRestore?: boolean + napi_has_named_property(env, value, "autoRestore", &hasProperty); + if (hasProperty) { + ParseTimerOptions(env, context, "autoRestore", value, iTimerInfoInstance); + CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, context, context->errMessage, JsErrorCode::PARAMETER_ERROR); + } // interval?: number napi_has_named_property(env, value, "interval", &hasProperty); if (hasProperty) { diff --git a/interfaces/inner_api/include/itimer_info.h b/interfaces/inner_api/include/itimer_info.h index 8e2ae600..16cde63f 100644 --- a/interfaces/inner_api/include/itimer_info.h +++ b/interfaces/inner_api/include/itimer_info.h @@ -30,6 +30,7 @@ public: int type; bool repeat; bool disposable = false; + bool autoRestore = false; uint64_t interval; std::shared_ptr wantAgent; @@ -83,12 +84,23 @@ public: * @para: _disposable bool * true: the timer will be destoryed automaticly when it is triggered. * But do not take effect for repeat timer. - * fasle: the timer need to be destroyed by client + * false: the timer need to be destroyed by client */ void SetDisposable(const bool &_disposable) { disposable = _disposable; } + + /** + * SetAutoRestore set timer restored upon reboot + * @para: _autoRestore bool + * true: the timer will be restored when the device reboots. + * false: the timer won't be restored when the device reboots. + */ + void SetAutoRestore(bool _autoRestore) + { + autoRestore = _autoRestore; + } virtual void SetWantAgent(std::shared_ptr wantAgent) = 0; virtual void OnTrigger() = 0; }; diff --git a/services/ipc/proxy/time_service_proxy.cpp b/services/ipc/proxy/time_service_proxy.cpp index dc7cb335..cfd01ec2 100644 --- a/services/ipc/proxy/time_service_proxy.cpp +++ b/services/ipc/proxy/time_service_proxy.cpp @@ -74,6 +74,10 @@ int32_t TimeServiceProxy::CreateTimer(const std::shared_ptr &timerOp TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write disposable"); return E_TIME_WRITE_PARCEL_ERROR; } + if (!data.WriteBool(timerOptions->autoRestore)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write autoRestore"); + return E_TIME_WRITE_PARCEL_ERROR; + } if (!data.WriteUint64(timerOptions->interval)) { TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write interval"); return E_TIME_WRITE_PARCEL_ERROR; diff --git a/services/ipc/stub/time_service_stub.cpp b/services/ipc/stub/time_service_stub.cpp index f645d897..d7d8cab8 100644 --- a/services/ipc/stub/time_service_stub.cpp +++ b/services/ipc/stub/time_service_stub.cpp @@ -201,6 +201,7 @@ int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply auto type = data.ReadInt32(); auto repeat = data.ReadBool(); auto disposable = data.ReadBool(); + auto autoRestore = data.ReadBool(); auto interval = data.ReadUint64(); if (data.ReadBool()) { wantAgent = std::shared_ptr( @@ -220,6 +221,7 @@ int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply timerOptions->repeat = repeat; timerOptions->interval = interval; timerOptions->disposable = disposable; + timerOptions->autoRestore = autoRestore; timerOptions->wantAgent = wantAgent; uint64_t timerId = data.ReadUint64(); auto errCode = CreateTimer(timerOptions, obj, timerId); diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index a437422e..61778d4f 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -343,11 +343,11 @@ void TimeSystemAbility::OnStop() void TimeSystemAbility::ParseTimerPara(const std::shared_ptr &timerOptions, TimerPara ¶s) { auto uIntType = static_cast(timerOptions->type); - auto disposable = timerOptions->disposable; bool isRealtime = (uIntType & TIMER_TYPE_REALTIME_MASK) > 0; bool isWakeup = (uIntType & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0; paras.windowLength = (uIntType & TIMER_TYPE_EXACT_MASK) > 0 ? 0 : -1; paras.flag = (uIntType & TIMER_TYPE_EXACT_MASK) > 0 ? 1 : 0; + paras.autoRestore = timerOptions->autoRestore; if (isRealtime && isWakeup) { paras.timerType = ITimerManager::TimerType::ELAPSED_REALTIME_WAKEUP; } else if (isRealtime) { @@ -358,13 +358,13 @@ void TimeSystemAbility::ParseTimerPara(const std::shared_ptr &timerO paras.timerType = ITimerManager::TimerType::RTC; } if ((uIntType & TIMER_TYPE_IDLE_MASK) > 0) { - paras.flag += ITimerManager::TimerFlag::IDLE_UNTIL; + paras.flag |= ITimerManager::TimerFlag::IDLE_UNTIL; } if ((uIntType & TIMER_TYPE_INEXACT_REMINDER_MASK) > 0) { - paras.flag += ITimerManager::TimerFlag::INEXACT_REMINDER; + paras.flag |= ITimerManager::TimerFlag::INEXACT_REMINDER; } - if (disposable) { - paras.flag += ITimerManager::TimerFlag::IS_DISPOSABLE; + if (timerOptions->disposable) { + paras.flag |= ITimerManager::TimerFlag::IS_DISPOSABLE; } paras.interval = timerOptions->repeat ? timerOptions->interval : 0; } @@ -403,7 +403,7 @@ int32_t TimeSystemAbility::CreateTimer(const std::shared_ptr &timerO if ((static_cast(paras.flag) & static_cast(ITimerManager::TimerFlag::IDLE_UNTIL)) > 0 && !TimePermission::CheckProxyCallingPermission()) { TIME_HILOGW(TIME_MODULE_SERVICE, "App not support create idle timer."); - paras.flag = 0; + paras.flag &= ~ITimerManager::TimerFlag::IDLE_UNTIL; } auto type = DatabaseType::NOT_STORE; if (timerOptions->wantAgent != nullptr) { @@ -976,7 +976,7 @@ bool TimeSystemAbility::RecoverTimer() int count; holdResultSet->GetRowCount(count); TIME_HILOGI(TIME_MODULE_SERVICE, "hold result rows count: %{public}d", count); - RecoverTimerInner(holdResultSet); + RecoverTimerInner(holdResultSet, true); } if (holdResultSet != nullptr) { holdResultSet->Close(); @@ -990,7 +990,7 @@ bool TimeSystemAbility::RecoverTimer() int count; dropResultSet->GetRowCount(count); TIME_HILOGI(TIME_MODULE_SERVICE, "drop result rows count: %{public}d", count); - RecoverTimerInner(dropResultSet); + RecoverTimerInner(dropResultSet, false); } if (dropResultSet != nullptr) { dropResultSet->Close(); @@ -998,7 +998,7 @@ bool TimeSystemAbility::RecoverTimer() return true; } -void TimeSystemAbility::RecoverTimerInner(std::shared_ptr resultSet) +void TimeSystemAbility::RecoverTimerInner(std::shared_ptr resultSet, bool autoReboot) { auto timerManager = TimerManager::GetInstance(); if (timerManager == nullptr) { @@ -1017,6 +1017,8 @@ void TimeSystemAbility::RecoverTimerInner(std::shared_ptr(GetLong(resultSet, 4)), // Line 2 is 'flag' GetInt(resultSet, 2), + // autoRestore depends on the table type + autoReboot, // Callback can't recover. nullptr, // Line 7 is 'wantAgent' diff --git a/services/time_system_ability.h b/services/time_system_ability.h index f09f446a..7c100bbd 100644 --- a/services/time_system_ability.h +++ b/services/time_system_ability.h @@ -113,7 +113,7 @@ private: void RegisterPackageRemovedSubscriber(); void RegisterOsAccountSubscriber(); bool IsValidTime(int64_t time); - void RecoverTimerInner(std::shared_ptr resultSet); + void RecoverTimerInner(std::shared_ptr resultSet, bool autoReboot); void SetAutoReboot(); ServiceRunningState state_; diff --git a/services/timer/include/timer_info.h b/services/timer/include/timer_info.h index f87cf6f8..5541d2e5 100644 --- a/services/timer/include/timer_info.h +++ b/services/timer/include/timer_info.h @@ -31,6 +31,7 @@ public: const int type; const std::chrono::milliseconds origWhen; const bool wakeup; + const bool autoRestore; const std::function callback; const std::shared_ptr wantAgent; const uint32_t flags; @@ -56,6 +57,7 @@ public: std::function callback, std::shared_ptr wantAgent, uint32_t flags, + bool autoRestore, int uid, int pid, const std::string &bundleName); diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 85a0fb15..7b037ac7 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -79,6 +79,7 @@ private: int64_t windowLength, uint64_t interval, int flag, + bool autoRestore, std::function callback, std::shared_ptr wantAgent, int uid, @@ -94,6 +95,7 @@ private: std::function callback, const std::shared_ptr &wantAgent, uint32_t flags, + bool autoRestore, uint64_t callingUid, uint64_t callingPid, const std::string &bundleName); @@ -127,7 +129,7 @@ private: bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr &alarm); bool AdjustTimersBasedOnDeviceIdle(); void HandleRepeatTimer(const std::shared_ptr &timer, std::chrono::steady_clock::time_point nowElapsed); - inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type); + inline bool CheckNeedRecoverOnReboot(bool autoRestore, int type); #ifdef POWER_MANAGER_ENABLE void HandleRunningLock(const std::shared_ptr &firstWakeup); void AddRunningLock(long long holdLockTime); diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index 6c3dd44c..5e0e067b 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -29,6 +29,7 @@ struct TimerEntry { int64_t windowLength; uint64_t interval; int flag; + bool autoRestore; std::function callback; std::shared_ptr wantAgent; int uid; diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index 9ad408a5..2555c1c1 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -40,6 +40,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type, std::function _callback, std::shared_ptr _wantAgent, uint32_t _flags, + bool _autoRestore, int _uid, int _pid, const std::string &_bundleName) @@ -47,6 +48,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type, type {_type}, origWhen {_when}, wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP}, + autoRestore {_autoRestore}, callback {std::move(_callback)}, wantAgent {_wantAgent}, flags {_flags}, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 63b86436..5cc883e7 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -178,6 +178,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, paras.windowLength, paras.interval, paras.flag, + paras.autoRestore, std::move(callback), wantAgent, uid, @@ -189,7 +190,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, } if (type == NOT_STORE) { return E_TIME_OK; - } else if (CheckNeedRecoverOnReboot(bundleName, paras.timerType)) { + } else if (CheckNeedRecoverOnReboot(paras.autoRestore, paras.timerType)) { TimeDatabase::GetInstance().Insert(std::string(HOLD_ON_REBOOT), GetInsertValues(timerInfo, paras)); } else { @@ -229,10 +230,10 @@ int32_t TimerManager::StartTimer(uint64_t timerId, uint64_t triggerTime) RemoveLocked(timerId, false); } SetHandler(timerInfo->id, timerInfo->type, triggerTime, timerInfo->windowLength, timerInfo->interval, - timerInfo->flag, timerInfo->callback, timerInfo->wantAgent, timerInfo->uid, timerInfo->pid, - timerInfo->bundleName); + timerInfo->flag, timerInfo->autoRestore, timerInfo->callback, timerInfo->wantAgent, timerInfo->uid, + timerInfo->pid, timerInfo->bundleName); } - auto tableName = (CheckNeedRecoverOnReboot(timerInfo->bundleName, timerInfo->type) + auto tableName = (CheckNeedRecoverOnReboot(timerInfo->autoRestore, timerInfo->type) ? HOLD_ON_REBOOT : DROP_ON_REBOOT); OHOS::NativeRdb::ValuesBucket values; @@ -334,7 +335,7 @@ int32_t TimerManager::StopTimerInner(uint64_t timerNumber, bool needDestroy) TimerProxy::GetInstance().RemovePidProxy(timerNumber, it->second->pid); TimerProxy::GetInstance().EraseTimerFromProxyUidMap(timerNumber, it->second->uid); TimerProxy::GetInstance().EraseTimerFromProxyPidMap(timerNumber, it->second->pid); - needRecoverOnReboot = CheckNeedRecoverOnReboot(it->second->bundleName, it->second->type); + needRecoverOnReboot = CheckNeedRecoverOnReboot(it->second->autoRestore, it->second->type); if (needDestroy) { int uid = it->second->uid; timerEntryMap_.erase(it); @@ -373,6 +374,7 @@ void TimerManager::SetHandler(uint64_t id, int64_t windowLength, uint64_t interval, int flag, + bool autoRestore, std::function callback, std::shared_ptr wantAgent, int uid, @@ -416,6 +418,7 @@ void TimerManager::SetHandler(uint64_t id, std::move(callback), wantAgent, static_cast(flag), + autoRestore, uid, pid, bundleName); @@ -430,13 +433,14 @@ void TimerManager::SetHandlerLocked(uint64_t id, int type, std::function callback, const std::shared_ptr &wantAgent, uint32_t flags, + bool autoRestore, uint64_t callingUid, uint64_t callingPid, const std::string &bundleName) { TIME_HILOGD(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id); auto alarm = std::make_shared(id, type, when, whenElapsed, windowLength, maxWhen, - interval, std::move(callback), wantAgent, flags, callingUid, + interval, std::move(callback), wantAgent, flags, autoRestore, callingUid, callingPid, bundleName); if (TimerProxy::GetInstance().IsUidProxy(alarm->uid)) { TIME_HILOGI(TIME_MODULE_SERVICE, "Timer already proxy, uid=%{public}" PRIu64 " id=%{public}" PRId64 "", @@ -914,10 +918,10 @@ void TimerManager::DeliverTimersLocked(const std::vectorwantAgent) { - if (!NotifyWantAgent(timer) && CheckNeedRecoverOnReboot(timer->bundleName, timer->type)) { + if (!NotifyWantAgent(timer) && CheckNeedRecoverOnReboot(timer->autoRestore, timer->type)) { NotifyWantAgentRetry(timer); } - if (CheckNeedRecoverOnReboot(timer->bundleName, timer->type)) { + if (CheckNeedRecoverOnReboot(timer->autoRestore, timer->type)) { OHOS::NativeRdb::ValuesBucket values; values.PutInt("state", 0); OHOS::NativeRdb::RdbPredicates rdbPredicates(HOLD_ON_REBOOT); @@ -1360,17 +1364,16 @@ void TimerManager::HandleRepeatTimer( auto nextElapsed = timer->whenElapsed + delta; SetHandlerLocked(timer->id, timer->type, timer->when + delta, nextElapsed, timer->windowLength, MaxTriggerTime(nowElapsed, nextElapsed, timer->repeatInterval), timer->repeatInterval, timer->callback, - timer->wantAgent, timer->flags, timer->uid, timer->pid, timer->bundleName); + timer->wantAgent, timer->flags, timer->autoRestore, timer->uid, timer->pid, timer->bundleName); } else { TimerProxy::GetInstance().RemoveUidTimerMap(timer); TimerProxy::GetInstance().RemovePidTimerMap(timer); } } -inline bool TimerManager::CheckNeedRecoverOnReboot(std::string bundleName, int type) +inline bool TimerManager::CheckNeedRecoverOnReboot(bool autoRestore, int type) { - return (std::find(NEED_RECOVER_ON_REBOOT.begin(), NEED_RECOVER_ON_REBOOT.end(), bundleName) != - NEED_RECOVER_ON_REBOOT.end() && (type == RTC || type == RTC_WAKEUP)); + return (autoRestore && (type == RTC || type == RTC_WAKEUP)); } #ifdef POWER_MANAGER_ENABLE diff --git a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h index c7f3d61c..05a745a4 100644 --- a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h +++ b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h @@ -29,6 +29,7 @@ public: void SetRepeat(bool repeat) override; void SetInterval(const uint64_t &interval) override; void SetWantAgent(std::shared_ptr wantAgent) override; + void SetAutoRestore(bool _autoRestore); }; TimerInfo::TimerInfo() = default; @@ -52,6 +53,10 @@ void TimerInfo::SetWantAgent(std::shared_ptrwantAgent = wantAgent; } +void TimerInfo::SetAutoRestore(bool _autoRestore) +{ + this->autoRestore = _autoRestore; +} void TimerInfo::OnTrigger() { } diff --git a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp index 35c9a395..8130041c 100644 --- a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp +++ b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp @@ -62,6 +62,7 @@ bool FuzzTimeCreateTimerV9(int64_t data, size_t size) timerInfo->SetType(static_cast(data)); timerInfo->SetInterval(data); timerInfo->SetRepeat(data); + timerInfo->SetAutoRestore(false); uint64_t timerId = 0; TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); TimeServiceClient::GetInstance()->DestroyTimer(timerId); diff --git a/test/unittest/service_test/include/timer_info_test.h b/test/unittest/service_test/include/timer_info_test.h index e3104c15..4368916f 100644 --- a/test/unittest/service_test/include/timer_info_test.h +++ b/test/unittest/service_test/include/timer_info_test.h @@ -41,6 +41,7 @@ public: virtual void OnTrigger() override; virtual void SetType(const int &type) override; virtual void SetRepeat(bool repeat) override; + void SetAutoRestore(bool restore); virtual void SetInterval(const uint64_t &interval) override; void SetDisposable(const bool &disposable); virtual void SetWantAgent(std::shared_ptr wantAgent) override; @@ -98,6 +99,11 @@ void TimerInfoTest::SetDisposable(const bool &_disposable) disposable = _disposable; } +void TimerInfoTest::SetAutoRestore(bool _autoRestore) +{ + autoRestore = _autoRestore; +} + } // namespace MiscServices } // namespace OHOS #endif \ No newline at end of file diff --git a/test/unittest/service_test/src/time_client_test.cpp b/test/unittest/service_test/src/time_client_test.cpp index e701cc63..1d9dafb3 100644 --- a/test/unittest/service_test/src/time_client_test.cpp +++ b/test/unittest/service_test/src/time_client_test.cpp @@ -413,6 +413,38 @@ HWTEST_F(TimeClientTest, GetThreadTimeNs001, TestSize.Level1) EXPECT_EQ(errCode, TimeError::E_TIME_OK); } +/** +* @tc.name: CreateTimer000 +* @tc.desc: Create system timer. +* @tc.type: FUNC +*/ +HWTEST_F(TimeClientTest, CreateTimer000, TestSize.Level1) +{ + auto timerInfo1 = std::make_shared(); + timerInfo1->SetType(timerInfo1->TIMER_TYPE_REALTIME); + timerInfo1->SetRepeat(false); + timerInfo1->SetInterval(0); + timerInfo1->SetAutoRestore(true); + timerInfo1->SetWantAgent(nullptr); + timerInfo1->SetCallbackInfo(TimeOutCallback1); + uint64_t timerId1; + auto errCode1 = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo1, timerId1); + TIME_HILOGI(TIME_MODULE_CLIENT, "!!!!! first timerId : %{public}" PRId64 "", timerId1); + EXPECT_EQ(errCode1, TimeError::E_TIME_OK); + + auto timerInfo2 = std::make_shared(); + timerInfo2->SetType(timerInfo2->TIMER_TYPE_REALTIME); + timerInfo2->SetRepeat(false); + timerInfo2->SetInterval(0); + timerInfo2->SetAutoRestore(false); + timerInfo2->SetWantAgent(nullptr); + timerInfo2->SetCallbackInfo(TimeOutCallback1); + uint64_t timerId2; + auto errCode2 = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo2, timerId2); + TIME_HILOGI(TIME_MODULE_CLIENT, "!!!!! first timerId : %{public}" PRId64 "", timerId2); + EXPECT_EQ(errCode2, TimeError::E_TIME_OK); +} + /** * @tc.name: CreateTimer001 * @tc.desc: Create system timer. @@ -957,6 +989,7 @@ HWTEST_F(TimeClientTest, StartTimer013, TestSize.Level1) timerInfo->SetType(timerInfo->TIMER_TYPE_EXACT); timerInfo->SetRepeat(false); timerInfo->SetDisposable(true); + timerInfo->SetAutoRestore(true); timerInfo->SetCallbackInfo(TimeOutCallback1); auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); EXPECT_EQ(errCode, TimeError::E_TIME_OK); @@ -985,6 +1018,7 @@ HWTEST_F(TimeClientTest, StartTimer014, TestSize.Level1) timerInfo->SetRepeat(true); timerInfo->SetInterval(1000); timerInfo->SetDisposable(true); + timerInfo->SetAutoRestore(true); timerInfo->SetCallbackInfo(TimeOutCallback1); auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); EXPECT_EQ(errCode, TimeError::E_TIME_OK); diff --git a/test/unittest/service_test/src/time_proxy_test.cpp b/test/unittest/service_test/src/time_proxy_test.cpp index 32e08233..f7057f74 100644 --- a/test/unittest/service_test/src/time_proxy_test.cpp +++ b/test/unittest/service_test/src/time_proxy_test.cpp @@ -805,11 +805,11 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover002, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, 0, ""); + nullptr, nullptr, 0, false, UID, 0, ""); auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1); EXPECT_EQ(res, E_TIME_OK); auto timerInfo2 = std::make_shared(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, 0, ""); + nullptr, nullptr, 0, false, UID, 0, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -861,11 +861,11 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover003, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, PID, ""); + nullptr, nullptr, 0, false, 0, PID, ""); auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1); EXPECT_EQ(res, E_TIME_OK); auto timerInfo2 = std::make_shared(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, PID, ""); + nullptr, nullptr, 0, false, 0, PID, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -907,7 +907,7 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover004, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, PID, ""); + nullptr, nullptr, 0, false, UID, PID, ""); TimerProxy::GetInstance().RecordUidTimerMap(timerInfo, false); { std::lock_guard lock(TimerProxy::GetInstance().uidTimersMutex_); diff --git a/test/unittest/service_test/src/time_service_test.cpp b/test/unittest/service_test/src/time_service_test.cpp index ffdb25bf..46b517ab 100644 --- a/test/unittest/service_test/src/time_service_test.cpp +++ b/test/unittest/service_test/src/time_service_test.cpp @@ -1105,7 +1105,7 @@ HWTEST_F(TimeServiceTest, TimerManager001, TestSize.Level0) { auto timerId1 = TIMER_ID; auto entry = std::make_shared( - TimerEntry{timerId1, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{timerId1, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(timerId1, entry); std::lock_guard lock(TimerManager::GetInstance()->entryMapMutex_); @@ -1131,6 +1131,7 @@ HWTEST_F(TimeServiceTest, TimerManager002, TestSize.Level0) 10, 0, 1, + false, nullptr, nullptr, 0, @@ -1167,7 +1168,7 @@ HWTEST_F(TimeServiceTest, TimerManager004, TestSize.Level0) { TimerManager::GetInstance()->DestroyTimer(TIMER_ID); auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, UID, PID, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, PID, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); { @@ -1219,7 +1220,7 @@ HWTEST_F(TimeServiceTest, TimerManager005, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); auto res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo); EXPECT_FALSE(res); @@ -1287,7 +1288,7 @@ HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); std::lock_guard lock(TimerManager::GetInstance()->mutex_); auto alarm = TimerManager::GetInstance()->mPendingIdleUntil_; TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo1; @@ -1301,11 +1302,11 @@ HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0) auto duration1 = std::chrono::duration_cast( (timePoint + std::chrono::hours(1)).time_since_epoch()); auto timerInfo2 = std::make_shared(TIMER_ID, 1, duration1, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo2); EXPECT_TRUE(res); auto timerInfo3 = std::make_shared(TIMER_ID, 2, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo3); EXPECT_TRUE(res); @@ -1333,7 +1334,7 @@ HWTEST_F(TimeServiceTest, TimerManager008, TestSize.Level0) HWTEST_F(TimeServiceTest, TimerManager009, TestSize.Level0) { auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); uint64_t triggerTime = std::numeric_limits::max(); TimerManager::GetInstance()->StartTimer(TIMER_ID, triggerTime); @@ -1360,13 +1361,13 @@ HWTEST_F(TimeServiceTest, TimerManager010, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); { std::lock_guard lock(TimerManager::GetInstance()->mutex_); TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo; } auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); TimerManager::GetInstance()->HandleRSSDeath(); auto res = TimerManager::GetInstance()->DestroyTimer(TIMER_ID); @@ -1406,7 +1407,7 @@ HWTEST_F(TimeServiceTest, TimerManager012, TestSize.Level0) } auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, UID, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); timerManager->ReCreateTimer(TIMER_ID, entry); timerManager->OnPackageRemoved(UID); @@ -1469,13 +1470,13 @@ HWTEST_F(TimeServiceTest, TimerManager014, TestSize.Level0) uint64_t i = 0; for (; i <= TIMER_ALARM_COUNT; ++i) { auto entry = std::make_shared( - TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(i, entry); } EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 1); for (; i <= TIMER_ALARM_COUNT * 2; ++i) { auto entry = std::make_shared( - TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(i, entry); } EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 2); @@ -1669,7 +1670,7 @@ HWTEST_F(TimeServiceTest, TimerInfo001, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr, - nullptr, 0, 0, 0, ""); + nullptr, 0, false, 0, 0, ""); auto res = timerInfo.UpdateWhenElapsedFromNow(timePoint, duration); EXPECT_FALSE(res); } @@ -1684,7 +1685,7 @@ HWTEST_F(TimeServiceTest, TimerInfo002, TestSize.Level0) auto duration = std::chrono::milliseconds(0); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr, - nullptr, 0, 0, 0, ""); + nullptr, 0, false, 0, 0, ""); auto res = timerInfo.AdjustTimer(timePoint, 1); EXPECT_TRUE(res); } diff --git a/utils/native/include/time_common.h b/utils/native/include/time_common.h index 6ae9176e..a8b5841f 100644 --- a/utils/native/include/time_common.h +++ b/utils/native/include/time_common.h @@ -30,6 +30,7 @@ struct TimerPara { int64_t windowLength; uint64_t interval; bool disposable; + bool autoRestore; int flag; }; -- Gitee