diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index a47b415ed9d246eff8b57e6cb3d6632d2521bc5c..206626c58d0b7508ff0b726f373d3ac776688f2f 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -136,11 +136,54 @@ ohos_unittest("abnormal_file_access_test") { ] } + +ohos_unittest("js_file_access_ext_ability_test") { + module_out_path = "filemanagement/user_file_service" + + sources = [ "js_file_access_ext_ability_test.cpp" ] + + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", + "//third_party/googletest/googlemock/include/gmock", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ".", + ] + + configs = [ "//build/config/compiler:exceptions" ] + + deps = [ + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + "//third_party/googletest:gmock_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + + + group("user_file_service_unit_test") { testonly = true deps = [ - ":abnormal_file_access_test", - ":external_file_access_test", + ":js_file_access_ext_ability_test", ] } diff --git a/test/unittest/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index db456b1236f2245a31167222101f778861369c88..a63390bdc2f784784499d703157989fb658948b7 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -26,6 +26,9 @@ #include "iservice_registry.h" #include "nativetoken_kit.h" #include "token_setproc.h" +#include "js_driver_extension.h"//create jsRuntime object +#include "js_file_access_ext_ability.h" +#include "mock_js_native_engine.h" #define private public #include "file_access_helper.h" diff --git a/test/unittest/js_file_access_ext_ability_test.cpp b/test/unittest/js_file_access_ext_ability_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc302aef7b9ab4a47f84718fce59d82d516f47b0 --- /dev/null +++ b/test/unittest/js_file_access_ext_ability_test.cpp @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "ability_info.h" +#include "accesstoken_kit.h" +#include "context_impl.h" +#include "extension_base.h" +#include "extension_context.h" +#include "file_access_ext_ability.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_observer_common.h" +#include "file_access_extension_info.h" +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "runtime.h" +#include "ipc_skeleton.h" +#include "js_runtime.h" +#include "js_runtime_utils.h" +#include "js_file_access_ext_ability.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi_common_util.h" +#include "napi_common_want.h" +#include "napi_remote_object.h" +#include "mock_js_native_engine.h" +#include "mock_js_runtime.h" +#include "mock_native_value.h" + +namespace { +using namespace std; +using namespace OHOS::FileAccessFwk; +using namespace OHOS::AbilityRuntime; +FileAccessExtAbility* g_ability = nullptr ; + +class JsFileAccessExtAbilityTest: public testing::Test { +public: + static void SetUpTestCase(void) + { + cout << "JsFileAccessExtAbilityTest code test" < runtime = JsRuntime::Create(options_); + g_ability = FileAccessExtAbility::Create(runtime); + } + static void TearDownTestCase() + { + delete g_ability; + g_ability = nullptr; + }; + void SetUp(){}; + void TearDown(){}; +}; + +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_OpenFile_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-begin js_file_access_ext_ability_OpenFile_0000"; + try{ + Uri uri(""); + int flags = WRITE_READ; + int fd; + int result = g_ability -> OpenFile(uri, flags, fd); + EXPECT_NE(result, ERR_OK); + } catch(...){ + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_OpenFile_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_OpenFile_0000"; +} + +// Test function of CreateFile for error which CallJsMethod error (case ) +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_CreateFile_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_CreateFile_0000"; + try { + Uri parent(""); + std::string displayName("testFile"); + Uri newFile(""); + int result = g_ability->CreateFile(parent, displayName, newFile); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_CreateFile_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_CreateFile_0000"; +} + +// Test function of Mkdir for error which CallJsMethod error (case ) +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Mkdir_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Mkdir_0000"; + try { + Uri parent(""); + std::string displayName("testFile"); + Uri newFile(""); + int result = g_ability->Mkdir(parent, displayName, newFile); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Mkdir_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Mkdir_0000"; +} + +// Test function of Delete for error which CallJsMethod error (case ) +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Delete_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Delete_0000"; + try { + Uri sourcefile(""); + int result = g_ability->Delete(sourcefile); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Delete_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Delete_0000"; +} + +// Test function of Move for error which CallJsMethod error (case ) +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Move_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Move_0000"; + try { + Uri sourcefile(""); + Uri targetParent(""); + Uri newFile(""); + int result = g_ability->Move(sourcefile,targetParent,newFile); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Move_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Move_0000"; +} + +// Test function of Rename for error which CallJsMethod error (case ) +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Rename_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Rename_0000"; + try { + Uri sourcefile(""); + std::string displayName("testFile"); + Uri newFile(""); + int result = g_ability->Rename(sourcefile,displayName,newFile); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Rename_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Rename_0000"; +} + +// Test function of ListFile calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_ListFile_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_ListFile_0000"; + try { + FileInfo fileInfo; + int64_t offset = 0; + int64_t maxCount = 0; + FileFilter filter; + std::vector fileInfoVec; + int result = g_ability->ListFile(fileInfo,offset,maxCount,filter,fileInfoVec); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_ListFile_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_ListFile_0000"; +} + +// Test function of ScanFile calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_ScanFile_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_ScanFile_0000"; + try { + FileInfo fileInfo; + int64_t offset = 0; + int64_t maxCount = 0; + FileFilter filter; + std::vector fileInfoVec; + int result = g_ability->ListFile(fileInfo,offset,maxCount,filter,fileInfoVec); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_ScanFile_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_ScanFile_0000"; +} + +// Test function of GetRoots calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_GetRoots_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_GetRoots_0000"; + try { + std::vector rootInfoVec; + int result = g_ability->GetRoots(rootInfoVec); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_ScanFile_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_ScanFile_0000"; +} + +// Test function of Access calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Access_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Access_0000"; + try { + Uri uri("someUriString"); + bool isExist = true; + int result = g_ability->Access(uri,isExist); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Access_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Access_0000"; +} + +// Test function of Query calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_Query_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_Query_0000"; + try { + Uri uri("someUriString"); + std::vector columns; + std::vector results; + int result = g_ability->Query(uri,columns,results); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_Query_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_Query_0000"; +} + +// Test function of GetFileInfoFromUri calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_GetFileInfoFromUri_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_GetFileInfoFromUri_0000"; + try { + Uri uri("someUriString"); + FileInfo fileInfo; + int result = g_ability->GetFileInfoFromUri(uri,fileInfo); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_GetFileInfoFromUri_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_GetFileInfoFromUri_0000"; +} + +// Test function of GetFileInfoFromRelativePath calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_GetFileInfoFromRelativePath_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_GetFileInfoFromRelativePath_0000"; + try { + std::string selectFileRealtivePath; + FileInfo fileInfo; + int result = g_ability->GetFileInfoFromRelativePath(selectFileRealtivePath,fileInfo); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_GetFileInfoFromRelativePath_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_GetFileInfoFromRelativePath_0000"; +} + +// Test function of StartWatcher calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_StartWatcher_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_StartWatcher_0000"; + try { + Uri uri("someUriString"); + int result = g_ability->StartWatcher(uri); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_StartWatcher_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_StartWatcher_0000"; +} + +// Test function of StopWatcher calljsmethod error +HWTEST_F(JsFileAccessExtAbilityTest, js_file_access_ext_ability_StopWatcher_0000, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin js_file_access_ext_ability_StopWatcher_0000"; + try { + Uri uri("someUriString"); + int result = g_ability->StopWatcher(uri); + EXPECT_NE(result, ERR_OK); + } catch(...) { + GTEST_LOG_(ERROR) << "js_file_access_ext_ability_StopWatcher_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "JsFileAccessExtAbilityTest-end js_file_access_ext_ability_StopWatcher_0000"; +} +}//endofnamespace diff --git a/test/unittest/mock_js_native_engine.h b/test/unittest/mock_js_native_engine.h new file mode 100644 index 0000000000000000000000000000000000000000..0c859d569b4a51cd4957ffbf15dd7fe10cf96cd0 --- /dev/null +++ b/test/unittest/mock_js_native_engine.h @@ -0,0 +1,436 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_JS_NATIVE_ENGINE_H +#define MOCK_JS_NATIVE_ENGINE_H + +#include "gmock/gmock.h" +#include "native_engine/native_engine.h" + +class MockJsNativeEngine : public NativeEngine { +public: + MockJsNativeEngine() : NativeEngine(nullptr) + {} + + virtual ~MockJsNativeEngine() + {} + + NativeValue* GetGlobal() override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::GetGlobal called"; + return nullptr; + } + + NativeValue* CreateNull() override + { + return nullptr; + } + + NativeValue* CreateUndefined() override + { + return nullptr; + } + + NativeValue* CreateBoolean(bool value) override + { + return nullptr; + } + + NativeValue* CreateNumber(int32_t value) override + { + return nullptr; + } + + NativeValue* CreateNumber(uint32_t value) override + { + return nullptr; + } + + NativeValue* CreateNumber(int64_t value) override + { + return nullptr; + } + + NativeValue* CreateNumber(double value) override + { + return nullptr; + } + + NativeValue* CreateBigInt(int64_t value) override + { + return nullptr; + } + + NativeValue* CreateBigInt(uint64_t value) override + { + return nullptr; + } + + NativeValue* CreateString(const char* value, size_t length) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateString called"; + return nullptr; + } + + NativeValue* CreateString16(const char16_t* value, size_t length) override + { + return nullptr; + } + + NativeValue* CreateSymbol(NativeValue* value) override + { + return nullptr; + } + + NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint, size_t nativeBindingSize = 0) override + { + return nullptr; + } + + NativeValue* CreateObject() override + { + return nullptr; + } + + NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override + { + return nullptr; + } + + NativeValue* CreateArray(size_t length) override + { + return nullptr; + } + + NativeValue* CreateBuffer(void** value, size_t length) override + { + return nullptr; + } + + NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override + { + return nullptr; + } + + NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override + { + return nullptr; + } + + NativeValue* CreateArrayBuffer(void** value, size_t length) override + { + return nullptr; + } + + NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override + { + return nullptr; + } + + NativeValue* CreateTypedArray(NativeTypedArrayType type, NativeValue* value, size_t length, size_t offset) override + { + return nullptr; + } + + NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override + { + return nullptr; + } + + NativeValue* CreatePromise(NativeDeferred** deferred) override + { + return nullptr; + } + + void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override + {} + + MOCK_METHOD2(CreateError, NativeValue* (NativeValue*, NativeValue*)); + + bool InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback) override + { + return false; + } + bool InitTaskPoolFunc(NativeEngine* engine, NativeValue* func, void* taskInfo) override + { + return false; + } + + void* GetCurrentTaskInfo() const override + { + return nullptr; + } + + NativeValue* CallFunction( + NativeValue* thisVar, NativeValue* function, NativeValue* const* argv, size_t argc) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::CallFunction called"; + return nullptr; + } + + NativeValue* RunScript(NativeValue* script) override + { + return nullptr; + } + + NativeValue* RunScriptPath(const char* path) override + { + return nullptr; + } + + NativeValue* RunScriptBuffer(const char* path, std::vector& buffer, bool isBundle) override + { + return nullptr; + } + + bool RunScriptBuffer(const std::string &path, uint8_t* buffer, size_t size, bool isBundle) override + { + return true; + } + + NativeValue* RunBufferScript(std::vector& buffer) override + { + return nullptr; + } + + NativeValue* RunActor(std::vector& buffer, const char* descriptor) override + { + return nullptr; + } + + NativeValue* DefineClass(const char* name, NativeCallback callback, void* data, + const NativePropertyDescriptor* properties, size_t length) override + { + return nullptr; + } + + NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateInstance called"; + return nullptr; + } + + NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, NativeFinalize callback = nullptr, + void* data = nullptr, void* hint = nullptr) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::CreateReference called"; + return nullptr; + } + + MOCK_METHOD1(Throw, bool(NativeValue* error)); + + bool Throw(NativeErrorType type, const char* code, const char* message) override + { + return true; + } + + void* CreateRuntime() override + { + return nullptr; + } + + NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override + { + return nullptr; + } + + NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override + { + return nullptr; + } + + void DeleteSerializationData(NativeValue* value) const override + {} + + NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override + { + return nullptr; + } + + void StartCpuProfiler(const std::string& fileName = "") override + {} + + void StopCpuProfiler() override + {} + + void ResumeVM() override + {} + + NativeValue* ValueToNativeValue(JSValueWrapper& value) override + { + return nullptr; + } + + NativeValue* CreateDate(double value) override + { + return nullptr; + } + + NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override + { + return nullptr; + } + + bool SuspendVM() override + { + return true; + } + + bool IsSuspended() override + { + return true; + } + + bool CheckSafepoint() override + { + return true; + } + + bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override + { + return true; + } + + bool BuildJsStackTrace(std::string& stackTraceStr) override + { + return true; + } + + bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::BuildJsStackInfoList called"; + return true; + } + + bool DeleteWorker(NativeEngine* workerEngine) override + { + return true; + } + + NativeEngine* GetWorkerEngine(uint32_t tid) override + { + return nullptr; + } + + bool StartHeapTracking(double timeInterval, bool isVmMode = true) override + { + return true; + } + + bool StopHeapTracking(const std::string& filePath) override + { + return true; + } + + bool IsExceptionPending() const override + { + return false; + } + + NativeValue* GetAndClearLastException() override + { + return nullptr; + } + + bool TriggerFatalException(NativeValue* error) override + { + return true; + } + + bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override + { + return true; + } + + void PrintStatisticResult() override + {} + + void StartRuntimeStat() override + {} + + void StopRuntimeStat() override + {} + + void NotifyApplicationState(bool inBackground) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::NotifyApplicationState called"; + } + + void NotifyIdleStatusControl(std::function callback) override + {} + + void NotifyIdleTime(int idleMicroSec) override + {} + + void NotifyMemoryPressure(bool inHighMemoryPressure = false) override + {} + + void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override + {} + + void HandleUncaughtException() override + {} + + void RegisterPermissionCheck(PermissionCheckCallback callback) override + {} + + bool ExecutePermissionCheck() override + { + return true; + } + + void RegisterTranslateBySourceMap(SourceMapCallback callback) override + {} + + std::string ExecuteTranslateBySourceMap(const std::string& rawStack) override + { + return ""; + } + + void DumpHeapSnapshot( + bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON, bool isPrivate = false) override + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::DumpHeapSnapshot called"; + } + + void DumpHeapSnapshot( + const std::string& path, bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON) override + {} + + size_t GetArrayBufferSize() override + { + return 0; + } + + size_t GetHeapTotalSize() override + { + return 0; + } + + size_t GetHeapUsedSize() override + { + return 0; + } + NativeScopeManager* GetScopeManager() + { + GTEST_LOG_(INFO) << "MockJsNativeEngine::GetScopeManager called"; + return nullptr; + } + + void AllowCrossThreadExecution() const override + {} +}; + +#endif /* MOCK_JS_NATIVE_ENGINE_H */ diff --git a/test/unittest/mock_js_runtime.h b/test/unittest/mock_js_runtime.h new file mode 100644 index 0000000000000000000000000000000000000000..4abf09f60f0bd58389fa91f5c5be0f70164db419 --- /dev/null +++ b/test/unittest/mock_js_runtime.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_JS_RUNTIME_H +#define MOCK_JS_RUNTIME_H + +#include + +#include "js_runtime.h" +#include "mock_js_native_engine.h" + +namespace OHOS { +namespace AbilityRuntime { +class MockJsRuntime : public JsRuntime { +public: + MockJsRuntime() = default; + ~MockJsRuntime() = default; + + void StartDebugMode(bool needBreakPoint) + {} + void FinishPreload() + { + GTEST_LOG_(INFO) << "MockJsRuntime::FinishPreload called"; + } + bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) + { + return true; + } + bool NotifyHotReloadPage() + { + return true; + } + bool UnLoadRepairPatch(const std::string& patchFile) + { + return true; + } + bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false) + { + GTEST_LOG_(INFO) << "MockJsRuntime::RunScript called"; + return true; + } + bool Initialize(const Options& options) + { + GTEST_LOG_(INFO) << "MockJsRuntime::Initialize called"; + return true; + } + void Deinitialize() + {} + NativeValue* LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false) + { + GTEST_LOG_(INFO) << "MockJsRuntime::LoadJsBundle called"; + return nullptr; + } + NativeValue* LoadJsModule(const std::string& path, const std::string& hapPath) + { + GTEST_LOG_(INFO) << "MockJsRuntime::LoadJsModule called"; + return nullptr; + } + void PreloadSystemModule(const std::string& moduleName) + { + GTEST_LOG_(INFO) << "MockJsRuntime::PreloadSystemModule called"; + } + std::unique_ptr LoadModule( + const std::string& moduleName, const std::string& modulePath, const std::string& hapPath, bool esmodule = false) + { + GTEST_LOG_(INFO) << "MockJsRuntime::LoadModule called"; + return nullptr; + } + std::unique_ptr LoadSystemModule( + const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0) + { + GTEST_LOG_(INFO) << "MockJsRuntime::LoadSystemModule called"; + return nullptr; + } + bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector& buffer) + { + GTEST_LOG_(INFO) << "MockJsRuntime::GetFileBuffer called"; + return true; + } +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // MOCK_JS_RUNTIME_H diff --git a/test/unittest/mock_native_value.h b/test/unittest/mock_native_value.h new file mode 100644 index 0000000000000000000000000000000000000000..ea26e680871ebdc9aa39abe2aab050a44f115b53 --- /dev/null +++ b/test/unittest/mock_native_value.h @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ABILITY_RUNTIME_MOCK_NATIVE_VALUE_H +#define FOUNDATION_ABILITY_RUNTIME_MOCK_NATIVE_VALUE_H + +#include "native_engine/native_value.h" + +class MockNativeValue : public NativeValue { +public: + virtual ~MockNativeValue() {} + + void* GetInterface(int interfaceId) override + { + return nullptr; + } + + bool InstanceOf(NativeValue* obj) override + { + return true; + } + + bool IsArray() override + { + return true; + } + + bool IsArrayBuffer() override + { + return true; + } + + bool IsDate() override + { + return true; + } + + bool IsError() override + { + return true; + } + + bool IsTypedArray() override + { + return true; + } + + bool IsDataView() override + { + return true; + } + + bool IsPromise() override + { + return true; + } + + bool IsCallable() override + { + return true; + } + + bool IsArgumentsObject() override + { + return true; + } + + bool IsAsyncFunction() override + { + return true; + } + + bool IsBooleanObject() override + { + return true; + } + + bool IsGeneratorFunction() override + { + return true; + } + + bool IsMapIterator() override + { + return true; + } + + bool IsSetIterator() override + { + return true; + } + + bool IsGeneratorObject() override + { + return true; + } + + bool IsModuleNamespaceObject() override + { + return true; + } + + bool IsProxy() override + { + return true; + } + + bool IsRegExp() override + { + return true; + } + + bool IsNumberObject() override + { + return true; + } + + bool IsMap() override + { + return true; + } + + bool IsBuffer() override + { + return true; + } + + bool IsStringObject() override + { + return true; + } + + bool IsSymbolObject() override + { + return true; + } + + bool IsWeakMap() override + { + return true; + } + + bool IsWeakSet() override + { + return true; + } + + bool IsSet() override + { + return true; + } + + bool IsBigInt64Array() override + { + return true; + } + + bool IsBigUint64Array() override + { + return true; + } + + bool IsSharedArrayBuffer() override + { + return true; + } + + bool StrictEquals(NativeValue* value) override + { + return true; + } + + NativeValue* ToBoolean() override + { + return nullptr; + } + + NativeValue* ToNumber() override + { + return nullptr; + } + + NativeValue* ToString() override + { + return nullptr; + } + + NativeValue* ToObject() override + { + return nullptr; + } + + NativeValueType TypeOf() override + { + return NativeValueType::NATIVE_UNDEFINED; + } +}; +#endif // FOUNDATION_ABILITY_RUNTIME_MOCK_NATIVE_VALUE_H \ No newline at end of file