Skip to content

Commit e976dba

Browse files
Danielku15ChiHoc
authored andcommitted
Respect ties correctly in overflows (CoderLine#1002)
1 parent 9f49de8 commit e976dba

33 files changed

+673
-392
lines changed

src.csharp/AlphaTab.Test/VisualTests/VisualTestHelper.cs

+79-96
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
6+
using AlphaTab.Core;
57
using AlphaTab.Core.EcmaScript;
68
using AlphaTab.Importer;
79
using AlphaTab.Io;
@@ -15,140 +17,113 @@ namespace AlphaTab.VisualTests
1517
{
1618
partial class VisualTestHelper
1719
{
18-
public static async Task RunVisualTest(string inputFile, Settings? settings = null,
19-
IList<double>? tracks = null, string? message = null, double tolerancePercent = 1, bool triggerResize = false)
20+
private static async Task RunVisualTestScoreWithResize(Score score, IList<double> widths,
21+
IList<string?> referenceImages, Settings? settings, IList<double>? tracks, string? message,
22+
double tolerancePercent)
2023
{
21-
try
22-
{
23-
inputFile = $"test-data/visual-tests/{inputFile}";
24-
var inputFileData =
25-
await TestPlatform.LoadFile(inputFile);
26-
var referenceFileName = TestPlatform.ChangeExtension(inputFile, ".png");
27-
var score = ScoreLoader.LoadScoreFromBytes(inputFileData, settings);
28-
29-
await RunVisualTestScore(score, referenceFileName, settings,
30-
tracks, message, tolerancePercent, triggerResize);
31-
}
32-
catch (Exception e)
33-
{
34-
Assert.Fail($"Failed to run visual test {e}");
35-
}
36-
}
37-
38-
public static async Task RunVisualTestTex(string tex, string referenceFileName,
39-
Settings? settings = null,
40-
IList<double>? tracks = null, string? message = null)
41-
{
42-
try
43-
{
44-
settings ??= new Settings();
45-
46-
var importer = new AlphaTexImporter();
47-
importer.Init(ByteBuffer.FromString(tex), settings);
48-
var score = importer.ReadScore();
49-
50-
await RunVisualTestScore(score, referenceFileName, settings,
51-
tracks, message);
52-
}
53-
catch (Exception e)
54-
{
55-
Assert.Fail($"Failed to run visual test {e}");
56-
}
57-
}
58-
59-
public static async Task RunVisualTestScore(Score score, string referenceFileName,
60-
Settings? settings = null,
61-
IList<double>? tracks = null, string? message = null, double tolerancePercent = 1, bool triggerResize = false)
62-
{
63-
settings ??= new Settings();
64-
tracks ??= new AlphaTab.Collections.List<double> {0};
65-
66-
settings.Core.Engine = "skia";
67-
settings.Core.EnableLazyLoading = false;
68-
settings.Core.UseWorkers = false;
69-
70-
settings.Display.Resources.CopyrightFont.Family = "Roboto";
71-
settings.Display.Resources.TitleFont.Family = "PT Serif";
72-
settings.Display.Resources.SubTitleFont.Family = "PT Serif";
73-
settings.Display.Resources.WordsFont.Family = "PT Serif";
74-
settings.Display.Resources.EffectFont.Family = "PT Serif";
75-
settings.Display.Resources.FretboardNumberFont.Family = "Roboto";
76-
settings.Display.Resources.TablatureFont.Family = "Roboto";
77-
settings.Display.Resources.GraceFont.Family = "Roboto";
78-
settings.Display.Resources.BarNumberFont.Family = "Roboto";
79-
settings.Display.Resources.FingeringFont.Family = "PT Serif";
80-
settings.Display.Resources.MarkerFont.Family = "PT Serif";
81-
82-
LoadFonts();
24+
tracks ??= new List<double> { 0 };
25+
PrepareSettingsForTest(ref settings);
8326

84-
if (!referenceFileName.StartsWith("test-data/"))
27+
var referenceFileData = new List<Uint8Array?>();
28+
foreach (var referenceFileName in referenceImages)
8529
{
86-
referenceFileName = $"test-data/visual-tests/{referenceFileName}";
30+
if (referenceFileName == null)
31+
{
32+
referenceFileData.Add(null);
33+
}
34+
else
35+
{
36+
referenceFileData.Add(await TestPlatform.LoadFile(Path.Combine("test-data", "visual-tests", referenceFileName)));
37+
}
8738
}
8839

89-
var referenceFileData =
90-
await TestPlatform.LoadFile(referenceFileName);
91-
92-
var result = new AlphaTab.Collections.List<RenderFinishedEventArgs>();
93-
var totalWidth = 0.0;
94-
var totalHeight = 0.0;
95-
var isResizeRender = false;
40+
var results = new AlphaTab.Collections.List<AlphaTab.Collections.List<RenderFinishedEventArgs>>();
41+
var totalWidths = new AlphaTab.Collections.List<double>();
42+
var totalHeights = new AlphaTab.Collections.List<double>();
9643

9744
var task = new TaskCompletionSource<object?>();
9845
var renderer = new ScoreRenderer(settings)
9946
{
100-
Width = 1300
47+
Width = widths.Shift()
10148
};
10249
renderer.PreRender.On(isResize =>
10350
{
104-
result = new AlphaTab.Collections.List<RenderFinishedEventArgs>();
105-
totalWidth = 0.0;
106-
totalHeight = 0.0;
51+
results.Add(new AlphaTab.Collections.List<RenderFinishedEventArgs>());
52+
totalWidths.Add(0);
53+
totalHeights.Add(0);
10754
});
10855
renderer.PartialRenderFinished.On(e =>
10956
{
11057
if (e != null)
11158
{
112-
result.Add(e);
59+
results[^1].Add(e);
11360
}
11461
});
11562
renderer.RenderFinished.On(e =>
11663
{
117-
totalWidth = e.TotalWidth;
118-
totalHeight = e.TotalHeight;
119-
result.Add(e);
120-
if(!triggerResize || isResizeRender)
64+
totalWidths[^1] = e.TotalWidth;
65+
totalHeights[^1] = e.TotalHeight;
66+
results[^1].Add(e);
67+
if (widths.Count > 0)
12168
{
122-
task.SetResult(null);
69+
renderer.Width = widths.Shift();
70+
renderer.ResizeRender();
12371
}
124-
else if(triggerResize)
72+
else
12573
{
126-
isResizeRender = true;
127-
renderer.ResizeRender();
74+
task.SetResult(null);
12875
}
12976
});
13077
renderer.Error.On((e) => { task.SetException(e); });
13178

13279
renderer.RenderScore(score, tracks);
13380

134-
if (await Task.WhenAny(task.Task, Task.Delay(2000)) == task.Task)
81+
if (await Task.WhenAny(task.Task, Task.Delay(2000 * referenceImages.Count)) == task.Task)
13582
{
136-
CompareVisualResult(
137-
totalWidth,
138-
totalHeight,
139-
result,
140-
referenceFileName,
141-
referenceFileData,
142-
message,
143-
tolerancePercent
144-
);
83+
for (var i = 0; i < results.Count; i++)
84+
{
85+
if (referenceImages[i] != null)
86+
{
87+
CompareVisualResult(
88+
totalWidths[i],
89+
totalHeights[i],
90+
results[i],
91+
referenceImages[i]!,
92+
referenceFileData[i]!,
93+
message,
94+
tolerancePercent
95+
);
96+
}
97+
}
14598
}
14699
else
147100
{
148101
Assert.Fail("Rendering did not complete within timeout");
149102
}
150103
}
151104

105+
private static void PrepareSettingsForTest(ref Settings? settings)
106+
{
107+
settings ??= new Settings();
108+
settings.Core.Engine = "skia";
109+
settings.Core.EnableLazyLoading = false;
110+
settings.Core.UseWorkers = false;
111+
112+
settings.Display.Resources.CopyrightFont.Family = "Roboto";
113+
settings.Display.Resources.TitleFont.Family = "PT Serif";
114+
settings.Display.Resources.SubTitleFont.Family = "PT Serif";
115+
settings.Display.Resources.WordsFont.Family = "PT Serif";
116+
settings.Display.Resources.EffectFont.Family = "PT Serif";
117+
settings.Display.Resources.FretboardNumberFont.Family = "Roboto";
118+
settings.Display.Resources.TablatureFont.Family = "Roboto";
119+
settings.Display.Resources.GraceFont.Family = "Roboto";
120+
settings.Display.Resources.BarNumberFont.Family = "Roboto";
121+
settings.Display.Resources.FingeringFont.Family = "PT Serif";
122+
settings.Display.Resources.MarkerFont.Family = "PT Serif";
123+
124+
LoadFonts();
125+
}
126+
152127
private static bool _fontsLoaded;
153128
private static void LoadFonts()
154129
{
@@ -220,6 +195,10 @@ private static void CompareVisualResult(double totalWidth, double totalHeight,
220195
{
221196
try
222197
{
198+
Assert.AreEqual(totalWidth, referenceBitmap.Width,
199+
"Width of images does not match");
200+
Assert.AreEqual(totalHeight, referenceBitmap.Height,
201+
"Height of images does not match");
223202
var diffData = new Uint8Array(finalBitmap.Bytes.Length);
224203
var match = PixelMatch.Match(
225204
new Uint8Array(referenceBitmap.Bytes),
@@ -267,6 +246,10 @@ private static void CompareVisualResult(double totalWidth, double totalHeight,
267246
Assert.Fail(msg);
268247
}
269248
}
249+
catch (AssertFailedException)
250+
{
251+
throw;
252+
}
270253
catch (Exception e)
271254
{
272255
Assert.Fail($"Error comparing images: {e}, ${message}");

src.csharp/AlphaTab/Core/EcmaScript/Math.cs

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace AlphaTab.Core.EcmaScript
45
{
@@ -17,6 +18,16 @@ public static double Abs(double v)
1718
return System.Math.Abs(v);
1819
}
1920

21+
public static double Max(params double[] items)
22+
{
23+
return items.Max();
24+
}
25+
26+
public static double Min(params double[] items)
27+
{
28+
return items.Min();
29+
}
30+
2031
public static double Max(double a, double b)
2132
{
2233
return System.Math.Max(a, b);

src.csharp/AlphaTab/Core/TypeHelper.cs

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ public static IList<T> CreateList<T>(params T[] values)
1414
return new List<T>(values);
1515
}
1616

17+
public static void Add<T>(this IList<T> list, IList<T> newItems)
18+
{
19+
if(list is List<T> l)
20+
{
21+
l.AddRange(newItems);
22+
}
23+
else
24+
{
25+
foreach (var i in newItems)
26+
{
27+
list.Add(i);
28+
}
29+
}
30+
}
31+
1732
public static IList<T> Splice<T>(this IList<T> data, double start, double deleteCount)
1833
{
1934
var items = data.GetRange((int) start, (int) deleteCount);
@@ -101,6 +116,13 @@ public static void Unshift<T>(this IList<T> data, T item)
101116
data.Insert(0, item);
102117
}
103118

119+
public static T Shift<T>(this IList<T> data)
120+
{
121+
var i = data[0];
122+
data.RemoveAt(0);
123+
return i;
124+
}
125+
104126
public static T Pop<T>(this IList<T> data)
105127
{
106128
if (data.Count > 0)

src.kotlin/alphaTab/alphaTab/build.gradle.kts

-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ kotlin {
7979
}
8080
}
8181

82-
val androidAndroidTestRelease by getting
8382
val androidTest by getting {
84-
dependsOn(androidAndroidTestRelease)
8583
dependencies {
8684
implementation(kotlin("test-junit"))
8785
implementation("junit:junit:4.13.2")

src.kotlin/alphaTab/alphaTab/src/androidMain/kotlin/alphaTab/platform/android/AndroidUiFacade.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
359359
error: (arg1: Error) -> Unit
360360
): Boolean {
361361
when (data) {
362-
data is Score -> {
362+
(data is Score) -> {
363363
success(data as Score)
364364
return true
365365
}
366-
data is ByteArray -> {
366+
(data is ByteArray) -> {
367367
success(
368368
ScoreLoader.loadScoreFromBytes(
369369
Uint8Array((data as ByteArray).asUByteArray()),
@@ -372,7 +372,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
372372
)
373373
return true
374374
}
375-
data is UByteArray -> {
375+
(data is UByteArray) -> {
376376
success(
377377
ScoreLoader.loadScoreFromBytes(
378378
Uint8Array((data as UByteArray)),
@@ -381,7 +381,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
381381
)
382382
return true
383383
}
384-
data is InputStream -> {
384+
(data is InputStream) -> {
385385
val bos = ByteArrayOutputStream()
386386
(data as InputStream).copyTo(bos)
387387
success(
@@ -402,15 +402,15 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
402402
val player = api.player ?: return false
403403

404404
when (data) {
405-
data is ByteArray -> {
405+
(data is ByteArray) -> {
406406
player.loadSoundFont(Uint8Array((data as ByteArray).asUByteArray()), append)
407407
return true
408408
}
409-
data is UByteArray -> {
409+
(data is UByteArray) -> {
410410
player.loadSoundFont(Uint8Array((data as UByteArray)), append)
411411
return true
412412
}
413-
data is InputStream -> {
413+
(data is InputStream) -> {
414414
val bos = ByteArrayOutputStream()
415415
(data as InputStream).copyTo(bos)
416416
player.loadSoundFont(Uint8Array(bos.toByteArray().asUByteArray()), append)

0 commit comments

Comments
 (0)