• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2
3/*
4 * Copyright (C) 2022 Huawei Device Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18//@ts-ignore
19import { HeapLoader } from '../../../dist/js-heap/logic/HeapLoader.js';
20//@ts-ignore
21import { ConstructorItem, FileType } from '../../../dist/js-heap/model/UiStruct.js';
22
23jest.mock('../../../dist/js-heap/model/DatabaseStruct.js', () => ({
24    DetachedNessState: {
25        UNKNOWN: 0,
26        ATTACHED: 1,
27        DETACHED: 2,
28    },
29    NodeType: {
30        HIDDEN : 0,
31        ARRAY : 1,
32        STRING : 2,
33        OBJECT : 3,
34        CODE : 4,
35        CLOSURE : 5,
36        REGEXP : 6,
37        NUMBER : 7,
38        NATIVE : 8,
39        SYNTHETIC : 9,
40        CONCATENATED_STRING : 10,
41        SLICED_STRING : 11,
42        SYMBOL : 12,
43        BIGINT : 13,
44        OBJECT_SHAPE : 14,
45    },
46    EdgeType: {
47        CONTEXT : 0,
48        ELEMENT : 1,
49        PROPERTY : 2,
50        INTERNAL : 3,
51        HIDDEN : 4,
52        SHORTCUT : 5,
53        WEAK : 6,
54        STRING_OR_NUMBER : 6,
55        NODE : 7,
56        INVISIBLE : 8,
57    }
58}))
59
60jest.mock('../../../dist/js-heap/utils/Utils.js', () => {
61    return {
62        HeapNodeToConstructorItem: (node: any) => {
63            return {};
64        },
65    };
66});
67
68describe('HeapLoader Test', () => {
69    let rootNode = {
70        detachedness: 7,
71        displayName: '',
72        distance: 13200000,
73        edgeCount: 215,
74        fileId: 1,
75        firstEdgeIndex: 0,
76        flag: 1,
77        id: 24,
78        name: 'Test',
79        nodeIndex: 0,
80        nodeOldIndex: 0,
81        retainedSize: 728167,
82        retainsCount: 0,
83        retainsEdgeIdx: [2],
84        retainsNodeIdx: [0,2],
85        selfSize: 0,
86        traceNodeId: 0,
87        type: 9,
88        edges: [
89            {
90                edgeIndex: 43,
91                edgeOldIndex: 3,
92                fromNodeId: 1,
93                nameOrIndex: '-test-',
94                nodeId: 15436,
95                retainEdge: [],
96                retainsNode: [],
97                toNodeId: 43427,
98                type: 5,
99            },
100            {
101                edgeIndex: 15,
102                edgeOldIndex: 53,
103                fromNodeId: 15,
104                nameOrIndex: '-test-',
105                nodeId: 76,
106                retainEdge: [],
107                retainsNode: [],
108                toNodeId: 23,
109                type: 5,
110            },
111        ],
112    };
113    let data = {
114        end_ts: 2206,
115        id: 29,
116        isParseSuccess: true,
117        name: 'Test',
118        path: '',
119        pid: 423,
120        tart_ts: 83693464,
121        type: 0,
122        heapLoader: {
123            rootNode: rootNode,
124        },
125        snapshotStruct: {
126            traceNodes: [],
127            nodeMap: new Map(),
128            nodeCount: 1,
129            edges: [
130                {
131                    edgeIndex: 1,
132                    edgeOldIndex: 0,
133                    fromNodeId: 5,
134                    nameOrIndex: 152,
135                    nodeId: 1436,
136                    retainEdge: [],
137                    retainsNode: [],
138                    toNodeId: 4347,
139                    type: 15,
140                },
141                {
142                    edgeIndex: 31,
143                    edgeOldIndex: 33,
144                    fromNodeId: 1,
145                    nameOrIndex: 958,
146                    nodeId: 344,
147                    retainEdge: [],
148                    retainsNode: [],
149                    toNodeId: 44405,
150                    type: 5,
151                },
152            ],
153            samples: [],
154        },
155    };
156
157    let item = {
158        addedCount: 1,
159        addedIndx: [30, 326],
160        addedSize: 316,
161        childCount: 1296,
162        children: [],
163        classChildren: [],
164        deletedIdx: [9, 338],
165        deltaCount: 0,
166        deltaSize: -16,
167        distance: -1,
168        edgeCount: 0,
169        edgeName: '',
170        fileId: 0,
171        hasNext: true,
172        id: 30,
173        index: 0,
174        isAdd: false,
175        isHover: false,
176        isSelected: false,
177        nextId: [],
178        nodeName: 'SourceTextModule',
179        objectName: 'SourceTextModule@521',
180        removedCount: 28,
181        removedSize: 332,
182        retainedSize: 32,
183        retains: [],
184        shallowSize: 21,
185        showBox: false,
186        showCut: false,
187        status: true,
188        targetFileId: 211,
189        traceNodeId: 34,
190        type: 4,
191    };
192
193    it('HeapLoaderTest01', () => {
194        let heapLoader = new HeapLoader(data);
195        heapLoader.fileId = jest.fn(() => true);
196        expect(heapLoader).not.toBeUndefined();
197    });
198    it('HeapLoaderTest02', () => {
199        let heapLoader = new HeapLoader(data);
200        expect(heapLoader.loadAllocationParent({})).toBeUndefined();
201    });
202
203    it('HeapLoaderTest03', () => {
204        let heapLoader = new HeapLoader(data);
205        heapLoader.rootNode = rootNode;
206        heapLoader.nodes = [rootNode];
207        heapLoader.nodes[0].addEdge = jest.fn(() => true);
208        heapLoader.isEssentialEdge = jest.fn(() => false);
209        expect(heapLoader.preprocess()).toBeUndefined();
210    });
211
212    it('HeapLoaderTest04', () => {
213        let heapLoader = new HeapLoader(data);
214        heapLoader.nodes = [rootNode];
215        expect(heapLoader.getClassesForSummary(1,2)).toBeTruthy();
216    });
217
218    it('HeapLoaderTest05', () => {
219        let heapLoader = new HeapLoader(data);
220        heapLoader.nodes = [rootNode];
221        heapLoader.rootNode = rootNode;
222        expect(heapLoader.getRetains(item)).toBeDefined();
223    });
224    it('HeapLoaderTest06', () => {
225        let heapLoader = new HeapLoader(data);
226        expect(heapLoader.getAllocationFunctionList()).toStrictEqual([]);
227    });
228    it('HeapLoaderTest07', () => {
229        let heapLoader = new HeapLoader(data);
230        expect(heapLoader.getAllocationStack(1)).toStrictEqual([]);
231    });
232    it('HeapLoaderTest08', () => {
233        let heapLoader = new HeapLoader(data);
234        expect(heapLoader.getFunctionNodeIds(1)).toStrictEqual([]);
235    });
236    it('HeapLoaderTest09', () => {
237        let heapLoader = new HeapLoader(data);
238        expect(heapLoader.calDistances()).toBeUndefined();
239    });
240    it('HeapLoaderTest10', () => {
241        let heapLoader = new HeapLoader(data);
242        heapLoader.buildOrderIdxAndDominateTree = jest.fn(()=>{true});
243        expect(heapLoader.calRetainedSize()).toBeUndefined();
244    });
245    it('HeapLoaderTest11', () => {
246        let heapLoader = new HeapLoader(data);
247        heapLoader.buildDominatedNode = jest.fn(()=>{true});
248        expect(heapLoader.buildDominatedNode()).toStrictEqual();
249    });
250    it('HeapLoaderTest12', () => {
251        let heapLoader = new HeapLoader(data);
252        expect(heapLoader.buildSamples()).toBeUndefined();
253    });
254    it('HeapLoaderTest13', () => {
255        let heapLoader = new HeapLoader(data);
256        let samples = [
257            {length: 1},
258            {mid: 1}
259        ]
260        expect(heapLoader.binarySearchNodeInSamples(1, samples)).toBe(0);
261    });
262    it('HeapLoaderTest14', () => {
263        let heapLoader = new HeapLoader(data);
264        let node = {
265            index:1,
266            nodesToVisitLen:1
267        }
268        let edge = {
269            nodesToVisitLen:3,
270        }
271        expect(heapLoader.bfs(node,edge)).toBeUndefined();
272    });
273    it('HeapLoaderTest15', () => {
274        let heapLoader = new HeapLoader(data);
275        expect(heapLoader.markPageOwnedNodes()).toBeUndefined();
276    });
277    it('HeapLoaderTest16', () => {
278        let heapLoader = new HeapLoader(data);
279        let node = {
280            nodeIndex:1
281        }
282        expect(heapLoader.hasOnlyWeakRetainers(node)).toBe(true);
283    });
284    it('HeapLoaderTest17', () => {
285        let heapLoader = new HeapLoader(data);
286        let targetClass = {
287            fileId:1,
288            nodeName:'',
289            childCount:2,
290            classChildren:[],
291        }
292        let baseClass = {
293            childCount:1,
294            classChildren:[]
295        }
296        heapLoader.calClassDiff = jest.fn(()=>{true})
297        expect(heapLoader.calClassDiff(targetClass,baseClass)).toBeUndefined();
298    });
299    it('HeapLoaderTest18', () => {
300        let heapLoader = new HeapLoader(data);
301        let item = {
302            children:[{length: 1}],
303            index:1,
304            childCount:1,
305            edgeName:'',
306            hasNext:true,
307            traceNodeId:1,
308            type:0,
309            parent:{},
310            id:1
311        }
312        expect(heapLoader.getNextNode(item)).toStrictEqual([{"length": 1}]);
313    });
314    it('HeapLoaderTest19', () => {
315        let heapLoader = new HeapLoader(data);
316        let item = {
317            retains:[{length: 1}],
318            index:1,
319            childCount:1,
320            edgeName:'',
321            hasNext:true,
322            traceNodeId:1,
323            type:0,
324            parent:{},
325            id:1
326        }
327        expect(heapLoader.getRetains(item)).toStrictEqual([{"length": 1}]);
328    });
329    it('HeapLoaderTest20', () => {
330        let heapLoader = new HeapLoader(data);
331        let node = {
332            index:1,
333            nodesToVisitLen:1
334        }
335        let edge = {
336            nodesToVisitLen:3,
337        }
338        expect(heapLoader.filterForBpf(node,edge)).toBe(true);
339    });
340    it('HeapLoaderTest21', () => {
341        let heapLoader = new HeapLoader(data);
342        let datas = {
343            visited:[
344                {node:{nodeIndex:1}},
345            ],
346            attached:[],
347            detached:[],
348        }
349        let node = {
350            nodeIndex:1,
351            type:1,
352            detachedness:1,
353            id:1,
354            displayName: 'Detached ',
355            name:'',
356            flag:1
357        }
358        expect(heapLoader.processNode(datas,node,1)).toBeUndefined();
359    });
360});