1/* 2 * Copyright (C) 2023 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.d 15 */ 16 17import {CdkVirtualScrollViewport} from '@angular/cdk/scrolling'; 18import {ComponentFixture} from '@angular/core/testing'; 19import {assertDefined} from 'common/assert_utils'; 20import {animationFrameScheduler} from 'rxjs'; 21import {ViewerProtologComponent} from 'viewers/viewer_protolog/viewer_protolog_component'; 22import {ViewerTransactionsComponent} from 'viewers/viewer_transactions/viewer_transactions_component'; 23 24type ScrollComponent = ViewerProtologComponent | ViewerTransactionsComponent; 25 26export function executeScrollComponentTests( 27 itemClassName: string, 28 setUpTestEnvironment: () => Promise< 29 [ComponentFixture<ScrollComponent>, HTMLElement, CdkVirtualScrollViewport] 30 >, 31) { 32 describe('', () => { 33 let fixture: ComponentFixture<any>; 34 let htmlElement: HTMLElement; 35 let viewport: CdkVirtualScrollViewport; 36 37 beforeEach(async () => { 38 [fixture, htmlElement, viewport] = await setUpTestEnvironment(); 39 }); 40 41 it('renders initial state', () => { 42 const items = htmlElement.querySelectorAll(`.${itemClassName}`)!; 43 expect(items.length).toBe(20); 44 }); 45 46 it('gets data length', () => { 47 expect(viewport.getDataLength()).toBe(200); 48 }); 49 50 it('should get the rendered range', () => { 51 expect(viewport.getRenderedRange()).toEqual({start: 0, end: 20}); 52 }); 53 54 it('should scroll to index in large jumps', () => { 55 expect( 56 htmlElement.querySelector(`.${itemClassName}[item-id="30"]`), 57 ).toBeFalsy(); 58 checkScrollToIndex(30); 59 expect( 60 htmlElement.querySelector(`.${itemClassName}[item-id="70"]`), 61 ).toBeFalsy(); 62 checkScrollToIndex(70); 63 }); 64 65 it('should update without jumps as the user scrolls down or up', () => { 66 for (let i = 1; i < 50; i++) { 67 checkScrollToIndex(i); 68 } 69 for (let i = 49; i >= 0; i--) { 70 checkScrollToIndex(i); 71 } 72 }); 73 74 function checkScrollToIndex(i: number) { 75 viewport.scrollToIndex(i); 76 viewport.elementRef.nativeElement.dispatchEvent(new Event('scroll')); 77 animationFrameScheduler.flush(); 78 fixture.detectChanges(); 79 assertDefined( 80 htmlElement.querySelector(`.${itemClassName}[item-id="${i}"]`), 81 ); 82 } 83 }); 84} 85