2012-09-26 14:10:32 -07:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<!--
|
2013-04-16 05:01:13 -07:00
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=678695
|
2012-09-26 14:10:32 -07:00
|
|
|
-->
|
|
|
|
<head>
|
2013-04-16 05:01:13 -07:00
|
|
|
<title>Test for Bug 678695 Settings API</title>
|
2012-09-26 14:10:32 -07:00
|
|
|
<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>
|
|
|
|
|
2013-04-16 05:01:13 -07:00
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=678695">Mozilla Bug 678695</a>
|
2012-09-26 14:10:32 -07:00
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript">
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-03-26 16:49:12 -07:00
|
|
|
if (!SpecialPowers.getBoolPref("dom.mozSettings.enabled")) {
|
|
|
|
SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm");
|
|
|
|
SpecialPowers.setBoolPref("dom.mozSettings.enabled", true);
|
|
|
|
}
|
|
|
|
|
2012-10-31 17:26:05 -07:00
|
|
|
SpecialPowers.addPermission("settings-write", true, document);
|
|
|
|
SpecialPowers.addPermission("settings-read", true, document);
|
2012-09-26 14:10:32 -07:00
|
|
|
|
2013-04-16 05:01:13 -07:00
|
|
|
function tests() {
|
|
|
|
|
2012-09-26 14:10:32 -07:00
|
|
|
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;
|
|
|
|
},
|
2013-03-26 16:49:12 -07:00
|
|
|
|
2012-09-26 14:10:32 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 05:01:13 -07:00
|
|
|
permissionTest();
|
|
|
|
}
|
|
|
|
|
2012-09-26 14:10:32 -07:00
|
|
|
var gSettingsEnabled = SpecialPowers.getBoolPref("dom.mozSettings.enabled");
|
2013-04-16 05:01:13 -07:00
|
|
|
addLoadEvent(tests);
|
2012-09-26 14:10:32 -07:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
ok(true, "test passed");
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|