1 /* 2 * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_TEST_MOCK_MOCK_TASK_EXECUTOR_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_TEST_MOCK_MOCK_TASK_EXECUTOR_H 18 19 #include "base/thread/background_task_executor.h" 20 #include "base/thread/task_executor.h" 21 22 namespace OHOS::Ace { 23 class MockTaskExecutor final : public TaskExecutor { 24 public: WillRunOnCurrentThread(TaskType type)25 bool WillRunOnCurrentThread(TaskType type) const final 26 { 27 switch (type) { 28 case TaskType::PLATFORM: 29 case TaskType::UI: 30 case TaskType::IO: 31 case TaskType::GPU: 32 case TaskType::JS: 33 return false; 34 case TaskType::BACKGROUND: 35 // Always return false for background tasks. 36 return false; 37 default: 38 return false; 39 } 40 } 41 AddTaskObserver(Task && callback)42 void AddTaskObserver(Task&& callback) {} RemoveTaskObserver()43 void RemoveTaskObserver() {} WrapTaskWithTraceId(Task && task,int32_t id)44 Task WrapTaskWithTraceId(Task&& task, int32_t id) const final 45 { 46 return std::move(task); 47 } 48 49 private: OnPostTask(Task && task,TaskType type,uint32_t delayTime)50 bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime) const final 51 { 52 switch (type) { 53 case TaskType::PLATFORM: 54 case TaskType::UI: 55 case TaskType::IO: 56 case TaskType::GPU: 57 case TaskType::JS: 58 return false; 59 case TaskType::BACKGROUND: 60 // Ignore delay time 61 return BackgroundTaskExecutor::GetInstance().PostTask(std::move(task)); 62 default: 63 return false; 64 } 65 } 66 }; 67 } // namespace OHOS::Ace 68 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_TEST_MOCK_MOCK_TASK_EXECUTOR_H 69