gecko/dom/settings/tests/test_settings_basics.html

652 lines
18 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";
var comp = SpecialPowers.wrap(Components);
comp.utils.import("resource://gre/modules/SettingsChangeNotifier.jsm");
SpecialPowers.setBoolPref("dom.mozSettings.enabled", true);
SpecialPowers.addPermission("websettings-read", true, document);
SpecialPowers.addPermission("websettings-readwrite", true, document);
function onUnwantedSuccess() {
ok(false, "onUnwantedSuccess: shouldn't get here");
}
function onFailure() {
ok(false, "in on Failure!");
}
var wifi = {"net3g.apn": "internet.mnc012.mcc345.gprs"};
var wifi2 = {"net3g.apn": "internet.mnc012.mcc345.test"};
var wifiEnabled = {"wifi.enabled": true};
var wifiDisabled = {"wifi.enabled": false};
var screenBright = {"screen.brightness": 0.7};
var wifiNetworks0 = { "wifi.networks[0]": { ssid: "myfreenetwork", mac: "01:23:45:67:89:ab", passwd: "secret"}};
var wifiNetworks1 = { "wifi.networks[1]": { ssid: "myfreenetwork2", mac: "01:23:45:67:89:ab", passwd: "secret2"}};
function equals(o1, o2) {
var k1 = Object.keys(o1).sort();
var k2 = Object.keys(o2).sort();
if (k1.length != k2.length) return false;
return k1.zip(k2, function(keyPair) {
if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){
return equals(o1[keyPair[0]], o2[keyPair[1]])
} else {
return o1[keyPair[0]] == o2[keyPair[1]];
}
}).all();
};
function observer1(setting) {
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
};
function observer2(setting) {
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
};
function observerWithNext(setting) {
is(setting.settingName, "screen.brightness", "Same settingName");
is(setting.settingValue, "0.7", "Same settingvalue");
next();
};
function onsettingschangeWithNext(event) {
is(event.settingName, "screen.brightness", "Same settingName");
is(event.settingValue, "0.7", "Same settingvalue");
next();
};
function check(o1, o2) {
is(JSON.stringify(o1), JSON.stringify(o2), "same");
}
var req, req2, req3, req4, req5, req6;
var index = 0;
var mozSettings = window.navigator.mozSettings;
var steps = [
function () {
ok(true, "Deleting database");
var lock = mozSettings.getLock();
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");
next();
}
req2.onerror = onFailure;
},
function() {
ok(true, "Get unknown key");
var lock = mozSettings.getLock();
req = lock.get("abc.def");
req.onsuccess = function() {
is(req.result["abc.def"], undefined, "no result");
next();
};
req.onerror = onFailure;
},
function() {
ok(true, "adding onsettingchange");
navigator.mozSettings.onsettingchange = onsettingschangeWithNext;
var lock = mozSettings.getLock();
req2 = lock.get("screen.brightness");
req2.onsuccess = function() {
ok(true, "end adding onsettingchange");
next();
};
req2.onerror = onFailure;
},
function() {
ok(true, "Test onsettingchange");
var lock = mozSettings.getLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set done, observer has to call next");
}
req.onerror = onFailure;
},
function() {
ok(true, "delete onsettingschange");
var lock = mozSettings.getLock();
navigator.mozSettings.onsettingchange = null;
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set done");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Waiting for all set callbacks");
var lock = mozSettings.getLock();
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.getLock();
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.getLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set done");
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.getLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set 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.getLock();
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set done");
}
req.onerror = onFailure;
},
function() {
ok(true, "removing Event Listener");
var lock = mozSettings.getLock();
navigator.mozSettings.removeObserver("screen.brightness", observerWithNext);
req = lock.set(screenBright);
req.onsuccess = function () {
ok(true, "set 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.getLock();
req = lock.get("screen.brightness");
req.onsuccess = function () {
ok(true, "get done");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Nested test");
var lock = mozSettings.getLock();
req = lock.get("screen.brightness");
req.onsuccess = function () {
req3 = lock.set({"screen.brightness": req.result["screen.brightness"] + 1})
req3.onsuccess = function () {
req4 = lock.get("screen.brightness");
req4.onsuccess = function() {
is(req4.result["screen.brightness"], 1.7, "same Value");
}
req4.onerror = onFailure;
}
req3.onerror = onFailure;
};
req.onerror = onFailure;
req2 = lock.get("screen.brightness");
req2.onsuccess = function () {
is(req2.result["screen.brightness"], 0.7, "same Value");
}
req2.onerror = onFailure;
var lock2 = mozSettings.getLock();
req5 = lock2.get("screen.brightness");
req5.onsuccess = function () {
is(req5.result["screen.brightness"], 1.7, "same Value");
next();
}
req5.onerror = onFailure;
},
function () {
ok(true, "Deleting database");
var lock = mozSettings.getLock();
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
};
req.onerror = onFailure;
req2 = lock.set(wifi);
req2.onsuccess = function () {
ok(true, "set done");
}
req2.onerror = onFailure;
ok(true, "Get all settings");
var lock2 = mozSettings.getLock();
req = lock2.get("*");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifi, req.result);
ok(true, JSON.stringify(req.result));
ok(true, "Get all settings Done");
};
req.onerror = onFailure;
req2 = lock2.get("net3g.apn");
req2.onsuccess = function () {
is(Object.keys(req2.result).length, 1, "length 1");
check(wifi, req2.result);
ok(true, "Get net3g.apn Done");
next();
};
req2.onerror = onFailure;
},
function () {
ok(true, "Change wifi");
var lock = mozSettings.getLock();
req = lock.set(wifi2);
req.onsuccess = function () {
ok(true, "Set Done");
};
req.onerror = onFailure;
ok(true, "Get changed net3g.apn");
req2 = lock.get("net3g.apn");
req2.onsuccess = function () {
is(Object.keys(req2.result).length, 1, "length 1");
check(wifi2, req2.result);
ok(true, "Get net3g.apn Done");
next();
};
req2.onerror = onFailure;
},
function () {
ok(true, "Test locking");
var lock = mozSettings.getLock();
var lock2 = mozSettings.getLock();
req = lock.set(wifiEnabled);
req.onsuccess = function () {
ok(true, "Test Locking Done");
};
req.onerror = onFailure;
req2 = lock2.set(wifiDisabled);
req2.onsuccess = function () {
ok(true, "Set Done");
next();
};
req2.onerror = onFailure;
},
function () {
ok(true, "Test locking result");
var lock = mozSettings.getLock();
req = lock.get("wifi.enabled");
req.onsuccess = function() {
check(req.result, wifiDisabled);
ok(true, "Test1 locking result done");
next();
}
req.onerror = onFailure;
},
function () {
ok(true, "Test locking heavy");
for (var i=0; i<30; i++) {
// only new locks!
var lock = mozSettings.getLock();
var obj = {};
obj["wifi.enabled" + i] = true;
req = lock.set( obj );
req.onsuccess = function () {
ok(true, "Set1 Done");
};
req.onerror = onFailure;
};
{
var lock2 = mozSettings.getLock();
req = lock2.get("*");
req.onsuccess = function () {
is(Object.keys(req.result).length, 32, "length 12");
ok(true, JSON.stringify(req.result));
ok(true, "Get all settings Done");
};
req.onerror = onFailure;
}
var lock2 = mozSettings.getLock();
var obj = {};
obj["wifi.enabled" + 30] = true;
req2 = lock2.set( obj );
req2.onsuccess = function () {
ok(true, "Set12 Done");
};
req2.onerror = onFailure;
var lock3 = mozSettings.getLock();
// with one lock
for (var i = 0; i < 30; i++) {
req3 = lock3.get("wifi.enabled" + i);
var testObj = {};
testObj["wifi.enabled" + i] = true;
req3.onsuccess = function () {
check(this.request.result, this.testObj);
ok(true, "Get1 Done");
}.bind({testObj: testObj, request: req3});
req3.onerror = onFailure;
}
ok(true, "start next2!");
var lock4 = mozSettings.getLock();
for (var i=0; i<30; i++) {
var obj = {};
obj["wifi.enabled" + i] = false;
req4 = lock4.set( obj );
req4.onsuccess = function () {
ok(true, "Set2 Done");
};
req4.onerror = onFailure;
}
var lock5 = mozSettings.getLock();
for (var i=0; i<30; i++) {
req5 = lock5.get("wifi.enabled" + i);
var testObj = {};
testObj["wifi.enabled" + i] = false;
req5.onsuccess = function () {
check(this.request.result, this.testObj);
ok(true, "Get2 Done");
}.bind({testObj: testObj, request: req5});
req5.onerror = onFailure;
}
var lock5 = mozSettings.getLock();
req6 = lock5.clear();
req6.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req6.onerror = onFailure;
},
function () {
ok(true, "reverse Test locking");
var lock2 = mozSettings.getLock();
var lock = mozSettings.getLock();
req = lock.set(wifiEnabled);
req.onsuccess = function () {
ok(true, "Test Locking Done");
next();
};
req.onerror = onFailure;
req2 = lock2.set(wifiDisabled);
req2.onsuccess = function () {
ok(true, "Set Done");
};
req2.onerror = onFailure;
},
function () {
ok(true, "Test locking result");
var lock = mozSettings.getLock();
req = lock.get("wifi.enabled");
req.onsuccess = function() {
check(req.result, wifiEnabled);
ok(true, "Test2 locking result done");
}
req.onerror = onFailure;
req2 = lock.clear();
req2.onsuccess = function () {
ok(true, "Deleted the database");
};
req2.onerror = onFailure;
req3 = lock.set(wifi);
req3.onsuccess = function () {
ok(true, "set done");
next();
}
req3.onerror = onFailure;
},
function () {
ok(true, "Get all settings");
var lock = mozSettings.getLock();
req = lock.get("*");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifi, req.result);
ok(true, "Get all settings Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Get net3g.apn");
var lock = mozSettings.getLock();
req = lock.get("net3g.apn");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifi, req.result);
ok(true, "Get net3g.apn Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Change wifi");
var lock = mozSettings.getLock();
req = lock.set(wifi2);
req.onsuccess = function () {
ok(true, "Set Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Get net3g.apn");
var lock = mozSettings.getLock();
req = lock.get("net3g.apn");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifi2, req.result);
ok(true, "Get net3g.apn Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Add wifi.enabled");
var lock = mozSettings.getLock();
req = lock.set(wifiEnabled);
req.onsuccess = function () {
ok(true, "Set Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Get Wifi Enabled");
var lock = mozSettings.getLock();
req = lock.get("wifi.enabled");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifiEnabled, req.result);
ok(true, "Get wifi.enabledDone");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Get all");
var lock = mozSettings.getLock();
req = lock.get("*");
req.onsuccess = function () {
is(Object.keys(req.result).length, 2, "length 2");
check(wifiEnabled["wifi.enabled"], req.result["wifi.enabled"]);
check(wifi2["net3g.apn"], req.result["net3g.apn"]);
ok(true, "Get all Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Add wifiNetworks");
var lock = mozSettings.getLock();
req = lock.set(wifiNetworks0);
req.onsuccess = function () {
ok(true, "Set Done");
};
req.onerror = onFailure;
req2 = lock.set(wifiNetworks1);
req2.onsuccess = function () {
ok(true, "Set Done");
next();
};
req2.onerror = onFailure;
},
function () {
ok(true, "Get Wifi Networks");
var lock = mozSettings.getLock();
req = lock.get("wifi.networks[0]");
req.onsuccess = function () {
is(Object.keys(req.result).length, 1, "length 1");
check(wifiNetworks0, req.result);
ok(true, "Get wifi.networks[0]");
next();
};
req.onerror = onFailure;
},
function() {
ok(true, "Clear DB, multiple locks");
var lock4 = mozSettings.getLock();
var lock3 = mozSettings.getLock();
var lock2 = mozSettings.getLock();
var lock = mozSettings.getLock();
var lock6 = mozSettings.getLock();
var lock7 = mozSettings.getLock();
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Add wifiNetworks");
var lock = mozSettings.getLock();
req = lock.set(wifiNetworks0);
req.onsuccess = function () {
ok(true, "Set Done");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Test set after lock closed");
var lockx = mozSettings.getLock();
var cb = function() {
var reqx = null;
try {
reqx = lockx.set(wifiNetworks0);
ok(false, "should have thrown");
} catch (ex) {
ok(reqx == null, "request is still null");
ok(true, "Caught Exception");
next();
}
}
SimpleTest.executeSoon(cb);
},
function() {
ok(true, "Clear DB");
var lock = mozSettings.getLock();
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>