• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import faultlogger from '@ohos.faultLogger'
16
17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
18
19describe("FaultlogJsTest", function () {
20    beforeAll(function() {
21        /*
22         * @tc.setup: setup invoked before all testcases
23         */
24         console.info('FaultlogJsTest beforeAll called')
25    })
26
27    afterAll(function() {
28        /*
29         * @tc.teardown: teardown invoked after all testcases
30         */
31         console.info('FaultlogJsTest afterAll called')
32    })
33
34    beforeEach(function() {
35        /*
36         * @tc.setup: setup invoked before each testcases
37         */
38         console.info('FaultlogJsTest beforeEach called')
39    })
40
41    afterEach(function() {
42        /*
43         * @tc.teardown: teardown invoked after each testcases
44         */
45         console.info('FaultlogJsTest afterEach called')
46    })
47
48    async function msleep(time) {
49        let promise = new Promise((resolve, reject) => {
50            setTimeout(() => resolve("done!"), time)
51        });
52        let result = await promise;
53    }
54
55    /**
56     * test
57     *
58     * @tc.name: FaultlogJsException_001
59     * @tc.desc: API8 检验函数参数输入错误时程序是否会崩溃
60     * @tc.require: AR000GICT2
61     * @tc.author:
62     */
63    it('FaultlogJsException_001', 0, async function (done) {
64        console.info("---------------------------FaultlogJsException_001----------------------------------");
65        try {
66            let ret1 = faultlogger.querySelfFaultLog("faultloggertestsummary01");
67            console.info("FaultlogJsException_001 ret1 == " + ret1);
68            let a = expect(ret1).assertEqual(undefined);
69            console.info('ret1 assertEqual(undefined) ' + a);
70
71            let ret2 = faultlogger.querySelfFaultLog(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary01");
72            console.info("FaultlogJsException_001 ret2 == " + ret2);
73            expect(ret2).assertEqual(undefined);
74
75            let ret3 = faultlogger.querySelfFaultLog();
76            console.info("FaultlogJsException_001 ret3 == " + ret3);
77            expect(ret3).assertEqual(undefined);
78            done();
79            return;
80        } catch(err) {
81            console.info(err);
82        }
83        expect(false).assertTrue();
84        done();
85    })
86
87    /**
88     * test
89     *
90     * @tc.name: FaultlogJsException_002
91     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
92     * @tc.require: issueI5VRCC
93     * @tc.author:
94     */
95     it('FaultlogJsException_002', 0, function () {
96        console.info("---------------------------FaultlogJsException_002----------------------------------");
97        try {
98            let ret = faultlogger.query("faultloggertestsummary02");
99            console.info("FaultlogJsException_002 ret == " + ret);
100            return;
101        } catch(err) {
102            console.info(err.code);
103            console.info(err.message);
104            expect(err.code == 401).assertTrue();
105        }
106    })
107
108    /**
109     * test
110     *
111     * @tc.name: FaultlogJsException_003
112     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
113     * @tc.require: issueI5VRCC
114     * @tc.author:
115     */
116     it('FaultlogJsException_003', 0, function () {
117        console.info("---------------------------FaultlogJsException_003----------------------------------");
118        try {
119            let ret = faultlogger.query(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary03");
120            console.info("FaultlogJsException_003 ret == " + ret);
121            return;
122        } catch(err) {
123            console.info(err.code);
124            console.info(err.message);
125            expect(err.code == 401).assertTrue();
126        }
127    })
128
129    /**
130     * test
131     *
132     * @tc.name: FaultlogJsException_004
133     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
134     * @tc.require: issueI5VRCC
135     * @tc.author:
136     */
137     it('FaultlogJsException_004', 0, function () {
138        console.info("---------------------------FaultlogJsException_004----------------------------------");
139        try {
140            let ret = faultlogger.query();
141            console.info("FaultlogJsException_004 ret == " + ret);
142            return;
143        } catch(err) {
144            console.info(err.code);
145            console.info(err.message);
146            expect(err.code == 401).assertTrue();
147        }
148    })
149
150    /**
151     * test
152     *
153     * @tc.name: FaultlogJsException_005
154     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
155     * @tc.require: issueI5VRCC
156     * @tc.author:
157     */
158    it('FaultlogJsException_005', 0, function () {
159        console.info("---------------------------FaultlogJsException_005----------------------------------");
160        try {
161            let ret = faultlogger.query("aaa", "bbb", "ccc");
162            console.info("FaultlogJsException_005 ret == " + ret);
163            return;
164        } catch(err) {
165            console.info(err.code);
166            console.info(err.message);
167            expect(err.code == 401).assertTrue();
168        }
169    })
170
171    /**
172     * test
173     *
174     * @tc.name: FaultlogJsTest_005
175     * @tc.desc: API9 检验promise同步方式获取faultlog日志
176     * @tc.require: issueI5VRCC
177     * @tc.author:
178     */
179     it('FaultlogJsTest_005', 0, async function (done) {
180        console.info("---------------------------FaultlogJsTest_005----------------------------------");
181        try {
182            let now = Date.now();
183            console.info("FaultlogJsTest_005 2 + " + now);
184            let module = "com.ohos.hiviewtest.faultlogjs";
185            const loopTimes = 10;
186            for (let i = 0; i < loopTimes; i++) {
187                console.info("--------FaultlogJsTest_005 3 + " + i + "----------");
188                faultlogger.addFaultLog(i - 200,
189                    faultlogger.FaultType.CPP_CRASH, module, "faultloggertestsummary05 " + i);
190                await msleep(300);
191            }
192            await msleep(1000);
193
194            console.info("--------FaultlogJsTest_005 4" + "----------");
195            let ret = await faultlogger.query(faultlogger.FaultType.CPP_CRASH);
196            console.info("FaultlogJsTest_005 ret == " + ret.length);
197            expect(ret.length).assertEqual(loopTimes);
198            for (let i = 0; i < loopTimes; i++) {
199                console.info("faultloggertestsummary05 " + i + " fullLog.length " + ret[i].fullLog.length);
200                console.info(ret[i].fullLog);
201                if (ret[i].fullLog.indexOf("faultloggertestsummary05 " + (loopTimes - 1 - i)) != -1) {
202                    console.info("FaultlogJsTest_005 " + ret[i].fullLog.length);
203                    expect(true).assertTrue();
204                } else {
205                    expect(false).assertTrue();
206                }
207            }
208            done();
209            return;
210        } catch (err) {
211            console.info("catch (err) == " + err);
212        }
213        console.info("FaultlogJsTest_005 error");
214        expect(false).assertTrue();
215        done();
216    })
217
218    /**
219     * test
220     *
221     * @tc.name: FaultlogJsTest_006
222     * @tc.desc: API9 检验通过回调方式获取faultlog日志
223     * @tc.require: issueI5VRCC
224     * @tc.author:
225     */
226    it('FaultlogJsTest_006', 0, async function (done) {
227        console.info("---------------------------FaultlogJsTest_006----------------------------------");
228        try {
229            let now = Date.now();
230            console.info("FaultlogJsTest_006 start + " + now);
231            let module = "com.ohos.hiviewtest.faultlogjs";
232            const loopTimes = 10;
233            for (let i = 0; i < loopTimes; i++) {
234                console.info("--------FaultlogJsTest_006 + " + i + "----------");
235                faultlogger.addFaultLog(i - 100,
236                    faultlogger.FaultType.CPP_CRASH, module, "faultloggertestsummary06 " + i);
237                await msleep(300);
238            }
239            await msleep(1000);
240
241            console.info("--------FaultlogJsTest_006 4----------");
242            function queryFaultLogCallback(error, ret) {
243                if (error) {
244                    console.info('FaultlogJsTest_006  once error is ' + error);
245                } else {
246                    console.info("FaultlogJsTest_006 ret == " + ret.length);
247                    expect(ret.length).assertEqual(loopTimes);
248                    for (let i = 0; i < loopTimes; i++) {
249                        console.info("faultloggertestsummary06 " + i + " fullLog.length " + ret[i].fullLog.length);
250                        console.info(ret[i].fullLog);
251                        if (ret[i].fullLog.indexOf("faultloggertestsummary06 " + (loopTimes - 1 - i)) != -1) {
252                            console.info("FaultlogJsTest_006 " + ret[i].fullLog.length);
253                            expect(true).assertTrue();
254                        } else {
255                            expect(false).assertTrue();
256                        }
257                    }
258                }
259                done();
260            }
261            faultlogger.query(faultlogger.FaultType.CPP_CRASH, queryFaultLogCallback);
262            return;
263        } catch (err) {
264            console.info(err);
265        }
266        console.info("FaultlogJsTest_006 error");
267        expect(false).assertTrue();
268        done();
269    })
270})