• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
LoadSystemModuleByEngine(napi_env env,const std::string & moduleName,const napi_value * argv,size_t argc)59 std::unique_ptr<NativeReference> JsRuntime::LoadSystemModuleByEngine(
60     napi_env env, const std::string& moduleName, const napi_value* argv, size_t argc)
61 {
62     if (MyFlag::isLoadSystemModuleByEngine_) {
63         return std::make_unique<NativeReferenceMock>();
64     }
65     return nullptr;
66 }
67 
LoadSystemModule(const std::string & moduleName,const napi_value * argv,size_t argc)68 std::unique_ptr<NativeReference> JsRuntime::LoadSystemModule(
69     const std::string& moduleName, const napi_value* argv, size_t argc)
70 {
71     return nullptr;
72 }
73 
GetExportObjectFromOhmUrl(const std::string & srcEntrance,const std::string & key)74 napi_value JsRuntime::GetExportObjectFromOhmUrl(const std::string &srcEntrance, const std::string &key)
75 {
76     if (MyFlag::isGetExportObjectFromOhmUrlNullptr_) {
77         return nullptr;
78     }
79     napi_value fn;
80     napi_create_function(GetNapiEnv(), NULL, 0, VirtualFunc, NULL, &fn);
81     return fn;
82 }
83 
ExecuteSecureWithOhmUrl(const std::string & moduleName,const std::string & hapPath,const std::string & srcEntrance)84 bool JsRuntime::ExecuteSecureWithOhmUrl(const std::string &moduleName, const std::string &hapPath,
85     const std::string &srcEntrance)
86 {
87     return MyFlag::isExecuteSecureWithOhmUrl_;
88 }
89 
GetNapiEnv() const90 napi_env JsRuntime::GetNapiEnv() const
91 {
92     if (MyFlag::isGetNapiEnvNullptr_) {
93         return nullptr;
94     }
95     return reinterpret_cast<napi_env>(jsEnv_->GetNativeEngine());
96 }
97 }  // namespace AbilityRuntime
98 }  // namespace OHOS