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
<!--
  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 Directory#get 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="text/javascript">

devicestorage_setup();
SimpleTest.requestCompleteLog();

// The root directory object.
var gRoot = null;
var gSub1 = null;
var gSub2 = null;
var gTestCount = 0;
var gPath = "/";

function testGetSuccess(dir, path) {
  dir.get(path).then(getSuccess, cbError);
}

function testGetFailure(dir, path) {
  dir.get(path).then(cbSuccess, getFailure);
}

function getSuccess(r) {
  ok(r, "[" + gTestCount +"] Should get the file - " + gPath + ".");
  switch (gTestCount) {
    case 0:
      gRoot = r;
      // Get sub1/sub2/text.png from root.
      gPath = "sub1/sub2/test.png";
      testGetSuccess(gRoot, "sub1/sub2/test.png");
      break;
    case 1:
      // Get sub1 from root.
      gPath = "sub1";
      testGetSuccess(gRoot, "sub1");
      break;
    case 2:
      // Get sub1/sub2 from root.
      gSub1 = r;
      gPath = "sub1/sub2";
      testGetSuccess(gRoot, "sub1/sub2");
      break;
    case 3:
      // Get sub1/sub2 from sub2.
      gSub2 = r;
      gPath = "sub1/sub2";
      testGetSuccess(gSub1, "sub2");
      break;
    case 4:
      // Test path with leading and trailing white spaces.
      gPath = "sub1/sub2";
      testGetSuccess(gSub1, "\t sub2 ");
      break;
    case 5:
      // Get sub1 from sub1/sub2 with "..".
      gPath = "sub1/sub2/..";
      testGetFailure(gSub2, "..");
      break;
    default:
      ok(false, "Should not arrive at getSuccess!");
      devicestorage_cleanup();
      break;
  }
  gTestCount++;
}

function getFailure(e) {
  ok(true, "[" + gTestCount +"] Should not get the file - " + gPath + ". Error: " + e.name);
  switch (gTestCount) {
    case 6:
      // Test special path "..".
      gPath = "sub1/sub2/../sub2";
      testGetFailure(gSub2, "../sub2");
      break;
    case 7:
      gPath = "sub1/sub2/../sub2";
      testGetFailure(gRoot, "sub1/sub2/../sub2");
      break;
    case 8:
      // Test special path ".".
      gPath = "sub1/./sub2";
      testGetFailure(gRoot, "sub1/./sub2");
      break;
    case 9:
      gPath = "./sub1/sub2";
      testGetFailure(gRoot, "./sub1/sub2");
      break;
    case 10:
      gPath = "././sub1/sub2";
      testGetFailure(gRoot, "././sub1/sub2");
      break;
    case 11:
      gPath = "sub1/sub2/.";
      testGetFailure(gRoot, "sub1/sub2/.");
      break;
    case 12:
      gPath = "sub1/.";
      testGetFailure(gSub1, "./");
      break;
    case 13:
      // Test path starting with "/".
      gPath = "sub1/";
      testGetFailure(gSub1, "/");
      break;
    case 14:
      // Test path ending with "/".
      gPath = "sub1/";
      testGetFailure(gSub1, "sub2/");
      break;
    case 15:
      // Test empty path.
      gPath = "sub2";
      testGetFailure(gSub2, "");
      break;
    case 16:
      // Test special path "//".
      gPath = "sub1//sub2";
      testGetFailure(gRoot, "sub1//sub2");
      break;
    case 17:
      devicestorage_cleanup();
      break;
    default:
      ok(false, "Should not arrive here!");
      devicestorage_cleanup();
      break;
  }
  gTestCount++;
}

function cbError(e) {
  ok(false, "Should not arrive at cbError! Error: " + e.name);
  devicestorage_cleanup();
}

function cbSuccess(e) {
  ok(false, "Should not arrive at cbSuccess!");
  devicestorage_cleanup();
}

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

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

function createTestFile(path, callback) {
  function addNamed() {
    var req = gStorage.addNamed(createRandomBlob("image/png"), path);

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

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

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

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

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

createTestFile("sub1/sub2/test.png", function() {
  var promise = gStorage.getRoot();
  ok(promise, "Should have a non-null promise for getRoot.");
  promise.then(getSuccess, cbError);
});

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