dom/devicestorage/test/test_enumerate.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

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 function enumerateSuccess(e) {
michael@0 28
michael@0 29 if (e.target.result == null) {
michael@0 30 ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array")
michael@0 31 dump("We still have length = " + files.length + "\n");
michael@0 32 devicestorage_cleanup();
michael@0 33 return;
michael@0 34 }
michael@0 35
michael@0 36 var filename = e.target.result.name;
michael@0 37 if (filename[0] == "/") {
michael@0 38 // We got /storageName/prefix/filename
michael@0 39 // Remove the storageName (this shows up on FirefoxOS)
michael@0 40 filename = filename.substring(1); // Remove leading slash
michael@0 41 var slashIndex = filename.indexOf("/");
michael@0 42 if (slashIndex >= 0) {
michael@0 43 filename = filename.substring(slashIndex + 1); // Remove storageName
michael@0 44 }
michael@0 45 }
michael@0 46 if (filename.startsWith(prefix)) {
michael@0 47 filename = filename.substring(prefix.length + 1); // Remove prefix
michael@0 48 }
michael@0 49
michael@0 50 var index = files.indexOf(filename);
michael@0 51 files.remove(index);
michael@0 52
michael@0 53 ok(index > -1, "filename should be in the enumeration : " + e.target.result.name);
michael@0 54
michael@0 55 // clean up
michael@0 56 var cleanup = storage.delete(e.target.result.name);
michael@0 57 cleanup.onsuccess = function(e) {} // todo - can i remove this?
michael@0 58
michael@0 59 e.target.continue();
michael@0 60 }
michael@0 61
michael@0 62 function handleError(e) {
michael@0 63 ok(false, "handleError was called : " + e.target.error.name);
michael@0 64 devicestorage_cleanup();
michael@0 65 }
michael@0 66
michael@0 67 function addSuccess(e) {
michael@0 68 addedSoFar = addedSoFar + 1;
michael@0 69 if (addedSoFar == files.length) {
michael@0 70
michael@0 71 var cursor = storage.enumerate(prefix);
michael@0 72 cursor.onsuccess = enumerateSuccess;
michael@0 73 cursor.onerror = handleError;
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 function addError(e) {
michael@0 78 ok(false, "addError was called : " + e.target.error.name);
michael@0 79 devicestorage_cleanup();
michael@0 80 }
michael@0 81
michael@0 82 var storage = navigator.getDeviceStorage("pictures");
michael@0 83 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
michael@0 84 var prefix = "devicestorage/" + randomFilename(12) + ".png"
michael@0 85
michael@0 86 var files = [ "a.PNG", "b.pnG", "c.png", "d/a.png", "d/b.png", "d/c.png", "d/d.png", "The/quick/brown/fox/jumps/over/the/lazy/dog.png"]
michael@0 87 var addedSoFar = 0;
michael@0 88
michael@0 89
michael@0 90 for (var i=0; i<files.length; i++) {
michael@0 91
michael@0 92 request = storage.addNamed(createRandomBlob('image/png'), prefix + '/' + files[i]);
michael@0 93
michael@0 94 ok(request, "Should have a non-null request");
michael@0 95 request.onsuccess = addSuccess;
michael@0 96 request.onerror = addError;
michael@0 97 }
michael@0 98
michael@0 99 </script>
michael@0 100 </pre>
michael@0 101 </body>
michael@0 102 </html>
michael@0 103

mercurial