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
<!--
  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=910412
-->
<head>
  <title>Test createDirectory 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=910412">Mozilla Bug 910412</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 gTestCount = 0;
let gFileReader = new FileReader();
let gRoot;

function str2array(str) {
  let strlen = str.length;
  let buf = new ArrayBuffer(strlen);
  let bufView = new Uint8Array(buf);
  for (let i=0; i < strlen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

function array2str(data) {
  return String.fromCharCode.apply(String, new Uint8Array(data));
}

let gTestCases = [
  // Create with string data.
  {
    text: "My name is Yuan.",
    get data() { return this.text; },
    shouldPass: true,
    mode: "replace"
  },

  // Create with array buffer data.
  {
    text: "I'm from Kunming.",
    get data() { return str2array(this.text); },
    shouldPass: true,
    mode: "replace"
  },

  // Create with array buffer view data.
  {
    text: "Kunming is in Yunnan province of China.",
    get data() { return Uint8Array(str2array(this.text)); },
    shouldPass: true,
    mode: "replace"
  },

  // Create with blob data.
  {
    text: "Kunming is in Yunnan province of China.",
    get data() { return new Blob([this.text], {type: 'image/png'}); },
    shouldPass: true,
    mode: "replace"
  },

  // Don't overwrite existing file.
  {
    data: null,
    shouldPass: false,
    mode: "fail"
  }
];

function next() {
  if (gTestCount >= gTestCases.length) {
    devicestorage_cleanup();
    return;
  }
  let c = gTestCases[gTestCount++];
  gRoot.createFile("text.png", {
    data: c.data,
    ifExists: c.mode
  }).then(function(file) {
    is(c.shouldPass, true, "[" + gTestCount + "] Success callback was called for createFile.");
    if (!c.shouldPass) {
      SimpleTest.executeSoon(next);
      return;
    }
    // Check the file content.
    gFileReader.readAsArrayBuffer(file);
    gFileReader.onload = function(e) {
      ab = e.target.result;
      is(array2str(e.target.result), c.text, "[" + gTestCount + "] Wrong values.");
      SimpleTest.executeSoon(next);
    };
  }, function(e) {
    is(c.shouldPass, false, "[" + gTestCount + "] Error callback was called for createFile.");
    SimpleTest.executeSoon(next);
  });
}

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

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

// Get the root directory
storage.getRoot().then(function(dir) {
  ok(dir, "Should have gotten the root directory.");
  gRoot = dir;
  next();
}, function(e) {
  ok(false, e.name + " error should not arrive here!");
  devicestorage_cleanup();
});

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