LayerTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. LayersTestHelper.LayersAreEqual(layer, clone);
  48. }
  49. [Fact]
  50. public void TestThatCloneIsMakingDeepCopyOfBitmap()
  51. {
  52. Layer layer = new Layer("test", 5, 2);
  53. Layer clone = layer.Clone();
  54. clone.LayerBitmap.SetPixel(0, 0, Colors.Green); // Actually we are checking if modifying clone bitmap does not affect original
  55. Assert.NotEqual(Colors.Green, layer.GetPixel(0, 0));
  56. }
  57. [Fact]
  58. public void TestThatResizeResizesBitmap()
  59. {
  60. Layer layer = new Layer("layer", 1, 1);
  61. layer.SetPixel(new Coordinates(0, 0), Colors.Black);
  62. layer.Resize(2, 2, 2, 2);
  63. Assert.Equal(2, layer.Width);
  64. Assert.Equal(2, layer.Height);
  65. Assert.Equal(2, layer.MaxWidth);
  66. Assert.Equal(2, layer.MaxHeight);
  67. // 4 is new area of bitmap
  68. for (int y = 0; y < layer.Height; y++)
  69. {
  70. for (int x = 0; x < layer.Width; x++)
  71. {
  72. Assert.Equal(Colors.Black, layer.GetPixel(x, y));
  73. }
  74. }
  75. }
  76. [Fact]
  77. public void TestThatGetPixelReturnsTransparentIfOutOfBounds()
  78. {
  79. Layer layer = new Layer("layer");
  80. Assert.Equal(0, layer.GetPixel(-1, 999).A);
  81. }
  82. [Fact]
  83. public void TestThatSetPixelsSetsPixels() // This also tests if Dynamic Resize works
  84. {
  85. Coordinates[] pixels = { new Coordinates(4, 2), new Coordinates(0, 0), new Coordinates(15, 2) };
  86. Layer layer = new Layer("layer");
  87. layer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(pixels, Colors.Green));
  88. for (int i = 0; i < pixels.Length; i++)
  89. {
  90. Assert.Equal(Colors.Green, layer.GetPixelWithOffset(pixels[i].X, pixels[i].Y));
  91. }
  92. }
  93. [Fact]
  94. public void TestThatClipCanvasResizesBitmapCorrectly()
  95. {
  96. Layer layer = new Layer("layer", 10, 10);
  97. layer.SetPixel(new Coordinates(4, 4), Colors.Blue);
  98. layer.ClipCanvas();
  99. Assert.Equal(1, layer.Width);
  100. Assert.Equal(1, layer.Height);
  101. Assert.Equal(Colors.Blue, layer.GetPixel(0, 0));
  102. }
  103. }
  104. }