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 #include "bridge/declarative_frontend/jsview/js_mock.h"
16 #include "bridge/declarative_frontend/engine/jsi/jsi_bindings.h"
17 #include "bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h"
18 #include "bridge/js_frontend/engine/jsi/ark_js_value.h"
19 
20 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)21 void JSMockBaseNode::JSBind(BindingTarget globalObj)
22 {
23     JSClass<JSMockBaseNode>::Declare("__JSBaseNode__");
24 
25     JSClass<JSMockBaseNode>::CustomMethod("create", &JSMockBaseNode::Create);
26     JSClass<JSMockBaseNode>::CustomMethod("finishUpdateFunc", &JSMockBaseNode::FinishUpdateFunc);
27     JSClass<JSMockBaseNode>::CustomMethod("postTouchEvent", &JSMockBaseNode::PostTouchEvent);
28     JSClass<JSMockBaseNode>::CustomMethod("disposeNode", &JSMockBaseNode::Dispose);
29     JSClass<JSMockBaseNode>::CustomMethod("updateStart", &JSMockBaseNode::UpdateStart);
30     JSClass<JSMockBaseNode>::CustomMethod("updateEnd", &JSMockBaseNode::UpdateEnd);
31 
32     JSClass<JSMockBaseNode>::Bind(globalObj, JSMockBaseNode::ConstructorCallback, JSMockBaseNode::DestructorCallback);
33 }
34 
JSBind(BindingTarget globalObj,std::string name)35 void JSMockViewPU::JSBind(BindingTarget globalObj, std::string name)
36 {
37     JSClass<JSMockViewPU>::Declare(name.c_str());
38 
39     JSClass<JSMockViewPU>::CustomMethod("id__", &JSMockViewPU::Id);
40     JSClass<JSMockViewPU>::CustomMethod("updateStateVars", &JSMockViewPU::UpdateStateVars);
41     JSClass<JSMockViewPU>::CustomMethod("aboutToReuseInternal", &JSMockViewPU::AboutToReuseInternal);
42     JSClass<JSMockViewPU>::CustomMethod("aboutToRecycleInternal", &JSMockViewPU::AboutToRecycleInternal);
43     JSClass<JSMockViewPU>::CustomMethod("updateDirtyElements", &JSMockViewPU::UpdateDirtyElements);
44 
45     JSClass<JSMockViewPU>::Bind(globalObj);
46 }
47 
JSBind(BindingTarget globalObj)48 void JSMockScopeUtil::JSBind(BindingTarget globalObj)
49 {
50     JSClass<JSMockScopeUtil>::Declare("__JSScopeUtil__");
51     JSClass<JSMockScopeUtil>::StaticMethod("syncInstanceId", &JSMockScopeUtil::SyncInstanceId);
52     JSClass<JSMockScopeUtil>::StaticMethod("restoreInstanceId", &JSMockScopeUtil::RestoreInstanceId);
53 
54     JSClass<JSMockScopeUtil>::Bind(globalObj);
55 }
56 
JSBind(BindingTarget globalObj)57 void MockCustomDialogController::JSBind(BindingTarget globalObj)
58 {
59     JSClass<MockCustomDialogController>::Declare("CustomDialogController");
60 
61     JSClass<MockCustomDialogController>::CustomMethod("open", &MockCustomDialogController::Open);
62     JSClass<MockCustomDialogController>::CustomMethod("close", &MockCustomDialogController::Close);
63 
64     JSClass<JSMockBaseNode>::Bind(globalObj);
65 }
66 
JSBind(BindingTarget globalObj)67 void JSMock::JSBind(BindingTarget globalObj)
68 {
69     JSMockBaseNode::JSBind(globalObj);
70     JSMockViewPU::JSBind(globalObj, "ViewPU");
71     JSMockViewPU::JSBind(globalObj, "PUV2ViewBase");
72     JSMockScopeUtil::JSBind(globalObj);
73     MockCustomDialogController::JSBind(globalObj);
74 }
75 
InitModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & thisObj,const std::string & moduleName)76 bool JSMock::InitModule(
77     const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj, const std::string& moduleName)
78 {
79     static const std::unordered_map<std::string,
80         void (*)(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj)>
81         MODULE_LIST = {
82             { "ohos.curves", [](const shared_ptr<JsRuntime>& runtime,
83                                  shared_ptr<JsValue>& thisObj) { InitCurvesModule(runtime, thisObj); } },
84         };
85     auto iter = MODULE_LIST.find(moduleName);
86     if (iter != MODULE_LIST.end()) {
87         iter->second(runtime, thisObj);
88         return true;
89     } else {
90         return false;
91     }
92 }
93 
MockRequireNativeModule(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)94 shared_ptr<JsValue> MockRequireNativeModule(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
95     const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
96 {
97     std::string moduleName = argv[0]->ToString(runtime);
98 
99     // has already init module object
100     shared_ptr<JsValue> global = runtime->GetGlobal();
101     shared_ptr<JsValue> moduleObject = global->GetProperty(runtime, moduleName);
102     if (moduleObject != nullptr && moduleObject->IsObject(runtime)) {
103         return moduleObject;
104     }
105 
106     // init module object first time
107     shared_ptr<JsValue> newObject = runtime->NewObject();
108     if (JSMock::InitModule(runtime, newObject, moduleName)) {
109         global->SetProperty(runtime, moduleName, newObject);
110         return newObject;
111     }
112 
113     return runtime->NewNull();
114 }
115 
PreloadWorkerRequireNative(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & global)116 bool JSMock::PreloadWorkerRequireNative(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& global)
117 {
118     return global->SetProperty(runtime, "requireNativeModule", runtime->NewFunction(MockRequireNativeModule));
119 }
120 } // namespace OHOS::Ace::Framework
121