@@ -14,54 +14,125 @@ class CoverServiceTests: XCTestCase {
1414 lazy var cover = UIImage ( contentsOfFile: coverURL. path) !
1515 lazy var cover2 = UIImage ( data: fixtures. data ( at: " cover2.jpg " ) ) !
1616
17- /// `Publication.cover` will use the `CoverService` if there's one .
18- func testCoverHelperUsesCoverService ( ) async {
17+ /// `Publication.cover` will use a custom `CoverService` if provided .
18+ func testCoverHelperUsesCustomCoverService ( ) async {
1919 let publication = makePublication { _ in TestCoverService ( cover: self . cover2) }
2020 let result = await publication. cover ( )
2121 AssertImageEqual ( result, . success( cover2) )
2222 }
2323
24- /// `Publication.cover` will try to fetch the cover from a manifest link with rel `cover`, if
25- /// no `CoverService` is provided.
26- func testCoverHelperFallsBackOnManifest( ) async {
24+ /// `Publication.cover` uses `ResourceCoverService` by default.
25+ func testCoverHelperUsesResourceCoverServiceByDefault( ) async {
2726 let publication = makePublication ( )
2827 let result = await publication. cover ( )
2928 AssertImageEqual ( result, . success( cover) )
3029 }
3130
32- /// `Publication.coverFitting` will use the `CoverService` if there's one .
33- func testCoverFittingHelperUsesCoverService ( ) async {
31+ /// `Publication.coverFitting` will use a custom `CoverService` if provided .
32+ func testCoverFittingHelperUsesCustomCoverService ( ) async {
3433 let size = CGSize ( width: 100 , height: 100 )
3534 let publication = makePublication { _ in TestCoverService ( cover: self . cover2) }
3635 let result = await publication. coverFitting ( maxSize: size)
3736 AssertImageEqual ( result, . success( cover2. scaleToFit ( maxSize: size) ) )
3837 }
3938
40- /// `Publication.coverFitting` will try to fetch the cover from a manifest link with rel `cover`, if
41- /// no `CoverService` is provided.
42- func testCoverFittingHelperFallsBackOnManifest( ) async {
39+ /// `Publication.coverFitting` uses `ResourceCoverService` by default.
40+ func testCoverFittingHelperUsesResourceCoverServiceByDefault( ) async {
4341 let size = CGSize ( width: 100 , height: 100 )
4442 let publication = makePublication ( )
4543 let result = await publication. coverFitting ( maxSize: size)
4644 AssertImageEqual ( result, . success( cover. scaleToFit ( maxSize: size) ) )
4745 }
4846
49- private func makePublication( cover: CoverServiceFactory ? = nil ) -> Publication {
50- let coverPath = " cover.jpg "
51- return Publication (
52- manifest: Manifest (
53- metadata: Metadata (
54- title: " title "
47+ /// `ResourceCoverService` uses the first bitmap reading order item when no explicit `.cover`
48+ /// link is declared.
49+ func testResourceCoverServiceUsesFirstBitmapReadingOrderItem( ) async {
50+ let publication = makePublication (
51+ readingOrder: [
52+ Link ( href: " cover.jpg " , mediaType: . jpeg) ,
53+ Link ( href: " page2.jpg " , mediaType: . jpeg) ,
54+ ] ,
55+ resources: [ ]
56+ )
57+ let result = await publication. cover ( )
58+ AssertImageEqual ( result, . success( cover) )
59+ }
60+
61+ /// `ResourceCoverService` uses the first bitmap alternate of the first reading order item
62+ /// when that item is not a bitmap.
63+ func testResourceCoverServiceUsesFirstReadingOrderBitmapAlternate( ) async {
64+ let publication = makePublication (
65+ readingOrder: [
66+ Link (
67+ href: " chapter1.xhtml " ,
68+ mediaType: . xhtml,
69+ alternates: [
70+ Link ( href: " cover.jpg " , mediaType: . jpeg) ,
71+ ]
5572 ) ,
73+ ] ,
74+ resources: [ ]
75+ )
76+ let result = await publication. cover ( )
77+ AssertImageEqual ( result, . success( cover) )
78+ }
79+
80+ /// `ResourceCoverService` returns nil when no explicit `.cover` link is declared and no bitmap
81+ /// is available.
82+ func testResourceCoverServiceReturnsNilWhenNoBitmapAvailable( ) async {
83+ let publication = makePublication (
84+ readingOrder: [ Link ( href: " chapter1.xhtml " , mediaType: . xhtml) ] ,
85+ resources: [ ]
86+ )
87+ let result = await publication. cover ( )
88+ AssertImageEqual ( result, . success( nil ) )
89+ }
90+
91+ /// `ResourceCoverService` prioritizes explicit `.cover` links over first reading order item.
92+ func testResourceCoverServicePrioritizesExplicitCoverLink( ) async {
93+ let publication = Publication (
94+ manifest: Manifest (
95+ metadata: Metadata ( title: " title " ) ,
5696 readingOrder: [
57- Link ( href: " titlepage.xhtml " , rels : [ . cover ] ) ,
97+ Link ( href: " page1.jpg " , mediaType : . jpeg ) ,
5898 ] ,
5999 resources: [
60- Link ( href: coverPath , rels: [ . cover] ) ,
100+ Link ( href: " cover2.jpg " , rels: [ . cover] ) ,
61101 ]
62102 ) ,
63- container: FileContainer ( href: RelativeURL ( path: coverPath) !, file: coverURL) ,
64- servicesBuilder: PublicationServicesBuilder ( cover: cover)
103+ container: CompositeContainer (
104+ SingleResourceContainer (
105+ resource: FileResource ( file: fixtures. url ( for: " cover.jpg " ) ) ,
106+ at: AnyURL ( string: " page1.jpg " ) !
107+ ) ,
108+ SingleResourceContainer (
109+ resource: FileResource ( file: fixtures. url ( for: " cover2.jpg " ) ) ,
110+ at: AnyURL ( string: " cover2.jpg " ) !
111+ )
112+ )
113+ )
114+ let result = await publication. cover ( )
115+ AssertImageEqual ( result, . success( cover2) )
116+ }
117+
118+ private func makePublication(
119+ readingOrder: [ Link ] = [ ] ,
120+ resources: [ Link ] = [ Link ( href: " cover.jpg " , rels: [ . cover] ) ] ,
121+ cover: CoverServiceFactory ? = nil
122+ ) -> Publication {
123+ var builder = PublicationServicesBuilder ( )
124+ if let cover { builder. setCoverServiceFactory ( cover) }
125+ return Publication (
126+ manifest: Manifest (
127+ metadata: Metadata ( title: " title " ) ,
128+ readingOrder: readingOrder,
129+ resources: resources
130+ ) ,
131+ container: SingleResourceContainer (
132+ resource: FileResource ( file: coverURL) ,
133+ at: AnyURL ( string: " cover.jpg " ) !
134+ ) ,
135+ servicesBuilder: builder
65136 )
66137 }
67138}
0 commit comments