• 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 mockCommonEvent() {
19  const CommonEventSubscriberMock = {
20    getCode: function (...args) {
21      console.warn("CommonEventSubscriber.getCode interface mocked in the Previewer. How this interface works on the" +
22        " Previewer may be different from that on a real device.");
23      const len = args.length;
24      if (typeof args[len - 1] === 'function') {
25        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
26      } else {
27        return new Promise((resolve) => {
28          resolve(paramMock.paramNumberMock);
29        });
30      }
31    },
32    setCode: function (...args) {
33      console.warn("CommonEventSubscriber.setCode interface mocked in the Previewer. How this interface works on the" +
34        " Previewer may be different from that on a real device.");
35      const len = args.length;
36      if (typeof args[len - 1] === 'function') {
37        args[len - 1].call(this, paramMock.businessErrorMock);
38      } else {
39        return new Promise((resolve) => {
40          resolve();
41        });
42      }
43    },
44    getData: function (...args) {
45      console.warn("CommonEventSubscriber.getData interface mocked in the Previewer. How this interface works on the" +
46        " Previewer may be different from that on a real device.");
47      const len = args.length;
48      if (typeof args[len - 1] === 'function') {
49        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock);
50      } else {
51        return new Promise((resolve) => {
52          resolve(paramMock.paramStringMock);
53        });
54      }
55    },
56    setData: function (...args) {
57      console.warn("CommonEventSubscriber.setData interface mocked in the Previewer. How this interface works on the" +
58        " Previewer may be different from that on a real device.");
59      const len = args.length;
60      if (typeof args[len - 1] === 'function') {
61        args[len - 1].call(this, paramMock.businessErrorMock);
62      } else {
63        return new Promise((resolve) => {
64          resolve();
65        });
66      }
67    },
68    setCodeAndData: function (...args) {
69      console.warn("CommonEventSubscriber.setCodeAndData interface mocked in the Previewer. How this interface works on the" +
70        " Previewer may be different from that on a real device.");
71      const len = args.length;
72      if (typeof args[len - 1] === 'function') {
73        args[len - 1].call(this, paramMock.businessErrorMock);
74      } else {
75        return new Promise((resolve) => {
76          resolve();
77        });
78      }
79    },
80    isOrderedCommonEvent: function (...args) {
81      console.warn("CommonEventSubscriber.isOrderedCommonEvent interface mocked in the Previewer. How this interface works on the" +
82        " Previewer may be different from that on a real device.");
83      const len = args.length;
84      if (typeof args[len - 1] === 'function') {
85        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
86      } else {
87        return new Promise((resolve) => {
88          resolve(paramMock.paramBooleanMock);
89        });
90      }
91    },
92    isStickyCommonEvent: function (...args) {
93      console.warn("CommonEventSubscriber.isStickyCommonEvent interface mocked in the Previewer. How this interface works on the" +
94        " Previewer may be different from that on a real device.");
95      const len = args.length;
96      if (typeof args[len - 1] === 'function') {
97        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
98      } else {
99        return new Promise((resolve) => {
100          resolve(paramMock.paramBooleanMock);
101        });
102      }
103    },
104    abortCommonEvent: function (...args) {
105      console.warn("CommonEventSubscriber.abortCommonEvent interface mocked in the Previewer. How this interface works on the" +
106        " Previewer may be different from that on a real device.");
107      const len = args.length;
108      if (typeof args[len - 1] === 'function') {
109        args[len - 1].call(this, paramMock.businessErrorMock);
110      } else {
111        return new Promise((resolve) => {
112          resolve();
113        });
114      }
115    },
116    clearAbortCommonEvent: function (...args) {
117      console.warn("CommonEventSubscriber.clearAbortCommonEvent interface mocked in the Previewer. How this interface works on the" +
118        " Previewer may be different from that on a real device.");
119      const len = args.length;
120      if (typeof args[len - 1] === 'function') {
121        args[len - 1].call(this, paramMock.businessErrorMock);
122      } else {
123        return new Promise((resolve) => {
124          resolve();
125        });
126      }
127    },
128    getAbortCommonEvent: function (...args) {
129      console.warn("CommonEventSubscriber.getAbortCommonEvent interface mocked in the Previewer. How this interface works on the" +
130        " Previewer may be different from that on a real device.");
131      const len = args.length;
132      if (typeof args[len - 1] === 'function') {
133        args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock);
134      } else {
135        return new Promise((resolve) => {
136          resolve(paramMock.paramBooleanMock);
137        });
138      }
139    },
140    getSubscribeInfo: function (...args) {
141      console.warn("CommonEventSubscriber.getSubscribeInfo interface mocked in the Previewer. How this interface works on the" +
142        " Previewer may be different from that on a real device.");
143      const len = args.length;
144      if (typeof args[len - 1] === 'function') {
145        args[len - 1].call(this, paramMock.businessErrorMock, CommonEventSubscribeInfoMock);
146      } else {
147        return new Promise((resolve) => {
148          resolve(CommonEventSubscribeInfoMock);
149        });
150      }
151    }
152  }
153  const CommonEventDataMock = {
154    event: '[PC preview] unknow event',
155    bundleName: '[PC preview] unknow bundleName',
156    code: '[PC preview] unknow code',
157    data: '[PC preview] unknow data',
158    parameters: '[PC preview] unknow parameters',
159  }
160  const CommonEventSubscribeInfoMock = {
161    events: '[PC preview] unknow events',
162    publisherPermission: '[PC preview] unknow publisherPermission',
163    publisherDeviceId: '[PC preview] unknow publisherDeviceId',
164    userId: '[PC preview] unknow userId',
165    priority: '[PC preview] unknow priority',
166  }
167  global.ohosplugin.commonEvent = {
168    publish: function (...args) {
169      console.warn("commonEvent.publish interface mocked in the Previewer. How this interface works on the" +
170        " Previewer may be different from that on a real device.");
171      const len = args.length;
172      if (typeof args[len - 1] === 'function') {
173        args[len - 1].call(this, paramMock.businessErrorMock);
174      }
175    },
176    createSubscriber: function (...args) {
177      console.warn("commonEvent.createSubscriber interface mocked in the Previewer. How this interface works on the" +
178        " Previewer may be different from that on a real device.");
179      const len = args.length;
180      if (typeof args[len - 1] === 'function') {
181        args[len - 1].call(this, paramMock.businessErrorMock, CommonEventSubscriberMock);
182      } else {
183        return new Promise((resolve) => {
184          resolve(CommonEventSubscriberMock);
185        });
186      }
187    },
188    subscribe: function (...args) {
189      console.warn("commonEvent.subscribe interface mocked in the Previewer. How this interface works on the" +
190        " Previewer may be different from that on a real device.");
191      const len = args.length;
192      if (typeof args[len - 1] === 'function') {
193        args[len - 1].call(this, paramMock.businessErrorMock, CommonEventDataMock);
194      }
195    },
196    unsubscribe: function (...args) {
197      console.warn("commonEvent.unsubscribe interface mocked in the Previewer. How this interface works on the" +
198        " Previewer may be different from that on a real device.");
199      const len = args.length;
200      if (typeof args[len - 1] === 'function') {
201        args[len - 1].call(this, paramMock.businessErrorMock);
202      }
203    },
204  }
205
206}
207