DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (777e60ca8853)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html> <!--
https://bugzilla.mozilla.org/show_bug.cgi?id=934368
-->
<head>
  <title>Test Directory#remove and #removeDeep of the FileSystem API for device storage</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="devicestorage_common.js"></script>

<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=934368">Mozilla Bug 934368</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

devicestorage_setup();

let gStorage = null;
let gTestCount = 0;
let gFileMap = {};
let gRemoveDeep = true;

let gTestCases = [
  // Remove a non-existent file should return false.
  {
    dir: "/",
    path: "non-existent.png",
    ret: false,
    shouldPass: true
  },

  // Remove parent directory should fail.
  {
    dir: "sub1/sub2",
    target: "sub1",
    ret: true,
    shouldPass: false
  },

  // Remove root directory should fail.
  {
    dir: "/",
    target: "/",
    ret: true,
    shouldPass: false
  },

  // Remove non-descendant file should fail.
  {
    dir: "sub1",
    target: "sub/b.png",
    ret: true,
    shouldPass: false
  },

  // Remove descendant file should return true.
  {
    dir: "sub1",
    target: "sub1/sub2/a.png",
    ret: true,
    shouldPass: true
  },

  // Remove empty directory should return true.
  {
    dir: "sub1",
    path: "sub2",
    ret: true,
    shouldPass: true
  },


  // Remove non-empty directory should return true for "removeDeep" and fail
  // for "remove".
  {
    dir: "/",
    path: "sub",
    ret: true,
    get shouldPass() { return gRemoveDeep; }
  }
];

function createTestFiles(storage, callback) {
  function createTestFile(path) {
    return new Promise(function(resolve, reject) {
      function addNamed() {
        var req = storage.addNamed(createRandomBlob("image/png"), path);

        req.onsuccess = function() {
          ok(true, path + " was created.");
          resolve();
        };

        req.onerror = function(e) {
          ok(false, "Failed to create " + path + ': ' + e.target.error.name);
          reject();
        };
      }

      // Bug 980136. Check if the file exists before we create.
      var req = storage.get(path);

      req.onsuccess = function() {
        ok(true, path + " exists. Do not need to create.");
        resolve();
      };

      req.onerror = function(e) {
        ok(true, path + " does not exists: " + e.target.error.name);
        addNamed();
      };
    });
  }

  let arr = [];

  ["sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
    arr.push(createTestFile(path));
  });

  Promise.all(arr).then(function() {
    callback();
  }, function() {
    ok(false, "Failed to created test files.");
    devicestorage_cleanup();
  });
}

function runTest() {
  gTestCount = 0;
  createTestFiles(gStorage, function() {
    function cbError(e) {
      ok(false, "Should not arrive at cbError! Error: " + e.name);
      devicestorage_cleanup();
    }

    function cbSuccess(r) {
      ok(r, "Should get the file - " + this);
      gFileMap[this] = r;
    }

    // Get directory and file objects.
    gStorage.getRoot().then(function(root) {
      ok(root, "Should get root directory.");
      gFileMap["/"] = root;

      let arr = [];

      ["sub1", "sub1/sub2", "sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
        arr.push(root.get(path).then(cbSuccess.bind(path), cbError));
      });

      Promise.all(arr).then(function() {
        testNextRemove();
      }, function() {
        ok(false, "Failed to get test files.");
        devicestorage_cleanup();
      });
    }, cbError);
  });
}

function testNextRemove() {
  if (gTestCount < gTestCases.length) {
    let data = gTestCases[gTestCount++];
    let dir = gFileMap[data.dir];
    let path = data.path || gFileMap[data.target];
    let targetPath = data.path || data.target;
    let promise = gRemoveDeep ? dir.removeDeep(path) : dir.remove(path);
    promise.then(function(result) {
      ok(data.shouldPass, "Success callback was called to remove " +
        targetPath + " from " + data.dir);
      is(result, data.ret, "Return value should match to remove " +
        targetPath + " from " + data.dir);
      SimpleTest.executeSoon(testNextRemove);
    }, function(err) {
      ok(!data.shouldPass, "Error callback was called to remove " +
        targetPath + " from " + data.dir + '. Error: ' + err.name);
      SimpleTest.executeSoon(testNextRemove);
    });
    return;
  }

  if (gRemoveDeep) {
    // Test "remove" after "removeDeep".
    gRemoveDeep = false;
    runTest();
    return;
  }

  devicestorage_cleanup();
}

ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");

let gStorage = navigator.getDeviceStorage("pictures");
ok(gStorage, "Should have gotten a storage.");

// Test "removeDeep" first.
gRemoveDeep = true;
runTest();

</script>
</pre>
</body>
</html>