1/* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {TreeNodeUtils} from 'test/unit/tree_node_utils'; 18import {TreeNode} from 'trace/tree_node/tree_node'; 19import {UiHierarchyTreeNode} from 'viewers/common/ui_hierarchy_tree_node'; 20import {AddDiffsHierarchyTree} from './add_diffs_hierarchy_tree'; 21import {executeAddDiffsTests} from './add_diffs_test_utils'; 22import {DiffType} from './diff_type'; 23 24describe('AddDiffsHierarchyTree', () => { 25 let newRoot: UiHierarchyTreeNode; 26 let oldRoot: UiHierarchyTreeNode; 27 let expectedRoot: UiHierarchyTreeNode; 28 29 const isModified = async ( 30 newTree: TreeNode | undefined, 31 oldTree: TreeNode | undefined, 32 denylistProperties: string[], 33 ) => { 34 return ( 35 (newTree as UiHierarchyTreeNode) 36 .getEagerPropertyByName('exampleProperty') 37 ?.getValue() !== 38 (oldTree as UiHierarchyTreeNode) 39 .getEagerPropertyByName('exampleProperty') 40 ?.getValue() 41 ); 42 }; 43 const addDiffs = new AddDiffsHierarchyTree(isModified, []); 44 45 describe('AddDiffs tests', () => { 46 executeAddDiffsTests( 47 TreeNodeUtils.treeNodeEqualityTester, 48 makeRoot, 49 makeChildAndAddToRoot, 50 addDiffs, 51 ); 52 }); 53 54 describe('Hierarchy tree tests', () => { 55 beforeEach(() => { 56 jasmine.addCustomEqualityTester(TreeNodeUtils.treeNodeEqualityTester); 57 newRoot = makeRoot(); 58 oldRoot = makeRoot(); 59 expectedRoot = makeRoot(); 60 }); 61 62 it('does not add MODIFIED to hierarchy root', async () => { 63 oldRoot = makeRoot('oldValue'); 64 await addDiffs.executeInPlace(newRoot, oldRoot); 65 expect(newRoot).toEqual(expectedRoot); 66 }); 67 68 it('adds ADDED_MOVE and DELETED_MOVE', async () => { 69 const newParent = makeParentAndAddToRoot(newRoot); 70 makeChildAndAddToRoot(newParent); 71 makeParentAndAddToRoot(oldRoot); 72 makeChildAndAddToRoot(oldRoot); 73 74 const expectedParent = makeParentAndAddToRoot(expectedRoot); 75 76 const expectedNewChild = makeChildAndAddToRoot(expectedParent); 77 expectedNewChild.setDiff(DiffType.ADDED_MOVE); 78 79 const expectedOldChild = makeChildAndAddToRoot(expectedRoot); 80 expectedOldChild.setDiff(DiffType.DELETED_MOVE); 81 82 await addDiffs.executeInPlace(newRoot, oldRoot); 83 expect(newRoot).toEqual(expectedRoot); 84 }); 85 86 it('adds ADDED, ADDED_MOVE and DELETED_MOVE', async () => { 87 const newParent = makeParentAndAddToRoot(newRoot); 88 makeChildAndAddToRoot(newParent); 89 makeChildAndAddToRoot(oldRoot); 90 91 const expectedOldChild = makeChildAndAddToRoot(expectedRoot); 92 expectedOldChild.setDiff(DiffType.DELETED_MOVE); 93 94 const expectedParent = makeParentAndAddToRoot(expectedRoot); 95 expectedParent.setDiff(DiffType.ADDED); 96 97 const expectedNewChild = makeChildAndAddToRoot(expectedParent); 98 expectedNewChild.setDiff(DiffType.ADDED_MOVE); 99 100 await addDiffs.executeInPlace(newRoot, oldRoot); 101 expect(newRoot).toEqual(expectedRoot); 102 }); 103 }); 104 105 function makeRoot(value = 'value'): UiHierarchyTreeNode { 106 return TreeNodeUtils.makeUiHierarchyNode({ 107 id: 'test', 108 name: 'root', 109 exampleProperty: value, 110 }); 111 } 112 113 function makeChildAndAddToRoot( 114 rootNode: UiHierarchyTreeNode, 115 value = 'value', 116 ): UiHierarchyTreeNode { 117 const child = TreeNodeUtils.makeUiHierarchyNode({ 118 id: 'test node', 119 name: 'child', 120 exampleProperty: value, 121 }); 122 rootNode.addOrReplaceChild(child); 123 child.setParent(rootNode); 124 return child; 125 } 126 127 function makeParentAndAddToRoot( 128 rootNode: UiHierarchyTreeNode, 129 ): UiHierarchyTreeNode { 130 const parent = TreeNodeUtils.makeUiHierarchyNode({ 131 id: 'test node', 132 name: 'parent', 133 exampleProperty: 'value', 134 }); 135 rootNode.addOrReplaceChild(parent); 136 parent.setParent(rootNode); 137 return parent; 138 } 139}); 140