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 JSMockNativeCustomSpan::JSBind(BindingTarget globalObj)
68 {
69 JSClass<JSMockNativeCustomSpan>::Declare("NativeCustomSpan");
70 JSClass<JSMockNativeCustomSpan>::CustomMethod("invalidate", &JSMockNativeCustomSpan::Invalidate);
71 JSClass<JSMockNativeCustomSpan>::Bind(globalObj);
72 }
73
JSBind(BindingTarget globalObj)74 void JSMockViewBuildNodeBase::JSBind(BindingTarget globalObj)
75 {
76 JSClass<JSMockViewBuildNodeBase>::Declare("ViewBuildNodeBase");
77
78 JSClass<JSMockViewBuildNodeBase>::StaticMethod(
79 "setArkThemeScopeManager", &JSMockViewBuildNodeBase::SetArkThemeScopeManager);
80 JSClass<JSMockViewBuildNodeBase>::CustomMethod("id__", &JSMockViewBuildNodeBase::Id);
81 JSClass<JSMockViewBuildNodeBase>::CustomMethod("debugInfo__", &JSMockViewBuildNodeBase::DebugInfo);
82 JSClass<JSMockViewBuildNodeBase>::CustomMethod("debugInfoElmtId", &JSMockViewBuildNodeBase::DebugInfoElmtId);
83 JSClass<JSMockViewBuildNodeBase>::CustomMethod("getChildById", &JSMockViewBuildNodeBase::GetChildById);
84 JSClass<JSMockViewBuildNodeBase>::CustomMethod("addChild", &JSMockViewBuildNodeBase::AddChild);
85 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
86 "purgeDeletedElmtIds", &JSMockViewBuildNodeBase::PurgeDeletedElmtIds);
87 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
88 "updateStateVarsOfChildByElmtId", &JSMockViewBuildNodeBase::UpdateStateVarsOfChildByElmtId);
89 JSClass<JSMockViewBuildNodeBase>::CustomMethod("createOrGetNode", &JSMockViewBuildNodeBase::CreateOrGetNode);
90 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
91 "ifElseBranchUpdateFunction", &JSMockViewBuildNodeBase::IfElseBranchUpdateFunction);
92 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
93 "onWillApplyThemeInternally", &JSMockViewBuildNodeBase::OnWillApplyThemeInternally);
94 JSClass<JSMockViewBuildNodeBase>::CustomMethod("onWillApplyTheme", &JSMockViewBuildNodeBase::OnWillApplyTheme);
95 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
96 "onGlobalThemeChanged", &JSMockViewBuildNodeBase::OnGlobalThemeChanged);
97
98 JSClass<JSMockViewBuildNodeBase>::Bind(
99 globalObj, JSMockViewBuildNodeBase::ConstructorCallback, JSMockViewBuildNodeBase::DestructorCallback);
100 }
101
JSBind(BindingTarget globalObj)102 void JSMock::JSBind(BindingTarget globalObj)
103 {
104 JSMockBaseNode::JSBind(globalObj);
105 JSMockViewPU::JSBind(globalObj, "ViewPU");
106 JSMockViewPU::JSBind(globalObj, "PUV2ViewBase");
107 JSMockScopeUtil::JSBind(globalObj);
108 MockCustomDialogController::JSBind(globalObj);
109 JSMockNativeCustomSpan::JSBind(globalObj);
110 JSMockViewBuildNodeBase::JSBind(globalObj);
111 }
112
InitModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & thisObj,const std::string & moduleName)113 bool JSMock::InitModule(
114 const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj, const std::string& moduleName)
115 {
116 static const std::unordered_map<std::string,
117 void (*)(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj)>
118 MODULE_LIST = {
119 { "ohos.curves", [](const shared_ptr<JsRuntime>& runtime,
120 shared_ptr<JsValue>& thisObj) { InitCurvesModule(runtime, thisObj); } },
121 };
122 auto iter = MODULE_LIST.find(moduleName);
123 if (iter != MODULE_LIST.end()) {
124 iter->second(runtime, thisObj);
125 return true;
126 } else {
127 return false;
128 }
129 }
130
MockRequireNativeModule(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)131 shared_ptr<JsValue> MockRequireNativeModule(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
132 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
133 {
134 std::string moduleName = argv[0]->ToString(runtime);
135
136 // has already init module object
137 shared_ptr<JsValue> global = runtime->GetGlobal();
138 shared_ptr<JsValue> moduleObject = global->GetProperty(runtime, moduleName);
139 if (moduleObject != nullptr && moduleObject->IsObject(runtime)) {
140 return moduleObject;
141 }
142
143 // init module object first time
144 shared_ptr<JsValue> newObject = runtime->NewObject();
145 if (JSMock::InitModule(runtime, newObject, moduleName)) {
146 global->SetProperty(runtime, moduleName, newObject);
147 return newObject;
148 }
149
150 return runtime->NewNull();
151 }
152
PreloadWorkerRequireNative(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & global)153 bool JSMock::PreloadWorkerRequireNative(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& global)
154 {
155 return global->SetProperty(runtime, "requireNativeModule", runtime->NewFunction(MockRequireNativeModule));
156 }
157 } // namespace OHOS::Ace::Framework
158