Bug 1313865 - Employ common assertions in action module; r=maja_zf

MozReview-Commit-ID: 4WIc6QEjoEf

--HG--
extra : rebase_source : 2f98292d040046296279c65a8a3f6df0c452b6cc
This commit is contained in:
Andreas Tolfsen 2016-10-31 22:11:25 +00:00
Родитель e5010028a2
Коммит 1b0af3460d
2 изменённых файлов: 14 добавлений и 25 удалений

Просмотреть файл

@ -248,14 +248,16 @@ action.Action = class {
case action.PointerDown:
case action.PointerUp:
assertPositiveInteger(actionItem.button, "button");
assert.positiveInteger(actionItem.button,
error.pprint`Expected 'button' (${actionItem.button}) to be >= 0`);
item.button = actionItem.button;
break;
case action.PointerMove:
item.duration = actionItem.duration;
if (typeof item.duration != "undefined"){
assertPositiveInteger(item.duration, "duration");
assert.positiveInteger(item.duration,
error.pprint`Expected 'duration' (${item.duration}) to be >= 0`);
}
if (typeof actionItem.element != "undefined" &&
!element.isWebElementReference(actionItem.element)) {
@ -267,11 +269,11 @@ action.Action = class {
item.x = actionItem.x;
if (typeof item.x != "undefined") {
assertPositiveInteger(item.x, "x");
assert.positiveInteger(item.x, error.pprint`Expected 'x' (${item.x}) to be >= 0`);
}
item.y = actionItem.y;
if (typeof item.y != "undefined") {
assertPositiveInteger(item.y, "y");
assert.positiveInteger(item.y, error.pprint`Expected 'y' (${item.y}) to be >= 0`);
}
break;
@ -282,7 +284,8 @@ action.Action = class {
case action.Pause:
item.duration = actionItem.duration;
if (typeof item.duration != "undefined") {
assertPositiveInteger(item.duration, "duration");
assert.positiveInteger(item.duration,
error.pprint`Expected 'duration' (${item.duration}) to be >= 0`);
}
break;
}
@ -390,7 +393,7 @@ action.Sequence = class extends Array {
action.PointerParameters = class {
constructor(pointerType = "mouse", primary = true) {
this.pointerType = action.PointerType.get(pointerType);
assertBoolean(primary, "primary");
assert.boolean(primary);
this.primary = primary;
};
@ -441,20 +444,6 @@ action.processPointerAction = function processPointerAction(id, pointerParams, a
};
// helpers
function assertPositiveInteger(value, name = undefined) {
let suffix = name ? ` (${name})` : '';
if (!Number.isInteger(value) || value < 0) {
throw new InvalidArgumentError(`Expected integer >= 0${suffix}, got: ${value}`);
}
}
function assertBoolean(value, name = undefined) {
let suffix = name ? ` (${name})` : '';
if (typeof(value) != "boolean") {
throw new InvalidArgumentError(`Expected boolean${suffix}, got: ${value}`);
}
}
function capitalize(str) {
if (typeof str != "string") {
throw new InvalidArgumentError(`Expected string, got: ${str}`);

Просмотреть файл

@ -18,6 +18,7 @@ add_test(function test_createAction() {
Assert.throws(
() => new action.Action(1, 2, "sometype"), /Expected string/, "Non-string arguments.");
ok(new action.Action("id", "sometype", "sometype"));
run_next_test();
});
@ -37,8 +38,7 @@ add_test(function test_processPointerParameters() {
check(/Unknown pointerType/, message, parametersData);
parametersData.pointerType = "pen";
parametersData.primary = "a";
check(/Expected boolean \(primary\)/, message, parametersData);
check(/Expected \[object String\] "a" to be boolean/, message, parametersData);
parametersData.primary = false;
deepEqual(action.PointerParameters.fromJson(parametersData),
{pointerType: action.PointerType.Pen, primary: false});
@ -52,7 +52,7 @@ add_test(function test_processPointerUpDownAction() {
for (let d of [-1, "a"]) {
actionItem.button = d;
checkErrors(
/integer >= 0/, action.Action.fromJson, [actionSequence, actionItem],
/Expected 'button' \(.*\) to be >= 0/, action.Action.fromJson, [actionSequence, actionItem],
`button: ${actionItem.button}`);
}
actionItem.button = 5;
@ -69,7 +69,7 @@ add_test(function test_validateActionDurationAndCoordinates() {
message = message || `duration: ${actionItem.duration}, subtype: ${subtype}`;
actionItem.type = subtype;
actionSequence.type = type;
checkErrors(/integer >= 0/,
checkErrors(/Expected '.*' \(.*\) to be >= 0/,
action.Action.fromJson, [actionSequence, actionItem], message);
};
for (let d of [-1, "a"]) {
@ -97,7 +97,7 @@ add_test(function test_processPointerMoveActionElementValidation() {
[actionSequence, actionItem],
`actionItem.element: (${getTypeString(d)})`);
}
actionItem.element = {[element.Key]:"something"};
actionItem.element = {[element.Key]: "something"};
let a = action.Action.fromJson(actionSequence, actionItem);
deepEqual(a.element, actionItem.element);