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 16export function mockResourceManager() { 17 function Configuration(direction, locale) { 18 this.direction = direction; 19 this.locale = locale; 20 } 21 22 function DeviceCapability(screenDensity, deviceType) { 23 this.screenDensity = screenDensity; 24 this.deviceType = deviceType; 25 } 26 27 function ResourceManager(mgrId, module) { 28 var resMgrId = mgrId; 29 var resourcemgrModuleGroup = module; 30 31 this.getString = getString; 32 async function getString(resId, callback) { 33 var data = "mock string"; 34 console.warn("ResourceManager.getString interface mocked in the Previewer. " + 35 "How this interface works on the Previewer may be different from that on a real device.") 36 if (typeof callback === 'function') { 37 callback.call(this, null, data); 38 } else { 39 return new Promise((resolve) => { 40 resolve(data); 41 }) 42 } 43 } 44 45 this.getStringArray = getStringArray; 46 async function getStringArray(resId, callback) { 47 var data = ["mock string1", "mock string2"]; 48 console.warn("ResourceManager.getStringArray interface mocked in the Previewer. " + 49 "How this interface works on the Previewer may be different from that on a real device.") 50 if (typeof callback === 'function') { 51 callback.call(this, null, data); 52 } else { 53 return new Promise((resolve) => { 54 resolve(data); 55 }) 56 } 57 } 58 59 this.getMedia = getMedia; 60 async function getMedia(resId, callback) { 61 var data = "mock media data"; 62 console.warn("ResourceManager.getMedia interface mocked in the Previewer. " + 63 "How this interface works on the Previewer may be different from that on a real device.") 64 if (typeof callback === 'function') { 65 callback.call(this, 1, data); 66 } else { 67 return new Promise((resolve, reject) => { 68 reject(data); 69 }) 70 } 71 } 72 73 this.getMediaBase64 = getMediaBase64; 74 async function getMediaBase64(resId, callback) { 75 var data = "mock media data"; 76 console.warn("ResourceManager.getMediaBase64 interface mocked in the Previewer. " + 77 "How this interface works on the Previewer may be different from that on a real device.") 78 if (typeof callback === 'function') { 79 callback.call(this, 1, data); 80 } else { 81 return new Promise((resolve, reject) => { 82 reject(data); 83 }) 84 } 85 } 86 87 this.getPluralString = getPluralString; 88 async function getPluralString(resId, num, callback) { 89 var data = "mock plural string"; 90 console.warn("ResourceManager.getPluralString interface mocked in the Previewer. " + 91 "How this interface works on the Previewer may be different from that on a real device.") 92 if (typeof callback === 'function') { 93 callback.call(this, null, data); 94 } else { 95 return new Promise((resolve) => { 96 resolve(data); 97 }) 98 } 99 } 100 101 this.getConfiguration = getConfiguration; 102 async function getConfiguration(callback) { 103 var data = { "direction": 0, "locale": "zh_CN" }; 104 console.warn("ResourceManager.getConfiguration interface mocked in the Previewer. " + 105 "How this interface works on the Previewer may be different from that on a real device.") 106 if (typeof callback === 'function') { 107 callback.call(this, null, data); 108 } else { 109 return new Promise((resolve) => { 110 resolve(data); 111 }) 112 } 113 } 114 115 this.getDeviceCapability = getDeviceCapability; 116 async function getDeviceCapability(callback) { 117 var data = { "deviceType": 0, "screenDensity": 480 }; 118 console.warn("ResourceManager.getDeviceCapability interface mocked in the Previewer. " + 119 "How this interface works on the Previewer may be different from that on a real device.") 120 if (typeof callback === 'function') { 121 callback.call(this, null, data); 122 } else { 123 return new Promise((resolve) => { 124 resolve(data); 125 }) 126 } 127 } 128 129 this.release = release; 130 async function release(callback) { 131 console.warn("ResourceManager.release interface mocked in the Previewer. " + 132 "How this interface works on the Previewer may be different from that on a real device.") 133 } 134 135 this.getRawFile = getRawFile; 136 async function getRawFile(path, callback) { 137 var data = "mock raw file"; 138 console.warn("ResourceManager.getRawFile interface mocked in the Previewer. " + 139 "How this interface works on the Previewer may be different from that on a real device.") 140 if (typeof callback === 'function') { 141 callback.call(this, null, data); 142 } else { 143 return new Promise((resolve) => { 144 resolve(data); 145 }) 146 } 147 } 148 149 this.getRawFileDescriptor = getRawFileDescriptor; 150 async function getRawFileDescriptor(path, callback) { 151 var data = "mock raw file descriptor"; 152 console.warn("ResourceManager.getRawFileDescriptor interface mocked in the Previewer. " + 153 "How this interface works on the Previewer may be different from that on a real device.") 154 if (typeof callback === 'function') { 155 callback.call(this, null, data); 156 } else { 157 return new Promise((resolve) => { 158 resolve(data); 159 }) 160 } 161 } 162 163 this.closeRawFileDescriptor = closeRawFileDescriptor; 164 async function closeRawFileDescriptor(path, callback) { 165 var data = "mock close raw file descriptor"; 166 console.warn("ResourceManager.closeRawFileDescriptor interface mocked in the Previewer. " + 167 "How this interface works on the Previewer may be different from that on a real device.") 168 if (typeof callback === 'function') { 169 callback.call(this, null, data); 170 } else { 171 return new Promise((resolve) => { 172 resolve(data); 173 }) 174 } 175 } 176 } 177 178 var resourceManagerMock = { 179 getResourceManager: async function getResourceManager(optBundleName, optCallback) { 180 let bundleName = ''; 181 let callback; 182 if (typeof optCallback == 'function') { 183 bundleName = optBundleName ? optBundleName : ''; 184 callback = optCallback; 185 } else if (typeof optBundleName == 'function') { 186 callback = optBundleName; 187 } else { 188 bundleName = optBundleName ? optBundleName : ''; 189 } 190 let data = new ResourceManager(1, null); 191 console.warn("ResourceManager.getResourceManager interface mocked in the Previewer. " + 192 "How this interface works on the Previewer may be different from that on a real device.") 193 if (typeof callback === 'function') { 194 callback.call(this, 1, data); 195 } else { 196 return new Promise((resolve) => { 197 resolve(data); 198 }) 199 } 200 } 201 }; 202 203 const resourceManager = { 204 getResourceManager: resourceManagerMock.getResourceManager 205 } 206 return resourceManager 207} 208