ImporterTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using PixiEditor.Exceptions;
  2. using PixiEditor.Models.DataHolders;
  3. using PixiEditor.Models.IO;
  4. using PixiEditorTests.ModelsTests.ColorsTests;
  5. using SkiaSharp;
  6. using System;
  7. using System.IO;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using Xunit;
  11. namespace PixiEditorTests.ModelsTests.IO
  12. {
  13. public class ImporterTests
  14. {
  15. private readonly string testImagePath;
  16. private readonly string testCorruptedPixiImagePath;
  17. // I am not testing ImportDocument, because it's just a wrapper for BinarySerialization which is tested.
  18. public ImporterTests()
  19. {
  20. testImagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\TestImage.png";
  21. testCorruptedPixiImagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\CorruptedPixiFile.pixi";
  22. }
  23. [Theory]
  24. [InlineData("wubba.png")]
  25. [InlineData("lubba.pixi")]
  26. [InlineData("dub.jpeg")]
  27. [InlineData("-.JPEG")]
  28. [InlineData("dub.jpg")]
  29. public void TestThatIsSupportedFile(string file)
  30. {
  31. Assert.True(Importer.IsSupportedFile(file));
  32. }
  33. [Fact]
  34. public void TestThatImportImageImportsImage()
  35. {
  36. SKColor color = new SKColor(255, 255, 0, 0);
  37. Surface image = Importer.ImportSurface(testImagePath);
  38. Assert.NotNull(image);
  39. Assert.Equal(5, image.Width);
  40. Assert.Equal(5, image.Height);
  41. Assert.Equal(color, image.GetSRGBPixel(0, 0)); // Top left
  42. Assert.Equal(color, image.GetSRGBPixel(4, 4)); // Bottom right
  43. Assert.Equal(color, image.GetSRGBPixel(0, 4)); // Bottom left
  44. Assert.Equal(color, image.GetSRGBPixel(4, 0)); // Top right
  45. Assert.Equal(color, image.GetSRGBPixel(2, 2)); // Middle center
  46. }
  47. [Fact]
  48. public void TestThatImporterThrowsCorruptedFileExceptionOnWrongPixiFileWithSupportedExtension()
  49. {
  50. Assert.Throws<CorruptedFileException>(() => { Importer.ImportDocument(testCorruptedPixiImagePath); });
  51. }
  52. [Theory]
  53. [InlineData("CorruptedPNG.png")]
  54. [InlineData("CorruptedPNG2.png")]
  55. [InlineData("CorruptedJpg.jpg")]
  56. public void TestThatImporterThrowsCorruptedFileExceptionOnWrongImageFileWithSupportedExtension(string fileName)
  57. {
  58. string imagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\{fileName}";
  59. Assert.Throws<CorruptedFileException>(() => { Importer.ImportSurface(imagePath); });
  60. }
  61. [Fact]
  62. public void TestThatImportImageResizes()
  63. {
  64. Surface image = Importer.ImportImage(testImagePath, 10, 10);
  65. Assert.Equal(10, image.Width);
  66. Assert.Equal(10, image.Height);
  67. }
  68. [Fact]
  69. public void TestSaveAndLoadGZippedBytes()
  70. {
  71. using Surface original = new Surface(123, 456);
  72. original.SkiaSurface.Canvas.Clear(ExtendedColorTests.red);
  73. using SKPaint paint = new SKPaint();
  74. paint.BlendMode = SKBlendMode.Src;
  75. paint.Color = new SKColor(128, 64, 32, 16);
  76. original.SkiaSurface.Canvas.DrawRect(10, 10, 20, 20, paint);
  77. Exporter.SaveAsGZippedBytes("pleasedontoverwritethings", original);
  78. using var loaded = Importer.LoadFromGZippedBytes("pleasedontoverwritethings");
  79. File.Delete("pleasedontoverwritethings");
  80. Assert.Equal(original.Width, loaded.Width);
  81. Assert.Equal(original.Height, loaded.Height);
  82. Assert.Equal(original.GetSRGBPixel(0, 0), loaded.GetSRGBPixel(0, 0));
  83. Assert.Equal(original.GetSRGBPixel(15, 15), loaded.GetSRGBPixel(15, 15));
  84. }
  85. }
  86. }