1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci* Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci* you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci* You may obtain a copy of the License at 6bc03f14fSopenharmony_ci* 7bc03f14fSopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci* 9bc03f14fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci* See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci* limitations under the License. 14bc03f14fSopenharmony_ci*/ 15bc03f14fSopenharmony_ci 16bc03f14fSopenharmony_ci#include <gtest/gtest.h> 17bc03f14fSopenharmony_ci 18bc03f14fSopenharmony_ci#include "ffrt_utils.h" 19bc03f14fSopenharmony_ci 20bc03f14fSopenharmony_cinamespace OHOS::MiscServices { 21bc03f14fSopenharmony_ciusing namespace testing::ext; 22bc03f14fSopenharmony_ciusing namespace testing; 23bc03f14fSopenharmony_ci 24bc03f14fSopenharmony_ciclass FFRTTimerTest : public testing::Test { 25bc03f14fSopenharmony_cipublic: 26bc03f14fSopenharmony_ci static void SetUpTestCase(void); 27bc03f14fSopenharmony_ci static void TearDownTestCase(void); 28bc03f14fSopenharmony_ci void SetUp(); 29bc03f14fSopenharmony_ci void TearDown(); 30bc03f14fSopenharmony_ci}; 31bc03f14fSopenharmony_ci 32bc03f14fSopenharmony_civoid FFRTTimerTest::SetUpTestCase(void) {} 33bc03f14fSopenharmony_ci 34bc03f14fSopenharmony_civoid FFRTTimerTest::TearDownTestCase(void) {} 35bc03f14fSopenharmony_ci 36bc03f14fSopenharmony_civoid FFRTTimerTest::SetUp(void) {} 37bc03f14fSopenharmony_ci 38bc03f14fSopenharmony_civoid FFRTTimerTest::TearDown(void) {} 39bc03f14fSopenharmony_ci 40bc03f14fSopenharmony_ci/** 41bc03f14fSopenharmony_ci* @tc.name: SetTimerTest001 42bc03f14fSopenharmony_ci* @tc.desc: Set Timer 43bc03f14fSopenharmony_ci* @tc.type: FUNC 44bc03f14fSopenharmony_ci*/ 45bc03f14fSopenharmony_ciHWTEST_F(FFRTTimerTest, SetTimerTest001, TestSize.Level0) 46bc03f14fSopenharmony_ci{ 47bc03f14fSopenharmony_ci FFRTTimer ffrtTimer; 48bc03f14fSopenharmony_ci std::string timerId = "ffrt_test"; 49bc03f14fSopenharmony_ci int x = 4; 50bc03f14fSopenharmony_ci FFRTTask ffrtTask = [&x] { 51bc03f14fSopenharmony_ci x /= 2; 52bc03f14fSopenharmony_ci }; 53bc03f14fSopenharmony_ci ffrtTimer.SetTimer(timerId, ffrtTask, 0); 54bc03f14fSopenharmony_ci ffrt::wait(); 55bc03f14fSopenharmony_ci EXPECT_TRUE(x == 2); 56bc03f14fSopenharmony_ci} 57bc03f14fSopenharmony_ci 58bc03f14fSopenharmony_ci/** 59bc03f14fSopenharmony_ci* @tc.name: SetTimerTest002 60bc03f14fSopenharmony_ci* @tc.desc: Set Timer 61bc03f14fSopenharmony_ci* @tc.type: FUNC 62bc03f14fSopenharmony_ci*/ 63bc03f14fSopenharmony_ciHWTEST_F(FFRTTimerTest, SetTimerTest002, TestSize.Level0) 64bc03f14fSopenharmony_ci{ 65bc03f14fSopenharmony_ci FFRTTimer pbFfrtTimer("paste_ffrt_timer"); 66bc03f14fSopenharmony_ci std::string pbTimerId = "paste_ffrt_test1"; 67bc03f14fSopenharmony_ci int y = 8; 68bc03f14fSopenharmony_ci FFRTTask pbFfrtTask = [&y] { 69bc03f14fSopenharmony_ci y /= 2; 70bc03f14fSopenharmony_ci }; 71bc03f14fSopenharmony_ci pbFfrtTimer.SetTimer(pbTimerId, pbFfrtTask, 5); 72bc03f14fSopenharmony_ci ffrt::wait(); 73bc03f14fSopenharmony_ci uint32_t taskId = pbFfrtTimer.GetTaskId(pbTimerId); 74bc03f14fSopenharmony_ci EXPECT_TRUE(taskId == 1); 75bc03f14fSopenharmony_ci} 76bc03f14fSopenharmony_ci 77bc03f14fSopenharmony_ci/** 78bc03f14fSopenharmony_ci* @tc.name: SubmitQueueTasksTest 79bc03f14fSopenharmony_ci* @tc.desc: Submit Queue Tasks 80bc03f14fSopenharmony_ci* @tc.type: FUNC 81bc03f14fSopenharmony_ci*/ 82bc03f14fSopenharmony_ciHWTEST_F(FFRTTimerTest, SubmitQueueTasksTest, TestSize.Level0) 83bc03f14fSopenharmony_ci{ 84bc03f14fSopenharmony_ci std::vector<FFRTTask> tasks; 85bc03f14fSopenharmony_ci FFRTQueue que("QueueTasks"); 86bc03f14fSopenharmony_ci std::vector<FFRTTask>().swap(tasks); 87bc03f14fSopenharmony_ci FFRTUtils::SubmitQueueTasks(tasks, que); 88bc03f14fSopenharmony_ci int x = 2; 89bc03f14fSopenharmony_ci int z = 19; 90bc03f14fSopenharmony_ci FFRTTask task1 = [&x] { 91bc03f14fSopenharmony_ci x <<= 3; 92bc03f14fSopenharmony_ci }; 93bc03f14fSopenharmony_ci FFRTTask task2 = [&z] { 94bc03f14fSopenharmony_ci z /= 2; 95bc03f14fSopenharmony_ci }; 96bc03f14fSopenharmony_ci tasks.push_back(task1); 97bc03f14fSopenharmony_ci tasks.push_back(task2); 98bc03f14fSopenharmony_ci FFRTUtils::SubmitQueueTasks(tasks, que); 99bc03f14fSopenharmony_ci EXPECT_TRUE(true); 100bc03f14fSopenharmony_ci} 101bc03f14fSopenharmony_ci 102bc03f14fSopenharmony_ci/** 103bc03f14fSopenharmony_ci* @tc.name: SubmitDelayTaskTest 104bc03f14fSopenharmony_ci* @tc.desc: Submit Delay Task 105bc03f14fSopenharmony_ci* @tc.type: FUNC 106bc03f14fSopenharmony_ci*/ 107bc03f14fSopenharmony_ciHWTEST_F(FFRTTimerTest, SubmitDelayTaskTest, TestSize.Level0) 108bc03f14fSopenharmony_ci{ 109bc03f14fSopenharmony_ci std::shared_ptr<FFRTQueue> queu = std::make_shared<FFRTQueue>("delayTask"); 110bc03f14fSopenharmony_ci uint32_t delayMs = 20; 111bc03f14fSopenharmony_ci int x = 10; 112bc03f14fSopenharmony_ci FFRTTask task0 = [&x] { 113bc03f14fSopenharmony_ci x <<= 3; 114bc03f14fSopenharmony_ci }; 115bc03f14fSopenharmony_ci FFRTHandle handle = FFRTUtils::SubmitDelayTask(task0, delayMs, queu); 116bc03f14fSopenharmony_ci FFRTUtils::CancelTask(handle, queu); 117bc03f14fSopenharmony_ci EXPECT_TRUE(true); 118bc03f14fSopenharmony_ci} 119bc03f14fSopenharmony_ci 120bc03f14fSopenharmony_ci/** 121bc03f14fSopenharmony_ci* @tc.name: SubmitTimeoutTaskTest 122bc03f14fSopenharmony_ci* @tc.desc: Submit Timeout Task 123bc03f14fSopenharmony_ci* @tc.type: FUNC 124bc03f14fSopenharmony_ci*/ 125bc03f14fSopenharmony_ciHWTEST_F(FFRTTimerTest, SubmitTimeoutTaskTest, TestSize.Level0) 126bc03f14fSopenharmony_ci{ 127bc03f14fSopenharmony_ci uint32_t timeoutMs = 5; 128bc03f14fSopenharmony_ci int x = 9; 129bc03f14fSopenharmony_ci FFRTTask task = [&x] { 130bc03f14fSopenharmony_ci x <<= 3; 131bc03f14fSopenharmony_ci }; 132bc03f14fSopenharmony_ci bool res = FFRTUtils::SubmitTimeoutTask(task, timeoutMs); 133bc03f14fSopenharmony_ci EXPECT_TRUE(res); 134bc03f14fSopenharmony_ci} 135bc03f14fSopenharmony_ci} // namespace OHOS::MiscServices