dom/devicestorage/test/test_fs_get.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/devicestorage/test/test_fs_get.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,204 @@
     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=910412
    1.11 +-->
    1.12 +<head>
    1.13 +  <title>Test Directory#get 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=910412">Mozilla Bug 910412</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="text/javascript">
    1.26 +
    1.27 +devicestorage_setup();
    1.28 +SimpleTest.requestCompleteLog();
    1.29 +
    1.30 +// The root directory object.
    1.31 +var gRoot = null;
    1.32 +var gSub1 = null;
    1.33 +var gSub2 = null;
    1.34 +var gTestCount = 0;
    1.35 +var gPath = "/";
    1.36 +
    1.37 +function testGetSuccess(dir, path) {
    1.38 +  dir.get(path).then(getSuccess, cbError);
    1.39 +}
    1.40 +
    1.41 +function testGetFailure(dir, path) {
    1.42 +  dir.get(path).then(cbSuccess, getFailure);
    1.43 +}
    1.44 +
    1.45 +function getSuccess(r) {
    1.46 +  ok(r, "[" + gTestCount +"] Should get the file - " + gPath + ".");
    1.47 +  switch (gTestCount) {
    1.48 +    case 0:
    1.49 +      gRoot = r;
    1.50 +      // Get sub1/sub2/text.png from root.
    1.51 +      gPath = "sub1/sub2/test.png";
    1.52 +      testGetSuccess(gRoot, "sub1/sub2/test.png");
    1.53 +      break;
    1.54 +    case 1:
    1.55 +      // Get sub1 from root.
    1.56 +      gPath = "sub1";
    1.57 +      testGetSuccess(gRoot, "sub1");
    1.58 +      break;
    1.59 +    case 2:
    1.60 +      // Get sub1/sub2 from root.
    1.61 +      gSub1 = r;
    1.62 +      gPath = "sub1/sub2";
    1.63 +      testGetSuccess(gRoot, "sub1/sub2");
    1.64 +      break;
    1.65 +    case 3:
    1.66 +      // Get sub1/sub2 from sub2.
    1.67 +      gSub2 = r;
    1.68 +      gPath = "sub1/sub2";
    1.69 +      testGetSuccess(gSub1, "sub2");
    1.70 +      break;
    1.71 +    case 4:
    1.72 +      // Test path with leading and trailing white spaces.
    1.73 +      gPath = "sub1/sub2";
    1.74 +      testGetSuccess(gSub1, "\t sub2 ");
    1.75 +      break;
    1.76 +    case 5:
    1.77 +      // Get sub1 from sub1/sub2 with "..".
    1.78 +      gPath = "sub1/sub2/..";
    1.79 +      testGetFailure(gSub2, "..");
    1.80 +      break;
    1.81 +    default:
    1.82 +      ok(false, "Should not arrive at getSuccess!");
    1.83 +      devicestorage_cleanup();
    1.84 +      break;
    1.85 +  }
    1.86 +  gTestCount++;
    1.87 +}
    1.88 +
    1.89 +function getFailure(e) {
    1.90 +  ok(true, "[" + gTestCount +"] Should not get the file - " + gPath + ". Error: " + e.name);
    1.91 +  switch (gTestCount) {
    1.92 +    case 6:
    1.93 +      // Test special path "..".
    1.94 +      gPath = "sub1/sub2/../sub2";
    1.95 +      testGetFailure(gSub2, "../sub2");
    1.96 +      break;
    1.97 +    case 7:
    1.98 +      gPath = "sub1/sub2/../sub2";
    1.99 +      testGetFailure(gRoot, "sub1/sub2/../sub2");
   1.100 +      break;
   1.101 +    case 8:
   1.102 +      // Test special path ".".
   1.103 +      gPath = "sub1/./sub2";
   1.104 +      testGetFailure(gRoot, "sub1/./sub2");
   1.105 +      break;
   1.106 +    case 9:
   1.107 +      gPath = "./sub1/sub2";
   1.108 +      testGetFailure(gRoot, "./sub1/sub2");
   1.109 +      break;
   1.110 +    case 10:
   1.111 +      gPath = "././sub1/sub2";
   1.112 +      testGetFailure(gRoot, "././sub1/sub2");
   1.113 +      break;
   1.114 +    case 11:
   1.115 +      gPath = "sub1/sub2/.";
   1.116 +      testGetFailure(gRoot, "sub1/sub2/.");
   1.117 +      break;
   1.118 +    case 12:
   1.119 +      gPath = "sub1/.";
   1.120 +      testGetFailure(gSub1, "./");
   1.121 +      break;
   1.122 +    case 13:
   1.123 +      // Test path starting with "/".
   1.124 +      gPath = "sub1/";
   1.125 +      testGetFailure(gSub1, "/");
   1.126 +      break;
   1.127 +    case 14:
   1.128 +      // Test path ending with "/".
   1.129 +      gPath = "sub1/";
   1.130 +      testGetFailure(gSub1, "sub2/");
   1.131 +      break;
   1.132 +    case 15:
   1.133 +      // Test empty path.
   1.134 +      gPath = "sub2";
   1.135 +      testGetFailure(gSub2, "");
   1.136 +      break;
   1.137 +    case 16:
   1.138 +      // Test special path "//".
   1.139 +      gPath = "sub1//sub2";
   1.140 +      testGetFailure(gRoot, "sub1//sub2");
   1.141 +      break;
   1.142 +    case 17:
   1.143 +      devicestorage_cleanup();
   1.144 +      break;
   1.145 +    default:
   1.146 +      ok(false, "Should not arrive here!");
   1.147 +      devicestorage_cleanup();
   1.148 +      break;
   1.149 +  }
   1.150 +  gTestCount++;
   1.151 +}
   1.152 +
   1.153 +function cbError(e) {
   1.154 +  ok(false, "Should not arrive at cbError! Error: " + e.name);
   1.155 +  devicestorage_cleanup();
   1.156 +}
   1.157 +
   1.158 +function cbSuccess(e) {
   1.159 +  ok(false, "Should not arrive at cbSuccess!");
   1.160 +  devicestorage_cleanup();
   1.161 +}
   1.162 +
   1.163 +ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
   1.164 +
   1.165 +var gStorage = navigator.getDeviceStorage("pictures");
   1.166 +ok(gStorage, "Should have gotten a storage.");
   1.167 +
   1.168 +function createTestFile(path, callback) {
   1.169 +  function addNamed() {
   1.170 +    var req = gStorage.addNamed(createRandomBlob("image/png"), path);
   1.171 +
   1.172 +    req.onsuccess = function() {
   1.173 +      ok(true, path + " was created.");
   1.174 +      callback();
   1.175 +    };
   1.176 +
   1.177 +    req.onerror = function(e) {
   1.178 +      ok(false, "Failed to create " + path + ": " + e.target.error.name);
   1.179 +      devicestorage_cleanup();
   1.180 +    };
   1.181 +  }
   1.182 +
   1.183 +  // Bug 980136. Check if the file exists before we create.
   1.184 +  var req = gStorage.get(path);
   1.185 +
   1.186 +  req.onsuccess = function() {
   1.187 +    ok(true, path + " exists. Do not need to create.");
   1.188 +    callback();
   1.189 +  };
   1.190 +
   1.191 +  req.onerror = function(e) {
   1.192 +    ok(true, path + " does not exists: " + e.target.error.name);
   1.193 +    addNamed();
   1.194 +  };
   1.195 +}
   1.196 +
   1.197 +createTestFile("sub1/sub2/test.png", function() {
   1.198 +  var promise = gStorage.getRoot();
   1.199 +  ok(promise, "Should have a non-null promise for getRoot.");
   1.200 +  promise.then(getSuccess, cbError);
   1.201 +});
   1.202 +
   1.203 +</script>
   1.204 +</pre>
   1.205 +</body>
   1.206 +</html>
   1.207 +

mercurial