1/* 2 * Copyright (c) 2021 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 16import { paramMock } from "../utils" 17 18const ValueTypes = '[PC Preview] unknow value type' 19 20const StorageObserver = { 21 key: '[PC Preview] unknown key' 22}; 23const MAX_KEY_LENGTH = 80; 24 25const MAX_VALUE_LENGTH = 8192; 26 27export function mockDataStorage() { 28 const storage = { 29 storageMock, 30 getStorageSync: function () { 31 console.warn("data.storage.getStorageSync interface mocked in the Previewer. How this interface works on the" + 32 " Previewer may be different from that on a real device.") 33 return storageMock 34 }, 35 getStorage: function (...args) { 36 console.warn("data.storage.getStorage interface mocked in the Previewer. How this interface works on the" + 37 " Previewer may be different from that on a real device.") 38 const len = args.length 39 if (typeof args[len - 1] === 'function') { 40 args[len - 1].call(this, paramMock.businessErrorMock, storageMock) 41 } else { 42 return new Promise((resolve) => { 43 resolve(storageMock) 44 }) 45 } 46 }, 47 deleteStorageSync: function () { 48 console.warn("data.storage.deleteStorageSync interface mocked in the Previewer. How this interface works on" + 49 " the Previewer may be different from that on a real device.") 50 }, 51 deleteStorage: function (...args) { 52 console.warn("data.storage.deleteStorage interface mocked in the Previewer. How this interface works on the" + 53 " Previewer may be different from that on a real device.") 54 const len = args.length 55 if (typeof args[len - 1] === 'function') { 56 args[len - 1].call(this, paramMock.businessErrorMock) 57 } else { 58 return new Promise((resolve) => { 59 resolve() 60 }) 61 } 62 }, 63 removeStorageFromCacheSync: function () { 64 console.warn("data.storage.removeStorageFromCacheSync interface mocked in the Previewer. How this interface" + 65 " works on the Previewer may be different from that on a real device.") 66 }, 67 removeStorageFromCache: function (...args) { 68 console.warn("data.storage.removeStorageFromCache interface mocked in the Previewer. How this interface works" + 69 " on the Previewer may be different from that on a real device.") 70 const len = args.length 71 if (typeof args[len - 1] === 'function') { 72 args[len - 1].call(this, paramMock.businessErrorMock) 73 } else { 74 return new Promise((resolve) => { 75 resolve() 76 }) 77 } 78 } 79 } 80 const storageMock = { 81 getSync: function () { 82 console.warn("Storage.getSync interface mocked in the Previewer. How this interface works on the Previewer may" + 83 " be different from that on a real device.") 84 return ValueTypes 85 }, 86 get: function (...args) { 87 console.warn("Storage.get interface mocked in the Previewer. How this interface works on the Previewer may be" + 88 " different from that on a real device.") 89 const len = args.length 90 if (typeof args[len - 1] === 'function') { 91 args[len - 1].call(this, paramMock.businessErrorMock, ValueTypes) 92 } else { 93 return new Promise((resolve) => { 94 resolve(ValueTypes) 95 }) 96 } 97 }, 98 hasSync: function () { 99 console.warn("Storage.hasSync interface mocked in the Previewer. How this interface works on the Previewer may" + 100 " be different from that on a real device.") 101 return paramMock.paramBooleanMock 102 }, 103 has: function (...args) { 104 console.warn("Storage.has interface mocked in the Previewer. How this interface works on the Previewer may be" + 105 " different from that on a real device.") 106 const len = args.length 107 if (typeof args[len - 1] === 'function') { 108 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock) 109 } else { 110 return new Promise((resolve) => { 111 resolve(paramMock.paramBooleanMock) 112 }) 113 } 114 }, 115 putSync: function () { 116 console.warn("Storage.putSync interface mocked in the Previewer. How this interface works on the Previewer may" + 117 " be different from that on a real device.") 118 }, 119 put: function (...args) { 120 console.warn("Storage.put interface mocked in the Previewer. How this interface works on the Previewer may be" + 121 " different from that on a real device.") 122 const len = args.length 123 if (typeof args[len - 1] === 'function') { 124 args[len - 1].call(this, paramMock.businessErrorMock) 125 } else { 126 return new Promise((resolve) => { 127 resolve() 128 }) 129 } 130 }, 131 deleteSync: function () { 132 console.warn("Storage.deleteSync interface mocked in the Previewer. How this interface works on the Previewer" + 133 " may be different from that on a real device.") 134 }, 135 delete: function (...args) { 136 console.warn("Storage.delete interface mocked in the Previewer. How this interface works on the Previewer may" + 137 " be different from that on a real device.") 138 const len = args.length 139 if (typeof args[len - 1] === 'function') { 140 args[len - 1].call(this, paramMock.businessErrorMock) 141 } else { 142 return new Promise((resolve) => { 143 resolve() 144 }) 145 } 146 }, 147 clearSync: function () { 148 console.warn("Storage.clearSync interface mocked in the Previewer. How this interface works on the Previewer" + 149 " may be different from that on a real device.") 150 }, 151 clear: function (...args) { 152 console.warn("Storage.clear interface mocked in the Previewer. How this interface works on the Previewer may" + 153 " be different from that on a real device.") 154 const len = args.length 155 if (typeof args[len - 1] === 'function') { 156 args[len - 1].call(this, paramMock.businessErrorMock) 157 } else { 158 return new Promise((resolve) => { 159 resolve() 160 }) 161 } 162 }, 163 flushSync: function () { 164 console.warn("Storage.flushSync interface mocked in the Previewer. How this interface works on the Previewer" + 165 " may be different from that on a real device.") 166 }, 167 flush: function (...args) { 168 console.warn("Storage.flush interface mocked in the Previewer. How this interface works on the Previewer may" + 169 " be different from that on a real device.") 170 const len = args.length 171 if (typeof args[len - 1] === 'function') { 172 args[len - 1].call(this, paramMock.businessErrorMock) 173 } else { 174 return new Promise((resolve) => { 175 resolve() 176 }) 177 } 178 }, 179 on: function (...args) { 180 console.warn("Storage.on interface mocked in the Previewer. How this interface works on the Previewer may be" + 181 " different from that on a real device.") 182 const len = args.length 183 args[len - 1].call(this, StorageObserver) 184 }, 185 off: function (...args) { 186 console.warn("Storage.off interface mocked in the Previewer. How this interface works on the Previewer may be" + 187 " different from that on a real device.") 188 const len = args.length 189 args[len - 1].call(this, storageObserver) 190 } 191 } 192 return storage 193}