File size: 9,715 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Schema;
using VersOne.Epub.Test.Unit.Mocks;

namespace VersOne.Epub.Test.Unit.Readers
{
    public class SpineReaderTests
    {
        [Fact(DisplayName = "Getting reading order for a minimal EPUB spine should succeed")]
        public void GetReadingOrderForMinimalSpineTest()
        {
            EpubSchema epubSchema = CreateEpubSchema();
            EpubContentRef epubContentRef = new();
            List<EpubLocalTextContentFileRef> expectedReadingOrder = [];
            List<EpubLocalTextContentFileRef> actualReadingOrder = SpineReader.GetReadingOrder(epubSchema, epubContentRef, new SpineReaderOptions());
            Assert.Equal(expectedReadingOrder, actualReadingOrder);
        }

        [Fact(DisplayName = "Getting reading order for a typical EPUB spine should succeed")]
        public void GetReadingOrderForTypicalSpineTest()
        {
            EpubSchema epubSchema = CreateEpubSchema
            (
                manifest: new EpubManifest
                (
                    items:
                    [
                        new
                        (
                            id: "item-1",
                            href: "chapter1.html",
                            mediaType: "application/xhtml+xml"
                        ),
                        new
                        (
                            id: "item-2",
                            href: "chapter2.html",
                            mediaType: "application/xhtml+xml"
                        )
                    ]
                ),
                spine: new EpubSpine
                (
                    items:
                    [
                        new
                        (
                            idRef: "item-1"
                        ),
                        new
                        (
                            idRef: "item-2"
                        )
                    ]
                )
            );
            EpubLocalTextContentFileRef expectedHtmlFileRef1 = CreateTestHtmlFileRef("chapter1.html");
            EpubLocalTextContentFileRef expectedHtmlFileRef2 = CreateTestHtmlFileRef("chapter2.html");
            List<EpubLocalTextContentFileRef> expectedHtmlLocal =
            [
                expectedHtmlFileRef1,
                expectedHtmlFileRef2
            ];
            EpubContentRef epubContentRef = new
            (
                html: new EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef>(expectedHtmlLocal.AsReadOnly())
            );
            List<EpubLocalTextContentFileRef> expectedReadingOrder =
            [
                expectedHtmlFileRef1,
                expectedHtmlFileRef2
            ];
            List<EpubLocalTextContentFileRef> actualReadingOrder = SpineReader.GetReadingOrder(epubSchema, epubContentRef, new SpineReaderOptions());
            Assert.Equal(expectedReadingOrder, actualReadingOrder);
        }

        [Fact(DisplayName = "GetReadingOrder should throw EpubPackageException if there is no manifest item with ID matching to the ID ref of a spine item and SpineReaderOptions.IgnoreMissingManifestItems is false")]
        public void GetReadingOrderWithMissingManifestItemWithoutIgnoringErrorsTest()
        {
            EpubSchema epubSchema = CreateEpubSchema
            (
                manifest: new EpubManifest
                (
                    items:
                    [
                        new
                        (
                            id: "item-2",
                            href: "chapter2.html",
                            mediaType: "application/xhtml+xml"
                        )
                    ]
                ),
                spine: new EpubSpine
                (
                    items:
                    [
                        new
                        (
                            idRef: "item-1"
                        )
                    ]
                )
            );
            EpubContentRef epubContentRef = new();
            Assert.Throws<EpubPackageException>(() => SpineReader.GetReadingOrder(epubSchema, epubContentRef, new SpineReaderOptions()));
        }

        [Fact(DisplayName = "GetReadingOrder should skip non-existent manifest items if SpineReaderOptions.IgnoreMissingManifestItems is true")]
        public void GetReadingOrderWithMissingManifestItemWithIgnoringErrorsTest()
        {
            EpubSchema epubSchema = CreateEpubSchema
            (
                manifest: new EpubManifest
                (
                    items:
                    [
                        new
                        (
                            id: "item-2",
                            href: "chapter2.html",
                            mediaType: "application/xhtml+xml"
                        )
                    ]
                ),
                spine: new EpubSpine
                (
                    items:
                    [
                        new
                        (
                            idRef: "item-1"
                        )
                    ]
                )
            );
            EpubContentRef epubContentRef = new();
            SpineReaderOptions spineReaderOptions = new()
            {
                IgnoreMissingManifestItems = true
            };
            List<EpubLocalTextContentFileRef> expectedReadingOrder = [];
            List<EpubLocalTextContentFileRef> actualReadingOrder = SpineReader.GetReadingOrder(epubSchema, epubContentRef, spineReaderOptions);
            Assert.Equal(expectedReadingOrder, actualReadingOrder);
        }

        [Fact(DisplayName = "GetReadingOrder should throw EpubPackageException if there is no HTML content file referenced by a manifest item")]
        public void GetReadingOrderWithMissingHtmlContentFileTest()
        {
            EpubSchema epubSchema = CreateEpubSchema
            (
                manifest: new
                (
                    items:
                    [
                        new
                        (
                            id: "item-1",
                            href: "chapter1.html",
                            mediaType: "application/xhtml+xml"
                        )
                    ]
                ),
                spine: new
                (
                    items:
                    [
                        new
                        (
                            idRef: "item-1"
                        )
                    ]
                )
            );
            EpubContentRef epubContentRef = new();
            Assert.Throws<EpubPackageException>(() => SpineReader.GetReadingOrder(epubSchema, epubContentRef, new SpineReaderOptions()));
        }

        [Fact(DisplayName = "GetReadingOrder should throw EpubPackageException if the HTML content file referenced by a spine item is a remote resource")]
        public void GetReadingOrderWithRemoteHtmlContentFileTest()
        {
            string remoteFileHref = "https://example.com/books/123/chapter1.html";
            EpubSchema epubSchema = CreateEpubSchema
            (
                manifest: new
                (
                    items:
                    [
                        new
                        (
                            id: "item-1",
                            href: remoteFileHref,
                            mediaType: "application/xhtml+xml"
                        )
                    ]
                ),
                spine: new
                (
                    items:
                    [
                        new
                        (
                            idRef: "item-1"
                        )
                    ]
                )
            );
            List<EpubRemoteTextContentFileRef> htmlRemote =
            [
                new
                (
                    metadata: new
                    (
                        key: remoteFileHref,
                        contentType: EpubContentType.XHTML_1_1,
                        contentMimeType: "application/xhtml+xml"
                    ),
                    epubContentLoader: new TestEpubContentLoader()
                )
            ];
            EpubContentRef epubContentRef = new
            (
                html: new EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef>(null, htmlRemote.AsReadOnly())
            );
            Assert.Throws<EpubPackageException>(() => SpineReader.GetReadingOrder(epubSchema, epubContentRef, new SpineReaderOptions()));
        }

        private static EpubSchema CreateEpubSchema(EpubManifest? manifest = null, EpubSpine? spine = null)
        {
            return new
            (
                package: new
                (
                    uniqueIdentifier: null,
                    epubVersion: EpubVersion.EPUB_3,
                    metadata: new EpubMetadata(),
                    manifest: manifest ?? new EpubManifest(),
                    spine: spine ?? new EpubSpine(),
                    guide: null
                ),
                epub2Ncx: null,
                epub3NavDocument: null,
                mediaOverlays: null,
                contentDirectoryPath: String.Empty
            );
        }

        private static EpubLocalTextContentFileRef CreateTestHtmlFileRef(string fileName)
        {
            return new(new EpubContentFileRefMetadata(fileName, EpubContentType.XHTML_1_1, "application/xhtml+xml"), fileName, new TestEpubContentLoader());
        }
    }
}