1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "common/include/task_scheduler.h" 17 #include <gtest/gtest.h> 18 19 using namespace testing; 20 using namespace testing::ext; 21 namespace OHOS { 22 namespace Rosen { 23 class TaskSchedulerText : public testing::Test { 24 public: TaskSchedulerText()25 TaskSchedulerText() {} ~TaskSchedulerText()26 ~TaskSchedulerText() {} 27 }; 28 29 namespace { 30 /** 31 * @tc.name: task_scheduler_test001 32 * @tc.desc: normal function 33 * @tc.type: FUNC 34 */ 35 HWTEST_F(TaskSchedulerText, task_scheduler_test001, Function | SmallTest | Level2) 36 { 37 GTEST_LOG_(INFO) << "TaskSchedulerText: task_scheduler_test001 start"; 38 std::string threadName = "threadName"; 39 std::string name = "name"; 40 TaskScheduler* taskScheduler = new(std::nothrow) TaskScheduler(threadName); 41 int res = 0; 42 taskScheduler->RemoveTask(name); 43 ASSERT_EQ(res, 0); 44 delete taskScheduler; 45 GTEST_LOG_(INFO) << "TaskSchedulerText: task_scheduler_test001 end"; 46 } 47 48 /** 49 * @tc.name: GetEventHandler 50 * @tc.desc: GetEventHandler function 51 * @tc.type: FUNC 52 */ 53 HWTEST_F(TaskSchedulerText, GetEventHandler, Function | SmallTest | Level2) 54 { 55 std::string threadName = "threadName"; 56 std::shared_ptr<TaskScheduler> taskScheduler = std::make_shared<TaskScheduler>(threadName); 57 ASSERT_NE(taskScheduler, nullptr); 58 EXPECT_NE(taskScheduler->GetEventHandler(), nullptr); 59 } 60 61 /** 62 * @tc.name: PostTask 63 * @tc.desc: PostTask function 64 * @tc.type: FUNC 65 */ 66 HWTEST_F(TaskSchedulerText, PostTask, Function | SmallTest | Level2) 67 { 68 std::string threadName = "threadName"; 69 std::shared_ptr<TaskScheduler> taskScheduler = std::make_shared<TaskScheduler>(threadName); 70 ASSERT_NE(taskScheduler, nullptr); 71 int resultValue = 0; __anon264bb7220202() 72 auto taskFunc = [&resultValue]() { 73 GTEST_LOG_(INFO) << "START_TASK"; 74 resultValue = 1; 75 }; 76 taskScheduler->PostAsyncTask(taskFunc); 77 EXPECT_NE(taskScheduler->handler_, nullptr); 78 EXPECT_EQ(resultValue, 0); 79 80 std::string name = "ssmTask"; 81 int64_t delayTime = 1; 82 taskScheduler->PostAsyncTask(taskFunc, name, delayTime); 83 EXPECT_EQ(resultValue, 0); 84 } 85 } // namespace 86 } // namespace Rosen 87 } // namespace OHOS 88