Closes #14, closes #15. Fix recovery path used when jp2 is requested at less than...
[bookreader.git] / BookReaderIA / test / unit / Preview.js
1 // Tests for BookReaderPreview.php
2
3 // Depends on common.js
4
5 module("Preview");
6
7 function Book(identifier, previewWidth, coverWidth, titleWidth, imageSize, bookId) {
8     this.identifier = identifier;
9     this.previewWidth = previewWidth;
10     this.coverWidth = coverWidth;
11     this.titleWidth = titleWidth;
12     if (bookId === undefined) {
13         bookId = identifier;
14     }
15     this.bookId = bookId;
16 }
17
18 var books = [
19     // Old books using title page as cover (ignoring marked cover)
20     new Book('coloritsapplicat00andriala', 1974, 1974, 1974),
21     new Book('lietuvostsrmoksl50liet', 1887, 1887, 1887),
22     new Book('oldtestamentrevi02slsn', 2019, 2019, 2019),
23     
24     // Protected book with marked cover returned as cover
25     new Book('joyofsoaringtrai00conw', 2571, 2571, 2419)
26     
27 ];
28
29 for (index in books) {
30     (function() {
31         var i = index; // closure
32     
33         asyncTest("Load preview for " + books[index].identifier, function() {
34             expect(2);
35     
36             var book = books[i];    
37             var identifier = book.identifier;            
38             
39             var pageURI = previewURL(identifier, book.bookId, 'preview');
40             var img = new Image();
41             $(img).bind( 'load error', function(eventObj) {
42                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
43                 equals(this.width, book.previewWidth, 'Preview width');
44                 start();
45             })
46             .attr('src', pageURI);
47             
48             img = null;
49         });
50         
51         asyncTest("Load cover for " + books[index].identifier, function() {
52             expect(2);
53     
54             var book = books[i];    
55             var identifier = book.identifier;
56             
57             var pageURI = previewURL(identifier, book.bookId, 'cover');
58             var img = new Image();
59             $(img).bind( 'load error', function(eventObj) {
60                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
61                 equals(this.width, book.coverWidth, 'Cover width');
62                 start();
63             })
64             .attr('src', pageURI);
65             
66             img = null;
67         });
68         
69         asyncTest("Load title for " + books[index].identifier, function() {
70             expect(2);
71             
72             var book = books[i];    
73             var identifier = book.identifier;
74             
75             var pageURI = previewURL(identifier, book.bookId, 'title');
76             var img = new Image();
77             $(img).bind( 'load error', function(eventObj) {
78                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
79                 equals(this.width, book.titleWidth, 'Title image width');
80                 start();
81             })
82             .attr('src', pageURI);
83             
84             img = null;
85         });
86     })();
87 }
88
89 // Multi-book item
90 var identifier = 'SubBookTest';
91 asyncTest("Load title for book without title specified " + identifier, function() {
92     expect(1);
93         
94     var pageURI = previewURL(identifier, identifier, 'title');
95     var img = new Image();
96     $(img).bind( 'load error', function(eventObj) {
97         equals(eventObj.type, 'error', 'Load image (' + pageURI + '). Event handler called');
98         start();
99     })
100     .attr('src', pageURI);
101     
102     img = null;
103 });
104
105 var subPrefix = 'subdir/subsubdir/book3/Rfp008011ResponseInternetArchive-without-resume';
106 asyncTest("Load preview for book in sub-dir " + identifier + '/' + subPrefix, function() {
107     expect(2);
108         
109     var pageURI = previewURL(identifier, subPrefix, 'title');
110     var img = new Image();
111     $(img).bind( 'load error', function(eventObj) {
112         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
113         equals(this.width, 5100, 'Preview image width');
114         start();
115     })
116     .attr('src', pageURI);
117     
118     img = null;
119 });