|
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=910412 |
|
8 --> |
|
9 <head> |
|
10 <title>Test createDirectory of the FileSystem API for device storage</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=910412">Mozilla Bug 910412</a> |
|
18 <p id="display"></p> |
|
19 <div id="content" style="display: none"> |
|
20 </div> |
|
21 <pre id="test"> |
|
22 <script class="testbody" type="application/javascript;version=1.7"> |
|
23 |
|
24 devicestorage_setup(); |
|
25 |
|
26 let gTestCount = 0; |
|
27 let gFileReader = new FileReader(); |
|
28 let gRoot; |
|
29 |
|
30 function str2array(str) { |
|
31 let strlen = str.length; |
|
32 let buf = new ArrayBuffer(strlen); |
|
33 let bufView = new Uint8Array(buf); |
|
34 for (let i=0; i < strlen; i++) { |
|
35 bufView[i] = str.charCodeAt(i); |
|
36 } |
|
37 return buf; |
|
38 } |
|
39 |
|
40 function array2str(data) { |
|
41 return String.fromCharCode.apply(String, new Uint8Array(data)); |
|
42 } |
|
43 |
|
44 let gTestCases = [ |
|
45 // Create with string data. |
|
46 { |
|
47 text: "My name is Yuan.", |
|
48 get data() { return this.text; }, |
|
49 shouldPass: true, |
|
50 mode: "replace" |
|
51 }, |
|
52 |
|
53 // Create with array buffer data. |
|
54 { |
|
55 text: "I'm from Kunming.", |
|
56 get data() { return str2array(this.text); }, |
|
57 shouldPass: true, |
|
58 mode: "replace" |
|
59 }, |
|
60 |
|
61 // Create with array buffer view data. |
|
62 { |
|
63 text: "Kunming is in Yunnan province of China.", |
|
64 get data() { return Uint8Array(str2array(this.text)); }, |
|
65 shouldPass: true, |
|
66 mode: "replace" |
|
67 }, |
|
68 |
|
69 // Create with blob data. |
|
70 { |
|
71 text: "Kunming is in Yunnan province of China.", |
|
72 get data() { return new Blob([this.text], {type: 'image/png'}); }, |
|
73 shouldPass: true, |
|
74 mode: "replace" |
|
75 }, |
|
76 |
|
77 // Don't overwrite existing file. |
|
78 { |
|
79 data: null, |
|
80 shouldPass: false, |
|
81 mode: "fail" |
|
82 } |
|
83 ]; |
|
84 |
|
85 function next() { |
|
86 if (gTestCount >= gTestCases.length) { |
|
87 devicestorage_cleanup(); |
|
88 return; |
|
89 } |
|
90 let c = gTestCases[gTestCount++]; |
|
91 gRoot.createFile("text.png", { |
|
92 data: c.data, |
|
93 ifExists: c.mode |
|
94 }).then(function(file) { |
|
95 is(c.shouldPass, true, "[" + gTestCount + "] Success callback was called for createFile."); |
|
96 if (!c.shouldPass) { |
|
97 SimpleTest.executeSoon(next); |
|
98 return; |
|
99 } |
|
100 // Check the file content. |
|
101 gFileReader.readAsArrayBuffer(file); |
|
102 gFileReader.onload = function(e) { |
|
103 ab = e.target.result; |
|
104 is(array2str(e.target.result), c.text, "[" + gTestCount + "] Wrong values."); |
|
105 SimpleTest.executeSoon(next); |
|
106 }; |
|
107 }, function(e) { |
|
108 is(c.shouldPass, false, "[" + gTestCount + "] Error callback was called for createFile."); |
|
109 SimpleTest.executeSoon(next); |
|
110 }); |
|
111 } |
|
112 |
|
113 ok(navigator.getDeviceStorage, "Should have getDeviceStorage."); |
|
114 |
|
115 let storage = navigator.getDeviceStorage("pictures"); |
|
116 ok(storage, "Should have gotten a storage."); |
|
117 |
|
118 // Get the root directory |
|
119 storage.getRoot().then(function(dir) { |
|
120 ok(dir, "Should have gotten the root directory."); |
|
121 gRoot = dir; |
|
122 next(); |
|
123 }, function(e) { |
|
124 ok(false, e.name + " error should not arrive here!"); |
|
125 devicestorage_cleanup(); |
|
126 }); |
|
127 |
|
128 </script> |
|
129 </pre> |
|
130 </body> |
|
131 </html> |
|
132 |