1 /*
2 * Copyright (c) 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 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include "preferences_helper.h"
19
20 namespace OHOS {
21 namespace NativePreferences {
22
23 class PreferencesHelperMock : public Preferences {
24 public:
25 MOCK_METHOD2(Get, PreferencesValue(const std::string &key, const PreferencesValue &defValue));
26 MOCK_METHOD2(Put, int(const std::string &key, const PreferencesValue &value));
27 MOCK_METHOD2(GetInt, int(const std::string &key, const int &defValue));
28 MOCK_METHOD2(GetString, std::string(const std::string &key, const std::string &defValue));
29 MOCK_METHOD2(GetBool, bool(const std::string &key, const bool &defValue));
30 MOCK_METHOD2(GetFloat, float(const std::string &key, const float &defValue));
31 MOCK_METHOD2(GetDouble, double(const std::string &key, const double &defValue));
32 MOCK_METHOD2(GetLong, int64_t(const std::string &key, const int64_t &defValue));
33 MOCK_METHOD0(GetAll, std::map<std::string, PreferencesValue>());
34 MOCK_METHOD1(HasKey, bool(const std::string &key));
35 MOCK_METHOD2(PutInt, int(const std::string &key, int value));
36 MOCK_METHOD2(PutString, int(const std::string &key, const std::string &value));
37 MOCK_METHOD2(PutBool, int(const std::string &key, bool value));
38 MOCK_METHOD2(PutLong, int(const std::string &key, int64_t value));
39 MOCK_METHOD2(PutFloat, int(const std::string &key, float value));
40 MOCK_METHOD2(PutDouble, int(const std::string &key, double value));
41 MOCK_METHOD1(Delete, int(const std::string &key));
42 MOCK_METHOD0(Clear, int());
43 MOCK_METHOD0(Flush, void());
44 MOCK_METHOD0(FlushSync, int());
45 MOCK_METHOD2(RegisterObserver, int(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode));
46 MOCK_METHOD2(UnRegisterObserver, int(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode));
47 };
48
GetRealPath(const std::string & path,int & errorCode)49 std::string PreferencesHelper::GetRealPath(const std::string &path, int &errorCode)
50 {
51 return "";
52 }
53
GetPreferences(const Options & options,int & errCode)54 std::shared_ptr<Preferences> PreferencesHelper::GetPreferences(const Options &options, int &errCode)
55 {
56 if (options.filePath == "") {
57 return nullptr;
58 }
59 std::shared_ptr<PreferencesHelperMock> pref = std::make_shared<PreferencesHelperMock>();
60 return pref;
61 }
62
DeletePreferences(const std::string & path)63 int PreferencesHelper::DeletePreferences(const std::string &path)
64 {
65 return 0;
66 }
67
RemovePreferencesFromCache(const std::string & path)68 int PreferencesHelper::RemovePreferencesFromCache(const std::string &path)
69 {
70 return 0;
71 }
72 } // End of namespace NativePreferences
73 } // End of namespace OHOS