• 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"
17
18export function mockRequest() {
19  const DownloadConfig = {
20    url: "[PC Preview] unknow uri",
21    header: "[PC Preview] unknow header",
22    enableMetered: "[PC Preview] unknow enableMetered",
23    enableRoaming: "[PC Preview] unknow enableRoaming",
24    description: "[PC Preview] unknow description",
25    networkType: "[PC Preview] unknow networkType",
26    filePath: "[PC Preview] unknow filePath",
27    title: "[PC Preview] unknow title",
28    background: "[PC Preview] unknow background",
29  };
30
31  const DownloadInfo = {
32    description: "[PC Preview] unknow description",
33    downloadedBytes: "[PC Preview] unknow downloadedBytes",
34    downloadId: "[PC Preview] unknow downloadId",
35    failedReason: "[PC Preview] unknow failedReason",
36    fileName: "[PC Preview] unknow fileName",
37    filePath: "[PC Preview] unknow filePath",
38    pausedReason: "[PC Preview] unknow pausedReason",
39    status: "[PC Preview] unknow status",
40    targetURI: "[PC Preview] unknow targetURI",
41    downloadTitle: "[PC Preview] unknow downloadTitle",
42    downloadTotalBytes: "[PC Preview] unknow downloadTotalBytes",
43  };
44
45  const DownloadTask = {
46    on: function (...args) {
47      console.warn("downloadTask.on interface mocked in the Previewer. How this interface works on the" +
48        " Previewer may be different from that on a real device.");
49      const len = args.length
50      if (typeof args[len - 1] === 'function') {
51        if (args[0] == 'progress') {
52          const receivedSize = "[PC Preview] unknow receivedSize"
53          const totalSize = "[PC Preview] unknow totalSize"
54          args[len - 1].call(this, receivedSize, totalSize)
55        } else if (args[0] == 'complete' || 'pause' || 'remove') {
56          args[len - 1].call(this)
57        } else if (args[0] == 'fail') {
58          const err = "[PC Preview] unknow err"
59          args[len - 1].call(this, err)
60        }
61      }
62    },
63
64    off: function (...args) {
65      console.warn("downloadTask.off interface mocked in the Previewer. How this interface works on the" +
66        " Previewer may be different from that on a real device.");
67      const len = args.length
68      if (typeof args[len - 1] === 'function') {
69        if (args[0] == 'progress') {
70          const receivedSize = "[PC Preview] unknow receivedSize"
71          const totalSize = "[PC Preview] unknow totalSize"
72          args[len - 1].call(this, receivedSize, totalSize)
73        } else if (args[0] == 'complete' || 'pause' || 'remove') {
74          args[len - 1].call(this)
75        } else if (args[0] == 'fail') {
76          const err = "[PC Preview]: unknow err"
77          args[len - 1].call(this, err)
78        }
79      }
80    },
81
82    remove: function (...args) {
83      console.warn("downloadTask.remove interface mocked in the Previewer. How this interface works on the" +
84        " Previewer may be different from that on a real device.")
85      const len = args.length
86      if (typeof args[len - 1] === 'function') {
87        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock)
88      } else {
89        return new Promise((resolve) => {
90          resolve(paramMock.paramBooleanMock)
91        })
92      }
93    },
94
95    pause: function (...args) {
96      console.warn("downloadTask.pause interface mocked in the Previewer. How this interface works on the" +
97        " Previewer may be different from that on a real device.")
98      const len = args.length
99      if (typeof args[len - 1] === 'function') {
100        args[len - 1].call(this, paramMock.businessErrorMock)
101      } else {
102        return new Promise((resolve) => {
103          resolve()
104        })
105      }
106    },
107
108    resume: function (...args) {
109      console.warn("downloadTask.resume interface mocked in the Previewer. How this interface works on the" +
110        " Previewer may be different from that on a real device.")
111      const len = args.length
112      if (typeof args[len - 1] === 'function') {
113        args[len - 1].call(this, paramMock.businessErrorMock)
114      } else {
115        return new Promise((resolve) => {
116          resolve()
117        })
118      }
119    },
120
121    query: function (...args) {
122      console.warn("downloadTask.query interface mocked in the Previewer. How this interface works on the" +
123        " Previewer may be different from that on a real device.")
124      const len = args.length
125      if (typeof args[len - 1] === 'function') {
126        args[len - 1].call(this, paramMock.businessErrorMock, downloadInfoMock)
127      } else {
128        return new Promise((resolve) => {
129          resolve(downloadInfoMock)
130        })
131      }
132    },
133
134    queryMimeType: function (...args) {
135      console.warn("downloadTask.queryMimeType interface mocked in the Previewer. How this interface works on the" +
136        " Previewer may be different from that on a real device.")
137      const len = args.length
138      if (typeof args[len - 1] === 'function') {
139        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock)
140      } else {
141        return new Promise((resolve) => {
142          resolve(paramMock.paramStringMock)
143        })
144      }
145    },
146  };
147
148  const File = {
149    filename: "[PC Preview] unknow filename",
150    name: "[PC Preview] unknow name",
151    uri: "[PC Preview] unknow uri",
152    type: "[PC Preview] unknow type",
153  };
154
155  const RequestData = {
156    name: "[PC Preview] unknow name",
157    value: "[PC Preview] unknow value",
158  };
159
160  const UploadConfig = {
161    url: "[PC Preview] unknow uri",
162    header: "[PC Preview] unknow header",
163    method: "[PC Preview] unknow method",
164    files: [File],
165    data: [RequestData],
166  };
167
168  const TaskState = {
169    path: "[PC Preview] unknow path",
170    responseCode: "[PC Preview] unknow responseCode",
171    message: "[PC Preview] unknow message",
172  };
173
174  const UploadTask = {
175    on: function (...args) {
176      console.warn("uploadTask.on interface mocked in the Previewer. How this interface works on the" +
177        " Previewer may be different from that on a real device.");
178      const len = args.length
179      if (typeof args[len - 1] === 'function') {
180        if (args[0] == 'progress') {
181          const uploadedSize = "[PC Preview] unknow receivedSize"
182          const totalSize = "[PC Preview] unknow totalSize"
183          args[len - 1].call(this, uploadedSize, totalSize)
184        } else if (args[0] == 'headerReceive') {
185          const header = "[PC Preview] unknow header"
186          args[len - 1].call(this, header)
187        } else if (args[0] == 'complete' || 'fail') {
188          const taskStates = "[PC Preview] unknow taskStates"
189          args[len - 1].call(this, taskStates)
190        }
191      }
192    },
193
194    off: function (...args) {
195      console.warn("uploadTask.off interface mocked in the Previewer. How this interface works on the" +
196        " Previewer may be different from that on a real device.");
197      const len = args.length
198      if (typeof args[len - 1] === 'function') {
199        if (args[0] == 'progress') {
200          const uploadedSize = "[PC Preview] unknow receivedSize"
201          const totalSize = "[PC Preview] unknow totalSize"
202          args[len - 1].call(this, uploadedSize, totalSize)
203        } else if (args[0] == 'headerReceive') {
204          const header = "[PC Preview] unknow header"
205          args[len - 1].call(this, header)
206        } else if (args[0] == 'complete' || 'fail') {
207          const taskStates = "[PC Preview] unknow taskStates"
208          args[len - 1].call(this, taskStates)
209        }
210      }
211    },
212
213    remove: function (...args) {
214      console.warn("uploadTask.remove interface mocked in the Previewer. How this interface works on the" +
215        " Previewer may be different from that on a real device.")
216      const len = args.length
217      if (typeof args[len - 1] === 'function') {
218        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock)
219      } else {
220        return new Promise((resolve) => {
221          resolve(paramMock.paramBooleanMock)
222        })
223      }
224    },
225  };
226
227  const request = {
228    NETWORK_MOBILE: '[PC preview] unknow NETWORK_MOBILE',
229    NETWORK_WIFI: '[PC preview] unknow NETWORK_WIFI',
230    ERROR_CANNOT_RESUME: '[PC preview] unknow ERROR_CANNOT_RESUME',
231    ERROR_DEVICE_NOT_FOUND: '[PC preview] unknow ERROR_DEVICE_NOT_FOUND',
232    ERROR_FILE_ALREADY_EXISTS: '[PC preview] unknow ERROR_FILE_ALREADY_EXISTS',
233    ERROR_FILE_ERROR: '[PC preview] unknow ERROR_FILE_ERROR',
234    ERROR_HTTP_DATA_ERROR: '[PC preview] unknow ERROR_HTTP_DATA_ERROR',
235    ERROR_INSUFFICIENT_SPACE: '[PC preview] unknow ERROR_INSUFFICIENT_SPACE',
236    ERROR_TOO_MANY_REDIRECTS: '[PC preview] unknow ERROR_TOO_MANY_REDIRECTS',
237    ERROR_UNHANDLED_HTTP_CODE: '[PC preview] unknow ERROR_UNHANDLED_HTTP_CODE',
238    ERROR_UNKNOWN: '[PC preview] unknow ERROR_UNKNOWN',
239    PAUSED_QUEUED_FOR_WIFI: '[PC preview] unknow PAUSED_QUEUED_FOR_WIFI',
240    PAUSED_UNKNOWN: '[PC preview] unknow PAUSED_UNKNOWN',
241    PAUSED_WAITING_FOR_NETWORK: '[PC preview] unknow PAUSED_WAITING_FOR_NETWORK',
242    PAUSED_WAITING_TO_RETRY: '[PC preview] unknow PAUSED_WAITING_TO_RETRY',
243    SESSION_FAILED: '[PC preview] unknow SESSION_FAILED',
244    SESSION_PAUSED: '[PC preview] unknow SESSION_PAUSED',
245    SESSION_PENDING: '[PC preview] unknow SESSION_PENDING',
246    SESSION_RUNNING: '[PC preview] unknow SESSION_RUNNING',
247    SESSION_SUCCESSFUL: '[PC preview] unknow SESSION_SUCCESSFUL',
248
249    download: function (...args) {
250      console.warn("request.download interface mocked in the Previewer. How this interface works on the" +
251        " 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, DownloadTask)
255        } else {
256          return new Promise((resolve) => {
257            resolve(DownloadTask);
258          })
259        }
260    },
261
262    upload: function (...args) {
263      console.warn("request.upload interface mocked in the Previewer. How this interface works on the" +
264        " Previewer may be different from that on a real device.")
265      const len = args.length
266      if (typeof args[len - 1] === 'function') {
267        args[len - 1].call(this, paramMock.businessErrorMock, UploadTask)
268      } else {
269        return new Promise((resolve) => {
270          resolve(UploadTask)
271        })
272      }
273    }
274  }
275  return request
276}