Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add warnings re: image distortion (#47082)
Checks whether image is visually distorted. Also adds a check to verify that width and height are set to a non-zero number. PR Close #47082
- Loading branch information
1 parent
c23f32c
commit 8d3701c
Showing
8 changed files
with
278 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ages/core/test/bundling/image-directive/e2e/image-distortion/image-distortion.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/* tslint:disable:no-console */ | ||
import {browser, by, element, ExpectedConditions} from 'protractor'; | ||
import {logging} from 'selenium-webdriver'; | ||
|
||
import {collectBrowserLogs} from '../util'; | ||
|
||
describe('NgOptimizedImage directive', () => { | ||
it('should not warn if there is no image distortion', async () => { | ||
await browser.get('/e2e/image-distortion-passing'); | ||
const logs = await collectBrowserLogs(logging.Level.WARNING); | ||
expect(logs.length).toEqual(0); | ||
}); | ||
it('should warn if there is image distortion', async () => { | ||
await browser.get('/e2e/image-distortion-failing'); | ||
const logs = await collectBrowserLogs(logging.Level.WARNING); | ||
|
||
expect(logs.length).toEqual(5); | ||
// Image loading order is not gaurenteed, so all logs, rather than single entry | ||
// needs to be checked in order to test whether a given error message is present. | ||
const expectErrorMessageInLogs = (logs: logging.Entry[], message: string) => { | ||
expect(logs.some((log) => { | ||
return log.message.includes(message); | ||
})).toBeTruthy(); | ||
}; | ||
|
||
// Images with incorrect width/height attributes | ||
expectErrorMessageInLogs( | ||
logs, | ||
'The NgOptimizedImage directive (activated on an \\u003Cimg> element ' + | ||
'with the \`rawSrc=\\"/e2e/b.png\\"`) has detected that ' + | ||
'the aspect ratio of the image does not match the aspect ratio indicated by the width and height attributes. ' + | ||
'Intrinsic image size: 250w x 250h (aspect-ratio: 1). ' + | ||
'Supplied width and height attributes: 26w x 30h (aspect-ratio: 0.8666666666666667). ' + | ||
'To fix this, update the width and height attributes.'); | ||
|
||
expectErrorMessageInLogs( | ||
logs, | ||
'The NgOptimizedImage directive (activated on an \\u003Cimg> element ' + | ||
'with the \`rawSrc=\\"/e2e/b.png\\"`) has detected that ' + | ||
'the aspect ratio of the image does not match the aspect ratio indicated by the width and height attributes. ' + | ||
'Intrinsic image size: 250w x 250h (aspect-ratio: 1). ' + | ||
'Supplied width and height attributes: 24w x 240h (aspect-ratio: 0.1). ' + | ||
'To fix this, update the width and height attributes.'); | ||
|
||
// Images with incorrect styling | ||
expectErrorMessageInLogs( | ||
logs, | ||
'The NgOptimizedImage directive (activated on an \\u003Cimg> element ' + | ||
'with the \`rawSrc=\\"/e2e/b.png\\"`) has detected that ' + | ||
'the aspect ratio of the rendered image does not match the image\'s intrinsic aspect ratio. ' + | ||
'Intrinsic image size: 250w x 250h (aspect-ratio: 1). ' + | ||
'Rendered image size: 250w x 30h (aspect-ratio: 8.333333333333334). ' + | ||
'This issue can occur if \\"width\\" and \\"height\\" attributes are added to an image ' + | ||
'without updating the corresponding image styling. In most cases, ' + | ||
'adding \\"height: auto\\" or \\"width: auto\\" to the image styling will fix this issue.'); | ||
|
||
expectErrorMessageInLogs( | ||
logs, | ||
'The NgOptimizedImage directive (activated on an \\u003Cimg> element ' + | ||
'with the \`rawSrc=\\"/e2e/b.png\\"`) has detected that ' + | ||
'the aspect ratio of the rendered image does not match the image\'s intrinsic aspect ratio. ' + | ||
'Intrinsic image size: 250w x 250h (aspect-ratio: 1). ' + | ||
'Rendered image size: 30w x 250h (aspect-ratio: 0.12). ' + | ||
'This issue can occur if \\"width\\" and \\"height\\" attributes are added to an image ' + | ||
'without updating the corresponding image styling. In most cases, ' + | ||
'adding \\"height: auto\\" or \\"width: auto\\" to the image styling will fix this issue.'); | ||
|
||
// Image with incorrect width/height attributes AND incorrect styling | ||
// This only generate only one error to ensure that users first fix the width and height | ||
// attributes. | ||
expectErrorMessageInLogs( | ||
logs, | ||
'The NgOptimizedImage directive (activated on an \\u003Cimg> element ' + | ||
'with the \`rawSrc=\\"/e2e/b.png\\"`) has detected that ' + | ||
'the aspect ratio of the image does not match the aspect ratio indicated by the width and height attributes. ' + | ||
'Intrinsic image size: 250w x 250h (aspect-ratio: 1). ' + | ||
'Supplied width and height attributes: 150w x 250h (aspect-ratio: 0.6). ' + | ||
'To fix this, update the width and height attributes.'); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/core/test/bundling/image-directive/e2e/image-distortion/image-distortion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ɵNgOptimizedImageModule as NgOptimizedImageModule} from '@angular/common'; | ||
import {Component} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'image-distortion-passing', | ||
standalone: true, | ||
imports: [NgOptimizedImageModule], | ||
template: ` | ||
<!-- All the images in this template should not throw --> | ||
<!-- This image is here for the sake of making sure the "LCP image is priority" assertion is passed --> | ||
<img rawSrc="/e2e/logo-500w.jpg" width="500" height="500" priority> | ||
<br> | ||
<!-- width and height attributes exacly match the intrinsic size of image --> | ||
<img rawSrc="/e2e/a.png" width="25" height="25"> | ||
<br> | ||
<!-- supplied aspect ratio exactly matches intrinsic aspect ratio--> | ||
<img rawSrc="/e2e/a.png" width="250" height="250"> | ||
<img rawSrc="/e2e/b.png" width="40" height="40"> | ||
<img rawSrc="/e2e/b.png" width="240" height="240"> | ||
<br> | ||
<!-- supplied aspect ratio is similar to intrinsic aspect ratio --> | ||
<!-- Aspect-ratio: 0.93333333333 --> | ||
<img rawSrc="/e2e/b.png" width="28" height="30"> | ||
<!-- Aspect-ratio: 0.9 --> | ||
<img rawSrc="/e2e/b.png" width="27" height="30"> | ||
<!-- Aspect-ratio: 1.09375 --> | ||
<img rawSrc="/e2e/b.png" width="350" height="320"> | ||
<!-- Aspect-ratio: 1.0652173913 --> | ||
<img rawSrc="/e2e/b.png" width="245" height="230"> | ||
<br> | ||
<!-- Supplied aspect ratio is correct & image has 0x0 rendered size --> | ||
<img rawSrc="/e2e/a.png" width="25" height="25" style="display: none"> | ||
<br> | ||
<!-- styling is correct --> | ||
<img rawSrc="/e2e/a.png" width="25" height="25" style="width: 100%; height: 100%"> | ||
<img rawSrc="/e2e/a.png" width="250" height="250" style="max-width: 100%; height: 100%"> | ||
<img rawSrc="/e2e/a.png" width="25" height="25" style="height: 25%; width: 25%;"> | ||
<br> | ||
`, | ||
}) | ||
export class ImageDistortionPassingComponent { | ||
} | ||
@Component({ | ||
selector: 'image-distortion-failing', | ||
standalone: true, | ||
imports: [NgOptimizedImageModule], | ||
template: ` | ||
<!-- With the exception of the priority image, all the images in this template should throw --> | ||
<!-- This image is here for the sake of making sure the "LCP image is priority" assertion is passed --> | ||
<img rawSrc="/e2e/logo-500w.jpg" width="500" height="500" priority> | ||
<br> | ||
<!-- These images should throw --> | ||
<!-- Supplied aspect ratio differs from intrinsic aspect ratio by > .1 --> | ||
<!-- Aspect-ratio: 0.86666666666 --> | ||
<img rawSrc="/e2e/b.png" width="26" height="30"> | ||
<!-- Aspect-ratio: 0.1 --> | ||
<img rawSrc="/e2e/b.png" width="24" height="240"> | ||
<!-- Supplied aspect ratio is incorrect & image has 0x0 rendered size --> | ||
<img rawSrc="/e2e/a.png" width="222" height="25" style="display: none"> | ||
<br> | ||
<!-- Image styling is causing distortion --> | ||
<div style="width: 300px; height: 300px"> | ||
<img rawSrc="/e2e/b.png" width="250" height="250" style="width: 10%"> | ||
<img rawSrc="/e2e/b.png" width="250" height="250" style="max-height: 10%"> | ||
<!-- Images dimensions are incorrect AND image styling is incorrect --> | ||
<img rawSrc="/e2e/b.png" width="150" height="250" style="max-height: 10%"> | ||
</div> | ||
`, | ||
}) | ||
export class ImageDistortionFailingComponent { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters