Thu, 22 Jan 2015 13:21:57 +0100
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=806374 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug 806374 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 | // A simple data URI that will be converted to a blob. |
michael@0 | 49 | let dataURI = "data:text/html;charset=utf-8,%3C!DOCTYPE html>%3Cbody style='background:black;"; |
michael@0 | 50 | |
michael@0 | 51 | function checkBlob(blob) { |
michael@0 | 52 | try { |
michael@0 | 53 | let url = URL.createObjectURL(blob); |
michael@0 | 54 | ok(true, "Valid blob"); |
michael@0 | 55 | } catch (e) { |
michael@0 | 56 | ok(false, "Valid blob"); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | let steps = [ |
michael@0 | 61 | function() { |
michael@0 | 62 | let lock = mozSettings.createLock(); |
michael@0 | 63 | req = lock.clear(); |
michael@0 | 64 | req.onsuccess = next; |
michael@0 | 65 | req.onerror = onFailure("Deleting database"); |
michael@0 | 66 | }, |
michael@0 | 67 | function() { |
michael@0 | 68 | function obs(e) { |
michael@0 | 69 | checkBlob(e.settingValue); |
michael@0 | 70 | mozSettings.removeObserver("test1", obs); |
michael@0 | 71 | next(); |
michael@0 | 72 | } |
michael@0 | 73 | mozSettings.addObserver("test1", obs); |
michael@0 | 74 | next(); |
michael@0 | 75 | }, |
michael@0 | 76 | function() { |
michael@0 | 77 | // next is called by the observer above |
michael@0 | 78 | let req = mozSettings.createLock().set({"test1": dataURI}); |
michael@0 | 79 | req.onerror = onFailure("Saving blob"); |
michael@0 | 80 | }, |
michael@0 | 81 | function() { |
michael@0 | 82 | let req = mozSettings.createLock().get("test1"); |
michael@0 | 83 | req.onsuccess = function(event) { |
michael@0 | 84 | checkBlob(event.target.result["test1"]); |
michael@0 | 85 | next(); |
michael@0 | 86 | }; |
michael@0 | 87 | req.onerror = onFailure("Getting blob"); |
michael@0 | 88 | }, |
michael@0 | 89 | function() { |
michael@0 | 90 | let req = mozSettings.createLock().set({"test2": [1, 2, dataURI, 4]}); |
michael@0 | 91 | req.onsuccess = next; |
michael@0 | 92 | req.onerror = onFailure("Saving array"); |
michael@0 | 93 | }, |
michael@0 | 94 | function() { |
michael@0 | 95 | let req = mozSettings.createLock().get("test2"); |
michael@0 | 96 | req.onsuccess = function(event) { |
michael@0 | 97 | let val = event.target.result["test2"]; |
michael@0 | 98 | ok(Array.isArray(val), "Result is an array"); |
michael@0 | 99 | ok(val[0] == 1 && val[1] == 2 && val[3] == 4, "Primitives are preserved"); |
michael@0 | 100 | checkBlob(val[2]); |
michael@0 | 101 | next(); |
michael@0 | 102 | }; |
michael@0 | 103 | req.onerror = onFailure("Getting array"); |
michael@0 | 104 | }, |
michael@0 | 105 | function() { |
michael@0 | 106 | let req = mozSettings.createLock().set({"test3": {foo: "bar", baz: {number: 1, arr: [dataURI]}}}); |
michael@0 | 107 | req.onsuccess = next(); |
michael@0 | 108 | req.onerror = onFailure("Saving object"); |
michael@0 | 109 | }, |
michael@0 | 110 | function() { |
michael@0 | 111 | let req = mozSettings.createLock().get("test3"); |
michael@0 | 112 | req.onsuccess = function(event) { |
michael@0 | 113 | let val = event.target.result["test3"]; |
michael@0 | 114 | ok(typeof(val) == "object", "Result is an object"); |
michael@0 | 115 | ok("foo" in val && typeof(val.foo) == "string", "String property preserved"); |
michael@0 | 116 | ok("baz" in val && typeof(val.baz) == "object", "Object property preserved"); |
michael@0 | 117 | let baz = val.baz; |
michael@0 | 118 | ok("number" in baz && baz.number == 1, "Primite inside object preserved"); |
michael@0 | 119 | ok("arr" in baz && Array.isArray(baz.arr), "Array inside object is preserved"); |
michael@0 | 120 | checkBlob(baz.arr[0]); |
michael@0 | 121 | next(); |
michael@0 | 122 | }; |
michael@0 | 123 | req.onerror = onFailure("Getting object"); |
michael@0 | 124 | }, |
michael@0 | 125 | function() { |
michael@0 | 126 | let req = mozSettings.createLock().clear(); |
michael@0 | 127 | req.onsuccess = function() { |
michael@0 | 128 | next(); |
michael@0 | 129 | }; |
michael@0 | 130 | req.onerror = onFailure("Deleting database"); |
michael@0 | 131 | }, |
michael@0 | 132 | function () { |
michael@0 | 133 | ok(true, "all done!\n"); |
michael@0 | 134 | SimpleTest.finish(); |
michael@0 | 135 | } |
michael@0 | 136 | ]; |
michael@0 | 137 | |
michael@0 | 138 | function next() { |
michael@0 | 139 | try { |
michael@0 | 140 | let step = steps.shift(); |
michael@0 | 141 | if (step) { |
michael@0 | 142 | step(); |
michael@0 | 143 | } |
michael@0 | 144 | } catch(ex) { |
michael@0 | 145 | ok(false, "Caught exception", ex); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 150 | addLoadEvent(next); |
michael@0 | 151 | </script> |
michael@0 | 152 | </pre> |
michael@0 | 153 | </body> |
michael@0 | 154 | </html> |