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 mockSocket() { 19 const SocketStateBase = { 20 isBound: "[PC Preview] unknow isBound", 21 isClose: "[PC Preview] unknow isClose", 22 isConnected: "[PC Preview] unknow isConnected" 23 } 24 const SocketRemoteInfo = { 25 address: "[PC Preview] unknow address", 26 family: "[PC Preview] unknow family", 27 port: "[PC Preview] unknow port", 28 size: "[PC Preview] unknow size" 29 } 30 const NetAddress = { 31 address: "[PC Preview] unknow saddressize", 32 family: "[PC Preview] unknow family", 33 port: "[PC Preview] unknow port " 34 } 35 const UDPSocket = { 36 bind: function (...args) { 37 console.warn("UDPSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" + 38 " may be different from that on a real device.") 39 const len = args.length 40 if (typeof args[len - 1] === 'function') { 41 args[len - 1].call(this, paramMock.businessErrorMock) 42 } else { 43 return new Promise((resolve, reject) => { 44 resolve(); 45 }) 46 } 47 }, 48 send: function (...args) { 49 console.warn("UDPSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 50 " be different from that on a real device.") 51 const len = args.length 52 if (typeof args[len - 1] === 'function') { 53 args[len - 1].call(this, paramMock.businessErrorMock) 54 } else { 55 return new Promise((resolve, reject) => { 56 resolve(); 57 }) 58 } 59 }, 60 close: function (...args) { 61 console.warn("UDPSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" + 62 " be different from that on a real device.") 63 const len = args.length 64 if (typeof args[len - 1] === 'function') { 65 args[len - 1].call(this, paramMock.businessErrorMock) 66 } else { 67 return new Promise((resolve, reject) => { 68 resolve(); 69 }) 70 } 71 }, 72 getState: function (...args) { 73 console.warn("UDPSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" + 74 " be different from that on a real device.") 75 const len = args.length 76 if (typeof args[len - 1] === 'function') { 77 args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase) 78 } else { 79 return new Promise((resolve, reject) => { 80 resolve(SocketStateBase); 81 }) 82 } 83 }, 84 setExtraOptions: function (...args) { 85 console.warn("UDPSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" + 86 " be different from that on a real device.") 87 const len = args.length 88 if (typeof args[len - 1] === 'function') { 89 args[len - 1].call(this, paramMock.businessErrorMock) 90 } else { 91 return new Promise((resolve, reject) => { 92 resolve(); 93 }) 94 } 95 }, 96 on: function (...args) { 97 console.warn("UDPSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 98 "be different from that on a real device.") 99 const len = args.length 100 if (typeof args[len - 1] === 'function') { 101 if (args[0] === 'message') { 102 args[len - 1].call(this, { 103 message: "[PC Preview] unknow message", 104 remoteInfo: SocketRemoteInfo 105 }); 106 } else if (args[0] === 'listening') { 107 args[len - 1].call(this, paramMock.businessErrorMock); 108 } else if (args[0] === 'error') { 109 args[len - 1].call(this, paramMock.businessErrorMock); 110 } 111 } 112 }, 113 off: function (...args) { 114 console.warn("UDPSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 115 " be different from that on a real device.") 116 const len = args.length 117 if (typeof args[len - 1] === 'function') { 118 if (args[0] === 'message') { 119 args[len - 1].call(this, { 120 message: "[PC Preview] unknow message", 121 remoteInfo: SocketRemoteInfo 122 }); 123 } else if (args[0] === 'listening') { 124 args[len - 1].call(this, paramMock.businessErrorMock); 125 } else if (args[0] === 'error') { 126 args[len - 1].call(this, paramMock.businessErrorMock); 127 } 128 } 129 } 130 } 131 132 const TCPSocket = { 133 bind: function (...args) { 134 console.warn("TcpSocket.bind 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 connect: function (...args) { 146 console.warn("TcpSocket.connect 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) 151 } else { 152 return new Promise((resolve, reject) => { 153 resolve(); 154 }) 155 } 156 }, 157 send: function (...args) { 158 console.warn("TcpSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 159 " 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) 163 } else { 164 return new Promise((resolve, reject) => { 165 resolve(); 166 }) 167 } 168 }, 169 close: function (...args) { 170 console.warn("TcpSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" + 171 " be different from that on a real device.") 172 const len = args.length 173 if (typeof args[len - 1] === 'function') { 174 args[len - 1].call(this, paramMock.businessErrorMock) 175 } else { 176 return new Promise((resolve, reject) => { 177 resolve(); 178 }) 179 } 180 }, 181 getRemoteAddress: function (...args) { 182 console.warn("TcpSocket.getRemoteAddress interface mocked in the Previewer. How this interface works on the Previewer may" + 183 " be different from that on a real device.") 184 const len = args.length 185 if (typeof args[len - 1] === 'function') { 186 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress) 187 } else { 188 return new Promise((resolve, reject) => { 189 resolve(NetAddress); 190 }) 191 } 192 }, 193 getState: function (...args) { 194 console.warn("TcpSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" + 195 " be different from that on a real device.") 196 const len = args.length 197 if (typeof args[len - 1] === 'function') { 198 args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase) 199 } else { 200 return new Promise((resolve, reject) => { 201 resolve(SocketStateBase); 202 }) 203 } 204 }, 205 setExtraOptions: function (...args) { 206 console.warn("TcpSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" + 207 " be different from that on a real device.") 208 const len = args.length 209 if (typeof args[len - 1] === 'function') { 210 args[len - 1].call(this, paramMock.businessErrorMock) 211 } else { 212 return new Promise((resolve, reject) => { 213 resolve(); 214 }) 215 } 216 }, 217 on: function (...args) { 218 console.warn("TcpSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 219 "be different from that on a real device.") 220 const len = args.length 221 if (typeof args[len - 1] === 'function') { 222 if (args[0] === 'message') { 223 args[len - 1].call(this, { 224 message: "[PC Preview] unknow message", 225 remoteInfo: SocketRemoteInfo 226 }); 227 } else if (args[0] === 'listening') { 228 args[len - 1].call(this, paramMock.businessErrorMock); 229 } else if (args[0] === 'error') { 230 args[len - 1].call(this, paramMock.businessErrorMock); 231 } 232 } 233 }, 234 off: function (...args) { 235 console.warn("TcpSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 236 " be different from that on a real device.") 237 const len = args.length 238 if (typeof args[len - 1] === 'function') { 239 if (args[0] === 'message') { 240 args[len - 1].call(this, { 241 message: "[PC Preview] unknow message", 242 remoteInfo: SocketRemoteInfo 243 }); 244 } else if (args[0] === 'listening') { 245 args[len - 1].call(this, paramMock.businessErrorMock); 246 } else if (args[0] === 'error') { 247 args[len - 1].call(this, paramMock.businessErrorMock); 248 } 249 } 250 } 251 } 252 const socket = { 253 constructUDPSocketInstance: function () { 254 console.warn("net.socket.constructUDPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + 255 " may be different from that on a real device.") 256 return UDPSocket; 257 }, 258 constructTCPSocketInstance: function () { 259 console.warn("net.socket.constructTCPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + 260 " may be different from that on a real device.") 261 return TCPSocket; 262 } 263 } 264 return socket 265}