mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
f59658d848
* feat(virtual-scroll): fixed size virtual scroll (#9316) * feat(virtual-scroll): fixed size virtual scroll * address some comments * change VirtualScrollStrategy interface a bit * address some more comments * fix lint & build * chore: move virtual-scroll to cdk-experimental (#9974) * virtual-scroll: simplify scroll listener logic (#10102) * virtual-scroll: only move views that need to be moved (#10099) * virtual-scroll: only move views that need to be moved * address comments * virtual-scroll: switch `throttleTime` to `sampleTime` (#10179) * virtual-scroll: switch throttleTime to sampleTime * add comment * virtual-scroll: allow user to pass `Observable<T[]>` (#10158) * virtual-scroll: rename `Range` to `ListRange` to avoid confusion with native `Range` (#10220) * virtual-scroll: add autosize scroll strategy (#10219) * rename fixed size virtual scroll directive * add autosize virtual scroll strategy * add item size estimator class * add logic for jumping rendered content based on scroll position * address comments * virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy` (#10290) * virtual-scroll: add `onContentRendered` hook to `VirtualScrollStrategy` * address comemnts * virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy` (#10504) * virtual-scroll: add incremental scroll logic in `AutosizeVirtualScrollStrategy`. This still has a couple issues that need to be ironed out and it doesn't have the code for correcting the error between the predicted and actual scroll position. (See various TODOs for additional things that need work). * fix lint * address comments * address comments * fix bazel * fix devapp * fix lint * cleanup * virtual-scroll: rewrite offset in terms of "to-top" and fix a bug where items were removed too soon (#10986) * rewrite offsets to the end of the rendered content as offsets to the start * add some more autosize demos for testing * make sure not to remove too many items * address comments * virtual-scroll: address amcdnl's feedback (#10988) * rewrite offsets to the end of the rendered content as offsets to the start * add some more autosize demos for testing * make sure not to remove too many items * virtual-scroll: address amcdnl's feedback * virtual-scroll: fix updating when data changes and add trackBy demos (#11085) * virtual-scroll: add logic to correct the scroll position as user move… (#11137) * virtual-scroll: add logic to correct the scroll position as user moves toward the top * address comments * fix(scrolling): adds right to fix pushed content (#11192) * fix(scrolling): adds right to fix pushed content * chore: address feedback * chore: address pr feedback * virtual-scroll: add tests for fixed size items (#11255) * virtual-scroll: add tests for fixed size items * address comments * fix bug in fixed size strategy * fix bazel build * virtual-scroll: add tests for `cdkVirtualFor` logic (#11275) * merge fixed size test components into one * add tests for cdkVirtualFor logic * allow undefined to be explicitly passed as trackBy * fix bazel build * address comments * add some basic tests (#11295) * fix lint * virtual-scroll: add e2e tests for autosize scroll strategy (#11345) * set up virtual scroll page in e2e app * add gulp task for e2e:watch * add e2e tests for autosize * address comments * address comments * fix lint and tests * fix bad rebase * fix(scrolling): scrollbar jump when data change on scroll (#11193) * fix(scrolling): data change not updating size * chore: remove todo * chore: more performant fix for jump
118 lines
5.3 KiB
TypeScript
118 lines
5.3 KiB
TypeScript
import {browser, by, element, ElementFinder} from 'protractor';
|
|
import {ILocation, ISize} from 'selenium-webdriver';
|
|
|
|
declare var window: any;
|
|
|
|
|
|
describe('autosize cdk-virtual-scroll', () => {
|
|
let viewport: ElementFinder;
|
|
|
|
describe('with uniform items', () => {
|
|
beforeEach(() => {
|
|
browser.get('/virtual-scroll');
|
|
viewport = element(by.css('.demo-virtual-scroll-uniform-size cdk-virtual-scroll-viewport'));
|
|
});
|
|
|
|
it('should scroll down slowly', async () => {
|
|
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000);
|
|
const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]'));
|
|
const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]'));
|
|
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
|
|
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
|
|
});
|
|
|
|
it('should jump scroll position down and slowly scroll back up', async () => {
|
|
// The estimate of the total content size is exactly correct, so we wind up scrolled to the
|
|
// same place as if we slowly scrolled down.
|
|
await browser.executeAsyncScript(scrollViewportTo, viewport, 2000);
|
|
const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]'));
|
|
const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]'));
|
|
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
|
|
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
|
|
|
|
// As we slowly scroll back up we should wind up back at the start of the content.
|
|
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0);
|
|
const first = element(by.css('.demo-virtual-scroll-uniform-size [data-index="0"]'));
|
|
expect(await isVisibleInViewport(first, viewport)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('with variable size', () => {
|
|
beforeEach(() => {
|
|
browser.get('/virtual-scroll');
|
|
viewport = element(by.css('.demo-virtual-scroll-variable-size cdk-virtual-scroll-viewport'));
|
|
});
|
|
|
|
it('should scroll down slowly', async () => {
|
|
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000);
|
|
const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]'));
|
|
const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="20"]'));
|
|
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
|
|
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
|
|
});
|
|
|
|
it('should jump scroll position down and slowly scroll back up', async () => {
|
|
// The estimate of the total content size is slightly different than the actual, so we don't
|
|
// wind up in the same spot as if we scrolled slowly down.
|
|
await browser.executeAsyncScript(scrollViewportTo, viewport, 2000);
|
|
const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="18"]'));
|
|
const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]'));
|
|
expect(await isVisibleInViewport(offScreen, viewport)).toBe(false);
|
|
expect(await isVisibleInViewport(onScreen, viewport)).toBe(true);
|
|
|
|
// As we slowly scroll back up we should wind up back at the start of the content. As we
|
|
// scroll the error from when we jumped the scroll position should be slowly corrected.
|
|
await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0);
|
|
const first = element(by.css('.demo-virtual-scroll-variable-size [data-index="0"]'));
|
|
expect(await isVisibleInViewport(first, viewport)).toBe(true);
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
/** Checks if the given element is visible in the given viewport. */
|
|
async function isVisibleInViewport(el: ElementFinder, viewport: ElementFinder): Promise<boolean> {
|
|
if (!await el.isPresent() || !await el.isDisplayed() || !await viewport.isPresent() ||
|
|
!await viewport.isDisplayed()) {
|
|
return false;
|
|
}
|
|
const viewportRect = getRect(await viewport.getLocation(), await viewport.getSize());
|
|
const elRect = getRect(await el.getLocation(), await el.getSize());
|
|
return elRect.left < viewportRect.right && elRect.right > viewportRect.left &&
|
|
elRect.top < viewportRect.bottom && elRect.bottom > viewportRect.top;
|
|
}
|
|
|
|
|
|
/** Gets the rect for an element given its location ans size. */
|
|
function getRect(location: ILocation, size: ISize):
|
|
{top: number, left: number, bottom: number, right: number} {
|
|
return {
|
|
top: location.y,
|
|
left: location.x,
|
|
bottom: location.y + size.height,
|
|
right: location.x + size.width
|
|
};
|
|
}
|
|
|
|
|
|
/** Immediately scrolls the viewport to the given offset. */
|
|
function scrollViewportTo(viewportEl: any, offset: number, done: () => void) {
|
|
viewportEl.scrollTop = offset;
|
|
window.requestAnimationFrame(() => done());
|
|
}
|
|
|
|
|
|
/** Smoothly scrolls the viewport to the given offset, 25px at a time. */
|
|
function smoothScrollViewportTo(viewportEl: any, offset: number, done: () => void) {
|
|
let promise = Promise.resolve();
|
|
let curOffset = viewportEl.scrollTop;
|
|
do {
|
|
const co = curOffset += Math.min(25, Math.max(-25, offset - curOffset));
|
|
promise = promise.then(() => new Promise<void>(resolve => {
|
|
viewportEl.scrollTop = co;
|
|
window.requestAnimationFrame(() => resolve());
|
|
}));
|
|
} while (curOffset != offset);
|
|
promise.then(() => done());
|
|
}
|