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 #ifndef INTERFACES_TEST_UNITTEST_CLASS_ATOMICFILE_MOCK_ATOMICFILE_MOCK_H 17 #define INTERFACES_TEST_UNITTEST_CLASS_ATOMICFILE_MOCK_ATOMICFILE_MOCK_H 18 19 #include "n_napi.h" 20 #include "atomicfile_entity.h" 21 #include "atomicfile_n_exporter.h" 22 #include "filemgmt_libn.h" 23 24 #include <cstdio> 25 #include <filesystem> 26 #include <gmock/gmock.h> 27 28 namespace OHOS::FileManagement::ModuleFileIO::Test { 29 using namespace std::filesystem; 30 using namespace OHOS::FileManagement::ModuleFileIO; 31 using namespace OHOS::FileManagement::LibN; 32 33 class IAtomicfileMock { 34 public: 35 virtual ~IAtomicfileMock() = default; 36 virtual int rename(const char *old_filename, const char *new_filename) = 0; 37 virtual int remove(const char *filename) = 0; 38 virtual napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) = 0; 39 virtual napi_status napi_delete_reference(napi_env env, napi_ref ref) = 0; 40 virtual bool InitArgs(std::function<bool()> argcChecker) = 0; 41 virtual napi_value GetThisVar() = 0; 42 virtual napi_status napi_unwrap(napi_env env, napi_value js_object, void **result) = 0; 43 virtual napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype *result) = 0; 44 virtual void ThrowErr(napi_env env, std::string errMsg) = 0; 45 }; 46 47 class AtomicfileMock : public IAtomicfileMock { 48 public: 49 MOCK_METHOD(int, rename, (const char *, const char *), (override)); 50 MOCK_METHOD(int, remove, (const char *), (override)); 51 MOCK_METHOD(napi_status, napi_get_reference_value, (napi_env, napi_ref, napi_value *), (override)); 52 MOCK_METHOD(napi_status, napi_delete_reference, (napi_env, napi_ref), (override)); 53 MOCK_METHOD(bool, InitArgs, (std::function<bool()>), (override)); 54 MOCK_METHOD(napi_value, GetThisVar, (), (override)); 55 MOCK_METHOD(napi_status, napi_unwrap, (napi_env, napi_value, void **), (override)); 56 MOCK_METHOD(napi_status, napi_typeof, (napi_env, napi_value, napi_valuetype *), (override)); 57 MOCK_METHOD(void, ThrowErr, (napi_env, std::string), (override)); 58 59 public: 60 static std::shared_ptr<AtomicfileMock> GetMock(); 61 static void EnableMock(); 62 static void DisableMock(); 63 static bool IsMockable(); 64 65 private: 66 static thread_local std::shared_ptr<AtomicfileMock> atomicfileMock; 67 static thread_local bool mockable; 68 }; 69 70 } // namespace OHOS::FileManagement::ModuleFileIO::Test 71 #endif // INTERFACES_TEST_UNITTEST_CLASS_ATOMICFILE_MOCK_ATOMICFILE_MOCK_H