1 /* 2 * Copyright (c) 2022 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_JS_NATIVE_ENGINE_H 17 #define MOCK_JS_NATIVE_ENGINE_H 18 19 #include "gmock/gmock.h" 20 #include "native_engine/native_engine.h" 21 22 class MockJsNativeEngine : public NativeEngine { 23 public: MockJsNativeEngine()24 MockJsNativeEngine() : NativeEngine(nullptr) 25 {} 26 ~MockJsNativeEngine()27 virtual ~MockJsNativeEngine() 28 {} 29 GetGlobal()30 NativeValue* GetGlobal() override 31 { 32 GTEST_LOG_(INFO) << "MockJsNativeEngine::GetGlobal called"; 33 return nullptr; 34 } 35 CreateNull()36 NativeValue* CreateNull() override 37 { 38 return nullptr; 39 } 40 CreateUndefined()41 NativeValue* CreateUndefined() override 42 { 43 return nullptr; 44 } 45 CreateBoolean(bool value)46 NativeValue* CreateBoolean(bool value) override 47 { 48 return nullptr; 49 } 50 CreateNumber(int32_t value)51 NativeValue* CreateNumber(int32_t value) override 52 { 53 return nullptr; 54 } 55 CreateNumber(uint32_t value)56 NativeValue* CreateNumber(uint32_t value) override 57 { 58 return nullptr; 59 } 60 CreateNumber(int64_t value)61 NativeValue* CreateNumber(int64_t value) override 62 { 63 return nullptr; 64 } 65 CreateNumber(double value)66 NativeValue* CreateNumber(double value) override 67 { 68 return nullptr; 69 } 70 CreateBigInt(int64_t value)71 NativeValue* CreateBigInt(int64_t value) override 72 { 73 return nullptr; 74 } 75 CreateBigInt(uint64_t value)76 NativeValue* CreateBigInt(uint64_t value) override 77 { 78 return nullptr; 79 } 80 CreateString(const char * value,size_t length)81 NativeValue* CreateString(const char* value, size_t length) override 82 { 83 GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateString called"; 84 return nullptr; 85 } 86 CreateString16(const char16_t * value,size_t length)87 NativeValue* CreateString16(const char16_t* value, size_t length) override 88 { 89 return nullptr; 90 } 91 CreateSymbol(NativeValue * value)92 NativeValue* CreateSymbol(NativeValue* value) override 93 { 94 return nullptr; 95 } 96 97 NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint, size_t nativeBindingSize = 0) override 98 { 99 return nullptr; 100 } 101 CreateObject()102 NativeValue* CreateObject() override 103 { 104 return nullptr; 105 } 106 CreateFunction(const char * name,size_t length,NativeCallback cb,void * value)107 NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override 108 { 109 return nullptr; 110 } 111 CreateArray(size_t length)112 NativeValue* CreateArray(size_t length) override 113 { 114 return nullptr; 115 } 116 CreateBuffer(void ** value,size_t length)117 NativeValue* CreateBuffer(void** value, size_t length) override 118 { 119 return nullptr; 120 } 121 CreateBufferCopy(void ** value,size_t length,const void * data)122 NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override 123 { 124 return nullptr; 125 } 126 CreateBufferExternal(void * value,size_t length,NativeFinalize cb,void * hint)127 NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override 128 { 129 return nullptr; 130 } 131 CreateArrayBuffer(void ** value,size_t length)132 NativeValue* CreateArrayBuffer(void** value, size_t length) override 133 { 134 return nullptr; 135 } 136 CreateArrayBufferExternal(void * value,size_t length,NativeFinalize cb,void * hint)137 NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override 138 { 139 return nullptr; 140 } 141 CreateTypedArray(NativeTypedArrayType type,NativeValue * value,size_t length,size_t offset)142 NativeValue* CreateTypedArray(NativeTypedArrayType type, NativeValue* value, size_t length, size_t offset) override 143 { 144 return nullptr; 145 } 146 CreateDataView(NativeValue * value,size_t length,size_t offset)147 NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override 148 { 149 return nullptr; 150 } 151 CreatePromise(NativeDeferred ** deferred)152 NativeValue* CreatePromise(NativeDeferred** deferred) override 153 { 154 return nullptr; 155 } 156 SetPromiseRejectCallback(NativeReference * rejectCallbackRef,NativeReference * checkCallbackRef)157 void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override 158 {} 159 160 MOCK_METHOD2(CreateError, NativeValue* (NativeValue*, NativeValue*)); 161 InitTaskPoolThread(NativeEngine * engine,NapiConcurrentCallback callback)162 bool InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback) override 163 { 164 return false; 165 } InitTaskPoolFunc(NativeEngine * engine,NativeValue * func,void * taskInfo)166 bool InitTaskPoolFunc(NativeEngine* engine, NativeValue* func, void* taskInfo) override 167 { 168 return false; 169 } 170 GetCurrentTaskInfo()171 void* GetCurrentTaskInfo() const override 172 { 173 return nullptr; 174 } 175 CallFunction(NativeValue * thisVar,NativeValue * function,NativeValue * const * argv,size_t argc)176 NativeValue* CallFunction( 177 NativeValue* thisVar, NativeValue* function, NativeValue* const* argv, size_t argc) override 178 { 179 GTEST_LOG_(INFO) << "MockJsNativeEngine::CallFunction called"; 180 return nullptr; 181 } 182 RunScript(NativeValue * script)183 NativeValue* RunScript(NativeValue* script) override 184 { 185 return nullptr; 186 } 187 RunScriptPath(const char * path)188 NativeValue* RunScriptPath(const char* path) override 189 { 190 return nullptr; 191 } 192 RunScriptBuffer(const char * path,std::vector<uint8_t> & buffer,bool isBundle)193 NativeValue* RunScriptBuffer(const char* path, std::vector<uint8_t>& buffer, bool isBundle) override 194 { 195 return nullptr; 196 } 197 RunBufferScript(std::vector<uint8_t> & buffer)198 NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override 199 { 200 return nullptr; 201 } 202 RunActor(std::vector<uint8_t> & buffer,const char * descriptor)203 NativeValue* RunActor(std::vector<uint8_t>& buffer, const char* descriptor) override 204 { 205 return nullptr; 206 } 207 DefineClass(const char * name,NativeCallback callback,void * data,const NativePropertyDescriptor * properties,size_t length)208 NativeValue* DefineClass(const char* name, NativeCallback callback, void* data, 209 const NativePropertyDescriptor* properties, size_t length) override 210 { 211 return nullptr; 212 } 213 CreateInstance(NativeValue * constructor,NativeValue * const * argv,size_t argc)214 NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override 215 { 216 GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateInstance called"; 217 return nullptr; 218 } 219 220 NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, NativeFinalize callback = nullptr, 221 void* data = nullptr, void* hint = nullptr) override 222 { 223 GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateReference called"; 224 return nullptr; 225 } 226 227 MOCK_METHOD1(Throw, bool(NativeValue* error)); 228 Throw(NativeErrorType type,const char * code,const char * message)229 bool Throw(NativeErrorType type, const char* code, const char* message) override 230 { 231 return true; 232 } 233 CreateRuntime()234 void* CreateRuntime() override 235 { 236 return nullptr; 237 } 238 Serialize(NativeEngine * context,NativeValue * value,NativeValue * transfer)239 NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override 240 { 241 return nullptr; 242 } 243 Deserialize(NativeEngine * context,NativeValue * recorder)244 NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override 245 { 246 return nullptr; 247 } 248 DeleteSerializationData(NativeValue * value)249 void DeleteSerializationData(NativeValue* value) const override 250 {} 251 LoadModule(NativeValue * str,const std::string & fileName)252 NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override 253 { 254 return nullptr; 255 } 256 257 void StartCpuProfiler(const std::string& fileName = "") override 258 {} 259 StopCpuProfiler()260 void StopCpuProfiler() override 261 {} 262 ResumeVM()263 void ResumeVM() override 264 {} 265 ValueToNativeValue(JSValueWrapper & value)266 NativeValue* ValueToNativeValue(JSValueWrapper& value) override 267 { 268 return nullptr; 269 } 270 CreateDate(double value)271 NativeValue* CreateDate(double value) override 272 { 273 return nullptr; 274 } 275 CreateBigWords(int sign_bit,size_t word_count,const uint64_t * words)276 NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override 277 { 278 return nullptr; 279 } 280 SuspendVM()281 bool SuspendVM() override 282 { 283 return true; 284 } 285 IsSuspended()286 bool IsSuspended() override 287 { 288 return true; 289 } 290 CheckSafepoint()291 bool CheckSafepoint() override 292 { 293 return true; 294 } 295 BuildNativeAndJsStackTrace(std::string & stackTraceStr)296 bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override 297 { 298 return true; 299 } 300 BuildJsStackTrace(std::string & stackTraceStr)301 bool BuildJsStackTrace(std::string& stackTraceStr) override 302 { 303 return true; 304 } 305 BuildJsStackInfoList(uint32_t tid,std::vector<JsFrameInfo> & jsFrames)306 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames) override 307 { 308 GTEST_LOG_(INFO) << "MockJsNativeEngine::BuildJsStackInfoList called"; 309 return true; 310 } 311 DeleteWorker(NativeEngine * workerEngine)312 bool DeleteWorker(NativeEngine* workerEngine) override 313 { 314 return true; 315 } 316 SuspendVMById(uint32_t tid)317 bool SuspendVMById(uint32_t tid) override 318 { 319 return true; 320 } 321 ResumeVMById(uint32_t tid)322 void ResumeVMById(uint32_t tid) override 323 {} 324 325 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override 326 { 327 return true; 328 } 329 StopHeapTracking(const std::string & filePath)330 bool StopHeapTracking(const std::string& filePath) override 331 { 332 return true; 333 } 334 IsExceptionPending()335 bool IsExceptionPending() const override 336 { 337 return false; 338 } 339 GetAndClearLastException()340 NativeValue* GetAndClearLastException() override 341 { 342 return nullptr; 343 } 344 TriggerFatalException(NativeValue * error)345 bool TriggerFatalException(NativeValue* error) override 346 { 347 return true; 348 } 349 AdjustExternalMemory(int64_t ChangeInBytes,int64_t * AdjustedValue)350 bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override 351 { 352 return true; 353 } 354 PrintStatisticResult()355 void PrintStatisticResult() override 356 {} 357 StartRuntimeStat()358 void StartRuntimeStat() override 359 {} 360 StopRuntimeStat()361 void StopRuntimeStat() override 362 {} 363 NotifyApplicationState(bool inBackground)364 void NotifyApplicationState(bool inBackground) override 365 { 366 GTEST_LOG_(INFO) << "MockJsNativeEngine::NotifyApplicationState called"; 367 } 368 NotifyIdleStatusControl(std::function<void (bool)> callback)369 void NotifyIdleStatusControl(std::function<void(bool)> callback) override 370 {} 371 NotifyIdleTime(int idleMicroSec)372 void NotifyIdleTime(int idleMicroSec) override 373 {} 374 375 void NotifyMemoryPressure(bool inHighMemoryPressure = false) override 376 {} 377 RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback)378 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override 379 {} 380 HandleUncaughtException()381 void HandleUncaughtException() override 382 {} 383 RegisterPermissionCheck(PermissionCheckCallback callback)384 void RegisterPermissionCheck(PermissionCheckCallback callback) override 385 {} 386 ExecutePermissionCheck()387 bool ExecutePermissionCheck() override 388 { 389 return true; 390 } 391 RegisterTranslateBySourceMap(SourceMapCallback callback)392 void RegisterTranslateBySourceMap(SourceMapCallback callback) override 393 {} 394 ExecuteTranslateBySourceMap(const std::string & rawStack)395 std::string ExecuteTranslateBySourceMap(const std::string& rawStack) override 396 { 397 return ""; 398 } 399 400 void DumpHeapSnapshot( 401 bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON, bool isPrivate = false) override 402 { 403 GTEST_LOG_(INFO) << "MockJsNativeEngine::DumpHeapSnapshot called"; 404 } 405 406 void DumpHeapSnapshot( 407 const std::string& path, bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON) override 408 {} 409 GetArrayBufferSize()410 size_t GetArrayBufferSize() override 411 { 412 return 0; 413 } 414 GetHeapTotalSize()415 size_t GetHeapTotalSize() override 416 { 417 return 0; 418 } 419 GetHeapUsedSize()420 size_t GetHeapUsedSize() override 421 { 422 return 0; 423 } GetScopeManager()424 NativeScopeManager* GetScopeManager() 425 { 426 GTEST_LOG_(INFO) << "MockJsNativeEngine::GetScopeManager called"; 427 return nullptr; 428 } 429 AllowCrossThreadExecution()430 void AllowCrossThreadExecution() const override 431 {} 432 }; 433 434 #endif /* MOCK_JS_NATIVE_ENGINE_H */ 435