dom/devicestorage/test/test_fs_get.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial