diff --git a/runtime/main/extend/systemplugin/napi/ohos_request.js b/runtime/main/extend/systemplugin/napi/ohos_request.js index f82bdcc6..c6bd6927 100644 --- a/runtime/main/extend/systemplugin/napi/ohos_request.js +++ b/runtime/main/extend/systemplugin/napi/ohos_request.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,38 +13,250 @@ * limitations under the License. */ -import { hasComplete } from "../utils" +import { paramMock } from "../utils" export function mockRequest() { - const uploadResponseMock = { - code: "[PC Preview]: unknow code", - data: "[PC Preview]: unknow data", - headers: "[PC Preview]: unknow headers" - } - const downloadResponse = { - token: "[PC Preview]: unknow token" - } - const onDownloadCompleteMock = { - uri: "[PC Preview]: unknow uri" - } + const DownloadConfig = { + url: "[PC Preview] unknow uri", + herder: "[PC Preview] unknow herder", + enableMetered: "[PC Preview] unknow enableMetered", + enableRoaming: "[PC Preview] unknow enableRoaming", + description: "[PC Preview] unknow description", + networkType: "[PC Preview] unknow networkType", + filePath: "[PC Preview] unknow filePath", + title: "[PC Preview] unknow title", + }; + + const DownloadInfo = { + description: "[PC Preview] unknow description", + downloadedBytes: "[PC Preview] unknow downloadedBytes", + downloadId: "[PC Preview] unknow downloadId", + failedReason: "[PC Preview] unknow failedReason", + fileName: "[PC Preview] unknow fileName", + filePath: "[PC Preview] unknow filePath", + pausedReason: "[PC Preview] unknow pausedReason", + status: "[PC Preview] unknow status", + targetURI: "[PC Preview] unknow targetURI", + downloadTitle: "[PC Preview] unknow downloadTitle", + downloadTotalBytes: "[PC Preview] unknow downloadTotalBytes", + }; + + const DownloadTask = { + on: function (...args) { + console.warn("downloadTask.on interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device."); + const len = args.length + if (typeof args[len - 1] === 'function') { + if (args[0] == 'progress') { + const receivedSize = "[PC Preview] unknow receivedSize" + const totalSize = "[PC Preview] unknow totalSize" + args[len - 1].call(this, receivedSize, totalSize) + } else if (args[0] == 'complete' | 'pause' | 'remove') { + args[len - 1].call(this) + } else if (args[0] == 'fail') { + const err = "[PC Preview] unknow err" + args[len - 1].call(this, err) + } + } + }, + + off: function (...args) { + console.warn("downloadTask.off interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device."); + const len = args.length + if (typeof args[len - 1] === 'function') { + if (args[0] == 'progress') { + const receivedSize = "[PC Preview] unknow receivedSize" + const totalSize = "[PC Preview] unknow totalSize" + args[len - 1].call(this, receivedSize, totalSize) + } else if (args[0] == 'complete' | 'pause' | 'remove') { + args[len - 1].call(this) + } else if (args[0] == 'fail') { + const err = "[PC Preview]: unknow err" + args[len - 1].call(this, err) + } + } + }, + + remove: function (...args) { + console.warn("downloadTask.remove interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock) + } else { + return new Promise((resolve) => { + resolve(paramMock.paramBooleanMock) + }) + } + }, + + pause: function (...args) { + console.warn("downloadTask.pause interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + resume: function (...args) { + console.warn("downloadTask.resume interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock) + } else { + return new Promise((resolve) => { + resolve() + }) + } + }, + + query: function (...args) { + console.warn("downloadTask.query interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, downloadInfoMock) + } else { + return new Promise((resolve) => { + resolve(downloadInfoMock) + }) + } + }, + + queryMimeType: function (...args) { + console.warn("downloadTask.queryMimeType interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock) + } else { + return new Promise((resolve) => { + resolve(paramMock.paramStringMock) + }) + } + }, + }; + + const File = { + filename: "[PC Preview] unknow filename", + name: "[PC Preview] unknow name", + uri: "[PC Preview] unknow uri", + type: "[PC Preview] unknow type", + }; + + const RequestData = { + name: "[PC Preview] unknow name", + value: "[PC Preview] unknow value", + }; + + const UploadConfig = { + url: "[PC Preview] unknow uri", + herder: "[PC Preview] unknow herder", + method: "[PC Preview] unknow method", + files: [File], + data: [RequestData], + }; + + const UploadTask = { + on: function (...args) { + console.warn("uploadTask.on interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device."); + const len = args.length + if (typeof args[len - 1] === 'function') { + if (args[0] == 'progress') { + const uploadedSize = "[PC Preview] unknow receivedSize" + const totalSize = "[PC Preview] unknow totalSize" + args[len - 1].call(this, uploadedSize, totalSize) + } else if (args[0] == 'headerReceive') { + const header = "[PC Preview] unknow header" + args[len - 1].call(this, header) + } + } + }, + + off: function (...args) { + console.warn("uploadTask.off interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device."); + const len = args.length + if (typeof args[len - 1] === 'function') { + if (args[0] == 'progress') { + const uploadedSize = "[PC Preview] unknow receivedSize" + const totalSize = "[PC Preview] unknow totalSize" + args[len - 1].call(this, uploadedSize, totalSize) + } else if (args[0] == 'headerReceive') { + const header = "[PC Preview] unknow header" + args[len - 1].call(this, header) + } + } + }, + + remove: function (...args) { + console.warn("uploadTask.remove interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock) + } else { + return new Promise((resolve) => { + resolve(paramMock.paramBooleanMock) + }) + } + }, + }; + const request = { - upload: function (...args) { - console.warn("system.request.upload interface mocked in the Previewer. How this interface works on the" + - " Previewer may be different from that on a real device.") - args[0].success(uploadResponseMock) - hasComplete(args[0].complete) - }, + NETWORK_MOBILE: '[PC preview] unknow NETWORK_MOBILE', + NETWORK_WIFI: '[PC preview] unknow NETWORK_WIFI', + ERROR_CANNOT_RESUME: '[PC preview] unknow ERROR_CANNOT_RESUME', + ERROR_DEVICE_NOT_FOUND: '[PC preview] unknow ERROR_DEVICE_NOT_FOUND', + ERROR_FILE_ALREADY_EXISTS: '[PC preview] unknow ERROR_FILE_ALREADY_EXISTS', + ERROR_FILE_ERROR: '[PC preview] unknow ERROR_FILE_ERROR', + ERROR_HTTP_DATA_ERROR: '[PC preview] unknow ERROR_HTTP_DATA_ERROR', + ERROR_INSUFFICIENT_SPACE: '[PC preview] unknow ERROR_INSUFFICIENT_SPACE', + ERROR_TOO_MANY_REDIRECTS: '[PC preview] unknow ERROR_TOO_MANY_REDIRECTS', + ERROR_UNHANDLED_HTTP_CODE: '[PC preview] unknow ERROR_UNHANDLED_HTTP_CODE', + ERROR_UNKNOWN: '[PC preview] unknow ERROR_UNKNOWN', + PAUSED_QUEUED_FOR_WIFI: '[PC preview] unknow PAUSED_QUEUED_FOR_WIFI', + PAUSED_UNKNOWN: '[PC preview] unknow PAUSED_UNKNOWN', + PAUSED_WAITING_FOR_NETWORK: '[PC preview] unknow PAUSED_WAITING_FOR_NETWORK', + PAUSED_WAITING_TO_RETRY: '[PC preview] unknow PAUSED_WAITING_TO_RETRY', + SESSION_FAILED: '[PC preview] unknow SESSION_FAILED', + SESSION_PAUSED: '[PC preview] unknow SESSION_PAUSED', + SESSION_PENDING: '[PC preview] unknow SESSION_PENDING', + SESSION_RUNNING: '[PC preview] unknow SESSION_RUNNING', + SESSION_SUCCESSFUL: '[PC preview] unknow SESSION_SUCCESSFUL', + download: function (...args) { - console.warn("system.request.download interface mocked in the Previewer. How this interface works on the" + + console.warn("request.download interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") - args[0].success(downloadResponse) - hasComplete(args[0].complete) + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, DownloadTask) + } else { + return new Promise((resolve) => { + resolve(DownloadTask); + }) + } }, - onDownloadComplete: function (...args) { - console.warn("system.request.onDownloadComplete interface mocked in the Previewer. How this interface works on" + - " the Previewer may be different from that on a real device.") - args[0].success(onDownloadCompleteMock) - hasComplete(args[0].complete) + + upload: function (...args) { + console.warn("request.upload interface mocked in the Previewer. How this interface works on the" + + " Previewer may be different from that on a real device.") + const len = args.length + if (typeof args[len - 1] === 'function') { + args[len - 1].call(this, paramMock.businessErrorMock, UploadTask) + } else { + return new Promise((resolve) => { + resolve(UploadTask) + }) + } } } return request