dom/devicestorage/test/test_fs_remove.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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>
    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">
    24 devicestorage_setup();
    26 let gStorage = null;
    27 let gTestCount = 0;
    28 let gFileMap = {};
    29 let gRemoveDeep = true;
    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   },
    40   // Remove parent directory should fail.
    41   {
    42     dir: "sub1/sub2",
    43     target: "sub1",
    44     ret: true,
    45     shouldPass: false
    46   },
    48   // Remove root directory should fail.
    49   {
    50     dir: "/",
    51     target: "/",
    52     ret: true,
    53     shouldPass: false
    54   },
    56   // Remove non-descendant file should fail.
    57   {
    58     dir: "sub1",
    59     target: "sub/b.png",
    60     ret: true,
    61     shouldPass: false
    62   },
    64   // Remove descendant file should return true.
    65   {
    66     dir: "sub1",
    67     target: "sub1/sub2/a.png",
    68     ret: true,
    69     shouldPass: true
    70   },
    72   // Remove empty directory should return true.
    73   {
    74     dir: "sub1",
    75     path: "sub2",
    76     ret: true,
    77     shouldPass: true
    78   },
    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 ];
    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);
    97         req.onsuccess = function() {
    98           ok(true, path + " was created.");
    99           resolve();
   100         };
   102         req.onerror = function(e) {
   103           ok(false, "Failed to create " + path + ': ' + e.target.error.name);
   104           reject();
   105         };
   106       }
   108       // Bug 980136. Check if the file exists before we create.
   109       var req = storage.get(path);
   111       req.onsuccess = function() {
   112         ok(true, path + " exists. Do not need to create.");
   113         resolve();
   114       };
   116       req.onerror = function(e) {
   117         ok(true, path + " does not exists: " + e.target.error.name);
   118         addNamed();
   119       };
   120     });
   121   }
   123   let arr = [];
   125   ["sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
   126     arr.push(createTestFile(path));
   127   });
   129   Promise.all(arr).then(function() {
   130     callback();
   131   }, function() {
   132     ok(false, "Failed to created test files.");
   133     devicestorage_cleanup();
   134   });
   135 }
   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     }
   145     function cbSuccess(r) {
   146       ok(r, "Should get the file - " + this);
   147       gFileMap[this] = r;
   148     }
   150     // Get directory and file objects.
   151     gStorage.getRoot().then(function(root) {
   152       ok(root, "Should get root directory.");
   153       gFileMap["/"] = root;
   155       let arr = [];
   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       });
   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 }
   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   }
   192   if (gRemoveDeep) {
   193     // Test "remove" after "removeDeep".
   194     gRemoveDeep = false;
   195     runTest();
   196     return;
   197   }
   199   devicestorage_cleanup();
   200 }
   202 ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
   204 let gStorage = navigator.getDeviceStorage("pictures");
   205 ok(gStorage, "Should have gotten a storage.");
   207 // Test "removeDeep" first.
   208 gRemoveDeep = true;
   209 runTest();
   211 </script>
   212 </pre>
   213 </body>
   214 </html>

mercurial