|
1 <!-- |
|
2 Any copyright is dedicated to the Public Domain. |
|
3 http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 --> |
|
5 <!DOCTYPE HTML> |
|
6 <html> <!-- |
|
7 https://bugzilla.mozilla.org/show_bug.cgi?id=717103 |
|
8 --> |
|
9 <head> |
|
10 <title>Test for the device storage API </title> |
|
11 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
12 <script type="text/javascript" src="devicestorage_common.js"></script> |
|
13 |
|
14 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
15 </head> |
|
16 <body> |
|
17 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717103">Mozilla Bug 717103</a> |
|
18 <p id="display"></p> |
|
19 <div id="content" style="display: none"> |
|
20 |
|
21 </div> |
|
22 <pre id="test"> |
|
23 <script class="testbody" type="text/javascript"> |
|
24 |
|
25 // Array Remove - By John Resig (MIT Licensed) |
|
26 Array.prototype.remove = function(from, to) { |
|
27 var rest = this.slice((to || from) + 1 || this.length); |
|
28 this.length = from < 0 ? this.length + from : from; |
|
29 return this.push.apply(this, rest); |
|
30 }; |
|
31 |
|
32 devicestorage_setup(); |
|
33 |
|
34 function enumerateSuccess(e) { |
|
35 |
|
36 if (e.target.result == null) { |
|
37 ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array") |
|
38 devicestorage_cleanup(); |
|
39 return; |
|
40 } |
|
41 |
|
42 var filename = e.target.result.name; |
|
43 if (filename[0] == "/") { |
|
44 // We got /storageName/prefix/filename |
|
45 // Remove the storageName (this shows up on FirefoxOS) |
|
46 filename = filename.substring(1); // Remove leading slash |
|
47 var slashIndex = filename.indexOf("/"); |
|
48 if (slashIndex >= 0) { |
|
49 filename = filename.substring(slashIndex + 1); // Remove storageName |
|
50 } |
|
51 } |
|
52 if (filename.startsWith(prefix)) { |
|
53 filename = filename.substring(prefix.length + 1); // Remove prefix |
|
54 } |
|
55 |
|
56 var index = files.indexOf(enumFilename); |
|
57 files.remove(index); |
|
58 |
|
59 ok(index > -1, "filename should be in the enumeration : " + e.target.result.name); |
|
60 |
|
61 // clean up |
|
62 var cleanup = storage.delete(e.target.result.name); |
|
63 cleanup.onsuccess = function(e) {} // todo - can i remove this? |
|
64 |
|
65 e.target.continue(); |
|
66 } |
|
67 |
|
68 function handleError(e) { |
|
69 ok(false, "handleError was called : " + e.target.error.name); |
|
70 devicestorage_cleanup(); |
|
71 } |
|
72 |
|
73 function addSuccess(e) { |
|
74 addedSoFar = addedSoFar + 1; |
|
75 if (addedSoFar == files.length) { |
|
76 |
|
77 var cursor = storage.enumerate(); |
|
78 cursor.onsuccess = enumerateSuccess; |
|
79 cursor.onerror = handleError; |
|
80 } |
|
81 } |
|
82 |
|
83 function addError(e) { |
|
84 ok(false, "addError was called : " + e.target.error.name); |
|
85 devicestorage_cleanup(); |
|
86 } |
|
87 |
|
88 var storage = navigator.getDeviceStorage("pictures"); |
|
89 ok(navigator.getDeviceStorage, "Should have getDeviceStorage"); |
|
90 var prefix = "devicestorage/" + randomFilename(12) |
|
91 |
|
92 var files = [ "a.png", "b.png", "c.png" ] |
|
93 var addedSoFar = 0; |
|
94 |
|
95 |
|
96 for (var i=0; i<files.length; i++) { |
|
97 |
|
98 request = storage.addNamed(createRandomBlob('image/png'), prefix + '/' + files[i]); |
|
99 |
|
100 ok(request, "Should have a non-null request"); |
|
101 request.onsuccess = addSuccess; |
|
102 request.onerror = addError; |
|
103 } |
|
104 |
|
105 </script> |
|
106 </pre> |
|
107 </body> |
|
108 </html> |
|
109 |