xref: /aosp_15_r20/development/tools/winscope/src/viewers/common/scroll_component_tests.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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.
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';
23import {ViewerTransitionsComponent} from 'viewers/viewer_transitions/viewer_transitions_component';
24
25type ScrollComponent =
26  | ViewerProtologComponent
27  | ViewerTransactionsComponent
28  | ViewerTransitionsComponent;
29
30export function executeScrollComponentTests(
31  setUpTestEnvironment: () => Promise<
32    [ComponentFixture<ScrollComponent>, HTMLElement, CdkVirtualScrollViewport]
33  >,
34) {
35  describe('common tests', () => {
36    let fixture: ComponentFixture<ScrollComponent>;
37    let htmlElement: HTMLElement;
38    let viewport: CdkVirtualScrollViewport;
39
40    beforeEach(async () => {
41      [fixture, htmlElement, viewport] = await setUpTestEnvironment();
42    });
43
44    it('renders initial state', () => {
45      const items = htmlElement.querySelectorAll('.entry');
46      expect(items.length).toBe(20);
47    });
48
49    it('gets data length', () => {
50      expect(viewport.getDataLength()).toBe(200);
51    });
52
53    it('should get the rendered range', () => {
54      expect(viewport.getRenderedRange()).toEqual({start: 0, end: 20});
55    });
56
57    it('should scroll to index in large jumps', () => {
58      expect(htmlElement.querySelector(`.entry[item-id="30"]`)).toBeFalsy();
59      checkScrollToIndex(30);
60      expect(htmlElement.querySelector(`.entry[item-id="70"]`)).toBeFalsy();
61      checkScrollToIndex(70);
62    });
63
64    it('should update without jumps as the user scrolls down or up', () => {
65      for (let i = 1; i < 50; i++) {
66        checkScrollToIndex(i);
67      }
68      for (let i = 49; i >= 0; i--) {
69        checkScrollToIndex(i);
70      }
71    });
72
73    function checkScrollToIndex(i: number) {
74      viewport.scrollToIndex(i);
75      viewport.elementRef.nativeElement.dispatchEvent(new Event('scroll'));
76      animationFrameScheduler.flush();
77      fixture.detectChanges();
78      assertDefined(htmlElement.querySelector(`.entry[item-id="${i}"]`));
79    }
80  });
81}
82