1/*
2 * Copyright (C) 2021-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 hiTraceChain from "@ohos.hiTraceChain"
17
18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
19
20describe('hiTraceChainJsUnitTest', function () {
21    beforeAll(function() {
22
23        /**
24         * @tc.setup: setup invoked before all test cases
25         */
26        console.info('hiTraceChainJsUnitTest beforeAll called')
27    })
28
29    afterAll(function() {
30
31        /**
32         * @tc.teardown: teardown invoked after all test cases
33         */
34        console.info('hiTraceChainJsUnitTest afterAll called')
35    })
36
37    beforeEach(function() {
38
39        /**
40         * @tc.setup: setup invoked before each test case
41         */
42        console.info('hiTraceChainJsUnitTest beforeEach called')
43    })
44
45    afterEach(function() {
46
47        /**
48         * @tc.teardown: teardown invoked after each test case
49         */
50        console.info('hiTraceChainJsUnitTest afterEach called')
51    })
52
53    /**
54     * @tc.name: hiTraceChainJsUnitTest001
55     * @tc.desc: test hiTraceChain.begin/hiTraceChain.isValid/hiTraceChain.end
56     * @tc.type: FUNC
57     */
58    it('hiTraceChainJsUnitTest001', 0, async function (done) {
59        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest001")
60        expect(hiTraceChain.isValid(traceId)).assertTrue()
61        hiTraceChain.end(traceId)
62        done()
63    });
64
65    /**
66     * @tc.name: hiTraceChainJsUnitTest002
67     * @tc.desc: test hiTraceChain.creadSpan
68     * @tc.type: FUNC
69     */
70    it('hiTraceChainJsUnitTest002', 0, async function (done) {
71        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest002")
72        traceId = hiTraceChain.createSpan()
73        expect(hiTraceChain.isValid(traceId)).assertTrue()
74        hiTraceChain.end(traceId)
75        done()
76    });
77
78    /**
79     * @tc.name: hiTraceChainJsUnitTest003
80     * @tc.desc: test hiTraceChain.tracepoint
81     * @tc.type: FUNC
82     */
83     it('hiTraceChainJsUnitTest003', 0, async function (done) {
84        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest003")
85        expect(hiTraceChain.isValid(traceId)).assertTrue()
86        hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.DEFAULT,
87            hiTraceChain.HiTraceTracepointType.CS, traceId, "hiTraceChainJsUnitTest003 test case")
88        expect(hiTraceChain.isValid(traceId)).assertTrue()
89        hiTraceChain.end(traceId);
90        done();
91    });
92
93    /**
94     * @tc.name: hiTraceChainJsUnitTest004
95     * @tc.desc: test hiTraceChain.isFlagEnabled
96     * @tc.type: FUNC
97     */
98    it('hiTraceChainJsUnitTest004', 0, async function (done) {
99        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest004")
100        expect(!hiTraceChain.isFlagEnabled(traceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC)).assertTrue()
101        hiTraceChain.enableFlag(traceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC)
102        expect(hiTraceChain.isFlagEnabled(traceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC)).assertTrue()
103        hiTraceChain.end(traceId)
104        done();
105    });
106
107    /**
108     * @tc.name: hiTraceChainJsUnitTest005
109     * @tc.desc: test call begin api function with parameters of wrong number
110     * @tc.type: FUNC
111     */
112    it('hiTraceChainJsUnitTest005', 0, async function (done) {
113        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest005", "testp1", "testp2")
114        expect(!hiTraceChain.isValid(traceId)).assertTrue()
115        hiTraceChain.end(traceId)
116        done();
117    });
118
119    /**
120     * @tc.name: hiTraceChainJsUnitTest006
121     * @tc.desc: test hiTraceChain.begin with undefined flag
122     * @tc.type: FUNC
123     */
124    it('hiTraceChainJsUnitTest006', 0, async function (done) {
125        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest006", undefined)
126        expect(hiTraceChain.isValid(traceId)).assertTrue()
127        hiTraceChain.end(traceId)
128        done()
129    });
130
131    /**
132     * @tc.name: hiTraceChainJsUnitTest007
133     * @tc.desc: test hiTraceChain.tracepoint with undefined msg
134     * @tc.type: FUNC
135     */
136    it('hiTraceChainJsUnitTest007', 0, async function (done) {
137        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest007")
138        expect(hiTraceChain.isValid(traceId)).assertTrue()
139        hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.DEFAULT,
140            hiTraceChain.HiTraceTracepointType.CS, traceId, undefined)
141        expect(hiTraceChain.isValid(traceId)).assertTrue()
142        hiTraceChain.end(traceId);
143        done();
144    });
145
146    /**
147     * @tc.name: hiTraceChainJsUnitTest008
148     * @tc.desc: test hiTraceChain.begin with null flag
149     * @tc.type: FUNC
150     */
151    it('hiTraceChainJsUnitTest008', 0, async function (done) {
152        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest008", null)
153        expect(hiTraceChain.isValid(traceId)).assertTrue()
154        hiTraceChain.end(traceId)
155        done()
156    });
157
158    /**
159     * @tc.name: hiTraceChainJsUnitTest009
160     * @tc.desc: test hiTraceChain.tracepoint with null msg
161     * @tc.type: FUNC
162     */
163    it('hiTraceChainJsUnitTest009', 0, async function (done) {
164        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest009")
165        expect(hiTraceChain.isValid(traceId)).assertTrue()
166        hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.DEFAULT,
167            hiTraceChain.HiTraceTracepointType.CS, traceId, null)
168        expect(hiTraceChain.isValid(traceId)).assertTrue()
169        hiTraceChain.end(traceId);
170        done();
171    });
172
173    /**
174     * @tc.name: hiTraceChainJsUnitTest010
175     * @tc.desc: test hiTraceChain.tracepoint with no msg
176     * @tc.type: FUNC
177     */
178    it('hiTraceChainJsUnitTest010', 0, async function (done) {
179        let traceId = hiTraceChain.begin("hiTraceChainJsUnitTest010")
180        expect(hiTraceChain.isValid(traceId)).assertTrue()
181        hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.DEFAULT,
182            hiTraceChain.HiTraceTracepointType.CS, traceId)
183        expect(hiTraceChain.isValid(traceId)).assertTrue()
184        hiTraceChain.end(traceId);
185        done();
186    });
187});