|
| 1 | +import {browser, by, element, ElementFinder} from 'protractor'; |
| 2 | +import {ILocation, ISize} from 'selenium-webdriver'; |
| 3 | + |
| 4 | +declare var window: any; |
| 5 | + |
| 6 | + |
| 7 | +describe('autosize cdk-virtual-scroll', () => { |
| 8 | + let viewport: ElementFinder; |
| 9 | + |
| 10 | + describe('with uniform items', () => { |
| 11 | + beforeEach(() => { |
| 12 | + browser.get('/virtual-scroll'); |
| 13 | + viewport = element(by.css('.demo-virtual-scroll-uniform-size cdk-virtual-scroll-viewport')); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should scroll down slowly', async () => { |
| 17 | + await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000); |
| 18 | + const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]')); |
| 19 | + const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]')); |
| 20 | + expect(await isVisibleInViewport(offScreen, viewport)).toBe(false); |
| 21 | + expect(await isVisibleInViewport(onScreen, viewport)).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should jump scroll position down and slowly scroll back up', async () => { |
| 25 | + // The estimate of the total content size is exactly correct, so we wind up scrolled to the |
| 26 | + // same place as if we slowly scrolled down. |
| 27 | + await browser.executeAsyncScript(scrollViewportTo, viewport, 2000); |
| 28 | + const offScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="39"]')); |
| 29 | + const onScreen = element(by.css('.demo-virtual-scroll-uniform-size [data-index="40"]')); |
| 30 | + expect(await isVisibleInViewport(offScreen, viewport)).toBe(false); |
| 31 | + expect(await isVisibleInViewport(onScreen, viewport)).toBe(true); |
| 32 | + |
| 33 | + // As we slowly scroll back up we should wind up back at the start of the content. |
| 34 | + await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0); |
| 35 | + const first = element(by.css('.demo-virtual-scroll-uniform-size [data-index="0"]')); |
| 36 | + expect(await isVisibleInViewport(first, viewport)).toBe(true); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('with variable size', () => { |
| 41 | + beforeEach(() => { |
| 42 | + browser.get('/virtual-scroll'); |
| 43 | + viewport = element(by.css('.demo-virtual-scroll-variable-size cdk-virtual-scroll-viewport')); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should scroll down slowly', async () => { |
| 47 | + await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 2000); |
| 48 | + const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]')); |
| 49 | + const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="20"]')); |
| 50 | + expect(await isVisibleInViewport(offScreen, viewport)).toBe(false); |
| 51 | + expect(await isVisibleInViewport(onScreen, viewport)).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should jump scroll position down and slowly scroll back up', async () => { |
| 55 | + // The estimate of the total content size is slightly different than the actual, so we don't |
| 56 | + // wind up in the same spot as if we scrolled slowly down. |
| 57 | + await browser.executeAsyncScript(scrollViewportTo, viewport, 2000); |
| 58 | + const offScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="18"]')); |
| 59 | + const onScreen = element(by.css('.demo-virtual-scroll-variable-size [data-index="19"]')); |
| 60 | + expect(await isVisibleInViewport(offScreen, viewport)).toBe(false); |
| 61 | + expect(await isVisibleInViewport(onScreen, viewport)).toBe(true); |
| 62 | + |
| 63 | + // As we slowly scroll back up we should wind up back at the start of the content. As we |
| 64 | + // scroll the error from when we jumped the scroll position should be slowly corrected. |
| 65 | + await browser.executeAsyncScript(smoothScrollViewportTo, viewport, 0); |
| 66 | + const first = element(by.css('.demo-virtual-scroll-variable-size [data-index="0"]')); |
| 67 | + expect(await isVisibleInViewport(first, viewport)).toBe(true); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | + |
| 73 | +/** Checks if the given element is visible in the given viewport. */ |
| 74 | +async function isVisibleInViewport(el: ElementFinder, viewport: ElementFinder): Promise<boolean> { |
| 75 | + if (!await el.isPresent() || !await el.isDisplayed() || !await viewport.isPresent() || |
| 76 | + !await viewport.isDisplayed()) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + const viewportRect = getRect(await viewport.getLocation(), await viewport.getSize()); |
| 80 | + const elRect = getRect(await el.getLocation(), await el.getSize()); |
| 81 | + return elRect.left < viewportRect.right && elRect.right > viewportRect.left && |
| 82 | + elRect.top < viewportRect.bottom && elRect.bottom > viewportRect.top; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +/** Gets the rect for an element given its location ans size. */ |
| 87 | +function getRect(location: ILocation, size: ISize): |
| 88 | + {top: number, left: number, bottom: number, right: number} { |
| 89 | + return { |
| 90 | + top: location.y, |
| 91 | + left: location.x, |
| 92 | + bottom: location.y + size.height, |
| 93 | + right: location.x + size.width |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +/** Immediately scrolls the viewport to the given offset. */ |
| 99 | +function scrollViewportTo(viewportEl: any, offset: number, done: () => void) { |
| 100 | + viewportEl.scrollTop = offset; |
| 101 | + window.requestAnimationFrame(() => done()); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +/** Smoothly scrolls the viewport to the given offset, 25px at a time. */ |
| 106 | +function smoothScrollViewportTo(viewportEl: any, offset: number, done: () => void) { |
| 107 | + let promise = Promise.resolve(); |
| 108 | + let curOffset = viewportEl.scrollTop; |
| 109 | + do { |
| 110 | + const co = curOffset += Math.min(25, Math.max(-25, offset - curOffset)); |
| 111 | + promise = promise.then(() => new Promise<void>(resolve => { |
| 112 | + viewportEl.scrollTop = co; |
| 113 | + window.requestAnimationFrame(() => resolve()); |
| 114 | + })); |
| 115 | + } while (curOffset != offset); |
| 116 | + promise.then(() => done()); |
| 117 | +} |
0 commit comments