• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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";
17import { RawFileDescriptor } from "./global/rawFileDescriptor"
18
19export function mockResourceManager() {
20  const Direction = {
21    DIRECTION_VERTICAL : 0,
22    DIRECTION_HORIZONTAL : 1
23  }
24
25  const ConfigurationClass = class Configuration {
26    constructor() {
27      console.warn('resourceManager.Configuration.constructor interface mocked in the Previewer. How this interface works on' +
28        ' the Previewer may be different from that on a real device.');
29      this.direction = Direction;
30      this.locale = '[PC Preview] unknow string';
31    }
32  }
33
34  const ScreenDensity = {
35    SCREEN_SDPI : 120,
36    SCREEN_MDPI : 160,
37    SCREEN_LDPI : 240,
38    SCREEN_XLDPI : 320,
39    SCREEN_XXLDPI : 480,
40    SCREEN_XXXLDPI : 640
41  }
42
43  const DeviceType = {
44    DEVICE_TYPE_PHONE : 0x00,
45    DEVICE_TYPE_TABLET : 0x01,
46    DEVICE_TYPE_CAR : 0x02,
47    DEVICE_TYPE_PC : 0x03,
48    DEVICE_TYPE_TV : 0x04,
49    DEVICE_TYPE_WEARABLE : 0x06
50  }
51
52  const DeviceCapabilityClass = class DeviceCapability {
53    constructor() {
54      console.warn('resourceManager.DeviceCapability.constructor interface mocked in the Previewer. How this interface works on' +
55        ' the Previewer may be different from that on a real device.');
56      this.screenDensity = ScreenDensity;
57      this.deviceType = DeviceType;
58    }
59  }
60
61  const ResourceManager = {
62    getString: function(...args) {
63      console.warn("ResourceManager.getString interface mocked in the Previewer. " +
64        "How this interface works on the Previewer may be different from that on a real device.")
65      const len = args.length
66      if (typeof args[len - 1] === 'function') {
67        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
68      } else {
69        return new Promise((resolve, reject) => {
70          resolve(paramMock.paramStringMock);
71        })
72      }
73    },
74
75    getStringArray: function(...args) {
76      console.warn("ResourceManager.getStringArray interface mocked in the Previewer. " +
77        "How this interface works on the Previewer may be different from that on a real device.")
78      const len = args.length
79      if (typeof args[len - 1] === 'function') {
80        args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paramStringMock]);
81      } else {
82        return new Promise((resolve, reject) => {
83          resolve([paramMock.paramStringMock]);
84        })
85      }
86    },
87
88    getMedia: function(...args) {
89      console.warn("ResourceManager.getMedia interface mocked in the Previewer. " +
90        "How this interface works on the Previewer may be different from that on a real device.")
91      const len = args.length
92      if (typeof args[len - 1] === 'function') {
93        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
94      } else {
95        return new Promise((resolve, reject) => {
96          resolve(paramMock.paramArrayMock);
97        })
98      }
99    },
100
101    getMediaBase64: function(...args) {
102      console.warn("ResourceManager.getMediaBase64 interface mocked in the Previewer. " +
103        "How this interface works on the Previewer may be different from that on a real device.")
104      const len = args.length
105      if (typeof args[len - 1] === 'function') {
106        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
107      } else {
108        return new Promise((resolve, reject) => {
109          resolve(paramMock.paramArrayMock);
110        })
111      }
112    },
113
114    getPluralString: function(...args) {
115      console.warn("ResourceManager.getPluralString interface mocked in the Previewer. " +
116        "How this interface works on the Previewer may be different from that on a real device.")
117      const len = args.length
118      if (typeof args[len - 1] === 'function') {
119        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
120      } else {
121        return new Promise((resolve, reject) => {
122          resolve(paramMock.paramStringMock);
123        })
124      }
125    },
126
127    getConfiguration: function(...args) {
128      console.warn("ResourceManager.getConfiguration interface mocked in the Previewer. " +
129        "How this interface works on the Previewer may be different from that on a real device.")
130      const len = args.length
131      if (typeof args[len - 1] === 'function') {
132        args[len - 1].call(this, paramMock.businessErrorMock, new ConfigurationClass());
133      } else {
134        return new Promise((resolve, reject) => {
135          resolve(new ConfigurationClass());
136        })
137      }
138    },
139
140    getDeviceCapability: function(...args) {
141      console.warn("ResourceManager.getDeviceCapability interface mocked in the Previewer. " +
142        "How this interface works on the Previewer may be different from that on a real device.")
143      const len = args.length
144      if (typeof args[len - 1] === 'function') {
145        args[len - 1].call(this, paramMock.businessErrorMock, new DeviceCapabilityClass());
146      } else {
147        return new Promise((resolve, reject) => {
148          resolve(new DeviceCapabilityClass());
149        })
150      }
151    },
152
153    release: function() {
154      console.warn("ResourceManager.release interface mocked in the Previewer. " +
155        "How this interface works on the Previewer may be different from that on a real device.")
156    },
157
158    getRawFile: function(...args) {
159      console.warn("ResourceManager.getRawFile interface mocked in the Previewer. " +
160        "How this interface works on the Previewer may be different from that on a real device.")
161      const len = args.length
162      if (typeof args[len - 1] === 'function') {
163        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
164      } else {
165        return new Promise((resolve, reject) => {
166          resolve(paramMock.paramArrayMock);
167        })
168      }
169    },
170
171    getRawFileDescriptor: function(...args) {
172      console.warn("ResourceManager.getRawFileDescriptor interface mocked in the Previewer. " +
173        "How this interface works on the Previewer may be different from that on a real device.")
174      const len = args.length
175      if (typeof args[len - 1] === 'function') {
176        args[len - 1].call(this, paramMock.businessErrorMock, RawFileDescriptor);
177      } else {
178        return new Promise((resolve, reject) => {
179          resolve(RawFileDescriptor);
180        })
181      }
182    },
183
184    closeRawFileDescriptor: function(...args) {
185      console.warn("ResourceManager.closeRawFileDescriptor interface mocked in the Previewer. " +
186        "How this interface works on the Previewer may be different from that on a real device.")
187      const len = args.length
188      if (typeof args[len - 1] === 'function') {
189        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
190      } else {
191        return new Promise((resolve, reject) => {
192          resolve(paramMock.paramStringMock);
193        })
194      }
195    },
196
197    getStringByName: function(...args) {
198      console.warn("ResourceManager.getStringByName interface mocked in the Previewer. " +
199        "How this interface works on the Previewer may be different from that on a real device.")
200      const len = args.length
201      if (typeof args[len - 1] === 'function') {
202        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
203      } else {
204        return new Promise((resolve, reject) => {
205          resolve(paramMock.paramStringMock);
206        })
207      }
208    },
209
210    getStringArrayByName: function(...args) {
211      console.warn("ResourceManager.getStringArrayByName interface mocked in the Previewer. " +
212        "How this interface works on the Previewer may be different from that on a real device.")
213      const len = args.length
214      if (typeof args[len - 1] === 'function') {
215        args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paramStringMock]);
216      } else {
217        return new Promise((resolve, reject) => {
218          resolve([paramMock.paramStringMock]);
219        })
220      }
221    },
222
223    getMediaByName: function(...args) {
224      console.warn("ResourceManager.getMediaByName interface mocked in the Previewer. " +
225        "How this interface works on the Previewer may be different from that on a real device.")
226      const len = args.length
227      if (typeof args[len - 1] === 'function') {
228        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
229      } else {
230        return new Promise((resolve, reject) => {
231          resolve(paramMock.paramArrayMock);
232        })
233      }
234    },
235
236    getMediaBase64ByName: function(...args) {
237      console.warn("ResourceManager.getMediaBase64ByName interface mocked in the Previewer. " +
238        "How this interface works on the Previewer may be different from that on a real device.")
239      const len = args.length
240      if (typeof args[len - 1] === 'function') {
241        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
242      } else {
243        return new Promise((resolve, reject) => {
244          resolve(paramMock.paramArrayMock);
245        })
246      }
247    },
248
249    getPluralStringByName: function(...args) {
250      console.warn("ResourceManager.getPluralStringByName interface mocked in the Previewer. " +
251        "How this interface works on the Previewer may be different from that on a real device.")
252      const len = args.length
253      if (typeof args[len - 1] === 'function') {
254        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
255      } else {
256        return new Promise((resolve, reject) => {
257          resolve(paramMock.paramStringMock);
258        })
259      }
260    },
261
262    getStringSync: function(...args) {
263      console.warn("ResourceManager.getStringSync interface mocked in the Previewer. " +
264        "How this interface works on the Previewer may be different from that on a real device.")
265      return paramMock.paramStringMock;
266    },
267
268    getStringByNameSync: function(...args) {
269      console.warn("ResourceManager.getStringByNameSync interface mocked in the Previewer. " +
270        "How this interface works on the Previewer may be different from that on a real device.")
271      return paramMock.paramStringMock;
272    },
273
274    getBoolean: function(...args) {
275      console.warn("ResourceManager.getBoolean interface mocked in the Previewer. " +
276        "How this interface works on the Previewer may be different from that on a real device.")
277      return paramMock.paramBooleanMock;
278    },
279
280    getBooleanByName: function(...args) {
281      console.warn("ResourceManager.getBooleanByName interface mocked in the Previewer. " +
282        "How this interface works on the Previewer may be different from that on a real device.")
283      return paramMock.paramBooleanMock;
284    },
285
286    getNumber: function(...args) {
287      console.warn("ResourceManager.getNumber interface mocked in the Previewer. " +
288        "How this interface works on the Previewer may be different from that on a real device.")
289      return paramMock.paramNumberMock;
290    },
291
292    getNumberByName: function(...args) {
293      console.warn("ResourceManager.getNumberByName interface mocked in the Previewer. " +
294        "How this interface works on the Previewer may be different from that on a real device.")
295      return paramMock.paramNumberMock;
296    }
297  }
298
299  const resourceManager = {
300    ResourceManager,
301    getResourceManager: async function getResourceManager(optBundleName, optCallback) {
302      let bundleName = '';
303      let callback;
304      if (typeof optCallback == 'function') {
305        bundleName = optBundleName ? optBundleName : '';
306        callback = optCallback;
307      } else if (typeof optBundleName == 'function') {
308        callback = optBundleName;
309      } else {
310        bundleName = optBundleName ? optBundleName : '';
311      }
312      console.warn("ResourceManager.getResourceManager interface mocked in the Previewer. " +
313        "How this interface works on the Previewer may be different from that on a real device.")
314      if (typeof callback === 'function') {
315        callback.call(this, paramMock.businessErrorMock, ResourceManager);
316      } else {
317        return new Promise((resolve, reject) => {
318          resolve(ResourceManager);
319        })
320      }
321    },
322  }
323  return resourceManager;
324}
325
326
327