Update unit tests to autodetect testing host
[bookreader.git] / BookReaderIA / test / unit / Common.js
1 // Defines common variables for testing
2
3 // What host to use for testing
4 function testHost() {
5     // Autodetect if running from a home directory, or use live site
6     var user = null;
7     var patterns = [ new RegExp('.*?/~(.*?)/'), new RegExp('www-(.*?)\\.') ]
8     for (index in patterns) {
9         var match = patterns[index].exec(document.location.href);
10         if (match) {
11             user = match[1];
12             break;
13         }
14     }
15     
16     if (user) {
17         return 'http://www-' + user + '.archive.org';
18     }
19     
20     return 'http://www.archive.org'; // live site
21 }
22
23 var common = {
24     testHost: testHost(),
25 }
26
27 // Set up dummy BookReader class for JSLocate
28 function BookReader() {
29 };
30
31 BookReader.prototype.init = function() {
32     return true;
33 };
34
35
36 // Returns locator URL for the given id
37 function jsLocateURL(identifier, book) {
38     var bookURL = common.testHost + '/bookreader/BookReaderJSLocate.php?id=' + identifier;
39     if (book) {
40         bookURL += '&book=' + book;
41     }
42     return bookURL;
43 }
44
45 // Page should be in ['cover','title','preview']
46 function previewURL(identifier, subPrefix, page) {
47     var bookPrefix = subPrefix || identifier;
48     var previewPage = bookPrefix + '_' + page;
49     return imagePermalink(identifier, subPrefix, previewPage);
50 }
51
52 // Page should be e.g. page5.jpg, n4.jpg, cover_t.jpg, n4_r3.jpg
53 function imagePermalink(identifier, subPrefix, page) {
54     var imageURL = common.testHost + '/download/' + identifier;
55     if (subPrefix) {
56         imageURL += '/' + subPrefix;
57     }
58     imageURL += '/page/' + page;
59     return imageURL;
60 }