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 16export function mockSystemPlugin () { 17 global.systemplugin = { 18 notification:{}, 19 vibrator: {}, 20 sensor: {}, 21 geolocation: {}, 22 network: {}, 23 brightness: { 24 argsV: { 25 value: 80 26 }, 27 argsM: { 28 mode: 0 29 } 30 }, 31 volume: { 32 args: {value: 0.5} 33 }, 34 battery: {}, 35 systemPackage: {}, 36 wifi: {}, 37 bluetooth: {}, 38 tts: {}, 39 alarm: {}, 40 request: {}, 41 fetch: {}, 42 storage: {}, 43 file: {} 44 } 45 mockNotification() 46 mockFetch() 47 mockRequest() 48 mockStorage() 49 mockFile() 50 mockVibrator() 51 mockSensor() 52 mockGeolocation() 53 mockNetwork() 54 mockBrightness() 55 mockVolume() 56 mockBattery() 57 mockSystemPackage() 58} 59 60function mockNotification () { 61 global.systemplugin.notification = { 62 show: function () {} 63 } 64} 65 66function mockVibrator () { 67 global.systemplugin.vibrator = { 68 vibrate: function () { 69 arguments[0].success() 70 isComplete(arguments[0].complete) 71 } 72 } 73} 74 75function mockSensor () { 76 mockAccelerometer() 77 mockCompass() 78 mockProximity() 79 mockLight() 80 mockStepCounter() 81 mockBarometer() 82 mockHeartRate() 83 mockOnBodyState() 84} 85 86function mockAccelerometer () { 87 Object.assign(global.systemplugin.sensor, { 88 subscribeAccelerometer: function () { 89 const time = { 90 normal: 200, 91 game: 20, 92 ui: 60 93 } 94 let ret = {} 95 let timer = 0 96 if(!arguments[0].interval) { 97 timer = time.normal 98 } else { 99 timer = time[arguments[0].interval] 100 } 101 this.unsubscribeAccelerometer() 102 this.unsubscribeAcc = setInterval(() => { 103 ret.x = Math.ceil(Math.random() * 10) 104 ret.y = Math.ceil(Math.random() * 10) 105 ret.z = Math.ceil(Math.random() * 10) 106 arguments[0].success(ret) 107 }, timer) 108 }, 109 unsubscribeAccelerometer: function () { 110 clearInterval(this.unsubscribeAcc) 111 delete this.unsubscribeAcc 112 } 113 }) 114} 115 116function mockCompass () { 117 Object.assign(global.systemplugin.sensor, { 118 subscribeCompass: function () { 119 if(!this.unsubscribeDirection) { 120 let ret = {} 121 this.unsubscribeDirection = setInterval(() => { 122 ret.direction = getRandomArbitrary(49, 50) 123 arguments[0].success(ret) 124 }, 100) 125 } 126 }, 127 unsubscribeCompass: function () { 128 clearInterval(this.unsubscribeDirection) 129 delete this.unsubscribeDirection 130 } 131 }) 132} 133 134function mockProximity() { 135 Object.assign(global.systemplugin.sensor, { 136 subscribeProximity: function () { 137 if(!this.unsubscribeDistance) { 138 let ret = {} 139 this.unsubscribeDistance = setInterval(() => { 140 ret.distance = Math.ceil(Math.random() * 100) 141 arguments[0].success(ret) 142 }, 1000) 143 } 144 }, 145 unsubscribeProximity: function () { 146 clearInterval(this.unsubscribeDistance) 147 delete this.unsubscribeDistance 148 } 149 }) 150} 151 152function mockLight () { 153 Object.assign(global.systemplugin.sensor, { 154 subscribeLight: function () { 155 if(!this.unsubscribeIntensity) { 156 let ret = {} 157 this.unsubscribeIntensity = setInterval(() => { 158 ret.intensity = getRandomArbitrary(660, 680) 159 arguments[0].success(ret) 160 }, 500) 161 } 162 }, 163 unsubscribeLight: function () { 164 clearInterval(this.unsubscribeIntensity) 165 delete this.unsubscribeIntensity 166 }, 167 }) 168} 169 170function mockStepCounter() { 171 Object.assign(global.systemplugin.sensor, { 172 subscribeStepCounter: function () { 173 if(!this.unsubscribeSteps) { 174 let ret = { steps: 0 } 175 this.unsubscribeSteps = setInterval(() => { 176 ret.steps += 1 177 arguments[0].success(ret) 178 }, 1000) 179 } 180 }, 181 unsubscribeStepCounter: function () { 182 clearInterval(this.unsubscribeSteps) 183 delete this.unsubscribeSteps 184 } 185 }) 186} 187 188function mockBarometer() { 189 Object.assign(global.systemplugin.sensor, { 190 subscribeBarometer: function () { 191 if(!this.unsubscribePressure) { 192 let ret = {} 193 this.unsubscribePressure = setInterval(() => { 194 ret.pressure = getRandomArbitrary(1110, 1111) 195 arguments[0].success(ret) 196 }, 500) 197 } 198 }, 199 unsubscribeBarometer: function () { 200 clearInterval(this.unsubscribePressure) 201 delete this.unsubscribePressure 202 } 203 }) 204} 205 206function mockHeartRate() { 207 Object.assign(global.systemplugin.sensor, { 208 subscribeHeartRate: function () { 209 if(!this.unsubscribeRate) { 210 let ret = {} 211 this.unsubscribeRate = setInterval(() => { 212 ret.heartRate = Math.ceil(Math.random() * 30) 213 arguments[0].success(ret) 214 }, 500) 215 } 216 }, 217 unsubscribeHeartRate: function () { 218 clearInterval(this.unsubscribeRate) 219 delete this.unsubscribeRate 220 }, 221 }) 222} 223 224function mockOnBodyState () { 225 Object.assign(global.systemplugin.sensor, { 226 subscribeOnBodyState: function () { 227 if(!this.unsubscribeBodyState) { 228 let ret = {} 229 this.unsubscribeBodyState = setInterval(() => { 230 ret.value = Math.ceil(Math.random() * 20) 231 arguments[0].success(ret) 232 }, 500) 233 } 234 }, 235 unsubscribeOnBodyState: function () { 236 clearInterval(this.unsubscribeBodyState) 237 delete this.unsubscribeBodyState 238 } 239 }) 240} 241 242function mockGeolocation () { 243 const data = { 244 latitude: '121.61934', 245 longitude: '31.257907', 246 accuracy: '15', 247 time: '160332896544' 248 } 249 global.systemplugin.geolocation = { 250 getLocation: function () { 251 arguments[0].success(data) 252 isComplete(arguments[0].complete) 253 }, 254 getLocationType: function () { 255 let args = {types: ['gps', 'network']} 256 arguments[0].success(args) 257 isComplete(arguments[0].complete) 258 }, 259 getSupportedCoordTypes() { 260 return ["wgs84"] 261 }, 262 subscribe: function () { 263 if(!this.unsubscribeLocation) { 264 this.unsubscribeLocation = setInterval(() => { 265 data.latitude = getRandomArbitrary(121, 122) 266 data.longitude = getRandomArbitrary(31, 32) 267 data.accuracy = getRandomArbitrary(14, 18) 268 arguments[0].success(data) 269 }, 1000) 270 } 271 }, 272 unsubscribe: function () { 273 clearInterval(this.unsubscribeLocation) 274 delete this.unsubscribeLocation 275 } 276 } 277} 278 279function mockNetwork () { 280 const data = { 281 metered: true, 282 type: "5g" 283 } 284 global.systemplugin.network = { 285 getType: function () { 286 arguments[0].success(data) 287 arguments[0].complete() 288 }, 289 subscribe: function () { 290 if(!this.unsubscribeNetwork) { 291 this.unsubscribeNetwork = setInterval(() => { 292 arguments[0].success(data) 293 }, 3000) 294 } 295 }, 296 unsubscribe: function () { 297 clearInterval(this.unsubscribeNetwork) 298 delete this.unsubscribeNetwork 299 } 300 } 301} 302 303function mockBrightness () { 304 Object.assign(global.systemplugin.brightness, { 305 getValue: function () { 306 arguments[0].success(this.argsV) 307 isComplete(arguments[0].complete) 308 }, 309 setValue: function () { 310 if(arguments[0].value) { 311 this.argsV.value = arguments[0].value 312 arguments[0].success("brightness setValue successfully") 313 isComplete(arguments[0].complete) 314 } 315 }, 316 getMode: function () { 317 arguments[0].success(this.argsM) 318 isComplete(arguments[0].complete) 319 }, 320 setMode: function () { 321 this.argsM.mode = arguments[0].mode 322 arguments[0].success("brightness setMode successfully") 323 isComplete(arguments[0].complete) 324 }, 325 setKeepScreenOn: function () { 326 arguments[0].success("brightness setKeepScreenOn successfully") 327 isComplete(arguments[0].complete) 328 } 329 }) 330} 331 332function mockVolume () { 333 Object.assign(global.systemplugin.volume, { 334 getMediaValue: function () { 335 arguments[0].success(this.args) 336 isComplete(arguments[0].complete) 337 }, 338 setMediaValue: function () { 339 if(arguments[0].value) { 340 this.args.value = arguments[0].value 341 arguments[0].success("set volume successfully") 342 isComplete(arguments[0].complete) 343 } 344 } 345 }) 346} 347 348function mockBattery () { 349 global.systemplugin.battery = { 350 getStatus: function () { 351 arguments[0].success.call(this, { level: 1, charging: false} ) 352 isComplete(arguments[0].complete) 353 } 354 } 355} 356 357function mockSystemPackage () { 358 global.systemplugin.package = { 359 hasInstalled: function () { 360 arguments[0].success(true) 361 isComplete(arguments[0].complete) 362 } 363 } 364} 365 366function mockRequest () { 367 const data = { 368 code: "[pc Preview]: no internet", 369 data: "[pc Preview]: no internet", 370 headers: "[pc Preview]: no internet", 371 token: "[pc Preview]: no internet", 372 uri: "[pc Preview]: no internet", 373 } 374 global.systemplugin.request = { 375 upload: function () { 376 arguments[0].success(data) 377 isComplete(arguments[0].complete) 378 }, 379 download: function () { 380 arguments[0].success(data) 381 isComplete(arguments[0].complete) 382 }, 383 onDownloadComplete: function () { 384 arguments[0].success(data) 385 isComplete(arguments[0].complete) 386 } 387 } 388} 389 390function mockFetch () { 391 const data = { 392 code: "[pc Preview]: no internet", 393 data: "[pc Preview]: no internet" 394 } 395 global.systemplugin.fetch = { 396 fetch: function () { 397 arguments[0].success(data) 398 isComplete(arguments[0].complete) 399 } 400 } 401} 402 403function mockStorage () { 404 global.systemplugin.storage = { 405 get: function () { 406 arguments[0].success("[pc Preview]: no system") 407 isComplete(arguments[0].complete) 408 }, 409 set: function () { 410 arguments[0].success("[pc Preview]: no system") 411 isComplete(arguments[0].complete) 412 }, 413 clear: function () { 414 arguments[0].success("[pc Preview]: no system") 415 isComplete(arguments[0].complete) 416 }, 417 delete: function () { 418 arguments[0].success("[pc Preview]: no system") 419 isComplete(arguments[0].complete) 420 } 421 } 422} 423 424function mockFile () { 425 global.systemplugin.file = { 426 move: function () { 427 arguments[0].success(arguments[0].dstUri) 428 isComplete(arguments[0].complete) 429 }, 430 copy: function () { 431 arguments[0].success(arguments[0].dstUri) 432 isComplete(arguments[0].complete) 433 }, 434 list: function () { 435 const data = { 436 fileList: [{ 437 uri:'[pc Preview]: no file', 438 lastModifiedTime:"[pc Preview]: no file", 439 length:"[pc Preview]: no file", 440 type: 'file'}] 441 } 442 arguments[0].success(data) 443 isComplete(arguments[0].complete) 444 }, 445 get: function () { 446 const data = { 447 uri:'[pc Preview]: no file', 448 lastModifiedTime:"[pc Preview]: no file", 449 length:"[pc Preview]: no file", 450 type: 'file', 451 subFiles: ["[pc Preview]: no file", "[pc Preview]: no file"] 452 } 453 arguments[0].success(data) 454 isComplete(arguments[0].complete) 455 }, 456 delete: function () { 457 arguments[0].success() 458 isComplete(arguments[0].complete) 459 }, 460 writeText: function () { 461 arguments[0].success() 462 isComplete(arguments[0].complete) 463 }, 464 writeArrayBuffer: function () { 465 arguments[0].success() 466 isComplete(arguments[0].complete) 467 }, 468 readText: function () { 469 const data = {text: "[pc Preview]: success default"} 470 arguments[0].success(data) 471 isComplete(arguments[0].complete) 472 }, 473 readArrayBuffer: function () { 474 const data = {buffer: ["[pc Preview]: default", "[pc Preview]: default", "[pc Preview]: default"]} 475 arguments[0].success(data) 476 isComplete(arguments[0].complete) 477 }, 478 access: function () { 479 arguments[0].success() 480 isComplete(arguments[0].complete) 481 }, 482 mkdir: function () { 483 arguments[0].success() 484 isComplete(arguments[0].complete) 485 }, 486 rmdir: function () { 487 arguments[0].success() 488 isComplete(arguments[0].complete) 489 } 490 } 491} 492 493function isComplete() { 494 if(arguments[0] === undefined) { 495 return 496 } 497 arguments[0].call(this) 498} 499 500function getRandomArbitrary(min, max) { 501 return Math.random().toFixed(6) * (max - min) + min; 502} 503