• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NOTIFY = "[PC Preview] unknow"
19const Properties = [
20  'NETWORK_MOBILE',
21  'NETWORK_WIFI',
22  'ERROR_CANNOT_RESUME',
23  'ERROR_DEVICE_NOT_FOUND',
24  'ERROR_FILE_ALREADY_EXISTS',
25  'ERROR_FILE_ERROR',
26  'ERROR_HTTP_DATA_ERROR',
27  'ERROR_INSUFFICIENT_SPACE',
28  'ERROR_TOO_MANY_REDIRECTS',
29  'ERROR_UNHANDLED_HTTP_CODE',
30  'ERROR_UNKNOWN',
31  'PAUSED_QUEUED_FOR_WIFI',
32  'PAUSED_UNKNOWN',
33  'PAUSED_WAITING_FOR_NETWORK',
34  'PAUSED_WAITING_TO_RETRY',
35  'SESSION_FAILED',
36  'SESSION_PAUSED',
37  'SESSION_PENDING',
38  'SESSION_RUNNING',
39  'SESSION_SUCCESSFUL'
40]
41
42export function mockOhosRequest() {
43  const ErrorMock = {
44    ERROR_HTTP_DATA_ERROR: 501,
45    ERROR_DEVICE_NOT_FOUND: 502,
46    ERROR_UNHANDLED_HTTP_CODE: 503,
47    ERROR_INSUFFICIENT_SPACE: 504,
48    ERROR_TOO_MANY_REDIRECTS: 505,
49    ERROR_CANNOT_RESUME: 506,
50    ERROR_FILE_ALREADY_EXISTS: 507,
51    ERROR_FILE_ERROR: 508,
52    ERROR_UNKNOWN: 509
53  }
54  const PauseMock = {
55    PAUSED_WAITING_TO_RETRY: 301,
56    PAUSED_WAITING_FOR_NETWORK: 302,
57    PAUSED_QUEUED_FOR_WIFI: 303,
58    PAUSED_UNKNOWN: 304,
59  }
60  const SessionMock = {
61    SESSION_PENDING: 1,
62    SESSION_RUNNING: 2,
63    SESSION_PAUSED: 4,
64    SESSION_SUCCESSFUL: 8,
65    SESSION_FAILED: 16,
66  }
67  const downloadInfo = {
68    downloadId: 1,
69    failedReason: ErrorMock,
70    fileName: "[PC preview] unknown file name",
71    filePath: "[PC preview] unknown file path",
72    pausedReason: PauseMock,
73    status: SessionMock,
74    targetURI: "[PC preview] unknown targetURI",
75    downloadTitle: "[PC preview] unknown download title",
76    downloadTotalBytes: 1024,
77    description: "[PC preview] unknown description",
78    downloadedBytes: 1
79  }
80  const DownloadTaskMock = {
81    on: function (...args) {
82      console.warn("DownloadTask.on interface mocked in the Previewer. How this interface works on the Previewer may" +
83        " be different from that on a real device.")
84      const len = args.length
85      if (typeof args[len - 1] === 'function') {
86        if (args[0] == 'progress') {
87          args[len - 1].call(this, paramMock.paramNumberMock, paramMock.paramNumberMock);
88        } else if (args[0] == 'complete' || args[0] == 'pause' || args[0] == 'remove') {
89          args[len - 1].call(this, paramMock.businessErrorMock);
90        } else if (args[0] == 'fail') {
91          args[len - 1].call(this, paramMock.businessErrorMock, ErrorMock);
92        }
93      }
94    },
95    off: function (...args) {
96      console.warn("DownloadTask.off interface mocked in the Previewer. How this interface works on the Previewer" +
97        " may be different from that on a real device.")
98      const len = args.length
99      if (typeof args[len - 1] === 'function') {
100        if (args[0] == 'progress') {
101          args[len - 1].call(this, paramMock.paramNumberMock, paramMock.paramNumberMock);
102        } else if (args[0] == 'complete' || args[0] == 'pause' || args[0] == 'remove') {
103          args[len - 1].call(this, paramMock.businessErrorMock);
104        } else if (args[0] == 'fail') {
105          args[len - 1].call(this, paramMock.businessErrorMock, ErrorMock);
106        }
107      }
108    },
109    remove: function (...args) {
110      console.warn("DownloadTask.remove interface mocked in the Previewer. How this interface works on the Previewer" +
111        " may be different from that on a real device.")
112      const len = args.length
113      if (typeof args[len - 1] === 'function') {
114        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
115      } else {
116        return new Promise((resolve, reject) => {
117          resolve(paramMock.paramBooleanMock);
118        })
119      }
120    },
121    pause: function (...args) {
122      console.warn("DownloadTask.pause interface mocked in the Previewer. How this interface works on the Previewer" +
123        " 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);
127      } else {
128        return new Promise((resolve, reject) => {
129          resolve();
130        })
131      }
132    },
133    resume: function (...args) {
134      console.warn("DownloadTask.resume interface mocked in the Previewer. How this interface works on the Previewer" +
135        " may be different from that on a real device.")
136      const len = args.length
137      if (typeof args[len - 1] === 'function') {
138        args[len - 1].call(this, paramMock.businessErrorMock);
139      } else {
140        return new Promise((resolve, reject) => {
141          resolve();
142        })
143      }
144    },
145    queryMimeType: function (...args) {
146      console.warn("DownloadTask.queryMimeType interface mocked in the Previewer. How this interface works on the Previewer" +
147        " may be different from that on a real device.")
148      const len = args.length
149      if (typeof args[len - 1] === 'function') {
150        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
151      } else {
152        return new Promise((resolve, reject) => {
153          resolve(paramMock.paramStringMock);
154        })
155      }
156    },
157    query: function (...args) {
158      console.warn("DownloadTask.query interface mocked in the Previewer. How this interface works on the Previewer" +
159        " may be different from that on a real device.")
160      const len = args.length
161      if (typeof args[len - 1] === 'function') {
162        args[len - 1].call(this, paramMock.businessErrorMock, downloadInfo);
163      } else {
164        return new Promise((resolve, reject) => {
165          resolve(downloadInfo);
166        })
167      }
168    }
169  }
170  const UploadTaskMock = {
171    on: function (...args) {
172      console.warn("UploadTask.on interface mocked in the Previewer. How this interface works on the Previewer may" +
173        " be different from that on a real device.")
174      const len = args.length
175      if (typeof args[len - 1] === 'function') {
176        if (args[0] == 'progress') {
177          args[len - 1].call(this, paramMock.paramNumberMock, paramMock.paramNumberMock);
178        } else if (args[0] == 'headerReceive') {
179          args[len - 1].call(this, paramMock.paramObjectMock);
180        }
181      }
182    },
183    off: function (...args) {
184      console.warn("UploadTask.off interface mocked in the Previewer. How this interface works on the Previewer may" +
185        " be different from that on a real device.")
186      const len = args.length
187      if (typeof args[len - 1] === 'function') {
188        if (args[0] == 'progress') {
189          args[len - 1].call(this, paramMock.paramNumberMock, paramMock.paramNumberMock);
190        } else if (args[0] == 'headerReceive') {
191          args[len - 1].call(this, paramMock.paramObjectMock);
192        }
193      }
194    },
195    remove: function (...args) {
196      console.warn("UploadTask.remove interface mocked in the Previewer. How this interface works on the Previewer" +
197        " may be different from that on a real device.")
198      const len = args.length
199      if (typeof args[len - 1] === 'function') {
200        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
201      } else {
202        return new Promise((resolve, reject) => {
203          resolve(paramMock.paramBooleanMock);
204        })
205      }
206    }
207  }
208  global.ohosplugin.request = {
209    download: function (...args) {
210      console.warn("ohos.request.download interface mocked in the Previewer. How this interface works on the" +
211        " Previewer may be different from that on a real device.")
212      const len = args.length
213      if (typeof args[len - 1] === 'function') {
214        args[len - 1].call(this, paramMock.businessErrorMock, DownloadTaskMock);
215      } else {
216        return new Promise((resolve, reject) => {
217          resolve(DownloadTaskMock);
218        })
219      }
220    },
221    upload: function (...args) {
222      console.warn("ohos.request.upload interface mocked in the Previewer. How this interface works on the Previewer" +
223        " may be different from that on a real device.")
224      const len = args.length
225      if (typeof args[len - 1] === 'function') {
226        args[len - 1].call(this, paramMock.businessErrorMock, UploadTaskMock);
227      } else {
228        return new Promise((resolve, reject) => {
229          resolve(UploadTaskMock);
230        })
231      }
232    }
233  }
234
235  for (let property of Properties) {
236    global.ohosplugin.request[property] = NOTIFY + " " + property
237  }
238}
239