5f7b09f2bdac5d513ad09332886a8bf35975a116
[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, 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 for (index in books) {
29     (function() {
30         var i = index; // closure
31     
32         asyncTest("Load preview for " + books[index].identifier, function() {
33             expect(2);
34     
35             var book = books[i];    
36             var identifier = book.identifier;            
37             
38             var pageURI = previewURL(identifier, book.bookId, 'preview');
39             var img = new Image();
40             $(img).bind( 'load error', function(eventObj) {
41                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
42                 equals(this.width, book.previewWidth, 'Preview width');
43                 start();
44             })
45             .attr('src', pageURI);
46             
47             img = null;
48         });
49         
50         asyncTest("Load cover for " + books[index].identifier, function() {
51             expect(2);
52     
53             var book = books[i];    
54             var identifier = book.identifier;
55             
56             var pageURI = previewURL(identifier, book.bookId, 'cover');
57             var img = new Image();
58             $(img).bind( 'load error', function(eventObj) {
59                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
60                 equals(this.width, book.coverWidth, 'Cover width');
61                 start();
62             })
63             .attr('src', pageURI);
64             
65             img = null;
66         });
67         
68         asyncTest("Load title for " + books[index].identifier, function() {
69             expect(2);
70             
71             var book = books[i];    
72             var identifier = book.identifier;
73             
74             var pageURI = previewURL(identifier, book.bookId, 'title');
75             var img = new Image();
76             $(img).bind( 'load error', function(eventObj) {
77                 equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
78                 equals(this.width, book.titleWidth, 'Title image width');
79                 start();
80             })
81             .attr('src', pageURI);
82             
83             img = null;
84         });
85     })();
86 }
87
88 // Multi-book item
89 var identifier = 'SubBookTest';
90 asyncTest("Load title for book without title specified " + identifier, function() {
91     expect(1);
92         
93     var pageURI = previewURL(identifier, identifier, 'title');
94     var img = new Image();
95     $(img).bind( 'load error', function(eventObj) {
96         equals(eventObj.type, 'error', 'Load image (' + pageURI + '). Event handler called');
97         start();
98     })
99     .attr('src', pageURI);
100     
101     img = null;
102 });
103
104 var subPrefix = 'subdir/subsubdir/book3/Rfp008011ResponseInternetArchive-without-resume';
105 asyncTest("Load preview for book in sub-dir " + identifier + '/' + subPrefix, function() {
106     expect(2);
107         
108     var pageURI = previewURL(identifier, subPrefix, 'title');
109     var img = new Image();
110     $(img).bind( 'load error', function(eventObj) {
111         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
112         equals(this.width, 5100, 'Preview image width');
113         start();
114     })
115     .attr('src', pageURI);
116     
117     img = null;
118 });