1 /*
2 * Copyright (c) 2025 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 "mock_my_flag.h"
17 #include "js_runtime.h"
18 #include <gmock/gmock.h>
19 #include "native_reference.h"
20 #include "ohos_js_environment_impl.h"
21 #include "js_environment.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25 class NativeReferenceMock : public NativeReference {
26 public:
27 NativeReferenceMock() = default;
28 virtual ~NativeReferenceMock() = default;
29 MOCK_METHOD0(Ref, uint32_t());
30 MOCK_METHOD0(Unref, uint32_t());
31 MOCK_METHOD0(Get, napi_value());
32 MOCK_METHOD0(GetData, void*());
operator napi_value()33 virtual operator napi_value() override
34 {
35 return reinterpret_cast<napi_value>(this);
36 }
37 MOCK_METHOD0(SetDeleteSelf, void());
38 MOCK_METHOD0(GetRefCount, uint32_t());
39 MOCK_METHOD0(GetFinalRun, bool());
GetNapiValue()40 napi_value GetNapiValue() override
41 {
42 return nullptr;
43 }
44 };
45
VirtualFunc(napi_env env,napi_callback_info info)46 napi_value VirtualFunc(napi_env env, napi_callback_info info)
47 {
48 return nullptr;
49 }
50
JsRuntime()51 JsRuntime::JsRuntime()
52 {
53 jsEnv_ = std::make_shared<JsEnv::JsEnvironment>(std::make_unique<OHOSJsEnvironmentImpl>());
54 }
55
~JsRuntime()56 JsRuntime::~JsRuntime()
57 {
58 }
59
LoadSystemModuleByEngine(napi_env env,const std::string & moduleName,const napi_value * argv,size_t argc)60 std::unique_ptr<NativeReference> JsRuntime::LoadSystemModuleByEngine(
61 napi_env env, const std::string& moduleName, const napi_value* argv, size_t argc)
62 {
63 if (MyFlag::isLoadSystemModuleByEngine_) {
64 return std::make_unique<NativeReferenceMock>();
65 }
66 return nullptr;
67 }
68
LoadSystemModule(const std::string & moduleName,const napi_value * argv,size_t argc)69 std::unique_ptr<NativeReference> JsRuntime::LoadSystemModule(
70 const std::string& moduleName, const napi_value* argv, size_t argc)
71 {
72 return nullptr;
73 }
74
GetExportObjectFromOhmUrl(const std::string & srcEntrance,const std::string & key)75 napi_value JsRuntime::GetExportObjectFromOhmUrl(const std::string &srcEntrance, const std::string &key)
76 {
77 if (MyFlag::isGetExportObjectFromOhmUrlNullptr_) {
78 return nullptr;
79 }
80 napi_value fn;
81 napi_create_function(GetNapiEnv(), NULL, 0, VirtualFunc, NULL, &fn);
82 return fn;
83 }
84
ExecuteSecureWithOhmUrl(const std::string & moduleName,const std::string & hapPath,const std::string & srcEntrance)85 bool JsRuntime::ExecuteSecureWithOhmUrl(const std::string &moduleName, const std::string &hapPath,
86 const std::string &srcEntrance)
87 {
88 return MyFlag::isExecuteSecureWithOhmUrl_;
89 }
90
GetNapiEnv() const91 napi_env JsRuntime::GetNapiEnv() const
92 {
93 if (MyFlag::isGetNapiEnvNullptr_) {
94 return nullptr;
95 }
96 return reinterpret_cast<napi_env>(jsEnv_->GetNativeEngine());
97 }
98 } // namespace AbilityRuntime
99 } // namespace OHOS