ImgMetadataRemover/ImageCore.Tests/TestLosslessCompressor.cs

52 lines
1.8 KiB
C#
Raw Normal View History

2022-01-25 20:19:56 +00:00
using System;
using System.IO;
using Image.Core;
2022-01-25 20:19:56 +00:00
using Xunit;
namespace ImageCore.Tests
{
public class TestLosslessCompressor
{
private readonly string _testsProjectDirectory;
public TestLosslessCompressor()
{
_testsProjectDirectory = Environment.GetEnvironmentVariable("IMAGE_CORE_TESTS");
}
[Fact]
public void TestLosslessCompressor_Compress()
{
ICompressor compressor = new LosslessCompressor();
2022-04-02 14:22:42 +00:00
var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures/IMG_0138.HEIC");
2022-01-25 20:19:56 +00:00
var destinationFileName = Path.GetTempFileName();
File.Copy(sourceFileName, destinationFileName, true);
compressor.Compress(destinationFileName);
var originalFile = File.Open(sourceFileName, FileMode.Open);
var compressedFile = File.Open(destinationFileName, FileMode.Open);
2022-04-02 14:22:42 +00:00
2022-01-25 20:19:56 +00:00
Assert.True(compressedFile.Length <= originalFile.Length);
2022-04-02 14:22:42 +00:00
2022-01-25 20:19:56 +00:00
originalFile.Close();
compressedFile.Close();
File.Delete(destinationFileName);
}
2022-04-02 14:22:42 +00:00
2022-01-25 20:19:56 +00:00
[Fact]
public void TestLosslessCompressor_Compress_Stream()
2022-01-25 20:19:56 +00:00
{
ICompressor compressor = new LosslessCompressor();
2022-04-02 14:22:42 +00:00
var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures/IMG_0138.HEIC");
2022-01-25 20:19:56 +00:00
var destinationFileName = Path.GetTempFileName();
File.Copy(sourceFileName, destinationFileName, true);
2022-04-02 14:22:42 +00:00
var destinationFileHandle = File.Open(destinationFileName, FileMode.Open);
var lengthBeforeCompression = destinationFileHandle.Length;
2022-04-02 14:22:42 +00:00
compressor.Compress(destinationFileHandle);
2022-04-02 14:22:42 +00:00
Assert.True(destinationFileHandle.Length < lengthBeforeCompression);
2022-01-25 20:19:56 +00:00
}
}
}