/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { getRandomArbitrary, hasComplete, paramMock } from './utils' export function mockSensor() { global.systemplugin.sensor = {} mockAccelerometer() mockBarometer() mockCompass() mockDeviceOrientation() mockGyroscope() mockHeartRate() mockLight() mockOnBodyState() mockProximity() mockStepCounter() mockGravity() mockMagnetic() mockHall() } function mockAccelerometer() { global.systemplugin.sensor.subscribeAccelerometer = function (...args) { console.warn("sensor.subscribeAccelerometer interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeAcc) delete this.unsubscribeAcc this.unsubscribeAcc = setInterval(() => { ret.x = Math.ceil(Math.random() * 10) ret.y = Math.ceil(Math.random() * 10) ret.z = Math.ceil(Math.random() * 10) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeAccelerometer = function () { console.warn("sensor.unsubscribeAccelerometer interface mocked in the Previewer. How this interface works on" + " the Previewer may be different from that on a real device.") clearInterval(this.unsubscribeAcc) delete this.unsubscribeAcc } } function mockBarometer() { global.systemplugin.sensor.subscribeBarometer = function (...args) { console.warn("sensor.subscribeBarometer interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribePressure) { let ret = {} this.unsubscribePressure = setInterval(() => { ret.pressure = getRandomArbitrary(1110, 1111) args[0].success(ret) }, 500) } } global.systemplugin.sensor.unsubscribeBarometer = function () { console.warn("sensor.unsubscribeBarometer interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribePressure) delete this.unsubscribePressure } } function mockCompass() { global.systemplugin.sensor.subscribeCompass = function (...args) { console.warn("sensor.subscribeCompass interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeDirection) { let ret = {} this.unsubscribeDirection = setInterval(() => { ret.direction = getRandomArbitrary(49, 50) args[0].success(ret) }, 100) } } global.systemplugin.sensor.unsubscribeCompass = function () { console.warn("sensor.unsubscribeCompass interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeDirection) delete this.unsubscribeDirection } } function mockGyroscope() { global.systemplugin.sensor.subscribeGyroscope = function (...args) { console.warn("sensor.subscribeGyroscope interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeGyr) delete this.unsubscribeGyr this.unsubscribeGyr = setInterval(() => { ret.x = Math.ceil(Math.random() * 10) ret.y = Math.ceil(Math.random() * 10) ret.z = Math.ceil(Math.random() * 10) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeGyroscope = function () { console.warn("sensor.unsubscribeGyroscope interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeGyr) delete this.unsubscribeGyr } } function mockDeviceOrientation() { global.systemplugin.sensor.subscribeDeviceOrientation = function (...args) { console.warn("sensor.subscribeDeviceOrientation interface mocked in the Previewer. How this interface works on" + " the Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeDevOri) delete this.unsubscribeDevOri this.unsubscribeDevOri = setInterval(() => { ret.alpha = Math.ceil(Math.random() * 10) ret.beta = Math.ceil(Math.random() * 10) ret.gamma = Math.ceil(Math.random() * 10) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeDeviceOrientation = function () { console.warn("sensor.unsubscribeDeviceOrientation interface mocked in the Previewer. How this interface works" + " on the Previewer may be different from that on a real device.") clearInterval(this.unsubscribeDevOri) delete this.unsubscribeDevOri } } function mockHeartRate() { global.systemplugin.sensor.subscribeHeartRate = function (...args) { console.warn("sensor.subscribeHeartRate interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeRate) { let ret = {} this.unsubscribeRate = setInterval(() => { ret.heartRate = Math.ceil(Math.random() * 30) args[0].success(ret) }, 500) } } global.systemplugin.sensor.unsubscribeHeartRate = function () { console.warn("sensor.unsubscribeHeartRate interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeRate) delete this.unsubscribeRate } } function mockLight() { global.systemplugin.sensor.subscribeLight = function (...args) { console.warn("sensor.subscribeLight interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeIntensity) { let ret = {} this.unsubscribeIntensity = setInterval(() => { ret.intensity = getRandomArbitrary(660, 680) args[0].success(ret) }, 500) } } global.systemplugin.sensor.unsubscribeLight = function () { console.warn("sensor.unsubscribeLight interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeIntensity) delete this.unsubscribeIntensity } } function mockOnBodyState() { global.systemplugin.sensor.subscribeOnBodyState = function (...args) { console.warn("sensor.subscribeOnBodyState interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeBodyState) { let ret = {} this.unsubscribeBodyState = setInterval(() => { ret.value = Math.ceil(Math.random() * 20) args[0].success(ret) }, 500) } } global.systemplugin.sensor.unsubscribeOnBodyState = function () { console.warn("sensor.unsubscribeOnBodyState interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeBodyState) delete this.unsubscribeBodyState } global.systemplugin.sensor.getOnBodyState = function (...args) { console.warn("sensor.getOnBodyState interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") let OnBodyStateResponseMock = { value: paramMock.paramBooleanMock } args[0].success(OnBodyStateResponseMock) hasComplete(args[0].complete) } } function mockProximity() { global.systemplugin.sensor.subscribeProximity = function (...args) { console.warn("sensor.subscribeProximity interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeDistance) { let ret = {} this.unsubscribeDistance = setInterval(() => { ret.distance = Math.ceil(Math.random() * 100) args[0].success(ret) }, 1000) } } global.systemplugin.sensor.unsubscribeProximity = function () { console.warn("sensor.unsubscribeProximity interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeDistance) delete this.unsubscribeDistance } } function mockStepCounter() { global.systemplugin.sensor.subscribeStepCounter = function (...args) { console.warn("sensor.subscribeStepCounter interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") if (!this.unsubscribeSteps) { let ret = { steps: 0 } this.unsubscribeSteps = setInterval(() => { ret.steps += 1 args[0].success(ret) }, 1000) } } global.systemplugin.sensor.unsubscribeStepCounter = function () { console.warn("sensor.unsubscribeStepCounter interface mocked in the Previewer. How this interface works on the" + " Previewer may be different from that on a real device.") clearInterval(this.unsubscribeSteps) delete this.unsubscribeSteps } } function mockGravity() { global.systemplugin.sensor.subscribeGravity = function (...args) { console.warn("sensor.subscribeGravity interface mocked in the Previewer. How this interface works on" + " the Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeGrav) delete this.unsubscribeGrav this.unsubscribeGrav = setInterval(() => { ret.x = Math.ceil(Math.random() * 10) ret.y = Math.ceil(Math.random() * 10) ret.z = Math.ceil(Math.random() * 10) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeGravity = function () { console.warn("sensor.unsubscribeGravity interface mocked in the Previewer. How this interface works" + " on the Previewer may be different from that on a real device.") clearInterval(this.unsubscribeGrav) delete this.unsubscribeGrav } } function mockMagnetic() { global.systemplugin.sensor.subscribeMagnetic = function (...args) { console.warn("sensor.subscribeMagnetic interface mocked in the Previewer. How this interface works on" + " the Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeMag) delete this.unsubscribeMag this.unsubscribeMag = setInterval(() => { ret.x = getRandomArbitrary(-100, 100) ret.y = getRandomArbitrary(-100, 100) ret.z = getRandomArbitrary(-100, 100) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeMagnetic = function () { console.warn("sensor.unsubscribeMagnetic interface mocked in the Previewer. How this interface works" + " on the Previewer may be different from that on a real device.") clearInterval(this.unsubscribeMag) delete this.unsubscribeMag } } function mockHall() { global.systemplugin.sensor.subscribeHall = function (...args) { console.warn("sensor.subscribeHall interface mocked in the Previewer. How this interface works on" + " the Previewer may be different from that on a real device.") const time = { normal: 200, game: 20, ui: 60 } let ret = {} let timer = 0 if (!args[0].interval) { timer = time.normal } else { timer = time[args[0].interval] } clearInterval(this.unsubscribeHal) delete this.unsubscribeHal this.unsubscribeHal = setInterval(() => { ret.value = Math.round(Math.random()) args[0].success(ret) }, timer) } global.systemplugin.sensor.unsubscribeHall = function () { console.warn("sensor.unsubscribeHall interface mocked in the Previewer. How this interface works" + " on the Previewer may be different from that on a real device.") clearInterval(this.unsubscribeHal) delete this.unsubscribeHal } }