-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageServiceTests.cs
More file actions
60 lines (45 loc) · 1.88 KB
/
Copy pathImageServiceTests.cs
File metadata and controls
60 lines (45 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Kattbot.Services.Images;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
namespace Kattbot.Tests;
[TestClass]
public class ImageServiceTests
{
private readonly TestContext _testContext;
public ImageServiceTests(TestContext testContext)
{
_testContext = testContext;
}
[TestMethod]
[DataRow("cute_cat.jpg")]
[DataRow("froge.png")]
public async Task EnsureMaxSize_DownscalesImageIfNeeded(string inputFilename)
{
const double maxSizeMb = 1;
string inputFile = Path.Combine("Resources", inputFilename);
Image inputImage = await Image.LoadAsync(inputFile, _testContext.CancellationToken);
Image resizedImage = await ImageService.EnsureMaxImageFileSize(inputImage, maxSizeMb);
double resizedImageSize = await ImageService.GetImageSizeInMb(resizedImage);
Assert.IsLessThanOrEqualTo(maxSizeMb, resizedImageSize);
}
[TestMethod]
[DataRow("slowpoke.jpg", "jpg")]
[DataRow("slowpoke.png", "png")]
[DataRow("slowpoke.webp", "webp")]
[DataRow("slowpoke.gif", "png")]
[DataRow("slowpoke.bmp", "png")]
public async Task EnsureSupportedImageFormatOrPng_ConvertsToPngIfFormatIsNotSupported(
string inputFilename,
string expectedFileType)
{
var supportedFileTypes = new[] { "jpg", "png", "webp" };
string inputFile = Path.Combine("Resources", inputFilename);
Image inputImage = await Image.LoadAsync(inputFile, _testContext.CancellationTokenSource.Token);
Image convertedImage = await ImageService.EnsureSupportedImageFormatOrPng(inputImage, supportedFileTypes);
string convertedImageFileExt = convertedImage.Metadata.GetFormatOrDefault().FileExtensions.First();
Assert.Contains(convertedImageFileExt, supportedFileTypes);
}
}