1 /*
2  * Copyright (C) 2024 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 "component_test/component_test_manager_impl.h"
17 
18 #include <mutex>
19 #include <shared_mutex>
20 #include <unordered_map>
21 
22 #include "adapter/preview/entrance/ace_container.h"
23 
24 namespace OHOS::Ace::ComponentTest {
25 
26 RefPtr<ComponentTestManagerImpl> ComponentTestManagerImpl::instance = nullptr;
27 std::mutex ComponentTestManagerImpl::mtx_;
28 
Get()29 RefPtr<ComponentTestManagerImpl> ComponentTestManagerImpl::Get()
30 {
31     std::lock_guard<std::mutex> lock(mtx_);
32     if (instance == nullptr) {
33         instance = new ComponentTestManagerImpl();
34     }
35     return instance;
36 }
37 
GetInitialized()38 RefPtr<ComponentTestManagerImpl> ComponentTestManagerImpl::GetInitialized()
39 {
40     std::lock_guard<std::mutex> lock(mtx_);
41     if (instance == nullptr) {
42         instance = new ComponentTestManagerImpl();
43     }
44     if (!instance->initialized_) {
45         auto container = OHOS::Ace::Container::GetContainer(Platform::ACE_INSTANCE_ID);
46         if (container) {
47             auto taskExecutor = container->GetTaskExecutor();
48             if (taskExecutor) {
49                 instance->Initialize(taskExecutor);
50             }
51         }
52     }
53     return instance;
54 }
55 
ComponentTestManagerImpl()56 ComponentTestManagerImpl::ComponentTestManagerImpl()
57 {
58     initialized_ = false;
59 }
60 
Initialize(RefPtr<TaskExecutor> taskExecutor)61 void ComponentTestManagerImpl::Initialize(RefPtr<TaskExecutor> taskExecutor)
62 {
63     idleWatcher_ = AceType::MakeRefPtr<IdleWatcher>(taskExecutor);
64     testTaskScheduler_.Initialize(idleWatcher_, taskExecutor);
65     testResultRecorder_.Initialize(testConfig.GetOut());
66     initialized_ = true;
67 }
68 
Destroy()69 void ComponentTestManagerImpl::Destroy()
70 {
71     testTaskScheduler_.Destroy();
72     idleWatcher_->Destroy();
73 }
74 
PostUITask( std::function<void(void*)>&& task, std::function<void(void*)>&& onFinish, void* data, uint32_t delay)75 void ComponentTestManagerImpl::PostUITask(
76     std::function<void(void*)>&& task, std::function<void(void*)>&& onFinish, void* data, uint32_t delay)
77 {
78     return testTaskScheduler_.PushTask(std::move(task), std::move(onFinish), TaskExecutor::TaskType::UI, data, delay);
79 }
80 
PostJSTask( std::function<void(void*)>&& task, std::function<void(void*)>&& onFinish, void* data, uint32_t delay)81 void ComponentTestManagerImpl::PostJSTask(
82     std::function<void(void*)>&& task, std::function<void(void*)>&& onFinish, void* data, uint32_t delay)
83 {
84     return testTaskScheduler_.PushTask(std::move(task), std::move(onFinish), TaskExecutor::TaskType::JS, data, delay);
85 }
86 
SetTestCaseAttribute(TestCaseAttribute attribute)87 void ComponentTestManagerImpl::SetTestCaseAttribute(TestCaseAttribute attribute)
88 {
89     testResultRecorder_.SetTestCaseAttribute(attribute);
90 }
91 
Record(std::string info, std::string position, Result result)92 void ComponentTestManagerImpl::Record(std::string info, std::string position, Result result)
93 {
94     testResultRecorder_.Record(info, position, result);
95 }
96 
Finish()97 void ComponentTestManagerImpl::Finish()
98 {
99     testResultRecorder_.Finish();
100 }
101 
ClaimLongOperation()102 void ComponentTestManagerImpl::ClaimLongOperation()
103 {
104     idleWatcher_->ClaimLongOperation();
105 }
106 
LongOperationComplete()107 void ComponentTestManagerImpl::LongOperationComplete()
108 {
109     idleWatcher_->LongOperationComplete();
110 }
111 
RequestContinuousIdleStatusNotification(IdleNotifycallback&& notifycallback)112 void ComponentTestManagerImpl::RequestContinuousIdleStatusNotification(IdleNotifycallback&& notifycallback)
113 {
114     idleWatcher_->RequestContinuousIdleStatusNotification(std::move(notifycallback));
115 }
116 
117 } // namespace OHOS::Ace::ComponentTest
118