dom/settings/tests/test_settings_blobs.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 <!DOCTYPE html>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=821630
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 821630 Settings API</title>
michael@0 8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
michael@0 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 11 </head>
michael@0 12 <body>
michael@0 13
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=821630">Mozilla Bug 821630</a>
michael@0 15 <p id="display"></p>
michael@0 16 <div id="content" style="display: none">
michael@0 17
michael@0 18 </div>
michael@0 19 <pre id="test">
michael@0 20 <script class="testbody" type="text/javascript;version=1.7">
michael@0 21
michael@0 22 "use strict";
michael@0 23
michael@0 24 if (SpecialPowers.isMainProcess()) {
michael@0 25 SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm");
michael@0 26 }
michael@0 27
michael@0 28 SpecialPowers.addPermission("settings-read", true, document);
michael@0 29 SpecialPowers.addPermission("settings-write", true, document);
michael@0 30
michael@0 31 function onUnwantedSuccess() {
michael@0 32 ok(false, "onUnwantedSuccess: shouldn't get here");
michael@0 33 }
michael@0 34
michael@0 35 function onFailure() {
michael@0 36 return function(s) {
michael@0 37 if (s) {
michael@0 38 ok(false, "in on Failure! - " + s);
michael@0 39 } else {
michael@0 40 ok(false, "in on Failure!");
michael@0 41 }
michael@0 42 }
michael@0 43 }
michael@0 44
michael@0 45 let mozSettings = window.navigator.mozSettings;
michael@0 46 let req;
michael@0 47
michael@0 48 let storedBlob = new Blob([""], {"type": "text/xml"});
michael@0 49
michael@0 50 function checkBlob(blob) {
michael@0 51 try {
michael@0 52 let url = URL.createObjectURL(blob);
michael@0 53 ok(true, "Valid blob");
michael@0 54 } catch (e) {
michael@0 55 ok(false, "Valid blob");
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 let steps = [
michael@0 60 function() {
michael@0 61 let lock = mozSettings.createLock();
michael@0 62 req = lock.clear();
michael@0 63 req.onsuccess = next;
michael@0 64 req.onerror = onFailure("Deleting database");
michael@0 65 },
michael@0 66 function() {
michael@0 67 function obs(e) {
michael@0 68 checkBlob(e.settingValue);
michael@0 69 mozSettings.removeObserver("test1", obs);
michael@0 70 next();
michael@0 71 }
michael@0 72 mozSettings.addObserver("test1", obs);
michael@0 73 next();
michael@0 74 },
michael@0 75 function() {
michael@0 76 // next is called by the observer above
michael@0 77 let req = mozSettings.createLock().set({"test1": storedBlob});
michael@0 78 req.onerror = onFailure("Saving blob");
michael@0 79 },
michael@0 80 function() {
michael@0 81 let req = mozSettings.createLock().get("test1");
michael@0 82 req.onsuccess = function(event) {
michael@0 83 checkBlob(event.target.result["test1"]);
michael@0 84 next();
michael@0 85 };
michael@0 86 req.onerror = onFailure("Getting blob");
michael@0 87 },
michael@0 88 function() {
michael@0 89 let req = mozSettings.createLock().set({"test2": [1, 2, storedBlob, 4]});
michael@0 90 req.onsuccess = next;
michael@0 91 req.onerror = onFailure("Saving array");
michael@0 92 },
michael@0 93 function() {
michael@0 94 let req = mozSettings.createLock().get("test2");
michael@0 95 req.onsuccess = function(event) {
michael@0 96 let val = event.target.result["test2"];
michael@0 97 ok(Array.isArray(val), "Result is an array");
michael@0 98 ok(val[0] == 1 && val[1] == 2 && val[3] == 4, "Primitives are preserved");
michael@0 99 checkBlob(val[2]);
michael@0 100 next();
michael@0 101 };
michael@0 102 req.onerror = onFailure("Getting array");
michael@0 103 },
michael@0 104 function() {
michael@0 105 let req = mozSettings.createLock().set({"test3": {foo: "bar", baz: {number: 1, arr: [storedBlob]}}});
michael@0 106 req.onsuccess = next();
michael@0 107 req.onerror = onFailure("Saving object");
michael@0 108 },
michael@0 109 function() {
michael@0 110 let req = mozSettings.createLock().get("test3");
michael@0 111 req.onsuccess = function(event) {
michael@0 112 let val = event.target.result["test3"];
michael@0 113 ok(typeof(val) == "object", "Result is an object");
michael@0 114 ok("foo" in val && typeof(val.foo) == "string", "String property preserved");
michael@0 115 ok("baz" in val && typeof(val.baz) == "object", "Object property preserved");
michael@0 116 let baz = val.baz;
michael@0 117 ok("number" in baz && baz.number == 1, "Primite inside object preserved");
michael@0 118 ok("arr" in baz && Array.isArray(baz.arr), "Array inside object is preserved");
michael@0 119 checkBlob(baz.arr[0]);
michael@0 120 next();
michael@0 121 };
michael@0 122 req.onerror = onFailure("Getting object");
michael@0 123 },
michael@0 124 function() {
michael@0 125 let req = mozSettings.createLock().clear();
michael@0 126 req.onsuccess = function() {
michael@0 127 next();
michael@0 128 };
michael@0 129 req.onerror = onFailure("Deleting database");
michael@0 130 },
michael@0 131 function () {
michael@0 132 ok(true, "all done!\n");
michael@0 133 SimpleTest.finish();
michael@0 134 }
michael@0 135 ];
michael@0 136
michael@0 137 function next() {
michael@0 138 try {
michael@0 139 let step = steps.shift();
michael@0 140 if (step) {
michael@0 141 step();
michael@0 142 }
michael@0 143 } catch(ex) {
michael@0 144 ok(false, "Caught exception", ex);
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 SimpleTest.waitForExplicitFinish();
michael@0 149 addLoadEvent(next);
michael@0 150 </script>
michael@0 151 </pre>
michael@0 152 </body>
michael@0 153 </html>

mercurial