dom/devicestorage/test/test_overrideDir.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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=717103
michael@0 8 -->
michael@0 9 <head>
michael@0 10 <title>Test for the device storage API </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=717103">Mozilla Bug 717103</a>
michael@0 18 <p id="display"></p>
michael@0 19 <div id="content" style="display: none">
michael@0 20
michael@0 21 </div>
michael@0 22 <pre id="test">
michael@0 23 <script class="testbody" type="text/javascript">
michael@0 24
michael@0 25 devicestorage_setup();
michael@0 26
michael@0 27 var gFileName = "devicestorage/" + randomFilename(12) + "/hi.png";
michael@0 28 var gData = "My name is Doug Turner. My IRC nick is DougT. I like Maple cookies."
michael@0 29 var gDataBlob = new Blob([gData], {type: 'image/png'});
michael@0 30 var gFileReader = new FileReader();
michael@0 31
michael@0 32 function getAfterDeleteSuccess(e) {
michael@0 33 ok(false, "file was deleted not successfully");
michael@0 34 devicestorage_cleanup();
michael@0 35 }
michael@0 36
michael@0 37 function getAfterDeleteError(e) {
michael@0 38 ok(true, "file was deleted successfully");
michael@0 39 devicestorage_cleanup();
michael@0 40 }
michael@0 41
michael@0 42 function deleteSuccess(e) {
michael@0 43
michael@0 44 ok(e.target.result == gFileName, "File name should match");
michael@0 45 dump(e.target.result + "\n")
michael@0 46
michael@0 47 // File was deleted using the sdcard stoage area. It should be gone
michael@0 48 // from the pictures as well.
michael@0 49 var storage = navigator.getDeviceStorage("pictures");
michael@0 50 request = storage.get(e.target.result);
michael@0 51 request.onsuccess = getAfterDeleteSuccess;
michael@0 52 request.onerror = getAfterDeleteError;
michael@0 53 }
michael@0 54
michael@0 55 function deleteError(e) {
michael@0 56 ok(false, "deleteError was called : " + e.target.error.name);
michael@0 57 devicestorage_cleanup();
michael@0 58 }
michael@0 59
michael@0 60 function getSuccess(e) {
michael@0 61 // We wrote the file out using pictures type. Since we've over-ridden the
michael@0 62 // root directory, we should be able to read it back using the sdcard
michael@0 63 // storage area.
michael@0 64 var storage = navigator.getDeviceStorage("sdcard");
michael@0 65 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
michael@0 66
michael@0 67 ok(e.target.result.name == gFileName, "File name should match");
michael@0 68 ok(e.target.result.size > 0, "File size be greater than zero");
michael@0 69 ok(e.target.result.type, "File should have a mime type");
michael@0 70 ok(e.target.result.lastModifiedDate, "File should have a last modified date");
michael@0 71
michael@0 72 var name = e.target.result.name;
michael@0 73
michael@0 74 gFileReader.readAsArrayBuffer(gDataBlob);
michael@0 75 gFileReader.onload = function(e) {
michael@0 76 readerCallback(e);
michael@0 77
michael@0 78 request = storage.delete(name)
michael@0 79 request.onsuccess = deleteSuccess;
michael@0 80 request.onerror = deleteError;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 function readerCallback(e) {
michael@0 85
michael@0 86 ab = e.target.result;
michael@0 87
michael@0 88 is(ab.byteLength, gData.length, "wrong arraybuffer byteLength");
michael@0 89 var u8v = new Uint8Array(ab);
michael@0 90 is(String.fromCharCode.apply(String, u8v), gData, "wrong values");
michael@0 91 }
michael@0 92
michael@0 93 function getError(e) {
michael@0 94 ok(false, "getError was called : " + e.target.error.name);
michael@0 95 devicestorage_cleanup();
michael@0 96 }
michael@0 97
michael@0 98 function addSuccess(e) {
michael@0 99
michael@0 100 var filename = e.target.result;
michael@0 101 if (filename[0] == "/") {
michael@0 102 // We got /storageName/prefix/filename
michael@0 103 // Remove the storageName (this shows up on FirefoxOS)
michael@0 104 filename = filename.substring(1); // Remove leading slash
michael@0 105 var slashIndex = filename.indexOf("/");
michael@0 106 if (slashIndex >= 0) {
michael@0 107 filename = filename.substring(slashIndex + 1); // Remove storageName
michael@0 108 }
michael@0 109 }
michael@0 110 ok(filename == gFileName, "File name should match");
michael@0 111
michael@0 112 // Update gFileName to be the fully qualified name so that
michael@0 113 // further checks will pass.
michael@0 114 gFileName = e.target.result;
michael@0 115
michael@0 116 var storage = navigator.getDeviceStorage("pictures");
michael@0 117 request = storage.get(gFileName);
michael@0 118 request.onsuccess = getSuccess;
michael@0 119 request.onerror = getError;
michael@0 120
michael@0 121 ok(true, "addSuccess was called");
michael@0 122 }
michael@0 123
michael@0 124 function addError(e) {
michael@0 125 ok(false, "addError was called : " + e.target.error.name);
michael@0 126 devicestorage_cleanup();
michael@0 127 }
michael@0 128
michael@0 129 function startTest() {
michael@0 130 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
michael@0 131
michael@0 132 var storage = navigator.getDeviceStorage("pictures");
michael@0 133 ok(storage, "Should have gotten a storage");
michael@0 134
michael@0 135 request = storage.addNamed(gDataBlob, gFileName);
michael@0 136 ok(request, "Should have a non-null request");
michael@0 137
michael@0 138 request.onsuccess = addSuccess;
michael@0 139 request.onerror = addError;
michael@0 140 }
michael@0 141
michael@0 142 try {
michael@0 143 const Cc = SpecialPowers.Cc;
michael@0 144 const Ci = SpecialPowers.Ci;
michael@0 145 var directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
michael@0 146 var f = directoryService.get("TmpD", Ci.nsIFile);
michael@0 147 f.appendRelativePath("device-storage-sdcard");
michael@0 148 try {
michael@0 149 // The remove will fail if the directory doesn't exist, which is fine.
michael@0 150 f.remove(true);
michael@0 151 } catch (e) {}
michael@0 152 SpecialPowers.pushPrefEnv({'set': [["device.storage.overrideRootDir", f.path],
michael@0 153 ["device.storage.testing", false]]},
michael@0 154 function() {
michael@0 155 startTest();
michael@0 156 });
michael@0 157 } catch(e) {}
michael@0 158
michael@0 159 </script>
michael@0 160 </pre>
michael@0 161 </body>
michael@0 162 </html>
michael@0 163

mercurial