forked from chrisben/imgcache.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
439 lines (404 loc) · 12.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>imgcache.js - Tests</title>
<script src="examples/jquery-1.6.4.min.js"></script>
<script src="js/imgcache.js"></script>
<!-- only for cordova (replace by phonegap.js for phonegap) - ignore on desktop browser -->
<script src="cordova.js"></script>
<script>
var LOG_LEVEL_INFO = 1;
var LOG_LEVEL_WARNING = 2;
var LOG_LEVEL_ERROR = 3;
var logger = function (str, level) {
var levelClass = '';
if (level == LOG_LEVEL_INFO) { levelClass = 'info' } ;
if (level == LOG_LEVEL_WARNING) { levelClass = 'warn' };
if (level == LOG_LEVEL_ERROR) { levelClass = 'error' };
$('#log').append($('<li></li>').addClass(levelClass).text(str));
};
var startTest = function () {
console.log('starting test');
var countTotalMethods = 0;
var testableMethods = [];
for (var m in ImgCache) {
if (typeof ImgCache[m] === 'function') {
countTotalMethods++;
testableMethods.push(m);
}
}
var testedMethods = {};
ImgCache.options.debug = true;
ImgCache.options.customLogger = logger;
ImgCache.options.cacheClearSize = 10;
var default_options = ImgCache.options;
var test_suites_options = [
{
usePersistentCache: true
},
{
usePersistentCache: false
},
{
useDataURI: true
},
];
$('#version').text('imgcache.js version ' + ImgCache.version);
var $test_img, $test_div;
var getBackgroundImageUrl = function ($div) {
var backgroundImageProperty = $div.css('background-image');
if (!backgroundImageProperty) {
return;
}
var regexp = /\((.+)\)/;
return regexp.exec(backgroundImageProperty)[1];
};
var getAppropriateElement = function (element) {
if (ImgCache.jQuery) {
return element;
}
return element[0];
};
// simple test bench : test function should call ok/nok callbacks then it carries on with next test
// name: exact name of the method to Test
// test: code that will get executed - can expect ok or error
var tests_list = [
{
name: 'init',
test: function (ok, nok) {
ImgCache.init(ok, nok);
}
},
{
name: 'clearCache',
test: function (ok, nok) {
ImgCache.clearCache(ok, nok);
}
},
{
name: 'getCurrentSize',
test: function (ok, nok) {
if (ImgCache.getCurrentSize() === 0) {
ok();
} else {
nok();
}
}
},
{
name: 'isCached',
test: function (ok, nok) {
// expected result: failure
ImgCache.isCached($test_img.attr('src'), function (src, res) { if (res) { nok(); } else { ok(); } });
}
},
{
name: 'getCachedFile',
test: function (ok, nok) {
// expected result: failure
ImgCache.getCachedFile($test_img.attr('src'), function (src, file_entry) { if (file_entry) { nok(); } else { ok(); } });
}
},
{
name: 'cacheFile',
test: function (ok, nok) {
ImgCache.cacheFile($test_img.attr('src'), ok, nok);
}
},
{
name: 'cacheBackground',
test: function (ok, nok) {
ImgCache.cacheBackground(getAppropriateElement($test_div), ok, nok);
}
},
{
name: 'isCached',
test: function (ok, nok) {
var img_src = getBackgroundImageUrl($test_div);
if (img_src) {
ImgCache.isCached(img_src, function (src, res) { if (res) { ok(); } else { nok(); } });
} else {
nok();
}
}
},
{
name: 'getCachedFile',
test: function (ok, nok) {
var img_src = getBackgroundImageUrl($test_div);
if (img_src) {
ImgCache.getCachedFile(img_src, function (src, file_entry) {
if (file_entry) {
logger('Cached File Name: ' + file_entry.name, LOG_LEVEL_INFO);
ok();
} else {
nok();
}
});
} else {
nok();
}
}
},
{
name: 'getCachedFileURL',
test: function (ok, nok) {
var img_src = getBackgroundImageUrl($test_div);
if (img_src) {
ImgCache.getCachedFileURL(img_src, function (src, file_url) {
if (file_url) {
logger('Cached File URL: ' + file_url, LOG_LEVEL_INFO);
ok();
} else {
nok();
}
}, function (img_src) {
logger('Failed to retrieve cache URL for file: ' + img_src, LOG_LEVEL_ERROR);
nok();
});
}
else {
nok();
}
}
},
{
name: 'useCachedFile',
test: function (ok, nok) {
ImgCache.useCachedFile(getAppropriateElement($test_img), ok, nok);
}
},
{
name: 'useCachedBackground',
test: function (ok, nok) {
ImgCache.useCachedBackground(getAppropriateElement($test_div), ok, nok);
}
},
{
name: 'useOnlineFile',
test: function (ok, nok) {
ImgCache.useOnlineFile(getAppropriateElement($test_img));
if ($test_img.attr('src').match('^http') == 'http') {
ok();
} else {
nok();
}
}
},
{
name: 'useBackgroundOnlineFile',
test: function (ok, nok) {
ImgCache.useBackgroundOnlineFile(getAppropriateElement($test_div));
var img_src = getBackgroundImageUrl($test_div);
if (img_src.match('^http') == 'http') {
ok();
} else {
nok();
}
}
},
{
name: 'getCurrentSize',
test: function (ok, nok) {
if (ImgCache.getCurrentSize() > 0) {
ok();
} else {
nok();
}
}
},
{
name: 'removeFile',
test: function (ok, nok) {
ImgCache.removeFile($test_img.attr('src'), ok, nok);
}
},
{
name: 'isCached',
test: function (ok, nok) {
// expected result: failure
ImgCache.isCached($test_img.attr('src'),
function (src, res) { if (res) { nok(); } else { ok(); } });
}
},
{
name: 'cacheFile',
test: function (ok, nok) {
ImgCache.cacheFile($test_img.attr('src'), ok, nok);
}
},
{
name: 'useCachedFileWithSource',
test: function (ok, nok) {
ImgCache.useCachedFileWithSource(getAppropriateElement($test_img), $test_img.attr('src'), ok, nok);
}
},
{
name: 'getCacheFolderURI',
test: function (ok, nok) {
var folder = ImgCache.getCacheFolderURI();
logger('Output: ' + folder, LOG_LEVEL_INFO);
if (folder && folder.length) {
ok();
} else {
nok();
}
}
},
{
name: 'clearCache',
test: function (ok, nok) {
ImgCache.clearCache(ok, nok);
}
}
];
var cur_test_suite = 0;
var num_tests = tests_list.length * (test_suites_options.length + 1);
var success_count = 0;
var item = 0;
var run_next_test_suite = function () {
if (cur_test_suite === test_suites_options.length + 1) {
// reset non-option
ImgCache.jQuery = window.jQuery;
logger('END OF TESTS', LOG_LEVEL_INFO);
return;
}
$test_img = $('#test_img').clone();
$test_div = $('#test_div').clone();
// add those new elements to the dom to get styles
$('#sink')
.append($test_img)
.append($test_div);
if (cur_test_suite === test_suites_options.length) {
ImgCache.jQuery = false;
logger('Running tests without jQuery', LOG_LEVEL_INFO);
} else {
// see console output for debug info
var cur_options = test_suites_options[cur_test_suite];
ImgCache.options = $.extend({}, default_options, cur_options);
logger('Running tests with ImgCache options: ' + JSON.stringify(cur_options), LOG_LEVEL_INFO);
}
item = 0;
test_method();
cur_test_suite ++;
};
var test_method = function (result) {
if (result !== undefined) {
if (result) {
logger('> Result: SUCCESS', LOG_LEVEL_INFO);
success_count ++;
}
else
logger('> Result: FAILURE', LOG_LEVEL_ERROR);
}
if (item >= tests_list.length) {
$('#result').text('TEST RESULT: ' + success_count + '/' + num_tests);
var countTestedMethods = 0;
for (var key in testedMethods) {
if (testedMethods.hasOwnProperty(key)) { countTestedMethods++ };
}
var coverage = 'API coverage: ' + countTestedMethods + '/' + countTotalMethods;
if (countTestedMethods < countTotalMethods) {
var untestedMethods = '';
for (var i = 0; i < testableMethods.length; i++) {
var method = testableMethods[i];
if (!testedMethods.hasOwnProperty(method)) {
if (untestedMethods.length > 0) {
untestedMethods += ' / ';
}
untestedMethods += method;
}
}
if (untestedMethods.length > 0) {
coverage += '<br>Untested methods: ' + untestedMethods;
}
}
$('#methods').html(coverage);
logger('--------------------------------------------', LOG_LEVEL_INFO);
// launch second set of tests with different options
run_next_test_suite();
return;
}
var cur_test = tests_list[item];
logger('*** Testing ' + cur_test.name + ' ['+(item+1)+'/'+num_tests+']', LOG_LEVEL_INFO);
testedMethods[cur_test.name] = true;
// ready for next one
item ++;
//starts test now!
cur_test.test(function () { test_method(true); }, function () { test_method(false); });
};
run_next_test_suite();
};
/*
if (typeof(cordova) !== 'undefined') {
// cordova test
console.log('cordova start');
document.addEventListener('deviceready', startTest, false);
} else {
// normal browser test
$(document).ready(function () {
console.log('starting test');
startTest();
});
}
*/
</script>
<style>
/* just to make the page look a little nicer.. no use for the example in itself.. */
.note {
background-color: #bbb;
margin: 1em;
padding: 1em;
display: inline-block;
float: right;
clear: right;
}
code {
display: block;
margin: 0.5em;
}
body { background-color: #eef; }
h1 { text-align: center; }
#log { clear: both; list-style-type: none; }
#log li.error { background-color: #f99; }
#log li.warn { background-color: #ff8c00; }
#log li.info { background-color: #87cefa; }
#version { font-size: 1.2em; }
#result { font-size: 1.5em; font-weight: bold; }
#test_img { display: none; }
.bgimg { display: none; background-image: url(http://data-gov.tw.rpi.edu/w/images/thumb/b/b1/State-lib-sum.png/180px-State-lib-sum.png); }
#actions li {
line-height: 2em;
}
</style>
</head>
<body>
<h1>imgcache.js</h1>
<ul id="actions">
<li><a href="#" onclick="startTest()">Start unit tests</a></li>
<li><a href="examples/example1.html">Example #1</a></li>
<li><a href="examples/example2.html">Example #2</a></li>
</ul>
<div class="note">
<p>Don't forget to accept your browser request to store data on the local computer!</p>
</div>
<div class="note">
<p>If seen in Chrome from file://, run chrome with the following flags: <code>--allow-file-access-from-files --allow-file-access</code> in order to <a href="http://stackoverflow.com/questions/6427870/html5-file-api-security-error-while-reading-a-file">avoid a security error</a>.</p>
<p>Otherwise run the page from a web server.</p>
<p><a href="http://updates.html5rocks.com/2011/08/Debugging-the-Filesystem-API">More info.</a></p>
</div>
<!-- this is the test image, it needs to in a CORS enabled domain -->
<img id="test_img" src="http://data-gov.tw.rpi.edu/w/images/thumb/b/b1/State-lib-sum.png/120px-State-lib-sum.png">
<!-- this is the test div, it needs to in a CORS enabled domain -->
<div id="test_div" class="bgimg"> </div>
<br/>
<p id="version"></p>
<div id="result"></div>
<div id="methods"></div>
<ul id="log">
<h2>log</h2>
</ul>
<div id="sink"></div>
</body>
</html>