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" 17 18export function mockWebSocket() { 19 global.systemplugin.net = {} 20 const WebSocketMock = { 21 connect: function (...args) { 22 console.warn("WebSocket.connect interface mocked in the Previewer. How this interface works on the Previewer" + 23 " may be different from that on a real device.") 24 const len = args.length 25 if (typeof args[len - 1] === 'function') { 26 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 27 } else { 28 return new Promise((resolve, reject) => { 29 resolve(paramMock.paramBooleanMock); 30 }) 31 } 32 }, 33 send: function (...args) { 34 console.warn("WebSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 35 " be different from that on a real device.") 36 const len = args.length 37 if (typeof args[len - 1] === 'function') { 38 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 39 } else { 40 return new Promise((resolve, reject) => { 41 resolve(paramMock.paramBooleanMock); 42 }) 43 } 44 }, 45 close: function (...args) { 46 console.warn("WebSocket.close interface mocked in the Previewer. How this interface works on the Previewer " + 47 "may be different from that on a real device.") 48 const len = args.length 49 if (typeof args[len - 1] === 'function') { 50 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 51 } else { 52 return new Promise((resolve, reject) => { 53 resolve(paramMock.paramBooleanMock); 54 }) 55 } 56 }, 57 on: function (...args) { 58 console.warn("WebSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 59 "be different from that on a real device.") 60 const len = args.length 61 if (typeof args[len - 1] === 'function') { 62 if (args[0] === 'open') { 63 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 64 } else if (args[0] === 'message') { 65 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock); 66 } else if (args[0] === 'close') { 67 args[len - 1].call(this, paramMock.businessErrorMock, { 68 code: "[PC Preview] unknow code", 69 reason: "[PC Preview] unknow reason" 70 }); 71 } else if (args[0] === 'error') { 72 args[len - 1].call(this, paramMock.businessErrorMock); 73 } 74 } 75 }, 76 off: function (...args) { 77 console.warn("WebSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 78 " be different from that on a real device.") 79 const len = args.length 80 if (typeof args[len - 1] === 'function') { 81 if (args[0] === 'open') { 82 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 83 } else if (args[0] === 'message') { 84 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock); 85 } else if (args[0] === 'close') { 86 args[len - 1].call(this, paramMock.businessErrorMock, { 87 code: "[PC Preview] unknow code", 88 reason: "[PC Preview] unknow reason" 89 }); 90 } else if (args[0] === 'error') { 91 args[len - 1].call(this, paramMock.businessErrorMock); 92 } 93 } 94 } 95 } 96 const webSocket = { 97 createWebSocket: function () { 98 console.warn("net.webSocket.createWebSocket interface mocked in the Previewer. How this interface works on the" + 99 " Previewer may be different from that on a real device.") 100 return WebSocketMock; 101 } 102 } 103 return webSocket 104}