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 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 global.systemplugin.net.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} 104 105export function mockHttp() { 106 const HttpResponseCacheOptions = { 107 filePath: "[PC Preview] unknow filePath", 108 fileChildPath: "[PC Preview] unknow fileChildPath", 109 cacheSize: "[PC Preview] unknow cacheSize" 110 } 111 const HttpResponseCache = { 112 close: function (...args) { 113 console.warn("net.HttpResponseCache.close interface mocked in the Previewer. How this interface works on the Previewer may" + 114 " be different from that on a real device.") 115 const len = args.length 116 if (typeof args[len - 1] === 'function') { 117 args[len - 1].call(this, paramMock.businessErrorMock); 118 } else { 119 return new Promise((resolve, reject) => { 120 resolve(); 121 }) 122 } 123 }, 124 delete: function (...args) { 125 console.warn("net.HttpResponseCache.delete interface mocked in the Previewer. How this interface works on the Previewer may" + 126 " be different from that on a real device.") 127 const len = args.length 128 if (typeof args[len - 1] === 'function') { 129 args[len - 1].call(this, paramMock.businessErrorMock); 130 } else { 131 return new Promise((resolve, reject) => { 132 resolve(); 133 }) 134 } 135 }, 136 flush: function (...args) { 137 console.warn("net.HttpResponseCache.flush interface mocked in the Previewer. How this interface works on the Previewer may" + 138 " be different from that on a real device.") 139 const len = args.length 140 if (typeof args[len - 1] === 'function') { 141 args[len - 1].call(this, paramMock.businessErrorMock); 142 } else { 143 return new Promise((resolve, reject) => { 144 resolve(); 145 }) 146 } 147 } 148 } 149 const HttpResponseMock = { 150 result: "[PC Preview] unknow result", 151 responseCode: "[PC Preview] unknow responseCode", 152 header: "[PC Preview] unknow header" 153 } 154 const HttpRequestMock = { 155 request: function (...args) { 156 console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" + 157 " may be different from that on a real device.") 158 const len = args.length 159 if (typeof args[len - 1] === 'function') { 160 args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseMock); 161 } else { 162 return new Promise((resolve, reject) => { 163 resolve(HttpResponseMock); 164 }) 165 } 166 }, 167 destroy: function () { 168 console.warn("HttpRequest.destroy interface mocked in the Previewer. How this interface works on the Previewer" + 169 " may be different from that on a real device.") 170 }, 171 on: function (...args) { 172 console.warn("HttpRequest.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 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 177 } 178 }, 179 off: function (...args) { 180 console.warn("HttpRequest.off interface mocked in the Previewer. How this interface works on the Previewer may" + 181 " be different from that on a real device.") 182 const len = args.length 183 if (typeof args[len - 1] === 'function') { 184 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 185 } 186 } 187 } 188 global.systemplugin.net.http = { 189 createHttp: function () { 190 console.warn("net.http.createHttp interface mocked in the Previewer. How this interface works on the Previewer" + 191 " may be different from that on a real device.") 192 return HttpRequestMock; 193 }, 194 getInstalledHttpResponseCache: function (...args) { 195 console.warn("net.http.getInstalledHttpResponseCache interface mocked in the Previewer. How this interface works on the Previewer may" + 196 " be different from that on a real device.") 197 const len = args.length 198 if (typeof args[len - 1] === 'function') { 199 args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseCache); 200 } else { 201 return new Promise((resolve, reject) => { 202 resolve(HttpResponseCache); 203 }) 204 } 205 }, 206 createHttpResponseCache: function (...args) { 207 console.warn("net.http.createHttpResponseCache interface mocked in the Previewer. How this interface works on the Previewer may" + 208 " be different from that on a real device.") 209 const len = args.length 210 if (typeof args[len - 1] === 'function') { 211 args[len - 1].call(this, paramMock.businessErrorMock, HttpResponseCacheOptions); 212 } else { 213 return new Promise((resolve, reject) => { 214 resolve(HttpResponseCache); 215 }) 216 } 217 } 218 } 219} 220 221export function mockMdns() { 222 const localServiceInfoMock = { 223 serviceType: "[PC Preview] unknow serviceType", 224 serviceName: "[PC Preview] unknow serviceName", 225 host: "[PC Preview] unknow host", 226 port: "[PC Preview] unknow port" 227 } 228 global.systemplugin.net.mdns = { 229 on: function (...args) { 230 console.warn("net.mdns.on interface mocked in the Previewer. How this interface works on the Previewer may " + 231 "be different from that on a real device.") 232 const len = args.length 233 if (typeof args[len - 1] === 'function') { 234 if (args[0] === 'serviceAdd') { 235 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 236 } else if (args[0] === 'serviceRemove') { 237 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 238 } else if (args[0] === 'serviceStart') { 239 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 240 } else if (args[0] === 'serviceStop') { 241 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 242 } else if (args[0] === 'serviceFound') { 243 args[len - 1].call(this, localServiceInfoMock); 244 } else if (args[0] === 'serviceLost') { 245 args[len - 1].call(this, localServiceInfoMock); 246 } else if (args[0] === 'serviceResolve') { 247 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 248 } else if (args[0] === 'serviceLost') { 249 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 250 } else if (args[0] === 'error') { 251 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 252 } else { 253 args[len - 1].call(this); 254 } 255 } 256 }, 257 off: function (...args) { 258 console.warn("net.mdns.off interface mocked in the Previewer. How this interface works on the Previewer may " + 259 "be different from that on a real device.") 260 const len = args.length 261 if (typeof args[len - 1] === 'function') { 262 if (args[0] === 'serviceAdd') { 263 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 264 } else if (args[0] === 'serviceRemove') { 265 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 266 } else if (args[0] === 'serviceStart') { 267 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 268 } else if (args[0] === 'serviceStop') { 269 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 270 } else if (args[0] === 'serviceFound') { 271 args[len - 1].call(this, localServiceInfoMock); 272 } else if (args[0] === 'serviceLost') { 273 args[len - 1].call(this, localServiceInfoMock); 274 } else if (args[0] === 'serviceResolve') { 275 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 276 } else if (args[0] === 'serviceLost') { 277 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 278 } else if (args[0] === 'error') { 279 args[len - 1].call(this, { serviceInfo: localServiceInfoMock, errorCode: 0 }); 280 } else { 281 args[len - 1].call(this); 282 } 283 } 284 }, 285 addLocalService: function (...args) { 286 console.warn("net.mdns.addLocalService interface mocked in the Previewer. How this interface works on the Previewer may" + 287 " be different from that on a real device.") 288 const len = args.length 289 if (typeof args[len - 1] === 'function') { 290 args[len - 1].call(this, paramMock.businessErrorMock); 291 } else { 292 return new Promise((resolve, reject) => { 293 resolve(); 294 }) 295 } 296 }, 297 removeLocalService: function (...args) { 298 console.warn("net.mdns.removeLocalService interface mocked in the Previewer. How this interface works on the Previewer may" + 299 " be different from that on a real device.") 300 const len = args.length 301 if (typeof args[len - 1] === 'function') { 302 args[len - 1].call(this, paramMock.businessErrorMock); 303 } else { 304 return new Promise((resolve, reject) => { 305 resolve(); 306 }) 307 } 308 }, 309 startSearchingMDNS: function (...args) { 310 console.warn("net.mdns.startSearchingMDNS interface mocked in the Previewer. How this interface works on the Previewer may" + 311 " be different from that on a real device.") 312 const len = args.length 313 if (typeof args[len - 1] === 'function') { 314 args[len - 1].call(this, paramMock.businessErrorMock); 315 } else { 316 return new Promise((resolve, reject) => { 317 resolve(); 318 }) 319 } 320 }, 321 stopSearchingMDNS: function (...args) { 322 console.warn("net.mdns.stopSearchingMDNS interface mocked in the Previewer. How this interface works on the Previewer may" + 323 " be different from that on a real device.") 324 const len = args.length 325 if (typeof args[len - 1] === 'function') { 326 args[len - 1].call(this, paramMock.businessErrorMock); 327 } else { 328 return new Promise((resolve, reject) => { 329 resolve(); 330 }) 331 } 332 } 333 } 334} 335 336export function mockSocket() { 337 const SocketStateBase = { 338 isBound: "[PC Preview] unknow isBound", 339 isClose: "[PC Preview] unknow isClose", 340 isConnected: "[PC Preview] unknow isConnected" 341 } 342 const SocketRemoteInfo = { 343 address: "[PC Preview] unknow address", 344 family: "[PC Preview] unknow family", 345 port: "[PC Preview] unknow port", 346 size: "[PC Preview] unknow size" 347 } 348 const NetAddress = { 349 address: "[PC Preview] unknow saddressize", 350 family: "[PC Preview] unknow family", 351 port: "[PC Preview] unknow port " 352 } 353 const UDPSocket = { 354 bind: function (...args) { 355 console.warn("UDPSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" + 356 " may be different from that on a real device.") 357 const len = args.length 358 if (typeof args[len - 1] === 'function') { 359 args[len - 1].call(this, paramMock.businessErrorMock) 360 } else { 361 return new Promise((resolve, reject) => { 362 resolve(); 363 }) 364 } 365 }, 366 send: function (...args) { 367 console.warn("UDPSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 368 " be different from that on a real device.") 369 const len = args.length 370 if (typeof args[len - 1] === 'function') { 371 args[len - 1].call(this, paramMock.businessErrorMock) 372 } else { 373 return new Promise((resolve, reject) => { 374 resolve(); 375 }) 376 } 377 }, 378 close: function (...args) { 379 console.warn("UDPSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" + 380 " be different from that on a real device.") 381 const len = args.length 382 if (typeof args[len - 1] === 'function') { 383 args[len - 1].call(this, paramMock.businessErrorMock) 384 } else { 385 return new Promise((resolve, reject) => { 386 resolve(); 387 }) 388 } 389 }, 390 getState: function (...args) { 391 console.warn("UDPSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" + 392 " be different from that on a real device.") 393 const len = args.length 394 if (typeof args[len - 1] === 'function') { 395 args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase) 396 } else { 397 return new Promise((resolve, reject) => { 398 resolve(SocketStateBase); 399 }) 400 } 401 }, 402 setExtraOptions: function (...args) { 403 console.warn("UDPSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" + 404 " be different from that on a real device.") 405 const len = args.length 406 if (typeof args[len - 1] === 'function') { 407 args[len - 1].call(this, paramMock.businessErrorMock) 408 } else { 409 return new Promise((resolve, reject) => { 410 resolve(); 411 }) 412 } 413 }, 414 on: function (...args) { 415 console.warn("UDPSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 416 "be different from that on a real device.") 417 const len = args.length 418 if (typeof args[len - 1] === 'function') { 419 if (args[0] === 'message') { 420 args[len - 1].call(this, { 421 message: "[PC Preview] unknow message", 422 remoteInfo: SocketRemoteInfo 423 }); 424 } else if (args[0] === 'listening') { 425 args[len - 1].call(this, paramMock.businessErrorMock); 426 } else if (args[0] === 'error') { 427 args[len - 1].call(this, paramMock.businessErrorMock); 428 } 429 } 430 }, 431 off: function (...args) { 432 console.warn("UDPSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 433 " be different from that on a real device.") 434 const len = args.length 435 if (typeof args[len - 1] === 'function') { 436 if (args[0] === 'message') { 437 args[len - 1].call(this, { 438 message: "[PC Preview] unknow message", 439 remoteInfo: SocketRemoteInfo 440 }); 441 } else if (args[0] === 'listening') { 442 args[len - 1].call(this, paramMock.businessErrorMock); 443 } else if (args[0] === 'error') { 444 args[len - 1].call(this, paramMock.businessErrorMock); 445 } 446 } 447 } 448 } 449 450 const TCPSocket = { 451 bind: function (...args) { 452 console.warn("TcpSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" + 453 " may be different from that on a real device.") 454 const len = args.length 455 if (typeof args[len - 1] === 'function') { 456 args[len - 1].call(this, paramMock.businessErrorMock) 457 } else { 458 return new Promise((resolve, reject) => { 459 resolve(); 460 }) 461 } 462 }, 463 connect: function (...args) { 464 console.warn("TcpSocket.connect interface mocked in the Previewer. How this interface works on the Previewer" + 465 " may be different from that on a real device.") 466 const len = args.length 467 if (typeof args[len - 1] === 'function') { 468 args[len - 1].call(this, paramMock.businessErrorMock) 469 } else { 470 return new Promise((resolve, reject) => { 471 resolve(); 472 }) 473 } 474 }, 475 send: function (...args) { 476 console.warn("TcpSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" + 477 " be different from that on a real device.") 478 const len = args.length 479 if (typeof args[len - 1] === 'function') { 480 args[len - 1].call(this, paramMock.businessErrorMock) 481 } else { 482 return new Promise((resolve, reject) => { 483 resolve(); 484 }) 485 } 486 }, 487 close: function (...args) { 488 console.warn("TcpSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" + 489 " be different from that on a real device.") 490 const len = args.length 491 if (typeof args[len - 1] === 'function') { 492 args[len - 1].call(this, paramMock.businessErrorMock) 493 } else { 494 return new Promise((resolve, reject) => { 495 resolve(); 496 }) 497 } 498 }, 499 getRemoteAddress: function (...args) { 500 console.warn("TcpSocket.getRemoteAddress interface mocked in the Previewer. How this interface works on the Previewer may" + 501 " be different from that on a real device.") 502 const len = args.length 503 if (typeof args[len - 1] === 'function') { 504 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress) 505 } else { 506 return new Promise((resolve, reject) => { 507 resolve(NetAddress); 508 }) 509 } 510 }, 511 getState: function (...args) { 512 console.warn("TcpSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" + 513 " be different from that on a real device.") 514 const len = args.length 515 if (typeof args[len - 1] === 'function') { 516 args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase) 517 } else { 518 return new Promise((resolve, reject) => { 519 resolve(SocketStateBase); 520 }) 521 } 522 }, 523 setExtraOptions: function (...args) { 524 console.warn("TcpSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" + 525 " be different from that on a real device.") 526 const len = args.length 527 if (typeof args[len - 1] === 'function') { 528 args[len - 1].call(this, paramMock.businessErrorMock) 529 } else { 530 return new Promise((resolve, reject) => { 531 resolve(); 532 }) 533 } 534 }, 535 on: function (...args) { 536 console.warn("TcpSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " + 537 "be different from that on a real device.") 538 const len = args.length 539 if (typeof args[len - 1] === 'function') { 540 if (args[0] === 'message') { 541 args[len - 1].call(this, { 542 message: "[PC Preview] unknow message", 543 remoteInfo: SocketRemoteInfo 544 }); 545 } else if (args[0] === 'listening') { 546 args[len - 1].call(this, paramMock.businessErrorMock); 547 } else if (args[0] === 'error') { 548 args[len - 1].call(this, paramMock.businessErrorMock); 549 } 550 } 551 }, 552 off: function (...args) { 553 console.warn("TcpSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" + 554 " be different from that on a real device.") 555 const len = args.length 556 if (typeof args[len - 1] === 'function') { 557 if (args[0] === 'message') { 558 args[len - 1].call(this, { 559 message: "[PC Preview] unknow message", 560 remoteInfo: SocketRemoteInfo 561 }); 562 } else if (args[0] === 'listening') { 563 args[len - 1].call(this, paramMock.businessErrorMock); 564 } else if (args[0] === 'error') { 565 args[len - 1].call(this, paramMock.businessErrorMock); 566 } 567 } 568 } 569 } 570 global.systemplugin.net.socket = { 571 constructUDPSocketInstance: function () { 572 console.warn("net.socket.constructUDPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + 573 " may be different from that on a real device.") 574 return UDPSocket; 575 }, 576 constructTCPSocketInstance: function () { 577 console.warn("net.socket.constructTCPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" + 578 " may be different from that on a real device.") 579 return TCPSocket; 580 } 581 } 582} 583 584export function mockHotspot() { 585 global.systemplugin.net.hotspot = { 586 isHotspotSupported: function (...args) { 587 console.warn("net.hotspot.isHotspotSupported interface mocked in the Previewer. How this interface works on the Previewer" + 588 " may be different from that on a real device.") 589 const len = args.length 590 if (typeof args[len - 1] === 'function') { 591 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 592 } else { 593 return new Promise((resolve, reject) => { 594 resolve(paramMock.paramBooleanMock); 595 }) 596 } 597 }, 598 stopUsbHotspot: function (...args) { 599 console.warn("net.hotspot.stopUsbHotspot interface mocked in the Previewer. How this interface works on the Previewer" + 600 " may be different from that on a real device.") 601 const len = args.length 602 if (typeof args[len - 1] === 'function') { 603 args[len - 1].call(this, paramMock.businessErrorMock); 604 } else { 605 return new Promise((resolve, reject) => { 606 resolve(); 607 }) 608 } 609 }, 610 startUsbHotspot: function (...args) { 611 console.warn("net.hotspot.startUsbHotspot interface mocked in the Previewer. How this interface works on the Previewer" + 612 " may be different from that on a real device.") 613 const len = args.length 614 if (typeof args[len - 1] === 'function') { 615 args[len - 1].call(this, paramMock.businessErrorMock); 616 } else { 617 return new Promise((resolve, reject) => { 618 resolve(); 619 }) 620 } 621 }, 622 getRxBytes: function (...args) { 623 console.warn("net.hotspot.getRxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 624 " may be different from that on a real device.") 625 const len = args.length 626 if (typeof args[len - 1] === 'function') { 627 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 628 } else { 629 return new Promise((resolve, reject) => { 630 resolve(paramMock.paramNumberMock); 631 }) 632 } 633 }, 634 getTxBytes: function (...args) { 635 console.warn("net.hotspot.getTxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 636 " may be different from that on a real device.") 637 const len = args.length 638 if (typeof args[len - 1] === 'function') { 639 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 640 } else { 641 return new Promise((resolve, reject) => { 642 resolve(paramMock.paramNumberMock); 643 }) 644 } 645 }, 646 getNetHotspotIfaces: function (...args) { 647 console.warn("net.hotspot.getNetHotspotIfaces interface mocked in the Previewer. How this interface works on the Previewer" + 648 " may be different from that on a real device.") 649 var stringArray = new Array(); 650 stringArray.push(paramMock.paramStringMock); 651 const len = args.length 652 if (typeof args[len - 1] === 'function') { 653 args[len - 1].call(this, paramMock.businessErrorMock, stringArray); 654 } else { 655 return new Promise((resolve, reject) => { 656 resolve(stringArray); 657 }) 658 } 659 }, 660 } 661} 662export function mockStatistics() { 663 global.systemplugin.net.statistics = { 664 getIfaceRxBytes: function (...args) { 665 console.warn("net.statistics.getIfaceRxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 666 " may be different from that on a real device.") 667 const len = args.length 668 if (typeof args[len - 1] === 'function') { 669 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 670 } else { 671 return new Promise((resolve, reject) => { 672 resolve(paramMock.paramNumberMock); 673 }) 674 } 675 }, 676 getIfaceTxBytes: function (...args) { 677 console.warn("net.statistics.getIfaceTxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 678 " may be different from that on a real device.") 679 const len = args.length 680 if (typeof args[len - 1] === 'function') { 681 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 682 } else { 683 return new Promise((resolve, reject) => { 684 resolve(paramMock.paramNumberMock); 685 }) 686 } 687 }, 688 getCellularRxBytes: function (...args) { 689 console.warn("net.statistics.getCellularRxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 690 " may be different from that on a real device.") 691 const len = args.length 692 if (typeof args[len - 1] === 'function') { 693 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 694 } else { 695 return new Promise((resolve, reject) => { 696 resolve(paramMock.paramNumberMock); 697 }) 698 } 699 }, 700 getCellularTxBytes: function (...args) { 701 console.warn("net.statistics.getCellularTxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 702 " may be different from that on a real device.") 703 const len = args.length 704 if (typeof args[len - 1] === 'function') { 705 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 706 } else { 707 return new Promise((resolve, reject) => { 708 resolve(paramMock.paramNumberMock); 709 }) 710 } 711 }, 712 getAllTxBytes: function (...args) { 713 console.warn("net.statistics.getAllTxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 714 " may be different from that on a real device.") 715 const len = args.length 716 if (typeof args[len - 1] === 'function') { 717 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 718 } else { 719 return new Promise((resolve, reject) => { 720 resolve(paramMock.paramNumberMock); 721 }) 722 } 723 }, 724 getAllRxBytes: function (...args) { 725 console.warn("net.statistics.getAllRxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 726 " may be different from that on a real device.") 727 const len = args.length 728 if (typeof args[len - 1] === 'function') { 729 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 730 } else { 731 return new Promise((resolve, reject) => { 732 resolve(paramMock.paramNumberMock); 733 }) 734 } 735 }, 736 getUidRxBytes: function (...args) { 737 console.warn("net.statistics.getUidRxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 738 " may be different from that on a real device.") 739 const len = args.length 740 if (typeof args[len - 1] === 'function') { 741 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 742 } else { 743 return new Promise((resolve, reject) => { 744 resolve(paramMock.paramNumberMock); 745 }) 746 } 747 }, 748 getUidTxBytes: function (...args) { 749 console.warn("net.statistics.getUidTxBytes interface mocked in the Previewer. How this interface works on the Previewer" + 750 " may be different from that on a real device.") 751 const len = args.length 752 if (typeof args[len - 1] === 'function') { 753 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 754 } else { 755 return new Promise((resolve, reject) => { 756 resolve(paramMock.paramNumberMock); 757 }) 758 } 759 }, 760 } 761} 762 763const HttpResponse = { 764 result: "[PC Preview] unknow result", 765 responseCode: "[PC Preview] unknow responseCode", 766 header: "[PC Preview] unknow header", 767 cookies: "[PC Preview] unknow cookies" 768}; 769const HttpRequest = { 770 request: function () { 771 console.warn("HttpRequest.request interface mocked in the Previewer. How this interface works on the Previewer" + 772 " may be different from that on a real device.") 773 const len = args.length 774 if (typeof args[len - 1] === 'function') { 775 args[len - 1].call(this, paramMock.businessErrorMock, HttpResponse); 776 } else { 777 return new Promise((resolve, reject) => { 778 resolve(HttpResponse); 779 }) 780 } 781 }, 782 destroy: function () { 783 console.warn("HttpRequest.destroy interface mocked in the Previewer. How this interface works on the Previewer" + 784 " may be different from that on a real device.") 785 }, 786 on: function () { 787 console.warn("HttpRequest.on interface mocked in the Previewer. How this interface works on the Previewer" + 788 " may be different from that on a real device.") 789 const len = args.length 790 if (typeof args[len - 1] === 'function' && args[0] === 'headerReceive') { 791 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 792 } else { 793 return new Promise((resolve, reject) => { 794 resolve(paramMock.paramObjectMock); 795 }) 796 } 797 }, 798 off: function () { 799 console.warn("HttpRequest.off interface mocked in the Previewer. How this interface works on the Previewer" + 800 " may be different from that on a real device.") 801 const len = args.length 802 if (typeof args[len - 1] === 'function' && args[0] === 'headerReceive') { 803 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramObjectMock); 804 } else { 805 return new Promise((resolve, reject) => { 806 resolve(paramMock.paramObjectMock); 807 }) 808 } 809 } 810}; 811export function mockConnection() { 812 const NetAddress = "[PC Preview] unknow NetAddress" 813 const NetHandle = { 814 netId: "[PC Preview] unknow netId", 815 bindSocket: function () { 816 console.warn("NetHandle.bindSocket interface mocked in the Previewer. How this interface works on the Previewer" + 817 " may be different from that on a real device.") 818 const len = args.length 819 if (typeof args[len - 1] === 'function') { 820 args[len - 1].call(this, paramMock.businessErrorMock); 821 } else { 822 return new Promise((resolve, reject) => { 823 resolve(); 824 }) 825 } 826 }, 827 openConnection: function () { 828 console.warn("NetHandle.openConnection interface mocked in the Previewer. How this interface works on the Previewer" + 829 " may be different from that on a real device.") 830 const len = args.length 831 if (typeof args[len - 1] === 'function') { 832 args[len - 1].call(this, paramMock.businessErrorMock, HttpRequest); 833 } else { 834 return new Promise((resolve, reject) => { 835 resolve(HttpRequest); 836 }) 837 } 838 }, 839 getAddressesByName: function () { 840 console.warn("NetHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + 841 " may be different from that on a real device.") 842 const len = args.length 843 if (typeof args[len - 1] === 'function') { 844 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); 845 } else { 846 return new Promise((resolve, reject) => { 847 resolve(NetAddress); 848 }) 849 } 850 }, 851 getAddressByName: function () { 852 console.warn("NetHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + 853 " may be different from that on a real device.") 854 const len = args.length 855 if (typeof args[len - 1] === 'function') { 856 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); 857 } else { 858 return new Promise((resolve, reject) => { 859 resolve(NetAddress); 860 }) 861 } 862 }, 863 } 864 const NetBearType = "[PC Preview] unknow NetBearType" 865 const NetCapabilities = { 866 bearerTypes: function () { 867 console.warn("NetCapabilities.bearerTypes interface mocked in the Previewer. How this interface works on the Previewer" + 868 " may be different from that on a real device.") 869 var netBearTypeArray = new Array(); 870 netBearTypeArray.push(NetBearType); 871 return netBearTypeArray; 872 } 873 } 874 const NetCap = "[PC Preview] unknow NetCap" 875 const ConnectionProperties = { 876 interfaceName: "[PC Preview] unknow interfaceName", 877 isUsePrivateDns: "[PC Preview] unknow isUsePrivateDns", 878 privateDnsServerName: "[PC Preview] unknow privateDnsServerName", 879 domains: "[PC Preview] unknow domains", 880 httpProxy: "[PC Preview] unknow httpProxy", 881 linkAddresses: function () { 882 console.warn("ConnectionProperties.linkAddresses interface mocked in the Previewer. How this interface works on the Previewer" + 883 " may be different from that on a real device.") 884 var LinkAddress = new Array(); 885 LinkAddress.push(NetBearType); 886 return LinkAddress; 887 }, 888 dnses: function () { 889 console.warn("ConnectionProperties.dnses interface mocked in the Previewer. How this interface works on the Previewer" + 890 " may be different from that on a real device.") 891 var NetAddress = new Array(); 892 NetAddress.push(NetBearType); 893 return NetAddress; 894 }, 895 routes: function () { 896 console.warn("ConnectionProperties.routes interface mocked in the Previewer. How this interface works on the Previewer" + 897 " may be different from that on a real device.") 898 var RouteInfo = new Array(); 899 RouteInfo.push(NetBearType); 900 return RouteInfo; 901 }, 902 mtu: "[PC Preview] unknow mtu" 903 } 904 const HttpProxy = { 905 host: "[PC Preview] unknow host", 906 port: "[PC Preview] unknow port", 907 parsedExclusionList: "[PC Preview] unknow parsedExclusionList" 908 } 909 const blocked = "[PC Preview] unknow blocked" 910 const BackgroundPolicy = "[PC Preview] unknow BackgroundPolicy" 911 global.systemplugin.net.connection = { 912 on: function (...args) { 913 console.warn("net.connection.on interface mocked in the Previewer. How this interface works on the Previewer may " + 914 "be different from that on a real device.") 915 const len = args.length 916 if (typeof args[len - 1] === 'function') { 917 if (args[0] === 'netAvailable') { 918 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 919 } else if (args[0] === 'netBlockStatusChange') { 920 args[len - 1].call(this, paramMock.businessErrorMock, { NetHandle, blocked }); 921 } else if (args[0] === 'netCapabilitiesChange') { 922 args[len - 1].call(this, { NetHandle, NetCap }); 923 } else if (args[0] === 'netConnectionPropertiesChange') { 924 args[len - 1].call(this, { NetHandle, ConnectionProperties }); 925 } else if (args[0] === 'netLosing') { 926 args[len - 1].call(this, { 927 NetHandle, 928 maxMsToLive: "[PC Preview] unknow maxMsToLive" 929 }); 930 } else if (args[0] === 'netLost') { 931 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 932 } else if (args[0] === 'netUnavailable') { 933 args[len - 1].call(this, paramMock.businessErrorMock); 934 } 935 } 936 }, 937 off: function (...args) { 938 console.warn("net.connection.off interface mocked in the Previewer. How this interface works on the Previewer may " + 939 "be different from that on a real device.") 940 const len = args.length 941 if (typeof args[len - 1] === 'function') { 942 if (args[0] === 'netAvailable') { 943 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 944 } else if (args[0] === 'netBlockStatusChange') { 945 args[len - 1].call(this, { NetHandle, blocked }); 946 } else if (args[0] === 'netCapabilitiesChange') { 947 args[len - 1].call(this, { NetHandle, NetCap }); 948 } else if (args[0] === 'netConnectionPropertiesChange') { 949 args[len - 1].call(this, { NetHandle, ConnectionProperties }); 950 } else if (args[0] === 'netLosing') { 951 args[len - 1].call(this, { 952 NetHandle, 953 maxMsToLive: "[PC Preview] unknow maxMsToLive" 954 }); 955 } else if (args[0] === 'netLost') { 956 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 957 } else if (args[0] === 'netUnavailable') { 958 args[len - 1].call(this, paramMock.businessErrorMock); 959 } 960 } 961 }, 962 addNetStatusCallback: function (...args) { 963 console.warn("net.connection.addNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + 964 " may be different from that on a real device.") 965 const len = args.length 966 if (typeof args[len - 1] === 'function') { 967 args[len - 1].call(this, paramMock.businessErrorMock); 968 } else { 969 return new Promise((resolve, reject) => { 970 resolve(); 971 }) 972 } 973 }, 974 removeNetStatusCallback: function (...args) { 975 console.warn("net.connection.removeNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + 976 " may be different from that on a real device.") 977 const len = args.length 978 if (typeof args[len - 1] === 'function') { 979 args[len - 1].call(this, paramMock.businessErrorMock); 980 } else { 981 return new Promise((resolve, reject) => { 982 resolve(); 983 }) 984 } 985 }, 986 getAppNet: function (...args) { 987 console.warn("net.connection.getAppNet interface mocked in the Previewer. How this interface works on the Previewer" + 988 " may be different from that on a real device.") 989 const len = args.length 990 if (typeof args[len - 1] === 'function') { 991 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 992 } else { 993 return new Promise((resolve, reject) => { 994 resolve(NetHandle); 995 }) 996 } 997 }, 998 setAppNet: function (...args) { 999 console.warn("net.connection.setAppNet interface mocked in the Previewer. How this interface works on the Previewer" + 1000 " may be different from that on a real device.") 1001 const len = args.length 1002 if (typeof args[len - 1] === 'function') { 1003 args[len - 1].call(this, paramMock.businessErrorMock); 1004 } else { 1005 return new Promise((resolve, reject) => { 1006 resolve(); 1007 }) 1008 } 1009 }, 1010 getDefaultNet: function (...args) { 1011 console.warn("net.connection.getDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 1012 " may be different from that on a real device.") 1013 const len = args.length 1014 if (typeof args[len - 1] === 'function') { 1015 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 1016 } else { 1017 return new Promise((resolve, reject) => { 1018 resolve(NetHandle); 1019 }) 1020 } 1021 }, 1022 getAllNets: function (...args) { 1023 console.warn("net.connection.getAllNets interface mocked in the Previewer. How this interface works on the Previewer" + 1024 " may be different from that on a real device.") 1025 const len = args.length 1026 if (typeof args[len - 1] === 'function') { 1027 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 1028 } else { 1029 return new Promise((resolve, reject) => { 1030 resolve(NetHandle); 1031 }) 1032 } 1033 }, 1034 getDefaultHttpProxy: function (...args) { 1035 console.warn("net.connection.getDefaultHttpProxy interface mocked in the Previewer. How this interface works on the Previewer" + 1036 " may be different from that on a real device.") 1037 const len = args.length 1038 if (typeof args[len - 1] === 'function') { 1039 args[len - 1].call(this, paramMock.businessErrorMock, HttpProxy); 1040 } else { 1041 return new Promise((resolve, reject) => { 1042 resolve(HttpProxy); 1043 }) 1044 } 1045 }, 1046 getConnectionProperties: function (...args) { 1047 console.warn("net.connection.getConnectionProperties interface mocked in the Previewer. How this interface works on the Previewer" + 1048 " may be different from that on a real device.") 1049 const len = args.length 1050 if (typeof args[len - 1] === 'function') { 1051 args[len - 1].call(this, paramMock.businessErrorMock, ConnectionProperties); 1052 } else { 1053 return new Promise((resolve, reject) => { 1054 resolve(ConnectionProperties); 1055 }) 1056 } 1057 }, 1058 getNetCapabilities: function (...args) { 1059 console.warn("net.connection.getNetCapabilities interface mocked in the Previewer. How this interface works on the Previewer" + 1060 " may be different from that on a real device.") 1061 const len = args.length 1062 if (typeof args[len - 1] === 'function') { 1063 args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilities); 1064 } else { 1065 return new Promise((resolve, reject) => { 1066 resolve(NetCapabilities); 1067 }) 1068 } 1069 }, 1070 getBackgroundPolicy: function (...args) { 1071 console.warn("net.connection.getBackgroundPolicy interface mocked in the Previewer. How this interface works on the Previewer" + 1072 " may be different from that on a real device.") 1073 const len = args.length 1074 if (typeof args[len - 1] === 'function') { 1075 args[len - 1].call(this, paramMock.businessErrorMock, BackgroundPolicy); 1076 } else { 1077 return new Promise((resolve, reject) => { 1078 resolve(BackgroundPolicy); 1079 }) 1080 } 1081 }, 1082 isDefaultNetMetered: function (...args) { 1083 console.warn("net.connection.isDefaultNetMetered interface mocked in the Previewer. How this interface works on the Previewer" + 1084 " may be different from that on a real device.") 1085 const len = args.length 1086 if (typeof args[len - 1] === 'function') { 1087 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 1088 } else { 1089 return new Promise((resolve, reject) => { 1090 resolve(paramMock.paramBooleanMock); 1091 }) 1092 } 1093 }, 1094 hasDefaultNet: function (...args) { 1095 console.warn("net.connection.hasDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 1096 " may be different from that on a real device.") 1097 const len = args.length 1098 if (typeof args[len - 1] === 'function') { 1099 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 1100 } else { 1101 return new Promise((resolve, reject) => { 1102 resolve(paramMock.paramBooleanMock); 1103 }) 1104 } 1105 }, 1106 enableAirplaneMode: function (...args) { 1107 console.warn("net.connection.enableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 1108 " may be different from that on a real device.") 1109 const len = args.length 1110 if (typeof args[len - 1] === 'function') { 1111 args[len - 1].call(this, paramMock.businessErrorMock); 1112 } else { 1113 return new Promise((resolve, reject) => { 1114 resolve(); 1115 }) 1116 } 1117 }, 1118 disableAirplaneMode: function (...args) { 1119 console.warn("net.connection.disableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 1120 " may be different from that on a real device.") 1121 const len = args.length 1122 if (typeof args[len - 1] === 'function') { 1123 args[len - 1].call(this, paramMock.businessErrorMock); 1124 } else { 1125 return new Promise((resolve, reject) => { 1126 resolve(); 1127 }) 1128 } 1129 }, 1130 enableDistributedCellularData: function (...args) { 1131 console.warn("net.connection.enableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + 1132 " may be different from that on a real device.") 1133 const len = args.length 1134 if (typeof args[len - 1] === 'function') { 1135 args[len - 1].call(this, paramMock.businessErrorMock); 1136 } else { 1137 return new Promise((resolve, reject) => { 1138 resolve(); 1139 }) 1140 } 1141 }, 1142 disableDistributedCellularData: function (...args) { 1143 console.warn("net.connection.disableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + 1144 " may be different from that on a real device.") 1145 const len = args.length 1146 if (typeof args[len - 1] === 'function') { 1147 args[len - 1].call(this, paramMock.businessErrorMock); 1148 } else { 1149 return new Promise((resolve, reject) => { 1150 resolve(); 1151 }) 1152 } 1153 }, 1154 reportNetConnected: function (...args) { 1155 console.warn("net.connection.reportNetConnected interface mocked in the Previewer. How this interface works on the Previewer" + 1156 " may be different from that on a real device.") 1157 const len = args.length 1158 if (typeof args[len - 1] === 'function') { 1159 args[len - 1].call(this, paramMock.businessErrorMock); 1160 } else { 1161 return new Promise((resolve, reject) => { 1162 resolve(); 1163 }) 1164 } 1165 }, 1166 reportNetDisconnected: function (...args) { 1167 console.warn("net.connection.reportNetDisconnected interface mocked in the Previewer. How this interface works on the Previewer" + 1168 " may be different from that on a real device.") 1169 const len = args.length 1170 if (typeof args[len - 1] === 'function') { 1171 args[len - 1].call(this, paramMock.businessErrorMock); 1172 } else { 1173 return new Promise((resolve, reject) => { 1174 resolve(); 1175 }) 1176 } 1177 }, 1178 } 1179}