• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 { PixelMapMock } from "./ohos_multimedia_image"
18
19export function mockMultimediaMediaLibrary() {
20  const mockFileAsset = {
21    id: "[PC Preview] unknow id",
22    uri: "[PC Preview] unknow uri",
23    mimeType: "[PC Preview] unknow mimeType",
24    mediaType: "[PC Preview] unknow mediaType",
25    displayName: "[PC Preview] unknow displayName",
26    title: "[PC Preview] unknow title",
27    relativePath: "[PC Preview] unknow relativePath",
28    parent: "[PC Preview] unknow parent",
29    size: "[PC Preview] unknow size",
30    dateAdded: "[PC Preview] unknow dateAdded",
31    dateModified: "[PC Preview] unknow dateModified",
32    dateTaken: "[PC Preview] unknow dateTaken",
33    artist: "[PC Preview] unknow artist",
34    audioAlbum: "[PC Preview] unknow audioAlbum",
35    width: "[PC Preview] unknow width",
36    height: "[PC Preview] unknow height",
37    orientation: "[PC Preview] unknow orientation",
38    duration: "[PC Preview] unknow duration",
39    albumId: "[PC Preview] unknow albumId",
40    albumUri: "[PC Preview] unknow albumUri",
41    albumName: "[PC Preview] unknow albumName",
42    isDirectory: function (...args) {
43      console.warn("FileAsset.isDirectory interface mocked in the Previewer. How this interface works on the" +
44        " Previewer may be different from that on a real device.")
45      const len = args.length
46      if (len > 0 && typeof args[len - 1] === 'function') {
47        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock)
48      } else {
49        return new Promise((resolve, reject) => {
50          resolve(paramMock.paramBooleanMock)
51        })
52      }
53    },
54    commitModify: function (...args) {
55      console.warn("FileAsset.commitModify interface mocked in the Previewer. How this interface works on the" +
56        " Previewer may be different from that on a real device.")
57      const len = args.length
58      if (len > 0 && typeof args[len - 1] === 'function') {
59        args[len - 1].call(this, paramMock.businessErrorMock)
60      } else {
61        return new Promise((resolve, reject) => {
62          resolve()
63        })
64      }
65    },
66    open: function (...args) {
67      console.warn("FileAsset.open interface mocked in the Previewer. How this interface works on the" +
68        " Previewer may be different from that on a real device.")
69      const len = args.length
70      if (len > 0 && typeof args[len - 1] === 'function') {
71        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock)
72      } else {
73        return new Promise((resolve, reject) => {
74          resolve(paramMock.paramNumberMock)
75        })
76      }
77    },
78    close: function (...args) {
79      console.warn("FileAsset.close interface mocked in the Previewer. How this interface works on the" +
80        " Previewer may be different from that on a real device.")
81      const len = args.length
82      if (len > 0 && typeof args[len - 1] === 'function') {
83        args[len - 1].call(this, paramMock.businessErrorMock)
84      } else {
85        return new Promise((resolve, reject) => {
86          resolve()
87        })
88      }
89    },
90    getThumbnail: function (...args) {
91      console.warn("FileAsset.getThumbnail interface mocked in the Previewer. How this interface works on the" +
92        " Previewer may be different from that on a real device.")
93      const len = args.length
94      if (len > 0 && typeof args[len - 1] === 'function') {
95        args[len - 1].call(this, paramMock.businessErrorMock, mockImagePixelMap)
96      } else {
97        return new Promise((resolve, reject) => {
98          resolve(mockImagePixelMap)
99        })
100      }
101    },
102    favorite: function (...args) {
103      console.warn("FileAsset.favorite interface mocked in the Previewer. How this interface works on the" +
104        " Previewer may be different from that on a real device.")
105      const len = args.length
106      if (len > 0 && typeof args[len - 1] === 'function') {
107        args[len - 1].call(this, paramMock.businessErrorMock)
108      } else {
109        return new Promise((resolve, reject) => {
110          resolve()
111        })
112      }
113    },
114    isFavorite: function (...args) {
115      console.warn("FileAsset.isFavorite interface mocked in the Previewer. How this interface works on the" +
116        " Previewer may be different from that on a real device.")
117      const len = args.length
118      if (len > 0 && typeof args[len - 1] === 'function') {
119        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock)
120      } else {
121        return new Promise((resolve, reject) => {
122          resolve(paramMock.paramBooleanMock)
123        })
124      }
125    },
126    trash: function (...args) {
127      console.warn("FileAsset.trash interface mocked in the Previewer. How this interface works on the" +
128        " Previewer may be different from that on a real device.")
129      const len = args.length
130      if (len > 0 && typeof args[len - 1] === 'function') {
131        args[len - 1].call(this, paramMock.businessErrorMock)
132      } else {
133        return new Promise((resolve, reject) => {
134          resolve()
135        })
136      }
137    },
138    isTrash: function (...args) {
139      console.warn("FileAsset.isTrash interface mocked in the Previewer. How this interface works on the" +
140        " Previewer may be different from that on a real device.")
141      const len = args.length
142      if (len > 0 && typeof args[len - 1] === 'function') {
143        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock)
144      } else {
145        return new Promise((resolve, reject) => {
146          resolve(paramMock.paramBooleanMock)
147        })
148      }
149    },
150  };
151
152  const mockFetchFileResult = {
153    getCount: function (...args) {
154      console.warn("FetchFileResult.getCount interface mocked in the Previewer. How this interface works on the" +
155        " Previewer may be different from that on a real device.")
156      return paramMock.paramNumberMock
157    },
158    isAfterLast: function (...args) {
159      console.warn("FetchFileResult.isAfterLast interface mocked in the Previewer. How this interface works on the" +
160        " Previewer may be different from that on a real device.")
161      return paramMock.paramBooleanMock
162    },
163    close: function (...args) {
164      console.warn("FetchFileResult.close interface mocked in the Previewer. How this interface works on the" +
165        " Previewer may be different from that on a real device.")
166    },
167    getFirstObject: function (...args) {
168      console.warn("FetchFileResult.getFirstObject interface mocked in the Previewer. How this interface works on the" +
169        " Previewer may be different from that on a real device.")
170      const len = args.length
171      if (len > 0 && typeof args[len - 1] === 'function') {
172        args[len - 1].call(this, paramMock.businessErrorMock, mockFileAsset)
173      } else {
174        return new Promise((resolve, reject) => {
175          resolve(mockFileAsset)
176        })
177      }
178    },
179    getNextObject: function (...args) {
180      console.warn("FetchFileResult.getNextObject interface mocked in the Previewer. How this interface works on the" +
181        " Previewer may be different from that on a real device.")
182      const len = args.length
183      if (len > 0 && typeof args[len - 1] === 'function') {
184        args[len - 1].call(this, paramMock.businessErrorMock, mockFileAsset)
185      } else {
186        return new Promise((resolve, reject) => {
187          resolve(mockFileAsset)
188        })
189      }
190    },
191    getLastObject: function (...args) {
192      console.warn("FetchFileResult.getLastObject interface mocked in the Previewer. How this interface works on the" +
193        " Previewer may be different from that on a real device.")
194      const len = args.length
195      if (len > 0 && typeof args[len - 1] === 'function') {
196        args[len - 1].call(this, paramMock.businessErrorMock, mockFileAsset)
197      } else {
198        return new Promise((resolve, reject) => {
199          resolve(mockFileAsset)
200        })
201      }
202    },
203    getPositionObject: function (...args) {
204      console.warn("FetchFileResult.getPositionObject interface mocked in the Previewer. How this interface works on the" +
205        " Previewer may be different from that on a real device.")
206      const len = args.length
207      if (len > 0 && typeof args[len - 1] === 'function') {
208        args[len - 1].call(this, paramMock.businessErrorMock, mockFileAsset)
209      } else {
210        return new Promise((resolve, reject) => {
211          resolve(mockFileAsset)
212        })
213      }
214    },
215    getAllObject: function (...args) {
216      console.warn("FetchFileResult.getPositionObject interface mocked in the Previewer. How this interface works on the" +
217        " Previewer may be different from that on a real device.")
218      const len = args.length
219      if (len > 0 && typeof args[len - 1] === 'function') {
220        args[len - 1].call(this, paramMock.businessErrorMock, [mockFileAsset])
221      } else {
222        return new Promise((resolve, reject) => {
223          resolve([mockFileAsset])
224        })
225      }
226    },
227  };
228
229  const mockPeerInfo = {
230    deviceName: "[PC Preview] unknow deviceName",
231    networkId: "[PC Preview] unknow networkId",
232    deviceType: "[PC Preview] unknow deviceType",
233    isOnline: "[PC Preview] unknow isOnline"
234  };
235
236  const mockImagePixelMap = PixelMapMock;
237
238  const mockAlbum = {
239    albumId: "[PC Preview] unknow albumId",
240    albumName: "[PC Preview] unknow albumName",
241    albumUri: "[PC Preview] unknow albumUri",
242    dateModified: "[PC Preview] unknow dateModified",
243    count: "[PC Preview] unknow count",
244    relativePath: "[PC Preview] unknow relativePath",
245    coverUri: "[PC Preview] unknow coverUri",
246    commitModify: function (...args) {
247      console.warn("Album.commitModify interface mocked in the Previewer. How this interface works on the" +
248        " Previewer may be different from that on a real device.")
249      const len = args.length
250      if (len > 0 && typeof args[len - 1] === 'function') {
251        args[len - 1].call(this, paramMock.businessErrorMock);
252      } else {
253        return new Promise((resolve, reject) => {
254          resolve()
255        })
256      }
257    },
258    getFileAssets: function (...args) {
259      console.warn("Album.getFileAssets interface mocked in the Previewer. How this interface works on the" +
260        " Previewer may be different from that on a real device.")
261      const len = args.length
262      if (len > 0 && typeof args[len - 1] === 'function') {
263        args[len - 1].call(this, paramMock.businessErrorMock, mockFetchFileResult);
264      } else {
265        return new Promise((resolve, reject) => {
266          resolve(mockFetchFileResult)
267        })
268      }
269    }
270  };
271
272  const mockMediaLibrary = {
273    getPublicDirectory: function (...args) {
274      console.warn("MediaLibrary.getPublicDirectory interface mocked in the Previewer. How this interface works on the" +
275        " Previewer may be different from that on a real device.")
276      const len = args.length
277      if (typeof args[len - 1] === 'function') {
278        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
279      } else {
280        return new Promise((resolve, reject) => {
281          resolve(paramMock.paramStringMock)
282        })
283      }
284    },
285    getFileAssets: function (...args) {
286      console.warn("MediaLibrary.getFileAssets interface mocked in the Previewer. How this interface works on the" +
287        " Previewer may be different from that on a real device.")
288      const len = args.length
289      if (typeof args[len - 1] === 'function') {
290        args[len - 1].call(this, paramMock.businessErrorMock, mockFetchFileResult);
291      } else {
292        return new Promise((resolve, reject) => {
293          resolve(mockFetchFileResult)
294        })
295      }
296    },
297    on: function (...args) {
298      console.warn("MediaLibrary.on interface mocked in the Previewer. How this interface works on the" +
299        " Previewer may be different from that on a real device.")
300    },
301    off: function (...args) {
302      console.warn("MediaLibrary.off interface mocked in the Previewer. How this interface works on the" +
303        " Previewer may be different from that on a real device.")
304    },
305    createAsset: function (...args) {
306      console.warn("MediaLibrary.createAsset interface mocked in the Previewer. How this interface works on the" +
307        " Previewer may be different from that on a real device.")
308      const len = args.length
309      if (typeof args[len - 1] === 'function') {
310        args[len - 1].call(this, paramMock.businessErrorMock, mockFileAsset);
311      } else {
312        return new Promise((resolve, reject) => {
313          resolve(mockFileAsset)
314        })
315      }
316    },
317    deleteAsset: function (...args) {
318      console.warn("MediaLibrary.deleteAsset interface mocked in the Previewer. How this interface works on the" +
319        " Previewer may be different from that on a real device.")
320      const len = args.length
321      if (typeof args[len - 1] === 'function') {
322        args[len - 1].call(this, paramMock.businessErrorMock);
323      } else {
324        return new Promise((resolve, reject) => {
325          resolve()
326        })
327      }
328    },
329    getAlbums: function (...args) {
330      console.warn("MediaLibrary.getAlbums interface mocked in the Previewer. How this interface works on the" +
331        " Previewer may be different from that on a real device.")
332      const len = args.length
333      if (typeof args[len - 1] === 'function') {
334        args[len - 1].call(this, paramMock.businessErrorMock, [mockAlbum]);
335      } else {
336        return new Promise((resolve, reject) => {
337          resolve([mockAlbum])
338        })
339      }
340    },
341    storeMediaAsset: function (...args) {
342      console.warn("MediaLibrary.storeMediaAsset interface mocked in the Previewer. How this interface works on the" +
343        " Previewer may be different from that on a real device.")
344      const len = args.length
345      if (typeof args[len - 1] === 'function') {
346        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
347      } else {
348        return new Promise((resolve, reject) => {
349          resolve(paramMock.paramStringMock)
350        })
351      }
352    },
353    startImagePreview: function (...args) {
354      console.warn("MediaLibrary.startImagePreview interface mocked in the Previewer. How this interface works on the" +
355        " Previewer may be different from that on a real device.")
356      const len = args.length
357      if (typeof args[len - 1] === 'function') {
358        args[len - 1].call(this, paramMock.businessErrorMock);
359      } else {
360        return new Promise((resolve, reject) => {
361          resolve()
362        })
363      }
364    },
365    startMediaSelect: function (...args) {
366      console.warn("MediaLibrary.startMediaSelect interface mocked in the Previewer. How this interface works on the" +
367        " Previewer may be different from that on a real device.")
368      const len = args.length
369      if (typeof args[len - 1] === 'function') {
370        args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paramStringMock]);
371      } else {
372        return new Promise((resolve, reject) => {
373          resolve([paramMock.paramStringMock])
374        })
375      }
376    },
377    getActivePeers: function (...args) {
378      console.warn("MediaLibrary.getActivePeers interface mocked in the Previewer. How this interface works on the" +
379        " Previewer may be different from that on a real device.")
380      const len = args.length
381      if (typeof args[len - 1] === 'function') {
382        args[len - 1].call(this, paramMock.businessErrorMock, [mockPeerInfo]);
383      } else {
384        return new Promise((resolve, reject) => {
385          resolve([mockPeerInfo])
386        })
387      }
388    },
389    getAllPeers: function (...args) {
390      console.warn("MediaLibrary.getAllPeers interface mocked in the Previewer. How this interface works on the" +
391        " Previewer may be different from that on a real device.")
392      const len = args.length
393      if (typeof args[len - 1] === 'function') {
394        args[len - 1].call(this, paramMock.businessErrorMock, [mockPeerInfo]);
395      } else {
396        return new Promise((resolve, reject) => {
397          resolve([mockPeerInfo])
398        })
399      }
400    },
401    release: function (...args) {
402      console.warn("MediaLibrary.startImagePreview interface mocked in the Previewer. How this interface works on the" +
403        " Previewer may be different from that on a real device.")
404      const len = args.length
405      if (typeof args[len - 1] === 'function') {
406        args[len - 1].call(this, paramMock.businessErrorMock);
407      } else {
408        return new Promise((resolve, reject) => {
409          resolve()
410        })
411      }
412    }
413  };
414
415  const mediaLibrary = {
416    MediaType: {
417      FILE: 0,
418      IMAGE: 1,
419      VIDEO: 2,
420      AUDIO: 3
421    },
422    FileKey: {
423      ID: "file_id",
424      RELATIVE_PATH: "relative_path",
425      DISPLAY_NAME: "display_name",
426      PARENT: "parent",
427      MIME_TYPE: "mime_type",
428      MEDIA_TYPE: "media_type",
429      SIZE: "size",
430      DATE_ADDED: "date_added",
431      DATE_MODIFIED: "date_modified",
432      DATE_TAKEN: "date_taken",
433      TITLE: "title",
434      ARTIST: "artist",
435      AUDIOALBUM: "audio_album",
436      DURATION: "duration",
437      WIDTH: "width",
438      HEIGHT: "height",
439      ORIENTATION: "orientation",
440      ALBUM_ID: "bucket_id",
441      ALBUM_NAME: "bucket_display_name"
442    },
443    DirectoryType: {
444      DIR_CAMERA: 0,
445      DIR_VIDEO: 1,
446      DIR_IMAGE: 2,
447      DIR_AUDIO: 3,
448      DIR_DOCUMENTS: 4,
449      DIR_DOWNLOAD: 5
450    },
451    DeviceType: {
452      TYPE_UNKNOWN: 0,
453      TYPE_LAPTOP: 1,
454      TYPE_PHONE: 2,
455      TYPE_TABLET: 3,
456      TYPE_WATCH: 4,
457      TYPE_CAR: 5,
458      TYPE_TV: 6
459    },
460    getMediaLibrary: function (...args) {
461      console.warn("mediaLibrary.getMediaLibrary interface mocked in the Previewer. How this interface works on the Previewer may" +
462        " be different from that on a real device.")
463      return mockMediaLibrary;
464    }
465  };
466
467  return mediaLibrary;
468}
469