• 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"
17import { MessageParcelClass } from "../ohos_rpc"
18import { ContextClass } from "../ohos_application_ServiceExtAbility"
19
20const ModuleInfoMock = {
21  moduleName: '[PC preview] unknow moduleName',
22  moduleSourceDir: '[PC preview] unknow moduleSourceDir',
23}
24export const AbilityInfoMock = {
25  bundleName: '[PC preview] unknow bundleName',
26  name: '[PC preview] unknow name',
27  label: '[PC preview] unknow label',
28  description: '[PC preview] unknow description',
29  icon: '[PC preview] unknow icon',
30  labelId: '[PC preview] unknow labelId',
31  descriptionId: '[PC preview] unknow descriptionId',
32  iconId: '[PC preview] unknow iconId',
33  moduleName: '[PC preview] unknow moduleName',
34  process: '[PC preview] unknow process',
35  targetAbility: '[PC preview] unknow targetAbility',
36  backgroundModes: '[PC preview] unknow backgroundModes',
37  isVisible: '[PC preview] unknow isVisible',
38  abilityTypeMock: '[PC preview] unknow abilityTypeMock',
39  AbilitySubTypeMock: '[PC preview] unknow AbilitySubTypeMock',
40  DisplayOrientationMock: '[PC preview] unknow DisplayOrientationMock',
41  launchModeMock: '[PC preview] unknow launchModeMock',
42  formEnabled: '[PC preview] unknow formEnabled',
43  permissions: [paramMock.paramStringMock],
44  deviceTypes: [paramMock.paramStringMock],
45  deviceCapabilities: [paramMock.paramStringMock],
46  readPermission: '[PC preview] unknow readPermission',
47  writePermission: '[PC preview] unknow writePermission',
48  applicationInfo: applicationInfoMock,
49  formEntity: '[PC preview] unknow formEntity',
50  minFormHeight: '[PC preview] unknow minFormHeight',
51  defaultFormHeight: '[PC preview] unknow defaultFormHeight',
52  minFormWidth: '[PC preview] unknow minFormWidth',
53  defaultFormWidth: '[PC preview] unknow defaultFormWidth',
54  uri: '[PC preview] unknow uri',
55  customizeData: new Map([["[PC Preview] unknow customizeDataKey", "[PC Preview] unknow customizeDataValue"]])
56}
57const applicationInfoMock = {
58  name: '[PC preview] unknow name',
59  description: '[PC preview] unknow description',
60  descriptionId: '[PC preview] unknow descriptionId',
61  systemApp: '[PC preview] unknow systemApp',
62  enabled: '[PC preview] unknow enabled',
63  label: '[PC preview] unknow label',
64  labelId: '[PC preview] unknow labelId',
65  icon: '[PC preview] unknow icon',
66  iconId: '[PC preview] unknow iconId',
67  process: '[PC preview] unknow process',
68  supportedModes: '[PC preview] unknow supportedModes',
69  moduleSourceDirs: [paramMock.paramStringMock],
70  permissions: [paramMock.paramStringMock],
71  moduleInfo: [ModuleInfoMock],
72  entryDir: '[PC preview] unknow entryDir',
73  codepath: '[PC preview] unknow codepath',
74  removable: '[PC preview] unknow removable',
75  customizeData: new Map([["[PC Preview] unknow customizeDataKey", "[PC Preview] unknow customizeDataValue"]])
76}
77const HapModuleInfoMock = {
78  name: '[PC preview] unknow name',
79  description: '[PC preview] unknow description',
80  descriptionId: '[PC preview] unknow descriptionId',
81  icon: '[PC preview] unknow icon',
82  label: '[PC preview] unknow label',
83  labelId: '[PC preview] unknow labelId',
84  iconId: '[PC preview] unknow iconId',
85  backgroundImg: '[PC preview] unknow backgroundImg',
86  supportedModes: '[PC preview] unknow supportedModes',
87  reqCapabilities: [paramMock.paramStringMock],
88  deviceTypes: [paramMock.paramStringMock],
89  abilityInfo: AbilityInfoMock,
90  moduleName: '[PC preview] unknow moduleName',
91  mainAbilityName: '[PC preview] unknow mainAbilityName',
92  installationFree: '[PC preview] unknow installationFree',
93}
94const screenDensityMock = {
95  SCREEN_SDPI: '[PC preview] unknow SCREEN_SDPI',
96  SCREEN_MDPI: '[PC preview] unknow SCREEN_MDPI',
97  SCREEN_LDPI: '[PC preview] unknow SCREEN_LDPI',
98  SCREEN_XLDPI: '[PC preview] unknow SCREEN_XLDPI',
99  SCREEN_XXLDPI: '[PC preview] unknow SCREEN_XXLDPI',
100  SCREEN_XXXLDPI: '[PC preview] unknow SCREEN_XXXLDPI'
101}
102const deviceTypeMock = {
103  DEVICE_TYPE_PHONE: '[PC preview] unknow DEVICE_TYPE_PHONE',
104  DEVICE_TYPE_TABLET: '[PC preview] unknow DEVICE_TYPE_TABLET',
105  DEVICE_TYPE_CAR: '[PC preview] unknow DEVICE_TYPE_CAR',
106  DEVICE_TYPE_PC: '[PC preview] unknow DEVICE_TYPE_PC',
107  DEVICE_TYPE_TV: '[PC preview] unknow DEVICE_TYPE_TV',
108  DEVICE_TYPE_WEARABLE: '[PC preview] unknow DEVICE_TYPE_WEARABLE'
109}
110const DeviceCapabilityClass = class DeviceCapability {
111  constructor() {
112    console.warn('DeviceCapability.constructor interface mocked in the Previewer. How this interface works on' +
113      ' the Previewer may be different from that on a real device.');
114    this.screenDensity = screenDensityMock;
115    this.deviceType = deviceTypeMock;
116  }
117}
118const configurationClass = class configuration {
119  constructor() {
120    console.warn('configuration.constructor interface mocked in the Previewer. How this interface works on' +
121      ' the Previewer may be different from that on a real device.');
122    this.directionMock = '[PC preview] unknow directionMock';
123    this.locale = '[PC preview] unknow locale';
124  }
125}
126const resourceManagerMock = {
127  getString: function (...args) {
128    console.warn("ResourceManager.getString interface mocked in the Previewer." +
129      " How this interface works on the Previewer may be different from that on a real device.")
130    const len = args.length
131    if (len > 0 && typeof args[len - 1] === 'function') {
132      args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
133    } else {
134      return new Promise((resolve, reject) => {
135        resolve(paramMock.paramStringMock);
136      })
137    }
138  },
139  getStringArray: function (...args) {
140    console.warn("ResourceManager.getStringArray interface mocked in the Previewer." +
141      " How this interface works on the Previewer may be different from that on a real device.")
142    const len = args.length
143    if (len > 0 && typeof args[len - 1] === 'function') {
144      args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paramStringMock]);
145    } else {
146      return new Promise((resolve, reject) => {
147        resolve([paramMock.paramStringMock]);
148      })
149    }
150  },
151  getMedia: function (...args) {
152    console.warn("ResourceManager.getMedia interface mocked in the Previewer." +
153      " How this interface works on the Previewer may be different from that on a real device.")
154    const len = args.length
155    if (len > 0 && typeof args[len - 1] === 'function') {
156      args[len - 1].call(this, paramMock.businessErrorMock, new Uint8Array());
157    } else {
158      return new Promise((resolve, reject) => {
159        resolve(new Uint8Array());
160      })
161    }
162  },
163  getMediaBase64: function (...args) {
164    console.warn("ResourceManager.getMediaBase64 interface mocked in the Previewer." +
165      " How this interface works on the Previewer may be different from that on a real device.")
166    const len = args.length
167    if (len > 0 && typeof args[len - 1] === 'function') {
168      args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
169    } else {
170      return new Promise((resolve, reject) => {
171        resolve(paramMock.paramStringMock);
172      })
173    }
174  },
175  getDeviceCapability: function (...args) {
176    console.warn("ResourceManager.getDeviceCapability interface mocked in the Previewer." +
177      " How this interface works on the Previewer may be different from that on a real device.")
178    const len = args.length
179    if (len > 0 && typeof args[len - 1] === 'function') {
180      args[len - 1].call(this, paramMock.businessErrorMock, new DeviceCapabilityClass());
181    } else {
182      return new Promise((resolve, reject) => {
183        resolve(new DeviceCapabilityClass());
184      })
185    }
186  },
187  getConfiguration: function (...args) {
188    console.warn("ResourceManager.getConfiguration interface mocked in the Previewer." +
189      " How this interface works on the Previewer may be different from that on a real device.")
190    const len = args.length
191    if (len > 0 && typeof args[len - 1] === 'function') {
192      args[len - 1].call(this, paramMock.businessErrorMock, new configurationClass());
193    } else {
194      return new Promise((resolve, reject) => {
195        resolve(new configurationClass());
196      })
197    }
198  },
199  getPluralString: function (...args) {
200    console.warn("ResourceManager.getPluralString interface mocked in the Previewer." +
201      " How this interface works on the Previewer may be different from that on a real device.")
202    const len = args.length
203    if (len > 0 && typeof args[len - 1] === 'function') {
204      args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
205    } else {
206      return new Promise((resolve, reject) => {
207        resolve(paramMock.paramStringMock);
208      })
209    }
210  },
211  getRawFile: function (...args) {
212    console.warn("ResourceManager.getRawFile interface mocked in the Previewer." +
213      " How this interface works on the Previewer may be different from that on a real device.")
214    const len = args.length
215    if (len > 0 && typeof args[len - 1] === 'function') {
216      args[len - 1].call(this, paramMock.businessErrorMock, new Uint8Array());
217    } else {
218      return new Promise((resolve, reject) => {
219        resolve(new Uint8Array());
220      })
221    }
222  },
223}
224export const abilityStageContextClass = class abilityStageContext extends ContextClass {
225  constructor() {
226    super();
227    console.warn('Context.abilityStageContext.constructor interface mocked in the Previewer. How this interface works on' +
228      ' the Previewer may be different from that on a real device.');
229    this.currentHapModuleInfo = HapModuleInfoMock;
230  }
231}
232export const ExtensionContextClass = class ExtensionContext extends ContextClass {
233  constructor() {
234    super();
235    console.warn('Context.ExtensionContext.constructor interface mocked in the Previewer. How this interface works on' +
236      ' the Previewer may be different from that on a real device.');
237    this.currentHapModuleInfo = HapModuleInfoMock;
238  }
239};
240export const AbilityResultMock = {
241  resultCode: "[PC Preview] unknow resultCode",
242}
243const ConfigurationMock = {
244  language: '[PC preview] unknow language'
245}
246const PermissionRequestResultMock = {
247  permissions: [paramMock.paramStringMock],
248  authResults: [paramMock.paramNumberMock]
249}
250const CallerMock = {
251  call: function (...args) {
252    console.warn("AbilityContext.Caller.call interface mocked in the Previewer." +
253      " How this interface works on the Previewer may be different from that on a real device.")
254    return new Promise((resolve, reject) => {
255      resolve();
256    })
257  },
258  callWithResult: function (...args) {
259    console.warn("AbilityContext.Caller.callWithResult interface mocked in the Previewer." +
260      " How this interface works on the Previewer may be different from that on a real device.")
261    return new Promise((resolve, reject) => {
262      resolve(new MessageParcelClass());
263    })
264  },
265  release: function (...args) {
266    console.warn("AbilityContext.Caller.release interface mocked in the Previewer. How this interface works on the Previewer" +
267      " may be different from that on a real device.")
268  },
269  onRelease: function (...args) {
270    console.warn("AbilityContext.Caller.onRelease interface mocked in the Previewer. How this interface works on the Previewer" +
271      " may be different from that on a real device.")
272  },
273}
274export const AbilityContextClass = class AbilityContext extends ContextClass {
275  constructor() {
276    super();
277    console.warn('Ability.AbilityContext.constructor interface mocked in the Previewer. How this interface works on' +
278      ' the Previewer may be different from that on a real device.');
279    this.abilityInfo = AbilityInfoMock;
280    this.currentHapModuleInfo = HapModuleInfoMock;
281    this.config = ConfigurationMock;
282    this.startAbility = function (...args) {
283      console.warn("Ability.AbilityContext.startAbility interface mocked in the Previewer." +
284        " How this interface works on the Previewer may be different from that on a real device.")
285      const len = args.length
286      if (len > 0 && typeof args[len - 1] === 'function') {
287        args[len - 1].call(this, paramMock.businessErrorMock);
288      } else {
289        return new Promise((resolve, reject) => {
290          resolve();
291        })
292      }
293    };
294    this.startAbilityByCall = function (...args) {
295      console.warn("Ability.AbilityContext.startAbilityByCall interface mocked in the Previewer." +
296        " How this interface works on the Previewer may be different from that on a real device.")
297      return new Promise((resolve, reject) => {
298        resolve(CallerMock);
299      })
300    };
301    this.startAbilityWithAccount = function (...args) {
302      console.warn("Ability.AbilityContext.startAbilityWithAccount interface mocked in the Previewer." +
303        " How this interface works on the Previewer may be different from that on a real device.")
304      const len = args.length
305      if (len > 0 && typeof args[len - 1] === 'function') {
306        args[len - 1].call(this, paramMock.businessErrorMock);
307      } else {
308        return new Promise((resolve, reject) => {
309          resolve();
310        })
311      }
312    };
313    this.startAbilityForResult = function (...args) {
314      console.warn("Ability.AbilityContext.startAbilityForResult interface mocked in the Previewer." +
315        " How this interface works on the Previewer may be different from that on a real device.")
316      const len = args.length
317      if (len > 0 && typeof args[len - 1] === 'function') {
318        args[len - 1].call(this, paramMock.businessErrorMock, AbilityResultMock);
319      } else {
320        return new Promise((resolve, reject) => {
321          resolve(AbilityResultMock);
322        })
323      }
324    };
325    this.startAbilityForResultWithAccount = function (...args) {
326      console.warn("Ability.AbilityContext.startAbilityForResultWithAccount interface mocked in the Previewer." +
327        " How this interface works on the Previewer may be different from that on a real device.")
328      const len = args.length
329      if (len > 0 && typeof args[len - 1] === 'function') {
330        args[len - 1].call(this, paramMock.businessErrorMock, AbilityResultMock);
331      } else {
332        return new Promise((resolve, reject) => {
333          resolve(AbilityResultMock);
334        })
335      }
336    };
337    this.terminateSelf = function (...args) {
338      console.warn("Ability.AbilityContext.terminateSelf interface mocked in the Previewer." +
339        " How this interface works on the Previewer may be different from that on a real device.")
340      const len = args.length
341      if (len > 0 && typeof args[len - 1] === 'function') {
342        args[len - 1].call(this, paramMock.businessErrorMock);
343      } else {
344        return new Promise((resolve, reject) => {
345          resolve();
346        })
347      }
348    };
349    this.terminateSelfWithResult = function (...args) {
350      console.warn("Ability.AbilityContext.terminateSelfWithResult interface mocked in the Previewer." +
351        " How this interface works on the Previewer may be different from that on a real device.")
352      const len = args.length
353      if (len > 0 && typeof args[len - 1] === 'function') {
354        args[len - 1].call(this, paramMock.businessErrorMock);
355      } else {
356        return new Promise((resolve, reject) => {
357          resolve();
358        })
359      }
360    };
361    this.connectAbility = function (...args) {
362      console.warn("Ability.AbilityContext.connectAbility interface mocked in the Previewer. How this interface works on the Previewer" +
363        " may be different from that on a real device.")
364      return paramMock.paramNumberMock
365    };
366    this.connectAbilityWithAccount = function (...args) {
367      console.warn("Ability.AbilityContext.connectAbilityWithAccount interface mocked in the Previewer. How this interface works on the Previewer" +
368        " may be different from that on a real device.")
369      return paramMock.paramNumberMock
370    };
371    this.disconnectAbility = function (...args) {
372      console.warn("Ability.AbilityContext.disconnectAbility interface mocked in the Previewer." +
373        " How this interface works on the Previewer may be different from that on a real device.")
374      const len = args.length
375      if (len > 0 && typeof args[len - 1] === 'function') {
376        args[len - 1].call(this, paramMock.businessErrorMock);
377      } else {
378        return new Promise((resolve, reject) => {
379          resolve();
380        })
381      }
382    };
383    this.setMissionLabel = function (...args) {
384      console.warn("Ability.AbilityContext.setMissionLabel interface mocked in the Previewer." +
385        " How this interface works on the Previewer may be different from that on a real device.")
386      const len = args.length
387      if (len > 0 && typeof args[len - 1] === 'function') {
388        args[len - 1].call(this, paramMock.businessErrorMock);
389      } else {
390        return new Promise((resolve, reject) => {
391          resolve();
392        })
393      }
394    };
395    this.requestPermissionsFromUser = function (...args) {
396      console.warn("Ability.AbilityContext.requestPermissionsFromUser interface mocked in the Previewer." +
397        " How this interface works on the Previewer may be different from that on a real device.")
398      const len = args.length
399      if (len > 0 && typeof args[len - 1] === 'function') {
400        args[len - 1].call(this, paramMock.businessErrorMock, PermissionRequestResultMock);
401      } else {
402        return new Promise((resolve, reject) => {
403          resolve(PermissionRequestResultMock);
404        })
405      }
406    };
407  }
408}
409export function mockAbilityContext() {
410  return new AbilityContextClass();
411}
412