LayerTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Windows.Media;
  2. using System.Windows.Media.Imaging;
  3. using PixiEditor.Models.DataHolders;
  4. using PixiEditor.Models.Layers;
  5. using PixiEditor.Models.Position;
  6. using Xunit;
  7. namespace PixiEditorTests.ModelsTests.LayersTests
  8. {
  9. public class LayerTests
  10. {
  11. [Fact]
  12. public void TestThatEmptyLayerGeneratesCorrectly()
  13. {
  14. Layer layer = new Layer("layer");
  15. Assert.Equal("layer", layer.Name);
  16. Assert.Equal(0, layer.Width);
  17. Assert.Equal(0, layer.Height);
  18. Assert.Equal(1, layer.LayerBitmap.PixelWidth);
  19. Assert.Equal(1, layer.LayerBitmap.PixelHeight);
  20. }
  21. [Fact]
  22. public void TestThatEmptyLayerWithSizeGeneratesCorrectly()
  23. {
  24. Layer layer = new Layer("layer", 10, 10);
  25. Assert.Equal("layer", layer.Name);
  26. Assert.Equal(10, layer.Width);
  27. Assert.Equal(10, layer.Height);
  28. Assert.Equal(10, layer.LayerBitmap.PixelWidth);
  29. Assert.Equal(10, layer.LayerBitmap.PixelHeight);
  30. }
  31. [Fact]
  32. public void TestThatLayerFromBitmapGeneratesCorrectly()
  33. {
  34. WriteableBitmap bmp = BitmapFactory.New(10, 10);
  35. Layer layer = new Layer("layer", bmp);
  36. Assert.Equal("layer", layer.Name);
  37. Assert.Equal(10, layer.Width);
  38. Assert.Equal(10, layer.Height);
  39. Assert.Equal(10, layer.LayerBitmap.PixelWidth);
  40. Assert.Equal(10, layer.LayerBitmap.PixelHeight);
  41. }
  42. [Fact]
  43. public void TestThatCloneClonesCorrectly()
  44. {
  45. Layer layer = new Layer("test", 5, 2);
  46. Layer clone = layer.Clone();
  47. Assert.Equal(layer.Name, clone.Name);
  48. Assert.Equal(layer.Offset, clone.Offset);
  49. Assert.Equal(layer.Width, clone.Width);
  50. Assert.Equal(layer.Height, clone.Height);
  51. Assert.Equal(layer.MaxHeight, clone.MaxHeight);
  52. Assert.Equal(layer.MaxWidth, clone.MaxWidth);
  53. Assert.Equal(layer.Opacity, clone.Opacity);
  54. Assert.Equal(layer.IsVisible, clone.IsVisible);
  55. Assert.Equal(layer.IsRenaming, clone.IsRenaming);
  56. Assert.Equal(layer.ConvertBitmapToBytes(), clone.ConvertBitmapToBytes());
  57. }
  58. [Fact]
  59. public void TestThatCloneIsMakingDeepCopyOfBitmap()
  60. {
  61. Layer layer = new Layer("test", 5, 2);
  62. Layer clone = layer.Clone();
  63. clone.LayerBitmap.SetPixel(0, 0, Colors.Green); // Actually we are checking if modifying clone bitmap does not affect original
  64. Assert.NotEqual(Colors.Green, layer.GetPixel(0, 0));
  65. }
  66. [Fact]
  67. public void TestThatResizeResizesBitmap()
  68. {
  69. Layer layer = new Layer("layer", 1, 1);
  70. layer.SetPixel(new Coordinates(0, 0), Colors.Black);
  71. layer.Resize(2, 2, 2, 2);
  72. Assert.Equal(2, layer.Width);
  73. Assert.Equal(2, layer.Height);
  74. Assert.Equal(2, layer.MaxWidth);
  75. Assert.Equal(2, layer.MaxHeight);
  76. // 4 is new area of bitmap
  77. for (int y = 0; y < layer.Height; y++)
  78. {
  79. for (int x = 0; x < layer.Width; x++)
  80. {
  81. Assert.Equal(Colors.Black, layer.GetPixel(x, y));
  82. }
  83. }
  84. }
  85. [Fact]
  86. public void TestThatGetPixelReturnsTransparentIfOutOfBounds()
  87. {
  88. Layer layer = new Layer("layer");
  89. Assert.Equal(0, layer.GetPixel(-1, 999).A);
  90. }
  91. [Fact]
  92. public void TestThatSetPixelsSetsPixels() // This also tests if Dynamic Resize works
  93. {
  94. Coordinates[] pixels = { new Coordinates(4, 2), new Coordinates(0, 0), new Coordinates(15, 2) };
  95. Layer layer = new Layer("layer");
  96. layer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(pixels, Colors.Green));
  97. for (int i = 0; i < pixels.Length; i++)
  98. {
  99. Assert.Equal(Colors.Green, layer.GetPixelWithOffset(pixels[i].X, pixels[i].Y));
  100. }
  101. }
  102. [Fact]
  103. public void TestThatClipCanvasResizesBitmapCorrectly()
  104. {
  105. Layer layer = new Layer("layer", 10, 10);
  106. layer.SetPixel(new Coordinates(4, 4), Colors.Blue);
  107. layer.ClipCanvas();
  108. Assert.Equal(1, layer.Width);
  109. Assert.Equal(1, layer.Height);
  110. Assert.Equal(Colors.Blue, layer.GetPixel(0, 0));
  111. }
  112. }
  113. }