• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2021 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 hidebug from '@ohos.hidebug'
16import fs from '@ohos.file.fs'
17import process from '@ohos.process'
18import featureAbility from '@ohos.ability.featureAbility'
19
20import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
21
22describe("HidebugJsTest", function () {
23    beforeAll(function() {
24        /*
25         * @tc.setup: setup invoked before all testcases
26         */
27         console.info('HidebugJsTest beforeAll called')
28    })
29
30    afterAll(function() {
31        /*
32         * @tc.teardown: teardown invoked after all testcases
33         */
34         console.info('HidebugJsTest afterAll called')
35    })
36
37    beforeEach(function() {
38        /*
39         * @tc.setup: setup invoked before each testcases
40         */
41         console.info('HidebugJsTest beforeEach called')
42    })
43
44    afterEach(function() {
45        /*
46         * @tc.teardown: teardown invoked after each testcases
47         */
48         console.info('HidebugJsTest afterEach called')
49    })
50
51    async function msleep(time) {
52        let promise = new Promise((resolve, reject) => {
53            setTimeout(() => resolve("done!"), time)
54        });
55        let result = await promise;
56    }
57
58    /**
59     * test
60     *
61     * @tc.name: HidebugJsTest_001
62     * @tc.desc: 检测cpuProfiler采集的cpuprofiler数据是否含有js napi callframe信息
63     * @tc.type: FUNC
64     * @tc.require: issueI5NXHX
65     */
66    it('HidebugJsTest_001', 0, function () {
67        console.info("---------------------------HidebugJsTest_001----------------------------------");
68        try {
69            let timestamp = Date.now();
70            let filename = "cpuprofiler_" + timestamp.toString();
71            hidebug.startProfiling(filename);
72            for (var i = 0; i < 3; i++) {
73                hidebug.getSharedDirty();
74            }
75            hidebug.stopProfiling();
76            var pid = process.pid;
77            let path = "/proc/" + pid + "/root/data/storage/el2/base/files/" + filename + ".json";
78            let data = fs.readTextSync(path);
79            if (data.includes("napi")) {
80                expect(true).assertTrue();
81            } else {
82                expect(false).assertTrue();
83            }
84        } catch (err) {
85            console.error('HidebugJsTest_001 has failed for ' + err);
86            expect(false).assertTrue();
87        }
88    })
89
90    /**
91     * @tc.name: HidebugJsTest_002
92     * @tc.desc: startJsCpuProfiling/stopJsCpuProfiling的正常测试, startProfiling/stopProfiling的更新版本
93     * @tc.type: FUNC
94     * @tc.require: issueI5VY8L
95     */
96     it('HidebugJsTest_002', 0, function () {
97        console.info("---------------------------HidebugJsTest_002----------------------------------");
98        try {
99            let timestamp = Date.now();
100            let filename = "cpuprofiler_" + timestamp.toString();
101            hidebug.startJsCpuProfiling(filename);
102            for (var i = 0; i < 3; i++) {
103                hidebug.getSharedDirty();
104            }
105            hidebug.stopJsCpuProfiling();
106            var pid = process.pid;
107            let path = "/proc/" + pid + "/root/data/storage/el2/base/files/" + filename + ".json";
108            let data = fs.readTextSync(path);
109            if (data.includes("napi")) {
110                expect(true).assertTrue();
111            } else {
112                expect(false).assertTrue();
113            }
114        } catch (err) {
115            console.error('HidebugJsTest_001 has failed for ' + err);
116            expect(false).assertTrue();
117        }
118    })
119
120    /**
121     * @tc.name: HidebugJsTest_003
122     * @tc.desc: startJsCpuProfiling/stopJsCpuProfiling的异常测试, startProfiling/stopProfiling的更新版本
123     * @tc.type: FUNC
124     * @tc.require: issueI5VY8L
125     */
126     it('HidebugJsTest_003', 0, function () {
127        console.info("---------------------------HidebugJsTest_003----------------------------------");
128        try {
129            hidebug.startJsCpuProfiling();
130            for (var i = 0; i < 3; i++) {
131                hidebug.getSharedDirty();
132            }
133            hidebug.stopJsCpuProfiling();
134        } catch (error) {
135            console.info(error.code);
136            console.info(error.message);
137            expect(error.code == 401).assertTrue();
138        }
139    })
140
141    /**
142     * @tc.name: HidebugJsTest_004
143     * @tc.desc: dumpJsHeapData的正常测试, dumpHeapData的更新版本
144     * @tc.type: FUNC
145     * @tc.require: issueI5VY8L
146     */
147     it('HidebugJsTest_004', 0, function () {
148        console.info("---------------------------HidebugJsTest_004----------------------------------");
149        try {
150            hidebug.dumpJsHeapData("heapData");
151            expect(true).assertTrue();
152        } catch (error) {
153            console.info(error.code);
154            console.info(error.message);
155        }
156    })
157
158    /**
159     * @tc.name: HidebugJsTest_005
160     * @tc.desc: dumpJsHeapData的异常测试, dumpHeapData的更新版本
161     * @tc.type: FUNC
162     * @tc.require: issueI5VY8L
163     */
164     it('HidebugJsTest_005', 0, function () {
165        console.info("---------------------------HidebugJsTest_005----------------------------------");
166        try {
167            hidebug.dumpJsHeapData();
168        } catch (error) {
169            console.info(error.code);
170            console.info(error.message);
171            expect(error.code == 401).assertTrue();
172        }
173    })
174
175    /**
176     * @tc.name: HidebugJsTest_006
177     * @tc.desc: getServiceDump的正常测试
178     * @tc.type: FUNC
179     * @tc.require: issueI5VY8L
180     */
181     it('HidebugJsTest_006', 0, function () {
182        console.info("---------------------------HidebugJsTest_006----------------------------------");
183        let context = featureAbility.getContext();
184        context.getFilesDir().then((data) => {
185            var path = data + "/serviceInfo1.txt";
186            console.info("output path: " + path);
187            let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
188            var serviceId = 10;
189            var args = new Array("allInfo");
190            try {
191              hidebug.getServiceDump(serviceId, file.fd, args);
192              expect(true).assertTrue();
193            } catch (error) {
194              console.info(error.code);
195              console.info(error.message);
196            }
197            fs.closeSync(file);
198        })
199    })
200
201    /**
202     * @tc.name: HidebugJsTest_007
203     * @tc.desc: getServiceDump的异常测试,参数错误
204     * @tc.type: FUNC
205     * @tc.require: issueI5VY8L
206     */
207     it('HidebugJsTest_007', 0, function () {
208        console.info("---------------------------HidebugJsTest_007----------------------------------");
209        let context = featureAbility.getContext();
210        context.getFilesDir().then((data) => {
211            var path = data + "/serviceInfo2.txt";
212            console.info("output path: " + path)
213            let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
214            var serviceId = 10;
215            var args = new Array("allInfo");
216            try {
217                hidebug.getServiceDump(serviceId);
218            } catch (error) {
219              console.info(error.code);
220              console.info(error.message);
221              expect(error.code == 401).assertTrue();
222            }
223            fs.closeSync(file);
224        })
225    })
226
227    /**
228     * @tc.name: HidebugJsTest_008
229     * @tc.desc: getServiceDump的异常测试,查询system ability失败
230     * @tc.type: FUNC
231     * @tc.require: issueI5VY8L
232     */
233     it('HidebugJsTest_008', 0, function () {
234        console.info("---------------------------HidebugJsTest_008----------------------------------");
235        let context = featureAbility.getContext();
236        context.getFilesDir().then((data) => {
237            var path = data + "/serviceInfo3.txt";
238            console.info("output path: " + path);
239            let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
240            var serviceId = -10;
241            var args = new Array("allInfo");
242            try {
243                hidebug.getServiceDump(serviceId, file.fd, args);
244            } catch (error) {
245              console.info(error.code);
246              console.info(error.message);
247              expect(error.code == 11400101).assertTrue();
248            }
249            fs.closeSync(file);
250        })
251    })
252})