1 /*
2 * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 FRAMEWORKS_CORE_COMMON_TEST_UNITTEST_STORAGE_MOCK_STORAGE_H
16 #define FRAMEWORKS_CORE_COMMON_TEST_UNITTEST_STORAGE_MOCK_STORAGE_H
17
18 #include "base/utils/utils.h"
19 #include "core/common/storage/storage_proxy.h"
20
21 namespace OHOS::Ace {
22 const std::string FALSE_TEST = "false";
23
24 class MockDistributedStorage final : public Storage {
25 DECLARE_ACE_TYPE(MockDistributedStorage, Storage);
26
27 public:
28 std::string sessionId_;
MockDistributedStorage(const std::string & sessionId,const RefPtr<TaskExecutor> & taskExecutor)29 explicit MockDistributedStorage(const std::string& sessionId, const RefPtr<TaskExecutor>& taskExecutor)
30 : Storage(), sessionId_(sessionId)
31 {}
32 ~MockDistributedStorage() override = default;
33
34 bool Init(std::function<void(const std::string&)>&& notifier);
35
SetString(const std::string & key,const std::string & value)36 void SetString(const std::string& key, const std::string& value) override {};
GetString(const std::string & key)37 std::string GetString(const std::string& key) override
38 {
39 return FALSE_TEST;
40 }
SetDouble(const std::string & key,const double value)41 void SetDouble(const std::string& key, const double value) override {};
GetDouble(const std::string & key,double & value)42 bool GetDouble(const std::string& key, double& value) override
43 {
44 return false;
45 }
SetBoolean(const std::string & key,const bool value)46 void SetBoolean(const std::string& key, const bool value) override {};
GetBoolean(const std::string & key,bool & value)47 bool GetBoolean(const std::string& key, bool& value) override
48 {
49 return false;
50 }
51
GetDataType(const std::string & key)52 DataType GetDataType(const std::string& key) override
53 {
54 Storage::DataType dataType = Storage::DataType::NONE;
55 return dataType;
56 }
Clear()57 void Clear() override {};
Delete(const std::string & key)58 void Delete(const std::string& key) override {};
59 };
60
61 class MockStorageImpl : public Storage {
62 DECLARE_ACE_TYPE(MockStorageImpl, Storage);
63
64 public:
65 MockStorageImpl();
66 ~MockStorageImpl() override = default;
SetString(const std::string & key,const std::string & value)67 void SetString(const std::string& key, const std::string& value) override {};
GetString(const std::string & key)68 std::string GetString(const std::string& key) override
69 {
70 return "false";
71 }
SetDouble(const std::string & key,const double value)72 void SetDouble(const std::string& key, const double value) override {};
GetDouble(const std::string & key,double & value)73 bool GetDouble(const std::string& key, double& value) override
74 {
75 return false;
76 }
SetBoolean(const std::string & key,const bool value)77 void SetBoolean(const std::string& key, const bool value) override {};
GetBoolean(const std::string & key,bool & value)78 bool GetBoolean(const std::string& key, bool& value) override
79 {
80 return false;
81 }
Clear()82 void Clear() override {};
Delete(const std::string & key)83 void Delete(const std::string& key) override {};
84 };
85
MockStorageImpl()86 MockStorageImpl::MockStorageImpl() : Storage() {}
87
88 class MockStorageProxyImpl final : public StorageInterface {
89 public:
90 MockStorageProxyImpl() = default;
91 ~MockStorageProxyImpl() override = default;
92
GetStorage()93 RefPtr<Storage> GetStorage() const override
94 {
95 return AceType::MakeRefPtr<MockStorageImpl>();
96 }
97 };
98
99 class MockDistributedStorageProxyImpl final : public StorageInterface {
100 public:
101 MockDistributedStorageProxyImpl() = default;
102 ~MockDistributedStorageProxyImpl() override = default;
103
GetStorage(const std::string & sessionId,std::function<void (const std::string &)> && notifier,const RefPtr<TaskExecutor> & taskExecutor)104 RefPtr<Storage> GetStorage(const std::string& sessionId, std::function<void(const std::string&)>&& notifier,
105 const RefPtr<TaskExecutor>& taskExecutor) const override
106 {
107 return AceType::MakeRefPtr<MockDistributedStorage>(sessionId, taskExecutor);
108 }
109 };
110 } // namespace OHOS::Ace
111 #endif