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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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 = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body>
                    <par>
                        <text src="chapter1.html#paragraph1" />
                        <audio src="audio.mp3" clipBegin="0s" clipEnd="10s" />
                    </par>
                </body>
            </smil>
            """;

        private const string FULL_SMIL_FILE = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" xmlns:epub="http://www.idpf.org/2007/ops" id="smil" version="3.0" epub:prefix="test: http://example.com/test/spec/">
                <head>
                    <metadata>
                        <item1>value1</item1>
                        <item2>value2</item2>
                    </metadata>
                </head>
                <body id="body" epub:type="bodymatter" epub:textref="chapter1.html">
                    <seq id="seq1" epub:type="chapter" epub:textref="chapter1.html#section1">
                        <seq id="seq2" epub:textref="chapter1.html#figure1">
                            <par id="par3" epub:type="figure">
                                <text id="text3" src="chapter1.html#photo" />
                                <audio id="audio3" src="audio.mp3" clipBegin="0:24:18.123" clipEnd="0:24:28.764" />
                            </par>
                            <par id="par4" epub:type="title">
                                <text id="text4" src="chapter1.html#caption" />
                                <audio id="audio4" src="audio.mp3" clipBegin="0:24:28.764" clipEnd="0:24:50.010" />
                            </par>
                        </seq>
                        <par id="par1">
                            <text src="chapter1.html#paragraph1" />
                            <audio src="audio.mp3" clipBegin="0:23:34.221" clipEnd="0:23:59.003" />
                        </par>
                        <par id="par2">
                            <text src="chapter1.html#paragraph2" />
                            <audio src="audio.mp3" clipBegin="0:23:59.003" clipEnd="0:24:15.000" />
                        </par>
                    </seq>
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_SMIL_ELEMENT = """
            <?xml version='1.0' encoding='utf-8'?>
            <test />
            """;

        private const string SMIL_FILE_WITH_WRONG_SMIL_VERSION = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="2.0">
                <body>
                    <par>
                        <text src="chapter1.html#paragraph1" />
                        <audio src="audio.mp3" clipBegin="0s" clipEnd="10s" />
                    </par>
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_BODY_ELEMENT = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <test />
            </smil>
            """;

        private const string MINIMAL_SMIL_FILE_WITH_NON_METADATA_ELEMENT_IN_HEAD = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <head>
                    <test1 />
                    <test2 />
                </head>
                <body>
                    <par>
                        <text src="chapter1.html#paragraph1" />
                        <audio src="audio.mp3" clipBegin="0s" clipEnd="10s" />
                    </par>
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_BODY = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body />
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_SEQ_AND_PAR_ELEMENTS_IN_SEQ = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body>
                    <seq />
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_TEXT_ELEMENT_IN_PAR = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body>
                    <par />
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_TEXT_SRC_ATTRIBUTE = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body>
                    <par>
                        <text />
                    </par>
                </body>
            </smil>
            """;

        private const string SMIL_FILE_WITHOUT_AUDIO_SRC_ATTRIBUTE = """
            <?xml version='1.0' encoding='utf-8'?>
            <smil xmlns="http://www.w3.org/ns/SMIL" version="3.0">
                <body>
                    <par>
                        <text src="chapter1.html#paragraph1" />
                        <audio />
                    </par>
                </body>
            </smil>
            """;

        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<SmilSeq>(),
                    pars: new List<SmilPar>()
                    {
                        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<XElement>()
                        {
                            new(SmilXmlNamespace + "item1", "value1"),
                            new(SmilXmlNamespace + "item2", "value2")
                        }
                    )
                ),
                body: new SmilBody
                (
                    id: "body",
                    epubTypes: new List<Epub3StructuralSemanticsProperty>()
                    {
                        Epub3StructuralSemanticsProperty.BODYMATTER
                    },
                    epubTextRef: "chapter1.html",
                    seqs: new List<SmilSeq>()
                    {
                        new
                        (
                            id: "seq1",
                            epubTypes: new List<Epub3StructuralSemanticsProperty>()
                            {
                                Epub3StructuralSemanticsProperty.CHAPTER
                            },
                            epubTextRef: "chapter1.html#section1",
                            seqs: new List<SmilSeq>()
                            {
                                new
                                (
                                    id: "seq2",
                                    epubTypes: null,
                                    epubTextRef: "chapter1.html#figure1",
                                    seqs: new List<SmilSeq>(),
                                    pars: new List<SmilPar>()
                                    {
                                        new
                                        (
                                            id: "par3",
                                            epubTypes: new List<Epub3StructuralSemanticsProperty>()
                                            {
                                                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>()
                                            {
                                                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<SmilPar>()
                            {
                                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<SmilPar>()
                    {
                    }
                )
            );

        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<SmilSeq>(),
                    pars: new List<SmilPar>()
                    {
                        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<Smil> expectedSmils = new() { MinimalSmil, FullSmil };
            List<Smil> 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<EpubSmilException>(() => 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<EpubSmilException>(() => 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<EpubSmilException>(() => 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;
        }
    }
}