1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/devicestorage/test/test_enumerateNoParam.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +<!-- 1.5 + Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ 1.7 +--> 1.8 +<!DOCTYPE HTML> 1.9 +<html> <!-- 1.10 +https://bugzilla.mozilla.org/show_bug.cgi?id=717103 1.11 +--> 1.12 +<head> 1.13 + <title>Test for the device storage API </title> 1.14 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.15 + <script type="text/javascript" src="devicestorage_common.js"></script> 1.16 + 1.17 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.18 +</head> 1.19 +<body> 1.20 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717103">Mozilla Bug 717103</a> 1.21 +<p id="display"></p> 1.22 +<div id="content" style="display: none"> 1.23 + 1.24 +</div> 1.25 +<pre id="test"> 1.26 +<script class="testbody" type="text/javascript"> 1.27 + 1.28 +// Array Remove - By John Resig (MIT Licensed) 1.29 +Array.prototype.remove = function(from, to) { 1.30 + var rest = this.slice((to || from) + 1 || this.length); 1.31 + this.length = from < 0 ? this.length + from : from; 1.32 + return this.push.apply(this, rest); 1.33 +}; 1.34 + 1.35 +devicestorage_setup(); 1.36 + 1.37 +function enumerateSuccess(e) { 1.38 + 1.39 + if (e.target.result == null) { 1.40 + ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array") 1.41 + devicestorage_cleanup(); 1.42 + return; 1.43 + } 1.44 + 1.45 + var filename = e.target.result.name; 1.46 + if (filename[0] == "/") { 1.47 + // We got /storageName/prefix/filename 1.48 + // Remove the storageName (this shows up on FirefoxOS) 1.49 + filename = filename.substring(1); // Remove leading slash 1.50 + var slashIndex = filename.indexOf("/"); 1.51 + if (slashIndex >= 0) { 1.52 + filename = filename.substring(slashIndex + 1); // Remove storageName 1.53 + } 1.54 + } 1.55 + if (filename.startsWith(prefix)) { 1.56 + filename = filename.substring(prefix.length + 1); // Remove prefix 1.57 + } 1.58 + 1.59 + var index = files.indexOf(enumFilename); 1.60 + files.remove(index); 1.61 + 1.62 + ok(index > -1, "filename should be in the enumeration : " + e.target.result.name); 1.63 + 1.64 + // clean up 1.65 + var cleanup = storage.delete(e.target.result.name); 1.66 + cleanup.onsuccess = function(e) {} // todo - can i remove this? 1.67 + 1.68 + e.target.continue(); 1.69 +} 1.70 + 1.71 +function handleError(e) { 1.72 + ok(false, "handleError was called : " + e.target.error.name); 1.73 + devicestorage_cleanup(); 1.74 +} 1.75 + 1.76 +function addSuccess(e) { 1.77 + addedSoFar = addedSoFar + 1; 1.78 + if (addedSoFar == files.length) { 1.79 + 1.80 + var cursor = storage.enumerate(); 1.81 + cursor.onsuccess = enumerateSuccess; 1.82 + cursor.onerror = handleError; 1.83 + } 1.84 +} 1.85 + 1.86 +function addError(e) { 1.87 + ok(false, "addError was called : " + e.target.error.name); 1.88 + devicestorage_cleanup(); 1.89 +} 1.90 + 1.91 +var storage = navigator.getDeviceStorage("pictures"); 1.92 +ok(navigator.getDeviceStorage, "Should have getDeviceStorage"); 1.93 +var prefix = "devicestorage/" + randomFilename(12) 1.94 + 1.95 +var files = [ "a.png", "b.png", "c.png" ] 1.96 +var addedSoFar = 0; 1.97 + 1.98 + 1.99 +for (var i=0; i<files.length; i++) { 1.100 + 1.101 + request = storage.addNamed(createRandomBlob('image/png'), prefix + '/' + files[i]); 1.102 + 1.103 + ok(request, "Should have a non-null request"); 1.104 + request.onsuccess = addSuccess; 1.105 + request.onerror = addError; 1.106 +} 1.107 + 1.108 +</script> 1.109 +</pre> 1.110 +</body> 1.111 +</html> 1.112 +