Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect ties correctly in overflows #1002

Merged
merged 4 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 79 additions & 96 deletions src.csharp/AlphaTab.Test/VisualTests/VisualTestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AlphaTab.Core;
using AlphaTab.Core.EcmaScript;
using AlphaTab.Importer;
using AlphaTab.Io;
Expand All @@ -15,140 +17,113 @@ namespace AlphaTab.VisualTests
{
partial class VisualTestHelper
{
public static async Task RunVisualTest(string inputFile, Settings? settings = null,
IList<double>? tracks = null, string? message = null, double tolerancePercent = 1, bool triggerResize = false)
private static async Task RunVisualTestScoreWithResize(Score score, IList<double> widths,
IList<string?> referenceImages, Settings? settings, IList<double>? tracks, string? message,
double tolerancePercent)
{
try
{
inputFile = $"test-data/visual-tests/{inputFile}";
var inputFileData =
await TestPlatform.LoadFile(inputFile);
var referenceFileName = TestPlatform.ChangeExtension(inputFile, ".png");
var score = ScoreLoader.LoadScoreFromBytes(inputFileData, settings);

await RunVisualTestScore(score, referenceFileName, settings,
tracks, message, tolerancePercent, triggerResize);
}
catch (Exception e)
{
Assert.Fail($"Failed to run visual test {e}");
}
}

public static async Task RunVisualTestTex(string tex, string referenceFileName,
Settings? settings = null,
IList<double>? tracks = null, string? message = null)
{
try
{
settings ??= new Settings();

var importer = new AlphaTexImporter();
importer.Init(ByteBuffer.FromString(tex), settings);
var score = importer.ReadScore();

await RunVisualTestScore(score, referenceFileName, settings,
tracks, message);
}
catch (Exception e)
{
Assert.Fail($"Failed to run visual test {e}");
}
}

public static async Task RunVisualTestScore(Score score, string referenceFileName,
Settings? settings = null,
IList<double>? tracks = null, string? message = null, double tolerancePercent = 1, bool triggerResize = false)
{
settings ??= new Settings();
tracks ??= new AlphaTab.Collections.List<double> {0};

settings.Core.Engine = "skia";
settings.Core.EnableLazyLoading = false;
settings.Core.UseWorkers = false;

settings.Display.Resources.CopyrightFont.Family = "Roboto";
settings.Display.Resources.TitleFont.Family = "PT Serif";
settings.Display.Resources.SubTitleFont.Family = "PT Serif";
settings.Display.Resources.WordsFont.Family = "PT Serif";
settings.Display.Resources.EffectFont.Family = "PT Serif";
settings.Display.Resources.FretboardNumberFont.Family = "Roboto";
settings.Display.Resources.TablatureFont.Family = "Roboto";
settings.Display.Resources.GraceFont.Family = "Roboto";
settings.Display.Resources.BarNumberFont.Family = "Roboto";
settings.Display.Resources.FingeringFont.Family = "PT Serif";
settings.Display.Resources.MarkerFont.Family = "PT Serif";

LoadFonts();
tracks ??= new List<double> { 0 };
PrepareSettingsForTest(ref settings);

if (!referenceFileName.StartsWith("test-data/"))
var referenceFileData = new List<Uint8Array?>();
foreach (var referenceFileName in referenceImages)
{
referenceFileName = $"test-data/visual-tests/{referenceFileName}";
if (referenceFileName == null)
{
referenceFileData.Add(null);
}
else
{
referenceFileData.Add(await TestPlatform.LoadFile(Path.Combine("test-data", "visual-tests", referenceFileName)));
}
}

var referenceFileData =
await TestPlatform.LoadFile(referenceFileName);

var result = new AlphaTab.Collections.List<RenderFinishedEventArgs>();
var totalWidth = 0.0;
var totalHeight = 0.0;
var isResizeRender = false;
var results = new AlphaTab.Collections.List<AlphaTab.Collections.List<RenderFinishedEventArgs>>();
var totalWidths = new AlphaTab.Collections.List<double>();
var totalHeights = new AlphaTab.Collections.List<double>();

var task = new TaskCompletionSource<object?>();
var renderer = new ScoreRenderer(settings)
{
Width = 1300
Width = widths.Shift()
};
renderer.PreRender.On(isResize =>
{
result = new AlphaTab.Collections.List<RenderFinishedEventArgs>();
totalWidth = 0.0;
totalHeight = 0.0;
results.Add(new AlphaTab.Collections.List<RenderFinishedEventArgs>());
totalWidths.Add(0);
totalHeights.Add(0);
});
renderer.PartialRenderFinished.On(e =>
{
if (e != null)
{
result.Add(e);
results[^1].Add(e);
}
});
renderer.RenderFinished.On(e =>
{
totalWidth = e.TotalWidth;
totalHeight = e.TotalHeight;
result.Add(e);
if(!triggerResize || isResizeRender)
totalWidths[^1] = e.TotalWidth;
totalHeights[^1] = e.TotalHeight;
results[^1].Add(e);
if (widths.Count > 0)
{
task.SetResult(null);
renderer.Width = widths.Shift();
renderer.ResizeRender();
}
else if(triggerResize)
else
{
isResizeRender = true;
renderer.ResizeRender();
task.SetResult(null);
}
});
renderer.Error.On((e) => { task.SetException(e); });

renderer.RenderScore(score, tracks);

if (await Task.WhenAny(task.Task, Task.Delay(2000)) == task.Task)
if (await Task.WhenAny(task.Task, Task.Delay(2000 * referenceImages.Count)) == task.Task)
{
CompareVisualResult(
totalWidth,
totalHeight,
result,
referenceFileName,
referenceFileData,
message,
tolerancePercent
);
for (var i = 0; i < results.Count; i++)
{
if (referenceImages[i] != null)
{
CompareVisualResult(
totalWidths[i],
totalHeights[i],
results[i],
referenceImages[i]!,
referenceFileData[i]!,
message,
tolerancePercent
);
}
}
}
else
{
Assert.Fail("Rendering did not complete within timeout");
}
}

private static void PrepareSettingsForTest(ref Settings? settings)
{
settings ??= new Settings();
settings.Core.Engine = "skia";
settings.Core.EnableLazyLoading = false;
settings.Core.UseWorkers = false;

settings.Display.Resources.CopyrightFont.Family = "Roboto";
settings.Display.Resources.TitleFont.Family = "PT Serif";
settings.Display.Resources.SubTitleFont.Family = "PT Serif";
settings.Display.Resources.WordsFont.Family = "PT Serif";
settings.Display.Resources.EffectFont.Family = "PT Serif";
settings.Display.Resources.FretboardNumberFont.Family = "Roboto";
settings.Display.Resources.TablatureFont.Family = "Roboto";
settings.Display.Resources.GraceFont.Family = "Roboto";
settings.Display.Resources.BarNumberFont.Family = "Roboto";
settings.Display.Resources.FingeringFont.Family = "PT Serif";
settings.Display.Resources.MarkerFont.Family = "PT Serif";

LoadFonts();
}

private static bool _fontsLoaded;
private static void LoadFonts()
{
Expand Down Expand Up @@ -220,6 +195,10 @@ private static void CompareVisualResult(double totalWidth, double totalHeight,
{
try
{
Assert.AreEqual(totalWidth, referenceBitmap.Width,
"Width of images does not match");
Assert.AreEqual(totalHeight, referenceBitmap.Height,
"Height of images does not match");
var diffData = new Uint8Array(finalBitmap.Bytes.Length);
var match = PixelMatch.Match(
new Uint8Array(referenceBitmap.Bytes),
Expand Down Expand Up @@ -267,6 +246,10 @@ private static void CompareVisualResult(double totalWidth, double totalHeight,
Assert.Fail(msg);
}
}
catch (AssertFailedException)
{
throw;
}
catch (Exception e)
{
Assert.Fail($"Error comparing images: {e}, ${message}");
Expand Down
11 changes: 11 additions & 0 deletions src.csharp/AlphaTab/Core/EcmaScript/Math.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace AlphaTab.Core.EcmaScript
{
Expand All @@ -17,6 +18,16 @@ public static double Abs(double v)
return System.Math.Abs(v);
}

public static double Max(params double[] items)
{
return items.Max();
}

public static double Min(params double[] items)
{
return items.Min();
}

public static double Max(double a, double b)
{
return System.Math.Max(a, b);
Expand Down
22 changes: 22 additions & 0 deletions src.csharp/AlphaTab/Core/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public static IList<T> CreateList<T>(params T[] values)
return new List<T>(values);
}

public static void Add<T>(this IList<T> list, IList<T> newItems)
{
if(list is List<T> l)
{
l.AddRange(newItems);
}
else
{
foreach (var i in newItems)
{
list.Add(i);
}
}
}

public static IList<T> Splice<T>(this IList<T> data, double start, double deleteCount)
{
var items = data.GetRange((int) start, (int) deleteCount);
Expand Down Expand Up @@ -101,6 +116,13 @@ public static void Unshift<T>(this IList<T> data, T item)
data.Insert(0, item);
}

public static T Shift<T>(this IList<T> data)
{
var i = data[0];
data.RemoveAt(0);
return i;
}

public static T Pop<T>(this IList<T> data)
{
if (data.Count > 0)
Expand Down
2 changes: 0 additions & 2 deletions src.kotlin/alphaTab/alphaTab/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ kotlin {
}
}

val androidAndroidTestRelease by getting
val androidTest by getting {
dependsOn(androidAndroidTestRelease)
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
error: (arg1: Error) -> Unit
): Boolean {
when (data) {
data is Score -> {
(data is Score) -> {
success(data as Score)
return true
}
data is ByteArray -> {
(data is ByteArray) -> {
success(
ScoreLoader.loadScoreFromBytes(
Uint8Array((data as ByteArray).asUByteArray()),
Expand All @@ -372,7 +372,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
)
return true
}
data is UByteArray -> {
(data is UByteArray) -> {
success(
ScoreLoader.loadScoreFromBytes(
Uint8Array((data as UByteArray)),
Expand All @@ -381,7 +381,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
)
return true
}
data is InputStream -> {
(data is InputStream) -> {
val bos = ByteArrayOutputStream()
(data as InputStream).copyTo(bos)
success(
Expand All @@ -402,15 +402,15 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
val player = api.player ?: return false

when (data) {
data is ByteArray -> {
(data is ByteArray) -> {
player.loadSoundFont(Uint8Array((data as ByteArray).asUByteArray()), append)
return true
}
data is UByteArray -> {
(data is UByteArray) -> {
player.loadSoundFont(Uint8Array((data as UByteArray)), append)
return true
}
data is InputStream -> {
(data is InputStream) -> {
val bos = ByteArrayOutputStream()
(data as InputStream).copyTo(bos)
player.loadSoundFont(Uint8Array(bos.toByteArray().asUByteArray()), append)
Expand Down
Loading