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 hiSysEvent from "@ohos.hiSysEvent"
17
18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
19
20describe('hiSysEventPermissionJsTest', function () {
21    beforeAll(function() {
22
23        /**
24         * @tc.setup: setup invoked before all test cases
25         */
26        console.info('hiSysEventPermissionJsTest beforeAll called')
27    })
28
29    afterAll(function() {
30
31        /**
32         * @tc.teardown: teardown invoked after all test cases
33         */
34        console.info('hiSysEventPermissionJsTest afterAll called')
35    })
36
37    beforeEach(function() {
38
39        /**
40         * @tc.setup: setup invoked before each test case
41         */
42        console.info('hiSysEventPermissionJsTest beforeEach called')
43    })
44
45    afterEach(function() {
46
47        /**
48         * @tc.teardown: teardown invoked after each test case
49         */
50        console.info('hiSysEventPermissionJsTest afterEach called')
51    })
52
53    /**
54     * @tc.name: hiSysEventPermissionJsTest001
55     * @tc.desc: Test the write interface with callbak.
56     * @tc.type: FUNC
57     */
58    it('hiSysEventPermissionJsTest001', 0, async function (done) {
59        console.info('hiSysEventPermissionJsTest001 start')
60        try {
61            hiSysEvent.write({
62                domain: "RELIABILITY",
63                name: "STACK",
64                eventType: hiSysEvent.EventType.FAULT,
65                params: {}
66            }, (err) => {
67                expect(false).assertTrue()
68                done()
69            })
70        } catch (err) {
71            expect(err.code == 202).assertTrue()
72            console.info('hiSysEventPermissionJsTest001 end')
73            done()
74        }
75    })
76
77    /**
78     * @tc.name: hiSysEventPermissionJsTest002
79     * @tc.desc: Test the write interface with promise.
80     * @tc.type: FUNC
81     */
82     it('hiSysEventPermissionJsTest002', 0, async function (done) {
83        console.info('hiSysEventPermissionJsTest002 start')
84        try {
85            hiSysEvent.write({
86                domain: "RELIABILITY",
87                name: "STACK",
88                eventType: hiSysEvent.EventType.FAULT,
89                params: {}
90            }).then(
91                () => {
92                    expect(false).assertTrue()
93                    done()
94                }
95            ).catch(
96                (err) => {
97                    expect(false).assertTrue()
98                    done()
99                }
100            );
101        } catch (err) {
102            expect(err.code == 202).assertTrue()
103            console.info('hiSysEventPermissionJsTest002 end')
104            done()
105        }
106    })
107
108    /**
109     * @tc.name: hiSysEventPermissionJsTest003
110     * @tc.desc: Test the addWatcher interface.
111     * @tc.type: FUNC
112     */
113    it('hiSysEventPermissionJsTest003', 0, function () {
114        console.info('hiSysEventPermissionJsTest003 start')
115        let watcher = {
116            rules: [{
117                domain: "RELIABILITY",
118                name: "STACK",
119                tag: "STABILITY",
120                ruleType: hiSysEvent.RuleType.WHOLE_WORD,
121            }],
122            onEvent: (info) => {},
123            onServiceDied: () => {}
124        }
125        try {
126            hiSysEvent.addWatcher(watcher)
127        } catch (err) {
128            expect(err.code == 202).assertTrue()
129            console.info('hiSysEventPermissionJsTest003 end')
130        }
131    })
132
133    /**
134     * @tc.name: hiSysEventPermissionJsTest004
135     * @tc.desc: Test the removeWatcher interface.
136     * @tc.type: FUNC
137     */
138     it('hiSysEventPermissionJsTest004', 0, function () {
139        console.info('hiSysEventPermissionJsTest004 start')
140        let watcher = {
141            rules: [{
142                domain: "RELIABILITY",
143                name: "STACK",
144                tag: "STABILITY",
145                ruleType: hiSysEvent.RuleType.WHOLE_WORD,
146            }],
147            onEvent: (info) => {},
148            onServiceDied: () => {}
149        }
150        try {
151            hiSysEvent.removeWatcher(watcher)
152        } catch (err) {
153            expect(err.code == 202).assertTrue()
154            console.info('hiSysEventPermissionJsTest004 end')
155        }
156    })
157
158    /**
159     * @tc.name: hiSysEventPermissionJsTest005
160     * @tc.desc: Test the query interface.
161     * @tc.type: FUNC
162     */
163    it('hiSysEventPermissionJsTest005', 0, function () {
164        console.info('hiSysEventPermissionJsTest005 start')
165        try {
166            hiSysEvent.query({
167                beginTime: 0,
168                endTime: 0,
169                maxEvents: 100,
170            }, [{
171                domain: "RELIABILITY",
172                names: ["STACK"],
173            }], {
174                onQuery: function () {},
175                onComplete: function() {}
176            })
177        } catch (err) {
178            expect(err.code == 202).assertTrue()
179            console.info('hiSysEventPermissionJsTest005 end')
180        }
181    })
182});