Bug 900551 - Mochitests for settings permissions; r=bent r=ehsan

This commit is contained in:
Kyle Machulis 2014-08-19 13:43:57 -07:00
parent 7a86d08564
commit 360a7a2be2
11 changed files with 258 additions and 22 deletions

View File

@ -277,6 +277,10 @@ const kEventConstructors = {
return new MozSettingsEvent(aName, aProps);
},
},
MozSettingsTransactionEvent: { create: function (aName, aProps) {
return new MozSettingsTransactionEvent(aName, aProps);
},
},
MozSmsEvent: { create: function (aName, aProps) {
return new MozSmsEvent(aName, aProps);
},

View File

@ -2,6 +2,7 @@
skip-if = (toolkit == 'gonk' && debug) || e10s #debug-only failure, bug 932878
[test_settings_basics.html]
[test_settings_permissions.html]
[test_settings_blobs.html]
[test_settings_data_uris.html]
[test_settings_events.html]

View File

@ -29,6 +29,7 @@ SpecialPowers.addPermission("settings-api-read", true, document);
SpecialPowers.addPermission("settings-api-write", true, document);
SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-write", true, document);
SpecialPowers.addPermission("settings-clear", true, document);
function onUnwantedSuccess() {
ok(false, "onUnwantedSuccess: shouldn't get here");
@ -326,8 +327,11 @@ var steps = [
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req.onerror = onFailure;
},
function () {
var lock = mozSettings.createLock();
req2 = lock.set(wifi);
req2.onsuccess = function () {
ok(true, "set done");
@ -336,23 +340,23 @@ var steps = [
ok(true, "Get all settings");
var lock2 = mozSettings.createLock();
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));
req3 = lock2.get("*");
req3.onsuccess = function () {
is(Object.keys(req3.result).length, 1, "length 1");
check(req3.result, wifi);
ok(true, JSON.stringify(req3.result));
ok(true, "Get all settings Done");
};
req.onerror = onFailure;
req3.onerror = onFailure;
req2 = lock2.get("net3g.apn");
req2.onsuccess = function () {
is(Object.keys(req2.result).length, 1, "length 1");
check(wifi, req2.result);
req4 = lock2.get("net3g.apn");
req4.onsuccess = function () {
is(Object.keys(req4.result).length, 1, "length 1");
check(wifi, req4.result);
ok(true, "Get net3g.apn Done");
next();
};
req2.onerror = onFailure;
req4.onerror = onFailure;
},
function () {
ok(true, "Change wifi1");
@ -469,8 +473,8 @@ var steps = [
req5.onerror = onFailure;
}
var lock5 = mozSettings.createLock();
req6 = lock5.clear();
var lock6 = mozSettings.createLock();
req6 = lock6.clear();
req6.onsuccess = function () {
ok(true, "Deleted the database");
next();
@ -506,13 +510,15 @@ var steps = [
}
req.onerror = onFailure;
req2 = lock.clear();
var lock2 = mozSettings.createLock();
req2 = lock2.clear();
req2.onsuccess = function () {
ok(true, "Deleted the database");
};
req2.onerror = onFailure;
req3 = lock.set(wifi);
var lock3 = mozSettings.createLock();
req3 = lock3.set(wifi);
req3.onsuccess = function () {
ok(true, "set done");
next();
@ -766,7 +772,7 @@ var steps = [
function () {
ok(true, "Call success callback when transaction commits");
var lock = mozSettings.createLock();
lock.ontransactionsuccess = function () {
lock.onsettingstransactionsuccess = function () {
next();
};
req = lock.set({"setting-obj": {foo: {bar: 23}}});

View File

@ -29,6 +29,7 @@ SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-write", true, document);
SpecialPowers.addPermission("settings-api-read", true, document);
SpecialPowers.addPermission("settings-api-write", true, document);
SpecialPowers.addPermission("settings-clear", true, document);
function onUnwantedSuccess() {
ok(false, "onUnwantedSuccess: shouldn't get here");

View File

@ -29,6 +29,7 @@ SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-write", true, document);
SpecialPowers.addPermission("settings-api-read", true, document);
SpecialPowers.addPermission("settings-api-write", true, document);
SpecialPowers.addPermission("settings-clear", true, document);
function onUnwantedSuccess() {
ok(false, "onUnwantedSuccess: shouldn't get here");

View File

@ -32,8 +32,16 @@ e = new MozSettingsEvent("settingchanged", {settingName: "a", settingValue: true
is(e.settingName, "a", "Name should be a.");
is(e.settingValue, true, "Value should be true.");
var e = new MozSettingsTransactionEvent("settingtransactionsuccess", {});
ok(e, "Should have settings event!");
is(e.error, "", "error should be null");
var e = new MozSettingsTransactionEvent("settingtransactionfailure", {error: "Test error."});
ok(e, "Should have settings event!");
is(e.error, "Test error.", "error should be 'Test error.'");
</script>
</pre>
</body>
</html>
</html>

View File

@ -34,7 +34,8 @@ SpecialPowers.pushPermissions([
{type: "settings-read", allow: 0, context: document},
{type: "settings-write", allow: 0, context: document},
{type: "settings-api-read", allow: 0, context: document},
{type: "settings-api-write", allow: 0, context: document}
{type: "settings-api-write", allow: 0, context: document},
{type: "settings-clear", allow: 0, context: document}
], function() {
ise(frames[0].navigator.mozSettings, null, "navigator.mozSettings is null when the page doesn't have permissions");
testPref();

View File

@ -29,6 +29,7 @@ SpecialPowers.addPermission("settings-write", true, document);
SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-api-read", true, document);
SpecialPowers.addPermission("settings-api-write", true, document);
SpecialPowers.addPermission("settings-clear", true, document);
var screenBright = {"screen.brightness": 0.7};
@ -91,8 +92,12 @@ var steps = [
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req.onerror = onFailure;
},
function () {
var lock = mozSettings.createLock();
req2 = lock.set(screenBright);
req2.onsuccess = function () {
ok(true, "set done");
@ -269,16 +274,19 @@ var steps = [
req = lock.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
navigator.mozSettings.onsettingchange = onComplexSettingschangeWithNext;
next();
};
req.onerror = onFailure;
},
function () {
var lock = mozSettings.createLock();
navigator.mozSettings.onsettingchange = onComplexSettingschangeWithNext;
req2 = navigator.mozSettings.createLock().set({'test.key': cset});
req2.onsuccess = function () {
ok(true, "set done");
}
req2.onerror = onFailure;
},
function () {
ok(true, "all done!\n");
SimpleTest.finish();

View File

@ -0,0 +1,203 @@
<!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={900551}">Mozilla Bug {900551}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
if (SpecialPowers.isMainProcess()) {
SpecialPowers.Cu.import("resource://gre/modules/SettingsRequestManager.jsm");
}
SpecialPowers.removePermission("settings-read", document);
SpecialPowers.removePermission("settings-write", document);
SpecialPowers.addPermission("settings-api-read", true, document);
SpecialPowers.addPermission("settings-api-write", true, document);
SpecialPowers.addPermission("settings:wallpaper.image-read", true, document);
SpecialPowers.addPermission("settings:wallpaper.image-write", true, document);
SpecialPowers.addPermission("settings-clear", true, document);
function onUnwantedSuccess() {
ok(false, "onUnwantedSuccess: shouldn't get here");
}
function onFailure() {
ok(false, "in on Failure!");
}
const wifi = {"wifi.enabled": false}
const wallpaper = {"wallpaper.image": "test-image"};
var combination = {
"wifi.enabled": false,
"wallpaper.image": "test-image"
}
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 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 = navigator.mozSettings;
var steps = [
// Can't delete database here since that requires permissions we don't want
// to give the page.
function () {
ok(true, "Setting wallpaper");
var lock = mozSettings.createLock();
req = lock.set(wallpaper);
req.onsuccess = function () {
ok(true, "set done");
}
req.onerror = onFailure;
var lock2 = mozSettings.createLock();
req2 = lock2.get("wallpaper.image");
req2.onsuccess = function () {
is(Object.keys(req2.result).length, 1, "length 1");
check(wallpaper, req2.result);
ok(true, "Get wallpaper Done");
next();
};
req2.onerror = onFailure;
},
function () {
ok(true, "Get Wifi");
var lock = mozSettings.createLock();
req = lock.get("wifi.enabled");
req.onerror = function () {
ok(true, "get failed (expected)");
next();
}
req.onsuccess = onFailure;
},
function () {
ok(true, "Set Wifi");
var lock = mozSettings.createLock();
req = lock.set(wifi);
req.onerror = function () {
ok(true, "set failed (expected)");
next();
}
req.onsuccess = onFailure;
},
function () {
ok(true, "Set combination (1 valid 1 not valid)");
var lock = mozSettings.createLock();
req = lock.set(combination);
req.onerror = function () {
ok(true, "set failed (expected)");
next();
}
req.onsuccess = onFailure;
},
function () {
ok(true, "All requests on a failed lock should fail");
var lock = mozSettings.createLock();
lock.onsettingstransactionfailure = function (evt) {
ok(evt.error == "Lock failed a permissions check, all requests now failing.", "transaction failure on permissions error message correct.");
ok(true, "transaction failed (expected) ");
next();
};
lock.onsettingstransactionsuccess = onFailure;
req = lock.set(wifi);
req.onerror = function () {
ok(true, "set failed (expected)");
}
req.onsuccess = onFailure;
req2 = lock.get("wallpaper.image");
req2.onerror = function () {
ok(true, "get failed (expected)");
}
req2.onsuccess = onFailure;
},
function () {
ok(true, "Set combination (1 valid 1 not valid)");
var lock = mozSettings.createLock();
req = lock.set(combination);
req.onerror = function () {
ok(true, "set failed (expected)");
next();
}
req.onsuccess = onFailure;
},
function () {
ok(true, "Set combination (1 valid 1 not valid)");
var lock = mozSettings.createLock();
req = lock.set(combination);
req.onerror = function () {
ok(true, "set failed (expected)");
next();
}
req.onsuccess = 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;
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(next);
</script>
</pre>
</body>
</html>

View File

@ -706,6 +706,8 @@ var interfaceNamesInGlobalScope =
{name: "mozRTCSessionDescription", pref: "media.peerconnection.enabled"},
// IMPORTANT: Do not change this list without review from a DOM peer!
"MozSettingsEvent",
// IMPORTANT: Do not change this list without review from a DOM peer!
{name: "MozSettingsTransactionEvent", permission: "settings-api-read"},
// IMPORTANT: Do not change this list without review from a DOM peer!
"MozSmsEvent",
// IMPORTANT: Do not change this list without review from a DOM peer!

View File

@ -4,7 +4,8 @@
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
[Constructor(DOMString type, optional MozSettingsTransactionEventInit eventInitDict)]
[Constructor(DOMString type, optional MozSettingsTransactionEventInit eventInitDict),
CheckPermissions="settings-api-read settings-api-write"]
interface MozSettingsTransactionEvent : Event
{
readonly attribute DOMString? error;