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 {assertDefined} from 'common/assert_utils';
18import {HierarchyTreeBuilder} from 'test/unit/hierarchy_tree_builder';
19import {VisibilityComputation} from './visibility_computation';
20
21describe('VisibilityComputation', () => {
22  let computation: VisibilityComputation;
23
24  beforeEach(() => {
25    computation = new VisibilityComputation();
26  });
27
28  it('adds isComputedVisible true if root node is visible', () => {
29    const hierarchyRoot = new HierarchyTreeBuilder()
30      .setId('ViewNode')
31      .setName('test.package.name@123456789')
32      .setProperties({visibility: 0})
33      .build();
34
35    computation.setRoot(hierarchyRoot).executeInPlace();
36    expect(
37      assertDefined(
38        hierarchyRoot.getEagerPropertyByName('isComputedVisible'),
39      ).getValue(),
40    ).toBeTrue();
41  });
42
43  it('adds isComputedVisible true if node is visible and parent is visible', () => {
44    const hierarchyRoot = new HierarchyTreeBuilder()
45      .setId('ViewNode')
46      .setName('test.package.name@123456789')
47      .setProperties({visibility: 0})
48      .setChildren([
49        {
50          id: 'ViewNode',
51          name: 'test.package.name@987654321',
52          properties: {visibility: 0},
53        },
54      ])
55      .build();
56
57    computation.setRoot(hierarchyRoot).executeInPlace();
58    hierarchyRoot.forEachNodeDfs((node) =>
59      expect(
60        assertDefined(
61          node.getEagerPropertyByName('isComputedVisible'),
62        ).getValue(),
63      ).toBeTrue(),
64    );
65  });
66
67  it('adds isComputedVisible false if root is not visible', () => {
68    const hierarchyRoot = new HierarchyTreeBuilder()
69      .setId('ViewNode')
70      .setName('test.package.name@123456789')
71      .setProperties({visibility: 4})
72      .build();
73
74    computation.setRoot(hierarchyRoot).executeInPlace();
75    expect(
76      assertDefined(
77        hierarchyRoot.getEagerPropertyByName('isComputedVisible'),
78      ).getValue(),
79    ).toBeFalse();
80  });
81
82  it('adds isComputedVisible false if node is visible but parent is not visible', () => {
83    const hierarchyRoot = new HierarchyTreeBuilder()
84      .setId('ViewNode')
85      .setName('test.package.name@123456789')
86      .setProperties({visibility: 4})
87      .setChildren([
88        {
89          id: 'ViewNode',
90          name: 'test.package.name@987654321',
91          properties: {visibility: 0},
92        },
93      ])
94      .build();
95
96    computation.setRoot(hierarchyRoot).executeInPlace();
97    hierarchyRoot.forEachNodeDfs((node) =>
98      expect(
99        assertDefined(
100          node.getEagerPropertyByName('isComputedVisible'),
101        ).getValue(),
102      ).toBeFalse(),
103    );
104  });
105});
106