TiledMapImporterProcessorTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.IO;
  2. using System.Linq;
  3. using Microsoft.Xna.Framework.Content.Pipeline;
  4. using MonoGame.Extended.Content.Pipeline.Tiled;
  5. using MonoGame.Extended.Content.Tiled;
  6. using NSubstitute;
  7. namespace MonoGame.Extended.Content.Pipeline.Tests.Tiled
  8. {
  9. // TODO: Testing for ew content builder project type
  10. #if !MONOGAME_385_OR_NEWER
  11. public class TiledMapImporterProcessorTests
  12. {
  13. [Fact]
  14. public void TiledMapImporter_Import_Test()
  15. {
  16. var filePath = PathExtensions.GetApplicationFullPath("TestData", "level01.tmx");
  17. var logger = Substitute.For<ContentBuildLogger>();
  18. var importer = new TiledMapImporter();
  19. var importerContext = Substitute.For<ContentImporterContext>();
  20. importerContext.Logger.Returns(logger);
  21. var contentItem = importer.Import(filePath, importerContext);
  22. var map = contentItem.Data;
  23. Assert.Equal("1.0", map.Version);
  24. Assert.Equal(TiledMapOrientationContent.Orthogonal, map.Orientation);
  25. Assert.Equal(TiledMapTileDrawOrderContent.RightDown, map.RenderOrder);
  26. Assert.Equal(20, map.Width);
  27. Assert.Equal(10, map.Height);
  28. Assert.Equal(128, map.TileWidth);
  29. Assert.Equal(128, map.TileHeight);
  30. Assert.Equal("#7d7d7d", map.BackgroundColor);
  31. Assert.Equal("awesome", map.Properties[0].Name);
  32. Assert.Equal("42", map.Properties[0].Value);
  33. Assert.Single(map.Tilesets);
  34. Assert.Equal(3, map.Layers.Count);
  35. Assert.Equal(TiledMapOrientationContent.Orthogonal, map.Orientation);
  36. var tileset = map.Tilesets.First();
  37. Assert.Equal(1, tileset.FirstGlobalIdentifier);
  38. Assert.Equal("free-tileset.png", Path.GetFileName(tileset.Image.Source));
  39. Assert.Equal(652, tileset.Image.Width);
  40. Assert.Equal(783, tileset.Image.Height);
  41. Assert.Equal(2, tileset.Margin);
  42. Assert.Equal(30, tileset.TileCount);
  43. Assert.Equal("free-tileset", tileset.Name);
  44. Assert.Null(tileset.Source);
  45. Assert.Equal(2, tileset.Spacing);
  46. //Assert.Equal(0, tileset.TerrainTypes.Count);
  47. Assert.Empty(tileset.Properties);
  48. Assert.Equal(128, tileset.TileHeight);
  49. Assert.Equal(128, tileset.TileWidth);
  50. Assert.Equal(0, tileset.TileOffset.X);
  51. Assert.Equal(0, tileset.TileOffset.Y);
  52. var tileLayer2 = (TiledMapTileLayerContent)map.Layers[0];
  53. Assert.Equal("Tile Layer 2", tileLayer2.Name);
  54. Assert.Equal(1, tileLayer2.Opacity);
  55. Assert.Empty(tileLayer2.Properties);
  56. Assert.True(tileLayer2.Visible);
  57. Assert.Equal(200, tileLayer2.Data.Tiles.Count);
  58. Assert.Equal(0, tileLayer2.X);
  59. Assert.Equal(0, tileLayer2.Y);
  60. var imageLayer = (TiledMapImageLayerContent)map.Layers[1];
  61. Assert.Equal("Image Layer 1", imageLayer.Name);
  62. Assert.Equal(1, imageLayer.Opacity);
  63. Assert.Empty(imageLayer.Properties);
  64. Assert.True(imageLayer.Visible);
  65. Assert.Equal("hills.png", Path.GetFileName(imageLayer.Image.Source));
  66. Assert.Equal(100, imageLayer.X);
  67. Assert.Equal(100, imageLayer.Y);
  68. var tileLayer1 = (TiledMapTileLayerContent)map.Layers[2];
  69. Assert.Equal("Tile Layer 1", tileLayer1.Name);
  70. Assert.Equal(2, tileLayer1.Properties.Count);
  71. Assert.Equal("customlayerprop", tileLayer1.Properties[0].Name);
  72. Assert.Equal("1", tileLayer1.Properties[0].Value);
  73. Assert.Equal("customlayerprop2", tileLayer1.Properties[1].Name);
  74. Assert.Equal("2", tileLayer1.Properties[1].Value);
  75. }
  76. [Fact]
  77. public void TiledMapImporter_Xml_Test()
  78. {
  79. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-xml.tmx");
  80. var map = ImportAndProcessMap(filePath);
  81. var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
  82. var actualData = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();
  83. Assert.Null(layer.Data.Encoding);
  84. Assert.Null(layer.Data.Compression);
  85. Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(actualData));
  86. }
  87. [Fact]
  88. public void TiledMapImporter_Csv_Test()
  89. {
  90. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-csv.tmx");
  91. var map = ImportAndProcessMap(filePath);
  92. var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
  93. var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();
  94. Assert.Equal("csv", layer.Data.Encoding);
  95. Assert.Null(layer.Data.Compression);
  96. //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
  97. }
  98. [Fact]
  99. public void TiledMapImporter_Base64_Test()
  100. {
  101. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-base64.tmx");
  102. var map = ImportAndProcessMap(filePath);
  103. var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
  104. var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();
  105. Assert.Equal("base64", layer.Data.Encoding);
  106. Assert.Null(layer.Data.Compression);
  107. //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
  108. }
  109. [Fact]
  110. public void TiledMapImporter_Gzip_Test()
  111. {
  112. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-gzip.tmx");
  113. var map = ImportAndProcessMap(filePath);
  114. var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
  115. var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();
  116. Assert.Equal("base64", layer.Data.Encoding);
  117. Assert.Equal("gzip", layer.Data.Compression);
  118. //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
  119. }
  120. [Fact]
  121. public void TiledMapImporter_Zlib_Test()
  122. {
  123. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-zlib.tmx");
  124. var map = ImportAndProcessMap(filePath);
  125. var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
  126. var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();
  127. Assert.Equal("base64", layer.Data.Encoding);
  128. Assert.Equal("zlib", layer.Data.Compression);
  129. //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
  130. }
  131. [Fact]
  132. public void TiledMapImporter_ObjectLayer_Test()
  133. {
  134. var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-object-layer.tmx");
  135. var map = ImportAndProcessMap(filePath);
  136. Assert.Single(map.Layers);
  137. Assert.IsType<TiledMapObjectLayerContent>(map.Layers[0]);
  138. var tmxObjectGroup = map.Layers[0] as TiledMapObjectLayerContent;
  139. var tmxObject = tmxObjectGroup.Objects[0];
  140. var tmxPolygon = tmxObjectGroup.Objects[3].Polygon;
  141. var tmxPolyline = tmxObjectGroup.Objects[4].Polyline;
  142. Assert.Equal("Object Layer 1", tmxObjectGroup.Name);
  143. Assert.Equal(1, tmxObject.Identifier);
  144. Assert.Equal(131.345f, tmxObject.X);
  145. Assert.Equal(65.234f, tmxObject.Y);
  146. Assert.Equal(311.111f, tmxObject.Width);
  147. Assert.Equal(311.232f, tmxObject.Height);
  148. Assert.Single(tmxObject.Properties);
  149. Assert.Equal("shape", tmxObject.Properties[0].Name);
  150. Assert.Equal("circle", tmxObject.Properties[0].Value);
  151. Assert.NotNull(tmxObject.Ellipse);
  152. Assert.False(tmxObjectGroup.Objects[1].Visible);
  153. Assert.Equal((uint)0, tmxObjectGroup.Objects[1].GlobalIdentifier);
  154. Assert.Equal((uint)23, tmxObjectGroup.Objects[5].GlobalIdentifier);
  155. Assert.Equal("rectangle", tmxObjectGroup.Objects[2].Type);
  156. Assert.Equal("sprite", tmxObjectGroup.Objects[1].Class);
  157. Assert.NotNull(tmxPolygon);
  158. Assert.Equal("0,0 180,90 -8,275 -45,81 38,77", tmxPolygon.Points);
  159. Assert.NotNull(tmxPolyline);
  160. Assert.Equal("0,0 28,299 326,413 461,308", tmxPolyline.Points);
  161. }
  162. private static TiledMapContent ImportAndProcessMap(string filename)
  163. {
  164. var logger = Substitute.For<ContentBuildLogger>();
  165. var importer = new TiledMapImporter();
  166. var importerContext = Substitute.For<ContentImporterContext>();
  167. importerContext.Logger.Returns(logger);
  168. var processor = new TiledMapProcessor();
  169. var processorContext = Substitute.For<ContentProcessorContext>();
  170. processorContext.Logger.Returns(logger);
  171. var import = importer.Import(filename, importerContext);
  172. var result = processor.Process(import, processorContext);
  173. return result.Data;
  174. }
  175. }
  176. #endif
  177. }