1-bit detection routines for jp2, tiff, jpeg and png
[bookreader.git] / BookReaderIA / test / unit / Images.js
1 // Tests for BookReaderImages.php
2
3 // $$$ TODO -- make the test host configurable/automagic
4
5 module("Images");
6
7 // $$$ set to test host
8 var testHost = 'http://www-mang.archive.org';
9
10 // Returns locator URL for the given id
11 function jsLocateURL(identifier, book) {
12     var bookURL = testHost + '/bookreader/BookReaderJSLocate.php?id=' + identifier;
13     if (book) {
14         bookURL += '&book=' + book;
15     }
16     return bookURL;
17 }
18
19 // Set up dummy BookReader class for JSLocate
20 function BookReader() {
21 };
22
23 BookReader.prototype.init = function() {
24     return true;
25 };
26
27
28 // Test image info - jpeg
29 asyncTest("JSLocate for armageddonafter00couruoft - jpeg", function() {
30     expect(1);
31     $.getScript( jsLocateURL('armageddonafter00couruoft'), function() {
32         equals(br.bookTitle, 'Armageddon and after', 'Title');
33         start();
34     });
35 });
36
37 asyncTest("Image info for jpeg", function() {
38     expect(3);
39     var expected = {"width":1349,"height":2105,"bits":8,"type":"jpeg"};
40     var imageInfoURL = br.getPageURI(8) + '&ext=json&callback=?';
41     
42     $.getJSON(imageInfoURL, function(data) {
43         equals(data != null, true, 'data is not null');
44         if (data != null) {
45             equals(data.width, expected.width, 'Image width');
46             same(data, expected, 'Image info object');
47         }
48         start();
49     });
50 });
51
52
53
54 // Test image info
55 asyncTest("JSLocate for zc-f-c-b-4 - 1-bit jp2", function() {
56     expect(1);
57     $.getScript( jsLocateURL('zc-f-c-b-4', 'concept-of-infection'), function() {
58         equals(br.numLeafs, 13, 'numLeafs');
59         start();
60     });
61 });
62
63 asyncTest("Image info for 1-bit jp2", function() {
64     expect(3);
65     var expected = {"width":3295,"height":2561,"bits":1,"type":"jp2"};
66     var imageInfoURL = br.getPageURI(0) + '&ext=json&callback=?';
67     
68     $.getJSON(imageInfoURL, function(data) {
69         equals(data != null, true, 'data is not null');
70         if (data != null) {
71             equals(data.width, expected.width, 'Image width');
72             same(data, expected, 'Image info object');
73         }
74         start();
75     });
76 });
77
78 /// windwavesatseabr00bige - jp2 zip
79 asyncTest("JSLocate for windwavesatseabr00bige - Scribe jp2.zip book", function() {
80     expect(1);
81     $.getScript( jsLocateURL('windwavesatseabr00bige'), function(data, textStatus) {
82         equals(br.numLeafs, 224, 'JSLocate successful. numLeafs');
83         start();
84     });
85 });
86     
87 test("Image URI for windwavesatseabr00bige page index 5", function() {
88     expect(1);
89     var index = 5;
90     var expectedEnding = "file=windwavesatseabr00bige_jp2/windwavesatseabr00bige_0006.jp2&scale=1&rotate=0";
91     var pageURI = br.getPageURI(index);
92     var reg = new RegExp('file=.*$');
93     var actualEnding = reg.exec(pageURI);
94     equals(actualEnding, expectedEnding, 'URI for page index 5 ends with');
95 });
96
97 asyncTest("Load windwavesatseabr00bige image 5", function() {
98     var pageURI = br.getPageURI(5);
99     var img = new Image();
100     $(img).bind( 'load error', function(eventObj) {
101         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
102         start();
103     })
104     // Actually load the image
105     .attr('src', pageURI);
106 });
107
108
109 /// nybc200109 - 1-bit tiff zip
110 asyncTest("JSLocate for nybc200109 - 1-bit tiff.zip book", function() {
111     expect(1);
112     $.getScript( jsLocateURL('nybc200109'), function() {
113         equals(br.numLeafs,
114                694,
115                'Number of pages');
116         start();
117     });
118 });
119
120 asyncTest("Image info for 1-bit tiff", function() {
121     expect(3);
122     var expected = {"width":5081,"height":6592,"bits":1,"type":"tiff"};
123     var imageInfoURL = br.getPageURI(0) + '&ext=json&callback=?';
124     
125     $.getJSON(imageInfoURL, function(data) {
126         equals(data != null, true, 'data is not null');
127         if (data != null) {
128             equals(data.width, expected.width, 'Image width');
129             same(data, expected, 'Image info object');
130         }
131         start();
132     });
133 });
134
135 asyncTest("Load 1-bit tiff image from zip", function() {
136     expect(2);
137     var pageURI = br.getPageURI(6, 16);
138     var img = new Image();
139     $(img).bind( 'load error', function(eventObj) {
140         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
141         equals(this.width, 318, 'Image width');
142         start();
143     })
144     .attr('src', pageURI);
145 });
146
147
148
149 /// asamoandictiona00pragoog - tiff zip
150 asyncTest("JSLocate for asamoandictiona00pragoog - tiff.zip book", function() {
151     expect(1);
152     $.getScript( jsLocateURL('asamoandictiona00pragoog'), function() {
153         equals(br.bookTitle,
154                'A Samoan dictionary: English and Samoan, and Samoan and English;',
155                'Book title');
156         start();
157     });
158 });
159
160 asyncTest("Image info for 8-bit tiff", function() {
161     expect(3);
162     var expected = {"width":1275,"height":1650,"bits":8,"type":"tiff"};
163     var imageInfoURL = br.getPageURI(0) + '&ext=json&callback=?';
164     
165     $.getJSON(imageInfoURL, function(data) {
166         equals(data != null, true, 'data is not null');
167         if (data != null) {
168             equals(data.width, expected.width, 'Image width');
169             same(data, expected, 'Image info object');
170         }
171         start();
172     });
173 });
174
175 asyncTest("Load tiff image from zip", function() {
176     expect(2);
177     var pageURI = br.getPageURI(23, 8);
178     var img = new Image();
179     $(img).bind( 'load error', function(eventObj) {
180         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
181         equals(this.width, 351, 'Image width');
182         start();
183     })
184     .attr('src', pageURI);
185 });
186
187
188 /// hccapp56191900uoft - jpeg tar
189 asyncTest("JSLocate for hccapp56191900uoft - jpg.tar", function() {
190     expect(1);
191     $.getScript( jsLocateURL('hccapp56191900uoft'), function() {
192         equals(br.numLeafs, 1101, 'Number of pages');
193         start();
194     });
195 });
196
197 asyncTest('Load jpg image from tar file - https://bugs.launchpad.net/bookreader/+bug/323003', function() {
198     expect(2);
199     var pageURI = br.getPageURI(6, 8);
200     var img = new Image();
201     $(img).bind( 'load error', function(eventObj) {
202         equals(eventObj.type, 'load', 'Load image (' + pageURI + '). Event handler called');
203         equals(this.width, 244, 'Image width');
204         start();
205     })
206     .attr('src', pageURI);
207 });
208