Skip to content

Commit

Permalink
Fixes tiler tests
Browse files Browse the repository at this point in the history
Tests were breaking because one asset had transparency encoded as white+transparent, and the images by default output transparency encoded as black+transparent. Since black+transparent makes more sense, we changed the asset.

We also ensured that generated files listed from the filesystem were ordered before they were compared.

Work done for #196
  • Loading branch information
atruskie committed Mar 19, 2020
1 parent 7590128 commit 11c9199
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// <copyright file="AbsoluteDateTimeTilerTests.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace Acoustics.Test.AudioAnalysisTools.TileImage
{
using System;
using System.Collections.Generic;
using SixLabors.ImageSharp;
using System.IO;
using System.Linq;

using Acoustics.Shared.Contracts;
using Acoustics.Test.TestHelpers;
using global::AudioAnalysisTools.LongDurationSpectrograms;
using global::AudioAnalysisTools.LongDurationSpectrograms.Zooming;
using global::AudioAnalysisTools.TileImage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using TestHelpers;
using Acoustics.Shared.Contracts;
using SixLabors.ImageSharp.Processing;
using Drawing = Acoustics.Shared.ImageSharp.Drawing;

Expand Down Expand Up @@ -54,12 +57,6 @@ public void Setup()
256);
}

[TestCleanup]
public void Cleanup()
{
PathHelper.DeleteTempDir(this.outputDirectory);
}

[TestMethod]
public void TestNamingPattern()
{
Expand Down Expand Up @@ -150,8 +147,7 @@ public void TestLeftPaddingInLowerLayers()

var expectedImage = expectedImages[i];
var actualImage = Image.Load<Rgba32>(actualFiles[i].FullName);
var areEqual = TilerTests.BitmapEquals(expectedImage, actualImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedFiles[i], actualFiles[i]);
Assert.That.ImageMatches(expectedImages[i], actualImage, message: $"Bitmaps were not equal {expectedImages[i]}, {actualFiles[i]}");
}
}

Expand All @@ -175,8 +171,7 @@ public void TestItShouldCutAndPadRightWithTransparency()
.ToArray();

var producedImage = Image.Load<Rgba32>(producedFiles[23].FullName);
var areEqual = TilerTests.BitmapEquals((Image<Rgba32>)expectedImages[0], (Image<Rgba32>)producedImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[0], producedFiles[23].Name);
Assert.That.ImageMatches(expectedImages[0], producedImage, message: $"Bitmaps were not equal {expectedImages[0]}, {producedFiles[23].Name}");
}

[TestMethod]
Expand Down Expand Up @@ -234,8 +229,7 @@ public void TestPaddingANonBlockTime()
.ToArray().Length; i++)
{
var producedImage = Image.Load<Rgba32>(producedFiles[i].FullName);
var areEqual = TilerTests.BitmapEquals(expectedImages[i], producedImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[i], producedFiles[i].Name);
Assert.That.ImageMatches(expectedImages[i], producedImage, message: $"Bitmaps were not equal {expectedImages[i]}, {producedFiles[i].Name}");
}
}
}
Expand Down
114 changes: 38 additions & 76 deletions tests/Acoustics.Test/AudioAnalysisTools/TileImage/TilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@ namespace Acoustics.Test.AudioAnalysisTools.TileImage
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp;
using System.IO;
using System.Linq;
using Acoustics.Shared.Contracts;
using Acoustics.Test.TestHelpers;
using global::AudioAnalysisTools.LongDurationSpectrograms;
using global::AudioAnalysisTools.TileImage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using TestHelpers;
using Acoustics.Shared.Contracts;

[TestClass]
public class TilerTests
{
private readonly ISuperTile[] superTileTestCases =
{
MakeTile(24, 0),
MakeTile(24, 1),
MakeTile(12, 16),
MakeTile(12, 15.5),
MakeTile(12, 15),
MakeTile(60, 0),
MakeTile(1.0, 0),
};

private PanoJsTilingProfile tilingProfile;
private Tiler tiler;
private DirectoryInfo outputDirectory;

private Rectangle[] testCases;

private ImageComponent[][] answers;

[TestInitialize]
public void Setup()
{
Expand All @@ -45,12 +60,6 @@ public void Setup()
300);
}

[TestCleanup]
public void Cleanup()
{
PathHelper.DeleteTempDir(this.outputDirectory);
}

[TestMethod]
public void TestCalculatedLayers()
{
Expand Down Expand Up @@ -82,8 +91,6 @@ public void TestCalculatedLayers()
[TestMethod]
public void TestTileManyGroupsTilesByScaleAndSortsByOffset()
{


ISuperTile[] testCases =
{
MakeTile(60, 0),
Expand Down Expand Up @@ -140,17 +147,6 @@ public void TestTileManyGroupsTilesByScaleAndSortsByOffset()
}
}

private readonly ISuperTile[] superTileTestCases =
{
MakeTile(24, 0),
MakeTile(24, 1),
MakeTile(12, 16),
MakeTile(12, 15.5),
MakeTile(12, 15),
MakeTile(60, 0),
MakeTile(1.0, 0),
};

[TestMethod]
public void TestTileManyCatchesRepeatedTilesA()
{
Expand Down Expand Up @@ -188,7 +184,7 @@ public void Test60Resolution()

////Debug.WriteLine(this.outputDirectory.FullName);
////Debug.WriteLine(this.outputDirectory.GetFiles().Length);
var producedFiles = this.outputDirectory.GetFiles();
var producedFiles = this.outputDirectory.GetFiles().OrderBy(x => x.Name).ToArray();

Assert.AreEqual(6, producedFiles.Length);

Expand All @@ -206,8 +202,7 @@ public void Test60Resolution()
for (int i = 0; i < loadedImages.Length; i++)
{
var producedImage = Image.Load<Rgba32>(producedFiles[i].FullName);
var areEqual = BitmapEquals(loadedImages[i], producedImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[i], producedFiles[i].Name);
Assert.That.ImageMatches(loadedImages[i], producedImage, message: $"Bitmaps were not equal {expectedImages[i]}, {producedFiles[i].Name}");
}
}

Expand Down Expand Up @@ -246,33 +241,8 @@ public void Test1Resolution()
for (int i = 0; i < loadedImages.Length; i++)
{
var producedImage = Image.Load<Rgba32>(producedFiles[i].FullName);
var areEqual = BitmapEquals(loadedImages[i], producedImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[i], producedFiles[i].Name);
}
}

public static bool BitmapEquals(Image<Rgba32> bmp1, Image<Rgba32> bmp2)
{
if (!bmp1.Size().Equals(bmp2.Size()))
{
return false;
}

for (int x = 0; x < bmp1.Width; ++x)
{
for (int y = 0; y < bmp1.Height; ++y)
{
var pixel1 = bmp1[x, y];
var pixel2 = bmp2[x, y];
var alphaEqual = pixel1.A == 0 && pixel2.A == 0;
if (pixel1 != pixel2 && !alphaEqual)
{
return false;
}
}
Assert.That.ImageMatches(loadedImages[i], producedImage, message: $"Bitmaps were not equal {expectedImages[i]}, {producedFiles[i].Name}");
}

return true;
}

[TestMethod]
Expand Down Expand Up @@ -342,7 +312,6 @@ public void EnsureSameTileNotRenderedTwice_ForWeirdlyOrderedResults()
TestHelper.ExceptionMatches<DuplicateTileException>(
() => { singleScaleTiler.Tile(testCases[1]); },
"Tile 'panojstile_00000_00001_00000' has already been created");

}

[TestMethod]
Expand Down Expand Up @@ -418,10 +387,8 @@ public void EnsureSameThreeTilesWrittenForTwoOddlySizedSuperTiles()
for (int i = 0; i < loadedImages.Length; i++)
{
var producedImage = Image.Load<Rgba32>(producedFiles[i].FullName);
var areEqual = BitmapEquals(loadedImages[i], (producedImage));
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[i], producedFiles[i].Name);
Assert.That.ImageMatches(loadedImages[i], producedImage, message: $"Bitmaps were not equal {expectedImages[i]}, {producedFiles[i].Name}");
}

}

[TestMethod]
Expand All @@ -431,13 +398,12 @@ public void EnsureReallyShortRecordingsWork()
testBitmap.Mutate(graphics =>
{
graphics.Fill(Brushes.Solid(Color.Red), testBitmap.Bounds());

});

var superTile = new TimeOffsetSingleLayerSuperTile(TimeSpan.Zero, SpectrogramType.Index, 60.Seconds(), testBitmap, TimeSpan.Zero);
var superTile = new TimeOffsetSingleLayerSuperTile(TimeSpan.Zero, SpectrogramType.Index, 60.Seconds(), testBitmap, TimeSpan.Zero);
this.tiler.Tile(superTile);

var producedFiles = this.outputDirectory.GetFiles();
var producedFiles = this.outputDirectory.GetFiles().OrderBy(x => x.Name).ToArray();

Assert.AreEqual(1, producedFiles.Length);

Expand All @@ -446,16 +412,14 @@ public void EnsureReallyShortRecordingsWork()
expected.Mutate(graphics =>
{
graphics.Fill(Brushes.Solid(Color.Red), new Rectangle(180, 0, 2, 300));

});

var expectedImages = new[] { expected };

for (int i = 0; i < expectedImages.Length; i++)
{
var producedImage = Image.Load<Rgba32>(producedFiles[i].FullName);
var areEqual = BitmapEquals(expectedImages[i], (Image<Rgba32>)producedImage);
Assert.IsTrue(areEqual, "Bitmaps were not equal {0}, {1}", expectedImages[i], producedFiles[i].Name);
Assert.That.ImageMatches(expectedImages[i], producedImage, message: $"Bitmaps were not equal {producedImage}, {producedFiles[i].Name}");
}
}

Expand All @@ -471,7 +435,7 @@ public void EnsureReallyShortRecordingsWork()
/// ---------------
/// 3 b a | b a | b a
/// 3 c d | c d | c d
///
/// .
/// </summary>
[TestMethod]
public void TestGetImageParts()
Expand Down Expand Up @@ -507,11 +471,19 @@ public void TestGetImagePartsNonInstersectingRectangle()
Tiler.GetImageParts(baseRectangle, otherRectangle);
}

private Rectangle[] testCases;

private ImageComponent[][] answers;
private static TimeOffsetSingleLayerSuperTile MakeTile(double scale, double offsetMinutes)
{
return new TimeOffsetSingleLayerSuperTile(
TimeSpan.Zero,
SpectrogramType.Index,
scale.Seconds(),
null,
TimeSpan.FromMinutes(offsetMinutes));
}

[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation",
[SuppressMessage(
"StyleCop.CSharp.NamingRules",
"SA1305:FieldNamesMustNotUseHungarianNotation",
Justification = "Reviewed. Suppression is OK here.")]
private void InitializeAnswers()
{
Expand Down Expand Up @@ -638,15 +610,5 @@ private void InitializeAnswers()

// ReSharper restore InconsistentNaming
}

private static TimeOffsetSingleLayerSuperTile MakeTile(double scale, double offsetMinutes)
{
return new TimeOffsetSingleLayerSuperTile(
TimeSpan.Zero,
SpectrogramType.Index,
scale.Seconds(),
null,
TimeSpan.FromMinutes(offsetMinutes));
}
}
}
2 changes: 1 addition & 1 deletion tests/Acoustics.Test/TestHelpers/GeneratedImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected GeneratedImageTest(WriteTestOutput writeImages, WriteTestOutput writeD
/// <summary>
/// Gets or sets extra string tokens to insert into saved imaged file names.
/// MSTest does not support getting any information about data rows
/// or dynamic data tests at runtime :-/
/// or dynamic data tests at runtime :-/.
/// </summary>
protected string ExtraName { get; set; }

Expand Down
16 changes: 0 additions & 16 deletions tests/Acoustics.Test/TestHelpers/ImageAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,6 @@ public static void ImageRegionHasColors(
}
}

public static void ImageMatches(this Assert assert, Image<Rgb24> expectedImage, Image<Rgb24> actualImage, double tolerance = 0.0, string message = "")
{
Assert.AreEqual(expectedImage.Size(), actualImage.Size());

var expectedHistogram = ImageTools.GetColorHistogramNormalized(expectedImage);
var actualHistogram = ImageTools.GetColorHistogramNormalized(actualImage);

// sum the deltas
var delta = expectedHistogram
.Zip(actualHistogram, ValueTuple.Create)
.Select(pair => Math.Abs(pair.Item1.Value - pair.Item2.Value))
.Sum();

Assert.IsTrue(delta <= tolerance, $"Images are not equal - total delta {delta} is not less than tolerance {tolerance}.\n" + message);
}

public static void ImageRegionIsRepeatedHorizontally(this Assert assert, Rectangle region, int repeats, int spacing, Image<Rgb24> actualImage, double tolerance = 0.0)
{
Contract.Requires(spacing >= 1);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 11c9199

Please sign in to comment.