/* Copyright (c) Microsoft Corporation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as React from 'react'; import { useMeasure } from '../uiUtils'; import { ResizeView } from './resizeView'; type TestAttachment = { name: string; body?: string; path?: string; contentType: string; }; export type ImageDiff = { name: string, expected?: { attachment: TestAttachment, title: string }, actual?: { attachment: TestAttachment }, diff?: { attachment: TestAttachment }, }; async function loadImage(src?: string): Promise { const image = new Image(); if (src) { image.src = src; await new Promise((f, r) => { image.onload = f; image.onerror = f; }); } return image; } const checkerboardStyle: React.CSSProperties = { backgroundImage: `linear-gradient(45deg, #80808020 25%, transparent 25%), linear-gradient(-45deg, #80808020 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #80808020 75%), linear-gradient(-45deg, transparent 75%, #80808020 75%)`, backgroundSize: '20px 20px', backgroundPosition: '0 0, 0 10px, 10px -10px, -10px 0px', boxShadow: `rgb(0 0 0 / 10%) 0px 1.8px 1.9px, rgb(0 0 0 / 15%) 0px 6.1px 6.3px, rgb(0 0 0 / 10%) 0px -2px 4px, rgb(0 0 0 / 15%) 0px -6.1px 12px, rgb(0 0 0 / 25%) 0px 6px 12px` }; export const ImageDiffView: React.FC<{ diff: ImageDiff, noTargetBlank?: boolean, }> = ({ diff, noTargetBlank }) => { const [mode, setMode] = React.useState<'diff' | 'actual' | 'expected' | 'slider' | 'sxs'>(diff.diff ? 'diff' : 'actual'); const [showSxsDiff, setShowSxsDiff] = React.useState(false); const [expectedImage, setExpectedImage] = React.useState(null); const [actualImage, setActualImage] = React.useState(null); const [diffImage, setDiffImage] = React.useState(null); const [measure, ref] = useMeasure(); React.useEffect(() => { (async () => { setExpectedImage(await loadImage(diff.expected?.attachment.path)); setActualImage(await loadImage(diff.actual?.attachment.path)); setDiffImage(await loadImage(diff.diff?.attachment.path)); })(); }, [diff]); const isLoaded = expectedImage && actualImage && diffImage; const imageWidth = isLoaded ? Math.max(expectedImage.naturalWidth, actualImage.naturalWidth, 200) : 500; const imageHeight = isLoaded ? Math.max(expectedImage.naturalHeight, actualImage.naturalHeight, 200) : 500; const scale = Math.min(1, (measure.width - 30) / imageWidth); const sxsScale = Math.min(1, (measure.width - 50) / imageWidth / 2); const fitWidth = imageWidth * scale; const fitHeight = imageHeight * scale; const modeStyle: React.CSSProperties = { flex: 'none', margin: '0 10px', cursor: 'pointer', userSelect: 'none', }; return
{isLoaded && <>
{diff.diff &&
setMode('diff')}>Diff
}
setMode('actual')}>Actual
setMode('expected')}>Expected
setMode('sxs')}>Side by side
setMode('slider')}>Slider
{diff.diff && mode === 'diff' && } {diff.diff && mode === 'actual' && } {diff.diff && mode === 'expected' && } {diff.diff && mode === 'slider' && } {diff.diff && mode === 'sxs' &&
setShowSxsDiff(!showSxsDiff)} canvasWidth={sxsScale * imageWidth} canvasHeight={sxsScale * imageHeight} scale={sxsScale} />
} {!diff.diff && mode === 'actual' && } {!diff.diff && mode === 'expected' && } {!diff.diff && mode === 'sxs' &&
}
}
; }; export const ImageDiffSlider: React.FC<{ expectedImage: HTMLImageElement, actualImage: HTMLImageElement, canvasWidth: number, canvasHeight: number, scale: number, }> = ({ expectedImage, actualImage, canvasWidth, canvasHeight, scale }) => { const absoluteStyle: React.CSSProperties = { position: 'absolute', top: 0, left: 0, }; const [slider, setSlider] = React.useState(canvasWidth / 2); const sameSize = expectedImage.naturalWidth === actualImage.naturalWidth && expectedImage.naturalHeight === actualImage.naturalHeight; return
{!sameSize && Expected } {expectedImage.naturalWidth} x {expectedImage.naturalHeight} {!sameSize && Actual } {!sameSize && {actualImage.naturalWidth}} {!sameSize && x} {!sameSize && {actualImage.naturalHeight}}
setSlider(offsets[0])} resizerColor={'#57606a80'} resizerWidth={6}> Expected
Actual
; }; const ImageWithSize: React.FunctionComponent<{ image: HTMLImageElement, title?: string, alt?: string, canvasWidth: number, canvasHeight: number, scale: number, onClick?: () => void; }> = ({ image, title, alt, canvasWidth, canvasHeight, scale, onClick }) => { return
{title && {title}} {image.naturalWidth} x {image.naturalHeight}
{title
; };