ImporterTests.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Windows.Media;
  3. using System.Windows.Media.Imaging;
  4. using PixiEditor.Exceptions;
  5. using PixiEditor.Models.IO;
  6. using Xunit;
  7. namespace PixiEditorTests.ModelsTests.IO
  8. {
  9. public class ImporterTests
  10. {
  11. private readonly string testImagePath;
  12. private readonly string testCorruptedPixiImagePath;
  13. // I am not testing ImportDocument, because it's just a wrapper for BinarySerialization which is tested.
  14. public ImporterTests()
  15. {
  16. testImagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\TestImage.png";
  17. testCorruptedPixiImagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\CorruptedPixiFile.pixi";
  18. }
  19. [Theory]
  20. [InlineData("wubba.png")]
  21. [InlineData("lubba.pixi")]
  22. [InlineData("dub.jpeg")]
  23. [InlineData("-.JPEG")]
  24. [InlineData("dub.jpg")]
  25. public void TestThatIsSupportedFile(string file)
  26. {
  27. Assert.True(Importer.IsSupportedFile(file));
  28. }
  29. [Fact]
  30. public void TestThatImportImageImportsImage()
  31. {
  32. Color color = Color.FromArgb(255, 255, 0, 0);
  33. WriteableBitmap image = Importer.ImportImage(testImagePath);
  34. Assert.NotNull(image);
  35. Assert.Equal(5, image.PixelWidth);
  36. Assert.Equal(5, image.PixelHeight);
  37. Assert.Equal(color, image.GetPixel(0, 0)); // Top left
  38. Assert.Equal(color, image.GetPixel(4, 4)); // Bottom right
  39. Assert.Equal(color, image.GetPixel(0, 4)); // Bottom left
  40. Assert.Equal(color, image.GetPixel(4, 0)); // Top right
  41. Assert.Equal(color, image.GetPixel(2, 2)); // Middle center
  42. }
  43. [Fact]
  44. public void TestThatImporterThrowsCorruptedFileExceptionOnWrongPixiFileWithSupportedExtension()
  45. {
  46. Assert.Throws<CorruptedFileException>(() => { Importer.ImportDocument(testCorruptedPixiImagePath); });
  47. }
  48. [Theory]
  49. [InlineData("CorruptedPNG.png")]
  50. [InlineData("CorruptedPNG2.png")]
  51. [InlineData("CorruptedJpg.jpg")]
  52. public void TestThatImporterThrowsCorruptedFileExceptionOnWrongImageFileWithSupportedExtension(string fileName)
  53. {
  54. string imagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\{fileName}";
  55. Assert.Throws<CorruptedFileException>(() => { Importer.ImportImage(imagePath); });
  56. }
  57. [Fact]
  58. public void TestThatImportImageResizes()
  59. {
  60. WriteableBitmap image = Importer.ImportImage(testImagePath, 10, 10);
  61. Assert.Equal(10, image.PixelWidth);
  62. Assert.Equal(10, image.PixelHeight);
  63. }
  64. }
  65. }