1 /*
2  * Copyright (c) 2022-2023 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 #ifndef MOCK_RUNTIME_H
17 #define MOCK_RUNTIME_H
18 
19 #include <gmock/gmock.h>
20 #include "js_runtime.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 class MockRuntime : public Runtime {
25 public:
26     const int DEFAULT_LANGUAGE = 100;
27     MockRuntime() = default;
28     ~MockRuntime() = default;
29 
GetLanguage()30     Language GetLanguage() const override
31     {
32         return static_cast<Runtime::Language>(DEFAULT_LANGUAGE);
33     }
34 
StartDebugMode(const DebugOption debugOption)35     void StartDebugMode(const DebugOption debugOption) override
36     {
37         return;
38     }
39 
FinishPreload()40     void FinishPreload() override {}
LoadRepairPatch(const std::string & patchFile,const std::string & baseFile)41     bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) override
42     {
43         return true;
44     }
NotifyHotReloadPage()45     bool NotifyHotReloadPage() override
46     {
47         return true;
48     }
SuspendVM(uint32_t tid)49     bool SuspendVM(uint32_t tid) override
50     {
51         return true;
52     }
ResumeVM(uint32_t tid)53     void ResumeVM(uint32_t tid) override {}
UnLoadRepairPatch(const std::string & patchFile)54     bool UnLoadRepairPatch(const std::string& patchFile) override
55     {
56         return true;
57     }
DumpHeapSnapshot(bool isPrivate)58     void DumpHeapSnapshot(bool isPrivate) override
59     {
60         return;
61     }
DumpCpuProfile()62     void DumpCpuProfile() override
63     {
64         return;
65     }
DestroyHeapProfiler()66     void DestroyHeapProfiler() override
67     {
68         return;
69     }
ForceFullGC()70     void ForceFullGC() override
71     {
72         return;
73     }
AllowCrossThreadExecution()74     void AllowCrossThreadExecution() override
75     {
76         return;
77     }
GetHeapPrepare()78     void GetHeapPrepare() override
79     {
80         return;
81     }
NotifyApplicationState(bool isBackground)82     void NotifyApplicationState(bool isBackground) override
83     {
84         return;
85     }
PreloadSystemModule(const std::string & moduleName)86     void PreloadSystemModule(const std::string& moduleName) override
87     {
88         return;
89     }
PreloadMainAbility(const std::string & moduleName,const std::string & srcPath,const std::string & hapPath,bool isEsMode,const std::string & srcEntrance)90     void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
91         const std::string& hapPath,  bool isEsMode, const std::string& srcEntrance)
92     {
93         return;
94     }
PreloadModule(const std::string & moduleName,const std::string & srcPath,const std::string & hapPath,bool isEsMode,bool useCommonTrunk)95     void PreloadModule(const std::string& moduleName, const std::string& srcPath,
96         const std::string& hapPath, bool isEsMode, bool useCommonTrunk)
97     {
98         return;
99     }
100     bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false)
101     {
102         return true;
103     }
Initialize(const Options & options)104     bool Initialize(const Options& options)
105     {
106         return true;
107     }
Deinitialize()108     void Deinitialize() {}
109     napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false)
110     {
111         return nullptr;
112     }
LoadJsModule(const std::string & path,const std::string & hapPath)113     napi_value LoadJsModule(const std::string& path, const std::string& hapPath)
114     {
115         return nullptr;
116     }
117     bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false)
118     {
119         return true;
120     }
RegisterQuickFixQueryFunc(const std::map<std::string,std::string> & moduleAndPath)121     void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override
122     {
123         return;
124     }
SetDeviceDisconnectCallback(const std::function<bool ()> & cb)125     void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override
126     {
127         return;
128     }
129 
StartProfiler(const DebugOption debugOption)130     void StartProfiler(const DebugOption debugOption) override
131     {
132         return;
133     }
134 
DoCleanWorkAfterStageCleaned()135     void DoCleanWorkAfterStageCleaned() override {}
DumpHeapSnapshot(uint32_t tid,bool isFullGC)136     void DumpHeapSnapshot(uint32_t tid, bool isFullGC) override {}
ForceFullGC(uint32_t tid)137     void ForceFullGC(uint32_t tid) override {}
UpdatePkgContextInfoJson(std::string moduleName,std::string hapPath,std::string packageName)138     void UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName) override {}
139 public:
140     Language language;
141 };
142 
143 class MockRuntimeJS : public Runtime {
144 public:
145     const int DEFAULT_LANGUAGE = 0;
146     MockRuntimeJS() = default;
147     ~MockRuntimeJS() = default;
148 
GetLanguage()149     Language GetLanguage() const override
150     {
151         return static_cast<Runtime::Language>(DEFAULT_LANGUAGE);
152     }
153 
StartDebugMode(const DebugOption debugOption)154     void StartDebugMode(const DebugOption debugOption) override
155     {
156         return;
157     }
158 
FinishPreload()159     void FinishPreload() override {}
LoadRepairPatch(const std::string & patchFile,const std::string & baseFile)160     bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) override
161     {
162         return true;
163     }
NotifyHotReloadPage()164     bool NotifyHotReloadPage() override
165     {
166         return true;
167     }
SuspendVM(uint32_t tid)168     bool SuspendVM(uint32_t tid) override
169     {
170         return true;
171     }
ResumeVM(uint32_t tid)172     void ResumeVM(uint32_t tid) override {}
UnLoadRepairPatch(const std::string & patchFile)173     bool UnLoadRepairPatch(const std::string& patchFile) override
174     {
175         return true;
176     }
DumpHeapSnapshot(bool isPrivate)177     void DumpHeapSnapshot(bool isPrivate) override
178     {
179         return;
180     }
DumpCpuProfile()181     void DumpCpuProfile() override
182     {
183         return;
184     }
DestroyHeapProfiler()185     void DestroyHeapProfiler() override
186     {
187         return;
188     }
ForceFullGC()189     void ForceFullGC() override
190     {
191         return;
192     }
AllowCrossThreadExecution()193     void AllowCrossThreadExecution() override
194     {
195         return;
196     }
GetHeapPrepare()197     void GetHeapPrepare() override
198     {
199         return;
200     }
NotifyApplicationState(bool isBackground)201     void NotifyApplicationState(bool isBackground) override
202     {
203         return;
204     }
PreloadSystemModule(const std::string & moduleName)205     void PreloadSystemModule(const std::string& moduleName) override
206     {
207         return;
208     }
PreloadMainAbility(const std::string & moduleName,const std::string & srcPath,const std::string & hapPath,bool isEsMode,const std::string & srcEntrance)209     void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
210         const std::string& hapPath,  bool isEsMode, const std::string& srcEntrance)
211     {
212         return;
213     }
PreloadModule(const std::string & moduleName,const std::string & srcPath,const std::string & hapPath,bool isEsMode,bool useCommonTrunk)214     void PreloadModule(const std::string& moduleName, const std::string& srcPath,
215         const std::string& hapPath, bool isEsMode, bool useCommonTrunk)
216     {
217         return;
218     }
219     bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false)
220     {
221         return true;
222     }
Initialize(const Options & options)223     bool Initialize(const Options& options)
224     {
225         return true;
226     }
Deinitialize()227     void Deinitialize() {}
228     napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false)
229     {
230         return nullptr;
231     }
LoadJsModule(const std::string & path,const std::string & hapPath)232     napi_value LoadJsModule(const std::string& path, const std::string& hapPath)
233     {
234         return nullptr;
235     }
236     bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false)
237     {
238         return true;
239     }
RegisterQuickFixQueryFunc(const std::map<std::string,std::string> & moduleAndPath)240     void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override
241     {
242         return;
243     }
SetDeviceDisconnectCallback(const std::function<bool ()> & cb)244     void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override
245     {
246         return;
247     }
248 
StartProfiler(const DebugOption debugOption)249     void StartProfiler(const DebugOption debugOption) override
250     {
251         return;
252     }
253 
DoCleanWorkAfterStageCleaned()254     void DoCleanWorkAfterStageCleaned() override {}
DumpHeapSnapshot(uint32_t tid,bool isFullGC)255     void DumpHeapSnapshot(uint32_t tid, bool isFullGC) override {}
ForceFullGC(uint32_t tid)256     void ForceFullGC(uint32_t tid) override {}
UpdatePkgContextInfoJson(std::string moduleName,std::string hapPath,std::string packageName)257     void UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName) override {}
258 public:
259     Language language;
260 };
261 } // namespace AbilityRuntime
262 } // namespace OHOS
263 #endif // MOCK_RUNTIME_H
264