Bug 910412 - Basic tests for filesystem API. r=dhylands
authorYuan Xulei <xyuan@mozilla.com>
Wed, 05 Mar 2014 11:24:53 +0800
changeset 173624 b1886a252ed6914c8d2c1887242d9b0ae6589a3d
parent 173623 7e61acb41c3f60f4f220072a0be40d6323b6dde5
child 173625 1b02211faa78a8df0ac159bae64abc99e1f70214
push id26414
push userkwierso@gmail.com
push dateSat, 15 Mar 2014 02:34:24 +0000
treeherdermozilla-central@092d63342910 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands
bugs910412
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 910412 - Basic tests for filesystem API. r=dhylands
dom/devicestorage/test/mochitest.ini
dom/devicestorage/test/test_fs_basic.html
dom/devicestorage/test/test_fs_createDirectory.html
dom/devicestorage/test/test_fs_get.html
--- a/dom/devicestorage/test/mochitest.ini
+++ b/dom/devicestorage/test/mochitest.ini
@@ -18,8 +18,13 @@ support-files = devicestorage_common.js
 [test_enumerateOptions.html]
 [test_freeSpace.html]
 [test_lastModificationFilter.html]
 [test_overwrite.html]
 [test_sanity.html]
 [test_usedSpace.html]
 [test_watch.html]
 [test_watchOther.html]
+
+# FileSystem API tests
+[test_fs_basic.html]
+[test_fs_createDirectory.html]
+[test_fs_get.html]
new file mode 100644
--- /dev/null
+++ b/dom/devicestorage/test/test_fs_basic.html
@@ -0,0 +1,70 @@
+<!--
+  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 for 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();
+
+var gFileName = randomFilename(12);
+
+// The root directory object.
+var gRoot;
+
+function getRootSuccess(r) {
+  ok(r && r.name === storage.storageName, "Failed to get the root directory.");
+
+  gRoot = r;
+
+  // Create a new directory under the root.
+  gRoot.createDirectory(gFileName).then(createDirectorySuccess, cbError);
+}
+
+function createDirectorySuccess(d) {
+  ok(d.name === gFileName, "Failed to create directory: name mismatch.");
+
+  // Get the new created directory from the root.
+  gRoot.get(gFileName).then(getSuccess, cbError);
+}
+
+function getSuccess(d) {
+  ok(d.name === gFileName, "Should get directory - " + gFileName + ".");
+  devicestorage_cleanup();
+}
+
+function cbError(e) {
+  ok(false,  "Should not arrive here! Error: " + e.name);
+  devicestorage_cleanup();
+}
+
+ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
+
+var storage = navigator.getDeviceStorage("pictures");
+ok(storage, "Should have gotten a storage");
+
+var promise = storage.getRoot();
+ok(promise, "Should have a non-null promise");
+
+promise.then(getRootSuccess, cbError);
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/dom/devicestorage/test/test_fs_createDirectory.html
@@ -0,0 +1,105 @@
+<!--
+  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="text/javascript">
+
+devicestorage_setup();
+
+// The root directory object.
+var gRoot;
+var gTestCount = 0;
+var gPath = '';
+var gName = '';
+
+function testCreateDirectory(rootDir, path) {
+  rootDir.createDirectory(path).then(createDirectorySuccess, cbError);
+}
+
+function createDirectorySuccess(d) {
+  ok(d.name === gName, "Failed to create directory: name mismatch.");
+
+  // Get the new created directory from the root.
+  gRoot.get(gPath).then(getSuccess, cbError);
+}
+
+function getSuccess(d) {
+  ok(d.name === gName, "Should get directory - " + (gPath || "[root]") + ".");
+  switch (gTestCount) {
+    case 0:
+      gRoot = d;
+      // Create a new directory under the root.
+      gName = gPath = randomFilename(12);
+      testCreateDirectory(gRoot, gName);
+      break;
+    case 1:
+      // Create a sub-directory under current directory.
+      gName = randomFilename(12);
+      gPath = gPath + '/' + gName;
+      testCreateDirectory(d, gName);
+      break;
+    case 2:
+      // Create directory with an existing path.
+      gRoot.createDirectory(gPath).then(function(what) {
+        ok(false, "Should not overwrite an existing directory.");
+        devicestorage_cleanup();
+      }, function(e) {
+        ok(true, "Creating directory should fail if it already exists.");
+
+        // Create a directory whose intermediate directory doesn't exit.
+        gName = randomFilename(12);
+        gPath = 'sub1/sub2/' + gName;
+        testCreateDirectory(gRoot, gPath);
+      });
+      break;
+    default:
+      // Create the parent directory.
+      d.createDirectory('..').then(function(what) {
+        ok(false, "Should not overwrite an existing directory.");
+        devicestorage_cleanup();
+      }, function(e) {
+        ok(true, "Accessing parent directory with '..' is not allowed.");
+        devicestorage_cleanup();
+      });
+      break;
+  }
+  gTestCount++;
+}
+
+function cbError(e) {
+  ok(false, e.name + " error should not arrive here!");
+  devicestorage_cleanup();
+}
+
+ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
+
+var storage = navigator.getDeviceStorage("pictures");
+ok(storage, "Should have gotten a storage.");
+
+var promise = storage.getRoot();
+ok(promise, "Should have a non-null promise for getRoot.");
+
+gName = storage.storageName;
+promise.then(getSuccess, cbError);
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/dom/devicestorage/test/test_fs_get.html
@@ -0,0 +1,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>
+