dom/devicestorage/test/test_basic.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 var storage = navigator.getDeviceStorage("pictures");
michael@0 48 request = storage.get(e.target.result);
michael@0 49 request.onsuccess = getAfterDeleteSuccess;
michael@0 50 request.onerror = getAfterDeleteError;
michael@0 51
michael@0 52 }
michael@0 53
michael@0 54 function deleteError(e) {
michael@0 55 ok(false, "deleteError was called : " + e.target.error.name);
michael@0 56 devicestorage_cleanup();
michael@0 57 }
michael@0 58
michael@0 59 function getSuccess(e) {
michael@0 60 var storage = navigator.getDeviceStorage("pictures");
michael@0 61 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
michael@0 62
michael@0 63 ok(e.target.result.name == gFileName, "File name should match");
michael@0 64 ok(e.target.result.size > 0, "File size be greater than zero");
michael@0 65 ok(e.target.result.type, "File should have a mime type");
michael@0 66 ok(e.target.result.lastModifiedDate, "File should have a last modified date");
michael@0 67
michael@0 68 var name = e.target.result.name;
michael@0 69
michael@0 70 gFileReader.readAsArrayBuffer(gDataBlob);
michael@0 71 gFileReader.onload = function(e) {
michael@0 72 readerCallback(e);
michael@0 73
michael@0 74 request = storage.delete(name)
michael@0 75 request.onsuccess = deleteSuccess;
michael@0 76 request.onerror = deleteError;
michael@0 77 }
michael@0 78 }
michael@0 79
michael@0 80 function readerCallback(e) {
michael@0 81
michael@0 82 ab = e.target.result;
michael@0 83
michael@0 84 is(ab.byteLength, gData.length, "wrong arraybuffer byteLength");
michael@0 85 var u8v = new Uint8Array(ab);
michael@0 86 is(String.fromCharCode.apply(String, u8v), gData, "wrong values");
michael@0 87 }
michael@0 88
michael@0 89 function getError(e) {
michael@0 90 ok(false, "getError was called : " + e.target.error.name);
michael@0 91 devicestorage_cleanup();
michael@0 92 }
michael@0 93
michael@0 94 function addSuccess(e) {
michael@0 95
michael@0 96 var filename = e.target.result;
michael@0 97 if (filename[0] == "/") {
michael@0 98 // We got /storageName/prefix/filename
michael@0 99 // Remove the storageName (this shows up on FirefoxOS)
michael@0 100 filename = filename.substring(1); // Remove leading slash
michael@0 101 var slashIndex = filename.indexOf("/");
michael@0 102 if (slashIndex >= 0) {
michael@0 103 filename = filename.substring(slashIndex + 1); // Remove storageName
michael@0 104 }
michael@0 105 }
michael@0 106 ok(filename == gFileName, "File name should match");
michael@0 107
michael@0 108 // Update gFileName to be the fully qualified name so that
michael@0 109 // further checks will pass.
michael@0 110 gFileName = e.target.result;
michael@0 111
michael@0 112 var storage = navigator.getDeviceStorage("pictures");
michael@0 113 request = storage.get(gFileName);
michael@0 114 request.onsuccess = getSuccess;
michael@0 115 request.onerror = getError;
michael@0 116
michael@0 117 ok(true, "addSuccess was called");
michael@0 118 }
michael@0 119
michael@0 120 function addError(e) {
michael@0 121 ok(false, "addError was called : " + e.target.error.name);
michael@0 122 devicestorage_cleanup();
michael@0 123 }
michael@0 124
michael@0 125 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
michael@0 126
michael@0 127 var storage = navigator.getDeviceStorage("pictures");
michael@0 128 ok(storage, "Should have gotten a storage");
michael@0 129
michael@0 130 request = storage.addNamed(gDataBlob, gFileName);
michael@0 131 ok(request, "Should have a non-null request");
michael@0 132
michael@0 133 request.onsuccess = addSuccess;
michael@0 134 request.onerror = addError;
michael@0 135
michael@0 136 </script>
michael@0 137 </pre>
michael@0 138 </body>
michael@0 139 </html>
michael@0 140

mercurial