1// Copyright (C) 2022 The Android Open Source Project 2// 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 {Time} from '../../base/time'; 16import {UNEXPECTED_PINK} from '../colorizer'; 17import {Slice} from '../../public/track'; 18import {filterVisibleSlicesForTesting as filterVisibleSlices} from './base_slice_track'; 19 20function slice(start: number, duration: number, depth: number = 0): Slice { 21 const startNs = Time.fromRaw(BigInt(start)); 22 const durNs = Time.fromRaw(BigInt(duration)); 23 const endNs = Time.fromRaw(startNs + durNs); 24 return { 25 id: 42, 26 startNs, 27 endNs, 28 durNs, 29 ts: startNs, 30 dur: durNs, 31 depth, 32 flags: 0, 33 title: '', 34 subTitle: '', 35 colorScheme: UNEXPECTED_PINK, 36 fillRatio: 1, 37 isHighlighted: false, 38 }; 39} 40 41const s = slice; 42const t = Time.fromRaw; 43 44test('filterVisibleSlices', () => { 45 expect(filterVisibleSlices([], t(0n), t(100n))).toEqual([]); 46 expect(filterVisibleSlices([s(10, 80)], t(0n), t(100n))).toEqual([s(10, 80)]); 47 expect(filterVisibleSlices([s(0, 20)], t(10n), t(100n))).toEqual([s(0, 20)]); 48 expect(filterVisibleSlices([s(0, 10)], t(10n), t(100n))).toEqual([s(0, 10)]); 49 expect(filterVisibleSlices([s(100, 10)], t(10n), t(100n))).toEqual([ 50 s(100, 10), 51 ]); 52 expect(filterVisibleSlices([s(10, 0)], t(10n), t(100n))).toEqual([s(10, 0)]); 53 expect(filterVisibleSlices([s(100, 0)], t(10n), t(100n))).toEqual([ 54 s(100, 0), 55 ]); 56 expect(filterVisibleSlices([s(0, 5)], t(10n), t(90n))).toEqual([]); 57 expect(filterVisibleSlices([s(95, 5)], t(10n), t(90n))).toEqual([]); 58 expect(filterVisibleSlices([s(0, 5), s(95, 5)], t(10n), t(90n))).toEqual([]); 59 expect( 60 filterVisibleSlices([s(0, 5), s(50, 0), s(95, 5)], t(10n), t(90n)), 61 ).toEqual([s(50, 0)]); 62 expect( 63 filterVisibleSlices([s(0, 5), s(1, 9), s(6, 3)], t(10n), t(90n)), 64 ).toContainEqual(s(1, 9)); 65 expect( 66 filterVisibleSlices([s(0, 5), s(1, 9), s(6, 3), s(50, 0)], t(10n), t(90n)), 67 ).toContainEqual(s(1, 9)); 68 expect(filterVisibleSlices([s(85, 10), s(100, 10)], t(10n), t(90n))).toEqual([ 69 s(85, 10), 70 ]); 71 expect(filterVisibleSlices([s(0, 100)], t(10n), t(90n))).toEqual([s(0, 100)]); 72 expect( 73 filterVisibleSlices( 74 [ 75 s(0, 1), 76 s(1, 1), 77 s(2, 1), 78 s(3, 1), 79 s(4, 1), 80 s(5, 10), 81 s(6, 1), 82 s(7, 1), 83 s(8, 1), 84 s(9, 1), 85 ], 86 t(10n), 87 t(90n), 88 ), 89 ).toContainEqual(s(5, 10)); 90}); 91 92test('filterVisibleSlicesOrderByDepthAndTs', () => { 93 expect( 94 filterVisibleSlices( 95 [ 96 s(5, 2, 0), 97 s(5, 4, 0), 98 s(5, 6, 0), 99 s(7, 5, 0), 100 s(8, 10, 0), 101 s(4, 1, 1), 102 s(6, 3, 1), 103 s(8, 6, 1), 104 s(6, 1, 2), 105 s(10, 9, 2), 106 s(11, 3, 2), 107 ], 108 t(10n), 109 t(90n), 110 ), 111 ).toEqual([ 112 s(5, 6, 0), 113 s(7, 5, 0), 114 s(8, 10, 0), 115 s(8, 6, 1), 116 s(10, 9, 2), 117 s(11, 3, 2), 118 ]); 119}); 120 121test('filterVisibleSlicesOrderByTs', () => { 122 expect( 123 filterVisibleSlices( 124 [s(4, 5), s(4, 3), s(5, 10), s(6, 3), s(7, 2), s(10, 10)], 125 t(10n), 126 t(90n), 127 ), 128 ).toEqual([s(5, 10), s(10, 10)]); 129}); 130