1/* 2 * Copyright (c) 2021-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 const NetCap = { 19 NET_CAPABILITY_MMS: 0, 20 NET_CAPABILITY_NOT_METERED: 11, 21 NET_CAPABILITY_INTERNET: 12, 22 NET_CAPABILITY_NOT_VPN: 15, 23 NET_CAPABILITY_VALIDATED: 16 24}; 25 26export const NetBearType = { 27 BEARER_CELLULAR: 0, 28 BEARER_WIFI: 1, 29 BEARER_ETHERNET: 3 30}; 31 32export function mockConnection() { 33 const NetSpecifier = { 34 netCapabilities: NetCapabilities, 35 bearerPrivateIdentifier: '[PC preview] unknow bearerPrivateIdentifier' 36 } 37 38 const NetCapabilities = { 39 linkUpBandwidthKbps: '[PC preview] unknow linkUpBandwidthKbps', 40 linkDownBandwidthKbps: '[PC preview] unknow linkDownBandwidthKbps', 41 networkCap:[NetCap], 42 bearerTypes:[NetBearType] 43 } 44 45 const ConnectionProperties = { 46 interfaceName: '[PC preview] unknow interfaceName', 47 domains: '[PC preview] unknow domains', 48 linkAddresses: [LinkAddress], 49 dnses: [NetAddress], 50 routes: [RouteInfo], 51 mtu: '[PC preview] unknow mtu' 52 } 53 54 const LinkAddress = { 55 address: NetAddress, 56 prefixLength: '[PC preview] unknow prefixLength' 57 } 58 59 const NetAddress = { 60 address: '[PC preview] unknow address', 61 family: '[PC preview] unknow family', 62 port: '[PC preview] unknow port' 63 } 64 65 const RouteInfo = { 66 interface: '[PC preview] unknow interface', 67 destination: LinkAddress, 68 gateway: NetAddress, 69 hasGateway: '[PC preview] unknow hasGateway', 70 isDefaultRoute: '[PC preview] unknow isDefaultRoute' 71 } 72 73 const NetConnection = { 74 on: function (...args) { 75 console.warn("NetConnection.on interface mocked in the Previewer. How this interface works on the Previewer" + 76 " may be different from that on a real device.") 77 const len = args.length 78 if (typeof args[len - 1] === 'function') { 79 if (args[0] === 'netAvailable') { 80 args[len - 1].call(this, NetHandle); 81 } else if (args[0] === 'netBlockStatusChange') { 82 var array = new Array(NetHandle, paramMock.paramBooleanMock); 83 args[len - 1].call(this, array); 84 } else if (args[0] === 'netCapabilitiesChange') { 85 var array = new Array(NetHandle, NetCapabilities); 86 args[len - 1].call(this, array); 87 } else if (args[0] === 'netConnectionPropertiesChange') { 88 var array = new Array(NetHandle, ConnectionProperties); 89 args[len - 1].call(this, array); 90 } else if (args[0] === 'netLost') { 91 args[len - 1].call(this, NetHandle); 92 } else if (args[0] === 'netUnavailable') { 93 args[len - 1].call(this); 94 } 95 } 96 }, 97 98 register: function (...args) { 99 console.warn("NetConnection.register interface mocked in the Previewer. How this interface works on the Previewer" + 100 " may be different from that on a real device.") 101 const len = args.length 102 if (len > 0 && typeof args[len - 1] === 'function') { 103 args[len - 1].call(this, paramMock.businessErrorMock) 104 } 105 }, 106 107 unregister: function (...args) { 108 console.warn("NetConnection.unregister interface mocked in the Previewer. How this interface works on the Previewer" + 109 " may be different from that on a real device.") 110 const len = args.length 111 if (len > 0 && typeof args[len - 1] === 'function') { 112 args[len - 1].call(this, paramMock.businessErrorMock) 113 } 114 } 115 } 116 117 const NetHandle = { 118 netId: '[PC preview] unknow netId', 119 120 getAddressesByName: function (...args) { 121 console.warn("NetHandle.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + 122 " may be different from that on a real device.") 123 const len = args.length 124 if (len > 0 && typeof args[len - 1] === 'function') { 125 args[len - 1].call(this, paramMock.businessErrorMock, [NetAddress]) 126 } else { 127 return new Promise((resolve, reject) => { 128 resolve([NetAddress]) 129 }) 130 } 131 }, 132 133 getAddressByName: function (...args) { 134 console.warn("NetHandle.getAddressByName 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 (len > 0 && typeof args[len - 1] === 'function') { 138 args[len - 1].call(this, paramMock.businessErrorMock, NetAddress) 139 } else { 140 return new Promise((resolve, reject) => { 141 resolve(NetAddress) 142 }) 143 } 144 } 145 } 146 147 const connection = { 148 NetCap, 149 NetBearType, 150 createNetConnection: function () { 151 console.warn("connection.createNetConnection interface mocked in the Previewer. How this interface works on the Previewer" + 152 " may be different from that on a real device.") 153 return NetConnection; 154 }, 155 156 getDefaultNet: function (...args) { 157 console.warn("connection.getDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 158 " may be different from that on a real device.") 159 const len = args.length 160 if (typeof args[len - 1] === 'function') { 161 args[len - 1].call(this, paramMock.businessErrorMock, NetHandle) 162 } else { 163 return new Promise((resolve) => { 164 resolve(NetHandle) 165 }) 166 } 167 }, 168 169 getAllNets: function (...args) { 170 console.warn("connection.getAllNets 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, [NetHandle]) 175 } else { 176 return new Promise((resolve) => { 177 resolve([NetHandle]) 178 }) 179 } 180 }, 181 182 getConnectionProperties: function (...args) { 183 console.warn("connection.getConnectionProperties interface mocked in the Previewer. How this interface works on the Previewer" + 184 " may be different from that on a real device.") 185 const len = args.length 186 if (typeof args[len - 1] === 'function') { 187 args[len - 1].call(this, paramMock.businessErrorMock, ConnectionProperties) 188 } else { 189 return new Promise((resolve) => { 190 resolve(ConnectionProperties) 191 }) 192 } 193 }, 194 195 getNetCapabilities: function (...args) { 196 console.warn("connection.getNetCapabilities interface mocked in the Previewer. How this interface works on the Previewer" + 197 " may be different from that on a real device.") 198 const len = args.length 199 if (typeof args[len - 1] === 'function') { 200 args[len - 1].call(this, paramMock.businessErrorMock, NetCapabilities) 201 } else { 202 return new Promise((resolve) => { 203 resolve(NetCapabilities) 204 }) 205 } 206 }, 207 208 hasDefaultNet: function (...args) { 209 console.warn("connection.hasDefaultNet interface mocked in the Previewer. How this interface works on the Previewer" + 210 " may be different from that on a real device.") 211 const len = args.length 212 if (typeof args[len - 1] === 'function') { 213 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock) 214 } else { 215 return new Promise((resolve) => { 216 resolve(paramMock.paramBooleanMock) 217 }) 218 } 219 }, 220 221 enableAirplaneMode: function (...args) { 222 console.warn("connection.enableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 223 " may be different from that on a real device.") 224 const len = args.length 225 if (typeof args[len - 1] === 'function') { 226 args[len - 1].call(this, paramMock.businessErrorMock) 227 } else { 228 return new Promise((resolve) => { 229 resolve() 230 }) 231 } 232 }, 233 234 disableAirplaneMode: function (...args) { 235 console.warn("connection.disableAirplaneMode interface mocked in the Previewer. How this interface works on the Previewer" + 236 " may be different from that on a real device.") 237 const len = args.length 238 if (typeof args[len - 1] === 'function') { 239 args[len - 1].call(this, paramMock.businessErrorMock) 240 } else { 241 return new Promise((resolve) => { 242 resolve() 243 }) 244 } 245 }, 246 247 reportNetConnected: function (...args) { 248 console.warn("connection.reportNetConnected interface mocked in the Previewer. How this interface works on the Previewer" + 249 " may be different from that on a real device.") 250 const len = args.length 251 if (typeof args[len - 1] === 'function') { 252 args[len - 1].call(this, paramMock.businessErrorMock) 253 } else { 254 return new Promise((resolve) => { 255 resolve() 256 }) 257 } 258 }, 259 260 reportNetDisconnected: function (...args) { 261 console.warn("connection.reportNetDisconnected interface mocked in the Previewer. How this interface works on the Previewer" + 262 " may be different from that on a real device.") 263 const len = args.length 264 if (typeof args[len - 1] === 'function') { 265 args[len - 1].call(this, paramMock.businessErrorMock) 266 } else { 267 return new Promise((resolve) => { 268 resolve() 269 }) 270 } 271 }, 272 273 getAddressesByName: function (...args) { 274 console.warn("connection.getAddressesByName interface mocked in the Previewer. How this interface works on the Previewer" + 275 " may be different from that on a real device.") 276 const len = args.length 277 if (typeof args[len - 1] === 'function') { 278 args[len - 1].call(this, paramMock.businessErrorMock, [NetAddress]) 279 } else { 280 return new Promise((resolve) => { 281 resolve([NetAddress]) 282 }) 283 } 284 }, 285 }; 286 287 return connection; 288}