Bug 1313865 - Employ common assertions in action module; r?maja_zf draft
authorAndreas Tolfsen <ato@mozilla.com>
Mon, 31 Oct 2016 22:11:25 +0000
changeset 437155 2a4e1b3f3a669e8d11a0e49b2d3973f411a9c780
parent 437154 20faa46f2c43fbc2fa8b9a31c5e247acd6555097
child 437156 6ea956317651e4d137dcd90d0b6d258aaa92f99e
push id35357
push userbmo:ato@mozilla.com
push dateThu, 10 Nov 2016 15:56:31 +0000
reviewersmaja_zf
bugs1313865
milestone52.0a1
Bug 1313865 - Employ common assertions in action module; r?maja_zf MozReview-Commit-ID: 4WIc6QEjoEf
testing/marionette/action.js
testing/marionette/test_action.js
--- a/testing/marionette/action.js
+++ b/testing/marionette/action.js
@@ -243,51 +243,54 @@ action.Action = class {
           throw new InvalidArgumentError("Expected 'key' to be a single-character string, " +
                                          "got: " + key);
         }
         item.value = key;
         break;
 
       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)) {
           throw new InvalidArgumentError(
               "Expected 'actionItem.element' to be a web element reference, " +
               `got: ${actionItem.element}`);
         }
         item.element = actionItem.element;
 
         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;
 
       case action.PointerCancel:
         throw new UnsupportedOperationError();
         break;
 
       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;
     }
 
     return item;
   }
 };
 
@@ -385,17 +388,17 @@ action.Sequence = class extends Array {
  *     Type of pointing device. If the parameter is undefined, "mouse" is used.
  * @param {boolean=} primary
  *     Whether the input source is the primary pointing device.
  *     If the parameter is underfined, true is used.
  */
 action.PointerParameters = class {
   constructor(pointerType = "mouse", primary = true) {
     this.pointerType = action.PointerType.get(pointerType);
-    assertBoolean(primary, "primary");
+    assert.boolean(primary);
     this.primary = primary;
   };
 
   toString() {
     return `[pointerParameters ${this.pointerType}, primary=${this.primary}]`;
   }
 
   /**
@@ -436,28 +439,14 @@ action.processPointerAction = function p
         `Expected 'id' ${id} to be mapped to InputState whose subtype is ` +
         `${action.inputStateMap.get(id).subtype}, got: ${subtype}`);
   }
   act.pointerType = pointerParams.pointerType;
   act.primary = pointerParams.primary;
 };
 
 // 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}`);
   }
   return str.charAt(0).toUpperCase() + str.slice(1);
 }
--- a/testing/marionette/test_action.js
+++ b/testing/marionette/test_action.js
@@ -13,16 +13,17 @@ Cu.import("chrome://marionette/content/e
 add_test(function test_createAction() {
   Assert.throws(() => new action.Action(), InvalidArgumentError,
       "Missing Action constructor args");
   Assert.throws(() => new action.Action(1,2), InvalidArgumentError,
       "Missing Action constructor args");
   Assert.throws(
       () => new action.Action(1, 2, "sometype"), /Expected string/, "Non-string arguments.");
   ok(new action.Action("id", "sometype", "sometype"));
+
   run_next_test();
 });
 
 add_test(function test_defaultPointerParameters() {
   let defaultParameters = {pointerType: action.PointerType.Mouse, primary: true};
   deepEqual(action.PointerParameters.fromJson(), defaultParameters);
 
   run_next_test();
@@ -32,49 +33,48 @@ add_test(function test_processPointerPar
   let check = (regex, message, arg) => checkErrors(
       regex, action.PointerParameters.fromJson, [arg], message);
   let parametersData = {pointerType: "foo"};
   let message = `parametersData: [pointerType: ${parametersData.pointerType}, ` +
       `primary: ${parametersData.primary}]`;
   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});
 
   run_next_test();
 });
 
 add_test(function test_processPointerUpDownAction() {
   let actionItem = {type: "pointerDown"};
   let actionSequence = {type: "pointer", id: "some_id"};
   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;
   let act = action.Action.fromJson(actionSequence, actionItem);
   equal(act.button, actionItem.button);
 
   run_next_test();
 });
 
 add_test(function test_validateActionDurationAndCoordinates() {
   let actionItem = {};
   let actionSequence = {id: "some_id"};
   let check = function(type, subtype, message = undefined) {
     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"]) {
     actionItem.duration = d;
     check("none", "pause");
     check("pointer", "pointerMove");
   }
   actionItem.duration = 5;
@@ -92,17 +92,17 @@ add_test(function test_processPointerMov
   let actionItem = {duration: 5, type: "pointerMove"};
   for (let d of [-1, "a", {a: "blah"}]) {
     actionItem.element = d;
     checkErrors(/Expected 'actionItem.element' to be a web element reference/,
         action.Action.fromJson,
         [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);
 
   run_next_test();
 });
 
 add_test(function test_processPointerMoveAction() {
   let actionSequence = {id: "some_id", type: "pointer"};