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