LayerTests.cs 4.5 KB

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