• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import {getRandomArbitrary, hasComplete, paramMock} from './utils'
2
3export function mockSensor() {
4  global.systemplugin.sensor = {}
5  mockAccelerometer()
6  mockBarometer()
7  mockCompass()
8  mockDeviceOrientation()
9  mockGyroscope()
10  mockHeartRate()
11  mockLight()
12  mockOnBodyState()
13  mockProximity()
14  mockStepCounter()
15}
16
17function mockAccelerometer() {
18  global.systemplugin.sensor.subscribeAccelerometer = function(...args) {
19    console.warn("sensor.subscribeAccelerometer interface mocked in the Previewer. How this interface works on the" +
20      " Previewer may be different from that on a real device.")
21    const time = {
22      normal: 200,
23      game: 20,
24      ui: 60
25    }
26    let ret = {}
27    let timer = 0
28    if (!args[0].interval) {
29      timer = time.normal
30    } else {
31      timer = time[args[0].interval]
32    }
33    clearInterval(this.unsubscribeAcc)
34    delete this.unsubscribeAcc
35    this.unsubscribeAcc = setInterval(() => {
36      ret.x = Math.ceil(Math.random() * 10)
37      ret.y = Math.ceil(Math.random() * 10)
38      ret.z = Math.ceil(Math.random() * 10)
39      args[0].success(ret)
40    }, timer)
41  }
42  global.systemplugin.sensor.unsubscribeAccelerometer = function() {
43    console.warn("sensor.unsubscribeAccelerometer interface mocked in the Previewer. How this interface works on" +
44      " the Previewer may be different from that on a real device.")
45    clearInterval(this.unsubscribeAcc)
46    delete this.unsubscribeAcc
47  }
48}
49
50function mockBarometer() {
51  global.systemplugin.sensor.subscribeBarometer = function(...args) {
52    console.warn("sensor.subscribeBarometer interface mocked in the Previewer. How this interface works on the" +
53      " Previewer may be different from that on a real device.")
54    if (!this.unsubscribePressure) {
55      let ret = {}
56      this.unsubscribePressure = setInterval(() => {
57        ret.pressure = getRandomArbitrary(1110, 1111)
58        args[0].success(ret)
59      }, 500)
60    }
61  }
62  global.systemplugin.sensor.unsubscribeBarometer = function() {
63    console.warn("sensor.unsubscribeBarometer interface mocked in the Previewer. How this interface works on the" +
64      " Previewer may be different from that on a real device.")
65    clearInterval(this.unsubscribePressure)
66    delete this.unsubscribePressure
67  }
68}
69
70function mockCompass() {
71  global.systemplugin.sensor.subscribeCompass = function(...args) {
72    console.warn("sensor.subscribeCompass interface mocked in the Previewer. How this interface works on the" +
73      " Previewer may be different from that on a real device.")
74    if (!this.unsubscribeDirection) {
75      let ret = {}
76      this.unsubscribeDirection = setInterval(() => {
77        ret.direction = getRandomArbitrary(49, 50)
78        args[0].success(ret)
79      }, 100)
80    }
81  }
82  global.systemplugin.sensor.unsubscribeCompass = function() {
83    console.warn("sensor.unsubscribeCompass interface mocked in the Previewer. How this interface works on the" +
84      " Previewer may be different from that on a real device.")
85    clearInterval(this.unsubscribeDirection)
86    delete this.unsubscribeDirection
87  }
88}
89
90function mockGyroscope() {
91  global.systemplugin.sensor.subscribeGyroscope = function(...args) {
92    console.warn("sensor.subscribeGyroscope interface mocked in the Previewer. How this interface works on the" +
93      " Previewer may be different from that on a real device.")
94    const time = {
95      normal: 200,
96      game: 20,
97      ui: 60
98    }
99    let ret = {}
100    let timer = 0
101    if (!args[0].interval) {
102      timer = time.normal
103    } else {
104      timer = time[args[0].interval]
105    }
106    clearInterval(this.unsubscribeGyr)
107    delete this.unsubscribeGyr
108    this.unsubscribeGyr = setInterval(() => {
109      ret.x = Math.ceil(Math.random() * 10)
110      ret.y = Math.ceil(Math.random() * 10)
111      ret.z = Math.ceil(Math.random() * 10)
112      args[0].success(ret)
113    }, timer)
114  }
115  global.systemplugin.sensor.unsubscribeGyroscope = function() {
116    console.warn("sensor.unsubscribeGyroscope interface mocked in the Previewer. How this interface works on the" +
117      " Previewer may be different from that on a real device.")
118    clearInterval(this.unsubscribeGyr)
119    delete this.unsubscribeGyr
120  }
121}
122
123function mockDeviceOrientation() {
124  global.systemplugin.sensor.subscribeDeviceOrientation = function(...args) {
125    console.warn("sensor.subscribeDeviceOrientation interface mocked in the Previewer. How this interface works on" +
126      " the Previewer may be different from that on a real device.")
127    const time = {
128      normal: 200,
129      game: 20,
130      ui: 60
131    }
132    let ret = {}
133    let timer = 0
134    if (!args[0].interval) {
135      timer = time.normal
136    } else {
137      timer = time[args[0].interval]
138    }
139    clearInterval(this.unsubscribeDevOri)
140    delete this.unsubscribeDevOri
141    this.unsubscribeDevOri = setInterval(() => {
142      ret.alpha = Math.ceil(Math.random() * 10)
143      ret.beta = Math.ceil(Math.random() * 10)
144      ret.gamma = Math.ceil(Math.random() * 10)
145      args[0].success(ret)
146    }, timer)
147  }
148  global.systemplugin.sensor.unsubscribeDeviceOrientation = function() {
149    console.warn("sensor.unsubscribeDeviceOrientation interface mocked in the Previewer. How this interface works" +
150      " on the Previewer may be different from that on a real device.")
151    clearInterval(this.unsubscribeDevOri)
152    delete this.unsubscribeDevOri
153  }
154}
155
156function mockHeartRate() {
157  global.systemplugin.sensor.subscribeHeartRate = function(...args) {
158    console.warn("sensor.subscribeHeartRate interface mocked in the Previewer. How this interface works on the" +
159      " Previewer may be different from that on a real device.")
160    if (!this.unsubscribeRate) {
161      let ret = {}
162      this.unsubscribeRate = setInterval(() => {
163        ret.heartRate = Math.ceil(Math.random() * 30)
164        args[0].success(ret)
165      }, 500)
166    }
167  },
168  global.systemplugin.sensor.unsubscribeHeartRate = function() {
169    console.warn("sensor.unsubscribeHeartRate interface mocked in the Previewer. How this interface works on the" +
170      " Previewer may be different from that on a real device.")
171    clearInterval(this.unsubscribeRate)
172    delete this.unsubscribeRate
173  }
174}
175
176function mockLight() {
177  global.systemplugin.sensor.subscribeLight = function(...args) {
178    console.warn("sensor.subscribeLight interface mocked in the Previewer. How this interface works on the" +
179      " Previewer may be different from that on a real device.")
180    if (!this.unsubscribeIntensity) {
181      let ret = {}
182      this.unsubscribeIntensity = setInterval(() => {
183        ret.intensity = getRandomArbitrary(660, 680)
184        args[0].success(ret)
185      }, 500)
186    }
187  }
188  global.systemplugin.sensor.unsubscribeLight = function() {
189    console.warn("sensor.unsubscribeLight interface mocked in the Previewer. How this interface works on the" +
190      " Previewer may be different from that on a real device.")
191    clearInterval(this.unsubscribeIntensity)
192    delete this.unsubscribeIntensity
193  }
194}
195
196function mockOnBodyState() {
197  global.systemplugin.sensor.subscribeOnBodyState = function(...args) {
198    console.warn("sensor.subscribeOnBodyState interface mocked in the Previewer. How this interface works on the" +
199      " Previewer may be different from that on a real device.")
200    if (!this.unsubscribeBodyState) {
201      let ret = {}
202      this.unsubscribeBodyState = setInterval(() => {
203        ret.value = Math.ceil(Math.random() * 20)
204        args[0].success(ret)
205      }, 500)
206    }
207  }
208  global.systemplugin.sensor.unsubscribeOnBodyState = function() {
209    console.warn("sensor.unsubscribeOnBodyState interface mocked in the Previewer. How this interface works on the" +
210      " Previewer may be different from that on a real device.")
211    clearInterval(this.unsubscribeBodyState)
212    delete this.unsubscribeBodyState
213  }
214  global.systemplugin.sensor.getOnBodyState = function (...args) {
215    console.warn("sensor.getOnBodyState interface mocked in the Previewer. How this interface works on the" +
216      " Previewer may be different from that on a real device.")
217    let OnBodyStateResponseMock = {
218      value: paramMock.paramBooleanMock
219    }
220    args[0].success(OnBodyStateResponseMock)
221    hasComplete(args[0].complete)
222  }
223}
224
225function mockProximity() {
226  global.systemplugin.sensor.subscribeProximity = function(...args) {
227    console.warn("sensor.subscribeProximity interface mocked in the Previewer. How this interface works on the" +
228      " Previewer may be different from that on a real device.")
229    if (!this.unsubscribeDistance) {
230      let ret = {}
231      this.unsubscribeDistance = setInterval(() => {
232        ret.distance = Math.ceil(Math.random() * 100)
233        args[0].success(ret)
234      }, 1000)
235    }
236  }
237  global.systemplugin.sensor.unsubscribeProximity = function() {
238    console.warn("sensor.unsubscribeProximity interface mocked in the Previewer. How this interface works on the" +
239      " Previewer may be different from that on a real device.")
240    clearInterval(this.unsubscribeDistance)
241    delete this.unsubscribeDistance
242  }
243}
244
245function mockStepCounter() {
246  global.systemplugin.sensor.subscribeStepCounter = function(...args) {
247    console.warn("sensor.subscribeStepCounter interface mocked in the Previewer. How this interface works on the" +
248      " Previewer may be different from that on a real device.")
249    if (!this.unsubscribeSteps) {
250      let ret = {steps: 0}
251      this.unsubscribeSteps = setInterval(() => {
252        ret.steps += 1
253        args[0].success(ret)
254      }, 1000)
255    }
256  }
257  global.systemplugin.sensor.unsubscribeStepCounter = function() {
258    console.warn("sensor.unsubscribeStepCounter interface mocked in the Previewer. How this interface works on the" +
259      " Previewer may be different from that on a real device.")
260    clearInterval(this.unsubscribeSteps)
261    delete this.unsubscribeSteps
262  }
263}