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