• 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 { paramMock } from "../utils"
17
18export function mockOhosGeolocation() {
19  const Location = {
20    latitude: '[PC preview] unknow latitude',
21    longitude: '[PC preview] unknow longitude',
22    altitude: '[PC preview] unknow altitude',
23    accuracy: '[PC preview] unknow accuracy',
24    speed: '[PC preview] unknow speed',
25    timeStamp: '[PC preview] unknow timeStamp',
26    direction: '[PC preview] unknow direction',
27    timeSinceBoot: '[PC preview] unknow timeSinceBoot',
28    additions: [paramMock.paramStringMock],
29    additionSize: '[PC preview] unknow additionSize',
30  }
31  const GeoAddress = {
32    latitude: '[PC preview] unknow latitude',
33    longitude: '[PC preview] unknow longitude',
34    locale: '[PC preview] unknow locale',
35    placeName: '[PC preview] unknow placeName',
36    countryCode: '[PC preview] unknow countryCode',
37    countryName: '[PC preview] unknow countryName',
38    administrativeArea: '[PC preview] unknow administrativeArea',
39    subAdministrativeArea: '[PC preview] unknow subAdministrativeArea',
40    locality: '[PC preview] unknow locality',
41    subLocality: '[PC preview] unknow subLocality',
42    roadName: '[PC preview] unknow roadName',
43    subRoadName: '[PC preview] unknow subRoadName',
44    premises: '[PC preview] unknow premises',
45    postalCode: '[PC preview] unknow postalCode',
46    phoneNumber: '[PC preview] unknow phoneNumber',
47    addressUrl: '[PC preview] unknow addressUrl',
48    descriptions: [paramMock.paramStringMock],
49    descriptionsSize: '[PC preview] unknow descriptionsSize',
50  }
51  const SatelliteStatusInfo = {
52    satellitesNumber: '[PC preview] unknow satellitesNumber',
53    satelliteIds: [paramMock.paramNumberMock],
54    carrierToNoiseDensitys: [paramMock.paramNumberMock],
55    altitudes: [paramMock.paramNumberMock],
56    azimuths: [paramMock.paramNumberMock],
57    carrierFrequencies: [paramMock.paramNumberMock],
58  }
59  const CountryCodeType = {
60    COUNTRY_CODE_FROM_LOCALE: 1,
61    COUNTRY_CODE_FROM_SIM: 2,
62    COUNTRY_CODE_FROM_LOCATION: 3,
63    COUNTRY_CODE_FROM_NETWORK: 4,
64  }
65  const CountryCode = {
66    country: '[PC preview] unknow country',
67    type: CountryCodeType,
68  }
69  const geolocation = {
70    on: function (...args) {
71      console.warn("geolocation.on interface mocked in the Previewer. How this interface works on the" +
72        " Previewer may be different from that on a real device.");
73      const len = args.length;
74      if (typeof args[len - 1] === 'function') {
75        if (args[0] == 'locationChange') {
76            args[len - 1].call(this, Location);
77        } else if (args[0] == 'locationServiceState') {
78            args[len - 1].call(this, paramMock.paramBooleanMock);
79        } else if (args[0] == 'cachedGnssLocationsReporting') {
80            args[len - 1].call(this, [Location]);
81        } else if (args[0] == 'gnssStatusChange') {
82            args[len - 1].call(this, SatelliteStatusInfo);
83        } else if (args[0] == 'nmeaMessageChange') {
84            args[len - 1].call(this, paramMock.paramStringMock);
85        } else if (args[0] == 'countryCodeChange') {
86            args[len - 1].call(this, CountryCode);
87        }
88      }
89    },
90    off: function (...args) {
91      console.warn("geolocation.off interface mocked in the Previewer. How this interface works on the" +
92        " Previewer may be different from that on a real device.");
93      const len = args.length;
94      if (typeof args[len - 1] === 'function') {
95        if (args[0] == 'locationChange') {
96            args[len - 1].call(this, Location);
97        } else if (args[0] == 'locationServiceState') {
98            args[len - 1].call(this, paramMock.paramBooleanMock);
99        } else if (args[0] == 'cachedGnssLocationsReporting') {
100            args[len - 1].call(this, [Location]);
101        } else if (args[0] == 'gnssStatusChange') {
102            args[len - 1].call(this, SatelliteStatusInfo);
103        } else if (args[0] == 'nmeaMessageChange') {
104            args[len - 1].call(this, paramMock.paramStringMock);
105        } else if (args[0] == 'countryCodeChange') {
106            args[len - 1].call(this, CountryCode);
107        }
108      }
109    },
110    getCurrentLocation: function (...args) {
111      console.warn("geolocation.getCurrentLocation interface mocked in the Previewer. How this interface works on the" +
112        " Previewer may be different from that on a real device.");
113      const len = args.length;
114      if (typeof args[len - 1] === 'function') {
115        args[len - 1].call(this, paramMock.businessErrorMock, Location);
116      } else {
117        return new Promise((resolve) => {
118          resolve(Location);
119        });
120      }
121    },
122    getLastLocation: function (...args) {
123      console.warn("geolocation.getLastLocation interface mocked in the Previewer. How this interface works on the" +
124        " Previewer may be different from that on a real device.");
125      const len = args.length;
126      if (typeof args[len - 1] === 'function') {
127        args[len - 1].call(this, paramMock.businessErrorMock, Location);
128      } else {
129        return new Promise((resolve) => {
130          resolve(Location);
131        });
132      }
133    },
134    isLocationEnabled: function (...args) {
135      console.warn("geolocation.isLocationEnabled interface mocked in the Previewer. How this interface works on the" +
136        " Previewer may be different from that on a real device.");
137      const len = args.length;
138      if (typeof args[len - 1] === 'function') {
139        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
140      } else {
141        return new Promise((resolve) => {
142          resolve(paramMock.paramBooleanMock);
143        });
144      }
145    },
146    requestEnableLocation: function (...args) {
147      console.warn("geolocation.requestEnableLocation interface mocked in the Previewer. How this interface works on the" +
148        " Previewer may be different from that on a real device.");
149      const len = args.length;
150      if (typeof args[len - 1] === 'function') {
151        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
152      } else {
153        return new Promise((resolve) => {
154          resolve(paramMock.paramBooleanMock);
155        });
156      }
157    },
158    enableLocation: function (...args) {
159      console.warn("geolocation.enableLocation interface mocked in the Previewer. How this interface works on the" +
160        " Previewer may be different from that on a real device.");
161      const len = args.length;
162      if (typeof args[len - 1] === 'function') {
163        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
164      } else {
165        return new Promise((resolve) => {
166          resolve(paramMock.paramBooleanMock);
167        });
168      }
169    },
170    disableLocation: function (...args) {
171      console.warn("geolocation.disableLocation interface mocked in the Previewer. How this interface works on the" +
172        " Previewer may be different from that on a real device.");
173      const len = args.length;
174      if (typeof args[len - 1] === 'function') {
175        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
176      } else {
177        return new Promise((resolve) => {
178          resolve(paramMock.paramBooleanMock);
179        });
180      }
181    },
182    getAddressesFromLocation: function (...args) {
183      console.warn("geolocation.getAddressesFromLocation interface mocked in the Previewer. How this interface works on the" +
184        " Previewer may be different from that on a real device.");
185      const len = args.length;
186      if (typeof args[len - 1] === 'function') {
187        args[len - 1].call(this, paramMock.businessErrorMock, [GeoAddress]);
188      } else {
189        return new Promise((resolve) => {
190          resolve([GeoAddress]);
191        });
192      }
193    },
194    getAddressesFromLocationName: function (...args) {
195      console.warn("geolocation.getAddressesFromLocationName interface mocked in the Previewer. How this interface works on the" +
196        " Previewer may be different from that on a real device.");
197      const len = args.length;
198      if (typeof args[len - 1] === 'function') {
199        args[len - 1].call(this, paramMock.businessErrorMock, [GeoAddress]);
200      } else {
201        return new Promise((resolve) => {
202          resolve([GeoAddress]);
203        });
204      }
205    },
206    isGeoServiceAvailable: function (...args) {
207      console.warn("geolocation.isGeoServiceAvailable interface mocked in the Previewer. How this interface works on the" +
208        " Previewer may be different from that on a real device.");
209      const len = args.length;
210      if (typeof args[len - 1] === 'function') {
211        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
212      } else {
213        return new Promise((resolve) => {
214          resolve(paramMock.paramBooleanMock);
215        });
216      }
217    },
218    getCachedGnssLocationsSize: function (...args) {
219      console.warn("geolocation.getCachedGnssLocationsSize interface mocked in the Previewer. How this interface works on the" +
220        " Previewer may be different from that on a real device.");
221      const len = args.length;
222      if (typeof args[len - 1] === 'function') {
223          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
224      } else {
225          return new Promise((resolve) => {
226              resolve(paramMock.paramNumberMock);
227          });
228      }
229    },
230    flushCachedGnssLocations: function (...args) {
231      console.warn("geolocation.flushCachedGnssLocations interface mocked in the Previewer. How this interface works on the" +
232        " Previewer may be different from that on a real device.");
233      const len = args.length;
234      if (typeof args[len - 1] === 'function') {
235          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
236      } else {
237          return new Promise((resolve) => {
238              resolve(paramMock.paramBooleanMock);
239          });
240      }
241    },
242    sendCommand: function (...args) {
243      console.warn("geolocation.sendCommand interface mocked in the Previewer. How this interface works on the" +
244        " Previewer may be different from that on a real device.");
245      const len = args.length;
246      if (typeof args[len - 1] === 'function') {
247          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
248      } else {
249          return new Promise((resolve) => {
250              resolve(paramMock.paramBooleanMock);
251          });
252      }
253    },
254    getCountryCode: function (...args) {
255      console.warn("geolocation.getCountryCode interface mocked in the Previewer. How this interface works on the" +
256        " Previewer may be different from that on a real device.");
257      const len = args.length;
258      if (typeof args[len - 1] === 'function') {
259          args[len - 1].call(this, paramMock.businessErrorMock, CountryCode);
260      } else {
261          return new Promise((resolve) => {
262              resolve(CountryCode);
263          });
264      }
265    },
266    enableLocationMock: function (...args) {
267      console.warn("geolocation.enableLocationMock interface mocked in the Previewer. How this interface works on the" +
268        " Previewer may be different from that on a real device.");
269      const len = args.length;
270      if (typeof args[len - 1] === 'function') {
271          args[len - 1].call(this, paramMock.businessErrorMock);
272      } else {
273          return new Promise((resolve) => {
274              resolve();
275          });
276      }
277    },
278    disableLocationMock: function (...args) {
279      console.warn("geolocation.disableLocationMock interface mocked in the Previewer. How this interface works on the" +
280        " Previewer may be different from that on a real device.");
281      const len = args.length;
282      if (typeof args[len - 1] === 'function') {
283          args[len - 1].call(this, paramMock.businessErrorMock);
284      } else {
285          return new Promise((resolve) => {
286              resolve();
287          });
288      }
289    },
290    setMockedLocations: function (...args) {
291      console.warn("geolocation.setMockedLocations interface mocked in the Previewer. How this interface works on the" +
292        " Previewer may be different from that on a real device.");
293      const len = args.length;
294      if (typeof args[len - 1] === 'function') {
295          args[len - 1].call(this, paramMock.businessErrorMock);
296      } else {
297          return new Promise((resolve) => {
298              resolve();
299          });
300      }
301    },
302    enableReverseGeocodingMock: function (...args) {
303      console.warn("geolocation.enableReverseGeocodingMock interface mocked in the Previewer. How this interface works on the" +
304        " Previewer may be different from that on a real device.");
305      const len = args.length;
306      if (typeof args[len - 1] === 'function') {
307          args[len - 1].call(this, paramMock.businessErrorMock);
308      } else {
309          return new Promise((resolve) => {
310              resolve();
311          });
312      }
313    },
314    disableReverseGeocodingMock: function (...args) {
315      console.warn("geolocation.disableReverseGeocodingMock interface mocked in the Previewer. How this interface works on the" +
316        " Previewer may be different from that on a real device.");
317      const len = args.length;
318      if (typeof args[len - 1] === 'function') {
319          args[len - 1].call(this, paramMock.businessErrorMock);
320      } else {
321          return new Promise((resolve) => {
322              resolve();
323          });
324      }
325    },
326    setReverseGeocodingMockInfo: function (...args) {
327      console.warn("geolocation.setReverseGeocodingMockInfo interface mocked in the Previewer. How this interface works on the" +
328        " Previewer may be different from that on a real device.");
329      const len = args.length;
330      if (typeof args[len - 1] === 'function') {
331          args[len - 1].call(this, paramMock.businessErrorMock);
332      } else {
333          return new Promise((resolve) => {
334              resolve();
335          });
336      }
337    },
338    isLocationPrivacyConfirmed: function (...args) {
339      console.warn("geolocation.isLocationPrivacyConfirmed interface mocked in the Previewer. How this interface works on the" +
340        " Previewer may be different from that on a real device.");
341      const len = args.length;
342      if (typeof args[len - 1] === 'function') {
343          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
344      } else {
345          return new Promise((resolve) => {
346              resolve(paramMock.paramBooleanMock);
347          });
348      }
349    },
350    setLocationPrivacyConfirmStatus: function (...args) {
351      console.warn("geolocation.setLocationPrivacyConfirmStatus interface mocked in the Previewer. How this interface works on the" +
352        " Previewer may be different from that on a real device.");
353      const len = args.length;
354      if (typeof args[len - 1] === 'function') {
355          args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
356      } else {
357          return new Promise((resolve) => {
358              resolve(paramMock.paramBooleanMock);
359          });
360      }
361    },
362  }
363  return geolocation
364}
365