• 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 { ContextClass } from "./Context"
18import { Configuration } from "../ohos_application_Configuration"
19import { Caller } from "../ohos_application_Ability"
20import { AbilityInfo } from "../bundle/abilityInfo"
21import { HapModuleInfo } from "../bundle/hapModuleInfo"
22import { AbilityResult } from "../ability/abilityResult"
23import { PermissionRequestResultClass } from "./PermissionRequestResult"
24
25export const AbilityContextClass = class AbilityContext extends ContextClass {
26  constructor() {
27    super();
28    console.warn('Ability.AbilityContext.constructor interface mocked in the Previewer. How this interface works on' +
29      ' the Previewer may be different from that on a real device.');
30    this.abilityInfo = AbilityInfo;
31    this.currentHapModuleInfo = HapModuleInfo;
32    this.config = Configuration;
33    this.startAbility = function (...args) {
34      console.warn("Ability.AbilityContext.startAbility interface mocked in the Previewer." +
35        " How this interface works on the Previewer may be different from that on a real device.")
36      const len = args.length
37      if (len > 0 && typeof args[len - 1] === 'function') {
38        args[len - 1].call(this, paramMock.businessErrorMock);
39      } else {
40        return new Promise((resolve, reject) => {
41          resolve();
42        })
43      }
44    };
45    this.startAbilityByCall = function (...args) {
46      console.warn("Ability.AbilityContext.startAbilityByCall interface mocked in the Previewer." +
47        " How this interface works on the Previewer may be different from that on a real device.")
48      return new Promise((resolve, reject) => {
49        resolve(Caller);
50      })
51    };
52    this.startAbilityWithAccount = function (...args) {
53      console.warn("Ability.AbilityContext.startAbilityWithAccount interface mocked in the Previewer." +
54        " How this interface works on the Previewer may be different from that on a real device.")
55      const len = args.length
56      if (len > 0 && typeof args[len - 1] === 'function') {
57        args[len - 1].call(this, paramMock.businessErrorMock);
58      } else {
59        return new Promise((resolve, reject) => {
60          resolve();
61        })
62      }
63    };
64    this.startAbilityForResult = function (...args) {
65      console.warn("Ability.AbilityContext.startAbilityForResult interface mocked in the Previewer." +
66        " How this interface works on the Previewer may be different from that on a real device.")
67      const len = args.length
68      if (len > 0 && typeof args[len - 1] === 'function') {
69        args[len - 1].call(this, paramMock.businessErrorMock, AbilityResult);
70      } else {
71        return new Promise((resolve, reject) => {
72          resolve(AbilityResult);
73        })
74      }
75    };
76    this.startAbilityForResultWithAccount = function (...args) {
77      console.warn("Ability.AbilityContext.startAbilityForResultWithAccount interface mocked in the Previewer." +
78        " How this interface works on the Previewer may be different from that on a real device.")
79      const len = args.length
80      if (len > 0 && typeof args[len - 1] === 'function') {
81        args[len - 1].call(this, paramMock.businessErrorMock, AbilityResult);
82      } else {
83        return new Promise((resolve, reject) => {
84          resolve(AbilityResult);
85        })
86      }
87    };
88    this.startServiceExtensionAbility = function (...args) {
89      console.warn("Ability.AbilityContext.startServiceExtensionAbility interface mocked in the Previewer." +
90        " How this interface works on the Previewer may be different from that on a real device.")
91      const len = args.length
92      if (len > 0 && typeof args[len - 1] === 'function') {
93        args[len - 1].call(this, paramMock.businessErrorMock);
94      } else {
95        return new Promise((resolve, reject) => {
96          resolve();
97        })
98      }
99    };
100    this.startServiceExtensionAbilityWithAccount = function (...args) {
101      console.warn("Ability.AbilityContext.startServiceExtensionAbilityWithAccount interface mocked in the Previewer." +
102        " How this interface works on the Previewer may be different from that on a real device.")
103      const len = args.length
104      if (len > 0 && typeof args[len - 1] === 'function') {
105        args[len - 1].call(this, paramMock.businessErrorMock);
106      } else {
107        return new Promise((resolve, reject) => {
108          resolve();
109        })
110      }
111    };
112    this.stopServiceExtensionAbility = function (...args) {
113      console.warn("Ability.AbilityContext.stopServiceExtensionAbility interface mocked in the Previewer." +
114        " How this interface works on the Previewer may be different from that on a real device.")
115      const len = args.length
116      if (len > 0 && typeof args[len - 1] === 'function') {
117        args[len - 1].call(this, paramMock.businessErrorMock);
118      } else {
119        return new Promise((resolve, reject) => {
120          resolve();
121        })
122      }
123    };
124    this.stopServiceExtensionAbilityWithAccount = function (...args) {
125      console.warn("Ability.AbilityContext.stopServiceExtensionAbilityWithAccount interface mocked in the Previewer." +
126        " How this interface works on the Previewer may be different from that on a real device.")
127      const len = args.length
128      if (len > 0 && typeof args[len - 1] === 'function') {
129        args[len - 1].call(this, paramMock.businessErrorMock);
130      } else {
131        return new Promise((resolve, reject) => {
132          resolve();
133        })
134      }
135    };
136    this.terminateSelf = function (...args) {
137      console.warn("Ability.AbilityContext.terminateSelf interface mocked in the Previewer." +
138        " How this interface works on the Previewer may be different from that on a real device.")
139      const len = args.length
140      if (len > 0 && typeof args[len - 1] === 'function') {
141        args[len - 1].call(this, paramMock.businessErrorMock);
142      } else {
143        return new Promise((resolve, reject) => {
144          resolve();
145        })
146      }
147    };
148    this.terminateSelfWithResult = function (...args) {
149      console.warn("Ability.AbilityContext.terminateSelfWithResult interface mocked in the Previewer." +
150        " How this interface works on the Previewer may be different from that on a real device.")
151      const len = args.length
152      if (len > 0 && typeof args[len - 1] === 'function') {
153        args[len - 1].call(this, paramMock.businessErrorMock);
154      } else {
155        return new Promise((resolve, reject) => {
156          resolve();
157        })
158      }
159    };
160    this.connectAbility = function (...args) {
161      console.warn("Ability.AbilityContext.connectAbility interface mocked in the Previewer. How this interface works on the Previewer" +
162        " may be different from that on a real device.")
163      return paramMock.paramNumberMock
164    };
165    this.connectAbilityWithAccount = function (...args) {
166      console.warn("Ability.AbilityContext.connectAbilityWithAccount interface mocked in the Previewer. How this interface works on the Previewer" +
167        " may be different from that on a real device.")
168      return paramMock.paramNumberMock
169    };
170    this.disconnectAbility = function (...args) {
171      console.warn("Ability.AbilityContext.disconnectAbility interface mocked in the Previewer." +
172        " How this interface works on the Previewer may be different from that on a real device.")
173      const len = args.length
174      if (len > 0 && typeof args[len - 1] === 'function') {
175        args[len - 1].call(this, paramMock.businessErrorMock);
176      } else {
177        return new Promise((resolve, reject) => {
178          resolve();
179        })
180      }
181    };
182    this.setMissionLabel = function (...args) {
183      console.warn("Ability.AbilityContext.setMissionLabel interface mocked in the Previewer." +
184        " How this interface works on the Previewer may be different from that on a real device.")
185      const len = args.length
186      if (len > 0 && typeof args[len - 1] === 'function') {
187        args[len - 1].call(this, paramMock.businessErrorMock);
188      } else {
189        return new Promise((resolve, reject) => {
190          resolve();
191        })
192      }
193    };
194    this.setMissionIcon = function (...args) {
195      console.warn("Ability.AbilityContext.setMissionIcon interface mocked in the Previewer." +
196        " How this interface works on the Previewer may be different from that on a real device.")
197      const len = args.length
198      if (len > 0 && typeof args[len - 1] === 'function') {
199        args[len - 1].call(this, paramMock.businessErrorMock);
200      } else {
201        return new Promise((resolve, reject) => {
202          resolve();
203        })
204      }
205    };
206    this.requestPermissionsFromUser = function (...args) {
207      console.warn("Ability.AbilityContext.requestPermissionsFromUser interface mocked in the Previewer." +
208        " How this interface works on the Previewer may be different from that on a real device.")
209      const len = args.length
210      if (len > 0 && typeof args[len - 1] === 'function') {
211        args[len - 1].call(this, paramMock.businessErrorMock, new PermissionRequestResultClass());
212      } else {
213        return new Promise((resolve, reject) => {
214          resolve( new PermissionRequestResultClass());
215        })
216      }
217    };
218    this.restoreWindowStage = function (...args) {
219      console.warn("Ability.AbilityContext.restoreWindowStage interface mocked in the Previewer. How this interface works on the Previewer" +
220        " may be different from that on a real device.")
221    };
222    this.isTerminating = function () {
223      console.warn("Ability.AbilityContext.isTerminating interface mocked in the Previewer. How this interface works on the Previewer" +
224        " may be different from that on a real device.")
225      return paramMock.paramBooleanMock
226    };
227  }
228}
229export function mockAbilityContext() {
230  return new AbilityContextClass();
231}
232