• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { hasComplete } from "./utils"
2
3export function mockGeolocation() {
4    const data = {
5      latitude: '121.61934',
6      longitude: '31.257907',
7      accuracy: '15',
8      time: '160332896544'
9    }
10    global.systemplugin.geolocation = {
11      getLocation: function (...args) {
12        console.warn("geolocation.getLocation interface mocked in the Previewer. How this interface works on the" +
13          " Previewer may be different from that on a real device.")
14        args[0].success(data)
15        hasComplete(args[0].complete)
16      },
17      getLocationType: function(...args) {
18        console.warn("geolocation.getLocationType interface mocked in the Previewer. How this interface works on the" +
19          " Previewer may be different from that on a real device.")
20        const info = {types: ['gps', 'network']}
21        args[0].success(info)
22        hasComplete(args[0].complete)
23      },
24      getSupportedCoordTypes() {
25        console.warn("geolocation.getSupportedCoordTypes interface mocked in the Previewer. How this interface works" +
26          " on the Previewer may be different from that on a real device.")
27        return ["wgs84"]
28      },
29      subscribe: function(...args) {
30        console.warn("geolocation.subscribe interface mocked in the Previewer. How this interface works on the" +
31          " Previewer may be different from that on a real device.")
32        if (!this.unsubscribeLocation) {
33          this.unsubscribeLocation = setInterval(() => {
34            data.latitude = getRandomArbitrary(121, 122)
35            data.longitude = getRandomArbitrary(31, 32)
36            data.accuracy = getRandomArbitrary(14, 18)
37            args[0].success(data)
38          }, 1000)
39        }
40      },
41      unsubscribe: function() {
42        console.warn("geolocation.unsubscribe interface mocked in the Previewer. How this interface works on the" +
43          " Previewer may be different from that on a real device.")
44        clearInterval(this.unsubscribeLocation)
45        delete this.unsubscribeLocation
46      }
47    }
48  }