using System.Xml.Linq;
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Schema;
using VersOne.Epub.Test.Comparers;
using VersOne.Epub.Test.Unit.Mocks;
using VersOne.Epub.Test.Unit.TestData;
namespace VersOne.Epub.Test.Unit.Readers
{
public class SmilReaderTests
{
private const string CONTENT_DIRECTORY_PATH = "Content";
private const string SMIL_FILE_NAME = "chapter1.smil";
private const string SMIL_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{SMIL_FILE_NAME}";
private const string MINIMAL_SMIL_FILE = """
""";
private const string FULL_SMIL_FILE = """
value1
value2
""";
private const string SMIL_FILE_WITHOUT_SMIL_ELEMENT = """
""";
private const string SMIL_FILE_WITH_WRONG_SMIL_VERSION = """
""";
private const string SMIL_FILE_WITHOUT_BODY_ELEMENT = """
""";
private const string MINIMAL_SMIL_FILE_WITH_NON_METADATA_ELEMENT_IN_HEAD = """
""";
private const string SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_BODY = """
""";
private const string SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_SEQ = """
""";
private const string SMIL_FILE_WITHOUT_TEXT_ELEMENT_IN_PAR = """
""";
private const string SMIL_FILE_WITHOUT_TEXT_SRC_ATTRIBUTE = """
""";
private const string SMIL_FILE_WITHOUT_AUDIO_SRC_ATTRIBUTE = """
""";
private static Smil MinimalSmil =>
new
(
id: null,
version: SmilVersion.SMIL_3,
epubPrefix: null,
head: null,
body: new SmilBody
(
id: null,
epubTypes: null,
epubTextRef: null,
seqs: new List(),
pars: new List()
{
new
(
id: null,
epubTypes: null,
text: new SmilText
(
id: null,
src: "chapter1.html#paragraph1"
),
audio: new SmilAudio
(
id: null,
src: "audio.mp3",
clipBegin: "0s",
clipEnd: "10s"
)
)
}
)
);
private static XNamespace SmilXmlNamespace => "http://www.w3.org/ns/SMIL";
private static Smil FullSmil =>
new
(
id: "smil",
version: SmilVersion.SMIL_3,
epubPrefix: "test: http://example.com/test/spec/",
head: new SmilHead
(
metadata: new SmilMetadata
(
items: new List()
{
new(SmilXmlNamespace + "item1", "value1"),
new(SmilXmlNamespace + "item2", "value2")
}
)
),
body: new SmilBody
(
id: "body",
epubTypes: new List()
{
Epub3StructuralSemanticsProperty.BODYMATTER
},
epubTextRef: "chapter1.html",
seqs: new List()
{
new
(
id: "seq1",
epubTypes: new List()
{
Epub3StructuralSemanticsProperty.CHAPTER
},
epubTextRef: "chapter1.html#section1",
seqs: new List()
{
new
(
id: "seq2",
epubTypes: null,
epubTextRef: "chapter1.html#figure1",
seqs: new List(),
pars: new List()
{
new
(
id: "par3",
epubTypes: new List()
{
Epub3StructuralSemanticsProperty.FIGURE
},
text: new SmilText
(
id: "text3",
src: "chapter1.html#photo"
),
audio: new SmilAudio
(
id: "audio3",
src: "audio.mp3",
clipBegin: "0:24:18.123",
clipEnd: "0:24:28.764"
)
),
new
(
id: "par4",
epubTypes: new List()
{
Epub3StructuralSemanticsProperty.TITLE
},
text: new SmilText
(
id: "text4",
src: "chapter1.html#caption"
),
audio: new SmilAudio
(
id: "audio4",
src: "audio.mp3",
clipBegin: "0:24:28.764",
clipEnd: "0:24:50.010"
)
)
}
)
},
pars: new List()
{
new
(
id: "par1",
epubTypes: null,
text: new SmilText
(
id: null,
src: "chapter1.html#paragraph1"
),
audio: new SmilAudio
(
id: null,
src: "audio.mp3",
clipBegin: "0:23:34.221",
clipEnd: "0:23:59.003"
)
),
new
(
id: "par2",
epubTypes: null,
text: new SmilText
(
id: null,
src: "chapter1.html#paragraph2"
),
audio: new SmilAudio
(
id: null,
src: "audio.mp3",
clipBegin: "0:23:59.003",
clipEnd: "0:24:15.000"
)
)
}
)
},
pars: new List()
{
}
)
);
private static Smil MinimalSmilWithEmptyHead =>
new
(
id: null,
version: SmilVersion.SMIL_3,
epubPrefix: null,
head: new SmilHead(null),
body: new SmilBody
(
id: null,
epubTypes: null,
epubTextRef: null,
seqs: new List(),
pars: new List()
{
new
(
id: null,
epubTypes: null,
text: new SmilText
(
id: null,
src: "chapter1.html#paragraph1"
),
audio: new SmilAudio
(
id: null,
src: "audio.mp3",
clipBegin: "0s",
clipEnd: "10s"
)
)
}
)
);
[Fact(DisplayName = "Constructing a SmilReader instance with a non-null epubReaderOptions parameter should succeed")]
public void ConstructorWithNonNullEpubReaderOptionsTest()
{
_ = new SmilReader(new EpubReaderOptions());
}
[Fact(DisplayName = "Constructing a SmilReader instance with a null epubReaderOptions parameter should succeed")]
public void ConstructorWithNullEpubReaderOptionsTest()
{
_ = new SmilReader(null);
}
[Fact(DisplayName = "Reading a minimal SMIL file should succeed")]
public async Task ReadSmilAsyncWithMinimalSmilFileTest()
{
await TestSuccessfulReadOperation(MINIMAL_SMIL_FILE, MinimalSmil);
}
[Fact(DisplayName = "Reading a full SMIL file should succeed")]
public async Task ReadSmilAsyncWithFullSmilFileTest()
{
await TestSuccessfulReadOperation(FULL_SMIL_FILE, FullSmil);
}
[Fact(DisplayName = "Reading all SMIL documents in a EPUB package should succeed")]
public async Task ReadAllSmilDocumentsAsyncTest()
{
TestZipFile testZipFile = new();
string chapter1SmilFileName = "chapter1.smil";
string chapter2SmilFileName = "chapter2.smil";
testZipFile.AddEntry($"{CONTENT_DIRECTORY_PATH}/{chapter1SmilFileName}", MINIMAL_SMIL_FILE);
testZipFile.AddEntry($"{CONTENT_DIRECTORY_PATH}/{chapter2SmilFileName}", FULL_SMIL_FILE);
EpubPackage testEpubPackage = TestEpubPackages.CreateMinimalTestEpubPackage();
testEpubPackage.Manifest.Items.Add(new EpubManifestItem("smil1", chapter1SmilFileName, "application/smil+xml"));
testEpubPackage.Manifest.Items.Add(new EpubManifestItem("smil2", chapter2SmilFileName, "application/smil+xml"));
SmilReader smilReader = new();
List expectedSmils = new() { MinimalSmil, FullSmil };
List actualSmils = await smilReader.ReadAllSmilDocumentsAsync(testZipFile, CONTENT_DIRECTORY_PATH, testEpubPackage);
SmilComparers.CompareSmilLists(expectedSmils, actualSmils);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if EPUB file is missing the specified SMIL file")]
public async Task ReadSmilAsyncWithoutSmilFileTest()
{
TestZipFile testZipFile = new();
SmilReader smilReader = new();
await Assert.ThrowsAsync(() => smilReader.ReadSmilAsync(testZipFile, SMIL_FILE_PATH));
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the SMIL file is larger than 2 GB")]
public async Task ReadSmilAsyncWithLargeSmilFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(SMIL_FILE_PATH, new Test4GbZipFileEntry());
SmilReader smilReader = new();
await Assert.ThrowsAsync(() => smilReader.ReadSmilAsync(testZipFile, SMIL_FILE_PATH));
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the SMIL file has no 'smil' XML element")]
public async Task ReadSmilAsyncWithoutSmilElementTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_SMIL_ELEMENT);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the SMIL version in the file is not 3.0")]
public async Task ReadSmilAsyncWithWrongSmilVersionTest()
{
await TestFailingReadOperation(SMIL_FILE_WITH_WRONG_SMIL_VERSION);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'smil' XML element has no 'body' element")]
public async Task ReadSmilAsyncWithoutBodyElementTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_BODY_ELEMENT);
}
[Fact(DisplayName = "Non-metadata XML elements in the 'head' element should be ignored")]
public async Task ReadSmilAsyncWithNonMetadataElementsInHeadTest()
{
await TestSuccessfulReadOperation(MINIMAL_SMIL_FILE_WITH_NON_METADATA_ELEMENT_IN_HEAD, MinimalSmilWithEmptyHead);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'body' XML element has neither 'seq' nor 'par' elements")]
public async Task ReadSmilAsyncWithoutSeqAndParElementsInBodyTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_BODY);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'seq' XML element has neither 'seq' nor 'par' elements")]
public async Task ReadSmilAsyncWithoutSeqAndParElementsInSeqTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_SEQ);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'par' XML element has no 'text' element")]
public async Task ReadSmilAsyncWithoutTextElementInParTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_TEXT_ELEMENT_IN_PAR);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'text' XML element has no 'src' attribute")]
public async Task ReadSmilAsyncWithoutTextSrcAttributeTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_TEXT_SRC_ATTRIBUTE);
}
[Fact(DisplayName = "ReadSmilAsync should throw EpubSmilException if the 'audio' XML element has no 'src' attribute")]
public async Task ReadSmilAsyncWithoutAudioSrcAttributeTest()
{
await TestFailingReadOperation(SMIL_FILE_WITHOUT_AUDIO_SRC_ATTRIBUTE);
}
private static async Task TestSuccessfulReadOperation(string smilFileContent, Smil expectedSmil)
{
TestZipFile testZipFile = CreateTestZipFileWithSmilFile(smilFileContent);
SmilReader smilReader = new();
Smil actualSmil = await smilReader.ReadSmilAsync(testZipFile, SMIL_FILE_PATH);
SmilComparers.CompareSmils(expectedSmil, actualSmil);
}
private static async Task TestFailingReadOperation(string smilFileContent)
{
TestZipFile testZipFile = CreateTestZipFileWithSmilFile(smilFileContent);
SmilReader smilReader = new();
await Assert.ThrowsAsync(() => smilReader.ReadSmilAsync(testZipFile, SMIL_FILE_PATH));
}
private static TestZipFile CreateTestZipFileWithSmilFile(string smilFileContent)
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(SMIL_FILE_PATH, new TestZipFileEntry(smilFileContent));
return testZipFile;
}
}
}