|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using SixLabors.ImageSharp.Formats.Jpeg; |
| 6 | +using SixLabors.ImageSharp.Formats.Png; |
| 7 | +using SixLabors.ImageSharp.PixelFormats; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace SixLabors.ImageSharp.Web.Tests.Processors |
| 11 | +{ |
| 12 | + public class FormattedImageTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void ConstructorSetsProperties() |
| 16 | + { |
| 17 | + using var image = new Image<Rgba32>(1, 1); |
| 18 | + using var formatted = new FormattedImage(image, JpegFormat.Instance); |
| 19 | + |
| 20 | + Assert.NotNull(formatted.Image); |
| 21 | + Assert.Equal(image, formatted.Image); |
| 22 | + |
| 23 | + Assert.NotNull(formatted.Format); |
| 24 | + Assert.Equal(JpegFormat.Instance, formatted.Format); |
| 25 | + |
| 26 | + Assert.NotNull(formatted.Encoder); |
| 27 | + Assert.Equal(typeof(JpegEncoder), formatted.Encoder.GetType()); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void CanSetFormat() |
| 32 | + { |
| 33 | + using var image = new Image<Rgba32>(1, 1); |
| 34 | + using var formatted = new FormattedImage(image, JpegFormat.Instance); |
| 35 | + |
| 36 | + Assert.NotNull(formatted.Format); |
| 37 | + Assert.Equal(JpegFormat.Instance, formatted.Format); |
| 38 | + |
| 39 | + Assert.Throws<ArgumentNullException>(() => formatted.Format = null); |
| 40 | + |
| 41 | + formatted.Format = PngFormat.Instance; |
| 42 | + Assert.Equal(PngFormat.Instance, formatted.Format); |
| 43 | + Assert.Equal(typeof(PngEncoder), formatted.Encoder.GetType()); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void CanSetEncoder() |
| 48 | + { |
| 49 | + using var image = new Image<Rgba32>(1, 1); |
| 50 | + using var formatted = new FormattedImage(image, PngFormat.Instance); |
| 51 | + |
| 52 | + Assert.NotNull(formatted.Format); |
| 53 | + Assert.Equal(PngFormat.Instance, formatted.Format); |
| 54 | + |
| 55 | + Assert.Throws<ArgumentNullException>(() => formatted.Encoder = null); |
| 56 | + Assert.Throws<ArgumentException>(() => formatted.Encoder = new JpegEncoder()); |
| 57 | + |
| 58 | + formatted.Format = JpegFormat.Instance; |
| 59 | + Assert.Equal(typeof(JpegEncoder), formatted.Encoder.GetType()); |
| 60 | + |
| 61 | + JpegSubsample current = ((JpegEncoder)formatted.Encoder).Subsample.GetValueOrDefault(); |
| 62 | + |
| 63 | + Assert.Equal(JpegSubsample.Ratio444, current); |
| 64 | + formatted.Encoder = new JpegEncoder { Subsample = JpegSubsample.Ratio420 }; |
| 65 | + |
| 66 | + JpegSubsample replacement = ((JpegEncoder)formatted.Encoder).Subsample.GetValueOrDefault(); |
| 67 | + |
| 68 | + Assert.NotEqual(current, replacement); |
| 69 | + Assert.Equal(JpegSubsample.Ratio420, replacement); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments