File size: 22,114 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 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Schema;
using VersOne.Epub.Test.Comparers;
using VersOne.Epub.Test.Unit.Mocks;
namespace VersOne.Epub.Test.Unit.Readers
{
public class Epub3NavDocumentReaderTests
{
private const string CONTENT_DIRECTORY_PATH = "Content";
private const string NAV_FILE_NAME = "toc.html";
private const string NAV_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{NAV_FILE_NAME}";
private const string MINIMAL_NAV_FILE = """
<html xmlns="http://www.w3.org/1999/xhtml">
<body />
</html>
""";
private const string FULL_NAV_FILE = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h1>Table of Contents</h1>
<ol>
<li>
<span title="Test span title" alt="Test span alt">Test span header</span>
<ol>
<li>
<a href="chapter1.html" title="Test anchor title" alt="Test anchor alt">Chapter 1</a>
</li>
</ol>
</li>
<li>
<a epub:type="loi" href="illustrations.html">List of illustrations</a>
</li>
</ol>
</nav>
<nav epub:type="page-list" hidden="">
<h1>Page list</h1>
<ol hidden="">
<li>
<a href="chapter1.html#page-1">1</a>
</li>
</ol>
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H1_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h1>Test header</h1>
<ol />
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H2_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h2>Test header</h2>
<ol />
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H3_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h3>Test header</h3>
<ol />
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H4_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h4>Test header</h4>
<ol />
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H5_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h5>Test header</h5>
<ol />
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITH_H6_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<h6>Test header</h6>
<ol />
</nav>
</body>
</html>
""";
private const string NAV_FILE_WITHOUT_HTML_ELEMENT = """
<test />
""";
private const string NAV_FILE_WITHOUT_BODY_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml">
<test />
</html>
""";
private const string NAV_FILE_WITHOUT_TOP_OL_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc" />
</body>
</html>
""";
private const string NAV_FILE_WITH_EMPTY_LI_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<ol>
<li />
</ol>
</nav>
</body>
</html>
""";
private const string NAV_FILE_WITH_ESCAPED_HREF_IN_A_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav epub:type="toc">
<ol>
<li>
<a href="chapter%31.html">Chapter 1</a>
</li>
</ol>
</nav>
</body>
</html>
""";
private const string NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<div>
<nav epub:type="toc">
<h1>Test header</h1>
<ol />
</nav>
</div>
</body>
</html>
""";
private const string NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<ol>
<li>
<a href="chapter1.html">Chapter 1</a>
</li>
</ol>
</nav>
</body>
</html>
""";
private const string MINIMAL_NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav />
</body>
</html>
""";
private static EpubPackage MinimalEpubPackageWithNav =>
new
(
uniqueIdentifier: null,
epubVersion: EpubVersion.EPUB_3,
metadata: new EpubMetadata(),
manifest: new EpubManifest
(
items: new List<EpubManifestItem>()
{
new
(
id: "nav",
href: NAV_FILE_NAME,
mediaType: "application/xhtml+xml",
properties: new List<EpubManifestProperty>()
{
EpubManifestProperty.NAV
}
)
}
),
spine: new EpubSpine(),
guide: null
);
private static Epub3NavDocument MinimalEpub3NavDocument =>
new
(
filePath: NAV_FILE_PATH
);
private static Epub3NavDocument FullEpub3NavDocument =>
new
(
filePath: NAV_FILE_PATH,
navs: new List<Epub3Nav>()
{
new
(
type: Epub3StructuralSemanticsProperty.TOC,
isHidden: false,
head: "Table of Contents",
ol: new Epub3NavOl
(
isHidden: false,
lis: new List<Epub3NavLi>()
{
new
(
span: new Epub3NavSpan
(
title: "Test span title",
alt: "Test span alt",
text: "Test span header"
),
childOl: new Epub3NavOl
(
isHidden: false,
lis: new List<Epub3NavLi>()
{
new
(
anchor: new Epub3NavAnchor
(
href: "chapter1.html",
title: "Test anchor title",
alt: "Test anchor alt",
text: "Chapter 1"
)
)
}
)
),
new
(
anchor: new Epub3NavAnchor
(
type: Epub3StructuralSemanticsProperty.LOI,
href: "illustrations.html",
text: "List of illustrations"
)
)
}
)
),
new
(
type: Epub3StructuralSemanticsProperty.PAGE_LIST,
isHidden: true,
head: "Page list",
ol: new Epub3NavOl
(
isHidden: true,
lis: new List<Epub3NavLi>()
{
new
(
anchor: new Epub3NavAnchor
(
href: "chapter1.html#page-1",
text: "1"
)
)
}
)
)
}
);
private static Epub3NavDocument MinimalEpub3NavDocumentWithHeader =>
new
(
filePath: NAV_FILE_PATH,
navs: new List<Epub3Nav>()
{
new
(
type: Epub3StructuralSemanticsProperty.TOC,
isHidden: false,
head: "Test header",
ol: new Epub3NavOl()
)
}
);
public static IEnumerable<object[]> ReadEpub3NavDocumentAsyncWithMinimalNavFileWithHeaderTestData
{
get
{
yield return new object[] { MINIMAL_NAV_FILE_WITH_H1_HEADER, MinimalEpub3NavDocumentWithHeader };
yield return new object[] { MINIMAL_NAV_FILE_WITH_H2_HEADER, MinimalEpub3NavDocumentWithHeader };
yield return new object[] { MINIMAL_NAV_FILE_WITH_H3_HEADER, MinimalEpub3NavDocumentWithHeader };
yield return new object[] { MINIMAL_NAV_FILE_WITH_H4_HEADER, MinimalEpub3NavDocumentWithHeader };
yield return new object[] { MINIMAL_NAV_FILE_WITH_H5_HEADER, MinimalEpub3NavDocumentWithHeader };
yield return new object[] { MINIMAL_NAV_FILE_WITH_H6_HEADER, MinimalEpub3NavDocumentWithHeader };
}
}
[Fact(DisplayName = "Constructing a Epub3NavDocumentReader instance with a non-null epubReaderOptions parameter should succeed")]
public void ConstructorWithNonNullEpubReaderOptionsTest()
{
_ = new Epub3NavDocumentReader(new EpubReaderOptions());
}
[Fact(DisplayName = "Constructing a Epub3NavDocumentReader instance with a null epubReaderOptions parameter should succeed")]
public void ConstructorWithNullEpubReaderOptionsTest()
{
_ = new Epub3NavDocumentReader(null);
}
[Fact(DisplayName = "Reading a minimal NAV file should succeed")]
public async Task ReadEpub3NavDocumentAsyncWithMinimalNavFileTest()
{
await TestSuccessfulReadOperation(MINIMAL_NAV_FILE, MinimalEpub3NavDocument);
}
[Fact(DisplayName = "Reading a full NAV file should succeed")]
public async Task ReadEpub3NavDocumentAsyncWithFullNavFileTest()
{
await TestSuccessfulReadOperation(FULL_NAV_FILE, FullEpub3NavDocument);
}
[Theory(DisplayName = "Reading minimal NAV packages with h1-h6 headers should succeed")]
[MemberData(nameof(ReadEpub3NavDocumentAsyncWithMinimalNavFileWithHeaderTestData))]
public async Task ReadEpub3NavDocumentAsyncWithMinimalNavFileWithHeaderTest(string navFileContent, Epub3NavDocument expectedEpub3NavDocument)
{
await TestSuccessfulReadOperation(navFileContent, expectedEpub3NavDocument);
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if EpubPackage is missing the NAV item and EpubVersion is not EPUB_2")]
public async Task ReadEpub3NavDocumentAsyncForEpub3WithoutNavManifestItemTest()
{
TestZipFile testZipFile = new();
EpubPackage epubPackage = new
(
uniqueIdentifier: null,
epubVersion: EpubVersion.EPUB_3,
metadata: new EpubMetadata(),
manifest: new EpubManifest
(
items: new List<EpubManifestItem>()
{
new
(
id: "test",
href: "test.html",
mediaType: "application/xhtml+xml"
)
}
),
spine: new EpubSpine(),
guide: null
);
Epub3NavDocumentReader epub3NavDocumentReader = new();
await Assert.ThrowsAsync<Epub3NavException>(() => epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, epubPackage));
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should return null if EpubPackage is missing the NAV item and EpubVersion is EPUB_2")]
public async Task ReadEpub3NavDocumentAsyncForEpub2WithoutNavManifestItemTest()
{
TestZipFile testZipFile = new();
EpubPackage epubPackage = new
(
uniqueIdentifier: null,
epubVersion: EpubVersion.EPUB_2,
metadata: new EpubMetadata(),
manifest: new EpubManifest(),
spine: new EpubSpine(),
guide: null
);
Epub3NavDocumentReader epub3NavDocumentReader = new();
Epub3NavDocument? epub3NavDocument = await epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, epubPackage);
Assert.Null(epub3NavDocument);
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if EPUB file is missing the NAV file specified in the EpubPackage")]
public async Task ReadEpub3NavDocumentAsyncWithoutNavFileTest()
{
TestZipFile testZipFile = new();
Epub3NavDocumentReader epub3NavDocumentReader = new();
await Assert.ThrowsAsync<Epub3NavException>(() => epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, MinimalEpubPackageWithNav));
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if the NCX file is larger than 2 GB")]
public async Task ReadEpub3NavDocumentAsyncWithLargeNavFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(NAV_FILE_PATH, new Test4GbZipFileEntry());
Epub3NavDocumentReader epub3NavDocumentReader = new();
await Assert.ThrowsAsync<Epub3NavException>(() => epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, MinimalEpubPackageWithNav));
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if the NAV file is missing the 'html' XML element")]
public async Task ReadEpub3NavDocumentAsyncWithoutHtmlElement()
{
await TestFailingReadOperation(NAV_FILE_WITHOUT_HTML_ELEMENT);
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if the NAV file is missing the 'body' XML element")]
public async Task ReadEpub3NavDocumentAsyncWithoutBodyElement()
{
await TestFailingReadOperation(NAV_FILE_WITHOUT_BODY_ELEMENT);
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if the NAV file is missing the top 'ol' XML element")]
public async Task ReadEpub3NavDocumentAsyncWithoutTopOlElement()
{
await TestFailingReadOperation(NAV_FILE_WITHOUT_TOP_OL_ELEMENT);
}
[Fact(DisplayName = "ReadEpub3NavDocumentAsync should throw Epub3NavException if the NAV file has an empty 'li' XML element")]
public async Task ReadEpub3NavDocumentAsyncWithEmptyLiElement()
{
await TestFailingReadOperation(NAV_FILE_WITH_EMPTY_LI_ELEMENT);
}
[Fact(DisplayName = "Reading a NAV file with a URI-escaped 'href' attribute in an 'a' XML element should succeed")]
public async Task ReadEpub3NavDocumentAsyncWithEscapedAHrefTest()
{
Epub3NavDocument expectedEpub3NavDocument = new
(
filePath: NAV_FILE_PATH,
navs: new List<Epub3Nav>()
{
new
(
type: Epub3StructuralSemanticsProperty.TOC,
ol: new Epub3NavOl
(
lis: new List<Epub3NavLi>()
{
new
(
anchor: new Epub3NavAnchor
(
href: "chapter1.html",
text: "Chapter 1"
)
)
}
)
)
}
);
await TestSuccessfulReadOperation(NAV_FILE_WITH_ESCAPED_HREF_IN_A_ELEMENT, expectedEpub3NavDocument);
}
[Fact(DisplayName = "Reading a NAV file with non-top-level 'nav' element should succeed")]
public async Task ReadEpub3NavDocumentAsyncWithNonTopLevelNavElementTest()
{
await TestSuccessfulReadOperation(NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT, MinimalEpub3NavDocumentWithHeader);
}
[Fact(DisplayName = "'nav' elements without 'type' attribute should be ignored")]
public async Task ReadEpub3NavDocumentAsyncWithNavElementWithoutTypeTest()
{
await TestSuccessfulReadOperation(NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT, MinimalEpub3NavDocument);
}
[Fact(DisplayName = "'nav' elements without 'type' attribute that don't conform to EPUB standard should not throw an exception")]
public async Task ReadEpub3NavDocumentAsyncWithNonEpubNavElementWithoutTypeTest()
{
await TestSuccessfulReadOperation(MINIMAL_NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT, MinimalEpub3NavDocument);
}
private static async Task TestSuccessfulReadOperation(string navFileContent, Epub3NavDocument expectedEpub3NavDocument, EpubReaderOptions? epubReaderOptions = null)
{
TestZipFile testZipFile = CreateTestZipFileWithNavFile(navFileContent);
Epub3NavDocumentReader epub3NavDocumentReader = new(epubReaderOptions ?? new EpubReaderOptions());
Epub3NavDocument? actualEpub3NavDocument = await epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, MinimalEpubPackageWithNav);
Epub3NavDocumentComparer.CompareEpub3NavDocuments(expectedEpub3NavDocument, actualEpub3NavDocument);
}
private static async Task TestFailingReadOperation(string navFileContent)
{
TestZipFile testZipFile = CreateTestZipFileWithNavFile(navFileContent);
Epub3NavDocumentReader epub3NavDocumentReader = new();
await Assert.ThrowsAsync<Epub3NavException>(() => epub3NavDocumentReader.ReadEpub3NavDocumentAsync(testZipFile, CONTENT_DIRECTORY_PATH, MinimalEpubPackageWithNav));
}
private static TestZipFile CreateTestZipFileWithNavFile(string navFileContent)
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(NAV_FILE_PATH, new TestZipFileEntry(navFileContent));
return testZipFile;
}
}
}
|