gecko/dom/settings/tests/test_settings_onsettingchange.html

289 lines
8.0 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={678695}
-->
<head>
<title>Test for Bug {678695} Settings API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={678695}">Mozilla Bug {678695}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm");
SpecialPowers.setBoolPref("dom.mozSettings.enabled", true);
SpecialPowers.addPermission("settings-write", true, document);
SpecialPowers.addPermission("settings-read", true, document);
var screenBright = {"screen.brightness": 0.7};
function onFailure() {
ok(false, "in on Failure!");
}
function observer1(setting) {
dump("observer 1 called!\n");
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
};
function observer2(setting) {
dump("observer 2 called!\n");
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
};
var calls = 0;
function observerOnlyCalledOnce(setting) {
is(++calls, 1, "Observer only called once!");
};
function observerWithNext(setting) {
dump("observer with next called!\n");
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
next();
};
function onsettingschangeWithNext(event) {
dump("onsettingschangewithnext called!\n");
is(event.settingName, "screen.brightness", "Same settingName");
is(event.settingValue, "0.7", "Same settingvalue");
next();
};
var req, req2;
var index = 0;
var mozSettings = window.navigator.mozSettings;
var steps = [
function () {
ok(true, "Deleting database");
var lock = mozSettings.createLock();
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
};
req.onerror = onFailure;
req2 = lock.set(screenBright);
req2.onsuccess = function () {
ok(true, "set done");
navigator.mozSettings.onsettingchange = onsettingschangeWithNext;
next();
}
req2.onerror = onFailure;
},
function() {
ok(true, "testing");
var lock = mozSettings.createLock();
req2 = lock.set(screenBright);
req2.onsuccess = function() {
ok(true, "end adding onsettingchange");
};
req2.onerror = onFailure;
},
function() {
ok(true, "test observers");
var lock = mozSettings.createLock();
req = lock.get("screen.brightness");
req.onsuccess = function () {
ok(true, "get done");
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "adding Observers 1");
navigator.mozSettings.addObserver("screen.brightness", observer1);
navigator.mozSettings.addObserver("screen.brightness", observer1);
navigator.mozSettings.addObserver("screen.brightness", observer2);
navigator.mozSettings.addObserver("screen.brightness", observerOnlyCalledOnce);
var lock = mozSettings.createLock();
req2 = lock.get("screen.brightness");
req2.onsuccess = function() {
ok(true, "set observeSetting done!");
next();
};
req2.onerror = onFailure;
},
function() {
ok(true, "test observers");
var lock = mozSettings.createLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set1 done");
}
req.onerror = onFailure;
},
function() {
ok(true, "test observers");
var lock = mozSettings.createLock();
req = lock.get("screen.brightness");
navigator.mozSettings.removeObserver("screen.brightness", observerOnlyCalledOnce);
req.onsuccess = function () {
ok(true, "set1 done");
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.createLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set2 done");
navigator.mozSettings.removeObserver("screen.brightness", observer2);
navigator.mozSettings.removeObserver("screen.brightness", observer1);
navigator.mozSettings.removeObserver("screen.brightness", observer1);
}
req.onerror = onFailure;
},
function() {
ok(true, "delete onsettingschange");
var lock = mozSettings.createLock();
navigator.mozSettings.onsettingchange = null;
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set0 done");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Waiting for all set callbacks");
var lock = mozSettings.createLock();
req = lock.get("screen.brightness");
req.onsuccess = function() {
ok(true, "Done");
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "adding Observers 1");
navigator.mozSettings.addObserver("screen.brightness", observer1);
navigator.mozSettings.addObserver("screen.brightness", observer1);
navigator.mozSettings.addObserver("screen.brightness", observer2);
navigator.mozSettings.addObserver("screen.brightness", observerWithNext);
var lock = mozSettings.createLock();
req2 = lock.get("screen.brightness");
req2.onsuccess = function() {
ok(true, "set observeSetting done!");
next();
};
req2.onerror = onFailure;
},
function() {
ok(true, "test observers");
var lock = mozSettings.createLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set1 done");
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.createLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set2 done");
navigator.mozSettings.removeObserver("screen.brightness", observer2);
navigator.mozSettings.removeObserver("screen.brightness", observer1);
}
req.onerror = onFailure;
},
function() {
ok(true, "test Event Listener");
var lock = mozSettings.createLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set3 done");
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.createLock();
navigator.mozSettings.removeObserver("screen.brightness", observerWithNext);
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set4 done");
navigator.mozSettings.removeObserver("screen.brightness", observer2);
navigator.mozSettings.removeObserver("screen.brightness", observer1);
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.createLock();
req = lock.get("screen.brightness");
req.onsuccess = function () {
ok(true, "get5 done");
next();
}
req.onerror = onFailure;
},
function() {
ok(true, "Clear DB");
var lock = mozSettings.createLock();
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "all done!\n");
SimpleTest.finish();
}
];
function next() {
ok(true, "Begin!");
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
index += 1;
}
function permissionTest() {
if (gSettingsEnabled) {
next();
} else {
is(mozSettings, null, "mozSettings is null when not enabled.");
SimpleTest.finish();
}
}
var gSettingsEnabled = SpecialPowers.getBoolPref("dom.mozSettings.enabled");
SimpleTest.waitForExplicitFinish();
addLoadEvent(permissionTest);
ok(true, "test passed");
</script>
</pre>
</body>
</html>