dom/devicestorage/test/test_fs_remove.html

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:923789d5e6fa
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=934368
8 -->
9 <head>
10 <title>Test Directory#remove and #removeDeep 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=934368">Mozilla Bug 934368</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 gStorage = null;
27 let gTestCount = 0;
28 let gFileMap = {};
29 let gRemoveDeep = true;
30
31 let gTestCases = [
32 // Remove a non-existent file should return false.
33 {
34 dir: "/",
35 path: "non-existent.png",
36 ret: false,
37 shouldPass: true
38 },
39
40 // Remove parent directory should fail.
41 {
42 dir: "sub1/sub2",
43 target: "sub1",
44 ret: true,
45 shouldPass: false
46 },
47
48 // Remove root directory should fail.
49 {
50 dir: "/",
51 target: "/",
52 ret: true,
53 shouldPass: false
54 },
55
56 // Remove non-descendant file should fail.
57 {
58 dir: "sub1",
59 target: "sub/b.png",
60 ret: true,
61 shouldPass: false
62 },
63
64 // Remove descendant file should return true.
65 {
66 dir: "sub1",
67 target: "sub1/sub2/a.png",
68 ret: true,
69 shouldPass: true
70 },
71
72 // Remove empty directory should return true.
73 {
74 dir: "sub1",
75 path: "sub2",
76 ret: true,
77 shouldPass: true
78 },
79
80
81 // Remove non-empty directory should return true for "removeDeep" and fail
82 // for "remove".
83 {
84 dir: "/",
85 path: "sub",
86 ret: true,
87 get shouldPass() { return gRemoveDeep; }
88 }
89 ];
90
91 function createTestFiles(storage, callback) {
92 function createTestFile(path) {
93 return new Promise(function(resolve, reject) {
94 function addNamed() {
95 var req = storage.addNamed(createRandomBlob("image/png"), path);
96
97 req.onsuccess = function() {
98 ok(true, path + " was created.");
99 resolve();
100 };
101
102 req.onerror = function(e) {
103 ok(false, "Failed to create " + path + ': ' + e.target.error.name);
104 reject();
105 };
106 }
107
108 // Bug 980136. Check if the file exists before we create.
109 var req = storage.get(path);
110
111 req.onsuccess = function() {
112 ok(true, path + " exists. Do not need to create.");
113 resolve();
114 };
115
116 req.onerror = function(e) {
117 ok(true, path + " does not exists: " + e.target.error.name);
118 addNamed();
119 };
120 });
121 }
122
123 let arr = [];
124
125 ["sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
126 arr.push(createTestFile(path));
127 });
128
129 Promise.all(arr).then(function() {
130 callback();
131 }, function() {
132 ok(false, "Failed to created test files.");
133 devicestorage_cleanup();
134 });
135 }
136
137 function runTest() {
138 gTestCount = 0;
139 createTestFiles(gStorage, function() {
140 function cbError(e) {
141 ok(false, "Should not arrive at cbError! Error: " + e.name);
142 devicestorage_cleanup();
143 }
144
145 function cbSuccess(r) {
146 ok(r, "Should get the file - " + this);
147 gFileMap[this] = r;
148 }
149
150 // Get directory and file objects.
151 gStorage.getRoot().then(function(root) {
152 ok(root, "Should get root directory.");
153 gFileMap["/"] = root;
154
155 let arr = [];
156
157 ["sub1", "sub1/sub2", "sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
158 arr.push(root.get(path).then(cbSuccess.bind(path), cbError));
159 });
160
161 Promise.all(arr).then(function() {
162 testNextRemove();
163 }, function() {
164 ok(false, "Failed to get test files.");
165 devicestorage_cleanup();
166 });
167 }, cbError);
168 });
169 }
170
171 function testNextRemove() {
172 if (gTestCount < gTestCases.length) {
173 let data = gTestCases[gTestCount++];
174 let dir = gFileMap[data.dir];
175 let path = data.path || gFileMap[data.target];
176 let targetPath = data.path || data.target;
177 let promise = gRemoveDeep ? dir.removeDeep(path) : dir.remove(path);
178 promise.then(function(result) {
179 ok(data.shouldPass, "Success callback was called to remove " +
180 targetPath + " from " + data.dir);
181 is(result, data.ret, "Return value should match to remove " +
182 targetPath + " from " + data.dir);
183 SimpleTest.executeSoon(testNextRemove);
184 }, function(err) {
185 ok(!data.shouldPass, "Error callback was called to remove " +
186 targetPath + " from " + data.dir + '. Error: ' + err.name);
187 SimpleTest.executeSoon(testNextRemove);
188 });
189 return;
190 }
191
192 if (gRemoveDeep) {
193 // Test "remove" after "removeDeep".
194 gRemoveDeep = false;
195 runTest();
196 return;
197 }
198
199 devicestorage_cleanup();
200 }
201
202 ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
203
204 let gStorage = navigator.getDeviceStorage("pictures");
205 ok(gStorage, "Should have gotten a storage.");
206
207 // Test "removeDeep" first.
208 gRemoveDeep = true;
209 runTest();
210
211 </script>
212 </pre>
213 </body>
214 </html>
215

mercurial