dom/devicestorage/test/test_fs_createDirectory.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:16df74cd6e52
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 createDirectory 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>
13
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">
23
24 devicestorage_setup();
25
26 // The root directory object.
27 var gRoot;
28 var gTestCount = 0;
29 var gPath = '';
30 var gName = '';
31
32 function testCreateDirectory(rootDir, path) {
33 rootDir.createDirectory(path).then(createDirectorySuccess, cbError);
34 }
35
36 function createDirectorySuccess(d) {
37 ok(d.name === gName, "Failed to create directory: name mismatch.");
38
39 // Get the new created directory from the root.
40 gRoot.get(gPath).then(getSuccess, cbError);
41 }
42
43 function getSuccess(d) {
44 ok(d.name === gName, "Should get directory - " + (gPath || "[root]") + ".");
45 switch (gTestCount) {
46 case 0:
47 gRoot = d;
48 // Create a new directory under the root.
49 gName = gPath = randomFilename(12);
50 testCreateDirectory(gRoot, gName);
51 break;
52 case 1:
53 // Create a sub-directory under current directory.
54 gName = randomFilename(12);
55 gPath = gPath + '/' + gName;
56 testCreateDirectory(d, gName);
57 break;
58 case 2:
59 // Create directory with an existing path.
60 gRoot.createDirectory(gPath).then(function(what) {
61 ok(false, "Should not overwrite an existing directory.");
62 devicestorage_cleanup();
63 }, function(e) {
64 ok(true, "Creating directory should fail if it already exists.");
65
66 // Create a directory whose intermediate directory doesn't exit.
67 gName = randomFilename(12);
68 gPath = 'sub1/sub2/' + gName;
69 testCreateDirectory(gRoot, gPath);
70 });
71 break;
72 default:
73 // Create the parent directory.
74 d.createDirectory('..').then(function(what) {
75 ok(false, "Should not overwrite an existing directory.");
76 devicestorage_cleanup();
77 }, function(e) {
78 ok(true, "Accessing parent directory with '..' is not allowed.");
79 devicestorage_cleanup();
80 });
81 break;
82 }
83 gTestCount++;
84 }
85
86 function cbError(e) {
87 ok(false, e.name + " error should not arrive here!");
88 devicestorage_cleanup();
89 }
90
91 ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
92
93 var storage = navigator.getDeviceStorage("pictures");
94 ok(storage, "Should have gotten a storage.");
95
96 var promise = storage.getRoot();
97 ok(promise, "Should have a non-null promise for getRoot.");
98
99 gName = storage.storageName;
100 promise.then(getSuccess, cbError);
101 </script>
102 </pre>
103 </body>
104 </html>
105

mercurial