summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Content.Pipeline.Tests.Tiled/TiledMapImporterProcessorTests.cs
blob: 352457a15a4e2f54604f16b0d9e650098f3516af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework.Content.Pipeline;
using MonoGame.Extended.Content.Pipeline.Tiled;
using MonoGame.Extended.Tiled.Serialization;
using NSubstitute;
using Xunit;

namespace MonoGame.Extended.Content.Pipeline.Tests.Tiled
{

    public class TiledMapImporterProcessorTests
    {
        [Fact]
        public void TiledMapImporter_Import_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "level01.tmx");

            var logger = Substitute.For<ContentBuildLogger>();
            var importer = new TiledMapImporter();
            var importerContext = Substitute.For<ContentImporterContext>();
            importerContext.Logger.Returns(logger);

            var contentItem = importer.Import(filePath, importerContext);
            var map = contentItem.Data;

            Assert.Equal("1.0", map.Version);
            Assert.Equal(TiledMapOrientationContent.Orthogonal, map.Orientation);
            Assert.Equal(TiledMapTileDrawOrderContent.RightDown, map.RenderOrder);
            Assert.Equal(20, map.Width);
            Assert.Equal(10, map.Height);
            Assert.Equal(128, map.TileWidth);
            Assert.Equal(128, map.TileHeight);
            Assert.Equal("#7d7d7d", map.BackgroundColor);
            Assert.Equal("awesome", map.Properties[0].Name);
            Assert.Equal("42", map.Properties[0].Value);
            Assert.Single(map.Tilesets);
            Assert.Equal(3, map.Layers.Count);
            Assert.Equal(TiledMapOrientationContent.Orthogonal, map.Orientation);

            var tileset = map.Tilesets.First();
            Assert.Equal(1, tileset.FirstGlobalIdentifier);
            Assert.Equal("free-tileset.png", Path.GetFileName(tileset.Image.Source));
            Assert.Equal(652, tileset.Image.Width);
            Assert.Equal(783, tileset.Image.Height);
            Assert.Equal(2, tileset.Margin);
            Assert.Equal(30, tileset.TileCount);
            Assert.Equal("free-tileset", tileset.Name);
            Assert.Null(tileset.Source);
            Assert.Equal(2, tileset.Spacing);
            //Assert.Equal(0, tileset.TerrainTypes.Count);
            Assert.Empty(tileset.Properties);
            Assert.Equal(128, tileset.TileHeight);
            Assert.Equal(128, tileset.TileWidth);
            Assert.Equal(0, tileset.TileOffset.X);
            Assert.Equal(0, tileset.TileOffset.Y);

            var tileLayer2 = (TiledMapTileLayerContent)map.Layers[0];
            Assert.Equal("Tile Layer 2", tileLayer2.Name);
            Assert.Equal(1, tileLayer2.Opacity);
            Assert.Empty(tileLayer2.Properties);
            Assert.True(tileLayer2.Visible);
            Assert.Equal(200, tileLayer2.Data.Tiles.Count);
            Assert.Equal(0, tileLayer2.X);
            Assert.Equal(0, tileLayer2.Y);

            var imageLayer = (TiledMapImageLayerContent)map.Layers[1];
            Assert.Equal("Image Layer 1", imageLayer.Name);
            Assert.Equal(1, imageLayer.Opacity);
            Assert.Empty(imageLayer.Properties);
            Assert.True(imageLayer.Visible);
            Assert.Equal("hills.png", Path.GetFileName(imageLayer.Image.Source));
            Assert.Equal(100, imageLayer.X);
            Assert.Equal(100, imageLayer.Y);

            var tileLayer1 = (TiledMapTileLayerContent)map.Layers[2];
            Assert.Equal("Tile Layer 1", tileLayer1.Name);
            Assert.Equal(2, tileLayer1.Properties.Count);

            Assert.Equal("customlayerprop", tileLayer1.Properties[0].Name);
            Assert.Equal("1", tileLayer1.Properties[0].Value);

            Assert.Equal("customlayerprop2", tileLayer1.Properties[1].Name);
            Assert.Equal("2", tileLayer1.Properties[1].Value);
        }

        [Fact]
        public void TiledMapImporter_Xml_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-xml.tmx");
            var map = ImportAndProcessMap(filePath);
            var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
            var actualData = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();

            Assert.Null(layer.Data.Encoding);
            Assert.Null(layer.Data.Compression);
            Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(actualData));
        }

        [Fact]
        public void TiledMapImporter_Csv_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-csv.tmx");
            var map = ImportAndProcessMap(filePath);
            var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
            var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();

            Assert.Equal("csv", layer.Data.Encoding);
            Assert.Null(layer.Data.Compression);
            //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
        }

        [Fact]
        public void TiledMapImporter_Base64_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-base64.tmx");
            var map = ImportAndProcessMap(filePath);
            var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
            var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();

            Assert.Equal("base64", layer.Data.Encoding);
            Assert.Null(layer.Data.Compression);
            //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
        }

        [Fact]
        public void TiledMapImporter_Gzip_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-gzip.tmx");
            var map = ImportAndProcessMap(filePath);
            var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
            var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();

            Assert.Equal("base64", layer.Data.Encoding);
            Assert.Equal("gzip", layer.Data.Compression);
            //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
        }


        [Fact]
        public void TiledMapImporter_Zlib_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-tileset-zlib.tmx");
            var map = ImportAndProcessMap(filePath);
            var layer = map.Layers.OfType<TiledMapTileLayerContent>().First();
            var data = layer.Data.Tiles.Select(i => i.GlobalIdentifier).ToArray();

            Assert.Equal("base64", layer.Data.Encoding);
            Assert.Equal("zlib", layer.Data.Compression);
            //Assert.True(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.SequenceEqual(data));
        }

        [Fact]
        public void TiledMapImporter_ObjectLayer_Test()
        {
            var filePath = PathExtensions.GetApplicationFullPath("TestData", "test-object-layer.tmx");
            var map = ImportAndProcessMap(filePath);

            Assert.Single(map.Layers);
            Assert.IsType<TiledMapObjectLayerContent>(map.Layers[0]);
            var tmxObjectGroup = map.Layers[0] as TiledMapObjectLayerContent;
            var tmxObject = tmxObjectGroup.Objects[0];
            var tmxPolygon = tmxObjectGroup.Objects[3].Polygon;
            var tmxPolyline = tmxObjectGroup.Objects[4].Polyline;

            Assert.Equal("Object Layer 1", tmxObjectGroup.Name);
            Assert.Equal(1, tmxObject.Identifier);
            Assert.Equal(131.345f, tmxObject.X);
            Assert.Equal(65.234f, tmxObject.Y);
            Assert.Equal(311.111f, tmxObject.Width);
            Assert.Equal(311.232f, tmxObject.Height);
            Assert.Single(tmxObject.Properties);
            Assert.Equal("shape", tmxObject.Properties[0].Name);
            Assert.Equal("circle", tmxObject.Properties[0].Value);
            Assert.NotNull(tmxObject.Ellipse);
            Assert.False(tmxObjectGroup.Objects[1].Visible);
            Assert.Equal((uint)0, tmxObjectGroup.Objects[1].GlobalIdentifier);
            Assert.Equal((uint)23, tmxObjectGroup.Objects[5].GlobalIdentifier);
            Assert.Equal("rectangle", tmxObjectGroup.Objects[2].Type);
            Assert.Equal("sprite", tmxObjectGroup.Objects[1].Class);
            Assert.NotNull(tmxPolygon);
            Assert.Equal("0,0 180,90 -8,275 -45,81 38,77", tmxPolygon.Points);
            Assert.NotNull(tmxPolyline);
            Assert.Equal("0,0 28,299 326,413 461,308", tmxPolyline.Points);
        }


        private static TiledMapContent ImportAndProcessMap(string filename)
        {
            var logger = Substitute.For<ContentBuildLogger>();
            var importer = new TiledMapImporter();
            var importerContext = Substitute.For<ContentImporterContext>();
            importerContext.Logger.Returns(logger);

            var processor = new TiledMapProcessor();
            var processorContext = Substitute.For<ContentProcessorContext>();
            processorContext.Logger.Returns(logger);

            var import = importer.Import(filename, importerContext);
            var result = processor.Process(import, processorContext);

            return result.Data;
        }
    }
}