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=678695 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug 678695 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=678695">Mozilla Bug 678695</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"> |
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-write", true, document); |
michael@0 | 29 | SpecialPowers.addPermission("settings-read", true, document); |
michael@0 | 30 | |
michael@0 | 31 | var screenBright = {"screen.brightness": 0.7}; |
michael@0 | 32 | |
michael@0 | 33 | function onFailure() { |
michael@0 | 34 | ok(false, "in on Failure!"); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function observer1(setting) { |
michael@0 | 38 | dump("observer 1 called!\n"); |
michael@0 | 39 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 40 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | function observer2(setting) { |
michael@0 | 44 | dump("observer 2 called!\n"); |
michael@0 | 45 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 46 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | var calls = 0; |
michael@0 | 50 | function observerOnlyCalledOnce(setting) { |
michael@0 | 51 | is(++calls, 1, "Observer only called once!"); |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | function observerWithNext(setting) { |
michael@0 | 56 | dump("observer with next called!\n"); |
michael@0 | 57 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 58 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 59 | next(); |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | function onsettingschangeWithNext(event) { |
michael@0 | 63 | dump("onsettingschangewithnext called!\n"); |
michael@0 | 64 | is(event.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 65 | is(event.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 66 | next(); |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | var cset = {'a':'b','c':[{'d':'e'}]}; |
michael@0 | 70 | |
michael@0 | 71 | function onComplexSettingschangeWithNext(event) { |
michael@0 | 72 | is(event.settingName, "test.key", "Same settingName"); |
michael@0 | 73 | is(event.settingValue['a'], "b", "Same settingvalue"); |
michael@0 | 74 | var c = event.settingValue['c']; |
michael@0 | 75 | ok(Array.isArray(c), "c is array!"); |
michael@0 | 76 | is(c[0]['d'], 'e', "Right settingValue!"); |
michael@0 | 77 | next(); |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | var req, req2; |
michael@0 | 81 | var index = 0; |
michael@0 | 82 | |
michael@0 | 83 | var mozSettings = window.navigator.mozSettings; |
michael@0 | 84 | |
michael@0 | 85 | var steps = [ |
michael@0 | 86 | function () { |
michael@0 | 87 | ok(true, "Deleting database"); |
michael@0 | 88 | var lock = mozSettings.createLock(); |
michael@0 | 89 | req = lock.clear(); |
michael@0 | 90 | req.onsuccess = function () { |
michael@0 | 91 | ok(true, "Deleted the database"); |
michael@0 | 92 | }; |
michael@0 | 93 | req.onerror = onFailure; |
michael@0 | 94 | req2 = lock.set(screenBright); |
michael@0 | 95 | req2.onsuccess = function () { |
michael@0 | 96 | ok(true, "set done"); |
michael@0 | 97 | navigator.mozSettings.onsettingchange = onsettingschangeWithNext; |
michael@0 | 98 | next(); |
michael@0 | 99 | } |
michael@0 | 100 | req2.onerror = onFailure; |
michael@0 | 101 | }, |
michael@0 | 102 | function() { |
michael@0 | 103 | ok(true, "testing"); |
michael@0 | 104 | var lock = mozSettings.createLock(); |
michael@0 | 105 | req2 = lock.set(screenBright); |
michael@0 | 106 | req2.onsuccess = function() { |
michael@0 | 107 | ok(true, "end adding onsettingchange"); |
michael@0 | 108 | }; |
michael@0 | 109 | req2.onerror = onFailure; |
michael@0 | 110 | }, |
michael@0 | 111 | function() { |
michael@0 | 112 | ok(true, "test observers"); |
michael@0 | 113 | var lock = mozSettings.createLock(); |
michael@0 | 114 | req = lock.get("screen.brightness"); |
michael@0 | 115 | req.onsuccess = function () { |
michael@0 | 116 | ok(true, "get done"); |
michael@0 | 117 | next(); |
michael@0 | 118 | } |
michael@0 | 119 | req.onerror = onFailure; |
michael@0 | 120 | }, |
michael@0 | 121 | function() { |
michael@0 | 122 | ok(true, "adding Observers 1"); |
michael@0 | 123 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 124 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 125 | navigator.mozSettings.addObserver("screen.brightness", observer2); |
michael@0 | 126 | navigator.mozSettings.addObserver("screen.brightness", observerOnlyCalledOnce); |
michael@0 | 127 | var lock = mozSettings.createLock(); |
michael@0 | 128 | req2 = lock.get("screen.brightness"); |
michael@0 | 129 | req2.onsuccess = function() { |
michael@0 | 130 | ok(true, "set observeSetting done!"); |
michael@0 | 131 | next(); |
michael@0 | 132 | }; |
michael@0 | 133 | req2.onerror = onFailure; |
michael@0 | 134 | }, |
michael@0 | 135 | function() { |
michael@0 | 136 | ok(true, "test observers"); |
michael@0 | 137 | var lock = mozSettings.createLock(); |
michael@0 | 138 | req = lock.set(screenBright); |
michael@0 | 139 | req.onsuccess = function () { |
michael@0 | 140 | ok(true, "set1 done"); |
michael@0 | 141 | } |
michael@0 | 142 | req.onerror = onFailure; |
michael@0 | 143 | }, |
michael@0 | 144 | function() { |
michael@0 | 145 | ok(true, "test observers"); |
michael@0 | 146 | var lock = mozSettings.createLock(); |
michael@0 | 147 | req = lock.get("screen.brightness"); |
michael@0 | 148 | navigator.mozSettings.removeObserver("screen.brightness", observerOnlyCalledOnce); |
michael@0 | 149 | req.onsuccess = function () { |
michael@0 | 150 | ok(true, "set1 done"); |
michael@0 | 151 | } |
michael@0 | 152 | req.onerror = onFailure; |
michael@0 | 153 | }, |
michael@0 | 154 | function() { |
michael@0 | 155 | ok(true, "removing Event Listener"); |
michael@0 | 156 | var lock = mozSettings.createLock(); |
michael@0 | 157 | req = lock.set(screenBright); |
michael@0 | 158 | req.onsuccess = function () { |
michael@0 | 159 | ok(true, "set2 done"); |
michael@0 | 160 | navigator.mozSettings.removeObserver("screen.brightness", observer2); |
michael@0 | 161 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 162 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 163 | } |
michael@0 | 164 | req.onerror = onFailure; |
michael@0 | 165 | }, |
michael@0 | 166 | |
michael@0 | 167 | function() { |
michael@0 | 168 | ok(true, "delete onsettingschange"); |
michael@0 | 169 | var lock = mozSettings.createLock(); |
michael@0 | 170 | navigator.mozSettings.onsettingchange = null; |
michael@0 | 171 | req = lock.set(screenBright); |
michael@0 | 172 | req.onsuccess = function () { |
michael@0 | 173 | ok(true, "set0 done"); |
michael@0 | 174 | next(); |
michael@0 | 175 | } |
michael@0 | 176 | req.onerror = onFailure; |
michael@0 | 177 | }, |
michael@0 | 178 | function () { |
michael@0 | 179 | ok(true, "Waiting for all set callbacks"); |
michael@0 | 180 | var lock = mozSettings.createLock(); |
michael@0 | 181 | req = lock.get("screen.brightness"); |
michael@0 | 182 | req.onsuccess = function() { |
michael@0 | 183 | ok(true, "Done"); |
michael@0 | 184 | next(); |
michael@0 | 185 | } |
michael@0 | 186 | req.onerror = onFailure; |
michael@0 | 187 | }, |
michael@0 | 188 | function() { |
michael@0 | 189 | ok(true, "adding Observers 1"); |
michael@0 | 190 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 191 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 192 | navigator.mozSettings.addObserver("screen.brightness", observer2); |
michael@0 | 193 | navigator.mozSettings.addObserver("screen.brightness", observerWithNext); |
michael@0 | 194 | var lock = mozSettings.createLock(); |
michael@0 | 195 | req2 = lock.get("screen.brightness"); |
michael@0 | 196 | req2.onsuccess = function() { |
michael@0 | 197 | ok(true, "set observeSetting done!"); |
michael@0 | 198 | next(); |
michael@0 | 199 | }; |
michael@0 | 200 | req2.onerror = onFailure; |
michael@0 | 201 | }, |
michael@0 | 202 | function() { |
michael@0 | 203 | ok(true, "test observers"); |
michael@0 | 204 | var lock = mozSettings.createLock(); |
michael@0 | 205 | req = lock.set(screenBright); |
michael@0 | 206 | req.onsuccess = function () { |
michael@0 | 207 | ok(true, "set1 done"); |
michael@0 | 208 | } |
michael@0 | 209 | req.onerror = onFailure; |
michael@0 | 210 | }, |
michael@0 | 211 | function() { |
michael@0 | 212 | ok(true, "removing Event Listener"); |
michael@0 | 213 | var lock = mozSettings.createLock(); |
michael@0 | 214 | req = lock.set(screenBright); |
michael@0 | 215 | req.onsuccess = function () { |
michael@0 | 216 | ok(true, "set2 done"); |
michael@0 | 217 | navigator.mozSettings.removeObserver("screen.brightness", observer2); |
michael@0 | 218 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 219 | } |
michael@0 | 220 | req.onerror = onFailure; |
michael@0 | 221 | }, |
michael@0 | 222 | function() { |
michael@0 | 223 | ok(true, "test Event Listener"); |
michael@0 | 224 | var lock = mozSettings.createLock(); |
michael@0 | 225 | req = lock.set(screenBright); |
michael@0 | 226 | req.onsuccess = function () { |
michael@0 | 227 | ok(true, "set3 done"); |
michael@0 | 228 | } |
michael@0 | 229 | req.onerror = onFailure; |
michael@0 | 230 | }, |
michael@0 | 231 | function() { |
michael@0 | 232 | ok(true, "removing Event Listener"); |
michael@0 | 233 | var lock = mozSettings.createLock(); |
michael@0 | 234 | navigator.mozSettings.removeObserver("screen.brightness", observerWithNext); |
michael@0 | 235 | req = lock.set(screenBright); |
michael@0 | 236 | req.onsuccess = function () { |
michael@0 | 237 | ok(true, "set4 done"); |
michael@0 | 238 | navigator.mozSettings.removeObserver("screen.brightness", observer2); |
michael@0 | 239 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 240 | next(); |
michael@0 | 241 | } |
michael@0 | 242 | req.onerror = onFailure; |
michael@0 | 243 | }, |
michael@0 | 244 | function() { |
michael@0 | 245 | ok(true, "removing Event Listener"); |
michael@0 | 246 | var lock = mozSettings.createLock(); |
michael@0 | 247 | req = lock.get("screen.brightness"); |
michael@0 | 248 | req.onsuccess = function () { |
michael@0 | 249 | ok(true, "get5 done"); |
michael@0 | 250 | next(); |
michael@0 | 251 | } |
michael@0 | 252 | req.onerror = onFailure; |
michael@0 | 253 | }, |
michael@0 | 254 | function() { |
michael@0 | 255 | ok(true, "Clear DB"); |
michael@0 | 256 | var lock = mozSettings.createLock(); |
michael@0 | 257 | req = lock.clear(); |
michael@0 | 258 | req.onsuccess = function () { |
michael@0 | 259 | ok(true, "Deleted the database"); |
michael@0 | 260 | next(); |
michael@0 | 261 | }; |
michael@0 | 262 | req.onerror = onFailure; |
michael@0 | 263 | }, |
michael@0 | 264 | function () { |
michael@0 | 265 | ok(true, "Deleting database"); |
michael@0 | 266 | var lock = mozSettings.createLock(); |
michael@0 | 267 | req = lock.clear(); |
michael@0 | 268 | req.onsuccess = function () { |
michael@0 | 269 | ok(true, "Deleted the database"); |
michael@0 | 270 | navigator.mozSettings.onsettingchange = onComplexSettingschangeWithNext; |
michael@0 | 271 | }; |
michael@0 | 272 | req.onerror = onFailure; |
michael@0 | 273 | req2 = navigator.mozSettings.createLock().set({'test.key': cset}); |
michael@0 | 274 | req2.onsuccess = function () { |
michael@0 | 275 | ok(true, "set done"); |
michael@0 | 276 | } |
michael@0 | 277 | req2.onerror = onFailure; |
michael@0 | 278 | }, |
michael@0 | 279 | |
michael@0 | 280 | function () { |
michael@0 | 281 | ok(true, "all done!\n"); |
michael@0 | 282 | SimpleTest.finish(); |
michael@0 | 283 | } |
michael@0 | 284 | ]; |
michael@0 | 285 | |
michael@0 | 286 | function next() { |
michael@0 | 287 | ok(true, "Begin!"); |
michael@0 | 288 | if (index >= steps.length) { |
michael@0 | 289 | ok(false, "Shouldn't get here!"); |
michael@0 | 290 | return; |
michael@0 | 291 | } |
michael@0 | 292 | try { |
michael@0 | 293 | steps[index](); |
michael@0 | 294 | } catch(ex) { |
michael@0 | 295 | ok(false, "Caught exception", ex); |
michael@0 | 296 | } |
michael@0 | 297 | index += 1; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 301 | addLoadEvent(next); |
michael@0 | 302 | </script> |
michael@0 | 303 | </pre> |
michael@0 | 304 | </body> |
michael@0 | 305 | </html> |