• 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 { hasComplete, getRandomArbitrary } from "./utils"
17
18export function mockGeolocation() {
19  const data = {
20    latitude: '121.61934',
21    longitude: '31.257907',
22    accuracy: '15',
23    time: '160332896544'
24  }
25  global.systemplugin.geolocation = {
26    getLocation: function (...args) {
27      console.warn("geolocation.getLocation interface mocked in the Previewer. How this interface works on the" +
28        " Previewer may be different from that on a real device.")
29      args[0].success(data)
30      hasComplete(args[0].complete)
31    },
32    getLocationType: function (...args) {
33      console.warn("geolocation.getLocationType interface mocked in the Previewer. How this interface works on the" +
34        " Previewer may be different from that on a real device.")
35      const info = { types: ['gps', 'network'] }
36      args[0].success(info)
37      hasComplete(args[0].complete)
38    },
39    getSupportedCoordTypes() {
40      console.warn("geolocation.getSupportedCoordTypes interface mocked in the Previewer. How this interface works" +
41        " on the Previewer may be different from that on a real device.")
42      return ["wgs84"]
43    },
44    subscribe: function (...args) {
45      console.warn("geolocation.subscribe interface mocked in the Previewer. How this interface works on the" +
46        " Previewer may be different from that on a real device.")
47      if (!this.unsubscribeLocation) {
48        this.unsubscribeLocation = setInterval(() => {
49          data.latitude = getRandomArbitrary(121, 122)
50          data.longitude = getRandomArbitrary(31, 32)
51          data.accuracy = getRandomArbitrary(14, 18)
52          args[0].success(data)
53        }, 1000)
54      }
55    },
56    unsubscribe: function () {
57      console.warn("geolocation.unsubscribe interface mocked in the Previewer. How this interface works on the" +
58        " Previewer may be different from that on a real device.")
59      clearInterval(this.unsubscribeLocation)
60      delete this.unsubscribeLocation
61    }
62  }
63}