• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import { getRandomArbitrary, hasComplete, paramMock } from './utils'
17
18export function mockSensor() {
19  global.systemplugin.sensor = {}
20  mockAccelerometer()
21  mockBarometer()
22  mockCompass()
23  mockDeviceOrientation()
24  mockGyroscope()
25  mockHeartRate()
26  mockLight()
27  mockOnBodyState()
28  mockProximity()
29  mockStepCounter()
30  mockGravity()
31  mockMagnetic()
32  mockHall()
33}
34
35function mockAccelerometer() {
36  global.systemplugin.sensor.subscribeAccelerometer = function (...args) {
37    console.warn("sensor.subscribeAccelerometer interface mocked in the Previewer. How this interface works on the" +
38      " Previewer may be different from that on a real device.")
39    const time = {
40      normal: 200,
41      game: 20,
42      ui: 60
43    }
44    let ret = {}
45    let timer = 0
46    if (!args[0].interval) {
47      timer = time.normal
48    } else {
49      timer = time[args[0].interval]
50    }
51    clearInterval(this.unsubscribeAcc)
52    delete this.unsubscribeAcc
53    this.unsubscribeAcc = setInterval(() => {
54      ret.x = Math.ceil(Math.random() * 10)
55      ret.y = Math.ceil(Math.random() * 10)
56      ret.z = Math.ceil(Math.random() * 10)
57      args[0].success(ret)
58    }, timer)
59  }
60  global.systemplugin.sensor.unsubscribeAccelerometer = function () {
61    console.warn("sensor.unsubscribeAccelerometer interface mocked in the Previewer. How this interface works on" +
62      " the Previewer may be different from that on a real device.")
63    clearInterval(this.unsubscribeAcc)
64    delete this.unsubscribeAcc
65  }
66}
67
68function mockBarometer() {
69  global.systemplugin.sensor.subscribeBarometer = function (...args) {
70    console.warn("sensor.subscribeBarometer interface mocked in the Previewer. How this interface works on the" +
71      " Previewer may be different from that on a real device.")
72    if (!this.unsubscribePressure) {
73      let ret = {}
74      this.unsubscribePressure = setInterval(() => {
75        ret.pressure = getRandomArbitrary(1110, 1111)
76        args[0].success(ret)
77      }, 500)
78    }
79  }
80  global.systemplugin.sensor.unsubscribeBarometer = function () {
81    console.warn("sensor.unsubscribeBarometer interface mocked in the Previewer. How this interface works on the" +
82      " Previewer may be different from that on a real device.")
83    clearInterval(this.unsubscribePressure)
84    delete this.unsubscribePressure
85  }
86}
87
88function mockCompass() {
89  global.systemplugin.sensor.subscribeCompass = function (...args) {
90    console.warn("sensor.subscribeCompass interface mocked in the Previewer. How this interface works on the" +
91      " Previewer may be different from that on a real device.")
92    if (!this.unsubscribeDirection) {
93      let ret = {}
94      this.unsubscribeDirection = setInterval(() => {
95        ret.direction = getRandomArbitrary(49, 50)
96        args[0].success(ret)
97      }, 100)
98    }
99  }
100  global.systemplugin.sensor.unsubscribeCompass = function () {
101    console.warn("sensor.unsubscribeCompass interface mocked in the Previewer. How this interface works on the" +
102      " Previewer may be different from that on a real device.")
103    clearInterval(this.unsubscribeDirection)
104    delete this.unsubscribeDirection
105  }
106}
107
108function mockGyroscope() {
109  global.systemplugin.sensor.subscribeGyroscope = function (...args) {
110    console.warn("sensor.subscribeGyroscope interface mocked in the Previewer. How this interface works on the" +
111      " Previewer may be different from that on a real device.")
112    const time = {
113      normal: 200,
114      game: 20,
115      ui: 60
116    }
117    let ret = {}
118    let timer = 0
119    if (!args[0].interval) {
120      timer = time.normal
121    } else {
122      timer = time[args[0].interval]
123    }
124    clearInterval(this.unsubscribeGyr)
125    delete this.unsubscribeGyr
126    this.unsubscribeGyr = setInterval(() => {
127      ret.x = Math.ceil(Math.random() * 10)
128      ret.y = Math.ceil(Math.random() * 10)
129      ret.z = Math.ceil(Math.random() * 10)
130      args[0].success(ret)
131    }, timer)
132  }
133  global.systemplugin.sensor.unsubscribeGyroscope = function () {
134    console.warn("sensor.unsubscribeGyroscope interface mocked in the Previewer. How this interface works on the" +
135      " Previewer may be different from that on a real device.")
136    clearInterval(this.unsubscribeGyr)
137    delete this.unsubscribeGyr
138  }
139}
140
141function mockDeviceOrientation() {
142  global.systemplugin.sensor.subscribeDeviceOrientation = function (...args) {
143    console.warn("sensor.subscribeDeviceOrientation interface mocked in the Previewer. How this interface works on" +
144      " the Previewer may be different from that on a real device.")
145    const time = {
146      normal: 200,
147      game: 20,
148      ui: 60
149    }
150    let ret = {}
151    let timer = 0
152    if (!args[0].interval) {
153      timer = time.normal
154    } else {
155      timer = time[args[0].interval]
156    }
157    clearInterval(this.unsubscribeDevOri)
158    delete this.unsubscribeDevOri
159    this.unsubscribeDevOri = setInterval(() => {
160      ret.alpha = Math.ceil(Math.random() * 10)
161      ret.beta = Math.ceil(Math.random() * 10)
162      ret.gamma = Math.ceil(Math.random() * 10)
163      args[0].success(ret)
164    }, timer)
165  }
166  global.systemplugin.sensor.unsubscribeDeviceOrientation = function () {
167    console.warn("sensor.unsubscribeDeviceOrientation interface mocked in the Previewer. How this interface works" +
168      " on the Previewer may be different from that on a real device.")
169    clearInterval(this.unsubscribeDevOri)
170    delete this.unsubscribeDevOri
171  }
172}
173
174function mockHeartRate() {
175  global.systemplugin.sensor.subscribeHeartRate = function (...args) {
176    console.warn("sensor.subscribeHeartRate interface mocked in the Previewer. How this interface works on the" +
177      " Previewer may be different from that on a real device.")
178    if (!this.unsubscribeRate) {
179      let ret = {}
180      this.unsubscribeRate = setInterval(() => {
181        ret.heartRate = Math.ceil(Math.random() * 30)
182        args[0].success(ret)
183      }, 500)
184    }
185  }
186  global.systemplugin.sensor.unsubscribeHeartRate = function () {
187    console.warn("sensor.unsubscribeHeartRate interface mocked in the Previewer. How this interface works on the" +
188      " Previewer may be different from that on a real device.")
189    clearInterval(this.unsubscribeRate)
190    delete this.unsubscribeRate
191  }
192}
193
194function mockLight() {
195  global.systemplugin.sensor.subscribeLight = function (...args) {
196    console.warn("sensor.subscribeLight interface mocked in the Previewer. How this interface works on the" +
197      " Previewer may be different from that on a real device.")
198    if (!this.unsubscribeIntensity) {
199      let ret = {}
200      this.unsubscribeIntensity = setInterval(() => {
201        ret.intensity = getRandomArbitrary(660, 680)
202        args[0].success(ret)
203      }, 500)
204    }
205  }
206  global.systemplugin.sensor.unsubscribeLight = function () {
207    console.warn("sensor.unsubscribeLight interface mocked in the Previewer. How this interface works on the" +
208      " Previewer may be different from that on a real device.")
209    clearInterval(this.unsubscribeIntensity)
210    delete this.unsubscribeIntensity
211  }
212}
213
214function mockOnBodyState() {
215  global.systemplugin.sensor.subscribeOnBodyState = function (...args) {
216    console.warn("sensor.subscribeOnBodyState interface mocked in the Previewer. How this interface works on the" +
217      " Previewer may be different from that on a real device.")
218    if (!this.unsubscribeBodyState) {
219      let ret = {}
220      this.unsubscribeBodyState = setInterval(() => {
221        ret.value = Math.ceil(Math.random() * 20)
222        args[0].success(ret)
223      }, 500)
224    }
225  }
226  global.systemplugin.sensor.unsubscribeOnBodyState = function () {
227    console.warn("sensor.unsubscribeOnBodyState interface mocked in the Previewer. How this interface works on the" +
228      " Previewer may be different from that on a real device.")
229    clearInterval(this.unsubscribeBodyState)
230    delete this.unsubscribeBodyState
231  }
232  global.systemplugin.sensor.getOnBodyState = function (...args) {
233    console.warn("sensor.getOnBodyState interface mocked in the Previewer. How this interface works on the" +
234      " Previewer may be different from that on a real device.")
235    let OnBodyStateResponseMock = {
236      value: paramMock.paramBooleanMock
237    }
238    args[0].success(OnBodyStateResponseMock)
239    hasComplete(args[0].complete)
240  }
241}
242
243function mockProximity() {
244  global.systemplugin.sensor.subscribeProximity = function (...args) {
245    console.warn("sensor.subscribeProximity interface mocked in the Previewer. How this interface works on the" +
246      " Previewer may be different from that on a real device.")
247    if (!this.unsubscribeDistance) {
248      let ret = {}
249      this.unsubscribeDistance = setInterval(() => {
250        ret.distance = Math.ceil(Math.random() * 100)
251        args[0].success(ret)
252      }, 1000)
253    }
254  }
255  global.systemplugin.sensor.unsubscribeProximity = function () {
256    console.warn("sensor.unsubscribeProximity interface mocked in the Previewer. How this interface works on the" +
257      " Previewer may be different from that on a real device.")
258    clearInterval(this.unsubscribeDistance)
259    delete this.unsubscribeDistance
260  }
261}
262
263function mockStepCounter() {
264  global.systemplugin.sensor.subscribeStepCounter = function (...args) {
265    console.warn("sensor.subscribeStepCounter interface mocked in the Previewer. How this interface works on the" +
266      " Previewer may be different from that on a real device.")
267    if (!this.unsubscribeSteps) {
268      let ret = { steps: 0 }
269      this.unsubscribeSteps = setInterval(() => {
270        ret.steps += 1
271        args[0].success(ret)
272      }, 1000)
273    }
274  }
275  global.systemplugin.sensor.unsubscribeStepCounter = function () {
276    console.warn("sensor.unsubscribeStepCounter interface mocked in the Previewer. How this interface works on the" +
277      " Previewer may be different from that on a real device.")
278    clearInterval(this.unsubscribeSteps)
279    delete this.unsubscribeSteps
280  }
281}
282
283function mockGravity() {
284  global.systemplugin.sensor.subscribeGravity = function (...args) {
285    console.warn("sensor.subscribeGravity interface mocked in the Previewer. How this interface works on" +
286      " the Previewer may be different from that on a real device.")
287    const time = {
288      normal: 200,
289      game: 20,
290      ui: 60
291    }
292    let ret = {}
293    let timer = 0
294    if (!args[0].interval) {
295      timer = time.normal
296    } else {
297      timer = time[args[0].interval]
298    }
299    clearInterval(this.unsubscribeGrav)
300    delete this.unsubscribeGrav
301    this.unsubscribeGrav = setInterval(() => {
302      ret.x = Math.ceil(Math.random() * 10)
303      ret.y = Math.ceil(Math.random() * 10)
304      ret.z = Math.ceil(Math.random() * 10)
305      args[0].success(ret)
306    }, timer)
307  }
308  global.systemplugin.sensor.unsubscribeGravity = function () {
309    console.warn("sensor.unsubscribeGravity interface mocked in the Previewer. How this interface works" +
310      " on the Previewer may be different from that on a real device.")
311    clearInterval(this.unsubscribeGrav)
312    delete this.unsubscribeGrav
313  }
314}
315
316function mockMagnetic() {
317  global.systemplugin.sensor.subscribeMagnetic = function (...args) {
318    console.warn("sensor.subscribeMagnetic interface mocked in the Previewer. How this interface works on" +
319      " the Previewer may be different from that on a real device.")
320    const time = {
321      normal: 200,
322      game: 20,
323      ui: 60
324    }
325    let ret = {}
326    let timer = 0
327    if (!args[0].interval) {
328      timer = time.normal
329    } else {
330      timer = time[args[0].interval]
331    }
332    clearInterval(this.unsubscribeMag)
333    delete this.unsubscribeMag
334    this.unsubscribeMag = setInterval(() => {
335      ret.x = getRandomArbitrary(-100, 100)
336      ret.y = getRandomArbitrary(-100, 100)
337      ret.z = getRandomArbitrary(-100, 100)
338      args[0].success(ret)
339    }, timer)
340  }
341  global.systemplugin.sensor.unsubscribeMagnetic = function () {
342    console.warn("sensor.unsubscribeMagnetic interface mocked in the Previewer. How this interface works" +
343      " on the Previewer may be different from that on a real device.")
344    clearInterval(this.unsubscribeMag)
345    delete this.unsubscribeMag
346  }
347}
348
349function mockHall() {
350  global.systemplugin.sensor.subscribeHall = function (...args) {
351    console.warn("sensor.subscribeHall interface mocked in the Previewer. How this interface works on" +
352      " the Previewer may be different from that on a real device.")
353    const time = {
354      normal: 200,
355      game: 20,
356      ui: 60
357    }
358    let ret = {}
359    let timer = 0
360    if (!args[0].interval) {
361      timer = time.normal
362    } else {
363      timer = time[args[0].interval]
364    }
365    clearInterval(this.unsubscribeHal)
366    delete this.unsubscribeHal
367    this.unsubscribeHal = setInterval(() => {
368      ret.value = Math.round(Math.random())
369      args[0].success(ret)
370    }, timer)
371  }
372  global.systemplugin.sensor.unsubscribeHall = function () {
373    console.warn("sensor.unsubscribeHall interface mocked in the Previewer. How this interface works" +
374      " on the Previewer may be different from that on a real device.")
375    clearInterval(this.unsubscribeHal)
376    delete this.unsubscribeHal
377  }
378}