ImporterTests.cs 3.6 KB

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