File size: 19,266 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 | using VersOne.Epub.Schema;
using VersOne.Epub.Test.Comparers;
using VersOne.Epub.Test.Unit.Mocks;
using VersOne.Epub.Test.Unit.TestData;
using static VersOne.Epub.Test.Unit.TestData.TestEpubData;
namespace VersOne.Epub.Test.Unit.Entities
{
public class EpubBookRefTests
{
private const string CONTENT_DIRECTORY_PATH = "Content";
private const string COVER_FILE_NAME = "image.jpg";
private const string COVER_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{COVER_FILE_NAME}";
private const EpubContentType COVER_FILE_CONTENT_TYPE = EpubContentType.IMAGE_JPEG;
private const string COVER_FILE_CONTENT_MIME_TYPE = "image/jpeg";
private const string HTML_FILE_NAME = "test.html";
private const string HTML_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{HTML_FILE_NAME}";
private const string TEST_ANCHOR_TEXT = "Test anchor";
private static readonly byte[] COVER_FILE_CONTENT = new byte[] { 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46 };
private static List<string> AuthorList =>
new()
{
BOOK_AUTHOR
};
private static EpubSchema Schema => TestEpubSchemas.CreateFullTestEpubSchema();
private static EpubContentRef Content => TestEpubContentRef.CreateFullTestEpubContentRef();
[Fact(DisplayName = "Constructing a EpubBookRef instance with non-null parameters should succeed")]
public void ConstructorWithNonNullParametersTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = new(testZipFile, EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, Schema, Content);
Assert.Equal(testZipFile, epubBookRef.EpubFile);
Assert.Equal(EPUB_FILE_PATH, epubBookRef.FilePath);
Assert.Equal(BOOK_TITLE, epubBookRef.Title);
Assert.Equal(BOOK_AUTHOR, epubBookRef.Author);
Assert.Equal(AuthorList, epubBookRef.AuthorList);
Assert.Equal(BOOK_DESCRIPTION, epubBookRef.Description);
EpubSchemaComparer.CompareEpubSchemas(Schema, epubBookRef.Schema);
EpubContentRefComparer.CompareEpubContentRefs(Content, epubBookRef.Content);
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if epubFile parameter is null")]
public void ConstructorWithNullEpubFileTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubBookRef(null!, EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, Schema, Content));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if title parameter is null")]
public void ConstructorWithNullTitleTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubBookRef(new TestZipFile(), EPUB_FILE_PATH, null!, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, Schema, Content));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if author parameter is null")]
public void ConstructorWithNullAuthorTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubBookRef(new TestZipFile(), EPUB_FILE_PATH, BOOK_TITLE, null!, AuthorList, BOOK_DESCRIPTION, Schema, Content));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if schema parameter is null")]
public void ConstructorWithNullSchemaTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubBookRef(new TestZipFile(), EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, null!, Content));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if content parameter is null")]
public void ConstructorWithNullContentTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubBookRef(new TestZipFile(), EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, Schema, null!));
}
[Fact(DisplayName = "Constructing a EpubBookRef instance with null filePath parameter should succeed")]
public void ConstructorWithNullFilePathTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = new(testZipFile, null, BOOK_TITLE, BOOK_AUTHOR, AuthorList, BOOK_DESCRIPTION, Schema, Content);
Assert.Equal(testZipFile, epubBookRef.EpubFile);
Assert.Null(epubBookRef.FilePath);
Assert.Equal(BOOK_TITLE, epubBookRef.Title);
Assert.Equal(BOOK_AUTHOR, epubBookRef.Author);
Assert.Equal(AuthorList, epubBookRef.AuthorList);
Assert.Equal(BOOK_DESCRIPTION, epubBookRef.Description);
EpubSchemaComparer.CompareEpubSchemas(Schema, epubBookRef.Schema);
EpubContentRefComparer.CompareEpubContentRefs(Content, epubBookRef.Content);
}
[Fact(DisplayName = "Constructing a EpubBookRef instance with null authorList parameter should succeed")]
public void ConstructorWithNullAuthorListTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = new(testZipFile, EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, null, BOOK_DESCRIPTION, Schema, Content);
Assert.Equal(testZipFile, epubBookRef.EpubFile);
Assert.Equal(EPUB_FILE_PATH, epubBookRef.FilePath);
Assert.Equal(BOOK_TITLE, epubBookRef.Title);
Assert.Equal(BOOK_AUTHOR, epubBookRef.Author);
Assert.Equal(new List<string>(), epubBookRef.AuthorList);
Assert.Equal(BOOK_DESCRIPTION, epubBookRef.Description);
EpubSchemaComparer.CompareEpubSchemas(Schema, epubBookRef.Schema);
EpubContentRefComparer.CompareEpubContentRefs(Content, epubBookRef.Content);
}
[Fact(DisplayName = "Constructing a EpubBookRef instance with null description parameter should succeed")]
public void ConstructorWithNullDescriptionTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = new(testZipFile, EPUB_FILE_PATH, BOOK_TITLE, BOOK_AUTHOR, AuthorList, null, Schema, Content);
Assert.Equal(testZipFile, epubBookRef.EpubFile);
Assert.Equal(EPUB_FILE_PATH, epubBookRef.FilePath);
Assert.Equal(BOOK_TITLE, epubBookRef.Title);
Assert.Equal(BOOK_AUTHOR, epubBookRef.Author);
Assert.Equal(AuthorList, epubBookRef.AuthorList);
Assert.Null(epubBookRef.Description);
EpubSchemaComparer.CompareEpubSchemas(Schema, epubBookRef.Schema);
EpubContentRefComparer.CompareEpubContentRefs(Content, epubBookRef.Content);
}
[Fact(DisplayName = "Reading the existing cover synchronously should succeed")]
public void ReadCoverTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithCover(COVER_FILE_CONTENT);
byte[]? coverContent = epubBookRef.ReadCover();
Assert.Equal(COVER_FILE_CONTENT, coverContent);
}
[Fact(DisplayName = "Reading the existing cover asynchronously should succeed")]
public async Task ReadCoverAsyncTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithCover(COVER_FILE_CONTENT);
byte[]? coverContent = await epubBookRef.ReadCoverAsync();
Assert.Equal(COVER_FILE_CONTENT, coverContent);
}
[Fact(DisplayName = "ReadCover should return null if the cover is missing")]
public void ReadCoverWithoutCoverTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = CreateEpubBookRef(testZipFile, EpubVersion.EPUB_3);
byte[]? coverContent = epubBookRef.ReadCover();
Assert.Null(coverContent);
}
[Fact(DisplayName = "ReadCoverAsync should return null if the cover is missing")]
public async Task ReadCoverAsyncWithoutCoverTest()
{
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = CreateEpubBookRef(testZipFile, EpubVersion.EPUB_3);
byte[]? coverContent = await epubBookRef.ReadCoverAsync();
Assert.Null(coverContent);
}
[Fact(DisplayName = "Getting reading order synchronously should succeed")]
public void GetReadingOrderTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithReadingOrder(HTML_FILE_NAME, HTML_FILE_PATH);
List<EpubLocalTextContentFileRef> expectedReadingOrder = new()
{
epubBookRef.Content.Html.GetLocalFileRefByKey(HTML_FILE_NAME)
};
List<EpubLocalTextContentFileRef> actualReadingOrder = epubBookRef.GetReadingOrder();
Assert.Equal(expectedReadingOrder, actualReadingOrder);
}
[Fact(DisplayName = "Getting reading order asynchronously should succeed")]
public async Task GetReadingOrderAsyncTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithReadingOrder(HTML_FILE_NAME, HTML_FILE_PATH);
List<EpubLocalTextContentFileRef> expectedReadingOrder = new()
{
epubBookRef.Content.Html.GetLocalFileRefByKey(HTML_FILE_NAME)
};
List<EpubLocalTextContentFileRef> actualReadingOrder = await epubBookRef.GetReadingOrderAsync();
Assert.Equal(expectedReadingOrder, actualReadingOrder);
}
[Fact(DisplayName = "Getting navigation items synchronously should succeed")]
public void GetNavigationTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithNavigation(HTML_FILE_NAME, HTML_FILE_PATH, TEST_ANCHOR_TEXT);
List<EpubNavigationItemRef> expectedNavigationItems = new()
{
CreateTestNavigationLink(TEST_ANCHOR_TEXT, HTML_FILE_NAME, epubBookRef.Content.Html.GetLocalFileRefByKey(HTML_FILE_NAME))
};
List<EpubNavigationItemRef>? actualNavigationItems = epubBookRef.GetNavigation();
EpubNavigationItemRefComparer.CompareNavigationItemRefLists(expectedNavigationItems, actualNavigationItems);
}
[Fact(DisplayName = "Getting navigation items asynchronously should succeed")]
public async Task GetNavigationAsyncTest()
{
EpubBookRef epubBookRef = CreateEpubBookRefWithNavigation(HTML_FILE_NAME, HTML_FILE_PATH, TEST_ANCHOR_TEXT);
List<EpubNavigationItemRef> expectedNavigationItems = new()
{
CreateTestNavigationLink(TEST_ANCHOR_TEXT, HTML_FILE_NAME, epubBookRef.Content.Html.GetLocalFileRefByKey(HTML_FILE_NAME))
};
List<EpubNavigationItemRef>? actualNavigationItems = await epubBookRef.GetNavigationAsync();
EpubNavigationItemRefComparer.CompareNavigationItemRefLists(expectedNavigationItems, actualNavigationItems);
}
[Fact(DisplayName = "GetNavigation should return null for EPUB 2 books with no navigation")]
public void GetNavigationForEpub2WithNoNavigationTest()
{
EpubBookRef epubBookRef = CreateEpubBookRef(new TestZipFile(), EpubVersion.EPUB_2);
List<EpubNavigationItemRef>? navigationItems = epubBookRef.GetNavigation();
Assert.Null(navigationItems);
}
[Fact(DisplayName = "GetNavigation should return null for EPUB 3 books with no navigation")]
public void GetNavigationForEpub3WithNoNavigationTest()
{
EpubBookRef epubBookRef = CreateEpubBookRef(new TestZipFile(), EpubVersion.EPUB_3);
List<EpubNavigationItemRef>? navigationItems = epubBookRef.GetNavigation();
Assert.Null(navigationItems);
}
[Fact(DisplayName = "GetNavigationAsync should return null for EPUB 2 books with no navigation")]
public async Task GetNavigationAsyncForEpub2WithNoNavigationTest()
{
EpubBookRef epubBookRef = CreateEpubBookRef(new TestZipFile(), EpubVersion.EPUB_2);
List<EpubNavigationItemRef>? navigationItems = await epubBookRef.GetNavigationAsync();
Assert.Null(navigationItems);
}
[Fact(DisplayName = "GetNavigationAsync should return null for EPUB 3 books with no navigation")]
public async Task GetNavigationAsyncForEpub3WithNoNavigationTest()
{
EpubBookRef epubBookRef = CreateEpubBookRef(new TestZipFile(), EpubVersion.EPUB_3);
List<EpubNavigationItemRef>? navigationItems = await epubBookRef.GetNavigationAsync();
Assert.Null(navigationItems);
}
[Fact(DisplayName = "Disposing EpubBookRef with EPUB file should succeed")]
public void DisposeWithEpubFileTest()
{
TestZipFile testZipFile = new();
using EpubBookRef epubBookRef = CreateEpubBookRef(testZipFile, EpubVersion.EPUB_3);
}
private static EpubBookRef CreateEpubBookRefWithCover(byte[] coverFileContent)
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(COVER_FILE_PATH, coverFileContent);
return CreateEpubBookRef
(
epubFile: testZipFile,
epubVersion: EpubVersion.EPUB_3,
content: new EpubContentRef
(
cover: new EpubLocalByteContentFileRef
(
metadata: new EpubContentFileRefMetadata(COVER_FILE_NAME, COVER_FILE_CONTENT_TYPE, COVER_FILE_CONTENT_MIME_TYPE),
filePath: COVER_FILE_PATH,
epubContentLoader: new TestEpubContentLoader(coverFileContent)
)
)
);
}
private static EpubBookRef CreateEpubBookRefWithReadingOrder(string htmlFileName, string htmlFilePath)
{
EpubLocalTextContentFileRef htmlFileRef = CreateTestHtmlFileRef(htmlFileName, htmlFilePath);
List<EpubLocalTextContentFileRef> htmlLocal = new()
{
htmlFileRef
};
return CreateEpubBookRef
(
epubFile: new TestZipFile(),
epubVersion: EpubVersion.EPUB_3,
manifest: new EpubManifest
(
items: new List<EpubManifestItem>()
{
new
(
id: "item-1",
href: htmlFileName,
mediaType: "application/xhtml+xml"
)
}
),
spine: new EpubSpine
(
items: new List<EpubSpineItemRef>()
{
new
(
idRef: "item-1"
)
}
),
content: new EpubContentRef
(
html: new EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef>(htmlLocal.AsReadOnly())
)
);
}
private static EpubBookRef CreateEpubBookRefWithNavigation(string htmlFileName, string htmlFilePath, string anchorText)
{
EpubLocalTextContentFileRef htmlFileRef = CreateTestHtmlFileRef(htmlFileName, htmlFilePath);
EpubLocalTextContentFileRef navigationFileRef = CreateTestHtmlFileRef("toc.html", $"{CONTENT_DIRECTORY_PATH}/toc.html");
List<EpubLocalTextContentFileRef> htmlLocal = new()
{
htmlFileRef
};
return CreateEpubBookRef
(
epubFile: new TestZipFile(),
epubVersion: EpubVersion.EPUB_3,
epub3NavDocument: new Epub3NavDocument
(
filePath: navigationFileRef.FilePath,
navs: new List<Epub3Nav>()
{
new
(
type: Epub3StructuralSemanticsProperty.TOC,
ol: new Epub3NavOl
(
lis: new List<Epub3NavLi>()
{
new
(
anchor: new Epub3NavAnchor
(
text: anchorText,
href: htmlFileName
)
)
}
)
)
}
),
content: new EpubContentRef
(
navigationHtmlFile: navigationFileRef,
html: new EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef>(htmlLocal.AsReadOnly())
)
);
}
private static EpubBookRef CreateEpubBookRef(TestZipFile epubFile, EpubVersion epubVersion, EpubManifest? manifest = null, EpubSpine? spine = null,
Epub3NavDocument? epub3NavDocument = null, EpubContentRef? content = null)
{
return new
(
epubFile: epubFile,
filePath: null,
title: String.Empty,
author: String.Empty,
authorList: null,
description: null,
schema: new EpubSchema
(
package: new EpubPackage
(
uniqueIdentifier: null,
epubVersion: epubVersion,
metadata: new EpubMetadata(),
manifest: manifest ?? new EpubManifest(),
spine: spine ?? new EpubSpine(),
guide: null
),
epub2Ncx: null,
epub3NavDocument: epub3NavDocument,
mediaOverlays: null,
contentDirectoryPath: CONTENT_DIRECTORY_PATH
),
content: content ?? new EpubContentRef()
);
}
private static EpubLocalTextContentFileRef CreateTestHtmlFileRef(string fileName, string filePath)
{
EpubContentFileRefMetadata htmlFileRefMetadata = new(fileName, EpubContentType.XHTML_1_1, "application/xhtml+xml");
return new(htmlFileRefMetadata, filePath, new TestEpubContentLoader());
}
private static EpubNavigationItemRef CreateTestNavigationLink(string title, string htmlFileUrl, EpubLocalTextContentFileRef htmlFileRef)
{
return new
(
type: EpubNavigationItemType.LINK,
title: title,
link: new EpubNavigationItemLink(htmlFileUrl, CONTENT_DIRECTORY_PATH),
htmlContentFileRef: htmlFileRef,
nestedItems: null
);
}
}
}
|