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 18function buildMockInfo(interfaceName) { 19 return interfaceName + " interface mocked in the Previewer. How this interface works on the Previewer" + 20 " may be different from that on a real device." 21} 22 23export function mockHiSysEvent() { 24 const EventType = { 25 FAULT: 1, 26 STATISTIC: 2, 27 SECURITY: 3, 28 BEHAVIOR: 4 29 } 30 const SysEventInfo = { 31 domain: paramMock.paramStringMock, 32 name: paramMock.paramStringMock, 33 eventType: EventType, 34 params: paramMock.paramObjectMock 35 } 36 const RuleType = { 37 WHOLE_WORD: 1, 38 PREFIX: 2, 39 REGULAR: 3 40 } 41 const WatchRule = { 42 domain: paramMock.paramStringMock, 43 name: paramMock.paramStringMock, 44 tag: paramMock.paramStringMock, 45 ruleType: RuleType 46 } 47 const Watcher = { 48 rules: [WatchRule], 49 onEvent: function(...args) { 50 console.warn(buildMockInfo("hisysEvent.Watcher.onEvent")) 51 }, 52 onServiceDied: function(...args) { 53 console.warn(buildMockInfo("hisysEvent.Watcher.onServiceDied")) 54 } 55 } 56 const QueryArg = { 57 beginTime: paramMock.paramNumberMock, 58 endTime: paramMock.paramNumberMock, 59 maxEvents: paramMock.paramNumberMock 60 } 61 const QueryRule = { 62 domain: paramMock.paramStringMock, 63 names: [paramMock.paramStringMock] 64 } 65 const Querier = { 66 onQuery: function(...args) { 67 console.warn(buildMockInfo("hisysEvent.Querier.onQuery")) 68 }, 69 onComplete: function(...args) { 70 console.warn(buildMockInfo("hisysEvent.Querier.onComplete")) 71 } 72 } 73 const hiSysEvent = { 74 EventType, 75 SysEventInfo, 76 RuleType, 77 WatchRule, 78 Watcher, 79 QueryArg, 80 QueryRule, 81 Querier, 82 write: function (...args) { 83 console.warn(buildMockInfo("hiSysEvent.write")) 84 const len = args.length 85 if (typeof args[len - 1] === 'function') { 86 args[len - 1].call(this, paramMock.businessErrorMock) 87 } else { 88 return new Promise((resolve) => { 89 resolve() 90 }) 91 } 92 }, 93 addWatcher: function(...args) { 94 console.warn(buildMockInfo("hiSysEvent.addWatcher")) 95 return paramMock.paramNumberMock; 96 }, 97 removeWatcher: function(...args) { 98 console.warn(buildMockInfo("hiSysEvent.removeWatcher")) 99 return paramMock.paramNumberMock; 100 }, 101 query: function(...args) { 102 console.warn(buildMockInfo("hiSysEvent.query")) 103 return paramMock.paramNumberMock; 104 } 105 } 106 return hiSysEvent 107}