jellyfin-server/MediaBrowser.Tests/MediaEncoding/Subtitles/SrtParserTests.cs

115 lines
11 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2014-05-11 02:29:34 +00:00
using System.IO;
using System.Threading;
using Emby.Server.MediaEncoding.Subtitles;
using Microsoft.Extensions.Logging;
2015-07-20 03:43:13 +00:00
using MediaBrowser.Model.MediaInfo;
2014-05-11 02:29:34 +00:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2014-08-31 18:28:20 +00:00
namespace MediaBrowser.Tests.MediaEncoding.Subtitles
{
2014-05-11 02:29:34 +00:00
[TestClass]
2014-08-31 18:28:20 +00:00
public class SrtParserTests
{
2014-05-11 02:29:34 +00:00
[TestMethod]
2014-08-31 18:28:20 +00:00
public void TestParse()
{
2014-05-11 02:29:34 +00:00
var expectedSubs =
2014-08-31 18:28:20 +00:00
new SubtitleTrackInfo
{
2017-08-19 19:43:35 +00:00
TrackEvents = new SubtitleTrackEvent[] {
2014-05-11 02:29:34 +00:00
new SubtitleTrackEvent {
Id = "1",
StartPositionTicks = 24000000,
EndPositionTicks = 52000000,
Text =
"[Background Music Playing]"
},
new SubtitleTrackEvent {
Id = "2",
StartPositionTicks = 157120000,
EndPositionTicks = 173990000,
Text =
2014-08-31 18:28:20 +00:00
"Oh my god, Watch out!"+ParserValues.NewLine+"It's coming!!"
2014-05-11 02:29:34 +00:00
},
new SubtitleTrackEvent {
Id = "3",
StartPositionTicks = 257120000,
EndPositionTicks = 303990000,
Text = "[Bird noises]"
},
new SubtitleTrackEvent {
Id = "4",
StartPositionTicks = 310000000,
EndPositionTicks = 319990000,
Text =
"This text is <font color=\"red\">RED</font> and has not been positioned."
},
new SubtitleTrackEvent {
Id = "5",
StartPositionTicks = 320000000,
EndPositionTicks = 329990000,
Text =
2014-08-31 18:28:20 +00:00
"This is a"+ParserValues.NewLine+"new line, as is"+ParserValues.NewLine+"this"
2014-05-11 02:29:34 +00:00
},
new SubtitleTrackEvent {
Id = "6",
StartPositionTicks = 330000000,
EndPositionTicks = 339990000,
Text =
"This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags"
},
new SubtitleTrackEvent {
Id = "7",
StartPositionTicks = 340000000,
EndPositionTicks = 349990000,
Text =
"Unclosed but <b>supported HTML tags are left in, SSA italics aren't"
},
new SubtitleTrackEvent {
Id = "8",
StartPositionTicks = 350000000,
EndPositionTicks = 359990000,
Text =
"&lt;ggg&gt;Unsupported&lt;/ggg&gt; HTML tags are escaped and left in, even if &lt;hhh&gt;not closed."
},
new SubtitleTrackEvent {
Id = "9",
StartPositionTicks = 360000000,
EndPositionTicks = 369990000,
Text =
"Multiple SSA tags are stripped"
},
new SubtitleTrackEvent {
Id = "10",
StartPositionTicks = 370000000,
EndPositionTicks = 379990000,
Text =
"Greater than (&lt;) and less than (&gt;) are shown"
}
}
2014-08-31 18:28:20 +00:00
};
2014-05-11 02:29:34 +00:00
2015-01-21 19:21:19 +00:00
var sut = new SrtParser(new NullLogger());
2014-05-11 02:29:34 +00:00
var stream = File.OpenRead(@"MediaEncoding\Subtitles\TestSubtitles\unit.srt");
var result = sut.Parse(stream, CancellationToken.None);
2014-05-11 02:29:34 +00:00
Assert.IsNotNull(result);
2017-08-19 19:43:35 +00:00
Assert.AreEqual(expectedSubs.TrackEvents.Length, result.TrackEvents.Length);
for (int i = 0; i < expectedSubs.TrackEvents.Length; i++)
2014-05-11 02:29:34 +00:00
{
Assert.AreEqual(expectedSubs.TrackEvents[i].Id, result.TrackEvents[i].Id);
Assert.AreEqual(expectedSubs.TrackEvents[i].StartPositionTicks, result.TrackEvents[i].StartPositionTicks);
Assert.AreEqual(expectedSubs.TrackEvents[i].EndPositionTicks, result.TrackEvents[i].EndPositionTicks);
Assert.AreEqual(expectedSubs.TrackEvents[i].Text, result.TrackEvents[i].Text);
}
}
}
}