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 #ifndef CLOUD_PREFERENCES_HELPER_H 16 #define CLOUD_PREFERENCES_HELPER_H 17 #include "preferences.h" 18 #include <gmock/gmock.h> 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 23 namespace OHOS { 24 namespace NativePreferences { 25 class PreferenceHelperInterface { 26 public: 27 virtual ~PreferenceHelperInterface() = default; 28 virtual std::shared_ptr<Preferences> GetPreferences(const std::string &filePath, int &errCode) = 0; 29 virtual void DeletePreferences(const std::string &path) = 0; 30 }; 31 32 class MockPreferenceHelperInterface : public PreferenceHelperInterface { 33 public: 34 MockPreferenceHelperInterface() = default; 35 ~MockPreferenceHelperInterface() override = default; 36 MOCK_METHOD2(GetPreferences, std::shared_ptr<Preferences>(const std::string &filePath, int &errCode)); 37 MOCK_METHOD1(DeletePreferences, void(const std::string &path)); 38 }; 39 40 class PreferencesHelper { 41 public: 42 static std::shared_ptr<Preferences> GetPreferences(const std::string &filePath, int &errCode); 43 static void DeletePreferences(const std::string &path); 44 static std::shared_ptr<MockPreferenceHelperInterface> GetInstance(); 45 static void DeleteInstance(); 46 47 private: 48 static std::shared_ptr<MockPreferenceHelperInterface> instance_; 49 static std::mutex mtx_; 50 }; 51 } // namespace NativePreferences 52 } // namespace OHOS 53 #endif // CLOUD_PREFERENCES_HELPER_H