1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/devicestorage/test/test_fs_remove.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 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=934368 1.11 +--> 1.12 +<head> 1.13 + <title>Test Directory#remove and #removeDeep of the FileSystem API for device storage</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=934368">Mozilla Bug 934368</a> 1.21 +<p id="display"></p> 1.22 +<div id="content" style="display: none"> 1.23 +</div> 1.24 +<pre id="test"> 1.25 +<script class="testbody" type="application/javascript;version=1.7"> 1.26 + 1.27 +devicestorage_setup(); 1.28 + 1.29 +let gStorage = null; 1.30 +let gTestCount = 0; 1.31 +let gFileMap = {}; 1.32 +let gRemoveDeep = true; 1.33 + 1.34 +let gTestCases = [ 1.35 + // Remove a non-existent file should return false. 1.36 + { 1.37 + dir: "/", 1.38 + path: "non-existent.png", 1.39 + ret: false, 1.40 + shouldPass: true 1.41 + }, 1.42 + 1.43 + // Remove parent directory should fail. 1.44 + { 1.45 + dir: "sub1/sub2", 1.46 + target: "sub1", 1.47 + ret: true, 1.48 + shouldPass: false 1.49 + }, 1.50 + 1.51 + // Remove root directory should fail. 1.52 + { 1.53 + dir: "/", 1.54 + target: "/", 1.55 + ret: true, 1.56 + shouldPass: false 1.57 + }, 1.58 + 1.59 + // Remove non-descendant file should fail. 1.60 + { 1.61 + dir: "sub1", 1.62 + target: "sub/b.png", 1.63 + ret: true, 1.64 + shouldPass: false 1.65 + }, 1.66 + 1.67 + // Remove descendant file should return true. 1.68 + { 1.69 + dir: "sub1", 1.70 + target: "sub1/sub2/a.png", 1.71 + ret: true, 1.72 + shouldPass: true 1.73 + }, 1.74 + 1.75 + // Remove empty directory should return true. 1.76 + { 1.77 + dir: "sub1", 1.78 + path: "sub2", 1.79 + ret: true, 1.80 + shouldPass: true 1.81 + }, 1.82 + 1.83 + 1.84 + // Remove non-empty directory should return true for "removeDeep" and fail 1.85 + // for "remove". 1.86 + { 1.87 + dir: "/", 1.88 + path: "sub", 1.89 + ret: true, 1.90 + get shouldPass() { return gRemoveDeep; } 1.91 + } 1.92 +]; 1.93 + 1.94 +function createTestFiles(storage, callback) { 1.95 + function createTestFile(path) { 1.96 + return new Promise(function(resolve, reject) { 1.97 + function addNamed() { 1.98 + var req = storage.addNamed(createRandomBlob("image/png"), path); 1.99 + 1.100 + req.onsuccess = function() { 1.101 + ok(true, path + " was created."); 1.102 + resolve(); 1.103 + }; 1.104 + 1.105 + req.onerror = function(e) { 1.106 + ok(false, "Failed to create " + path + ': ' + e.target.error.name); 1.107 + reject(); 1.108 + }; 1.109 + } 1.110 + 1.111 + // Bug 980136. Check if the file exists before we create. 1.112 + var req = storage.get(path); 1.113 + 1.114 + req.onsuccess = function() { 1.115 + ok(true, path + " exists. Do not need to create."); 1.116 + resolve(); 1.117 + }; 1.118 + 1.119 + req.onerror = function(e) { 1.120 + ok(true, path + " does not exists: " + e.target.error.name); 1.121 + addNamed(); 1.122 + }; 1.123 + }); 1.124 + } 1.125 + 1.126 + let arr = []; 1.127 + 1.128 + ["sub1/sub2/a.png", "sub/b.png"].forEach(function(path) { 1.129 + arr.push(createTestFile(path)); 1.130 + }); 1.131 + 1.132 + Promise.all(arr).then(function() { 1.133 + callback(); 1.134 + }, function() { 1.135 + ok(false, "Failed to created test files."); 1.136 + devicestorage_cleanup(); 1.137 + }); 1.138 +} 1.139 + 1.140 +function runTest() { 1.141 + gTestCount = 0; 1.142 + createTestFiles(gStorage, function() { 1.143 + function cbError(e) { 1.144 + ok(false, "Should not arrive at cbError! Error: " + e.name); 1.145 + devicestorage_cleanup(); 1.146 + } 1.147 + 1.148 + function cbSuccess(r) { 1.149 + ok(r, "Should get the file - " + this); 1.150 + gFileMap[this] = r; 1.151 + } 1.152 + 1.153 + // Get directory and file objects. 1.154 + gStorage.getRoot().then(function(root) { 1.155 + ok(root, "Should get root directory."); 1.156 + gFileMap["/"] = root; 1.157 + 1.158 + let arr = []; 1.159 + 1.160 + ["sub1", "sub1/sub2", "sub1/sub2/a.png", "sub/b.png"].forEach(function(path) { 1.161 + arr.push(root.get(path).then(cbSuccess.bind(path), cbError)); 1.162 + }); 1.163 + 1.164 + Promise.all(arr).then(function() { 1.165 + testNextRemove(); 1.166 + }, function() { 1.167 + ok(false, "Failed to get test files."); 1.168 + devicestorage_cleanup(); 1.169 + }); 1.170 + }, cbError); 1.171 + }); 1.172 +} 1.173 + 1.174 +function testNextRemove() { 1.175 + if (gTestCount < gTestCases.length) { 1.176 + let data = gTestCases[gTestCount++]; 1.177 + let dir = gFileMap[data.dir]; 1.178 + let path = data.path || gFileMap[data.target]; 1.179 + let targetPath = data.path || data.target; 1.180 + let promise = gRemoveDeep ? dir.removeDeep(path) : dir.remove(path); 1.181 + promise.then(function(result) { 1.182 + ok(data.shouldPass, "Success callback was called to remove " + 1.183 + targetPath + " from " + data.dir); 1.184 + is(result, data.ret, "Return value should match to remove " + 1.185 + targetPath + " from " + data.dir); 1.186 + SimpleTest.executeSoon(testNextRemove); 1.187 + }, function(err) { 1.188 + ok(!data.shouldPass, "Error callback was called to remove " + 1.189 + targetPath + " from " + data.dir + '. Error: ' + err.name); 1.190 + SimpleTest.executeSoon(testNextRemove); 1.191 + }); 1.192 + return; 1.193 + } 1.194 + 1.195 + if (gRemoveDeep) { 1.196 + // Test "remove" after "removeDeep". 1.197 + gRemoveDeep = false; 1.198 + runTest(); 1.199 + return; 1.200 + } 1.201 + 1.202 + devicestorage_cleanup(); 1.203 +} 1.204 + 1.205 +ok(navigator.getDeviceStorage, "Should have getDeviceStorage."); 1.206 + 1.207 +let gStorage = navigator.getDeviceStorage("pictures"); 1.208 +ok(gStorage, "Should have gotten a storage."); 1.209 + 1.210 +// Test "removeDeep" first. 1.211 +gRemoveDeep = true; 1.212 +runTest(); 1.213 + 1.214 +</script> 1.215 +</pre> 1.216 +</body> 1.217 +</html> 1.218 +