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 mockConnection() { 19 const NetAddress = "[PC Preview] unknow NetAddress" 20 const NetHandle = { 21 netId: "[PC Preview] unknow netId", 22 bindSocket: function () { 23 console.warn("NetHandle.bindSocket interface mocked in the Previewer. How this interface works on the Previewer" + 24 " may be different from that on a real device.") 25 const len = args.length 26 if (typeof args[len - 1] === 'function') { 27 args[len - 1].call(this, paramMock.businessErrorMock); 28 } else { 29 return new Promise((resolve, reject) => { 30 resolve(); 31 }) 32 } 33 }, 34 openConnection: function () { 35 console.warn("NetHandle.openConnection interface mocked in the Previewer. How this interface works on the Previewer" + 36 " may be different from that on a real device.") 37 const len = args.length 38 if (typeof args[len - 1] === 'function') { 39 args[len - 1].call(this, paramMock.businessErrorMock, HttpRequest); 40 } else { 41 return new Promise((resolve, reject) => { 42 resolve(HttpRequest); 43 }) 44 } 45 }, 46 getAddressesByName: function () { 47 console.warn("NetHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + 48 " may be different from that on a real device.") 49 const len = args.length 50 if (typeof args[len - 1] === 'function') { 51 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); 52 } else { 53 return new Promise((resolve, reject) => { 54 resolve(NetAddress); 55 }) 56 } 57 }, 58 getAddressByName: function () { 59 console.warn("NetHandle.getAddressByName interface mocked in the Previewer. How this interface works on the Previewer" + 60 " may be different from that on a real device.") 61 const len = args.length 62 if (typeof args[len - 1] === 'function') { 63 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress); 64 } else { 65 return new Promise((resolve, reject) => { 66 resolve(NetAddress); 67 }) 68 } 69 }, 70 } 71 const NetBearType = "[PC Preview] unknow NetBearType" 72 const NetCapabilities = { 73 bearerTypes: function () { 74 console.warn("NetCapabilities.bearerTypes interface mocked in the Previewer. How this interface works on the Previewer" + 75 " may be different from that on a real device.") 76 var netBearTypeArray = new Array(); 77 netBearTypeArray.push(NetBearType); 78 return netBearTypeArray; 79 } 80 } 81 const NetCap = "[PC Preview] unknow NetCap" 82 const ConnectionProperties = { 83 interfaceName: "[PC Preview] unknow interfaceName", 84 isUsePrivateDns: "[PC Preview] unknow isUsePrivateDns", 85 privateDnsServerName: "[PC Preview] unknow privateDnsServerName", 86 domains: "[PC Preview] unknow domains", 87 httpProxy: "[PC Preview] unknow httpProxy", 88 linkAddresses: function () { 89 console.warn("ConnectionProperties.linkAddresses interface mocked in the Previewer. How this interface works on the Previewer" + 90 " may be different from that on a real device.") 91 var LinkAddress = new Array(); 92 LinkAddress.push(NetBearType); 93 return LinkAddress; 94 }, 95 dnses: function () { 96 console.warn("ConnectionProperties.dnses interface mocked in the Previewer. How this interface works on the Previewer" + 97 " may be different from that on a real device.") 98 var NetAddress = new Array(); 99 NetAddress.push(NetBearType); 100 return NetAddress; 101 }, 102 routes: function () { 103 console.warn("ConnectionProperties.routes interface mocked in the Previewer. How this interface works on the Previewer" + 104 " may be different from that on a real device.") 105 var RouteInfo = new Array(); 106 RouteInfo.push(NetBearType); 107 return RouteInfo; 108 }, 109 mtu: "[PC Preview] unknow mtu" 110 } 111 const HttpProxy = { 112 host: "[PC Preview] unknow host", 113 port: "[PC Preview] unknow port", 114 parsedExclusionList: "[PC Preview] unknow parsedExclusionList" 115 } 116 const blocked = "[PC Preview] unknow blocked" 117 const BackgroundPolicy = "[PC Preview] unknow BackgroundPolicy" 118 const connection = { 119 on: function (...args) { 120 console.warn("net.connection.on interface mocked in the Previewer. How this interface works on the Previewer may " + 121 "be different from that on a real device.") 122 const len = args.length 123 if (typeof args[len - 1] === 'function') { 124 if (args[0] === 'netAvailable') { 125 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 126 } else if (args[0] === 'netBlockStatusChange') { 127 args[len - 1].call(this, paramMock.businessErrorMock, { NetHandle, blocked }); 128 } else if (args[0] === 'netCapabilitiesChange') { 129 args[len - 1].call(this, { NetHandle, NetCap }); 130 } else if (args[0] === 'netConnectionPropertiesChange') { 131 args[len - 1].call(this, { NetHandle, ConnectionProperties }); 132 } else if (args[0] === 'netLosing') { 133 args[len - 1].call(this, { 134 NetHandle, 135 maxMsToLive: "[PC Preview] unknow maxMsToLive" 136 }); 137 } else if (args[0] === 'netLost') { 138 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 139 } else if (args[0] === 'netUnavailable') { 140 args[len - 1].call(this, paramMock.businessErrorMock); 141 } 142 } 143 }, 144 off: function (...args) { 145 console.warn("net.connection.off interface mocked in the Previewer. How this interface works on the Previewer may " + 146 "be different from that on a real device.") 147 const len = args.length 148 if (typeof args[len - 1] === 'function') { 149 if (args[0] === 'netAvailable') { 150 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 151 } else if (args[0] === 'netBlockStatusChange') { 152 args[len - 1].call(this, { NetHandle, blocked }); 153 } else if (args[0] === 'netCapabilitiesChange') { 154 args[len - 1].call(this, { NetHandle, NetCap }); 155 } else if (args[0] === 'netConnectionPropertiesChange') { 156 args[len - 1].call(this, { NetHandle, ConnectionProperties }); 157 } else if (args[0] === 'netLosing') { 158 args[len - 1].call(this, { 159 NetHandle, 160 maxMsToLive: "[PC Preview] unknow maxMsToLive" 161 }); 162 } else if (args[0] === 'netLost') { 163 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 164 } else if (args[0] === 'netUnavailable') { 165 args[len - 1].call(this, paramMock.businessErrorMock); 166 } 167 } 168 }, 169 addNetStatusCallback: function (...args) { 170 console.warn("net.connection.addNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + 171 " may 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 removeNetStatusCallback: function (...args) { 182 console.warn("net.connection.removeNetStatusCallback interface mocked in the Previewer. How this interface works on the Previewer" + 183 " may 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); 187 } else { 188 return new Promise((resolve, reject) => { 189 resolve(); 190 }) 191 } 192 }, 193 getAppNet: function (...args) { 194 console.warn("net.connection.getAppNet interface mocked in the Previewer. How this interface works on the Previewer" + 195 " may 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, NetHandle); 199 } else { 200 return new Promise((resolve, reject) => { 201 resolve(NetHandle); 202 }) 203 } 204 }, 205 setAppNet: function (...args) { 206 console.warn("net.connection.setAppNet interface mocked in the Previewer. How this interface works on the Previewer" + 207 " may 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 getDefaultNet: function (...args) { 218 console.warn("net.connection.getDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 219 " may be different from that on a real device.") 220 const len = args.length 221 if (typeof args[len - 1] === 'function') { 222 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 223 } else { 224 return new Promise((resolve, reject) => { 225 resolve(NetHandle); 226 }) 227 } 228 }, 229 getAllNets: function (...args) { 230 console.warn("net.connection.getAllNets interface mocked in the Previewer. How this interface works on the Previewer" + 231 " may be different from that on a real device.") 232 const len = args.length 233 if (typeof args[len - 1] === 'function') { 234 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle); 235 } else { 236 return new Promise((resolve, reject) => { 237 resolve(NetHandle); 238 }) 239 } 240 }, 241 getDefaultHttpProxy: function (...args) { 242 console.warn("net.connection.getDefaultHttpProxy interface mocked in the Previewer. How this interface works on the Previewer" + 243 " may be different from that on a real device.") 244 const len = args.length 245 if (typeof args[len - 1] === 'function') { 246 args[len - 1].call(this, paramMock.businessErrorMock, HttpProxy); 247 } else { 248 return new Promise((resolve, reject) => { 249 resolve(HttpProxy); 250 }) 251 } 252 }, 253 getConnectionProperties: function (...args) { 254 console.warn("net.connection.getConnectionProperties interface mocked in the Previewer. How this interface works on the Previewer" + 255 " may be different from that on a real device.") 256 const len = args.length 257 if (typeof args[len - 1] === 'function') { 258 args[len - 1].call(this, paramMock.businessErrorMock, ConnectionProperties); 259 } else { 260 return new Promise((resolve, reject) => { 261 resolve(ConnectionProperties); 262 }) 263 } 264 }, 265 getNetCapabilities: function (...args) { 266 console.warn("net.connection.getNetCapabilities interface mocked in the Previewer. How this interface works on the Previewer" + 267 " may be different from that on a real device.") 268 const len = args.length 269 if (typeof args[len - 1] === 'function') { 270 args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilities); 271 } else { 272 return new Promise((resolve, reject) => { 273 resolve(NetCapabilities); 274 }) 275 } 276 }, 277 getBackgroundPolicy: function (...args) { 278 console.warn("net.connection.getBackgroundPolicy interface mocked in the Previewer. How this interface works on the Previewer" + 279 " may be different from that on a real device.") 280 const len = args.length 281 if (typeof args[len - 1] === 'function') { 282 args[len - 1].call(this, paramMock.businessErrorMock, BackgroundPolicy); 283 } else { 284 return new Promise((resolve, reject) => { 285 resolve(BackgroundPolicy); 286 }) 287 } 288 }, 289 isDefaultNetMetered: function (...args) { 290 console.warn("net.connection.isDefaultNetMetered interface mocked in the Previewer. How this interface works on the Previewer" + 291 " may be different from that on a real device.") 292 const len = args.length 293 if (typeof args[len - 1] === 'function') { 294 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 295 } else { 296 return new Promise((resolve, reject) => { 297 resolve(paramMock.paramBooleanMock); 298 }) 299 } 300 }, 301 hasDefaultNet: function (...args) { 302 console.warn("net.connection.hasDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 303 " may be different from that on a real device.") 304 const len = args.length 305 if (typeof args[len - 1] === 'function') { 306 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 307 } else { 308 return new Promise((resolve, reject) => { 309 resolve(paramMock.paramBooleanMock); 310 }) 311 } 312 }, 313 enableAirplaneMode: function (...args) { 314 console.warn("net.connection.enableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 315 " may be different from that on a real device.") 316 const len = args.length 317 if (typeof args[len - 1] === 'function') { 318 args[len - 1].call(this, paramMock.businessErrorMock); 319 } else { 320 return new Promise((resolve, reject) => { 321 resolve(); 322 }) 323 } 324 }, 325 disableAirplaneMode: function (...args) { 326 console.warn("net.connection.disableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 327 " may be different from that on a real device.") 328 const len = args.length 329 if (typeof args[len - 1] === 'function') { 330 args[len - 1].call(this, paramMock.businessErrorMock); 331 } else { 332 return new Promise((resolve, reject) => { 333 resolve(); 334 }) 335 } 336 }, 337 enableDistributedCellularData: function (...args) { 338 console.warn("net.connection.enableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + 339 " may be different from that on a real device.") 340 const len = args.length 341 if (typeof args[len - 1] === 'function') { 342 args[len - 1].call(this, paramMock.businessErrorMock); 343 } else { 344 return new Promise((resolve, reject) => { 345 resolve(); 346 }) 347 } 348 }, 349 disableDistributedCellularData: function (...args) { 350 console.warn("net.connection.disableDistributedCellularData interface mocked in the Previewer. How this interface works on the Previewer" + 351 " may be different from that on a real device.") 352 const len = args.length 353 if (typeof args[len - 1] === 'function') { 354 args[len - 1].call(this, paramMock.businessErrorMock); 355 } else { 356 return new Promise((resolve, reject) => { 357 resolve(); 358 }) 359 } 360 }, 361 reportNetConnected: function (...args) { 362 console.warn("net.connection.reportNetConnected interface mocked in the Previewer. How this interface works on the Previewer" + 363 " may be different from that on a real device.") 364 const len = args.length 365 if (typeof args[len - 1] === 'function') { 366 args[len - 1].call(this, paramMock.businessErrorMock); 367 } else { 368 return new Promise((resolve, reject) => { 369 resolve(); 370 }) 371 } 372 }, 373 reportNetDisconnected: function (...args) { 374 console.warn("net.connection.reportNetDisconnected interface mocked in the Previewer. How this interface works on the Previewer" + 375 " may be different from that on a real device.") 376 const len = args.length 377 if (typeof args[len - 1] === 'function') { 378 args[len - 1].call(this, paramMock.businessErrorMock); 379 } else { 380 return new Promise((resolve, reject) => { 381 resolve(); 382 }) 383 } 384 }, 385 } 386 return connection 387} 388