dom/devicestorage/test/test_fs_get.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!--
     2   Any copyright is dedicated to the Public Domain.
     3   http://creativecommons.org/publicdomain/zero/1.0/
     4 -->
     5 <!DOCTYPE HTML>
     6 <html> <!--
     7 https://bugzilla.mozilla.org/show_bug.cgi?id=910412
     8 -->
     9 <head>
    10   <title>Test Directory#get of the FileSystem API for device storage</title>
    11   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    12   <script type="text/javascript" src="devicestorage_common.js"></script>
    14 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    15 </head>
    16 <body>
    17 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=910412">Mozilla Bug 910412</a>
    18 <p id="display"></p>
    19 <div id="content" style="display: none">
    20 </div>
    21 <pre id="test">
    22 <script class="testbody" type="text/javascript">
    24 devicestorage_setup();
    25 SimpleTest.requestCompleteLog();
    27 // The root directory object.
    28 var gRoot = null;
    29 var gSub1 = null;
    30 var gSub2 = null;
    31 var gTestCount = 0;
    32 var gPath = "/";
    34 function testGetSuccess(dir, path) {
    35   dir.get(path).then(getSuccess, cbError);
    36 }
    38 function testGetFailure(dir, path) {
    39   dir.get(path).then(cbSuccess, getFailure);
    40 }
    42 function getSuccess(r) {
    43   ok(r, "[" + gTestCount +"] Should get the file - " + gPath + ".");
    44   switch (gTestCount) {
    45     case 0:
    46       gRoot = r;
    47       // Get sub1/sub2/text.png from root.
    48       gPath = "sub1/sub2/test.png";
    49       testGetSuccess(gRoot, "sub1/sub2/test.png");
    50       break;
    51     case 1:
    52       // Get sub1 from root.
    53       gPath = "sub1";
    54       testGetSuccess(gRoot, "sub1");
    55       break;
    56     case 2:
    57       // Get sub1/sub2 from root.
    58       gSub1 = r;
    59       gPath = "sub1/sub2";
    60       testGetSuccess(gRoot, "sub1/sub2");
    61       break;
    62     case 3:
    63       // Get sub1/sub2 from sub2.
    64       gSub2 = r;
    65       gPath = "sub1/sub2";
    66       testGetSuccess(gSub1, "sub2");
    67       break;
    68     case 4:
    69       // Test path with leading and trailing white spaces.
    70       gPath = "sub1/sub2";
    71       testGetSuccess(gSub1, "\t sub2 ");
    72       break;
    73     case 5:
    74       // Get sub1 from sub1/sub2 with "..".
    75       gPath = "sub1/sub2/..";
    76       testGetFailure(gSub2, "..");
    77       break;
    78     default:
    79       ok(false, "Should not arrive at getSuccess!");
    80       devicestorage_cleanup();
    81       break;
    82   }
    83   gTestCount++;
    84 }
    86 function getFailure(e) {
    87   ok(true, "[" + gTestCount +"] Should not get the file - " + gPath + ". Error: " + e.name);
    88   switch (gTestCount) {
    89     case 6:
    90       // Test special path "..".
    91       gPath = "sub1/sub2/../sub2";
    92       testGetFailure(gSub2, "../sub2");
    93       break;
    94     case 7:
    95       gPath = "sub1/sub2/../sub2";
    96       testGetFailure(gRoot, "sub1/sub2/../sub2");
    97       break;
    98     case 8:
    99       // Test special path ".".
   100       gPath = "sub1/./sub2";
   101       testGetFailure(gRoot, "sub1/./sub2");
   102       break;
   103     case 9:
   104       gPath = "./sub1/sub2";
   105       testGetFailure(gRoot, "./sub1/sub2");
   106       break;
   107     case 10:
   108       gPath = "././sub1/sub2";
   109       testGetFailure(gRoot, "././sub1/sub2");
   110       break;
   111     case 11:
   112       gPath = "sub1/sub2/.";
   113       testGetFailure(gRoot, "sub1/sub2/.");
   114       break;
   115     case 12:
   116       gPath = "sub1/.";
   117       testGetFailure(gSub1, "./");
   118       break;
   119     case 13:
   120       // Test path starting with "/".
   121       gPath = "sub1/";
   122       testGetFailure(gSub1, "/");
   123       break;
   124     case 14:
   125       // Test path ending with "/".
   126       gPath = "sub1/";
   127       testGetFailure(gSub1, "sub2/");
   128       break;
   129     case 15:
   130       // Test empty path.
   131       gPath = "sub2";
   132       testGetFailure(gSub2, "");
   133       break;
   134     case 16:
   135       // Test special path "//".
   136       gPath = "sub1//sub2";
   137       testGetFailure(gRoot, "sub1//sub2");
   138       break;
   139     case 17:
   140       devicestorage_cleanup();
   141       break;
   142     default:
   143       ok(false, "Should not arrive here!");
   144       devicestorage_cleanup();
   145       break;
   146   }
   147   gTestCount++;
   148 }
   150 function cbError(e) {
   151   ok(false, "Should not arrive at cbError! Error: " + e.name);
   152   devicestorage_cleanup();
   153 }
   155 function cbSuccess(e) {
   156   ok(false, "Should not arrive at cbSuccess!");
   157   devicestorage_cleanup();
   158 }
   160 ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
   162 var gStorage = navigator.getDeviceStorage("pictures");
   163 ok(gStorage, "Should have gotten a storage.");
   165 function createTestFile(path, callback) {
   166   function addNamed() {
   167     var req = gStorage.addNamed(createRandomBlob("image/png"), path);
   169     req.onsuccess = function() {
   170       ok(true, path + " was created.");
   171       callback();
   172     };
   174     req.onerror = function(e) {
   175       ok(false, "Failed to create " + path + ": " + e.target.error.name);
   176       devicestorage_cleanup();
   177     };
   178   }
   180   // Bug 980136. Check if the file exists before we create.
   181   var req = gStorage.get(path);
   183   req.onsuccess = function() {
   184     ok(true, path + " exists. Do not need to create.");
   185     callback();
   186   };
   188   req.onerror = function(e) {
   189     ok(true, path + " does not exists: " + e.target.error.name);
   190     addNamed();
   191   };
   192 }
   194 createTestFile("sub1/sub2/test.png", function() {
   195   var promise = gStorage.getRoot();
   196   ok(promise, "Should have a non-null promise for getRoot.");
   197   promise.then(getSuccess, cbError);
   198 });
   200 </script>
   201 </pre>
   202 </body>
   203 </html>

mercurial