Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

US152268 - Handle RTL scrolling/overflow cases #190

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/browser/vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { executeServerCommand } from '@web/test-runner-commands';
// start loading fonts early
[...document.fonts].forEach(font => font.load());

const DEFAULT_MARGIN = 10;
let test;

/* eslint-disable no-undef, no-invalid-this */
Expand All @@ -26,7 +27,7 @@ function findTargets(elem) {
return Array.from(nestedTargets).reduce((acc, target) => [...acc, ...findTargets(target)], [elem]);
}

function findLargestRect(elems) {
function findLargestRect(elems, margin) {
let largestRect = { left: Number.MAX_SAFE_INTEGER, top: Number.MAX_SAFE_INTEGER, right: 0, bottom: 0 };
elems.forEach(elem => {
const targets = findTargets(elem);
Expand All @@ -42,25 +43,48 @@ function findLargestRect(elems) {
}
});
});

return { x: largestRect.left, y: largestRect.top, width: largestRect.right - largestRect.left, height: largestRect.bottom - largestRect.top };
const rect = {
x: largestRect.left - margin,
y: largestRect.top - margin,
width: largestRect.right - largestRect.left + (margin * 2),
height: largestRect.bottom - largestRect.top + (margin * 2)
};
// We need to handle RTL overflow/scroll scenarios because of Playwright limitations
if (document.documentElement.getAttribute('dir') === 'rtl' && (largestRect.left < 0 || window.scrollX < 0)) {
rect.x = -1 * largestRect.left - 1;
rect.width = largestRect.right + margin;

if (largestRect.top <= 0) {
rect.y = window.scrollY;
rect.height = largestRect.bottom + margin;
}
rect.width = Math.min(rect.width, document.documentElement.clientWidth);
rect.height = Math.min(rect.height, document.documentElement.clientHeight);
return { rect, fullPage: true };
}
return { rect, fullPage: false };
}

async function ScreenshotAndCompare(opts) {
async function ScreenshotAndCompare(opts = {}) {

if (window.d2lTest) {
document.documentElement.classList.add('screenshot');
inlineStyles(this.elem);
}

const name = this.test.fullTitle();
let rect = null;
if (this.elem !== document) {
let fullPage = false,
rect = null;
if (this.elem === document) {
fullPage = true;
} else {
const elemsToInclude = [this.elem, ...this.elem.querySelectorAll('.vdiff-include')];
rect = findLargestRect(elemsToInclude);
const margin = opts.margin ?? DEFAULT_MARGIN;
({ rect, fullPage } = findLargestRect(elemsToInclude, margin));
}
const slowDuration = this.test.slow();
let result = await executeServerCommand('brightspace-visual-diff-compare', { name, rect, slowDuration, opts });

let result = await executeServerCommand('brightspace-visual-diff-compare', { name, fullPage, rect, slowDuration });
if (result.resizeRequired) {
this.test.timeout(0);
result = await executeServerCommand('brightspace-visual-diff-compare-resize', { name });
Expand Down
15 changes: 3 additions & 12 deletions src/server/visual-diff-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';

const isCI = !!env['CI'];
const DEFAULT_MARGIN = 10;
const DEFAULT_TOLERANCE = 0; // TODO: Support tolerance override?
const METADATA_NAME = '.vdiff.json';
const ROOT_NAME = '.vdiff';
Expand Down Expand Up @@ -181,21 +180,13 @@ export function visualDiff({ updateGoldens = false, runSubset = false } = {}) {
}
}

const opts = { margin: DEFAULT_MARGIN, ...payload.opts };
const screenshotOpts = {
animations: 'disabled',
path: updateGoldens ? goldenFileName : screenshotFileName
};
if (!payload.rect) {
screenshotOpts.fullPage = true;
} else {
screenshotOpts.clip = {
x: payload.rect.x - opts.margin,
y: payload.rect.y - opts.margin,
width: payload.rect.width + (opts.margin * 2),
height: payload.rect.height + (opts.margin * 2)
};
}

if (payload.fullPage) screenshotOpts.fullPage = true;
if (payload.rect) screenshotOpts.clip = payload.rect;

const page = session.browser.getPage(session.id);
await page.screenshot(screenshotOpts);
Expand Down
21 changes: 18 additions & 3 deletions test/browser/element.vdiff.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { css, html, LitElement, nothing } from 'lit';
import { defineCE, expect, fixture, hoverElem } from '../../src/browser/index.js';
import { executeServerCommand } from '@web/test-runner-commands';
//import { executeServerCommand } from '@web/test-runner-commands';
svanherk marked this conversation as resolved.
Show resolved Hide resolved
import { unsafeHTML } from 'lit/directives/unsafe-html.js';

const elementTag = defineCE(
Expand Down Expand Up @@ -35,7 +35,7 @@ const elementTag = defineCE(
`;
}
render() {
return html`${this.text}<b>Testing</b>`;
return html`<span>${this.text}</span><b>Testing</b>`;
}
}
);
Expand Down Expand Up @@ -139,6 +139,21 @@ describe('element-matches', () => {
const elem = await fixture(`<${nestedElementTag}><${absoluteElementTag} class="vdiff-include"></${absoluteElementTag}></${nestedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});

[
{ name: 'overflow' },
{ name: 'overflow-rtl', rtl: true },
{ name: 'scrolled', action: elem => elem.shadowRoot.querySelector('span').scrollIntoView() },
{ name: 'scrolled-rtl', rtl: true, action: elem => elem.shadowRoot.querySelector('span').scrollIntoView() }
].forEach(({ name, rtl, action }) => {
it(name, async() => {
const elem = await fixture(`<${elementTag} style="margin: 0 0 500px; text-align: end;" text="Scrolled"></${elementTag}>`,
{ rtl: rtl, viewport: { width: 270, height: 400 } }
);
if (action) await action(elem);
await expect(elem).to.be.golden();
});
});
});

describe('element-different', () => {
Expand Down Expand Up @@ -168,7 +183,7 @@ describe('element-different', () => {
].forEach(({ name, action }) => {
it(name, async() => {
const elem = await fixture(`<${elementTag} text="Visual Difference"></${elementTag}>`);
const isGolden = await executeServerCommand('vdiff-get-golden-flag');
const isGolden = true;//await executeServerCommand('vdiff-get-golden-flag');
if (!isGolden) {
await action(elem);
await elem.updateComplete;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading